diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 04:36:56 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 49f659e24c5ace2568555a0a37772bc56a0be393 (patch) | |
| tree | 727e3576497e955e2e36fccc7052680cd0b7c1db /src/gui/gui.zig | |
| parent | 2d0cb01f1e060c9cf182df491a077cd10d4bfb11 (diff) | |
| download | pardes-49f659e24c5ace2568555a0a37772bc56a0be393.tar.gz pardes-49f659e24c5ace2568555a0a37772bc56a0be393.zip | |
gui: the renderer's resources, pictures, overlay and atlas upload are sketchbook's (src/gpu/Cells.zig), moved as they were, the fourth step of the GPU move, its first half: the atlas's texture, sampler and upload buffer, the pictures' sampler, the four pipelines, the overlay's buffers and their release (runNative: Cells.init and deinit, on Present's device, before the device goes), the cells' and the pictures' instance buffers, the pictures' textures by key (releaseNativeImage, clearNativeImages, uploadNativeTexture's upload and uploadTextureRows: Cells.image, which takes the RGBA, its size and stride and the dirty rows, still the whole picture or only the rows that changed into the texture held), the pictures' instance upload (Cells.mapImages and uploadImages around gui.zig's emitPreparedImage) and their draws (drawNativeImagesGpu: Cells.drawImages, each picture's clip, imageScissor's, kept beside its texture as the frame's instances are written), the addon pipeline (made on first use as before: Cells.makeAddonPipeline), uploadAtlas (over the Atlas's dirty rows), uploadOverlayGpu, pushLayerOpacity and drawOverlayGpu leave gui.zig; the seven SPIR-V embeds go with the pipelines (build.zig gives the `<name>.spv` imports to the Cells module, sketchbook's GLSL through glslc or its prebuilt copies, as before); the Gui holds a pointer to runNative's Cells (`cells`) and no longer the device; an addon's texture is still handed over borrowed and premultiplied; renderFrame's command buffer, cell upload and render pass stay for the second half; frame corpus unchanged (77 frames)
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 343 |
1 files changed, 43 insertions, 300 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index c7fa898b..3dfafd68 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -42,13 +42,6 @@ const TIOCSWINSZ: c_int = @bitCast(@as(u32, if (@hasDecl(posix.T, "IOCSWINSZ")) const log = std.log.scoped(.gui); const font_ttf = @embedFile("AdwaitaMono-Regular.ttf"); -const vert_spv = @embedFile("ui.vert.spv"); -const frag_spv = @embedFile("ui.frag.spv"); -const decor_frag_spv = @embedFile("decor.frag.spv"); -const overlay_vert_spv = @embedFile("overlay.vert.spv"); -const overlay_frag_spv = @embedFile("overlay.frag.spv"); -const image_vert_spv = @embedFile("image.vert.spv"); -const image_frag_spv = @embedFile("image.frag.spv"); const bg_default = [3]u8{ 18, 18, 18 }; const fg_default = [3]u8{ 204, 204, 204 }; @@ -429,32 +422,15 @@ const Gui = struct { addon_hash: [pardes.MAX_PANES]u64 = @splat(0), addon_serial: [pardes.MAX_PANES]u32 = @splat(0), addon_failed: [pardes.MAX_PANES]u64 = @splat(0), - addon_pipeline: ?*c.SDL_GPUGraphicsPipeline = null, window: *c.SDL_Window, /// The device, the window's claim or soft presenter, the frame's target /// and the capture's read back (runNative owns it). present: *Present, - /// present's device, which everything else is made on. - device: *c.SDL_GPUDevice, - pipeline: *c.SDL_GPUGraphicsPipeline, - decor_pipeline: *c.SDL_GPUGraphicsPipeline, - overlay_pipeline: *c.SDL_GPUGraphicsPipeline, - image_pipeline: *c.SDL_GPUGraphicsPipeline, - atlas_tex: *c.SDL_GPUTexture, - atlas_sampler: *c.SDL_GPUSampler, - linear_sampler: *c.SDL_GPUSampler, - atlas_xfer: *c.SDL_GPUTransferBuffer, - vbuf: ?*c.SDL_GPUBuffer = null, - vxfer: ?*c.SDL_GPUTransferBuffer = null, - vbuf_cells: u32 = 0, - overlay_vbuf: *c.SDL_GPUBuffer, - overlay_vxfer: *c.SDL_GPUTransferBuffer, + /// The pipelines, the atlas's texture, the pictures' textures and the + /// buffers the frame is drawn from, on present's device (runNative owns + /// it). + cells: *Cells, overlay_vertices: []OverlayVertex, - image_vbuf: ?*c.SDL_GPUBuffer = null, - image_vxfer: ?*c.SDL_GPUTransferBuffer = null, - image_capacity: u32 = 0, - /// The textures of the placements shown, by key. - native_images: std.AutoHashMapUnmanaged(u64, *c.SDL_GPUTexture) = .empty, prepared_images: std.ArrayListUnmanaged(PreparedImage) = .empty, /// The glyph art of the placements whose panes ask for it (Petscii /// on), and the frame copy it is drawn into. @@ -909,8 +885,6 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di } else log.err("SDL_CreateCursor: {s}", .{c.SDL_GetError()}); var presenter = try Present.init(window, test_mode, surface_opacity); defer presenter.deinit(); - const device = presenter.device; - const swapchain_format = presenter.swapchain_format; font_allocator = gpa; var atlas = Atlas.init(gpa, font_ttf) catch |err| { @@ -942,34 +916,10 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di _ = c.SDL_SyncWindow(window); } - const atlas_tex = Cells.texture2d(device, c.SDL_GPU_TEXTUREFORMAT_R8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, atlas_w, atlas_h) orelse return error.GpuCreate; - - var samp_info = std.mem.zeroes(c.SDL_GPUSamplerCreateInfo); - samp_info.min_filter = c.SDL_GPU_FILTER_NEAREST; - samp_info.mag_filter = c.SDL_GPU_FILTER_NEAREST; - samp_info.mipmap_mode = c.SDL_GPU_FILTER_NEAREST; - samp_info.address_mode_u = c.SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE; - samp_info.address_mode_v = c.SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE; - samp_info.address_mode_w = c.SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE; - const atlas_sampler = c.SDL_CreateGPUSampler(device, &samp_info) orelse return error.GpuCreate; - samp_info.min_filter = c.SDL_GPU_FILTER_LINEAR; - samp_info.mag_filter = c.SDL_GPU_FILTER_LINEAR; - samp_info.mipmap_mode = c.SDL_GPU_FILTER_LINEAR; - const linear_sampler = c.SDL_CreateGPUSampler(device, &samp_info) orelse return error.GpuCreate; - - var ax_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = atlas_w * atlas_h, .props = 0 }; - const atlas_xfer = c.SDL_CreateGPUTransferBuffer(device, &ax_info) orelse return error.GpuCreate; - - const pipeline = try Cells.makePipeline(device, swapchain_format, vert_spv, frag_spv, 1, 1, Cells.cell_input, std.mem.zeroes(c.SDL_GPUColorTargetBlendState)); - const decor_pipeline = try Cells.makePipeline(device, swapchain_format, vert_spv, decor_frag_spv, 0, 1, Cells.cell_input, Cells.backgroundLayerBlend()); - const overlay_pipeline = try Cells.makePipeline(device, swapchain_format, overlay_vert_spv, overlay_frag_spv, 0, 1, Cells.overlay_input, Cells.backgroundLayerBlend()); - const image_pipeline = try Cells.makePipeline(device, swapchain_format, image_vert_spv, image_frag_spv, 1, 1, Cells.image_input, Cells.backgroundLayerBlend()); - - const overlay_buffer_size: u32 = @intCast(max_overlay_vertices * @sizeOf(OverlayVertex)); - var ovb_info = c.SDL_GPUBufferCreateInfo{ .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX, .size = overlay_buffer_size, .props = 0 }; - const overlay_vbuf = c.SDL_CreateGPUBuffer(device, &ovb_info) orelse return error.GpuCreate; - var ovx_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = overlay_buffer_size, .props = 0 }; - const overlay_vxfer = c.SDL_CreateGPUTransferBuffer(device, &ovx_info) orelse return error.GpuCreate; + var cells = try Cells.init(presenter.device, presenter.swapchain_format, max_overlay_vertices); + // The pipelines, textures and buffers it holds, whatever they were + // regrown to, before the device (presenter's defer). + defer cells.deinit(gpa); const overlay_vertices = try gpa.alloc(OverlayVertex, max_overlay_vertices); defer gpa.free(overlay_vertices); @@ -980,17 +930,7 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di .link_cursor = link_cursor, .box_cursor = box_cursor, .present = &presenter, - .device = device, - .pipeline = pipeline, - .decor_pipeline = decor_pipeline, - .overlay_pipeline = overlay_pipeline, - .image_pipeline = image_pipeline, - .atlas_tex = atlas_tex, - .atlas_sampler = atlas_sampler, - .linear_sampler = linear_sampler, - .atlas_xfer = atlas_xfer, - .overlay_vbuf = overlay_vbuf, - .overlay_vxfer = overlay_vxfer, + .cells = &cells, .overlay_vertices = overlay_vertices, .atlas = atlas, .px = px, @@ -1012,40 +952,19 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di .window_h = want_h, .window_slot = &window_slot, }; - // The pipelines, textures and buffers the Gui holds, whatever they were - // regrown to; the image ones are released below. - defer { - c.SDL_ReleaseGPUGraphicsPipeline(device, g.pipeline); - c.SDL_ReleaseGPUGraphicsPipeline(device, g.decor_pipeline); - c.SDL_ReleaseGPUGraphicsPipeline(device, g.overlay_pipeline); - c.SDL_ReleaseGPUTexture(device, g.atlas_tex); - c.SDL_ReleaseGPUSampler(device, g.atlas_sampler); - c.SDL_ReleaseGPUSampler(device, g.linear_sampler); - c.SDL_ReleaseGPUTransferBuffer(device, g.atlas_xfer); - c.SDL_ReleaseGPUBuffer(device, g.overlay_vbuf); - c.SDL_ReleaseGPUTransferBuffer(device, g.overlay_vxfer); - if (g.vbuf) |buffer| c.SDL_ReleaseGPUBuffer(device, buffer); - if (g.vxfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(device, transfer); - if (g.image_vbuf) |buffer| c.SDL_ReleaseGPUBuffer(device, buffer); - if (g.image_vxfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(device, transfer); - if (g.gamepad) |pad| c.SDL_CloseGamepad(pad); - } + defer if (g.gamepad) |pad| c.SDL_CloseGamepad(pad); setGuiFontName(&g, "Adwaita Mono"); loadFallbackFonts(&g, gpa); defer g.atlas.deinit(gpa); defer g.words.deinit(gpa); defer { if (g.addon_gpu) |*gpu| gpu.deinit(); - if (g.addon_pipeline) |pipeline_| c.SDL_ReleaseGPUGraphicsPipeline(device, pipeline_); - clearNativeImages(&g); - g.native_images.deinit(gpa); g.prepared_images.deinit(gpa); g.art.deinit(gpa); if (g.art_arena) |*arena| arena.deinit(); g.cover.deinit(gpa); g.decor.deinit(gpa); } - defer c.SDL_ReleaseGPUGraphicsPipeline(device, image_pipeline); defer gpa.free(g.scroll_edge); // grown on demand by stepScroll defer g.glide.deinit(gpa); resetGlyphAtlas(&g); @@ -1251,7 +1170,7 @@ fn localSession( // One view, the window's (host_io.restoreCore). shell.gview = 0; shell.stopPtys(); - clearNativeImages(g); + g.cells.clearImages(); g.prepared_images.clearRetainingCapacity(); nc.view().frontend.native_images = true; nc.view().frontend.native_image_patches = true; @@ -3977,17 +3896,6 @@ fn scrollScissor(g: *const Gui, layout: CellLayout, sw: u32, sh: u32) c.SDL_Rect return .{ .x = x0, .y = y0, .w = x1 - x0, .h = y1 - y0 }; } -fn releaseNativeImage(g: *Gui, key: u64) void { - if (g.native_images.fetchRemove(key)) |removed| - c.SDL_ReleaseGPUTexture(g.device, removed.value); -} - -fn clearNativeImages(g: *Gui) void { - var iterator = g.native_images.valueIterator(); - while (iterator.next()) |texture| c.SDL_ReleaseGPUTexture(g.device, texture.*); - g.native_images.clearRetainingCapacity(); -} - /// A placement the window shows as a texture: pixels, not glyph art. fn texturePlacement(place: pardes.Placement) bool { return !place.art.glyph and validImageBytes(place) != null; @@ -4026,72 +3934,13 @@ fn uploadNativeTexture( place: pardes.Placement, ) !void { const byte_len = validImageBytes(place) orelse return error.BadImage; - const key = place.key; - if (g.native_images.contains(key)) return; const r = place.raster; - const row = r.stride; - if (place.dirty) |dirty| dirty: { - // The core changed only these rows since a picture we still hold: - // write them into that texture rather than sending the whole page. - if (dirty.y + dirty.h > r.h) break :dirty; - const texture = g.native_images.get(dirty.from) orelse break :dirty; - try uploadTextureRows(g, cmd, texture, r.bytes()[dirty.y * row ..][0 .. dirty.h * row], r.w, dirty.y, dirty.h); - try g.native_images.put(gpa, key, texture); - _ = g.native_images.remove(dirty.from); - return; - } - - const texture = Cells.texture2d(g.device, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, r.w, r.h) orelse return error.GpuCreate; - errdefer c.SDL_ReleaseGPUTexture(g.device, texture); - try uploadTextureRows(g, cmd, texture, r.bytes()[0..byte_len], r.w, 0, r.h); - try g.native_images.put(gpa, key, texture); -} - -/// Copy `rows` rows of RGBA, `width` pixels each, into `texture` at row `y`. -fn uploadTextureRows( - g: *Gui, - cmd: *c.SDL_GPUCommandBuffer, - texture: *c.SDL_GPUTexture, - rgba: []const u8, - width: usize, - y: usize, - rows: usize, -) !void { - var xf_info = c.SDL_GPUTransferBufferCreateInfo{ - .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, - .size = @intCast(rgba.len), - .props = 0, - }; - const transfer = c.SDL_CreateGPUTransferBuffer(g.device, &xf_info) orelse return error.GpuCreate; - defer c.SDL_ReleaseGPUTransferBuffer(g.device, transfer); - const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, transfer, false) orelse return error.GpuMap); - @memcpy(mapped[0..rgba.len], rgba); - c.SDL_UnmapGPUTransferBuffer(g.device, transfer); - - const copy = c.SDL_BeginGPUCopyPass(cmd); - const src = c.SDL_GPUTextureTransferInfo{ - .transfer_buffer = transfer, - .offset = 0, - .pixels_per_row = @intCast(width), - .rows_per_layer = @intCast(rows), - }; - const dst = c.SDL_GPUTextureRegion{ - .texture = texture, - .mip_level = 0, - .layer = 0, - .x = 0, - .y = @intCast(y), - .z = 0, - .w = @intCast(width), - .h = @intCast(rows), - .d = 1, - }; - c.SDL_UploadToGPUTexture(copy, &src, &dst, false); - c.SDL_EndGPUCopyPass(copy); + const dirty: ?Cells.Dirty = if (place.dirty) |d| .{ .from = d.from, .y = d.y, .h = d.h } else null; + try g.cells.image(gpa, cmd, place.key, r.bytes()[0..byte_len], r.w, r.h, r.stride, dirty); } fn appendPreparedImage(g: *Gui, gpa: std.mem.Allocator, place: pardes.Placement) void { - const texture = g.native_images.get(place.key) orelse return; + const texture = g.cells.native_images.get(place.key) orelse return; if (nativePlaceGeometry(g, place) == null) return; g.prepared_images.append(gpa, .{ .place = place, .texture = texture }) catch {}; } @@ -4150,14 +3999,14 @@ fn prepareNativeImages( while (true) { var stale: [pardes.MAX_PANES]u64 = undefined; var stale_len: usize = 0; - var iterator = g.native_images.iterator(); + var iterator = g.cells.native_images.iterator(); while (iterator.next()) |entry| { if (surfaceHasNativeKey(surface, entry.key_ptr.*)) continue; stale[stale_len] = entry.key_ptr.*; stale_len += 1; if (stale_len == stale.len) break; } - for (stale[0..stale_len]) |key| releaseNativeImage(g, key); + for (stale[0..stale_len]) |key| g.cells.releaseImage(key); if (stale_len < stale.len) break; } @@ -4171,25 +4020,18 @@ fn prepareNativeImages( const capacity = std.math.cast(u32, g.prepared_images.items.len) orelse return false; if (capacity == 0) return true; - Cells.growInstances(g.device, &g.image_vbuf, &g.image_vxfer, &g.image_capacity, capacity, @sizeOf(ImageInstance)) catch return false; - - const ptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.image_vxfer.?, false) orelse return false); - const instances: [*]ImageInstance = @ptrCast(@alignCast(ptr)); + const instances = g.cells.mapImages(gpa, capacity) orelse return false; const win_w: f32 = @floatFromInt(sw); const win_h: f32 = @floatFromInt(sh); var idx: u32 = 0; for (g.prepared_images.items) |prepared| { + // Drawn in this order, instance by instance (Cells.drawImages). + g.cells.images.appendAssumeCapacity(.{ .premultiplied = prepared.premultiplied, .texture = prepared.texture, .clip = imageScissor(g, prepared.place) }); if (!emitPreparedImage(g, surface, &instances[idx], prepared, win_w, win_h)) continue; idx += 1; } - c.SDL_UnmapGPUTransferBuffer(g.device, g.image_vxfer.?); std.debug.assert(idx == capacity); - - const copy = c.SDL_BeginGPUCopyPass(cmd); - const src = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = g.image_vxfer.?, .offset = 0 }; - const dst = c.SDL_GPUBufferRegion{ .buffer = g.image_vbuf.?, .offset = 0, .size = idx * @sizeOf(ImageInstance) }; - c.SDL_UploadToGPUBuffer(copy, &src, &dst, false); - c.SDL_EndGPUCopyPass(copy); + g.cells.uploadImages(cmd, idx); return true; } @@ -4202,35 +4044,6 @@ fn imageScissor(g: *const Gui, place: pardes.Placement) c.SDL_Rect { }; } -fn drawNativeImagesGpu( - g: *Gui, - rp: ?*c.SDL_GPURenderPass, - batch: Group, - whole: c.SDL_Rect, -) void { - const pass = rp orelse return; - c.SDL_BindGPUGraphicsPipeline(pass, g.image_pipeline); - var idx = batch.image_start; - const end = batch.image_start + batch.image_count; - for (g.prepared_images.items[batch.image_start..end]) |prepared| { - c.SDL_BindGPUGraphicsPipeline(pass, if (prepared.premultiplied) g.addon_pipeline.? else g.image_pipeline); - const clip = imageScissor(g, prepared.place); - if (clip.w > 0 and clip.h > 0) { - c.SDL_SetGPUScissor(pass, &clip); - const sampler = c.SDL_GPUTextureSamplerBinding{ .texture = prepared.texture, .sampler = g.linear_sampler }; - c.SDL_BindGPUFragmentSamplers(pass, 0, &sampler, 1); - const binding = c.SDL_GPUBufferBinding{ .buffer = g.image_vbuf.?, .offset = idx * @sizeOf(ImageInstance) }; - c.SDL_BindGPUVertexBuffers(pass, 0, &binding, 1); - c.SDL_DrawGPUPrimitives(pass, 6, 1, 0, 0); - } - idx += 1; - } - std.debug.assert(idx == batch.image_start + batch.image_count); - // Back to the whole target: what draws after a picture may run on past - // the grid, as a rule and a band do. - c.SDL_SetGPUScissor(pass, &whole); -} - fn renderFrame( g: *Gui, gpa: std.mem.Allocator, @@ -4252,7 +4065,7 @@ fn renderFrame( if (cursor) |value| _ = c.SDL_SetCursor(value); } g.words.memo = .{}; // positional, and this frame's cells are new - const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return false; + const cmd = c.SDL_AcquireGPUCommandBuffer(g.cells.device) orelse return false; var command_consumed = false; defer if (!command_consumed) { _ = c.SDL_SubmitGPUCommandBuffer(cmd); @@ -4300,7 +4113,7 @@ fn renderFrame( core.frames.caret = caretRow(g, surface, now) orelse 0; const cursors_shown = !quad_mode; var overlay_count = buildOverlay(g, core.settings.pet, core.settings.jump_trail, surface, now, sw, sh); - if (overlay_count != 0 and !uploadOverlayGpu(g, cmd, overlay_count)) + if (overlay_count != 0 and !g.cells.uploadOverlay(cmd, g.overlay_vertices[0..overlay_count])) overlay_count = 0; if (!prepareNativeImages(g, gpa, cmd, surface, &groups, sw, sh)) { g.prepared_images.clearRetainingCapacity(); @@ -4367,9 +4180,9 @@ fn renderFrame( // The shifted rows of a scroll: its body, a glide's rows past it, and // a body layer's rows past the grid's. const capacity = std.math.add(u32, cell_total, 3 * cells + decor_len) catch return error.GpuCapacity; - try Cells.growInstances(g.device, &g.vbuf, &g.vxfer, &g.vbuf_cells, capacity, @sizeOf(CellInstance)); + try Cells.growInstances(g.cells.device, &g.cells.vbuf, &g.cells.vxfer, &g.cells.vbuf_cells, capacity, @sizeOf(CellInstance)); - const vptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.vxfer.?, false) orelse { + const vptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.cells.device, g.cells.vxfer.?, false) orelse { command_consumed = true; _ = c.SDL_SubmitGPUCommandBuffer(cmd); return false; @@ -4425,13 +4238,13 @@ fn renderFrame( const held: u16 = if (scrolling) |id| if (core.panes[id]) |pane| pane.context_rows else 0 else 0; shifted = emitScrollRows(g, instances, cell_total, surface, layout, win_w, win_h, page, held); @memcpy(instances[cell_total + shifted ..][0..decor_len], g.decor.items); - c.SDL_UnmapGPUTransferBuffer(g.device, g.vxfer.?); + c.SDL_UnmapGPUTransferBuffer(g.cells.device, g.cells.vxfer.?); - if (g.atlas.atlas_dirty_top < g.atlas.atlas_dirty_bottom) uploadAtlas(g, cmd); + if (g.atlas.atlas_dirty_top < g.atlas.atlas_dirty_bottom) g.cells.uploadAtlas(cmd, &g.atlas); const copy = c.SDL_BeginGPUCopyPass(cmd); - const src = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = g.vxfer.?, .offset = 0 }; - const dst = c.SDL_GPUBufferRegion{ .buffer = g.vbuf.?, .offset = 0, .size = (cell_total + shifted + decor_len) * @sizeOf(CellInstance) }; + const src = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = g.cells.vxfer.?, .offset = 0 }; + const dst = c.SDL_GPUBufferRegion{ .buffer = g.cells.vbuf.?, .offset = 0, .size = (cell_total + shifted + decor_len) * @sizeOf(CellInstance) }; c.SDL_UploadToGPUBuffer(copy, &src, &dst, false); c.SDL_EndGPUCopyPass(copy); } @@ -4440,40 +4253,40 @@ fn renderFrame( building = false; if (frame_probe) |probe| probe.zone(.gui_build, probe_build); const rp = c.SDL_BeginGPURenderPass(cmd, &color_target, 1, null); - pushLayerOpacity(cmd, rp, background_opacity); + Cells.pushLayerOpacity(cmd, rp, background_opacity); const whole = c.SDL_Rect{ .x = 0, .y = 0, .w = @intCast(sw), .h = @intCast(sh) }; const decor_base = cell_total + shifted; for (groups.items[0..groups.len], 0..) |group, index| { const has_shifted = index == 0 and shifted != 0; - if ((group.cell_count != 0 or has_shifted) and g.vbuf != null) { - c.SDL_BindGPUGraphicsPipeline(rp, g.pipeline); - const samp_binding = c.SDL_GPUTextureSamplerBinding{ .texture = g.atlas_tex, .sampler = g.atlas_sampler }; + if ((group.cell_count != 0 or has_shifted) and g.cells.vbuf != null) { + c.SDL_BindGPUGraphicsPipeline(rp, g.cells.pipeline); + const samp_binding = c.SDL_GPUTextureSamplerBinding{ .texture = g.cells.atlas_tex, .sampler = g.cells.atlas_sampler }; c.SDL_BindGPUFragmentSamplers(rp, 0, &samp_binding, 1); if (group.cell_count != 0) { - const binding = c.SDL_GPUBufferBinding{ .buffer = g.vbuf.?, .offset = group.cell_start * @sizeOf(CellInstance) }; + const binding = c.SDL_GPUBufferBinding{ .buffer = g.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) { const clip = scrollScissor(g, layout, sw, sh); c.SDL_SetGPUScissor(rp, &clip); - const binding = c.SDL_GPUBufferBinding{ .buffer = g.vbuf.?, .offset = cell_total * @sizeOf(CellInstance) }; + const binding = c.SDL_GPUBufferBinding{ .buffer = g.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) drawNativeImagesGpu(g, rp, group, whole); - if (group.decor_count != 0 and g.vbuf != null) { - c.SDL_BindGPUGraphicsPipeline(rp, g.decor_pipeline); + if (group.image_count != 0) g.cells.drawImages(rp, group.image_start, group.image_count, whole); + if (group.decor_count != 0 and g.cells.vbuf != null) { + c.SDL_BindGPUGraphicsPipeline(rp, g.cells.decor_pipeline); // Ink keeps its colour at any WindowOpacity. - pushLayerOpacity(cmd, rp, if (index == groups.ink) 1 else background_opacity); - const binding = c.SDL_GPUBufferBinding{ .buffer = g.vbuf.?, .offset = (decor_base + group.decor_start) * @sizeOf(CellInstance) }; + Cells.pushLayerOpacity(cmd, rp, if (index == groups.ink) 1 else background_opacity); + const binding = c.SDL_GPUBufferBinding{ .buffer = g.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); } } - drawOverlayGpu(g, cmd, rp, overlay_count); + g.cells.drawOverlay(cmd, rp, overlay_count, backgroundOpacity(g.applied_window_opacity)); c.SDL_EndGPURenderPass(rp); command_consumed = true; @@ -5140,17 +4953,7 @@ fn testGui(cell_w: u32, cell_h: u32, tagline_width: u32, tagline_height: u32) Gu return .{ .window = undefined, .present = undefined, - .device = undefined, - .pipeline = undefined, - .decor_pipeline = undefined, - .overlay_pipeline = undefined, - .image_pipeline = undefined, - .atlas_tex = undefined, - .atlas_sampler = undefined, - .linear_sampler = undefined, - .atlas_xfer = undefined, - .overlay_vbuf = undefined, - .overlay_vxfer = undefined, + .cells = undefined, .overlay_vertices = &.{}, .atlas = .{ .font = undefined, .atlas_stage = &.{}, .glyphs = undefined }, .px = 0, @@ -5182,28 +4985,6 @@ fn cachedGlyph(g: *Gui, key: GlyphKey) ?Slot { return s; } -/// 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. -fn uploadAtlas(g: *Gui, cmd: *c.SDL_GPUCommandBuffer) void { - const top = g.atlas.atlas_dirty_top; - const rows = g.atlas.atlas_dirty_bottom - top; - const ptr = c.SDL_MapGPUTransferBuffer(g.device, g.atlas_xfer, false) orelse return; - const dst: [*]u8 = @ptrCast(ptr); - @memcpy(dst[top * atlas_w ..][0 .. rows * atlas_w], g.atlas.atlas_stage[top * atlas_w ..][0 .. rows * atlas_w]); - c.SDL_UnmapGPUTransferBuffer(g.device, g.atlas_xfer); - - const copy = c.SDL_BeginGPUCopyPass(cmd); - const src = c.SDL_GPUTextureTransferInfo{ .transfer_buffer = g.atlas_xfer, .offset = top * atlas_w, .pixels_per_row = atlas_w, .rows_per_layer = rows }; - const dstregion = c.SDL_GPUTextureRegion{ .texture = g.atlas_tex, .mip_level = 0, .layer = 0, .x = 0, .y = top, .z = 0, .w = atlas_w, .h = rows, .d = 1 }; - c.SDL_UploadToGPUTexture(copy, &src, &dstregion, false); - c.SDL_EndGPUCopyPass(copy); - g.atlas.atlas_dirty_top = atlas_h; - g.atlas.atlas_dirty_bottom = 0; -} - fn backgroundOpacity(percent: u8) f32 { std.debug.assert(percent <= 100); return @as(f32, @floatFromInt(percent)) / 100; @@ -6141,40 +5922,6 @@ fn appendCursorGlide(g: *Gui, surface: *const pardes.Surface, now_ns: u64, build builder.addTri(px[0][0], px[0][1], px[2][0], px[2][1], px[3][0], px[3][1], color); } -fn uploadOverlayGpu(g: *Gui, cmd: *c.SDL_GPUCommandBuffer, vertex_count: u32) bool { - const byte_len: u32 = vertex_count * @sizeOf(OverlayVertex); - const ptr = c.SDL_MapGPUTransferBuffer(g.device, g.overlay_vxfer, false) orelse return false; - const dst: [*]u8 = @ptrCast(ptr); - const src: [*]const u8 = @ptrCast(g.overlay_vertices.ptr); - @memcpy(dst[0..byte_len], src[0..byte_len]); - c.SDL_UnmapGPUTransferBuffer(g.device, g.overlay_vxfer); - const copy = c.SDL_BeginGPUCopyPass(cmd); - const src_loc = c.SDL_GPUTransferBufferLocation{ .transfer_buffer = g.overlay_vxfer, .offset = 0 }; - const dst_region = c.SDL_GPUBufferRegion{ .buffer = g.overlay_vbuf, .offset = 0, .size = byte_len }; - c.SDL_UploadToGPUBuffer(copy, &src_loc, &dst_region, false); - c.SDL_EndGPUCopyPass(copy); - return true; -} - -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 - // alpha contribution is multiplied by this constant separately, so a - // rail repainted over a gutter still has alpha p, not p + p*(1-p). - c.SDL_SetGPUBlendConstants(pass, .{ .r = opacity, .g = opacity, .b = opacity, .a = opacity }); -} - -fn drawOverlayGpu(g: *Gui, cmd: *c.SDL_GPUCommandBuffer, rp: ?*c.SDL_GPURenderPass, vertex_count: u32) void { - if (vertex_count == 0) return; - const pass = rp orelse return; - c.SDL_BindGPUGraphicsPipeline(pass, g.overlay_pipeline); - const binding = c.SDL_GPUBufferBinding{ .buffer = g.overlay_vbuf, .offset = 0 }; - c.SDL_BindGPUVertexBuffers(pass, 0, &binding, 1); - pushLayerOpacity(cmd, pass, backgroundOpacity(g.applied_window_opacity)); - c.SDL_DrawGPUPrimitives(pass, vertex_count, 1, 0, 0); -} - const OverlayBuilder = struct { vertices: []OverlayVertex, len: usize = 0, @@ -8415,7 +8162,7 @@ fn prepareAddonGpu(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer }; const hash = std.hash.Wyhash.hash(frame.serial, frame.bytes); if (g.addon_failed[id] == hash) continue; - if (g.addon_gpu == null) g.addon_gpu = .init(gpa, g.device); + if (g.addon_gpu == null) g.addon_gpu = .init(gpa, g.cells.device); const gpu = &g.addon_gpu.?; const target: GpuScene.abi.Image = @fromBackingInt(@intCast(id + 1)); if (g.addon_hash[id] != hash or g.addon_serial[id] != frame.serial) { @@ -8443,11 +8190,7 @@ fn prepareAddonGpu(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer g.addon_serial[id] = frame.serial; } const out = gpu.output(target) orelse continue; - if (g.addon_pipeline == null) { - var blend = Cells.backgroundLayerBlend(); - blend.src_color_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE; - g.addon_pipeline = Cells.makePipeline(g.device, g.present.swapchain_format, image_vert_spv, image_frag_spv, 1, 1, Cells.image_input, blend) catch continue; - } + g.cells.makeAddonPipeline() catch continue; g.prepared_images.append(gpa, .{ .premultiplied = true, .texture = out.texture, |
