diff options
Diffstat (limited to 'src/sketch')
| -rw-r--r-- | src/sketch/clock.zig | 2 | ||||
| -rw-r--r-- | src/sketch/root.zig | 27 | ||||
| -rw-r--r-- | src/sketch/web.zig | 5 |
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 |
