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
|
//! 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));
}
|