diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 03:50:16 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 2d0cb01f1e060c9cf182df491a077cd10d4bfb11 (patch) | |
| tree | 7f5483809a0bca9e45d9bddc641b2a4dd37406b3 /src/gui/gui.zig | |
| parent | 1d4015b1b9740a894bea288dadb11d212eceff73 (diff) | |
| download | pardes-2d0cb01f1e060c9cf182df491a077cd10d4bfb11.tar.gz pardes-2d0cb01f1e060c9cf182df491a077cd10d4bfb11.zip | |
gui: presentation is sketchbook's (src/gpu/Present.zig), moved as it was, the third step of the GPU move: the device, the window's claim or the soft presenter (a transparent Wayland window, or a claim refused), the present mode, frames in flight and the swapchain's format (runNative), the window's release and claim again (closeWindow, reopenWindow: Present.detach and attach, the window passed in), the frame's target (acquireTarget), the soft present (its renderer, texture and formats, the read back into the capture transfer buffer, the flip), the capture texture and its read back leave gui.zig; Present.init logs what runNative logged, under the gui scope; the Gui holds a pointer to runNative's Present (`present`, its device still `device`), which releases the capture's texture and transfer buffer before the device at the end (they were released in the Gui's defer, beside the gamepad); gui.zig keeps what it writes: captureFrame takes the frame Present.readFrame maps (its submit and fence times for the latency trace, as measured before) and writes latest and the series as before, writeCapturePpm asks Present.bgr for the byte order, and PARDES_FRAMES_CAPTURE is the soft present's frame tap (captureSeries, which reads its directory itself); a soft present's wait, upload and flip come back as Present.Timings and go to the frame probe's zones; releaseWindowParts, which nothing calls, loses the capture's two lines; frame corpus unchanged (77 frames)
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 303 |
1 files changed, 40 insertions, 263 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 14194122..c7fa898b 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1,6 +1,7 @@ const SketchGpu = @import("sketch_gpu"); const Cells = @import("gpu_cells"); // sketchbook's src/gpu/Cells.zig const Atlas = @import("font_atlas"); // sketchbook's src/font/Atlas.zig +const Present = @import("gpu_present"); // sketchbook's src/gpu/Present.zig const GpuScene = @import("gpu_scene"); const filesystem = @import("../fs.zig"); const std = @import("std"); @@ -430,8 +431,11 @@ const Gui = struct { 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, - swapchain_format: c.SDL_GPUTextureFormat, pipeline: *c.SDL_GPUGraphicsPipeline, decor_pipeline: *c.SDL_GPUGraphicsPipeline, overlay_pipeline: *c.SDL_GPUGraphicsPipeline, @@ -549,7 +553,6 @@ const Gui = struct { /// null attached, where the session's tag is another process's. hit_core: ?*pardes.Pardes = null, - capture: bool = false, /// Pixels the capture holds past the grid (PARDES_TEST_PAD). capture_pad: u32 = 0, capture_dir: []const u8 = "", @@ -557,27 +560,15 @@ const Gui = struct { /// frame series to look at. capture_series: bool = false, captured: u32 = 0, - capture_tex: ?*c.SDL_GPUTexture = null, - capture_tex_w: u32 = 0, - capture_tex_h: u32 = 0, - capture_xfer: ?*c.SDL_GPUTransferBuffer = null, - capture_xfer_size: u32 = 0, - soft_present: bool = false, - surface_opacity: bool = false, applied_window_opacity: u8 = 100, - soft_renderer: ?*c.SDL_Renderer = null, - soft_texture: ?*c.SDL_Texture = null, /// What a window is opened again with (reopenWindow): the flags (not - /// hidden), the present mode, its size when it was closed. + /// hidden), its size when it was closed (present keeps its present mode). win_flags: c.SDL_WindowFlags = 0, - present_mode: c.SDL_GPUPresentMode = c.SDL_GPU_PRESENTMODE_VSYNC, window_w: c_int = 1120, window_h: c_int = 720, /// The window runNative destroys at the end: the one open then, if any. window_slot: ?*?*c.SDL_Window = null, - soft_tex_w: u32 = 0, - soft_tex_h: u32 = 0, gamepad: ?*c.SDL_Gamepad = null, pad_x: f32 = 0, @@ -589,8 +580,6 @@ const Gui = struct { /// What a window held of its own let go (closeWindow lets the window go). fn releaseWindowParts(g: *Gui, gpa: std.mem.Allocator) void { - if (g.capture_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); - if (g.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(g.device, transfer); g.glide.deinit(gpa); gpa.free(g.scroll_edge); g.prepared_images.deinit(gpa); @@ -918,39 +907,10 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di if (arrow_cursor) |cur| { _ = c.SDL_SetCursor(cur); } else log.err("SDL_CreateCursor: {s}", .{c.SDL_GetError()}); - const device = c.SDL_CreateGPUDevice(c.SDL_GPU_SHADERFORMAT_SPIRV, true, null) orelse { - log.err("SDL_CreateGPUDevice: {s}", .{c.SDL_GetError()}); - return error.SdlInit; - }; - defer c.SDL_DestroyGPUDevice(device); - var soft_present = false; - var soft_renderer: ?*c.SDL_Renderer = null; - if (surface_opacity or !c.SDL_ClaimWindowForGPUDevice(device, window)) { - if (surface_opacity) - log.info("transparent window: the GPU API will not claim one, presenting through SDL_Renderer", .{}) - else - log.warn("ClaimWindowForGPUDevice: {s}; presenting in software", .{c.SDL_GetError()}); - soft_renderer = c.SDL_CreateRenderer(window, null) orelse { - log.err("SDL_CreateRenderer: {s}", .{c.SDL_GetError()}); - return error.SdlInit; - }; - soft_present = true; - } - const present_mode: c.SDL_GPUPresentMode = blk: { - if (soft_present) break :blk c.SDL_GPU_PRESENTMODE_VSYNC; - for ([_]c.SDL_GPUPresentMode{ c.SDL_GPU_PRESENTMODE_IMMEDIATE, c.SDL_GPU_PRESENTMODE_MAILBOX, c.SDL_GPU_PRESENTMODE_VSYNC }) |mode| { - if (c.SDL_WindowSupportsGPUPresentMode(device, window, mode)) break :blk mode; - } - break :blk c.SDL_GPU_PRESENTMODE_VSYNC; - }; - if (!test_mode) _ = c.SDL_SetGPUAllowedFramesInFlight(device, 1); - if (!soft_present) { - _ = c.SDL_SetGPUSwapchainParameters(device, window, c.SDL_GPU_SWAPCHAINCOMPOSITION_SDR, present_mode); - } - const swapchain_format = if (soft_present) - softTargetFormat(device) - else - c.SDL_GetGPUSwapchainTextureFormat(device, window); + 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| { @@ -1019,8 +979,8 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di .arrow_cursor = arrow_cursor, .link_cursor = link_cursor, .box_cursor = box_cursor, + .present = &presenter, .device = device, - .swapchain_format = swapchain_format, .pipeline = pipeline, .decor_pipeline = decor_pipeline, .overlay_pipeline = overlay_pipeline, @@ -1042,17 +1002,12 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di .cell_h = cell_h, .ascent = asc, .tagline_baseline = tagline.baseline, - .capture = test_mode, .capture_pad = if (test_mode) envU16(env, "PARDES_TEST_PAD") orelse 0 else 0, .capture_series = test_mode and env.get("PARDES_TEST_SERIES") != null, .capture_dir = capture_dir orelse "", - .soft_present = soft_present, .demo = if (env.get("PARDES_FRAMES_DEMO")) |path| try pardes.FrameStats.readScript(init.arena.allocator(), init.io, path) else &.{}, .demo_next = if (env.get("PARDES_FRAMES_DEMO") != null) 0 else null, - .surface_opacity = surface_opacity, - .soft_renderer = soft_renderer, .win_flags = reopen_flags, - .present_mode = present_mode, .window_w = want_w, .window_h = want_h, .window_slot = &window_slot, @@ -1073,8 +1028,6 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di 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.capture_tex) |texture| c.SDL_ReleaseGPUTexture(device, texture); - if (g.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(device, transfer); if (g.gamepad) |pad| c.SDL_CloseGamepad(pad); } setGuiFontName(&g, "Adwaita Mono"); @@ -1110,14 +1063,7 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di fn closeWindow(g: *Gui) void { _ = c.SDL_GetWindowSize(g.window, &g.window_w, &g.window_h); _ = c.SDL_StopTextInput(g.window); - if (g.soft_renderer) |renderer| { - if (g.soft_texture) |texture| c.SDL_DestroyTexture(texture); - g.soft_texture = null; - g.soft_tex_w = 0; - g.soft_tex_h = 0; - c.SDL_DestroyRenderer(renderer); - g.soft_renderer = null; - } else c.SDL_ReleaseWindowFromGPUDevice(g.device, g.window); + g.present.detach(g.window); c.SDL_DestroyWindow(g.window); if (g.window_slot) |slot| if (slot.* == g.window) { slot.* = null; @@ -1129,17 +1075,9 @@ fn closeWindow(g: *Gui) void { fn reopenWindow(g: *Gui) bool { const window = c.SDL_CreateWindow("pardes", g.window_w, g.window_h, g.win_flags) orelse return false; if (comptime builtin.os.tag == .macos) c.ui_macos_trackpad_attach(window); - if (g.soft_present) { - g.soft_renderer = c.SDL_CreateRenderer(window, null) orelse { - c.SDL_DestroyWindow(window); - return false; - }; - } else { - if (!c.SDL_ClaimWindowForGPUDevice(g.device, window)) { - c.SDL_DestroyWindow(window); - return false; - } - _ = c.SDL_SetGPUSwapchainParameters(g.device, window, c.SDL_GPU_SWAPCHAINCOMPOSITION_SDR, g.present_mode); + if (!g.present.attach(window)) { + c.SDL_DestroyWindow(window); + return false; } g.window = window; if (g.window_slot) |slot| slot.* = window; @@ -1147,7 +1085,7 @@ fn reopenWindow(g: *Gui) bool { // ponytail: another view's window asks the hit test of whichever // window's share the Gui holds then (the workspace tag sits in the same // place in every view); a test per window if one moves it. - if (!g.capture) _ = c.SDL_SetWindowHitTest(window, hitTest, g); + if (!g.present.capture) _ = c.SDL_SetWindowHitTest(window, hitTest, g); _ = c.SDL_StartTextInput(window); return true; } @@ -3163,7 +3101,8 @@ fn pollFrame(ctx: ?*anyopaque) void { var frame_capture_dir: ?[]const u8 = null; var frame_capture_count: u32 = 0; -fn captureSeries(dir: []const u8, pixels: []const u8, w: u32, h: u32) void { +fn captureSeries(pixels: []const u8, w: u32, h: u32) void { + const dir = frame_capture_dir orelse return; var path_buf: [4096]u8 = undefined; const path = std.fmt.bufPrintSentinel(&path_buf, "{s}/{d:0>6}.bgra", .{ dir, frame_capture_count }, 0) catch return; const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); @@ -3320,7 +3259,7 @@ fn syncWindowOpacity(g: *Gui, core: *pardes.Pardes) void { if (!core.settings.window_opacity_pending) return; // Offscreen captures also support per-pixel alpha. Never approximate this // with SDL_SetWindowOpacity: that would make foreground text translucent. - finishWindowOpacity(core, &g.applied_window_opacity, if (g.surface_opacity or g.capture or core.settings.window_opacity == 100) + finishWindowOpacity(core, &g.applied_window_opacity, if (g.present.surface_opacity or g.present.capture or core.settings.window_opacity == 100) null else "background transparency requires a native Wayland or alpha-capable SDL window"); @@ -4318,7 +4257,7 @@ fn renderFrame( defer if (!command_consumed) { _ = c.SDL_SubmitGPUCommandBuffer(cmd); }; - const frame_target = try acquireTarget(g, cmd, @as(u32, surface.cols) * g.cell_w + g.capture_pad, @as(u32, surface.rows) * g.cell_h + g.capture_pad) orelse { + const frame_target = try g.present.acquireTarget(cmd, g.window, @as(u32, surface.cols) * g.cell_w + g.capture_pad, @as(u32, surface.rows) * g.cell_h + g.capture_pad) orelse { command_consumed = true; _ = c.SDL_SubmitGPUCommandBuffer(cmd); return false; @@ -4541,43 +4480,22 @@ fn renderFrame( return finishFrame(g, gpa, cmd, frame_target); } -const Target = struct { texture: *c.SDL_GPUTexture, w: u32, h: u32 }; - -/// What this frame draws into: the capture texture in test mode (the grid's -/// size plus PARDES_TEST_PAD) or with a soft presenter, else the swapchain's -/// image; null when there is none to be had this time. -fn acquireTarget(g: *Gui, cmd: *c.SDL_GPUCommandBuffer, capture_w: u32, capture_h: u32) !?Target { - if (g.capture) { - if (capture_w == 0 or capture_h == 0) return null; - try ensureCaptureTexture(g, capture_w, capture_h); - return .{ .texture = g.capture_tex.?, .w = capture_w, .h = capture_h }; - } - if (g.soft_present) { - var pw: c_int = 0; - var ph: c_int = 0; - if (!c.SDL_GetWindowSizeInPixels(g.window, &pw, &ph) or pw <= 0 or ph <= 0) return null; - try ensureCaptureTexture(g, @intCast(pw), @intCast(ph)); - return .{ .texture = g.capture_tex.?, .w = @intCast(pw), .h = @intCast(ph) }; - } - var swap_tex: ?*c.SDL_GPUTexture = null; - var sw: u32 = 0; - var sh: u32 = 0; - if (!c.SDL_AcquireGPUSwapchainTexture(cmd, g.window, &swap_tex, &sw, &sh)) return null; - // A busy swapchain skips the frame. - return .{ .texture = swap_tex orelse return null, .w = sw, .h = sh }; -} - /// Submits the frame: captured, handed to the soft presenter, or presented. -fn finishFrame(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer, target: Target) !bool { - if (g.capture) { +fn finishFrame(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer, target: Present.Target) !bool { + if (g.present.capture) { try captureFrame(g, gpa, cmd, target.texture, target.w, target.h); return true; } - if (g.soft_present) { - try softPresentFrame(g, cmd, target.texture, target.w, target.h); - return true; - } - return c.SDL_SubmitGPUCommandBuffer(cmd); + var took: Present.Timings = .{}; + const presented = try g.present.finishFrame(cmd, target, &captureSeries, &took); + // A soft present's wait for the GPU, its upload of the frame and its flip. + if (g.present.soft_present) if (frame_probe) |probe| { + const now = monoNs(); + probe.zone(.gui_wait, now -| took.wait_ns); + probe.zone(.gui_upload, now -| took.upload_ns); + probe.zone(.gui_flip, now -| took.flip_ns); + }; + return presented; } const ResolvedCell = struct { @@ -5221,8 +5139,8 @@ fn cellSlot(g: *Gui, line: []const pardes.Cell, at: usize, cursor: ?usize, role: fn testGui(cell_w: u32, cell_h: u32, tagline_width: u32, tagline_height: u32) Gui { return .{ .window = undefined, + .present = undefined, .device = undefined, - .swapchain_format = undefined, .pipeline = undefined, .decor_pipeline = undefined, .overlay_pipeline = undefined, @@ -5286,128 +5204,6 @@ fn uploadAtlas(g: *Gui, cmd: *c.SDL_GPUCommandBuffer) void { g.atlas.atlas_dirty_bottom = 0; } -fn ensureCaptureTexture(g: *Gui, width: u32, height: u32) !void { - if (g.capture_tex != null and g.capture_tex_w == width and g.capture_tex_h == height) return; - const next = Cells.texture2d(g.device, g.swapchain_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, width, height) orelse return error.GpuCreate; - if (g.capture_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); - g.capture_tex = next; - g.capture_tex_w = width; - g.capture_tex_h = height; -} - -fn softTargetFormat(device: *c.SDL_GPUDevice) c.SDL_GPUTextureFormat { - const candidates = [_]c.SDL_GPUTextureFormat{ - c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, - c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, - }; - for (candidates) |format| { - if (c.SDL_GPUTextureSupportsFormat( - device, - format, - c.SDL_GPU_TEXTURETYPE_2D, - c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | c.SDL_GPU_TEXTUREUSAGE_SAMPLER, - )) return format; - } - return c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM; -} - -fn softPixelFormat(format: c.SDL_GPUTextureFormat) ?c.SDL_PixelFormat { - return switch (format) { - c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, - c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB, - => c.SDL_PIXELFORMAT_ARGB8888, - c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, - c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB, - => c.SDL_PIXELFORMAT_ABGR8888, - else => null, - }; -} - -fn ensureSoftTexture(g: *Gui, width: u32, height: u32) !*c.SDL_Texture { - if (g.soft_texture) |texture| { - if (g.soft_tex_w == width and g.soft_tex_h == height) return texture; - c.SDL_DestroyTexture(texture); - g.soft_texture = null; - } - const renderer = g.soft_renderer orelse return error.GpuCreate; - const pixel_format = softPixelFormat(g.swapchain_format) orelse return error.UnsupportedCaptureFormat; - const next = c.SDL_CreateTexture( - renderer, - pixel_format, - c.SDL_TEXTUREACCESS_STREAMING, - @intCast(width), - @intCast(height), - ) orelse return error.GpuCreate; - _ = c.SDL_SetTextureScaleMode(next, c.SDL_SCALEMODE_NEAREST); - _ = c.SDL_SetTextureBlendMode(next, c.SDL_BLENDMODE_NONE); - g.soft_texture = next; - g.soft_tex_w = width; - g.soft_tex_h = height; - return next; -} - -/// The frame in `target`, `size` bytes, copied down into `capture_xfer` -/// (grown to it first) by `cmd`, which the caller submits and waits for. -/// With no buffer to be had the command buffer is submitted as it is. -fn downloadTarget(g: *Gui, cmd: *c.SDL_GPUCommandBuffer, target: *c.SDL_GPUTexture, sw: u32, sh: u32, size: u32) !*c.SDL_GPUTransferBuffer { - if (g.capture_xfer == null or g.capture_xfer_size < size) { - var info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD, .size = size, .props = 0 }; - const next = c.SDL_CreateGPUTransferBuffer(g.device, &info) orelse { - _ = c.SDL_SubmitGPUCommandBuffer(cmd); - return error.GpuCreate; - }; - if (g.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(g.device, transfer); - g.capture_xfer = next; - g.capture_xfer_size = size; - } - const xfer = g.capture_xfer.?; - const copy = c.SDL_BeginGPUCopyPass(cmd); - const src = c.SDL_GPUTextureRegion{ .texture = target, .mip_level = 0, .layer = 0, .x = 0, .y = 0, .z = 0, .w = sw, .h = sh, .d = 1 }; - const dst = c.SDL_GPUTextureTransferInfo{ .transfer_buffer = xfer, .offset = 0, .pixels_per_row = sw, .rows_per_layer = sh }; - c.SDL_DownloadFromGPUTexture(copy, &src, &dst); - c.SDL_EndGPUCopyPass(copy); - return xfer; -} - -fn softPresentFrame(g: *Gui, cmd: *c.SDL_GPUCommandBuffer, target: *c.SDL_GPUTexture, sw: u32, sh: u32) !void { - const bpp = c.SDL_GPUTextureFormatTexelBlockSize(g.swapchain_format); - const size = c.SDL_CalculateGPUTextureFormatSize(g.swapchain_format, sw, sh, 1); - if (size == 0 or bpp != 4) { - _ = c.SDL_SubmitGPUCommandBuffer(cmd); - return error.UnsupportedCaptureFormat; - } - const xfer = try downloadTarget(g, cmd, target, sw, sh, size); - - const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; - defer c.SDL_ReleaseGPUFence(g.device, fence); - var fences = [_]*c.SDL_GPUFence{fence}; - const probe_wait = monoNs(); - if (!c.SDL_WaitForGPUFences(g.device, true, &fences, 1)) return error.GpuSubmit; - if (frame_probe) |probe| probe.zone(.gui_wait, probe_wait); - - const mapped = c.SDL_MapGPUTransferBuffer(g.device, xfer, false) orelse return error.GpuMap; - defer c.SDL_UnmapGPUTransferBuffer(g.device, xfer); - - const texture = try ensureSoftTexture(g, sw, sh); - const renderer = g.soft_renderer orelse return error.GpuCreate; - const probe_upload = monoNs(); - defer if (frame_probe) |probe| probe.zone(.gui_upload, probe_upload); - if (!c.SDL_UpdateTexture(texture, null, mapped, @intCast(sw * bpp))) return error.GpuMap; - // PARDES_FRAMES_CAPTURE: every presented frame's pixels as they went - // out, and when, for a recording at the frames' own times. One write a - // frame; nothing else. - if (frame_capture_dir) |dir| captureSeries(dir, @as([*]const u8, @ptrCast(mapped))[0..size], sw, sh); - // The cell shader already produced premultiplied backgrounds with opaque - // foreground ink. Copy that alpha unchanged; never modulate the full frame. - _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_NONE); - _ = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, if (g.surface_opacity) 0 else 255); - _ = c.SDL_RenderClear(renderer); - _ = c.SDL_RenderTexture(renderer, texture, null, null); - const probe_flip = monoNs(); - _ = c.SDL_RenderPresent(renderer); - if (frame_probe) |probe| probe.zone(.gui_flip, probe_flip); -} - fn backgroundOpacity(percent: u8) f32 { std.debug.assert(percent <= 100); return @as(f32, @floatFromInt(percent)) / 100; @@ -5436,27 +5232,14 @@ fn latencyNote(comptime fmt: []const u8, args: anytype) void { /// Writes the frame to latest.ppm in the capture directory, and in a /// series to frame-<n>.ppm too. fn captureFrame(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer, target: *c.SDL_GPUTexture, sw: u32, sh: u32) !void { - const size = c.SDL_CalculateGPUTextureFormatSize(g.swapchain_format, sw, sh, 1); - if (size == 0) { - _ = c.SDL_SubmitGPUCommandBuffer(cmd); - return error.UnsupportedCaptureFormat; - } - const xfer = try downloadTarget(g, cmd, target, sw, sh, size); - - const submit_ns = monoNs(); - const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; - defer c.SDL_ReleaseGPUFence(g.device, fence); - var fences = [_]*c.SDL_GPUFence{fence}; - if (!c.SDL_WaitForGPUFences(g.device, true, &fences, 1)) return error.GpuSubmit; - latencyNote("F {d} {d} {d}\n", .{ frame_started_ns, monoNs(), submit_ns }); + const frame = try g.present.readFrame(cmd, target, sw, sh); + defer g.present.releaseFrame(frame); + latencyNote("F {d} {d} {d}\n", .{ frame_started_ns, frame.fence_ns, frame.submit_ns }); - const mapped = c.SDL_MapGPUTransferBuffer(g.device, xfer, false) orelse return error.GpuMap; - defer c.SDL_UnmapGPUTransferBuffer(g.device, xfer); - const pixels: [*]const u8 = @ptrCast(mapped); - try writeCapturePpm(g, gpa, pixels[0..size], sw, sh, "latest", false); + try writeCapturePpm(g, gpa, frame.pixels, sw, sh, "latest", false); var name: [24]u8 = undefined; // unreachable below: "frame-" and a u32 fit 24 - if (g.capture_series) try writeCapturePpm(g, gpa, pixels[0..size], sw, sh, std.fmt.bufPrint(&name, "frame-{d:0>5}", .{g.captured}) catch unreachable, true); + if (g.capture_series) try writeCapturePpm(g, gpa, frame.pixels, sw, sh, std.fmt.bufPrint(&name, "frame-{d:0>5}", .{g.captured}) catch unreachable, true); g.captured +%= 1; } @@ -5493,13 +5276,7 @@ fn writeCapturePpm(g: *Gui, gpa: std.mem.Allocator, pixels: []const u8, width: u // A latency trace times frames, and an 8 MB file write per frame would // stall the loop it is timing. if (g.capture_dir.len == 0 or latency_fd >= 0) return; - const bpp = c.SDL_GPUTextureFormatTexelBlockSize(g.swapchain_format); - if (bpp != 4) return error.UnsupportedCaptureFormat; - const bgr = switch (g.swapchain_format) { - c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB => false, - c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM, c.SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB => true, - else => return error.UnsupportedCaptureFormat, - }; + const bgr = try g.present.bgr(); var tmp_buf: [4096]u8 = undefined; var final_buf: [4096]u8 = undefined; @@ -8669,7 +8446,7 @@ fn prepareAddonGpu(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer 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.swapchain_format, image_vert_spv, image_frag_spv, 1, 1, Cells.image_input, blend) catch continue; + 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.prepared_images.append(gpa, .{ .premultiplied = true, |
