From 2348776791e9dd17afeb12a06ba42a4a71917bcf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 21:34:41 -0300 Subject: sketchbook: a drawing layer on top, host fixes for replay and migration --- src/host/gpu.zig | 74 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 13 deletions(-) (limited to 'src/host/gpu.zig') diff --git a/src/host/gpu.zig b/src/host/gpu.zig index 9c77205..94b2e39 100644 --- a/src/host/gpu.zig +++ b/src/host/gpu.zig @@ -119,6 +119,10 @@ const Target = struct { texture: *c.SDL_GPUTexture, samples: u8 = 1, msaa: ?*c.SDL_GPUTexture = null, + /// Not rendered into yet: undefined natively, so the first pass that + /// would load clears to zero (WebGPU's starting contents). + fresh: bool = true, + msaa_fresh: bool = true, stencil_samples: u8 = 0, stencil: ?*c.SDL_GPUTexture = null, }; @@ -187,6 +191,12 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { if (pass.width == 0 or pass.height == 0 or pass.draws.len == 0 and pass.clear == null) return; const samples = g.supportedSamples(pass.samples); const t = try g.renderTarget(images, pass.image, pass.width, pass.height, samples, pass.stencil); + // Each pipeline is looked up on first use in the pass, not per draw: + // the lookup hashes the shaders' code. Outer null: not looked up yet; + // inner null: it failed (warned once). + const built = try g.gpa.alloc(??*c.SDL_GPUGraphicsPipeline, pass.pipelines.len); + defer g.gpa.free(built); + @memset(built, null); const start = g.startTiming(); // Every draw's vertices go up in one copy. @@ -210,16 +220,23 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { const clear = pass.clear orelse .{ 0, 0, 0, 0 }; var color: c.SDL_GPUColorTargetInfo = std.mem.zeroes(c.SDL_GPUColorTargetInfo); color.clear_color = .{ .r = clear[0], .g = clear[1], .b = clear[2], .a = clear[3] }; - // A multisampled pass cannot load resolved pixels: it always clears. - color.load_op = if (pass.clear != null or samples > 1) c.SDL_GPU_LOADOP_CLEAR else c.SDL_GPU_LOADOP_LOAD; + // `clear = null` keeps what the attachment holds, as on the web: a + // multisampled pass loads its multisampled attachment, which every + // pass therefore stores as well as resolves. A new attachment is + // cleared to zero instead, since Vulkan starts it undefined where + // WebGPU starts it zeroed. + const fresh = if (samples > 1) &t.msaa_fresh else &t.fresh; + color.load_op = if (pass.clear != null or fresh.*) c.SDL_GPU_LOADOP_CLEAR else c.SDL_GPU_LOADOP_LOAD; + fresh.* = false; if (samples > 1) { color.texture = t.msaa; - color.store_op = c.SDL_GPU_STOREOP_RESOLVE; + color.store_op = c.SDL_GPU_STOREOP_RESOLVE_AND_STORE; color.resolve_texture = t.texture; } else { color.texture = t.texture; color.store_op = c.SDL_GPU_STOREOP_STORE; } + t.fresh = false; var depth: c.SDL_GPUDepthStencilTargetInfo = std.mem.zeroes(c.SDL_GPUDepthStencilTargetInfo); if (pass.stencil) { depth.texture = t.stencil; @@ -231,29 +248,59 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { } const rp = c.SDL_BeginGPURenderPass(cmd, &color, 1, if (pass.stencil) &depth else null) orelse return error.GpuRenderPass; var bound: ?*c.SDL_GPUGraphicsPipeline = null; + // State already set in this pass, so draws that repeat it skip the call. + var vertex_uniforms: ?[]const u8 = null; + var stencil_reference: ?u8 = null; + var scissor: ?c.SDL_Rect = null; + var vertex_base: ?u32 = null; var offset: u32 = 0; for (pass.draws) |d| { defer offset += @intCast(std.mem.alignForward(usize, d.vertices.len, 4)); if (d.pipeline >= pass.pipelines.len or d.vertex_count == 0) continue; - const built = g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil) catch |err| { + const slot = &built[d.pipeline]; + if (slot.* == null) slot.* = g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil) catch |err| blk: { std.log.warn("gpu pipeline {d}: {t} ({s})", .{ d.pipeline, err, c.SDL_GetError() }); - continue; + break :blk @as(?*c.SDL_GPUGraphicsPipeline, null); }; - if (bound != built) { - c.SDL_BindGPUGraphicsPipeline(rp, built); - bound = built; + const chosen = slot.*.? orelse continue; + if (bound != chosen) { + c.SDL_BindGPUGraphicsPipeline(rp, chosen); + bound = chosen; + } + // Vertices at a multiple of the stride are reached from one binding + // at 0 by the draw's first vertex; others get a binding of their own. + var first: u32 = 0; + if (d.vertices.len > 0) { + const stride = pass.pipelines[d.pipeline].stride; + const base = if (stride > 0 and offset % stride == 0) 0 else offset; + if (base == 0 and stride > 0) first = offset / stride; + if (vertex_base != base) { + c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = base }, 1); + vertex_base = base; + } } - if (d.vertices.len > 0) c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1); var bound_storage: [8]*c.SDL_GPUBuffer = undefined; if (d.vertex_storage.len > 0) c.SDL_BindGPUVertexStorageBuffers(rp, 0, &bound_storage, g.resolve(d.vertex_storage, &bound_storage) catch continue); if (d.fragment_storage.len > 0) c.SDL_BindGPUFragmentStorageBuffers(rp, 0, &bound_storage, g.resolve(d.fragment_storage, &bound_storage) catch continue); - if (d.vertex_uniforms.len > 0) c.SDL_PushGPUVertexUniformData(cmd, 0, d.vertex_uniforms.ptr, @intCast(d.vertex_uniforms.len)); + // Pushed uniforms stay for later draws: the same bytes (sketches + // share one slice for a pass's viewport) need no second push. + if (d.vertex_uniforms.len > 0) if (vertex_uniforms == null or vertex_uniforms.?.ptr != d.vertex_uniforms.ptr or vertex_uniforms.?.len != d.vertex_uniforms.len) { + c.SDL_PushGPUVertexUniformData(cmd, 0, d.vertex_uniforms.ptr, @intCast(d.vertex_uniforms.len)); + vertex_uniforms = d.vertex_uniforms; + }; if (d.fragment_uniforms.len > 0) c.SDL_PushGPUFragmentUniformData(cmd, 0, d.fragment_uniforms.ptr, @intCast(d.fragment_uniforms.len)); - if (pass.stencil) c.SDL_SetGPUStencilReference(rp, d.stencil_reference); + if (pass.stencil and stencil_reference != d.stencil_reference) { + c.SDL_SetGPUStencilReference(rp, d.stencil_reference); + stencil_reference = d.stencil_reference; + } const full: abi.Rect = .{ .w = @floatFromInt(pass.width), .h = @floatFromInt(pass.height) }; const s = d.scissor orelse full; - c.SDL_SetGPUScissor(rp, &.{ .x = @intFromFloat(@max(s.x, 0)), .y = @intFromFloat(@max(s.y, 0)), .w = @intFromFloat(@max(s.w, 0)), .h = @intFromFloat(@max(s.h, 0)) }); - c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, 0, 0); + const rect: c.SDL_Rect = .{ .x = @intFromFloat(@max(s.x, 0)), .y = @intFromFloat(@max(s.y, 0)), .w = @intFromFloat(@max(s.w, 0)), .h = @intFromFloat(@max(s.h, 0)) }; + if (scissor == null or !std.meta.eql(scissor.?, rect)) { + c.SDL_SetGPUScissor(rp, &rect); + scissor = rect; + } + c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, first, 0); } c.SDL_EndGPURenderPass(rp); try g.submit(cmd, if (pass.label.len > 0) pass.label else "pass", start); @@ -457,6 +504,7 @@ fn renderTarget(g: *Gpu, images: *gfx.Images, image: abi.Image, w: u32, h: u32, if (t.msaa) |m| c.SDL_ReleaseGPUTexture(g.device, m); t.msaa = try g.texture(w, h, color_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, samples); t.samples = samples; + t.msaa_fresh = true; } if (stencil and (t.stencil == null or t.stencil_samples != samples)) { if (t.stencil) |s| c.SDL_ReleaseGPUTexture(g.device, s); -- cgit v1.3