diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 22:02:43 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 1cb36ee39a8bbc2cf61c28003670d760841d6d3e (patch) | |
| tree | d7d01eb5b3a40920a8f71d091a106561ca56b282 /src/gpu/scene.zig | |
| parent | 6933f99ddb2d7103515d26838496309a807bba26 (diff) | |
| download | sketchbook-1cb36ee39a8bbc2cf61c28003670d760841d6d3e.tar.gz sketchbook-1cb36ee39a8bbc2cf61c28003670d760841d6d3e.zip | |
gpu: add a post-process pass with optional depth sampling, and describe the executor pardes shares
`f.postProcess(source, target, .{...})` records a fullscreen pass that samples a completed render into a distinct target image: colour at fragment binding 0 and, with `sample_depth`, depth at 1, which needs the source's latest pass to have depth and one sample (missing depth, multisampled depth and feedback loops are rejected). The scene format's version 4 carries it, the executor records it on the caller's command buffer and owns the sampler, the web host does the same with its texture and sampler pairs, and native profiling reports the work as `postprocess`. The README says how to use it.
docs/pardes-rendering.md describes the executor as pardes uses it for addon scenes: a borrowed SDL GPU device, `record` into the caller's command buffer, `run`, `output` and `postProcess`.
The working copy as it stood on 2026-11-16; message written by Claude from the diff, at the author's request.
Diffstat (limited to 'src/gpu/scene.zig')
| -rw-r--r-- | src/gpu/scene.zig | 55 |
1 files changed, 47 insertions, 8 deletions
diff --git a/src/gpu/scene.zig b/src/gpu/scene.zig index e5ff3d0..d5e1db5 100644 --- a/src/gpu/scene.zig +++ b/src/gpu/scene.zig @@ -2,6 +2,7 @@ //! Version 1: "SGPU", u32 version, then Pass fields in declaration order. //! Version 2 appends one fullscreen PostProcess sampling that pass's output. //! Version 3 adds depth fields and an optional PostProcess after the pass. +//! Version 4 adds optional depth sampling to PostProcess. //! Integers/enums/f32 are little endian; slices have a u32 count, optionals //! and bools a byte (0 or 1). No pointers, native padding, or device handles. //! Storage buffers are not part of this version: each frame stands alone. @@ -22,11 +23,12 @@ pub fn encode(w: *std.Io.Writer, pass: abi.gpu.Pass) !void { pub fn encodePostProcess(w: *std.Io.Writer, pass: abi.gpu.Pass, post: abi.gpu.PostProcess) !void { try validate(pass); try validatePostProcess(post); + if (post.sample_depth and (!pass.depth or pass.samples != 1)) return error.BadScene; try w.writeAll("SGPU"); - const version: u32 = if (pass.depth) 3 else 2; + const version: u32 = if (post.sample_depth) 4 else if (pass.depth) 3 else 2; try w.writeInt(u32, version, .little); try put(w, pass, version); - if (version == 3) try put(w, @as(?abi.gpu.PostProcess, post), version) else try put(w, post, version); + if (version >= 3) try put(w, @as(?abi.gpu.PostProcess, post), version) else try put(w, post, version); } /// Allocations belong to the caller's frame arena. Never run unvalidated @@ -42,13 +44,16 @@ pub fn decodeFrame(arena: std.mem.Allocator, bytes: []const u8) !Frame { var r: std.Io.Reader = .fixed(bytes); if (!std.mem.eql(u8, try r.take(4), "SGPU")) return error.BadScene; const version = try r.takeInt(u32, .little); - if (version < 1 or version > 3) return error.BadScene; + if (version < 1 or version > 4) return error.BadScene; var budget: usize = max_bytes * 4; const pass = try get(abi.gpu.Pass, arena, &r, &budget, version); - const post: ?abi.gpu.PostProcess = if (version == 3) try get(?abi.gpu.PostProcess, arena, &r, &budget, version) else if (version == 2) try get(abi.gpu.PostProcess, arena, &r, &budget, version) else null; + const post: ?abi.gpu.PostProcess = if (version >= 3) try get(?abi.gpu.PostProcess, arena, &r, &budget, version) else if (version == 2) try get(abi.gpu.PostProcess, arena, &r, &budget, version) else null; if (r.end != r.seek) return error.BadScene; try validate(pass); - if (post) |p| try validatePostProcess(p); + if (post) |p| { + try validatePostProcess(p); + if (p.sample_depth and (!pass.depth or pass.samples != 1)) return error.BadScene; + } return .{ .pass = pass, .post = post }; } @@ -57,6 +62,11 @@ fn depthField(comptime T: type, comptime name: []const u8) bool { (T == abi.gpu.Pipeline and (std.mem.eql(u8, name, "depth_test") or std.mem.eql(u8, name, "depth_write"))); } +fn absentField(comptime T: type, comptime name: []const u8, version: u32) bool { + return (version < 3 and depthField(T, name)) or + (version < 4 and T == abi.gpu.PostProcess and std.mem.eql(u8, name, "sample_depth")); +} + fn put(w: *std.Io.Writer, value: anytype, version: u32) anyerror!void { const T = @TypeOf(value); switch (@typeInfo(T)) { @@ -74,7 +84,7 @@ fn put(w: *std.Io.Writer, value: anytype, version: u32) anyerror!void { for (value) |v| try put(w, v, version); }, .@"struct" => |s| inline for (s.field_names) |name| { - if (version >= 3 or !depthField(T, name)) try put(w, @field(value, name), version); + if (!absentField(T, name, version)) try put(w, @field(value, name), version); }, else => @compileError("unsupported scene field"), } @@ -117,7 +127,7 @@ fn get(comptime T: type, a: std.mem.Allocator, r: *std.Io.Reader, budget: *usize .@"struct" => |s| { var out: T = undefined; inline for (s.field_names, s.field_types) |name, F| { - @field(out, name) = if (version < 3 and comptime depthField(T, name)) std.mem.zeroes(F) else try get(F, a, r, budget, version); + @field(out, name) = if (absentField(T, name, version)) std.mem.zeroes(F) else try get(F, a, r, budget, version); } return out; }, @@ -227,7 +237,7 @@ test "GPU postprocess keeps v1 readable and validates the complete second pass" bad = post; bad.fragment.storage_buffers = 1; try std.testing.expectError(error.BadShader, validatePostProcess(bad)); - out.written()[4] = 4; + out.written()[4] = 5; try std.testing.expectError(error.BadScene, decodeFrame(arena.allocator(), out.written())); } @@ -261,3 +271,32 @@ test "depth uses v3 and requires an attachment; legacy frames keep depth disable const legacy = try decode(arena.allocator(), out.written()); try std.testing.expect(!legacy.depth and legacy.pipelines[0].depth_test == null and !legacy.pipelines[0].depth_write); } + +test "depth postprocess uses v4 and rejects missing or multisampled source depth" { + const code = std.mem.asBytes(&[_]u32{ 0x07230203, 0, 0, 0, 0 }); + const pipelines = [_]abi.gpu.Pipeline{.{ .vertex = .{ .code = code }, .fragment = .{ .code = code }, .stride = 0, .attributes = &.{} }}; + var pass: abi.gpu.Pass = .{ .image = .none, .width = 32, .height = 24, .depth = true, .pipelines = &pipelines, .draws = &.{.{ .pipeline = 0, .vertex_count = 3 }} }; + var post: abi.gpu.PostProcess = .{ .sample_depth = true, .vertex = .{ .code = code }, .fragment = .{ .code = code } }; + var out: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer out.deinit(); + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + try encodePostProcess(&out.writer, pass, post); + try std.testing.expectEqual(4, out.written()[4]); + try std.testing.expect((try decodeFrame(arena.allocator(), out.written())).post.?.sample_depth); + for (0..out.written().len) |i| { + _ = decodeFrame(arena.allocator(), out.written()[0..i]) catch continue; + return error.AcceptedTruncatedScene; + } + pass.depth = false; + try std.testing.expectError(error.BadScene, encodePostProcess(&out.writer, pass, post)); + pass.depth = true; + pass.samples = 4; + try std.testing.expectError(error.BadScene, encodePostProcess(&out.writer, pass, post)); + pass.samples = 1; + post.sample_depth = false; + out.clearRetainingCapacity(); + try encodePostProcess(&out.writer, pass, post); + try std.testing.expectEqual(3, out.written()[4]); + try std.testing.expect(!(try decodeFrame(arena.allocator(), out.written())).post.?.sample_depth); +} |
