diff options
Diffstat (limited to 'src/gui/gui.zig')
| -rw-r--r-- | src/gui/gui.zig | 199 |
1 files changed, 17 insertions, 182 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index cc43d8b6..d0d46f44 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1,4 +1,5 @@ const SketchGpu = @import("sketch_gpu"); +const Cells = @import("gpu_cells"); // sketchbook's src/gpu/Cells.zig const GpuScene = @import("gpu_scene"); const filesystem = @import("../fs.zig"); const std = @import("std"); @@ -240,52 +241,13 @@ fn inkBounds(bitmap: []const u8, stride: usize, w: usize, h: usize) ?InkBounds { return bounds; } -const CellInstance = extern struct { - x0: f32, - y0: f32, - x1: f32, - y1: f32, - u0: f32, - v0: f32, - u1: f32, - v1: f32, - fr: f32, - fg: f32, - fb: f32, - br: f32, - bg: f32, - bb: f32, - /// The flags (the high bits, ui.vert.glsl). - effect: u32 = 0, - /// A clip rectangle in target pixels; x1 < x0 is none (ui.vert.glsl). - clip_x0: f32 = 0, - clip_y0: f32 = 0, - clip_x1: f32 = -1, - clip_y1: f32 = -1, -}; +const CellInstance = Cells.CellInstance; +const OverlayVertex = Cells.OverlayVertex; +const ImageInstance = Cells.ImageInstance; +const opaque_bg_bit = Cells.opaque_bg_bit; -const OverlayVertex = extern struct { - x: f32, - y: f32, - r: f32, - g: f32, - b: f32, - a: f32, -}; const OverlayColor = struct { r: f32, g: f32, b: f32, a: f32 }; -const ImageInstance = extern struct { - x0: f32, - y0: f32, - x1: f32, - y1: f32, - u0: f32, - v0: f32, - u1: f32, - v1: f32, -}; - -const opaque_bg_bit: u32 = 0x2000_0000; const PreparedImage = struct { premultiplied: bool = false, @@ -1131,7 +1093,7 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di _ = c.SDL_SyncWindow(window); } - const atlas_tex = texture2d(device, c.SDL_GPU_TEXTUREFORMAT_R8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, atlas_w, atlas_h) orelse return error.GpuCreate; + 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; @@ -1149,10 +1111,10 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, headless: bool, di 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 makePipeline(device, swapchain_format, vert_spv, frag_spv, 1, 1, cell_input, std.mem.zeroes(c.SDL_GPUColorTargetBlendState)); - const decor_pipeline = try makePipeline(device, swapchain_format, vert_spv, decor_frag_spv, 0, 1, cell_input, backgroundLayerBlend()); - const overlay_pipeline = try makePipeline(device, swapchain_format, overlay_vert_spv, overlay_frag_spv, 0, 1, overlay_input, backgroundLayerBlend()); - const image_pipeline = try makePipeline(device, swapchain_format, image_vert_spv, image_frag_spv, 1, 1, image_input, backgroundLayerBlend()); + 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 }; @@ -4264,7 +4226,7 @@ fn uploadNativeTexture( return; } - const texture = texture2d(g.device, c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM, c.SDL_GPU_TEXTUREUSAGE_SAMPLER, r.w, r.h) orelse return error.GpuCreate; + 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); @@ -4394,7 +4356,7 @@ fn prepareNativeImages( const capacity = std.math.cast(u32, g.prepared_images.items.len) orelse return false; if (capacity == 0) return true; - growInstances(g, &g.image_vbuf, &g.image_vxfer, &g.image_capacity, capacity, @sizeOf(ImageInstance)) catch return false; + 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)); @@ -4590,7 +4552,7 @@ 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 growInstances(g, &g.vbuf, &g.vxfer, &g.vbuf_cells, capacity, @sizeOf(CellInstance)); + try Cells.growInstances(g.device, &g.vbuf, &g.vxfer, &g.vbuf_cells, capacity, @sizeOf(CellInstance)); const vptr: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.vxfer.?, false) orelse { command_consumed = true; @@ -5542,130 +5504,9 @@ fn uploadAtlas(g: *Gui, cmd: *c.SDL_GPUCommandBuffer) void { g.atlas_dirty_bottom = 0; } -/// A vertex buffer and its upload buffer holding at least `needed` -/// elements of `stride` bytes: those there when they do, else twice as -/// many again until they would (the old ones let go). -fn growInstances(g: *Gui, buffer: *?*c.SDL_GPUBuffer, transfer: *?*c.SDL_GPUTransferBuffer, capacity: *u32, needed: u32, stride: u32) !void { - if (needed <= capacity.* and buffer.* != null and transfer.* != null) return; - var grown = @max(@as(u32, 1), capacity.*); - while (grown < needed) grown = std.math.mul(u32, grown, 2) catch return error.GpuCapacity; - const size = std.math.mul(u32, grown, stride) catch return error.GpuCapacity; - var vb_info = c.SDL_GPUBufferCreateInfo{ .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX, .size = size, .props = 0 }; - const next_buffer = c.SDL_CreateGPUBuffer(g.device, &vb_info) orelse return error.GpuCreate; - errdefer c.SDL_ReleaseGPUBuffer(g.device, next_buffer); - var xf_info = c.SDL_GPUTransferBufferCreateInfo{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = size, .props = 0 }; - const next_transfer = c.SDL_CreateGPUTransferBuffer(g.device, &xf_info) orelse return error.GpuCreate; - if (buffer.*) |old| c.SDL_ReleaseGPUBuffer(g.device, old); - if (transfer.*) |old| c.SDL_ReleaseGPUTransferBuffer(g.device, old); - buffer.* = next_buffer; - transfer.* = next_transfer; - capacity.* = grown; -} - -/// One vertex buffer of a pipeline: `pitch` bytes an element, read per -/// vertex or per instance through `attrs`. -const VertexInput = struct { pitch: u32, rate: c.SDL_GPUVertexInputRate, attrs: []const c.SDL_GPUVertexAttribute }; - -/// The cells' and the decor's instances (ui.vert.glsl). -const cell_input: VertexInput = .{ .pitch = @sizeOf(CellInstance), .rate = c.SDL_GPU_VERTEXINPUTRATE_INSTANCE, .attrs = &.{ - .{ .location = 0, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(CellInstance, "x0") }, - .{ .location = 1, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(CellInstance, "u0") }, - .{ .location = 2, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .offset = @offsetOf(CellInstance, "fr") }, - .{ .location = 3, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .offset = @offsetOf(CellInstance, "br") }, - .{ .location = 4, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_UINT, .offset = @offsetOf(CellInstance, "effect") }, - .{ .location = 5, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(CellInstance, "clip_x0") }, -} }; - -const overlay_input: VertexInput = .{ .pitch = @sizeOf(OverlayVertex), .rate = c.SDL_GPU_VERTEXINPUTRATE_VERTEX, .attrs = &.{ - .{ .location = 0, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2, .offset = @offsetOf(OverlayVertex, "x") }, - .{ .location = 1, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(OverlayVertex, "r") }, -} }; - -const image_input: VertexInput = .{ .pitch = @sizeOf(ImageInstance), .rate = c.SDL_GPU_VERTEXINPUTRATE_INSTANCE, .attrs = &.{ - .{ .location = 0, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(ImageInstance, "x0") }, - .{ .location = 1, .buffer_slot = 0, .format = c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = @offsetOf(ImageInstance, "u0") }, -} }; - -/// A pipeline drawing triangles into `color_format`: `vert`, and `frag` -/// with its samplers and uniform buffers, over one vertex buffer of -/// `input` (none: the post chain's triangle over the whole frame). -pub fn makePipeline( - device: *c.SDL_GPUDevice, - color_format: c.SDL_GPUTextureFormat, - vert: []const u8, - frag: []const u8, - samplers: u32, - uniforms: u32, - input: ?VertexInput, - blend: c.SDL_GPUColorTargetBlendState, -) !*c.SDL_GPUGraphicsPipeline { - const vs = try makeShader(device, vert, c.SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); - defer c.SDL_ReleaseGPUShader(device, vs); - const fs = try makeShader(device, frag, c.SDL_GPU_SHADERSTAGE_FRAGMENT, samplers, uniforms); - defer c.SDL_ReleaseGPUShader(device, fs); - var col_desc = c.SDL_GPUColorTargetDescription{ .format = color_format, .blend_state = blend }; - var vbuf_desc: c.SDL_GPUVertexBufferDescription = undefined; - var info = std.mem.zeroes(c.SDL_GPUGraphicsPipelineCreateInfo); - info.vertex_shader = vs; - info.fragment_shader = fs; - info.primitive_type = c.SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; - if (input) |in| { - vbuf_desc = .{ .slot = 0, .pitch = in.pitch, .input_rate = in.rate, .instance_step_rate = 0 }; - info.vertex_input_state = .{ - .vertex_buffer_descriptions = &vbuf_desc, - .num_vertex_buffers = 1, - .vertex_attributes = in.attrs.ptr, - .num_vertex_attributes = @intCast(in.attrs.len), - }; - } - info.rasterizer_state.fill_mode = c.SDL_GPU_FILLMODE_FILL; - info.rasterizer_state.cull_mode = c.SDL_GPU_CULLMODE_NONE; - info.multisample_state.sample_count = c.SDL_GPU_SAMPLECOUNT_1; - info.target_info = .{ .color_target_descriptions = &col_desc, .num_color_targets = 1 }; - return c.SDL_CreateGPUGraphicsPipeline(device, &info) orelse error.GpuCreate; -} - -fn backgroundLayerBlend() c.SDL_GPUColorTargetBlendState { - var blend = std.mem.zeroes(c.SDL_GPUColorTargetBlendState); - blend.src_color_blendfactor = c.SDL_GPU_BLENDFACTOR_SRC_ALPHA; - blend.dst_color_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; - blend.color_blend_op = c.SDL_GPU_BLENDOP_ADD; - blend.src_alpha_blendfactor = c.SDL_GPU_BLENDFACTOR_CONSTANT_COLOR; - blend.dst_alpha_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; - blend.alpha_blend_op = c.SDL_GPU_BLENDOP_ADD; - blend.enable_blend = true; - return blend; -} - -fn makeShader(device: *c.SDL_GPUDevice, code: []const u8, stage: c.SDL_GPUShaderStage, n_samplers: u32, n_uniform_buffers: u32) !*c.SDL_GPUShader { - var info = std.mem.zeroes(c.SDL_GPUShaderCreateInfo); - info.code = code.ptr; - info.code_size = code.len; - info.entrypoint = "main"; - info.format = c.SDL_GPU_SHADERFORMAT_SPIRV; - info.stage = stage; - info.num_samplers = n_samplers; - info.num_uniform_buffers = n_uniform_buffers; - return c.SDL_CreateGPUShader(device, &info) orelse error.GpuCreate; -} - -/// A 2D texture of one level, one layer and one sample. -pub fn texture2d(device: *c.SDL_GPUDevice, format: c.SDL_GPUTextureFormat, usage: c.SDL_GPUTextureUsageFlags, width: u32, height: u32) ?*c.SDL_GPUTexture { - var info = std.mem.zeroes(c.SDL_GPUTextureCreateInfo); - info.type = c.SDL_GPU_TEXTURETYPE_2D; - info.format = format; - info.usage = usage; - info.width = width; - info.height = height; - info.layer_count_or_depth = 1; - info.num_levels = 1; - info.sample_count = c.SDL_GPU_SAMPLECOUNT_1; - return c.SDL_CreateGPUTexture(device, &info); -} - 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; + 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; @@ -7116,12 +6957,6 @@ test "installed Nerd Symbols fallback covers Yazi directory icons" { try std.testing.expect(c.ui_font_glyph(face, 0xe5ff) != 0); } -test "the shader instance ABI: the flags after the colours, the clip after them" { - try std.testing.expectEqual(@as(usize, 76), @sizeOf(CellInstance)); - try std.testing.expectEqual(@as(usize, 56), @offsetOf(CellInstance, "effect")); - try std.testing.expectEqual(@as(usize, 60), @offsetOf(CellInstance, "clip_x0")); - try std.testing.expectEqual(@as(usize, 32), @sizeOf(ImageInstance)); -} test "GUI PTY reader delivers a real shell tail before EOF and joins on close" { const gpa = std.testing.allocator; @@ -8581,7 +8416,7 @@ test "the atlas's dirty rows hold every row a new glyph draws into, and a reset } test "overlapping background layers keep one uniform opacity" { - const blend = backgroundLayerBlend(); + const blend = Cells.backgroundLayerBlend(); try std.testing.expectEqual(@as(c.SDL_GPUBlendFactor, c.SDL_GPU_BLENDFACTOR_CONSTANT_COLOR), blend.src_alpha_blendfactor); try std.testing.expectEqual(@as(c.SDL_GPUBlendFactor, c.SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA), blend.dst_alpha_blendfactor); for ([_]f32{ 0, 0.25, 0.7, 1 }) |opacity| { @@ -9203,9 +9038,9 @@ fn prepareAddonGpu(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer } const out = gpu.output(target) orelse continue; if (g.addon_pipeline == null) { - var blend = backgroundLayerBlend(); + var blend = Cells.backgroundLayerBlend(); blend.src_color_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE; - g.addon_pipeline = makePipeline(g.device, g.swapchain_format, image_vert_spv, image_frag_spv, 1, 1, image_input, blend) catch continue; + 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.prepared_images.append(gpa, .{ .premultiplied = true, |
