diff options
Diffstat (limited to 'src/host/gpu.zig')
| -rw-r--r-- | src/host/gpu.zig | 311 |
1 files changed, 293 insertions, 18 deletions
diff --git a/src/host/gpu.zig b/src/host/gpu.zig index 34c817f..9c77205 100644 --- a/src/host/gpu.zig +++ b/src/host/gpu.zig @@ -1,7 +1,7 @@ -//! Runs `abi.gpu` passes with SDL_GPU: the translation from GPU work as data -//! to a real API. Shaders and pipelines are built on first use and cached by -//! the hash of their description; entries a sketch stops using (an edited -//! shader, after a reload) are released after a while. +//! Runs `abi.gpu` passes and dispatches with SDL_GPU: the translation from +//! GPU work as data to a real API. Shaders and pipelines are built on first +//! use and cached by the hash of their description; entries a sketch stops +//! using (an edited shader, after a reload) are released after a while. const Gpu = @This(); const std = @import("std"); const c = @import("c"); @@ -17,6 +17,11 @@ device: *c.SDL_GPUDevice, stencil_format: c.SDL_GPUTextureFormat, shaders: std.AutoHashMapUnmanaged(u64, Cached(c.SDL_GPUShader)) = .empty, pipelines: std.AutoHashMapUnmanaged(u64, Cached(c.SDL_GPUGraphicsPipeline)) = .empty, +kernels: std.AutoHashMapUnmanaged(u64, Cached(c.SDL_GPUComputePipeline)) = .empty, +/// Storage buffers by handle: `Buffer` n is `buffers[n - 1]`, null once freed. +buffers: std.ArrayList(?Storage) = .empty, +/// Uploads to storage buffers go through this, in chunks of its size. +staging: ?*c.SDL_GPUTransferBuffer = null, /// Render targets per image: the resolved texture the renderer shows, and /// multisampled and stencil attachments as passes ask for them. targets: std.AutoHashMapUnmanaged(abi.Image, Target) = .empty, @@ -25,11 +30,89 @@ vertex_capacity: u32 = 0, upload: ?*c.SDL_GPUTransferBuffer = null, upload_capacity: u32 = 0, frame: u64 = 0, +/// Set by `--profile-gpu`: every command is timed alone (see `Profile`). +profile: ?Profile = null, +/// Downloads in flight, oldest first, and transfer buffers to reuse. +downloads: std.ArrayList(Download) = .empty, +spare: std.ArrayList(Transfer) = .empty, + +const Transfer = struct { buffer: *c.SDL_GPUTransferBuffer, size: u32 }; +const Download = struct { key: u32, size: u32, transfer: Transfer, fence: *c.SDL_GPUFence }; + +/// GPU time per labelled command. Each command starts on an idle GPU and is +/// waited for, so its time is its own: the total of a frame is what the +/// GPU spends on it, though the waiting makes the frame itself slower. +pub const Profile = struct { + entries: std.StringArrayHashMapUnmanaged(Entry) = .empty, + arena: std.heap.ArenaAllocator, + + const Entry = struct { count: u64 = 0, total: u64 = 0, worst: u64 = 0 }; + + pub fn init(gpa: std.mem.Allocator) Profile { + return .{ .arena = .init(gpa) }; + } + + pub fn deinit(p: *Profile) void { + p.arena.deinit(); + } + + fn add(p: *Profile, label: []const u8, ns: u64) void { + const a = p.arena.allocator(); + const entry = p.entries.getOrPut(a, label) catch return; + if (!entry.found_existing) { + entry.key_ptr.* = a.dupe(u8, label) catch "?"; + entry.value_ptr.* = .{}; + } + entry.value_ptr.count += 1; + entry.value_ptr.total += ns; + entry.value_ptr.worst = @max(entry.value_ptr.worst, ns); + } + + pub fn report(p: *Profile, frames: u64) void { + std.debug.print("gpu time over {d} frames (ms)\n{s: <24} {s: >7} {s: >9} {s: >9} {s: >11}\n", .{ frames, "command", "count", "mean", "worst", "per frame" }); + var sum: u64 = 0; + var it = p.entries.iterator(); + while (it.next()) |e| { + const v = e.value_ptr.*; + sum += v.total; + std.debug.print("{s: <24} {d: >7} {d: >9.3} {d: >9.3} {d: >11.3}\n", .{ e.key_ptr.*, v.count, ms(v.total) / @as(f64, @floatFromInt(v.count)), ms(v.worst), ms(v.total) / @as(f64, @floatFromInt(@max(frames, 1))) }); + } + std.debug.print("{s: <24} {s: >7} {s: >9} {s: >9} {d: >11.3}\n", .{ "all", "", "", "", ms(sum) / @as(f64, @floatFromInt(@max(frames, 1))) }); + } + + fn ms(ns: u64) f64 { + return @as(f64, @floatFromInt(ns)) / std.time.ns_per_ms; + } +}; + +/// Submits `cmd`; when profiling, waits for it and books its time from +/// `start` (taken on an idle GPU, before recording) under `label`. +fn submit(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, label: []const u8, start: u64) !void { + const p = &(g.profile orelse { + if (!c.SDL_SubmitGPUCommandBuffer(cmd)) return error.GpuSubmit; + return; + }); + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + defer c.SDL_ReleaseGPUFence(g.device, fence); + _ = c.SDL_WaitForGPUFences(g.device, true, &fence, 1); + p.add(label, c.SDL_GetTicksNS() - start); +} + +/// When profiling: waits for the GPU to be idle, then the time to start from. +fn startTiming(g: *Gpu) u64 { + if (g.profile != null) _ = c.SDL_WaitForGPUIdle(g.device); + return c.SDL_GetTicksNS(); +} + +/// The most bytes one buffer upload stages at a time. +const staging_size = 4 << 20; fn Cached(comptime T: type) type { return struct { object: *T, used: u64 }; } +const Storage = struct { buffer: *c.SDL_GPUBuffer, size: u32 }; + const Target = struct { width: u32, height: u32, @@ -50,27 +133,49 @@ pub fn init(gpa: std.mem.Allocator, device: *c.SDL_GPUDevice) Gpu { } pub fn deinit(g: *Gpu, images: *gfx.Images) void { + if (g.profile) |*p| { + p.report(g.frame); + p.deinit(); + } var shaders = g.shaders.valueIterator(); while (shaders.next()) |s| c.SDL_ReleaseGPUShader(g.device, s.object); var pipelines = g.pipelines.valueIterator(); while (pipelines.next()) |p| c.SDL_ReleaseGPUGraphicsPipeline(g.device, p.object); + var kernels = g.kernels.valueIterator(); + while (kernels.next()) |k| c.SDL_ReleaseGPUComputePipeline(g.device, k.object); + for (g.buffers.items) |entry| if (entry) |b| c.SDL_ReleaseGPUBuffer(g.device, b.buffer); + for (g.downloads.items) |d| { + _ = c.SDL_WaitForGPUFences(g.device, true, &d.fence, 1); + c.SDL_ReleaseGPUFence(g.device, d.fence); + c.SDL_ReleaseGPUTransferBuffer(g.device, d.transfer.buffer); + } + for (g.spare.items) |t| c.SDL_ReleaseGPUTransferBuffer(g.device, t.buffer); + g.downloads.deinit(g.gpa); + g.spare.deinit(g.gpa); + if (g.staging) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); var targets = g.targets.iterator(); while (targets.next()) |entry| g.releaseTarget(images, entry.key_ptr.*, entry.value_ptr.*); if (g.vertices) |b| c.SDL_ReleaseGPUBuffer(g.device, b); if (g.upload) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); g.shaders.deinit(g.gpa); g.pipelines.deinit(g.gpa); + g.kernels.deinit(g.gpa); + g.buffers.deinit(g.gpa); g.targets.deinit(g.gpa); } /// Releases what nothing has used for a while. pub fn endFrame(g: *Gpu) void { g.frame += 1; - inline for (.{ &g.pipelines, &g.shaders }) |map| { + inline for (.{ &g.pipelines, &g.shaders, &g.kernels }) |map| { var it = map.iterator(); while (it.next()) |entry| { if (g.frame - entry.value_ptr.used < keep_frames) continue; - if (@TypeOf(map) == @TypeOf(&g.pipelines)) c.SDL_ReleaseGPUGraphicsPipeline(g.device, entry.value_ptr.object) else c.SDL_ReleaseGPUShader(g.device, entry.value_ptr.object); + switch (@TypeOf(map)) { + @TypeOf(&g.pipelines) => c.SDL_ReleaseGPUGraphicsPipeline(g.device, entry.value_ptr.object), + @TypeOf(&g.kernels) => c.SDL_ReleaseGPUComputePipeline(g.device, entry.value_ptr.object), + else => c.SDL_ReleaseGPUShader(g.device, entry.value_ptr.object), + } map.removeByPtr(entry.key_ptr); // The iterator is invalid after a removal: the rest waits a frame. break; @@ -83,6 +188,7 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { const samples = g.supportedSamples(pass.samples); const t = try g.renderTarget(images, pass.image, pass.width, pass.height, samples, pass.stencil); + const start = g.startTiming(); // Every draw's vertices go up in one copy. var total: u32 = 0; for (pass.draws) |d| total += @intCast(std.mem.alignForward(usize, d.vertices.len, 4)); @@ -137,7 +243,10 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { c.SDL_BindGPUGraphicsPipeline(rp, built); bound = built; } - c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1); + if (d.vertices.len > 0) c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1); + var bound_storage: [8]*c.SDL_GPUBuffer = undefined; + if (d.vertex_storage.len > 0) c.SDL_BindGPUVertexStorageBuffers(rp, 0, &bound_storage, g.resolve(d.vertex_storage, &bound_storage) catch continue); + if (d.fragment_storage.len > 0) c.SDL_BindGPUFragmentStorageBuffers(rp, 0, &bound_storage, g.resolve(d.fragment_storage, &bound_storage) catch continue); if (d.vertex_uniforms.len > 0) c.SDL_PushGPUVertexUniformData(cmd, 0, d.vertex_uniforms.ptr, @intCast(d.vertex_uniforms.len)); if (d.fragment_uniforms.len > 0) c.SDL_PushGPUFragmentUniformData(cmd, 0, d.fragment_uniforms.ptr, @intCast(d.fragment_uniforms.len)); if (pass.stencil) c.SDL_SetGPUStencilReference(rp, d.stencil_reference); @@ -147,7 +256,171 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, 0, 0); } c.SDL_EndGPURenderPass(rp); + try g.submit(cmd, if (pass.label.len > 0) pass.label else "pass", start); +} + +/// Runs one dispatch in a compute pass of its own. +pub fn compute(g: *Gpu, d: abi.gpu.Dispatch) !void { + if (d.groups[0] == 0 or d.groups[1] == 0 or d.groups[2] == 0) return; + const built = try g.kernel(d.kernel); + const start = g.startTiming(); + var readonly: [8]*c.SDL_GPUBuffer = undefined; + const readonly_count = try g.resolve(d.readonly, &readonly); + var readwrite: [8]*c.SDL_GPUBuffer = undefined; + const readwrite_count = try g.resolve(d.readwrite, &readwrite); + var bindings: [8]c.SDL_GPUStorageBufferReadWriteBinding = undefined; + for (readwrite[0..readwrite_count], bindings[0..readwrite_count]) |b, *binding| binding.* = std.mem.zeroInit(c.SDL_GPUStorageBufferReadWriteBinding, .{ .buffer = b }); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const pass = c.SDL_BeginGPUComputePass(cmd, null, 0, &bindings, readwrite_count) orelse { + _ = c.SDL_CancelGPUCommandBuffer(cmd); + return error.GpuComputePass; + }; + c.SDL_BindGPUComputePipeline(pass, built); + if (readonly_count > 0) c.SDL_BindGPUComputeStorageBuffers(pass, 0, &readonly, readonly_count); + if (d.uniforms.len > 0) c.SDL_PushGPUComputeUniformData(cmd, 0, d.uniforms.ptr, @intCast(d.uniforms.len)); + c.SDL_DispatchGPUCompute(pass, d.groups[0], d.groups[1], d.groups[2]); + c.SDL_EndGPUComputePass(pass); + try g.submit(cmd, if (d.label.len > 0) d.label else "compute", start); +} + +/// A zeroed storage buffer, readable by every stage and writable by compute. +pub fn newBuffer(g: *Gpu, size: u32) !abi.Buffer { + if (size == 0) return error.EmptyBuffer; + const usage = c.SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ | c.SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ | c.SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE; + const buffer = c.SDL_CreateGPUBuffer(g.device, &.{ .usage = usage, .size = size, .props = 0 }) orelse return error.GpuBuffer; + errdefer c.SDL_ReleaseGPUBuffer(g.device, buffer); + try g.buffers.append(g.gpa, .{ .buffer = buffer, .size = size }); + const handle: abi.Buffer = @fromBackingInt(@intCast(g.buffers.items.len)); + // Vulkan leaves new memory undefined; sketches get zeroes, copied from + // one zeroed staging block as many times as it takes. + const staging = try g.stagingBuffer(); + const block: u32 = @min(size, staging_size); + const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, staging, true) orelse return error.GpuMap); + @memset(mapped[0..block], 0); + c.SDL_UnmapGPUTransferBuffer(g.device, staging); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + var offset: u32 = 0; + while (offset < size) : (offset += block) { + c.SDL_UploadToGPUBuffer(copy, &.{ .transfer_buffer = staging, .offset = 0 }, &.{ .buffer = buffer, .offset = offset, .size = @min(block, size - offset) }, false); + } + c.SDL_EndGPUCopyPass(copy); if (!c.SDL_SubmitGPUCommandBuffer(cmd)) return error.GpuSubmit; + return handle; +} + +fn stagingBuffer(g: *Gpu) !*c.SDL_GPUTransferBuffer { + if (g.staging == null) g.staging = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = staging_size, .props = 0 }) orelse return error.GpuBuffer; + return g.staging.?; +} + +pub fn freeBuffer(g: *Gpu, handle: abi.Buffer) void { + const index = @backingInt(handle); + if (index == 0 or index > g.buffers.items.len) return; + const entry = &g.buffers.items[index - 1]; + if (entry.*) |b| c.SDL_ReleaseGPUBuffer(g.device, b.buffer); + entry.* = null; +} + +/// Copies `bytes` into a buffer at `offset`, through the staging buffer. +pub fn write(g: *Gpu, handle: abi.Buffer, offset: u32, bytes: []const u8) !void { + const target = g.storage(handle) orelse return error.UnknownBuffer; + if (@as(u64, offset) + bytes.len > target.size) return error.UploadOutOfBounds; + const staging = try g.stagingBuffer(); + var done: usize = 0; + while (done < bytes.len) { + const chunk = bytes[done..][0..@min(staging_size, bytes.len - done)]; + const start = g.startTiming(); + // Cycling: a chunk still in flight keeps its own memory. + const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, staging, true) orelse return error.GpuMap); + @memcpy(mapped[0..chunk.len], chunk); + c.SDL_UnmapGPUTransferBuffer(g.device, staging); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + c.SDL_UploadToGPUBuffer(copy, &.{ .transfer_buffer = staging, .offset = 0 }, &.{ .buffer = target.buffer, .offset = offset + @as(u32, @intCast(done)), .size = @intCast(chunk.len) }, false); + c.SDL_EndGPUCopyPass(copy); + try g.submit(cmd, "upload", start); + done += chunk.len; + } +} + +/// Starts copying `size` bytes of a buffer from `offset` back to the CPU; +/// `collect` hands them over once the GPU is done, a frame or more later. +pub fn download(g: *Gpu, d: @FieldType(abi.Command, "download")) !void { + const source = g.storage(d.buffer) orelse return error.UnknownBuffer; + if (d.size == 0 or @as(u64, d.offset) + d.size > source.size) return error.DownloadOutOfBounds; + const transfer = for (g.spare.items, 0..) |t, i| { + if (t.size >= d.size) break g.spare.swapRemove(i); + } else Transfer{ + .buffer = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD, .size = d.size, .props = 0 }) orelse return error.GpuBuffer, + .size = d.size, + }; + errdefer g.spare.append(g.gpa, transfer) catch c.SDL_ReleaseGPUTransferBuffer(g.device, transfer.buffer); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + c.SDL_DownloadFromGPUBuffer(copy, &.{ .buffer = source.buffer, .offset = d.offset, .size = d.size }, &.{ .transfer_buffer = transfer.buffer, .offset = 0 }); + c.SDL_EndGPUCopyPass(copy); + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + g.downloads.append(g.gpa, .{ .key = d.key, .size = d.size, .transfer = transfer, .fence = fence }) catch |err| { + _ = c.SDL_WaitForGPUFences(g.device, true, &fence, 1); + c.SDL_ReleaseGPUFence(g.device, fence); + return err; + }; +} + +/// The downloads the GPU has finished, copied into `arena`, in the order +/// they were asked for. +pub fn collect(g: *Gpu, arena: std.mem.Allocator) ![]const abi.Download { + var done: std.ArrayList(abi.Download) = .empty; + while (g.downloads.items.len > 0) { + const d = g.downloads.items[0]; + if (!c.SDL_QueryGPUFence(g.device, d.fence)) break; + _ = g.downloads.orderedRemove(0); + c.SDL_ReleaseGPUFence(g.device, d.fence); + defer g.spare.append(g.gpa, d.transfer) catch c.SDL_ReleaseGPUTransferBuffer(g.device, d.transfer.buffer); + const mapped: [*]const u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, d.transfer.buffer, false) orelse return error.GpuMap); + defer c.SDL_UnmapGPUTransferBuffer(g.device, d.transfer.buffer); + try done.append(arena, .{ .key = d.key, .bytes = try arena.dupe(u8, mapped[0..d.size]) }); + } + return done.items; +} + +fn storage(g: *Gpu, handle: abi.Buffer) ?Storage { + const index = @backingInt(handle); + if (index == 0 or index > g.buffers.items.len) return null; + return g.buffers.items[index - 1]; +} + +/// The SDL buffers behind `handles`, into `out`; returns how many. +fn resolve(g: *Gpu, handles: []const abi.Buffer, out: []*c.SDL_GPUBuffer) !u32 { + if (handles.len > out.len) return error.TooManyBuffers; + for (handles, out[0..handles.len]) |handle, *b| b.* = (g.storage(handle) orelse return error.UnknownBuffer).buffer; + return @intCast(handles.len); +} + +fn kernel(g: *Gpu, k: abi.gpu.Kernel) !*c.SDL_GPUComputePipeline { + var h = std.hash.Wyhash.init(0); + h.update(k.code); + inline for (.{ k.readonly_buffers, k.readwrite_buffers, k.uniform_buffers, k.threads[0], k.threads[1], k.threads[2] }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + const key = h.final(); + const entry = try g.kernels.getOrPut(g.gpa, key); + if (!entry.found_existing) { + errdefer _ = g.kernels.remove(key); + var info: c.SDL_GPUComputePipelineCreateInfo = std.mem.zeroes(c.SDL_GPUComputePipelineCreateInfo); + info.code_size = k.code.len; + info.code = k.code.ptr; + info.entrypoint = "main"; + info.format = c.SDL_GPU_SHADERFORMAT_SPIRV; + info.num_readonly_storage_buffers = k.readonly_buffers; + info.num_readwrite_storage_buffers = k.readwrite_buffers; + info.num_uniform_buffers = k.uniform_buffers; + info.threadcount_x = k.threads[0]; + info.threadcount_y = k.threads[1]; + info.threadcount_z = k.threads[2]; + entry.value_ptr.object = c.SDL_CreateGPUComputePipeline(g.device, &info) orelse return error.GpuComputePipeline; + } + entry.value_ptr.used = g.frame; + return entry.value_ptr.object; } fn supportedSamples(g: *Gpu, wanted: u8) u8 { @@ -236,21 +509,22 @@ fn reserve(g: *Gpu, bytes: u32) !void { fn shader(g: *Gpu, s: abi.gpu.Shader, stage: c.SDL_GPUShaderStage) !struct { *c.SDL_GPUShader, u64 } { var h = std.hash.Wyhash.init(stage); - h.update(s.spirv); + h.update(s.code); h.update(std.mem.asBytes(&s.uniform_buffers)); + h.update(std.mem.asBytes(&s.storage_buffers)); const key = h.final(); const entry = try g.shaders.getOrPut(g.gpa, key); if (!entry.found_existing) { errdefer _ = g.shaders.remove(key); entry.value_ptr.object = c.SDL_CreateGPUShader(g.device, &.{ - .code_size = s.spirv.len, - .code = s.spirv.ptr, + .code_size = s.code.len, + .code = s.code.ptr, .entrypoint = "main", .format = c.SDL_GPU_SHADERFORMAT_SPIRV, .stage = stage, .num_samplers = 0, .num_storage_textures = 0, - .num_storage_buffers = 0, + .num_storage_buffers = s.storage_buffers, .num_uniform_buffers = s.uniform_buffers, .props = 0, }) orelse return error.GpuShader; @@ -264,9 +538,9 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP const fragment, const fragment_key = try g.shader(p.fragment, c.SDL_GPU_SHADERSTAGE_FRAGMENT); // Field by field: no padding bytes in the key. var h = std.hash.Wyhash.init(vertex_key ^ (fragment_key *% 31)); - inline for (.{ p.stride, @intFromEnum(p.primitive), @intFromEnum(p.blend), @intFromBool(p.write_color), samples, @intFromBool(stencil) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); - for (p.attributes) |a| inline for (.{ a.location, @intFromEnum(a.format), a.offset }) |v| h.update(std.mem.asBytes(&@as(u32, v))); - if (p.stencil) |st| inline for (.{ st.front, st.back }) |face| inline for (.{ @intFromEnum(face.compare), @intFromEnum(face.pass), @intFromEnum(face.fail) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + inline for (.{ p.stride, @backingInt(p.primitive), @backingInt(p.blend), @intFromBool(p.write_color), samples, @intFromBool(stencil) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + for (p.attributes) |a| inline for (.{ a.location, @backingInt(a.format), a.offset }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + if (p.stencil) |st| inline for (.{ st.front, st.back }) |face| inline for (.{ @backingInt(face.compare), @backingInt(face.pass), @backingInt(face.fail) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); const key = h.final(); const entry = try g.pipelines.getOrPut(g.gpa, key); if (!entry.found_existing) { @@ -310,7 +584,8 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP info.fragment_shader = fragment; info.vertex_input_state = .{ .vertex_buffer_descriptions = &.{ .slot = 0, .pitch = p.stride, .input_rate = c.SDL_GPU_VERTEXINPUTRATE_VERTEX, .instance_step_rate = 0 }, - .num_vertex_buffers = 1, + // No attributes: the vertex shader makes its own vertices. + .num_vertex_buffers = @intFromBool(count > 0), .vertex_attributes = &attributes, .num_vertex_attributes = @intCast(count), }; @@ -338,9 +613,9 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP fn stencilFace(face: abi.gpu.StencilFace) c.SDL_GPUStencilOpState { // Both enums follow SDL's order, after its INVALID. return .{ - .compare_op = @as(u32, @intFromEnum(face.compare)) + 1, - .pass_op = @as(u32, @intFromEnum(face.pass)) + 1, - .fail_op = @as(u32, @intFromEnum(face.fail)) + 1, + .compare_op = @as(u32, @backingInt(face.compare)) + 1, + .pass_op = @as(u32, @backingInt(face.pass)) + 1, + .fail_op = @as(u32, @backingInt(face.fail)) + 1, .depth_fail_op = c.SDL_GPU_STENCILOP_KEEP, }; } |
