1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
//! 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,
};
}
|