//! pardes's cell renderer on SDL_GPU, part 1: the shaders' instance ABI //! (src/gpu/shaders/*.glsl), the pipelines over it and the resource helpers, //! moved from pardes's gui.zig as they were. `c` is the embedder's own SDL //! translation (pardes's: SDL3 and font.h), so the embedder's SDL is the one //! linked. const std = @import("std"); const c = @import("c"); pub 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, }; pub const OverlayVertex = extern struct { x: f32, y: f32, r: f32, g: f32, b: f32, a: f32, }; pub const ImageInstance = extern struct { x0: f32, y0: f32, x1: f32, y1: f32, u0: f32, v0: f32, u1: f32, v1: f32, }; pub const opaque_bg_bit: u32 = 0x2000_0000; /// 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). pub fn growInstances(device: *c.SDL_GPUDevice, 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(device, &vb_info) orelse return error.GpuCreate; errdefer c.SDL_ReleaseGPUBuffer(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(device, &xf_info) orelse return error.GpuCreate; if (buffer.*) |old| c.SDL_ReleaseGPUBuffer(device, old); if (transfer.*) |old| c.SDL_ReleaseGPUTransferBuffer(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`. pub const VertexInput = struct { pitch: u32, rate: c.SDL_GPUVertexInputRate, attrs: []const c.SDL_GPUVertexAttribute }; /// The cells' and the decor's instances (ui.vert.glsl). pub 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") }, } }; pub 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") }, } }; pub 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; } pub 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); } 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)); }