diff options
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/Post.zig | 653 | ||||
| -rw-r--r-- | src/gui/gui.zig | 191 |
2 files changed, 13 insertions, 831 deletions
diff --git a/src/gui/Post.zig b/src/gui/Post.zig deleted file mode 100644 index 19b10c52..00000000 --- a/src/gui/Post.zig +++ /dev/null @@ -1,653 +0,0 @@ -//! The post chain (docs/render-pipeline.md §6): Shadertoy passes over the -//! finished frame, each reading the one before it (the frame first) and the -//! last one writing the window. A pass is the bundled Crt (shaders/post/, -//! compiled with the build) or a user's file (Shader <path>), whose SPIR-V -//! comes from ShaderBuild.zig, compiled by this process (a window attached -//! to a session elsewhere runs the bundled passes alone). A file whose -//! compile fails keeps its last good pipeline. -//! -//! The uniforms are ghostty's (Uniforms), so a shader written for ghostty runs -//! here. Their clock is the shell's and moves only when the chain draws: -//! with a frame of the core's, or on its own while ShaderAnimation asks for -//! it (redraw level A, §5.5: the chain alone, over the retained frame). -const std = @import("std"); -const libc = std.c; -const ghostty_vt = @import("ghostty-vt"); -const pardes = @import("../pardes.zig"); -const gui = @import("gui.zig"); -const c = gui.c; - -const Post = @This(); -const Scene = pardes.config.Runtime.Scene; -const Chain = pardes.config.Runtime.Post; -const Pipeline = c.SDL_GPUGraphicsPipeline; -pub const max = Chain.max; - -const prefix = @embedFile("post-prefix.glsl"); -const Built = @import("../ShaderBuild.zig").Pass; -const vert_spv = @embedFile("post.vert.spv"); -const bundled_spv = std.EnumArray(Scene, []const u8).init(.{ - .crt = @embedFile("post-crt.frag.spv"), - .bloom = @embedFile("post-bloom.frag.spv"), - .vignette = @embedFile("post-vignette.frag.spv"), - .grain = @embedFile("post-grain.frag.spv"), -}); -const bloom_down_spv = @embedFile("bloom-down.frag.spv"); -const bloom_up_spv = @embedFile("bloom-up.frag.spv"); -/// Bloom's steps below the frame, at levels 1 to 3: a half, a quarter, ... -const bloom_steps = [_]u8{ 3, 4, 5 }; -const bloom_format = c.SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT; -/// Every pass writes its target whole: no blending. -const no_blend = std.mem.zeroes(c.SDL_GPUColorTargetBlendState); - -/// pardes's own block (shaders/post/prefix.glsl PardesPost), std140. -pub const Own = extern struct { - opaque_window: f32 = 1, - level: f32 = 0, - scale: f32 = 1, - cap: f32 = 0, - spare_count: f32 = 0, - _pad: [3]f32 = @splat(0), - spare: [max_spare][4]f32 = @splat(@splat(0)), -}; -pub const max_spare = 32; - -/// ghostty's Shadertoy uniform block, member for member in the GLSL block's -/// order and std140 layout (shaders/post/prefix.glsl). Filled by the GLSL -/// names: ghostty's own Zig mirror calls the second-to-last slot -/// selection_background, so there the two selection colours arrive swapped -/// (docs §6, open point 1); here each is what its GLSL name says. -pub const Uniforms = extern struct { - resolution: [3]f32 align(16) = .{ 0, 0, 1 }, - time: f32 align(4) = 0, - time_delta: f32 align(4) = 0, - frame_rate: f32 align(4) = 60, - frame: i32 align(4) = 0, - channel_time: [4][4]f32 align(16) = @splat(@splat(0)), - channel_resolution: [4][4]f32 align(16) = @splat(@splat(0)), - mouse: [4]f32 align(16) = @splat(0), - date: [4]f32 align(16) = @splat(0), - sample_rate: f32 align(4) = 44100, - current_cursor: [4]f32 align(16) = @splat(0), - previous_cursor: [4]f32 align(16) = @splat(0), - current_cursor_color: [4]f32 align(16) = @splat(0), - previous_cursor_color: [4]f32 align(16) = @splat(0), - current_cursor_style: i32 align(4) = 0, - previous_cursor_style: i32 align(4) = 0, - cursor_visible: i32 align(4) = 0, - cursor_change_time: f32 align(4) = 0, - time_focus: f32 align(4) = 0, - focus: i32 align(4) = 1, - palette: [256][4]f32 align(16) = @splat(@splat(0)), - background_color: [4]f32 align(16) = @splat(0), - foreground_color: [4]f32 align(16) = @splat(0), - cursor_color: [4]f32 align(16) = @splat(0), - cursor_text: [4]f32 align(16) = @splat(0), - selection_foreground_color: [4]f32 align(16) = @splat(0), - selection_background_color: [4]f32 align(16) = @splat(0), -}; - -pub const Pass = struct { - scene: ?Scene = null, - /// A bundled pass's strength (pardesLevel); 0 for a file. - level: u8 = 0, - /// A user's file, owned; empty for a bundled pass. - path: []u8 = &.{}, - pipeline: ?*Pipeline = null, - /// The SPIR-V revision `pipeline` was made from (ShaderBuild.Pass). - revision: u32 = 0, - /// It moves on its own (ShaderBuild.Pass). - animated: bool = false, -}; - -passes: [max]Pass = @splat(.{}), -len: usize = 0, -/// The chain key `passes` follow (sync); null before the first. -key: ?u32 = null, -bundled: std.EnumArray(Scene, ?*Pipeline) = .initFill(null), -ping: [2]?*c.SDL_GPUTexture = .{ null, null }, -ping_w: u32 = 0, -ping_h: u32 = 0, -uniforms: Uniforms = .{}, -/// The display's density, for a pass whose pattern is in logical pixels -/// (pardesScale); the shell sets it. -scale: f32 = 1, -/// What a bundled pass may do on this theme (pardesCap), each scene's, and -/// the rectangles every bundled pass leaves alone (pardesSpare): both from -/// the last frame described. -caps: std.EnumArray(Scene, f32) = .initFill(0), -spare: [max_spare][4]f32 = @splat(@splat(0)), -spare_len: usize = 0, -/// Bloom's float chain, a half the frame and down, and its two steps. -bloom_tex: [bloom_steps[bloom_steps.len - 1]]?*c.SDL_GPUTexture = @splat(null), -bloom_w: u32 = 0, -bloom_h: u32 = 0, -bloom_down: ?*Pipeline = null, -bloom_up: ?*Pipeline = null, -started_ns: ?u64 = null, -last_ns: u64 = 0, - -/// Brings the passes to the chain a shell was given (ShaderBuild.zig: a -/// local core's, or an attached session's over the wire), whose `key` moves -/// whenever the chain or a file's SPIR-V does. A file whose SPIR-V has not -/// moved keeps its pipeline; one the GPU refuses keeps its last, and is said -/// to `core`, or logged where there is none. Each loop step, off the frame -/// path. -pub fn sync(post: *Post, gpa: std.mem.Allocator, device: *c.SDL_GPUDevice, format: c.SDL_GPUTextureFormat, chain: []const Built, key: u32, core: ?*pardes.Pardes) void { - if (post.key == key) return; - post.key = key; - var next: [max]Pass = @splat(.{}); - for (chain, 0..) |entry, i| { - if (entry.scene) |scene| { - if (post.bundled.get(scene) == null) post.bundled.set(scene, gui.makePipeline(device, format, vert_spv, bundled_spv.get(scene), if (scene == .bloom) 2 else 1, 4, null, no_blend) catch null); - if (scene == .bloom and post.bloom_down == null) { - post.bloom_down = gui.makePipeline(device, bloom_format, vert_spv, bloom_down_spv, 1, 1, null, no_blend) catch null; - post.bloom_up = gui.makePipeline(device, bloom_format, vert_spv, bloom_up_spv, 1, 1, null, no_blend) catch null; - } - next[i] = .{ .scene = scene, .level = entry.level, .pipeline = post.bundled.get(scene), .animated = entry.animated }; - continue; - } - // A file already in the chain keeps its pipeline and its path. - for (post.passes[0..post.len]) |*pass| { - if (pass.scene != null or !std.mem.eql(u8, pass.path, entry.path)) continue; - next[i] = pass.*; - pass.* = .{}; - break; - } else next[i] = .{ .path = gpa.dupe(u8, entry.path) catch &.{} }; - const pass = &next[i]; - pass.animated = entry.animated; - if (pass.revision == entry.revision or entry.spirv.len == 0) continue; - pass.revision = entry.revision; - const made = gui.makePipeline(device, format, vert_spv, entry.spirv, 1, 4, null, no_blend) catch { - var buf: [512]u8 = undefined; - const text = std.fmt.bufPrint(&buf, "Shader {s}: the GPU refused it", .{entry.path}) catch "Shader: the GPU refused it"; - if (core) |p| p.setMessage(p.active, text) else std.log.err("{s}", .{text}); - continue; - }; - if (pass.pipeline) |old| c.SDL_ReleaseGPUGraphicsPipeline(device, old); - pass.pipeline = made; - } - for (post.passes[0..post.len]) |pass| { - if (pass.scene == null) if (pass.pipeline) |pipeline| c.SDL_ReleaseGPUGraphicsPipeline(device, pipeline); - gpa.free(pass.path); - } - post.passes = next; - post.len = chain.len; - // An emptied chain's clock starts again with the next pass put in it. - if (post.len == 0) { - post.started_ns = null; - post.last_ns = 0; - post.uniforms.frame = 0; - } -} - -/// A pass is ready to draw. -pub fn ready(post: *const Post) bool { - for (post.passes[0..post.len]) |pass| if (pass.pipeline != null) return true; - return false; -} - -/// The chain redraws on its own (level A) as ShaderAnimation says. -/// Only a pass that moves on its own (ShaderBuild.Pass.animated) asks for -/// frames while idle: a still one (Bloom, Vignette, Grain, a file that reads -/// no clock) is drawn again only with a frame of the core's. -pub fn animating(post: *const Post, mode: pardes.config.Runtime.ShaderAnimation) bool { - const moving = for (post.passes[0..post.len]) |pass| { - if (pass.pipeline != null and pass.animated) break true; - } else false; - return moving and switch (mode) { - .off => false, - .on => post.uniforms.focus != 0, - .always => true, - }; -} - -/// What a frame of the core's tells the passes: where its cursor is drawn -/// and in what, and the theme's colours. -pub const Frame = struct { - pub const Cursor = struct { x0: f32, y0: f32, x1: f32, y1: f32, rgb: [3]u8, text: [3]u8, bar: bool }; - cursor: ?Cursor, - /// What a bundled pass leaves alone, in framebuffer pixels: the tags, - /// grips and notices (and the cursor, added here). - spare: []const [4]f32 = &.{}, - background: [3]u8, - foreground: [3]u8, - selection_foreground: [3]u8, - selection_background: [3]u8, -}; - -fn rgba(rgb: [3]u8) [4]f32 { - return .{ @as(f32, @floatFromInt(rgb[0])) / 255, @as(f32, @floatFromInt(rgb[1])) / 255, @as(f32, @floatFromInt(rgb[2])) / 255, 1 }; -} - -/// Takes in a frame of the core's. The cursor's rectangle is (left, BOTTOM -/// edge, width, height) in framebuffer pixels, y down; a move or a new -/// colour makes the old one the previous and stamps the change. -pub fn describe(post: *Post, frame: Frame) void { - const u = &post.uniforms; - post.caps = capsOf(frame); - post.spare_len = 0; - for (frame.spare) |rect| post.addSpare(rect); - if (frame.cursor) |cursor| post.addSpare(.{ cursor.x0, cursor.y0, cursor.x1, cursor.y1 }); - u.background_color = rgba(frame.background); - u.foreground_color = rgba(frame.foreground); - u.selection_foreground_color = rgba(frame.selection_foreground); - u.selection_background_color = rgba(frame.selection_background); - if (u.palette[1][3] == 0) for (&u.palette, 0..) |*slot, i| { - const color = ghostty_vt.color.default[i]; - slot.* = rgba(.{ color.r, color.g, color.b }); - }; - u.cursor_visible = @intFromBool(frame.cursor != null); - const cursor = frame.cursor orelse return; - const rect: [4]f32 = .{ cursor.x0, cursor.y1, cursor.x1 - cursor.x0, cursor.y1 - cursor.y0 }; - const style: i32 = if (cursor.bar) 2 else 0; - const color = rgba(cursor.rgb); - u.cursor_color = color; - u.cursor_text = rgba(cursor.text); - if (std.meta.eql(rect, u.current_cursor) and std.meta.eql(color, u.current_cursor_color) and style == u.current_cursor_style) return; - u.previous_cursor = u.current_cursor; - u.previous_cursor_color = u.current_cursor_color; - u.previous_cursor_style = u.current_cursor_style; - u.current_cursor = rect; - u.current_cursor_color = color; - u.current_cursor_style = style; - u.cursor_change_time = u.time; -} - -/// A rectangle a bundled pass leaves alone; past `max_spare` the last one -/// grows to cover the rest (sparing more, never less). -fn addSpare(post: *Post, rect: [4]f32) void { - if (rect[2] <= rect[0] or rect[3] <= rect[1]) return; - if (post.spare_len < max_spare) { - post.spare[post.spare_len] = rect; - post.spare_len += 1; - return; - } - const last = &post.spare[max_spare - 1]; - last.* = .{ @min(last[0], rect[0]), @min(last[1], rect[1]), @max(last[2], rect[2]), @max(last[3], rect[3]) }; -} - -/// sRGB's relative luminance (WCAG) and a pair's contrast. -fn luminance(rgb: [3]f32) f32 { - var sum: f32 = 0; - for (rgb, [3]f32{ 0.2126, 0.7152, 0.0722 }) |v, weight| - sum += weight * (if (v <= 0.04045) v / 12.92 else std.math.pow(f32, (v + 0.055) / 1.055, 2.4)); - return sum; -} -fn contrastOf(a: f32, b: f32) f32 { - return (@max(a, b) + 0.05) / (@min(a, b) + 0.05); -} -fn unit(rgb: [3]u8) [3]f32 { - return .{ @as(f32, @floatFromInt(rgb[0])) / 255, @as(f32, @floatFromInt(rgb[1])) / 255, @as(f32, @floatFromInt(rgb[2])) / 255 }; -} - -/// Each bundled pass's ceiling on this theme (pardesCap, docs/effects.md): -/// the most it may do while the text on the page, and the selection's text -/// on its own ground, keep min(their contrast, 4.5). Bloom adds light to -/// both of a pair (linear, as a share of full white); a vignette darkens -/// both (a share); grain moves the page alone, either way (sRGB units). -pub fn capsOf(frame: Frame) std.EnumArray(Scene, f32) { - const pairs = [_][2][3]f32{ .{ unit(frame.foreground), unit(frame.background) }, .{ unit(frame.selection_foreground), unit(frame.selection_background) } }; - var bloom: f32 = 1; - for (pairs) |pair| { - const a = luminance(pair[0]); - const b = luminance(pair[1]); - const light = @max(a, b); - const dark = @min(a, b); - const target = @min(contrastOf(a, b), 4.5); - // (light + e + .05) / (dark + e + .05) >= target - bloom = @min(bloom, @max(0, (light + 0.05 - target * (dark + 0.05)) / @max(target - 1, 1e-3))); - } - // A vignette darkens both of a pair as a lift's shadow does. - const shade = @min(gui.shadowCeiling(frame.foreground, frame.background), gui.shadowCeiling(frame.selection_foreground, frame.selection_background)); - // Grain: the largest step off the page, up and down, that keeps the text - // on it at its contrast or 4.5 (searched in 1/1020 steps up to 8/255). - const ink = luminance(unit(frame.foreground)); - const page = unit(frame.background); - const target = @min(contrastOf(ink, luminance(page)), 4.5); - var grain: f32 = 0; - var step: f32 = 0.25 / 255.0; - while (step <= 8.0 / 255.0) : (step += 0.25 / 255.0) { - var up = page; - var down = page; - for (&up, &down) |*u, *d| { - u.* = @min(1, u.* + step); - d.* = @max(0, d.* - step); - } - if (contrastOf(ink, luminance(up)) < target - 1e-4 or contrastOf(ink, luminance(down)) < target - 1e-4) break; - grain = step; - } - var caps: std.EnumArray(Scene, f32) = .initFill(1); - caps.set(.bloom, bloom); - caps.set(.vignette, shade); - caps.set(.grain, grain); - return caps; -} - -test "no bundled pass at its ceiling takes a native theme's text or selection below its contrast or 4.5" { - for (pardes.themes[0..pardes.colors.native_count]) |*theme| { - const frame: Frame = .{ .cursor = null, .background = theme.bg orelse .{ 18, 18, 18 }, .foreground = theme.fg orelse .{ 204, 204, 204 }, .selection_foreground = theme.sel_fg, .selection_background = theme.sel_bg }; - const caps = capsOf(frame); - for ([_][2][3]u8{ .{ frame.foreground, frame.background }, .{ frame.selection_foreground, frame.selection_background } }, 0..) |pair, i| { - const a = luminance(unit(pair[0])); - const b = luminance(unit(pair[1])); - const target = @min(contrastOf(a, b), 4.5) - 1e-3; - // Bloom at its ceiling, over both. - const e = @min(caps.get(.bloom), 0.05); - try std.testing.expect(contrastOf(a + e, b + e) >= target); - // The vignette at its ceiling, over both. - const k = 1 - @min(caps.get(.vignette), 0.28); - try std.testing.expect(contrastOf(a * k, b * k) >= target); - // Grain moves the page alone. - if (i == 0) { - var up = unit(pair[1]); - for (&up) |*v| v.* = @min(1, v.* + caps.get(.grain)); - try std.testing.expect(contrastOf(a, luminance(up)) >= target); - } - } - } -} - -/// The window gained or lost the focus. -pub fn focus(post: *Post, focused: bool) void { - if (focused and post.uniforms.focus == 0) post.uniforms.time_focus = post.uniforms.time; - post.uniforms.focus = @intFromBool(focused); -} - -/// The pointer, in framebuffer pixels: Shadertoy's iMouse, xy where it is, -/// zw where the last press was, negative once released. -pub fn mouse(post: *Post, x: f32, y: f32, press: ?bool) void { - const m = &post.uniforms.mouse; - m[0] = x; - m[1] = y; - if (press) |down| { - if (down) { - m[2] = x; - m[3] = y; - } else { - m[2] = -@abs(m[2]); - m[3] = -@abs(m[3]); - } - } -} - -/// Runs the passes over `source`, the finished frame, into `target`: each -/// reads the one before, the ones between write two ping-pong textures that -/// start cleared, and iTime and iFrame move by this one draw. -pub fn draw(post: *Post, device: *c.SDL_GPUDevice, cmd: *c.SDL_GPUCommandBuffer, sampler: *c.SDL_GPUSampler, format: c.SDL_GPUTextureFormat, source: *c.SDL_GPUTexture, target: *c.SDL_GPUTexture, w: u32, h: u32, now_ns: u64, opaque_window: bool) !void { - var live: usize = 0; - for (post.passes[0..post.len]) |pass| live += @intFromBool(pass.pipeline != null); - if (live == 0) return; - if (live > 1 and (post.ping[0] == null or post.ping_w != w or post.ping_h != h)) { - for (&post.ping) |*texture| { - if (texture.*) |old| c.SDL_ReleaseGPUTexture(device, old); - texture.* = gui.texture2d(device, format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | c.SDL_GPU_TEXTUREUSAGE_SAMPLER, w, h) orelse return error.GpuCreate; - } - post.ping_w = w; - post.ping_h = h; - } - const u = &post.uniforms; - const started = post.started_ns orelse now_ns; - post.started_ns = started; - const elapsed = @as(f64, @floatFromInt(now_ns -| started)) / std.time.ns_per_s; - const delta: f32 = if (post.last_ns == 0) 0 else @floatCast(@as(f64, @floatFromInt(now_ns -| post.last_ns)) / std.time.ns_per_s); - post.last_ns = now_ns; - u.time = @floatCast(elapsed); - u.time_delta = delta; - u.frame_rate = if (delta > 0) 1 / delta else 60; - u.resolution = .{ @floatFromInt(w), @floatFromInt(h), 1 }; - u.channel_resolution[0] = .{ @floatFromInt(w), @floatFromInt(h), 1, 0 }; - // UTC: the year, the month from 0, the day, the second of the day. - var wall: libc.timespec = undefined; - if (libc.clock_gettime(.REALTIME, &wall) != 0) wall.sec = 0; - const epoch: std.time.epoch.EpochSeconds = .{ .secs = @intCast(@max(0, wall.sec)) }; - const day = epoch.getEpochDay().calculateYearDay(); - const month = day.calculateMonthDay(); - u.date = .{ @floatFromInt(day.year), @floatFromInt(month.month.numeric() - 1), @floatFromInt(month.day_index + 1), @floatFromInt(epoch.getDaySeconds().secs) }; - - var input = source; - var index: usize = 0; - for (post.passes[0..post.len]) |pass| { - const pipeline = pass.pipeline orelse continue; - index += 1; - const last = index == live; - const output = if (last) target else post.ping[index % 2].?; - var info = std.mem.zeroes(c.SDL_GPUColorTargetInfo); - info.texture = output; - info.load_op = if (last) c.SDL_GPU_LOADOP_DONT_CARE else c.SDL_GPU_LOADOP_CLEAR; - info.store_op = c.SDL_GPU_STOREOP_STORE; - // Bloom's glow first, from this pass's input, at half size and down. - // A chain that could not be made adds no glow, and the pass is the - // frame as it was. - const glow = if (pass.scene == .bloom) post.bloomChain(device, cmd, sampler, input, w, h, pass.level) catch null else null; - const pass_rp = c.SDL_BeginGPURenderPass(cmd, &info, 1, null); - c.SDL_BindGPUGraphicsPipeline(pass_rp, pipeline); - const bindings = [2]c.SDL_GPUTextureSamplerBinding{ .{ .texture = input, .sampler = sampler }, .{ .texture = glow orelse input, .sampler = sampler } }; - c.SDL_BindGPUFragmentSamplers(pass_rp, 0, &bindings, if (pass.scene == .bloom) 2 else 1); - pushUniforms(cmd, u); - var own: Own = .{ - .opaque_window = @floatFromInt(@intFromBool(opaque_window)), - .level = @floatFromInt(pass.level), - .scale = post.scale, - // A user's file has no ceiling of ours, and no spared rectangles. - .cap = if (pass.scene) |scene| (if (scene == .bloom and glow == null) 0 else post.caps.get(scene)) else 1, - .spare_count = if (pass.scene != null) @floatFromInt(post.spare_len) else 0, - }; - if (pass.scene != null) @memcpy(own.spare[0..post.spare_len], post.spare[0..post.spare_len]); - c.SDL_PushGPUFragmentUniformData(cmd, 3, &own, @sizeOf(Own)); - c.SDL_DrawGPUPrimitives(pass_rp, 3, 1, 0, 0); - c.SDL_EndGPURenderPass(pass_rp); - input = output; - } - u.frame +%= 1; -} - -/// ghostty's block as the prefix cuts it (SDL GPU binds at most 4 KiB of a -/// block): the globals up to the palette, the palette, the colours after it, -/// each a slice of the one struct, at set 3 bindings 0 to 2. -fn pushUniforms(cmd: *c.SDL_GPUCommandBuffer, u: *const Uniforms) void { - const bytes = std.mem.asBytes(u); - const palette = @offsetOf(Uniforms, "palette"); - const colors = @offsetOf(Uniforms, "background_color"); - c.SDL_PushGPUFragmentUniformData(cmd, 0, bytes.ptr, @intCast(palette)); - c.SDL_PushGPUFragmentUniformData(cmd, 1, bytes.ptr + palette, @intCast(colors - palette)); - c.SDL_PushGPUFragmentUniformData(cmd, 2, bytes.ptr + colors, @intCast(@sizeOf(Uniforms) - colors)); -} - -test "each slice of the uniforms fits the 4 KiB SDL GPU binds of a block" { - try std.testing.expect(@offsetOf(Uniforms, "palette") <= 4096); - try std.testing.expect(@offsetOf(Uniforms, "background_color") - @offsetOf(Uniforms, "palette") <= 4096); - try std.testing.expect(@sizeOf(Uniforms) - @offsetOf(Uniforms, "background_color") <= 4096); - try std.testing.expect(@sizeOf(Own) <= 4096); -} - -/// Bloom's glow of `input` (w x h): a bright pass and dual Kawase steps -/// down to 1/2^n of the frame and back up to a half, in float textures; -/// the half-size result, for the composite (shaders/post/bloom.glsl). -fn bloomChain(post: *Post, device: *c.SDL_GPUDevice, cmd: *c.SDL_GPUCommandBuffer, sampler: *c.SDL_GPUSampler, input: *c.SDL_GPUTexture, w: u32, h: u32, level: u8) !*c.SDL_GPUTexture { - const down = post.bloom_down orelse return error.GpuCreate; - const up = post.bloom_up orelse return error.GpuCreate; - const steps = bloom_steps[std.math.clamp(level, 1, 3) - 1]; - if (post.bloom_tex[0] == null or post.bloom_w != w or post.bloom_h != h) { - for (&post.bloom_tex, 0..) |*texture, i| { - if (texture.*) |old| c.SDL_ReleaseGPUTexture(device, old); - texture.* = gui.texture2d(device, bloom_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | c.SDL_GPU_TEXTUREUSAGE_SAMPLER, @max(1, w >> @intCast(i + 1)), @max(1, h >> @intCast(i + 1))) orelse return error.GpuCreate; - } - post.bloom_w = w; - post.bloom_h = h; - } - const Step = extern struct { texels: [4]f32, pass: [4]f32 }; - const size = struct { - fn of(full: u32, i: usize) f32 { - return @floatFromInt(if (i == 0) full else @max(1, full >> @intCast(i))); - } - }; - // Down: the frame (0) to 1, 1 to 2, ...; the first through the bright - // pass (a channel's linear peak over 0.6 to 0.75, by level). - const threshold: f32 = switch (std.math.clamp(level, 1, 3)) { - 1 => 0.75, - 2 => 0.7, - else => 0.6, - }; - // Only what is brighter than the page glows: on a light page, where the - // ground itself would pass, nothing does. - const page_peak = peak: { - var peak: f32 = 0; - for (post.uniforms.background_color[0..3]) |v| peak = @max(peak, if (v <= 0.04045) v / 12.92 else std.math.pow(f32, (v + 0.055) / 1.055, 2.4)); - break :peak peak; - }; - for (0..steps) |i| { - const from = if (i == 0) input else post.bloom_tex[i - 1].?; - const to = post.bloom_tex[i].?; - const params: Step = .{ - .texels = .{ 1 / size.of(w, i), 1 / size.of(h, i), 1 / size.of(w, i + 1), 1 / size.of(h, i + 1) }, - .pass = .{ if (i == 0) @max(threshold, page_peak + 0.15) else 0, 0.25, 0, 0 }, - }; - bloomStep(cmd, down, sampler, from, to, ¶ms, @sizeOf(Step)); - } - // Up: each level into the one above it, back to the half. - var i: usize = steps - 1; - while (i > 0) : (i -= 1) { - const params: Step = .{ - .texels = .{ 1 / size.of(w, i + 1), 1 / size.of(h, i + 1), 1 / size.of(w, i), 1 / size.of(h, i) }, - .pass = @splat(0), - }; - bloomStep(cmd, up, sampler, post.bloom_tex[i].?, post.bloom_tex[i - 1].?, ¶ms, @sizeOf(Step)); - } - return post.bloom_tex[0].?; -} - -fn bloomStep(cmd: *c.SDL_GPUCommandBuffer, pipeline: *Pipeline, sampler: *c.SDL_GPUSampler, from: *c.SDL_GPUTexture, to: *c.SDL_GPUTexture, params: *const anyopaque, len: u32) void { - var info = std.mem.zeroes(c.SDL_GPUColorTargetInfo); - info.texture = to; - info.load_op = c.SDL_GPU_LOADOP_DONT_CARE; - info.store_op = c.SDL_GPU_STOREOP_STORE; - const rp = c.SDL_BeginGPURenderPass(cmd, &info, 1, null); - c.SDL_BindGPUGraphicsPipeline(rp, pipeline); - const binding = c.SDL_GPUTextureSamplerBinding{ .texture = from, .sampler = sampler }; - c.SDL_BindGPUFragmentSamplers(rp, 0, &binding, 1); - c.SDL_PushGPUFragmentUniformData(cmd, 0, params, len); - c.SDL_DrawGPUPrimitives(rp, 3, 1, 0, 0); - c.SDL_EndGPURenderPass(rp); -} - -pub fn deinit(post: *Post, gpa: std.mem.Allocator, device: *c.SDL_GPUDevice) void { - for (post.bloom_tex) |texture| if (texture) |t| c.SDL_ReleaseGPUTexture(device, t); - for ([_]?*Pipeline{ post.bloom_down, post.bloom_up }) |pipeline| if (pipeline) |p| c.SDL_ReleaseGPUGraphicsPipeline(device, p); - for (post.passes[0..post.len]) |pass| { - if (pass.scene == null) if (pass.pipeline) |pipeline| c.SDL_ReleaseGPUGraphicsPipeline(device, pipeline); - gpa.free(pass.path); - } - for (post.bundled.values) |pipeline| if (pipeline) |p| c.SDL_ReleaseGPUGraphicsPipeline(device, p); - for (post.ping) |texture| if (texture) |t| c.SDL_ReleaseGPUTexture(device, t); -} - -/// ghostty 1.4.0's Uniforms (src/renderer/shadertoy.zig), copied: the offsets -/// ours must match. Its last two names are swapped against its own GLSL. -const GhosttyUniforms = extern struct { - resolution: [3]f32 align(16), - time: f32 align(4), - time_delta: f32 align(4), - frame_rate: f32 align(4), - frame: i32 align(4), - channel_time: [4][4]f32 align(16), - channel_resolution: [4][4]f32 align(16), - mouse: [4]f32 align(16), - date: [4]f32 align(16), - sample_rate: f32 align(4), - current_cursor: [4]f32 align(16), - previous_cursor: [4]f32 align(16), - current_cursor_color: [4]f32 align(16), - previous_cursor_color: [4]f32 align(16), - current_cursor_style: i32 align(4), - previous_cursor_style: i32 align(4), - cursor_visible: i32 align(4), - cursor_change_time: f32 align(4), - time_focus: f32 align(4), - focus: i32 align(4), - palette: [256][4]f32 align(16), - background_color: [4]f32 align(16), - foreground_color: [4]f32 align(16), - cursor_color: [4]f32 align(16), - cursor_text: [4]f32 align(16), - selection_background_color: [4]f32 align(16), - selection_foreground_color: [4]f32 align(16), -}; - -test "the uniform block is ghostty's, offset for offset, and the prefix names it in that order" { - const ours = @typeInfo(Uniforms).@"struct"; - const theirs = @typeInfo(GhosttyUniforms).@"struct"; - try std.testing.expectEqual(theirs.field_names.len, ours.field_names.len); - try std.testing.expectEqual(@sizeOf(GhosttyUniforms), @sizeOf(Uniforms)); - inline for (ours.field_names, ours.field_types, theirs.field_names, theirs.field_types) |a, a_type, b, b_type| { - try std.testing.expectEqual(@offsetOf(GhosttyUniforms, b), @offsetOf(Uniforms, a)); - try std.testing.expectEqual(b_type, a_type); - } - // By NAME: iSelectionForegroundColor sits where ghostty's Zig says - // selection_background, which is where its GLSL puts the foreground. - try std.testing.expectEqual(@offsetOf(GhosttyUniforms, "selection_background_color"), @offsetOf(Uniforms, "selection_foreground_color")); - // The prefix's block, member by member, in ghostty's GLSL names and types. - const glsl = [_]struct { []const u8, []const u8 }{ - .{ "vec3", "iResolution" }, .{ "float", "iTime" }, .{ "float", "iTimeDelta" }, - .{ "float", "iFrameRate" }, .{ "int", "iFrame" }, .{ "float", "iChannelTime[4]" }, - .{ "vec3", "iChannelResolution[4]" }, .{ "vec4", "iMouse" }, .{ "vec4", "iDate" }, - .{ "float", "iSampleRate" }, .{ "vec4", "iCurrentCursor" }, .{ "vec4", "iPreviousCursor" }, - .{ "vec4", "iCurrentCursorColor" }, .{ "vec4", "iPreviousCursorColor" }, .{ "int", "iCurrentCursorStyle" }, - .{ "int", "iPreviousCursorStyle" }, .{ "int", "iCursorVisible" }, .{ "float", "iTimeCursorChange" }, - .{ "float", "iTimeFocus" }, .{ "int", "iFocus" }, .{ "vec3", "iPalette[256]" }, - .{ "vec3", "iBackgroundColor" }, .{ "vec3", "iForegroundColor" }, .{ "vec3", "iCursorColor" }, - .{ "vec3", "iCursorText" }, .{ "vec3", "iSelectionForegroundColor" }, .{ "vec3", "iSelectionBackgroundColor" }, - }; - try std.testing.expectEqual(ours.field_names.len, glsl.len); - // Its three blocks (Globals, Palette, Colors) read as one, in order. - const start = std.mem.indexOf(u8, prefix, "uniform Globals {").?; - const end = std.mem.indexOf(u8, prefix, "uniform PardesPost {").?; - var lines = std.mem.tokenizeScalar(u8, prefix[start..end], '\n'); - var n: usize = 0; - while (lines.next()) |line| { - if (!std.mem.startsWith(u8, line, " uniform ")) continue; - var words = std.mem.tokenizeAny(u8, line, " ;"); - try std.testing.expectEqualStrings("uniform", words.next().?); - try std.testing.expectEqualStrings(glsl[n][0], words.next().?); - try std.testing.expectEqualStrings(glsl[n][1], words.next().?); - n += 1; - } - try std.testing.expectEqual(glsl.len, n); - // Each block starts where its first member is in the one struct. - try std.testing.expect(std.mem.indexOf(u8, prefix, "uniform Palette {\n uniform vec3 iPalette[256];\n};") != null); -} - -test "a moved cursor becomes the previous one, stamped with the time of the move" { - var post: Post = .{}; - const frame: Frame = .{ .cursor = .{ .x0 = 10, .y0 = 20, .x1 = 11, .y1 = 40, .rgb = .{ 255, 0, 0 }, .text = .{ 0, 0, 0 }, .bar = true }, .background = .{ 0, 0, 0 }, .foreground = .{ 255, 255, 255 }, .selection_foreground = .{ 1, 2, 3 }, .selection_background = .{ 4, 5, 6 } }; - post.uniforms.time = 1.5; - post.describe(frame); - // Left, BOTTOM edge, width, height, in framebuffer pixels, y down. - try std.testing.expectEqual([4]f32{ 10, 40, 1, 20 }, post.uniforms.current_cursor); - try std.testing.expectEqual(@as(i32, 2), post.uniforms.current_cursor_style); - try std.testing.expectEqual(@as(f32, 1.5), post.uniforms.cursor_change_time); - post.uniforms.time = 2; - post.describe(frame); - try std.testing.expectEqual(@as(f32, 1.5), post.uniforms.cursor_change_time); - var moved = frame; - moved.cursor.?.x0 = 20; - moved.cursor.?.x1 = 21; - post.describe(moved); - try std.testing.expectEqual([4]f32{ 10, 40, 1, 20 }, post.uniforms.previous_cursor); - try std.testing.expectEqual(@as(f32, 2), post.uniforms.cursor_change_time); - try std.testing.expectEqual([4]f32{ 1.0 / 255.0, 2.0 / 255.0, 3.0 / 255.0, 1 }, post.uniforms.selection_foreground_color); -} - -test "a still pass asks for no idle frames; one that reads the clock does" { - var post: Post = .{}; - const fake: *Pipeline = @ptrFromInt(0x1000); - post.passes[0] = .{ .scene = .bloom, .level = 2, .pipeline = fake }; - post.len = 1; - try std.testing.expect(!post.animating(.on) and !post.animating(.always)); - post.passes[1] = .{ .scene = .crt, .level = 2, .pipeline = fake, .animated = true }; - post.len = 2; - try std.testing.expect(post.animating(.on) and post.animating(.always) and !post.animating(.off)); - // Unfocused, `on` rests. - post.uniforms.focus = 0; - try std.testing.expect(!post.animating(.on) and post.animating(.always)); -} diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 28f5e695..2c2f53c4 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -12,9 +12,7 @@ const config = @import("../config.zig"); const look = @import("../look.zig"); const message = pardes.Messages.Message; const file_watch = @import("../file_watch.zig"); -const ShaderBuild = @import("../ShaderBuild.zig"); const deck = @import("deck.zig"); -const Post = @import("Post.zig"); const pet = @import("pet.zig"); const fonts = @import("../fonts.zig"); // the fallback faces const selection_pipe = @import("../selection_pipe.zig"); @@ -806,7 +804,7 @@ const Pty = struct { const Queue = host_io.Inbox; /// Wakes a blocking SDL_WaitEventTimeout from another thread: a push to -/// the queue, a link's message, a post chain's compile done (ShaderBuild). +/// the queue, a link's message. fn sdlWake(_: ?*anyopaque) void { var sev = std.mem.zeroes(c.SDL_Event); sev.type = c.SDL_EVENT_USER; @@ -1181,13 +1179,6 @@ const Gui = struct { blink_due: bool = false, no_blink: bool = false, cursor_quad_alpha: f32 = 0, - scene_tex: ?*c.SDL_GPUTexture = null, - scene_tex_w: u32 = 0, - scene_tex_h: u32 = 0, - scene_failures: u8 = 0, - scene_target_failed: bool = false, - /// The post chain, run over the frame in scene_tex. - post: Post = .{}, pointer_present: bool = false, arrow_cursor: ?*c.SDL_Cursor = null, link_cursor: ?*c.SDL_Cursor = null, @@ -1237,8 +1228,8 @@ const Gui = struct { }; /// The Gui's fields that are each window's own, one a view (views.zig): -/// the window and its presenting, its scroll and glide, its scene and post -/// chain, its pointer, its blink, its images as presented, its capture. The +/// the window and its presenting, its scroll and glide, its pointer, its +/// blink, its images as presented, its capture. The /// device, the atlas, the font and the pipelines are every window's. The /// Gui holds the live window's; the others wait in `Shell.windows`. const window_fields = [_][:0]const u8{ @@ -1247,8 +1238,7 @@ const window_fields = [_][:0]const u8{ "scroll_body_h", "scroll_held_px", "scroll_col", "scroll_row", "scroll_delta", "scroll_lag", "scroll_edge", "scroll_edge_len", "scroll_notch", "program_wheel", "glide", "pace_ns", "sel_glow", "hover_glow", "frame_arrival_ns", - "blink_due", "cursor_quad_alpha", "scene_tex", "scene_tex_w", "scene_tex_h", - "scene_failures", "scene_target_failed", "post", "pointer_present", "pointer_shape", + "blink_due", "cursor_quad_alpha", "pointer_present", "pointer_shape", "pointer_cell", "presented_images", "prepared_images", "touch", "live_ctrl", "live_alt", "applied_window_opacity", "capture_tex", "capture_tex_w", "capture_tex_h", "capture_xfer", "capture_xfer_size", "captured", "capture_dir", @@ -1288,10 +1278,8 @@ fn freshWindow(g: *Gui) void { /// 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.scene_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); if (g.capture_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); if (g.capture_xfer) |transfer| c.SDL_ReleaseGPUTransferBuffer(g.device, transfer); - g.post.deinit(gpa, g.device); g.glide.deinit(gpa); gpa.free(g.scroll_edge); g.presented_images.deinit(gpa); @@ -1903,7 +1891,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.scene_tex) |texture| c.SDL_ReleaseGPUTexture(device, texture); 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); @@ -1920,7 +1907,6 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di g.prepared_images.deinit(gpa); g.cover.deinit(gpa); g.decor.deinit(gpa); - g.post.deinit(gpa, device); } defer c.SDL_ReleaseGPUGraphicsPipeline(device, image_pipeline); defer if (g.font_bytes.len != 0) gpa.free(g.font_bytes); // set by Font, if it ran @@ -2082,7 +2068,6 @@ fn localSession( .test_hz = if (std.c.getenv("PARDES_TEST_HZ")) |hz| @max(1, std.fmt.parseInt(u64, std.mem.span(hz), 10) catch 60) else 60, }; defer shell.shutdownPtys(); - defer shell.shaders.deinit(gpa, inotify_fd, &watches); // A virtual clock never blinks: goldens and feel series hold still. g.no_blink = shell.test_clock != null; frame_probe = &core.frames; @@ -2388,7 +2373,6 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ .test_mode = test_mode, .remote = &client, }; - defer shell.shaders.deinit(gpa, -1, &watches); g.no_blink = test_mode; defer g.hit_core = null; _ = c.SDL_StartTextInput(g.window); @@ -2421,10 +2405,6 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ while (!link.quit) { core.frames.period_ns = refreshNs(g); syncLigatures(g, core); - { - var passes: [Post.max]ShaderBuild.Pass = undefined; - g.post.sync(gpa, g.device, g.swapchain_format, shell.shaders.view(&core.settings.post, &passes), shell.shaders.revision, core); - } var own: OwnView = .{ .g = g, .in = &in, .s = &shell, .pointer = drew }; r.within(&own, OwnView.run); client.feed(); @@ -2859,16 +2839,7 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { }, else => {}, } - // What the post chain sees of the pointer and the focus. switch (sev.type) { - c.SDL_EVENT_MOUSE_MOTION => { - const at = windowPointToPixels(windowGeometry(g.window), sev.motion.x, sev.motion.y); - g.post.mouse(at.x, at.y, null); - }, - c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_BUTTON_UP => { - const at = windowPointToPixels(windowGeometry(g.window), sev.button.x, sev.button.y); - g.post.mouse(at.x, at.y, sev.button.down); - }, c.SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED => { updateDisplayScale(g.window); // Underlines are drawn into the glyphs a logical pixel thick. @@ -2877,8 +2848,6 @@ fn dispatch(g: *Gui, in: *Input, sev: *const c.SDL_Event) void { r.link.dirty = true; } else in.core.needs_frame = true; }, - c.SDL_EVENT_WINDOW_FOCUS_GAINED => g.post.focus(true), - c.SDL_EVENT_WINDOW_FOCUS_LOST => g.post.focus(false), else => {}, } switch (sev.type) { @@ -3216,10 +3185,9 @@ test "tagline pointer mapping retains physical grips and carries logical text co try std.testing.expectEqual(pardes.TagKind.workspace, topbar.tag_hit.?.kind); } -test "a click at the window's corner lands in the corner cell with a post chain on" { +test "a click at the window's corner lands in the corner cell" { const core = try pardes.Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); defer core.deinit(); - try std.testing.expect(core.settings.post.set(.crt, "", true)); var g = testGui(10, 20, 6, 12); const geometry: WindowGeometry = .{ .window_w = 400, .window_h = 240, .pixel_w = 800, .pixel_h = 480 }; const last = mouseCellWithGeometry(&g, core, 399.9, 239.9, geometry); @@ -3230,9 +3198,6 @@ test "a click at the window's corner lands in the corner cell with a post chain try std.testing.expectEqual(@as(u16, 0), first.row); } -/// Where a window point lands: the cell drawn under it. Input is identity: -/// no post pass moves a pixel (the bundled Crt has no barrel), so a click -/// is exactly where it looks. /// A wheel turned over grid cell `mc` (SDL's sign: up positive): the /// part of dispatch after the pointer is placed, so a test can turn it. fn wheelAt(g: *Gui, in: *Input, w: WheelTurn, mc: MouseCell) void { @@ -3448,8 +3413,6 @@ const Shell = struct { pipe_tasks: *PipeTasks, inotify_fd: c_int, watches: *file_watch.Table, - /// The post chain's files as SPIR-V. - shaders: ShaderBuild = .{}, threads_ok: bool = false, test_mode: bool = false, feed: StdinFeed = .{}, @@ -3901,7 +3864,6 @@ const Shell = struct { .window => if (s.gui != null) s.openAnotherWindow(null) else s.openTheWindow(), .adopted => |a| s.adoptions.adoptShell(s.core, a.pid, a.fd), }; - if (check_files) s.shaders.recheck(); if (check_files and file_watch.reloadChanged(s.core, s.io, s.gpa, s.watches)) s.queue.push(.files_changed); } @@ -3931,20 +3893,14 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { // shell's queue raises for a terminal's output, a completion, a file // system request. What the shell moves or times itself polls every // frame: a smooth scroll, a gamepad's sticks, the test stdin feed, - // a post chain that moves on its own (ShaderAnimation), a present - // to retry. A minimized window sleeps through the core's animation + // a present to retry. A minimized window sleeps through the core's animation // too: nothing shows it, and it is caught up at once when the window // is back. - const polls = g.scroll_lag != 0 or g.scroll_delta != 0 or g.scroll_notch != 0 or g.glide.moving() or g.gamepad != null or s.test_mode or core.needs_frame or - g.post.animating(core.settings.shader_animation); + const polls = g.scroll_lag != 0 or g.scroll_delta != 0 or g.scroll_notch != 0 or g.glide.moving() or g.gamepad != null or s.test_mode or core.needs_frame; const minimized = !s.test_mode and c.SDL_GetWindowFlags(g.window) & (c.SDL_WINDOW_MINIMIZED | c.SDL_WINDOW_OCCLUDED) != 0; // A latency trace is fed on stdin, whose poll below paces the loop: // waiting here too would make every tick cost two frames. - // A chain that moves on its own wakes once a display refresh. - const poll_ms: c_int = if (g.post.animating(core.settings.shader_animation)) - @intCast(@max(1, refreshNs(g) / std.time.ns_per_ms)) - else - 16; + const poll_ms: c_int = 16; const waited: c_int = if (latency_fd >= 0) 0 else if (minimized) -1 else if (timeout_ms != 0) @min(@as(c_int, @intCast(@min(timeout_ms, std.math.maxInt(c_int)))), if (polls) poll_ms else std.math.maxInt(c_int)) else if (polls) poll_ms else -1; // The cursor's blink is the shell's to time (Chrome.cursor_idle_ms): // a frame a loop through an edge, else a wake at the next one; none @@ -4161,17 +4117,7 @@ fn pollFrame(ctx: ?*anyopaque) void { const geom = windowCells(g); if (updateCoreResize(core, geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height)) resetScroll(g); stepScroll(g, core, s.gpa); - _ = s.shaders.sync(s.gpa, s.io, core, s.inotify_fd, s.watches, sdlWake); - { - var passes: [Post.max]ShaderBuild.Pass = undefined; - g.post.sync(s.gpa, g.device, g.swapchain_format, s.shaders.view(&core.settings.post, &passes), s.shaders.revision, core); - } if (s.test_mode) writeTestStatus(g, core, s.presented); - // Between the core's frames, a chain that moves on its own redraws - // alone, once a refresh of the display the window is on (a millisecond - // early is on time: the wait that paces it is in whole milliseconds). - if (g.post.animating(core.settings.shader_animation) and !core.needs_frame and s.presented and - c.SDL_GetTicksNS() -| g.post.last_ns + std.time.ns_per_ms >= refreshNs(g)) redrawPost(g, s.gpa); // A blink edge: the last frame again, the cursor's alpha moved on. if (g.blink_due and !core.needs_frame and s.presented) if (s.surface) |frame| { g.blink_due = false; @@ -4350,8 +4296,6 @@ fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { const frame = surface; s.surface = frame; g.frame_arrival_ns = monoNs(); - const scene_requested = g.post.ready(); - if (!scene_requested) g.scene_failures = 0; s.presented = renderFrame(g, s.gpa, core, frame) catch |err| blk: { log.err("render: {t}", .{err}); break :blk false; @@ -4360,16 +4304,6 @@ fn present(ctx: ?*anyopaque, surface: *const pardes.Surface) void { // the next event. A few retries only: a minimized window never gets one. s.skipped_presents = if (s.presented) 0 else s.skipped_presents +| 1; if (!s.presented and s.skipped_presents <= 3) core.present_skipped = true; - if (g.scene_target_failed) { - g.scene_failures +|= 1; - log.err("scene target unavailable (attempt {d}/3)", .{g.scene_failures}); - if (g.scene_failures >= 3) { - // ponytail: a client's window keeps asking (its core is the - // session's to change). - if (s.remote == null) core.disableSceneEffects(); - g.scene_failures = 0; - } - } else if (scene_requested and s.presented) g.scene_failures = 0; } fn postPresent(ctx: ?*anyopaque) void { @@ -5852,7 +5786,6 @@ fn renderFrame( }; if (cursor) |value| _ = c.SDL_SetCursor(value); } - g.scene_target_failed = false; g.words.memo = .{}; // positional, and this frame's cells are new const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return false; var command_consumed = false; @@ -5870,15 +5803,6 @@ fn renderFrame( const win_w: f32 = @floatFromInt(sw); const win_h: f32 = @floatFromInt(sh); - // The post chain runs from the scene texture, over a local frame or an - // attached session's alike. - var scene_on = g.post.ready(); - if (scene_on) ensureSceneTexture(g, sw, sh) catch { - g.scene_target_failed = true; - scene_on = false; - }; - const scene: *c.SDL_GPUTexture = if (scene_on) g.scene_tex.? else target; - // The frame's CPU side: groups, decor, cover and every instance, up to // their upload. const build_zone = tracy.zone(@src(), "gui frame build"); @@ -5962,7 +5886,7 @@ fn renderFrame( const page = chrome.page orelse bg_default; const background_opacity = backgroundOpacity(g.applied_window_opacity); var color_target = std.mem.zeroes(c.SDL_GPUColorTargetInfo); - color_target.texture = scene; + color_target.texture = target; color_target.clear_color = .{ .r = @as(f32, @floatFromInt(page[0])) / 255.0 * background_opacity, .g = @as(f32, @floatFromInt(page[1])) / 255.0 * background_opacity, @@ -6276,25 +6200,6 @@ fn renderFrame( drawOverlayGpu(g, cmd, rp, overlay_count); c.SDL_EndGPURenderPass(rp); - if (scene_on) { - var boxes: [max_cursors]CursorBox = undefined; - const cursors = if (cursors_shown) cursorBoxes(g, surface, layout, win_h, &boxes) else 0; - var spare: [Post.max_spare][4]f32 = undefined; - g.post.describe(.{ - .cursor = if (cursors > 0) boxes[0] else null, - .spare = spare[0..postSpare(surface, layout, &spare)], - .background = chrome.page orelse bg_default, - .foreground = chrome.fg orelse fg_default, - .selection_foreground = core.theme().sel_fg, - .selection_background = core.theme().sel_bg, - }); - const chain_zone = tracy.zone(@src(), "post chain"); - defer chain_zone.end(); - g.post.scale = display_scale; - g.post.draw(g.device, cmd, g.linear_sampler, g.swapchain_format, g.scene_tex.?, target, sw, sh, c.SDL_GetTicksNS(), background_opacity >= 1) catch { - g.scene_target_failed = true; - }; - } command_consumed = true; const shown = try finishFrame(g, gpa, cmd, frame_target, if (groups.tracks != 0) groups.items[1].track.?.frame else null); if (shown and groups.tracks == 0) rememberPresentedImages(g, gpa, surface); @@ -6340,26 +6245,6 @@ fn finishFrame(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer, ta return c.SDL_SubmitGPUCommandBuffer(cmd); } -/// Redraw level A (docs §5.5): the post chain alone, over the frame the core -/// last drew, for a pass that moves on its own. No core render, no cell. -fn redrawPost(g: *Gui, gpa: std.mem.Allocator) void { - const scene = g.scene_tex orelse return; - const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return; - const target = (acquireTarget(g, cmd, g.scene_tex_w, g.scene_tex_h) catch null) orelse { - _ = c.SDL_SubmitGPUCommandBuffer(cmd); - return; - }; - // A window the retained frame no longer fits waits for the core's. - if (target.w != g.scene_tex_w or target.h != g.scene_tex_h) { - _ = c.SDL_SubmitGPUCommandBuffer(cmd); - return; - } - const zone = tracy.zone(@src(), "post chain redraw"); - g.post.draw(g.device, cmd, g.linear_sampler, g.swapchain_format, scene, target.texture, target.w, target.h, c.SDL_GetTicksNS(), backgroundOpacity(g.applied_window_opacity) >= 1) catch {}; - zone.end(); - _ = finishFrame(g, gpa, cmd, target, null) catch {}; -} - const ResolvedCell = struct { slot: Slot, fg: [3]u8, @@ -8105,15 +7990,6 @@ pub fn texture2d(device: *c.SDL_GPUDevice, format: c.SDL_GPUTextureFormat, usage return c.SDL_CreateGPUTexture(device, &info); } -fn ensureSceneTexture(g: *Gui, width: u32, height: u32) !void { - if (g.scene_tex != null and g.scene_tex_w == width and g.scene_tex_h == height) return; - const next = texture2d(g.device, g.swapchain_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | c.SDL_GPU_TEXTUREUSAGE_SAMPLER, width, height) orelse return error.GpuCreate; - if (g.scene_tex) |texture| c.SDL_ReleaseGPUTexture(g.device, texture); - g.scene_tex = next; - g.scene_tex_w = width; - g.scene_tex_h = height; -} - 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 = texture2d(g.device, g.swapchain_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, width, height) orelse return error.GpuCreate; @@ -9925,47 +9801,7 @@ test "a body tells its first line, and lapis's dots under it move a quarter as f try std.testing.expectEqual(@as(f32, 0), decor.parallaxAt(900, 10)); } -/// What a bundled post pass leaves alone (Post.Frame.spare), in window -/// pixels: every tag with its grip, the column and workspace tags, and the -/// notices, the chrome the focus indicators live in. A grip, the gap after -/// it and the tag beside it are one rectangle; past the list's end the last -/// grows to cover the rest. -fn postSpare(surface: *const pardes.Surface, layout: CellLayout, out: *[Post.max_spare][4]f32) usize { - var n: usize = 0; - for (surface.regionList()) |region| { - switch (region.kind) { - .grip, .tag, .notice, .column_grip, .column_tag, .workspace_tag => {}, - else => continue, - } - if (region.rect.w == 0 or region.rect.h == 0) continue; - const rect: [4]f32 = .{ - @as(f32, @floatFromInt(region.rect.x)) * layout.w, - @as(f32, @floatFromInt(region.rect.y)) * layout.h, - @as(f32, @floatFromInt(region.rect.x + region.rect.w)) * layout.w, - @as(f32, @floatFromInt(region.rect.y + region.rect.h)) * layout.h, - }; - const joined = for (out[0..n]) |*other| { - // The same rows, a gap cell or two apart (a grip, the gap after - // it, its tag): one band. - const gap = 2 * layout.w + 0.5; - if (other[1] == rect[1] and other[3] == rect[3] and rect[0] <= other[2] + gap and other[0] <= rect[2] + gap) { - other.* = .{ @min(other[0], rect[0]), rect[1], @max(other[2], rect[2]), rect[3] }; - break true; - } - } else false; - if (joined) continue; - if (n == out.len) { - const last = &out[n - 1]; - last.* = .{ @min(last[0], rect[0]), @min(last[1], rect[1]), @max(last[2], rect[2]), @max(last[3], rect[3]) }; - continue; - } - out[n] = rect; - n += 1; - } - return n; -} - -const CursorBox = Post.Frame.Cursor; +const CursorBox = struct { x0: f32, y0: f32, x1: f32, y1: f32, rgb: [3]u8, bar: bool }; const max_cursors = 1 + pardes.MAX_PANES + pardes.MAX_TAG_LAYERS; /// Where the frame draws the focused caret, its top in grid rows: from its @@ -9996,7 +9832,6 @@ fn caretRide(g: *const Gui, surface: *const pardes.Surface) f32 { /// is an eighth of its cell wide, a block the whole cell. fn cursorBoxes(g: *const Gui, surface: *const pardes.Surface, layout: CellLayout, win_h: f32, out: *[max_cursors]CursorBox) usize { const chrome = &surface.chrome; - const page = chrome.page orelse bg_default; var n: usize = 0; if (surface.cursor) |cursor| { if (cursor.x < surface.cols and cursor.y < surface.rows and bodyLayerIn(surface.bodyLayers(), cursor.x, cursor.y) == null and tagLayerIn(surface.tagLayers(), cursor.x, cursor.y) == null) { @@ -10006,7 +9841,7 @@ fn cursorBoxes(g: *const Gui, surface: *const pardes.Surface, layout: CellLayout const height = std.math.clamp(if (role == .tagline) @as(f32, @floatFromInt(g.tagline_height)) else layout.h, 1.0, layout.h); const x0 = @as(f32, @floatFromInt(cursor.x)) * layout.w; const y0 = @as(f32, @floatFromInt(cursor.y)) * layout.h + @as(f32, @floatFromInt(taglineBandOffset(cursor.y, win_h, @intFromFloat(layout.h), @intFromFloat(height)))); - out[n] = .{ .x0 = x0, .y0 = y0, .x1 = x0 + if (cursor.bar) @max(dp(1), width / 8.0) else width, .y1 = y0 + height, .rgb = cursorColor(chrome, cell, role), .text = page, .bar = cursor.bar }; + out[n] = .{ .x0 = x0, .y0 = y0, .x1 = x0 + if (cursor.bar) @max(dp(1), width / 8.0) else width, .y1 = y0 + height, .rgb = cursorColor(chrome, cell, role), .bar = cursor.bar }; n += 1; } } @@ -10023,7 +9858,7 @@ fn cursorBoxes(g: *const Gui, surface: *const pardes.Surface, layout: CellLayout const width: f32 = if (role == .tagline) @floatFromInt(g.tagline_width) else layout.w; const x = left + @as(f32, @floatFromInt(cursor.x)) * width; const cell = &layer.cells[@as(usize, cursor.y) * layer.cols + cursor.x]; - out[n] = .{ .x0 = x, .y0 = top, .x1 = x + if (cursor.bar) @max(dp(1), width / 8) else width, .y1 = @min(bottom, top + height), .rgb = cursorColor(chrome, cell, role), .text = page, .bar = cursor.bar }; + out[n] = .{ .x0 = x, .y0 = top, .x1 = x + if (cursor.bar) @max(dp(1), width / 8) else width, .y1 = @min(bottom, top + height), .rgb = cursorColor(chrome, cell, role), .bar = cursor.bar }; n += 1; } for (surface.tagLayers()) |*layer| { @@ -10034,7 +9869,7 @@ fn cursorBoxes(g: *const Gui, surface: *const pardes.Surface, layout: CellLayout const span = layer.caretSpan(cursor.x, layout.w, width, if (cursor.bar) @max(dp(1), width / 8) else width); const y = layer.viewport.y + cursor.y; const top = @as(f32, @floatFromInt(y)) * layout.h + @as(f32, @floatFromInt(taglineBandOffset(y, win_h, g.cell_h, g.tagline_height))); - out[n] = .{ .x0 = span[0], .y0 = top, .x1 = span[1], .y1 = top + @as(f32, @floatFromInt(g.tagline_height)), .rgb = cursorColor(chrome, &layer.cells[@as(usize, cursor.y) * layer.cols + cursor.x], .tagline), .text = page, .bar = cursor.bar }; + out[n] = .{ .x0 = span[0], .y0 = top, .x1 = span[1], .y1 = top + @as(f32, @floatFromInt(g.tagline_height)), .rgb = cursorColor(chrome, &layer.cells[@as(usize, cursor.y) * layer.cols + cursor.x], .tagline), .bar = cursor.bar }; n += 1; } return n; |
