-
Notifications
You must be signed in to change notification settings - Fork 24
/
minimal.c
272 lines (224 loc) · 8.15 KB
/
minimal.c
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
#include "example_base.h"
#include <string.h>
/* -------------------------------------------------------------------------- *
* WebGPU Example - Minimal
*
* Minimalistic render pipeline demonstrating how to render a full-screen
* colored quad.
*
* Ref:
* https://github.com/Palats/webgpu/blob/main/demos/demos/minimal.ts
* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
static const char* vertex_shader_wgsl;
static const char* fragment_shader_wgsl;
/* -------------------------------------------------------------------------- *
* Minimal example
* -------------------------------------------------------------------------- */
static WGPURenderPipeline pipeline = NULL;
// Render pass descriptor for frame buffer writes
static struct {
WGPURenderPassColorAttachment color_attachments[1];
WGPURenderPassDescriptor descriptor;
} render_pass = {0};
// Other variables
static const char* example_title = "Minimal";
static bool prepared = false;
static void setup_render_pass(wgpu_context_t* wgpu_context)
{
UNUSED_VAR(wgpu_context);
/* Color attachment */
render_pass.color_attachments[0] = (WGPURenderPassColorAttachment) {
.view = NULL, /* Assigned later */
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = 0.0f,
.g = 0.0f,
.b = 0.0f,
.a = 1.0f,
},
};
/* Render pass descriptor */
render_pass.descriptor = (WGPURenderPassDescriptor){
.label = "Render pass descriptor",
.colorAttachmentCount = 1u,
.colorAttachments = render_pass.color_attachments,
};
}
static void prepare_pipelines(wgpu_context_t* wgpu_context)
{
// Primitive state
WGPUPrimitiveState primitive_state = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_None,
};
// Color target state
WGPUBlendState blend_state = wgpu_create_blend_state(false);
WGPUColorTargetState color_target_state = (WGPUColorTargetState){
.format = wgpu_context->swap_chain.format,
.blend = &blend_state,
.writeMask = WGPUColorWriteMask_All,
};
// Vertex state
WGPUVertexState vertex_state = wgpu_create_vertex_state(
wgpu_context, &(wgpu_vertex_state_t){
.shader_desc = (wgpu_shader_desc_t){
// Vertex shader WGSL
.label = "Vertex shader WGSL",
.wgsl_code.source = vertex_shader_wgsl,
},
.buffer_count = 0,
.buffers = NULL,
});
// Fragment state
WGPUFragmentState fragment_state = wgpu_create_fragment_state(
wgpu_context, &(wgpu_fragment_state_t){
.shader_desc = (wgpu_shader_desc_t){
// Fragment shader WGSL
.label = "Fragment shader WGSL",
.wgsl_code.source = fragment_shader_wgsl,
},
.target_count = 1,
.targets = &color_target_state,
});
// Multisample state
WGPUMultisampleState multisample_state
= wgpu_create_multisample_state_descriptor(
&(create_multisample_state_desc_t){
.sample_count = 1,
});
// Create rendering pipeline using the specified states
pipeline = wgpuDeviceCreateRenderPipeline(wgpu_context->device,
&(WGPURenderPipelineDescriptor){
.label = "Quad - Render pipeline",
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.multisample = multisample_state,
});
ASSERT(pipeline != NULL);
// Partial cleanup
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static int example_initialize(wgpu_example_context_t* context)
{
if (context) {
prepare_pipelines(context->wgpu_context);
setup_render_pass(context->wgpu_context);
prepared = true;
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
static WGPUCommandBuffer build_command_buffer(wgpu_context_t* wgpu_context)
{
// Set target frame buffer
render_pass.color_attachments[0].view = wgpu_context->swap_chain.frame_buffer;
// Create command encoder
wgpu_context->cmd_enc
= wgpuDeviceCreateCommandEncoder(wgpu_context->device, NULL);
// Create render pass encoder for encoding drawing commands
wgpu_context->rpass_enc = wgpuCommandEncoderBeginRenderPass(
wgpu_context->cmd_enc, &render_pass.descriptor);
// Bind the rendering pipeline
wgpuRenderPassEncoderSetPipeline(wgpu_context->rpass_enc, pipeline);
// Set viewport
wgpuRenderPassEncoderSetViewport(
wgpu_context->rpass_enc, 0.0f, 0.0f, (float)wgpu_context->surface.width,
(float)wgpu_context->surface.height, 0.0f, 1.0f);
// Set scissor rectangle
wgpuRenderPassEncoderSetScissorRect(wgpu_context->rpass_enc, 0u, 0u,
wgpu_context->surface.width,
wgpu_context->surface.height);
// Draw quad
wgpuRenderPassEncoderDraw(wgpu_context->rpass_enc, 6, 1, 0, 0);
// End render pass
wgpuRenderPassEncoderEnd(wgpu_context->rpass_enc);
WGPU_RELEASE_RESOURCE(RenderPassEncoder, wgpu_context->rpass_enc)
// Get command buffer
WGPUCommandBuffer command_buffer
= wgpu_get_command_buffer(wgpu_context->cmd_enc);
WGPU_RELEASE_RESOURCE(CommandEncoder, wgpu_context->cmd_enc)
return command_buffer;
}
static int example_draw(wgpu_example_context_t* context)
{
wgpu_context_t* wgpu_context = context->wgpu_context;
// Prepare frame
prepare_frame(context);
// Command buffer to be submitted to the queue
wgpu_context->submit_info.command_buffer_count = 1;
wgpu_context->submit_info.command_buffers[0]
= build_command_buffer(context->wgpu_context);
// Submit command buffer to queue
submit_command_buffers(context);
// Submit frame
submit_frame(context);
return EXIT_SUCCESS;
}
static int example_render(wgpu_example_context_t* context)
{
if (!prepared) {
return EXIT_FAILURE;
}
return example_draw(context);
}
static void example_destroy(wgpu_example_context_t* context)
{
UNUSED_VAR(context);
WGPU_RELEASE_RESOURCE(RenderPipeline, pipeline)
}
void example_minimal(int argc, char* argv[])
{
// clang-format off
example_run(argc, argv, &(refexport_t){
.example_settings = (wgpu_example_settings_t){
.title = example_title,
.vsync = true,
},
.example_initialize_func = &example_initialize,
.example_render_func = &example_render,
.example_destroy_func = &example_destroy,
});
// clang-format on
}
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
// clang-format off
static const char* vertex_shader_wgsl = CODE(
struct VSOut {
@builtin(position) pos: vec4<f32>,
@location(0) coord: vec2<f32>
};
@vertex
fn main(@builtin(vertex_index) idx : u32) -> VSOut {
var data = array<vec2<f32>, 6>(
vec2<f32>(-1.0, -1.0),
vec2<f32>(1.0, -1.0),
vec2<f32>(1.0, 1.0),
vec2<f32>(-1.0, -1.0),
vec2<f32>(-1.0, 1.0),
vec2<f32>(1.0, 1.0),
);
var pos = data[idx];
var out : VSOut;
out.pos = vec4<f32>(pos, 0.0, 1.0);
out.coord.x = (pos.x + 1.0) / 2.0;
out.coord.y = (1.0 - pos.y) / 2.0;
return out;
}
);
static const char* fragment_shader_wgsl = CODE(
@fragment
fn main(@location(0) coord: vec2<f32>) -> @location(0) vec4<f32> {
return vec4<f32>(coord.x, coord.y, 0.5, 1.0);
}
);
// clang-format on