summaryrefslogtreecommitdiff
path: root/src/gpu/Cells.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 05:05:41 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit83527d21ac42adc16d59ad8bfffebd132a5ef98b (patch)
tree794a8936edb369dfcef9ad15a2c0188710f92953 /src/gpu/Cells.zig
parentada7e4137d66aa7c7c463bb8521c0db4e7fac18a (diff)
downloadsketchbook-83527d21ac42adc16d59ad8bfffebd132a5ef98b.tar.gz
sketchbook-83527d21ac42adc16d59ad8bfffebd132a5ef98b.zip
Add pardes's frame recording to Cells
Moved from pardes's src/gui/gui.zig (step S4b, the last of moving its renderer here): begin and submit acquire a frame's command buffer and give a frame up; mapCells grows and maps the cell instance buffer for the embedder to write in place; uploadCells sends it after the atlas's dirty rows; record makes the frame's render pass, cleared to the embedder's colour, and draws its groups (Group: cells, then pictures, then decor, each a run of instances), a scroll's shifted rows clipped to the rectangle given, then the overlay. uploadAtlas, drawImages, pushLayerOpacity and drawOverlay are Cells' own now.
Diffstat (limited to 'src/gpu/Cells.zig')
-rw-r--r--src/gpu/Cells.zig119
1 files changed, 114 insertions, 5 deletions
diff --git a/src/gpu/Cells.zig b/src/gpu/Cells.zig
index 5055d31..9ed1741 100644
--- a/src/gpu/Cells.zig
+++ b/src/gpu/Cells.zig
@@ -1,7 +1,8 @@
//! pardes's cell renderer on SDL_GPU: the shaders' instance ABI
//! (src/gpu/shaders/*.glsl), the pipelines over it, the glyph atlas's
//! texture and its upload, the pictures' textures and their draws, the
-//! overlay's triangles, and the resource helpers, moved from pardes's
+//! overlay's triangles, a frame's uploads and its render pass, and the
+//! resource helpers, moved from pardes's
//! gui.zig as they were. `c` is the embedder's own SDL translation (pardes's:
//! SDL3 and font.h), so the embedder's SDL is the one linked; the SPIR-V is
//! the build's (`<name>.spv` imports, from src/gpu/shaders/).
@@ -129,12 +130,120 @@ pub fn makeAddonPipeline(cells: *Cells) !void {
cells.addon_pipeline = try makePipeline(cells.device, cells.format, image_vert_spv, image_frag_spv, 1, 1, image_input, blend);
}
+/// What one group draws, in this order: its cells, its images, then its
+/// decor.
+pub const Group = struct {
+ cell_start: u32 = 0,
+ cell_count: u32 = 0,
+ image_start: u32 = 0,
+ image_count: u32 = 0,
+ decor_start: u32 = 0,
+ decor_count: u32 = 0,
+};
+
+/// The frame's command buffer: null when the device gives none.
+pub fn begin(cells: *Cells) ?*c.SDL_GPUCommandBuffer {
+ return c.SDL_AcquireGPUCommandBuffer(cells.device);
+}
+
+/// A frame given up: what it recorded so far is submitted, nothing drawn.
+pub fn submit(cmd: *c.SDL_GPUCommandBuffer) void {
+ _ = c.SDL_SubmitGPUCommandBuffer(cmd);
+}
+
+/// The frame's cell and decor instances, room for `n`, mapped for the
+/// caller to write; uploadCells sends them. Null when the buffer will not
+/// map.
+pub fn mapCells(cells: *Cells, n: u32) !?[*]CellInstance {
+ try growInstances(cells.device, &cells.vbuf, &cells.vxfer, &cells.vbuf_cells, n, @sizeOf(CellInstance));
+ const vptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(cells.device, cells.vxfer.?, false) orelse return null);
+ return @ptrCast(@alignCast(vptr));
+}
+
+/// The first `n` instances mapCells gave, sent after the atlas's rows drawn
+/// since the last upload (uploadAtlas).
+pub fn uploadCells(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, n: u32, atlas: *Atlas) void {
+ c.SDL_UnmapGPUTransferBuffer(cells.device, cells.vxfer.?);
+
+ if (atlas.atlas_dirty_top < atlas.atlas_dirty_bottom) cells.uploadAtlas(cmd, atlas);
+
+ const copy = c.SDL_BeginGPUCopyPass(cmd);
+ const src = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = cells.vxfer.?, .offset = 0 };
+ const dst = c.SDL_GPUBufferRegion{ .buffer = cells.vbuf.?, .offset = 0, .size = n * @sizeOf(CellInstance) };
+ c.SDL_UploadToGPUBuffer(copy, &src, &dst, false);
+ c.SDL_EndGPUCopyPass(copy);
+}
+
+/// The frame's render pass into `target`, `sw` by `sh`, cleared to `clear`:
+/// group by group, bottom to top, its cells (the first group's followed by
+/// a scroll's `shifted` rows, after the `cell_total` instances, clipped to
+/// `scroll_clip`), its pictures and its decor (after the shifted rows), at
+/// the layer opacity `opacity`, the `ink` group's decor at 1; then the
+/// overlay's first `overlay_count` vertices.
+pub fn record(
+ cells: *Cells,
+ cmd: *c.SDL_GPUCommandBuffer,
+ target: *c.SDL_GPUTexture,
+ sw: u32,
+ sh: u32,
+ clear: c.SDL_FColor,
+ opacity: f32,
+ groups: []const Group,
+ ink: usize,
+ cell_total: u32,
+ shifted: u32,
+ scroll_clip: c.SDL_Rect,
+ overlay_count: u32,
+) void {
+ var color_target = std.mem.zeroes(c.SDL_GPUColorTargetInfo);
+ color_target.texture = target;
+ color_target.clear_color = clear;
+ color_target.load_op = c.SDL_GPU_LOADOP_CLEAR;
+ color_target.store_op = c.SDL_GPU_STOREOP_STORE;
+
+ const rp = c.SDL_BeginGPURenderPass(cmd, &color_target, 1, null);
+ pushLayerOpacity(cmd, rp, opacity);
+ const whole = c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(sw), .h = @intCast(sh) };
+ const decor_base = cell_total + shifted;
+ for (groups, 0..) |group, index| {
+ const has_shifted = index == 0 and shifted != 0;
+ if ((group.cell_count != 0 or has_shifted) and cells.vbuf != null) {
+ c.SDL_BindGPUGraphicsPipeline(rp, cells.pipeline);
+ const samp_binding = c.SDL_GPUTextureSamplerBinding{ .texture = cells.atlas_tex, .sampler = cells.atlas_sampler };
+ c.SDL_BindGPUFragmentSamplers(rp, 0, &samp_binding, 1);
+ if (group.cell_count != 0) {
+ const binding = c.SDL_GPUBufferBinding{ .buffer = cells.vbuf.?, .offset = group.cell_start * @sizeOf(CellInstance) };
+ c.SDL_BindGPUVertexBuffers(rp, 0, &binding, 1);
+ c.SDL_DrawGPUPrimitives(rp, 6, group.cell_count, 0, 0);
+ }
+ if (has_shifted) {
+ c.SDL_SetGPUScissor(rp, &scroll_clip);
+ const binding = c.SDL_GPUBufferBinding{ .buffer = cells.vbuf.?, .offset = cell_total * @sizeOf(CellInstance) };
+ c.SDL_BindGPUVertexBuffers(rp, 0, &binding, 1);
+ c.SDL_DrawGPUPrimitives(rp, 6, shifted, 0, 0);
+ c.SDL_SetGPUScissor(rp, &whole);
+ }
+ }
+ if (group.image_count != 0) cells.drawImages(rp, group.image_start, group.image_count, whole);
+ if (group.decor_count != 0 and cells.vbuf != null) {
+ c.SDL_BindGPUGraphicsPipeline(rp, cells.decor_pipeline);
+ // Ink keeps its colour at any WindowOpacity.
+ pushLayerOpacity(cmd, rp, if (index == ink) 1 else opacity);
+ const binding = c.SDL_GPUBufferBinding{ .buffer = cells.vbuf.?, .offset = (decor_base + group.decor_start) * @sizeOf(CellInstance) };
+ c.SDL_BindGPUVertexBuffers(rp, 0, &binding, 1);
+ c.SDL_DrawGPUPrimitives(rp, 6, group.decor_count, 0, 0);
+ }
+ }
+ cells.drawOverlay(cmd, rp, overlay_count, opacity);
+ c.SDL_EndGPURenderPass(rp);
+}
+
/// Uploads only the atlas rows drawn into since the last upload. Glyphs fill
/// the atlas pen-style, a band of rows at a time, so new glyphs are a band a
/// cell or two tall, not the whole 4 MB texture. The band sits at the same
/// offset in the transfer buffer as in the stage, and the texture keeps the
/// rows outside it.
-pub fn uploadAtlas(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, atlas: *Atlas) void {
+fn uploadAtlas(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, atlas: *Atlas) void {
const atlas_w = Atlas.atlas_w;
const top = atlas.atlas_dirty_top;
const rows = atlas.atlas_dirty_bottom - top;
@@ -277,7 +386,7 @@ pub fn uploadImages(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, n: u32) void {
/// Draws `count` of the frame's pictures from `start`, each clipped to its
/// rectangle; the scissor is the whole target, `whole`, after.
-pub fn drawImages(cells: *Cells, rp: ?*c.SDL_GPURenderPass, start: u32, count: u32, whole: c.SDL_Rect) void {
+fn drawImages(cells: *Cells, rp: ?*c.SDL_GPURenderPass, start: u32, count: u32, whole: c.SDL_Rect) void {
const pass = rp orelse return;
c.SDL_BindGPUGraphicsPipeline(pass, cells.image_pipeline);
var idx = start;
@@ -318,7 +427,7 @@ pub fn uploadOverlay(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, vertices: []co
return true;
}
-pub fn pushLayerOpacity(cmd: *c.SDL_GPUCommandBuffer, pass: ?*c.SDL_GPURenderPass, opacity: f32) void {
+fn pushLayerOpacity(cmd: *c.SDL_GPUCommandBuffer, pass: ?*c.SDL_GPURenderPass, opacity: f32) void {
const uniforms = [4]f32{ opacity, 0, 0, 0 };
c.SDL_PushGPUFragmentUniformData(cmd, 0, &uniforms, @sizeOf(@TypeOf(uniforms)));
// The source alpha is geometric/image coverage, not window opacity. Its
@@ -329,7 +438,7 @@ pub fn pushLayerOpacity(cmd: *c.SDL_GPUCommandBuffer, pass: ?*c.SDL_GPURenderPas
/// The overlay's first `vertex_count` vertices (uploadOverlay), drawn at
/// the layer opacity `opacity`.
-pub fn drawOverlay(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, rp: ?*c.SDL_GPURenderPass, vertex_count: u32, opacity: f32) void {
+fn drawOverlay(cells: *Cells, cmd: *c.SDL_GPUCommandBuffer, rp: ?*c.SDL_GPURenderPass, vertex_count: u32, opacity: f32) void {
if (vertex_count == 0) return;
const pass = rp orelse return;
c.SDL_BindGPUGraphicsPipeline(pass, cells.overlay_pipeline);