diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 02:04:50 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | e8662178dba9d779cbfaa290154ae78c61402d30 (patch) | |
| tree | 0c596949f7e31377765f1193461f72fd15164255 | |
| parent | 30a603341e3b23da6e5ab5995b060c8e4ec52242 (diff) | |
| download | pardes-e8662178dba9d779cbfaa290154ae78c61402d30.tar.gz pardes-e8662178dba9d779cbfaa290154ae78c61402d30.zip | |
gui: the renderer's shaders, instance ABI and GPU resource helpers are sketchbook's (src/gpu/), moved as they were, the first step of the GPU move: the seven GLSL shaders and their prebuilt SPIR-V leave shaders/ (crt.ci.metal stays), and CellInstance, OverlayVertex, ImageInstance, opaque_bg_bit, the vertex inputs, makePipeline, backgroundLayerBlend, makeShader, texture2d and growInstances (the device for the Gui) leave gui.zig for sketchbook's src/gpu/Cells.zig, built from the sketchbook checkout over gui.zig's own `c` as the Executor is (module gpu_cells; its instance-ABI test with it, run by unit-test); the embedded SPIR-V comes from sketchbook's GLSL through glslc, or its prebuilt copies with -Dprebuilt-shaders, under the same @embedFile names, byte for byte the same; the `shaders` refresh step is sketchbook's now; building.typ says so
| -rw-r--r-- | build.zig | 29 | ||||
| -rw-r--r-- | docs/typ/building.typ | 2 | ||||
| -rw-r--r-- | shaders/decor.frag.glsl | 26 | ||||
| -rw-r--r-- | shaders/image.frag.glsl | 17 | ||||
| -rw-r--r-- | shaders/image.vert.glsl | 33 | ||||
| -rw-r--r-- | shaders/overlay.frag.glsl | 17 | ||||
| -rw-r--r-- | shaders/overlay.vert.glsl | 11 | ||||
| -rw-r--r-- | shaders/prebuilt/decor.frag.spv | bin | 2176 -> 0 bytes | |||
| -rw-r--r-- | shaders/prebuilt/image.frag.spv | bin | 1344 -> 0 bytes | |||
| -rw-r--r-- | shaders/prebuilt/image.vert.spv | bin | 3152 -> 0 bytes | |||
| -rw-r--r-- | shaders/prebuilt/overlay.frag.spv | bin | 1008 -> 0 bytes | |||
| -rw-r--r-- | shaders/prebuilt/overlay.vert.spv | bin | 1040 -> 0 bytes | |||
| -rw-r--r-- | shaders/prebuilt/ui.frag.spv | bin | 3184 -> 0 bytes | |||
| -rw-r--r-- | shaders/prebuilt/ui.vert.spv | bin | 3848 -> 0 bytes | |||
| -rw-r--r-- | shaders/ui.frag.glsl | 39 | ||||
| -rw-r--r-- | shaders/ui.vert.glsl | 53 | ||||
| -rw-r--r-- | src/gui/gui.zig | 199 |
17 files changed, 33 insertions, 393 deletions
@@ -161,18 +161,12 @@ pub fn build(b: *std.Build) void { const tracy = b.option([]const u8, "tracy", "enable Tracy profiling; supply the path to a Tracy source checkout"); const macos_sdk = b.option([]const u8, "macos-sdk", "the macOS SDK a gui built for macOS from another system takes SDL's headers and frameworks from"); const macos_identity = b.option([]const u8, "macos-identity", "codesigning identity for pardes.app (default: ad-hoc)") orelse "-"; - const prebuilt_shaders = b.option(bool, "prebuilt-shaders", "embed the committed shaders/prebuilt/*.spv instead of running glslc (default: on for a bare `zig build`, off when -Dplatform names a shell)") orelse also_gui; + const prebuilt_shaders = b.option(bool, "prebuilt-shaders", "embed sketchbook's committed src/gpu/shaders/prebuilt/*.spv instead of running glslc over its GLSL (default: on for a bare `zig build`, off when -Dplatform names a shell)") orelse also_gui; const mupdf_check = b.step("mupdf-check", "the pdf addon's check: MuPDF compiles, links, renders and searches docs/design.pdf, and its script runs"); const pdf_pointer_bench_step = b.step("pdf-pointer-bench", "time PDF hover and selection gestures and prove their pixels (-Doptimize=ReleaseFast -- [--label L] [--reps N] [--path FILE] [--cell WxH])"); const pdf_scroll_bench_step = b.step("pdf-scroll-bench", "benchmark fast continuous-strip PDF scrolling in ReleaseFast (-Doptimize=ReleaseFast -- [--json] [--reps N] [--warmup N] [--path FILE])"); - const shaders_step = b.step("shaders", "refresh the committed shaders/prebuilt/*.spv (glslc)"); - const update_shaders = b.addUpdateSourceFiles(); - for (gui_shaders) |name| { - update_shaders.addCopyFileToSource(compileGlsl(b, name), b.fmt("shaders/prebuilt/{s}.spv", .{name})); - } - shaders_step.dependOn(&update_shaders.step); // build-time IO: slurps the grammars' highlights.scm queries, and reads // vendor/themes to find the theme sources @@ -899,6 +893,8 @@ pub fn build(b: *std.Build) void { const gui_shell_mods: [2]?*std.Build.Module = .{ gui_shell_mod, if (platform == .gui) static_mod else null }; // SDL's headers, for the macOS trackpad adapter's test after the loop. var sdl_include: ?std.Build.LazyPath = null; + // sketchbook's Cells.zig's own tests (the instance ABI), for unit-test. + var cells_test: ?*std.Build.Step.Compile = null; for (gui_shell_mods) |gui_shell_entry| if (gui_shell_entry) |gm| { // SDL's build asks a cross build for macOS for the SDK's paths. const sdk: ?std.Build.LazyPath = if (macos_sdk) |p| .{ .cwd_relative = p } else null; @@ -927,6 +923,10 @@ pub fn build(b: *std.Build) void { if (b.lazyDependency("sketchbook", .{ .@"gpu-only" = true })) |sb| { const gpu = b.createModule(.{ .root_source_file = sb.path("src/gpu/Executor.zig"), .imports = &.{ .{ .name = "abi", .module = sb.module("abi") }, .{ .name = "c", .module = gui_c_mod } } }); gm.addImport("sketch_gpu", gpu); + // pardes's renderer, moved to sketchbook (src/gpu/), compiled + // against pardes's SDL; its tests run with unit-test. + gm.addImport("gpu_cells", b.createModule(.{ .root_source_file = sb.path("src/gpu/Cells.zig"), .imports = &.{.{ .name = "c", .module = gui_c_mod }} })); + if (cells_test == null) cells_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = sb.path("src/gpu/Cells.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "c", .module = gui_c_mod }} }), .filters = test_filters }); gm.addImport("gpu_scene", sb.module("gpu_scene")); } sdl_include = dep.path("include"); @@ -993,11 +993,11 @@ pub fn build(b: *std.Build) void { gm.addAnonymousImport("AdwaitaMono-Regular.ttf", .{ .root_source_file = b.path("assets/AdwaitaMono-Regular.ttf"), }); - for (gui_shaders) |name| gm.addAnonymousImport(b.fmt("{s}.spv", .{name}), .{ + if (b.lazyDependency("sketchbook", .{ .@"gpu-only" = true })) |sb| for (gui_shaders) |name| gm.addAnonymousImport(b.fmt("{s}.spv", .{name}), .{ .root_source_file = if (prebuilt_shaders) - b.path(b.fmt("shaders/prebuilt/{s}.spv", .{name})) + sb.path(b.fmt("src/gpu/shaders/prebuilt/{s}.spv", .{name})) else - compileGlsl(b, name), + compileGlsl(b, sb.path(b.fmt("src/gpu/shaders/{s}.glsl", .{name})), name), }); }; if (target.result.os.tag == .macos) if (sdl_include) |include| { @@ -1588,6 +1588,7 @@ pub fn build(b: *std.Build) void { // The perf and fs bench programs' own tests: they compile no editor. _ = units.add(b, perf_tests, null); _ = units.add(b, fs_bench_test, null); + if (cells_test) |t| _ = units.add(b, t, null); const core_test = b.addTest(.{ .root_module = core_module, .filters = test_filters }); const run_core_test = b.addRunArtifact(core_test); run_core_test.has_side_effects = true; @@ -2032,12 +2033,12 @@ fn reindexed(b: *std.Build, archive: std.Build.LazyPath, index: usize) std.Build return run.addOutputFileArg(b.fmt("{d}.a", .{index})); } -/// glslc shaders/<name>.glsl -fshader-stage=<vertex|fragment> -o <name>.spv, -/// returned as a LazyPath for @embedFile. The stage is the name's own suffix. -fn compileGlsl(b: *std.Build, name: []const u8) std.Build.LazyPath { +/// glslc <glsl> -fshader-stage=<vertex|fragment> -o <name>.spv, returned as +/// a LazyPath for @embedFile. The stage is the name's own suffix. +fn compileGlsl(b: *std.Build, glsl: std.Build.LazyPath, name: []const u8) std.Build.LazyPath { const stage = if (std.mem.endsWith(u8, name, ".vert")) "vertex" else "fragment"; const cmd = b.addSystemCommand(&.{ "glslc", b.fmt("-fshader-stage={s}", .{stage}) }); - cmd.addFileArg(b.path(b.fmt("shaders/{s}.glsl", .{name}))); + cmd.addFileArg(glsl); cmd.addArg("-o"); return cmd.addOutputFileArg(b.fmt("{s}.spv", .{name})); } diff --git a/docs/typ/building.typ b/docs/typ/building.typ index 6bf7dfce..cf80bd3a 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -65,7 +65,7 @@ must share with its session; `pardes --help` lists every flag. [`-Dtheme-animation`], [bool, `true`], [`-Dworkspace-tag`], [bool: draw the workspace tag row; on except for macOS, whose menu bar carries it], [`-Dmacos-sdk`], [path: the macOS SDK SDL's headers and frameworks come from, for a gui built for macOS elsewhere], - [`-Dprebuilt-shaders`], [bool: embed the committed SPIR-V; on for a bare `zig build`, off with `-Dplatform`; `zig build shaders` refreshes it with glslc], + [`-Dprebuilt-shaders`], [bool: embed the committed SPIR-V; on for a bare `zig build`, off with `-Dplatform`; the shaders are sketchbook's (`src/gpu/shaders/`), and its `zig build shaders` refreshes the SPIR-V with glslc], [`-Dtracy`], [path to a Tracy checkout; off], [`-Dmacos-identity`], [codesigning identity for `pardes.app`; `-` (ad hoc)], [`-Dtest-filter`], [run only tests whose name contains it; a filter that matches nothing fails], diff --git a/shaders/decor.frag.glsl b/shaders/decor.frag.glsl deleted file mode 100644 index 71bb12e3..00000000 --- a/shaders/decor.frag.glsl +++ /dev/null @@ -1,26 +0,0 @@ -#version 450 - -// Decor, drawn by ui.vert.glsl like a cell, and blended as the overlay -// pipeline blends (backgroundLayerBlend). Ink (a cursor) is drawn with the -// opacity uniform at 1. -// -// A hard-edged fill (a rule, a rail, a grip mark, a bar cursor): its colour -// rides in the cell's ground (a_bg), its coverage in a_fg.r, so the result -// is the overlay pipeline's exactly. - -layout(std140, set = 3, binding = 0) uniform UiUniforms { - float background_opacity; -} u_ui; - -layout(location = 1) in vec3 v_fg; -layout(location = 2) in vec3 v_bg; -layout(location = 5) flat in vec4 v_clip; - -layout(location = 0) out vec4 o_col; - -void main() { - if (v_clip.z >= v_clip.x && (gl_FragCoord.x < v_clip.x || gl_FragCoord.x >= v_clip.z || - gl_FragCoord.y < v_clip.y || gl_FragCoord.y >= v_clip.w)) discard; - float opacity = clamp(u_ui.background_opacity, 0.0, 1.0); - o_col = vec4(v_bg * opacity, v_fg.r); -} diff --git a/shaders/image.frag.glsl b/shaders/image.frag.glsl deleted file mode 100644 index 38a95da6..00000000 --- a/shaders/image.frag.glsl +++ /dev/null @@ -1,17 +0,0 @@ -#version 450 - -layout(set = 2, binding = 0) uniform sampler2D u_image; -layout(std140, set = 3, binding = 0) uniform UiUniforms { - float background_opacity; -} u_ui; - -layout(location = 0) in vec2 v_uv; -layout(location = 0) out vec4 o_col; - -void main() { - vec4 source = texture(u_image, v_uv); - // Keep source coverage for destination subtraction; the pipeline scales - // source alpha by the same opacity through its alpha blend constant. - source.rgb *= clamp(u_ui.background_opacity, 0.0, 1.0); - o_col = source; -} diff --git a/shaders/image.vert.glsl b/shaders/image.vert.glsl deleted file mode 100644 index ace55a53..00000000 --- a/shaders/image.vert.glsl +++ /dev/null @@ -1,33 +0,0 @@ -#version 450 - -// One instance per Surface image attachment. The CPU has already converted -// the pane's cell rectangle and the aspect-preserving contain fit to NDC. -layout(location = 0) in vec4 a_rect; // x0, y0, x1, y1 -layout(location = 1) in vec4 a_uv_rect; // u0, v0, u1, v1 - -layout(location = 0) out vec2 v_uv; - -void main() { - vec2 pos; - int corner = gl_VertexIndex % 6; - if (corner == 0) { - pos = vec2(a_rect.x, a_rect.y); - v_uv = vec2(a_uv_rect.x, a_uv_rect.y); - } else if (corner == 1) { - pos = vec2(a_rect.z, a_rect.y); - v_uv = vec2(a_uv_rect.z, a_uv_rect.y); - } else if (corner == 2) { - pos = vec2(a_rect.z, a_rect.w); - v_uv = vec2(a_uv_rect.z, a_uv_rect.w); - } else if (corner == 3) { - pos = vec2(a_rect.x, a_rect.y); - v_uv = vec2(a_uv_rect.x, a_uv_rect.y); - } else if (corner == 4) { - pos = vec2(a_rect.z, a_rect.w); - v_uv = vec2(a_uv_rect.z, a_uv_rect.w); - } else { - pos = vec2(a_rect.x, a_rect.w); - v_uv = vec2(a_uv_rect.x, a_uv_rect.w); - } - gl_Position = vec4(pos, 0.0, 1.0); -} diff --git a/shaders/overlay.frag.glsl b/shaders/overlay.frag.glsl deleted file mode 100644 index b5ac8d5a..00000000 --- a/shaders/overlay.frag.glsl +++ /dev/null @@ -1,17 +0,0 @@ -#version 450 - -layout(std140, set = 3, binding = 0) uniform UiUniforms { - float background_opacity; -} u_ui; - -layout(location = 0) in vec4 v_color; - -layout(location = 0) out vec4 o_col; - -void main() { - // Preserve shape coverage for destination subtraction. The pipeline uses - // the matching opacity blend constant for source alpha, so graphics laid - // over a background with the same opacity do not accumulate opaque seams. - // Text/cursor draw groups bind opacity 1 instead. - o_col = vec4(v_color.rgb * clamp(u_ui.background_opacity, 0.0, 1.0), v_color.a); -} diff --git a/shaders/overlay.vert.glsl b/shaders/overlay.vert.glsl deleted file mode 100644 index 5f6be593..00000000 --- a/shaders/overlay.vert.glsl +++ /dev/null @@ -1,11 +0,0 @@ -#version 450 - -layout(location = 0) in vec2 a_pos; -layout(location = 1) in vec4 a_color; - -layout(location = 0) out vec4 v_color; - -void main() { - v_color = a_color; - gl_Position = vec4(a_pos, 0.0, 1.0); -} diff --git a/shaders/prebuilt/decor.frag.spv b/shaders/prebuilt/decor.frag.spv Binary files differdeleted file mode 100644 index 0d5af9d2..00000000 --- a/shaders/prebuilt/decor.frag.spv +++ /dev/null diff --git a/shaders/prebuilt/image.frag.spv b/shaders/prebuilt/image.frag.spv Binary files differdeleted file mode 100644 index d663ed80..00000000 --- a/shaders/prebuilt/image.frag.spv +++ /dev/null diff --git a/shaders/prebuilt/image.vert.spv b/shaders/prebuilt/image.vert.spv Binary files differdeleted file mode 100644 index 1105bae2..00000000 --- a/shaders/prebuilt/image.vert.spv +++ /dev/null diff --git a/shaders/prebuilt/overlay.frag.spv b/shaders/prebuilt/overlay.frag.spv Binary files differdeleted file mode 100644 index e296e99e..00000000 --- a/shaders/prebuilt/overlay.frag.spv +++ /dev/null diff --git a/shaders/prebuilt/overlay.vert.spv b/shaders/prebuilt/overlay.vert.spv Binary files differdeleted file mode 100644 index a518c576..00000000 --- a/shaders/prebuilt/overlay.vert.spv +++ /dev/null diff --git a/shaders/prebuilt/ui.frag.spv b/shaders/prebuilt/ui.frag.spv Binary files differdeleted file mode 100644 index 91022fe2..00000000 --- a/shaders/prebuilt/ui.frag.spv +++ /dev/null diff --git a/shaders/prebuilt/ui.vert.spv b/shaders/prebuilt/ui.vert.spv Binary files differdeleted file mode 100644 index ad132bf9..00000000 --- a/shaders/prebuilt/ui.vert.spv +++ /dev/null diff --git a/shaders/ui.frag.glsl b/shaders/ui.frag.glsl deleted file mode 100644 index 7f70629a..00000000 --- a/shaders/ui.frag.glsl +++ /dev/null @@ -1,39 +0,0 @@ -#version 450 - -// Sample the glyph atlas (R = coverage 0..1) and mix the cell's bg up to its fg -// by that coverage. Output is PREMULTIPLIED: an opaque cell is unchanged. -// WindowOpacity changes only the ground's coverage, never the glyph's ink. - -layout(set = 2, binding = 0) uniform sampler2D u_atlas; -layout(std140, set = 3, binding = 0) uniform UiUniforms { - float background_opacity; -} u_ui; - -layout(location = 0) in vec2 v_uv; -layout(location = 1) in vec3 v_fg; -layout(location = 2) in vec3 v_bg; -layout(location = 4) flat in uint v_effect; -layout(location = 5) flat in vec4 v_clip; - -layout(location = 0) out vec4 o_col; - -void main() { - if (v_clip.z >= v_clip.x && (gl_FragCoord.x < v_clip.x || gl_FragCoord.x >= v_clip.z || - gl_FragCoord.y < v_clip.y || gl_FragCoord.y >= v_clip.w)) discard; - float a = texture(u_atlas, v_uv).r; - // A block cursor is foreground chrome even though its fill occupies the - // cell background: keep it visible at WindowOpacity 0. Otherwise the - // ground's coverage is the setting's. The flag is shared with - // ui.vert.glsl and src/gui/gui.zig. - float bgA = (v_effect & 0x20000000u) != 0u ? 1.0 : clamp(u_ui.background_opacity, 0.0, 1.0); - - // Ink over ground, both premultiplied: v_fg*a + (v_bg*bgA)*(1-a), which is - // mix() with the ground already premultiplied. At bgA = 1 that is exactly - // the mix(v_bg, v_fg, a) at alpha 1 this shader always wrote. - vec4 under = vec4(v_bg * bgA, bgA); - vec4 cell = vec4(mix(under.rgb, v_fg, a), mix(under.a, 1.0, a)); - - // FreeType's hinted grayscale bitmap is already the intended coverage. - // Preserve it exactly; weight-changing gamma hacks blur small stems. - o_col = cell; -} diff --git a/shaders/ui.vert.glsl b/shaders/ui.vert.glsl deleted file mode 100644 index e3532e68..00000000 --- a/shaders/ui.vert.glsl +++ /dev/null @@ -1,53 +0,0 @@ -#version 450 - -// Terminal cell renderer. The CPU uploads one instance per visible cell; this -// shader expands each instance into a two-triangle quad with gl_VertexIndex. -// The fragment shader mixes fg/bg by the atlas's alpha (a glyph mask in the R -// channel), so background fills and glyph foregrounds draw in a single pass. - -layout(location = 0) in vec4 a_rect; // x0, y0, x1, y1 -layout(location = 1) in vec4 a_uv_rect; // u0, v0, u1, v1 -layout(location = 2) in vec3 a_fg; -layout(location = 3) in vec3 a_bg; -// Flags, the high bits: an opaque block-cursor fill (ui.frag.glsl). -layout(location = 4) in uint a_effect; -// A clip rectangle in target pixels (x0, y0, x1, y1): the fragment shader -// drops what lies outside it. x1 < x0 is none. -layout(location = 5) in vec4 a_clip; - -layout(location = 0) out vec2 v_uv; -layout(location = 1) out vec3 v_fg; -layout(location = 2) out vec3 v_bg; -layout(location = 4) flat out uint v_effect; -layout(location = 5) flat out vec4 v_clip; - -void main() { - vec2 pos; - vec2 uv; - int corner = gl_VertexIndex % 6; - if (corner == 0) { - pos = vec2(a_rect.x, a_rect.y); - uv = vec2(a_uv_rect.x, a_uv_rect.y); - } else if (corner == 1) { - pos = vec2(a_rect.z, a_rect.y); - uv = vec2(a_uv_rect.z, a_uv_rect.y); - } else if (corner == 2) { - pos = vec2(a_rect.z, a_rect.w); - uv = vec2(a_uv_rect.z, a_uv_rect.w); - } else if (corner == 3) { - pos = vec2(a_rect.x, a_rect.y); - uv = vec2(a_uv_rect.x, a_uv_rect.y); - } else if (corner == 4) { - pos = vec2(a_rect.z, a_rect.w); - uv = vec2(a_uv_rect.z, a_uv_rect.w); - } else { - pos = vec2(a_rect.x, a_rect.w); - uv = vec2(a_uv_rect.x, a_uv_rect.w); - } - v_uv = uv; - v_fg = a_fg; - v_bg = a_bg; - v_effect = a_effect; - v_clip = a_clip; - gl_Position = vec4(pos, 0.0, 1.0); -} diff --git a/src/gui/gui.zig b/src/gui/gui.zig index cc43d8b6..d0d46f44 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1,4 +1,5 @@ const SketchGpu = @import("sketch_gpu"); +const Cells = @import("gpu_cells"); // sketchbook's src/gpu/Cells.zig const GpuScene = @import("gpu_scene"); const filesystem = @import("../fs.zig"); const std = @import("std"); @@ -240,52 +241,13 @@ fn inkBounds(bitmap: []const u8, stride: usize, w: usize, h: usize) ?InkBounds { return bounds; } -const CellInstance = extern struct { - x0: f32, - y0: f32, - x1: f32, - y1: f32, - u0: f32, - v0: f32, - u1: f32, - v1: f32, - fr: f32, - fg: f32, - fb: f32, - br: f32, - bg: f32, - bb: f32, - /// The flags (the high bits, ui.vert.glsl). - effect: u32 = 0, - /// A clip rectangle in target pixels; x1 < x0 is none (ui.vert.glsl). - clip_x0: f32 = 0, - clip_y0: f32 = 0, - clip_x1: f32 = -1, - clip_y1: f32 = -1, -}; +const CellInstance = Cells.CellInstance; +const OverlayVertex = Cells.OverlayVertex; +const ImageInstance = Cells.ImageInstance; +const opaque_bg_bit = Cells.opaque_bg_bit; -const OverlayVertex = extern struct { - x: f32, - y: f32, - r: f32, - g: f32, - b: f32, - a: f32, -}; const OverlayColor = struct { r: f32, g: f32, b: f32, a: f32 }; -const ImageInstance = extern struct { - x0: f32, - y0: f32, - x1: f32, - y1: f32, - u0: f32, - v0: f32, - u1: f32, - v1: f32, -}; - -const opaque_bg_bit: u32 = 0x2000_0000; const PreparedImage = struct { premultiplied: bool = false, @@ -1131,7 +1093,7 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di _ = c.SDL_SyncWindow(window); } - const atlas_tex = texture2d(device, c.SDL_GPU_TEXTUREFORMAT_R8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, atlas_w, atlas_h) orelse return error.GpuCreate; + const atlas_tex = Cells.texture2d(device, c.SDL_GPU_TEXTUREFORMAT_R8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, atlas_w, atlas_h) orelse return error.GpuCreate; var samp_info = std.mem.zeroes(c.SDL_GPUSamplerCreateInfo); samp_info.min_filter = c.SDL_GPU_FILTER_NEAREST; @@ -1149,10 +1111,10 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di var ax_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = atlas_w * atlas_h, .props = 0 }; const atlas_xfer = c.SDL_CreateGPUTransferBuffer(device, &ax_info) orelse return error.GpuCreate; - const pipeline = try makePipeline(device, swapchain_format, vert_spv, frag_spv, 1, 1, cell_input, std.mem.zeroes(c.SDL_GPUColorTargetBlendState)); - const decor_pipeline = try makePipeline(device, swapchain_format, vert_spv, decor_frag_spv, 0, 1, cell_input, backgroundLayerBlend()); - const overlay_pipeline = try makePipeline(device, swapchain_format, overlay_vert_spv, overlay_frag_spv, 0, 1, overlay_input, backgroundLayerBlend()); - const image_pipeline = try makePipeline(device, swapchain_format, image_vert_spv, image_frag_spv, 1, 1, image_input, backgroundLayerBlend()); + const pipeline = try Cells.makePipeline(device, swapchain_format, vert_spv, frag_spv, 1, 1, Cells.cell_input, std.mem.zeroes(c.SDL_GPUColorTargetBlendState)); + const decor_pipeline = try Cells.makePipeline(device, swapchain_format, vert_spv, decor_frag_spv, 0, 1, Cells.cell_input, Cells.backgroundLayerBlend()); + const overlay_pipeline = try Cells.makePipeline(device, swapchain_format, overlay_vert_spv, overlay_frag_spv, 0, 1, Cells.overlay_input, Cells.backgroundLayerBlend()); + const image_pipeline = try Cells.makePipeline(device, swapchain_format, image_vert_spv, image_frag_spv, 1, 1, Cells.image_input, Cells.backgroundLayerBlend()); const overlay_buffer_size: u32 = @intCast(max_overlay_vertices * @sizeOf(OverlayVertex)); var ovb_info = c.SDL_GPUBufferCreateInfo{ .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX, .size = overlay_buffer_size, .props = 0 }; @@ -4264,7 +4226,7 @@ fn uploadNativeTexture( return; } - const texture = texture2d(g.device, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, r.w, r.h) orelse return error.GpuCreate; + const texture = Cells.texture2d(g.device, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, r.w, r.h) orelse return error.GpuCreate; errdefer c.SDL_ReleaseGPUTexture(g.device, texture); try uploadTextureRows(g, cmd, texture, r.bytes()[0..byte_len], r.w, 0, r.h); try g.native_images.put(gpa, key, texture); @@ -4394,7 +4356,7 @@ fn prepareNativeImages( const capacity = std.math.cast(u32, g.prepared_images.items.len) orelse return false; if (capacity == 0) return true; - growInstances(g, &g.image_vbuf, &g.image_vxfer, &g.image_capacity, capacity, @sizeOf(ImageInstance)) catch return false; + Cells.growInstances(g.device, &g.image_vbuf, &g.image_vxfer, &g.image_capacity, capacity, @sizeOf(ImageInstance)) catch return false; const ptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.image_vxfer.?, false) orelse return false); const instances: [*]ImageInstance = @ptrCast(@alignCast(ptr)); @@ -4590,7 +4552,7 @@ fn renderFrame( // The shifted rows of a scroll: its body, a glide's rows past it, and // a body layer's rows past the grid's. const capacity = std.math.add(u32, cell_total, 3 * cells + decor_len) catch return error.GpuCapacity; - try growInstances(g, &g.vbuf, &g.vxfer, &g.vbuf_cells, capacity, @sizeOf(CellInstance)); + try Cells.growInstances(g.device, &g.vbuf, &g.vxfer, &g.vbuf_cells, capacity, @sizeOf(CellInstance)); const vptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.vxfer.?, false) orelse { command_consumed = true; @@ -5542,130 +5504,9 @@ fn uploadAtlas(g: *Gui, cmd: *c.SDL_GPUCommandBuffer) void { g.atlas_dirty_bottom = 0; } -/// A vertex buffer and its upload buffer holding at least `needed` -/// elements of `stride` bytes: those there when they do, else twice as -/// many again until they would (the old ones let go). -fn growInstances(g: *Gui, buffer: *?*c.SDL_GPUBuffer, transfer: *?*c.SDL_GPUTransferBuffer, capacity: *u32, needed: u32, stride: u32) !void { - if (needed <= capacity.* and buffer.* != null and transfer.* != null) return; - var grown = @max(@as(u32, 1), capacity.*); - while (grown < needed) grown = std.math.mul(u32, grown, 2) catch return error.GpuCapacity; - const size = std.math.mul(u32, grown, stride) catch return error.GpuCapacity; - var vb_info = c.SDL_GPUBufferCreateInfo{ .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX, .size = size, .props = 0 }; - const next_buffer = c.SDL_CreateGPUBuffer(g.device, &vb_info) orelse return error.GpuCreate; - errdefer c.SDL_ReleaseGPUBuffer(g.device, next_buffer); - var xf_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = size, .props = 0 }; - const next_transfer = c.SDL_CreateGPUTransferBuffer(g.device, &xf_info) orelse return error.GpuCreate; - if (buffer.*) |old| c.SDL_ReleaseGPUBuffer(g.device, old); - if (transfer.*) |old| c.SDL_ReleaseGPUTransferBuffer(g.device, old); - buffer.* = next_buffer; - transfer.* = next_transfer; - capacity.* = grown; -} - -/// One vertex buffer of a pipeline: `pitch` bytes an element, read per -/// vertex or per instance through `attrs`. -const VertexInput = struct { pitch: u32, rate: c.SDL_GPUVertexInputRate, attrs: []const c.SDL_GPUVertexAttribute }; - -/// The cells' and the decor's instances (ui.vert.glsl). -const cell_input: VertexInput = .{ .pitch = @sizeOf(CellInstance), .rate = c.SDL_GPU_VERTEXINPUTRATE_INSTANCE, .attrs = &.{ - .{ .location = 0, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(CellInstance, "x0") }, - .{ .location = 1, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(CellInstance, "u0") }, - .{ .location = 2, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .offset = @offsetOf(CellInstance, "fr") }, - .{ .location = 3, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .offset = @offsetOf(CellInstance, "br") }, - .{ .location = 4, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_UINT, .offset = @offsetOf(CellInstance, "effect") }, - .{ .location = 5, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(CellInstance, "clip_x0") }, -} }; - -const overlay_input: VertexInput = .{ .pitch = @sizeOf(OverlayVertex), .rate = c.SDL_GPU_VERTEXINPUTRATE_VERTEX, .attrs = &.{ - .{ .location = 0, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2, .offset = @offsetOf(OverlayVertex, "x") }, - .{ .location = 1, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(OverlayVertex, "r") }, -} }; - -const image_input: VertexInput = .{ .pitch = @sizeOf(ImageInstance), .rate = c.SDL_GPU_VERTEXINPUTRATE_INSTANCE, .attrs = &.{ - .{ .location = 0, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(ImageInstance, "x0") }, - .{ .location = 1, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(ImageInstance, "u0") }, -} }; - -/// A pipeline drawing triangles into `color_format`: `vert`, and `frag` -/// with its samplers and uniform buffers, over one vertex buffer of -/// `input` (none: the post chain's triangle over the whole frame). -pub fn makePipeline( - device: *c.SDL_GPUDevice, - color_format: c.SDL_GPUTextureFormat, - vert: []const u8, - frag: []const u8, - samplers: u32, - uniforms: u32, - input: ?VertexInput, - blend: c.SDL_GPUColorTargetBlendState, -) !*c.SDL_GPUGraphicsPipeline { - const vs = try makeShader(device, vert, c.SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); - defer c.SDL_ReleaseGPUShader(device, vs); - const fs = try makeShader(device, frag, c.SDL_GPU_SHADERSTAGE_FRAGMENT, samplers, uniforms); - defer c.SDL_ReleaseGPUShader(device, fs); - var col_desc = c.SDL_GPUColorTargetDescription{ .format = color_format, .blend_state = blend }; - var vbuf_desc: c.SDL_GPUVertexBufferDescription = undefined; - var info = std.mem.zeroes(c.SDL_GPUGraphicsPipelineCreateInfo); - info.vertex_shader = vs; - info.fragment_shader = fs; - info.primitive_type = c.SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; - if (input) |in| { - vbuf_desc = .{ .slot = 0, .pitch = in.pitch, .input_rate = in.rate, .instance_step_rate = 0 }; - info.vertex_input_state = .{ - .vertex_buffer_descriptions = &vbuf_desc, - .num_vertex_buffers = 1, - .vertex_attributes = in.attrs.ptr, - .num_vertex_attributes = @intCast(in.attrs.len), - }; - } - info.rasterizer_state.fill_mode = c.SDL_GPU_FILLMODE_FILL; - info.rasterizer_state.cull_mode = c.SDL_GPU_CULLMODE_NONE; - info.multisample_state.sample_count = c.SDL_GPU_SAMPLECOUNT_1; - info.target_info = .{ .color_target_descriptions = &col_desc, .num_color_targets = 1 }; - return c.SDL_CreateGPUGraphicsPipeline(device, &info) orelse error.GpuCreate; -} - -fn backgroundLayerBlend() c.SDL_GPUColorTargetBlendState { - var blend = std.mem.zeroes(c.SDL_GPUColorTargetBlendState); - blend.src_color_blendfactor = c.SDL_GPU_BLENDFACTOR_SRC_ALPHA; - blend.dst_color_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; - blend.color_blend_op = c.SDL_GPU_BLENDOP_ADD; - blend.src_alpha_blendfactor = c.SDL_GPU_BLENDFACTOR_CONSTANT_COLOR; - blend.dst_alpha_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; - blend.alpha_blend_op = c.SDL_GPU_BLENDOP_ADD; - blend.enable_blend = true; - return blend; -} - -fn makeShader(device: *c.SDL_GPUDevice, code: []const u8, stage: c.SDL_GPUShaderStage, n_samplers: u32, n_uniform_buffers: u32) !*c.SDL_GPUShader { - var info = std.mem.zeroes(c.SDL_GPUShaderCreateInfo); - info.code = code.ptr; - info.code_size = code.len; - info.entrypoint = "main"; - info.format = c.SDL_GPU_SHADERFORMAT_SPIRV; - info.stage = stage; - info.num_samplers = n_samplers; - info.num_uniform_buffers = n_uniform_buffers; - return c.SDL_CreateGPUShader(device, &info) orelse error.GpuCreate; -} - -/// A 2D texture of one level, one layer and one sample. -pub fn texture2d(device: *c.SDL_GPUDevice, format: c.SDL_GPUTextureFormat, usage: c.SDL_GPUTextureUsageFlags, width: u32, height: u32) ?*c.SDL_GPUTexture { - var info = std.mem.zeroes(c.SDL_GPUTextureCreateInfo); - info.type = c.SDL_GPU_TEXTURETYPE_2D; - info.format = format; - info.usage = usage; - info.width = width; - info.height = height; - info.layer_count_or_depth = 1; - info.num_levels = 1; - info.sample_count = c.SDL_GPU_SAMPLECOUNT_1; - return c.SDL_CreateGPUTexture(device, &info); -} - fn ensureCaptureTexture(g: *Gui, width: u32, height: u32) !void { if (g.capture_tex != null and g.capture_tex_w == width and g.capture_tex_h == height) return; - const next = texture2d(g.device, g.swapchain_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, width, height) orelse return error.GpuCreate; + const next = Cells.texture2d(g.device, g.swapchain_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, width, height) orelse return error.GpuCreate; if (g.capture_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); g.capture_tex = next; g.capture_tex_w = width; @@ -7116,12 +6957,6 @@ test "installed Nerd Symbols fallback covers Yazi directory icons" { try std.testing.expect(c.ui_font_glyph(face, 0xe5ff) != 0); } -test "the shader instance ABI: the flags after the colours, the clip after them" { - try std.testing.expectEqual(@as(usize, 76), @sizeOf(CellInstance)); - try std.testing.expectEqual(@as(usize, 56), @offsetOf(CellInstance, "effect")); - try std.testing.expectEqual(@as(usize, 60), @offsetOf(CellInstance, "clip_x0")); - try std.testing.expectEqual(@as(usize, 32), @sizeOf(ImageInstance)); -} test "GUI PTY reader delivers a real shell tail before EOF and joins on close" { const gpa = std.testing.allocator; @@ -8581,7 +8416,7 @@ test "the atlas's dirty rows hold every row a new glyph draws into, and a reset } test "overlapping background layers keep one uniform opacity" { - const blend = backgroundLayerBlend(); + const blend = Cells.backgroundLayerBlend(); try std.testing.expectEqual(@as(c.SDL_GPUBlendFactor, c.SDL_GPU_BLENDFACTOR_CONSTANT_COLOR), blend.src_alpha_blendfactor); try std.testing.expectEqual(@as(c.SDL_GPUBlendFactor, c.SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA), blend.dst_alpha_blendfactor); for ([_]f32{ 0, 0.25, 0.7, 1 }) |opacity| { @@ -9203,9 +9038,9 @@ fn prepareAddonGpu(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer } const out = gpu.output(target) orelse continue; if (g.addon_pipeline == null) { - var blend = backgroundLayerBlend(); + var blend = Cells.backgroundLayerBlend(); blend.src_color_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE; - g.addon_pipeline = makePipeline(g.device, g.swapchain_format, image_vert_spv, image_frag_spv, 1, 1, image_input, blend) catch continue; + g.addon_pipeline = Cells.makePipeline(g.device, g.swapchain_format, image_vert_spv, image_frag_spv, 1, 1, Cells.image_input, blend) catch continue; } g.prepared_images.append(gpa, .{ .premultiplied = true, |
