summaryrefslogtreecommitdiff
path: root/src/sketch
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 22:02:43 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit1cb36ee39a8bbc2cf61c28003670d760841d6d3e (patch)
treed7d01eb5b3a40920a8f71d091a106561ca56b282 /src/sketch
parent6933f99ddb2d7103515d26838496309a807bba26 (diff)
downloadsketchbook-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/sketch')
-rw-r--r--src/sketch/clock.zig2
-rw-r--r--src/sketch/root.zig27
-rw-r--r--src/sketch/web.zig5
3 files changed, 33 insertions, 1 deletions
diff --git a/src/sketch/clock.zig b/src/sketch/clock.zig
index e64e2a8..2d63e12 100644
--- a/src/sketch/clock.zig
+++ b/src/sketch/clock.zig
@@ -85,7 +85,7 @@ test "N updates of one tick each step exactly as one update of N ticks" {
var sum: u32 = 0;
for (per) |n| sum += n;
try std.testing.expectEqual(sum, total);
- try std.testing.expectEqual(std.mem.toBytes(one), std.mem.toBytes(all));
+ try std.testing.expectEqualDeep(one, all);
try std.testing.expectEqual(@as(u32, Clock.max_steps), per[300]);
try std.testing.expectEqual(@as(u64, total), all.count);
}
diff --git a/src/sketch/root.zig b/src/sketch/root.zig
index 434478c..cb20ab9 100644
--- a/src/sketch/root.zig
+++ b/src/sketch/root.zig
@@ -232,6 +232,12 @@ pub const Frame = struct {
kept.draws = draws;
f.emit(.{ .gpu = kept });
}
+ /// Samples the source color and optionally depth into a distinct image.
+ pub fn postProcess(f: *Frame, source: Image, destination: Image, post: abi.gpu.PostProcess) void {
+ var kept = post;
+ kept.fragment_uniforms = f.keep(u8, post.fragment_uniforms);
+ f.emit(.{ .post_process = .{ .source = source, .target = destination, .effect = kept } });
+ }
/// `bytes` into a buffer made by `Host.newBuffer`.
pub fn upload(f: *Frame, buffer: abi.Buffer, offset: u32, bytes: []const u8) void {
f.emit(.{ .upload = .{ .buffer = buffer, .offset = offset, .bytes = bytes } });
@@ -469,3 +475,24 @@ test "setup receives session resources without a Frame and reports errors" {
try std.testing.expectEqual(@as(u32, 1), session.calls);
try std.testing.expect(seen);
}
+
+test "postprocess preserves image handles and copies transient uniforms" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ var commands: std.ArrayList(abi.Command) = .empty;
+ var ctx: abi.Context = undefined;
+ ctx.commands = &commands;
+ var frame: Frame = undefined;
+ frame.ctx = &ctx;
+ frame.scratch = arena.allocator();
+ var uniforms = [_]u8{ 1, 2, 3, 4 };
+ const source: Image = @fromBackingInt(@intCast(1));
+ const target: Image = @fromBackingInt(@intCast(2));
+ frame.postProcess(source, target, .{ .vertex = .{ .code = "vertex" }, .fragment = .{ .code = "fragment" }, .sample_depth = true, .fragment_uniforms = &uniforms });
+ uniforms[0] = 9;
+ const post = commands.items[0].post_process;
+ try std.testing.expectEqual(source, post.source);
+ try std.testing.expectEqual(target, post.target);
+ try std.testing.expect(post.effect.sample_depth);
+ try std.testing.expectEqualSlices(u8, &.{ 1, 2, 3, 4 }, post.effect.fragment_uniforms);
+}
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 682bff6..937b491 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -43,6 +43,7 @@ const js = struct {
/// A `Dispatch` or a `Pass`, described in JSON (see `describe`).
extern "gpu" fn compute(ptr: [*]const u8, len: usize) void;
extern "gpu" fn pass(ptr: [*]const u8, len: usize) void;
+ extern "gpu" fn postProcess(ptr: [*]const u8, len: usize) void;
/// Answers through `sk_download`.
extern "gpu" fn download(id: u32, offset: u32, size: u32, key: u32) void;
extern "audio" fn rate() u32;
@@ -234,6 +235,10 @@ fn Driver(comptime App: type) type {
const json = describe(v);
js.pass(json.ptr, json.len);
},
+ .post_process => |v| {
+ const json = describe(v);
+ js.postProcess(json.ptr, json.len);
+ },
.download => |v| js.download(@backingInt(v.buffer), v.offset, v.size, v.key),
.open_file => |v| js.openFile(v.command.ptr, v.command.len, v.pattern.ptr, v.pattern.len),
// Synthesised voices are not on the web yet; quitting a page