//! 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. const Gpu = @This(); const std = @import("std"); const c = @import("c"); const abi = @import("abi"); const gfx = @import("gfx.zig"); pub const color_format = c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; /// Frames an unused shader or pipeline survives. const keep_frames = 300; gpa: std.mem.Allocator, 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, /// 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, vertices: ?*c.SDL_GPUBuffer = null, vertex_capacity: u32 = 0, upload: ?*c.SDL_GPUTransferBuffer = null, upload_capacity: u32 = 0, frame: u64 = 0, fn Cached(comptime T: type) type { return struct { object: *T, used: u64 }; } const Target = struct { width: u32, height: u32, texture: *c.SDL_GPUTexture, samples: u8 = 1, msaa: ?*c.SDL_GPUTexture = null, stencil_samples: u8 = 0, stencil: ?*c.SDL_GPUTexture = null, }; pub fn init(gpa: std.mem.Allocator, device: *c.SDL_GPUDevice) Gpu { const d24 = c.SDL_GPUTextureSupportsFormat(device, c.SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT, c.SDL_GPU_TEXTURETYPE_2D, c.SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET); return .{ .gpa = gpa, .device = device, .stencil_format = if (d24) c.SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT else c.SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT, }; } pub fn deinit(g: *Gpu, images: *gfx.Images) void { 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 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.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| { 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); map.removeByPtr(entry.key_ptr); // The iterator is invalid after a removal: the rest waits a frame. break; } } } pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { if (pass.width == 0 or pass.height == 0 or pass.draws.len == 0 and pass.clear == null) return; const samples = g.supportedSamples(pass.samples); const t = try g.renderTarget(images, pass.image, pass.width, pass.height, samples, pass.stencil); // 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)); const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; if (total > 0) { try g.reserve(total); const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.upload, true) orelse return error.GpuMap); var offset: usize = 0; for (pass.draws) |d| { @memcpy(mapped[offset..][0..d.vertices.len], d.vertices); offset += std.mem.alignForward(usize, d.vertices.len, 4); } c.SDL_UnmapGPUTransferBuffer(g.device, g.upload); const copy = c.SDL_BeginGPUCopyPass(cmd); c.SDL_UploadToGPUBuffer(copy, &.{ .transfer_buffer = g.upload, .offset = 0 }, &.{ .buffer = g.vertices, .offset = 0, .size = total }, true); c.SDL_EndGPUCopyPass(copy); } const clear = pass.clear orelse .{ 0, 0, 0, 0 }; var color: c.SDL_GPUColorTargetInfo = std.mem.zeroes(c.SDL_GPUColorTargetInfo); color.clear_color = .{ .r = clear[0], .g = clear[1], .b = clear[2], .a = clear[3] }; // A multisampled pass cannot load resolved pixels: it always clears. color.load_op = if (pass.clear != null or samples > 1) c.SDL_GPU_LOADOP_CLEAR else c.SDL_GPU_LOADOP_LOAD; if (samples > 1) { color.texture = t.msaa; color.store_op = c.SDL_GPU_STOREOP_RESOLVE; color.resolve_texture = t.texture; } else { color.texture = t.texture; color.store_op = c.SDL_GPU_STOREOP_STORE; } var depth: c.SDL_GPUDepthStencilTargetInfo = std.mem.zeroes(c.SDL_GPUDepthStencilTargetInfo); if (pass.stencil) { depth.texture = t.stencil; depth.load_op = c.SDL_GPU_LOADOP_DONT_CARE; depth.store_op = c.SDL_GPU_STOREOP_DONT_CARE; depth.stencil_load_op = c.SDL_GPU_LOADOP_CLEAR; depth.stencil_store_op = c.SDL_GPU_STOREOP_DONT_CARE; depth.clear_stencil = 0; } const rp = c.SDL_BeginGPURenderPass(cmd, &color, 1, if (pass.stencil) &depth else null) orelse return error.GpuRenderPass; var bound: ?*c.SDL_GPUGraphicsPipeline = null; var offset: u32 = 0; for (pass.draws) |d| { defer offset += @intCast(std.mem.alignForward(usize, d.vertices.len, 4)); if (d.pipeline >= pass.pipelines.len or d.vertex_count == 0) continue; const built = g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil) catch |err| { std.log.warn("gpu pipeline {d}: {t} ({s})", .{ d.pipeline, err, c.SDL_GetError() }); continue; }; if (bound != built) { c.SDL_BindGPUGraphicsPipeline(rp, built); bound = built; } c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1); 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); const full: abi.Rect = .{ .w = @floatFromInt(pass.width), .h = @floatFromInt(pass.height) }; const s = d.scissor orelse full; c.SDL_SetGPUScissor(rp, &.{ .x = @intFromFloat(@max(s.x, 0)), .y = @intFromFloat(@max(s.y, 0)), .w = @intFromFloat(@max(s.w, 0)), .h = @intFromFloat(@max(s.h, 0)) }); c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, 0, 0); } c.SDL_EndGPURenderPass(rp); if (!c.SDL_SubmitGPUCommandBuffer(cmd)) return error.GpuSubmit; } fn supportedSamples(g: *Gpu, wanted: u8) u8 { var s: u8 = if (wanted >= 8) 8 else if (wanted >= 4) 4 else if (wanted >= 2) 2 else 1; while (s > 1 and !c.SDL_GPUTextureSupportsSampleCount(g.device, color_format, sampleCount(s))) s /= 2; return s; } fn sampleCount(s: u8) c.SDL_GPUSampleCount { return switch (s) { 8 => c.SDL_GPU_SAMPLECOUNT_8, 4 => c.SDL_GPU_SAMPLECOUNT_4, 2 => c.SDL_GPU_SAMPLECOUNT_2, else => c.SDL_GPU_SAMPLECOUNT_1, }; } /// The image's render target, (re)created to fit the pass. fn renderTarget(g: *Gpu, images: *gfx.Images, image: abi.Image, w: u32, h: u32, samples: u8, stencil: bool) !*Target { var entry = try g.targets.getOrPut(g.gpa, image); if (entry.found_existing and (entry.value_ptr.width != w or entry.value_ptr.height != h)) { g.releaseTarget(images, image, entry.value_ptr.*); entry.found_existing = false; } if (!entry.found_existing) { errdefer _ = g.targets.remove(image); const resolved = try g.texture(w, h, color_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | c.SDL_GPU_TEXTUREUSAGE_SAMPLER, 1); errdefer c.SDL_ReleaseGPUTexture(g.device, resolved); try images.wrap(image, resolved, w, h); entry.value_ptr.* = .{ .width = w, .height = h, .texture = resolved }; } const t = entry.value_ptr; if (samples > 1 and (t.msaa == null or t.samples != samples)) { if (t.msaa) |m| c.SDL_ReleaseGPUTexture(g.device, m); t.msaa = try g.texture(w, h, color_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, samples); t.samples = samples; } if (stencil and (t.stencil == null or t.stencil_samples != samples)) { if (t.stencil) |s| c.SDL_ReleaseGPUTexture(g.device, s); t.stencil = try g.texture(w, h, g.stencil_format, c.SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET, samples); t.stencil_samples = samples; } return t; } fn releaseTarget(g: *Gpu, images: *gfx.Images, image: abi.Image, t: Target) void { images.unwrap(image); c.SDL_ReleaseGPUTexture(g.device, t.texture); if (t.msaa) |m| c.SDL_ReleaseGPUTexture(g.device, m); if (t.stencil) |s| c.SDL_ReleaseGPUTexture(g.device, s); } /// Forgets an image's target, when the image itself is freed. pub fn forget(g: *Gpu, images: *gfx.Images, image: abi.Image) void { const t = g.targets.fetchRemove(image) orelse return; g.releaseTarget(images, image, t.value); } fn texture(g: *Gpu, w: u32, h: u32, format: c.SDL_GPUTextureFormat, usage: c.SDL_GPUTextureUsageFlags, samples: u8) !*c.SDL_GPUTexture { var info: c.SDL_GPUTextureCreateInfo = std.mem.zeroes(c.SDL_GPUTextureCreateInfo); info.type = c.SDL_GPU_TEXTURETYPE_2D; info.format = format; info.usage = usage; info.width = w; info.height = h; info.layer_count_or_depth = 1; info.num_levels = 1; info.sample_count = sampleCount(samples); return c.SDL_CreateGPUTexture(g.device, &info) orelse error.GpuTexture; } fn reserve(g: *Gpu, bytes: u32) !void { if (bytes > g.vertex_capacity) { const capacity = @max(bytes, g.vertex_capacity * 2, 64 * 1024); if (g.vertices) |b| c.SDL_ReleaseGPUBuffer(g.device, b); g.vertices = c.SDL_CreateGPUBuffer(g.device, &.{ .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX, .size = capacity, .props = 0 }) orelse return error.GpuBuffer; g.vertex_capacity = capacity; } if (bytes > g.upload_capacity) { const capacity = @max(bytes, g.upload_capacity * 2, 64 * 1024); if (g.upload) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); g.upload = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = capacity, .props = 0 }) orelse return error.GpuBuffer; g.upload_capacity = capacity; } } 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(std.mem.asBytes(&s.uniform_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, .entrypoint = "main", .format = c.SDL_GPU_SHADERFORMAT_SPIRV, .stage = stage, .num_samplers = 0, .num_storage_textures = 0, .num_storage_buffers = 0, .num_uniform_buffers = s.uniform_buffers, .props = 0, }) orelse return error.GpuShader; } entry.value_ptr.used = g.frame; return .{ entry.value_ptr.object, key }; } fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GPUGraphicsPipeline { const vertex, const vertex_key = try g.shader(p.vertex, c.SDL_GPU_SHADERSTAGE_VERTEX); 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))); const key = h.final(); const entry = try g.pipelines.getOrPut(g.gpa, key); if (!entry.found_existing) { errdefer _ = g.pipelines.remove(key); var attributes: [16]c.SDL_GPUVertexAttribute = undefined; const count = @min(p.attributes.len, attributes.len); for (p.attributes[0..count], attributes[0..count]) |a, *out| out.* = .{ .location = a.location, .buffer_slot = 0, .offset = a.offset, .format = switch (a.format) { .float => c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT, .float2 => c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2, .float3 => c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .float4 => c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .ubyte4_norm => c.SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4_NORM, }, }; var blend: c.SDL_GPUColorTargetBlendState = std.mem.zeroes(c.SDL_GPUColorTargetBlendState); blend.enable_blend = p.blend != .none; blend.color_blend_op = c.SDL_GPU_BLENDOP_ADD; blend.alpha_blend_op = c.SDL_GPU_BLENDOP_ADD; blend.src_color_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE; blend.src_alpha_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE; const dst: c.SDL_GPUBlendFactor = if (p.blend == .additive) c.SDL_GPU_BLENDFACTOR_ONE else c.SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; blend.dst_color_blendfactor = dst; blend.dst_alpha_blendfactor = dst; blend.enable_color_write_mask = true; blend.color_write_mask = if (p.write_color) c.SDL_GPU_COLORCOMPONENT_R | c.SDL_GPU_COLORCOMPONENT_G | c.SDL_GPU_COLORCOMPONENT_B | c.SDL_GPU_COLORCOMPONENT_A else 0; const target_description: c.SDL_GPUColorTargetDescription = .{ .format = color_format, .blend_state = blend }; var depth_stencil: c.SDL_GPUDepthStencilState = std.mem.zeroes(c.SDL_GPUDepthStencilState); if (p.stencil) |st| { depth_stencil.enable_stencil_test = true; depth_stencil.compare_mask = 0xff; depth_stencil.write_mask = 0xff; depth_stencil.front_stencil_state = stencilFace(st.front); depth_stencil.back_stencil_state = stencilFace(st.back); } var info: c.SDL_GPUGraphicsPipelineCreateInfo = std.mem.zeroes(c.SDL_GPUGraphicsPipelineCreateInfo); info.vertex_shader = vertex; 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, .vertex_attributes = &attributes, .num_vertex_attributes = @intCast(count), }; info.primitive_type = switch (p.primitive) { .triangle_list => c.SDL_GPU_PRIMITIVETYPE_TRIANGLELIST, .triangle_strip => c.SDL_GPU_PRIMITIVETYPE_TRIANGLESTRIP, .line_list => c.SDL_GPU_PRIMITIVETYPE_LINELIST, .line_strip => c.SDL_GPU_PRIMITIVETYPE_LINESTRIP, }; info.rasterizer_state.fill_mode = c.SDL_GPU_FILLMODE_FILL; info.rasterizer_state.cull_mode = c.SDL_GPU_CULLMODE_NONE; info.rasterizer_state.front_face = c.SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE; info.multisample_state.sample_count = sampleCount(samples); info.depth_stencil_state = depth_stencil; info.target_info.color_target_descriptions = &target_description; info.target_info.num_color_targets = 1; info.target_info.depth_stencil_format = g.stencil_format; info.target_info.has_depth_stencil_target = stencil; entry.value_ptr.object = c.SDL_CreateGPUGraphicsPipeline(g.device, &info) orelse return error.GpuPipeline; } entry.value_ptr.used = g.frame; return entry.value_ptr.object; } 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, .depth_fail_op = c.SDL_GPU_STENCILOP_KEEP, }; }