-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathconservative_raster.c
578 lines (512 loc) · 19.9 KB
/
conservative_raster.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include "example_base.h"
#include <string.h>
/* -------------------------------------------------------------------------- *
* WebGPU Example - Conservative-raster
*
* This example shows how to render with conservative rasterization (native
* extension with limited support).
*
* When enabled, any pixel touched by a triangle primitive is rasterized.
* This is useful for various advanced techniques, most prominently for
* implementing realtime voxelization.
*
* The demonstration here is implemented by rendering a triangle to a
* low-resolution target and then upscaling it with nearest-neighbor filtering.
* The outlines of the triangle are then rendered in the original solution,
* using the same vertex shader as the triangle. Pixels only drawn with
* conservative rasterization enabled are colored red.
*
* Note: Conservative rasterization is not yet supported in Google Dawn.
*
* Ref:
* https://github.com/gpuweb/gpuweb/issues/137
* https://github.com/gfx-rs/wgpu/tree/master/wgpu/examples/conservative-raster
* -------------------------------------------------------------------------- */
// Texture
static texture_t low_res_target_texture = {0};
// Bind group layout
static WGPUBindGroupLayout bind_group_layout_upscale = NULL;
// Bind group
static WGPUBindGroup bind_group_upscale = NULL;
// Pipeline layout
static WGPUPipelineLayout pipeline_layout = NULL;
// Render pipelines
static struct {
WGPURenderPipeline triangle_conservative;
WGPURenderPipeline triangle_regular;
WGPURenderPipeline lines;
WGPURenderPipeline upscale;
} render_pipelines = {0};
// Other variables
static const char* example_title = "Conservative-raster";
static bool prepared = false;
static void create_low_res_target(wgpu_context_t* wgpu_context)
{
WGPUTextureDescriptor texture_desc = {
.label = "Low Resolution Target",
.usage = WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment,
.dimension = WGPUTextureDimension_2D,
.size = (WGPUExtent3D) {
.width = wgpu_context->surface.width / 16,
.height = wgpu_context->surface.height / 16,
.depthOrArrayLayers = 1,
},
.format = wgpu_context->swap_chain.format,
.mipLevelCount = 1,
.sampleCount = 1,
};
low_res_target_texture.texture
= wgpuDeviceCreateTexture(wgpu_context->device, &texture_desc);
ASSERT(low_res_target_texture.texture != NULL);
WGPUTextureViewDescriptor texture_view_dec = {
.format = wgpu_context->swap_chain.format,
.dimension = WGPUTextureViewDimension_2D,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
};
low_res_target_texture.view
= wgpuTextureCreateView(low_res_target_texture.texture, &texture_view_dec);
ASSERT(low_res_target_texture.view != NULL);
WGPUSamplerDescriptor sampler_desc = {
.label = "Nearest Neighbor Texture Sampler",
.addressModeU = WGPUAddressMode_ClampToEdge,
.addressModeV = WGPUAddressMode_ClampToEdge,
.addressModeW = WGPUAddressMode_ClampToEdge,
.minFilter = WGPUFilterMode_Nearest,
.magFilter = WGPUFilterMode_Nearest,
.mipmapFilter = WGPUMipmapFilterMode_Nearest,
.lodMinClamp = 0.0f,
.lodMaxClamp = 1.0f,
.maxAnisotropy = 1,
};
low_res_target_texture.sampler
= wgpuDeviceCreateSampler(wgpu_context->device, &sampler_desc);
ASSERT(low_res_target_texture.sampler != NULL);
WGPUBindGroupEntry bg_entries[2] = {
[0] = (WGPUBindGroupEntry) {
// Binding 0 : Fragment shader texture view
.binding = 0,
.textureView = low_res_target_texture.view,
},
[1] = (WGPUBindGroupEntry) {
// Binding 1: Fragment shader image sampler
.binding = 1,
.sampler = low_res_target_texture.sampler,
},
};
bind_group_upscale = wgpuDeviceCreateBindGroup(
wgpu_context->device, &(WGPUBindGroupDescriptor){
.label = "Upscale bind group",
.layout = bind_group_layout_upscale,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
});
ASSERT(bind_group_upscale != NULL);
}
static void prepare_pipeline_triangle_conservative(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 SPIR-V
.label = "Triangle and lines vertex shader SPIR-V",
.file = "shaders/conservative_raster/triangle_and_lines.vert.spv",
},
.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 SPIR-V
.label = "Triangle and lines red colored fragment shader SPIR-V",
.file = "shaders/conservative_raster/triangle_and_lines_red.frag.spv",
},
.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
render_pipelines.triangle_conservative = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Conservative Rasterization pipeline",
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.multisample = multisample_state,
});
ASSERT(render_pipelines.triangle_conservative != NULL);
// Partial cleanup
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static void prepare_pipeline_triangle_regular(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 SPIR-V
.label = "Triangle and lines vertex shader SPIR-V",
.file = "shaders/conservative_raster/triangle_and_lines.vert.spv",
},
.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 SPIR-V
.label = "Triangle and lines blue colored fragment shader SPIR-V",
.file = "shaders/conservative_raster/triangle_and_lines_blue.frag.spv",
},
.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
render_pipelines.triangle_regular = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Regular Rasterization pipeline",
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.multisample = multisample_state,
});
ASSERT(render_pipelines.triangle_regular != NULL);
// Partial cleanup
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static void prepare_pipeline_lines(wgpu_context_t* wgpu_context)
{
// Primitive state
WGPUPrimitiveState primitive_state = {
.topology = WGPUPrimitiveTopology_LineStrip,
.stripIndexFormat = WGPUIndexFormat_Uint32,
.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 SPIR-V
.label = "Triangle and lines vertex shader SPIR-V",
.file = "shaders/conservative_raster/triangle_and_lines.vert.spv",
},
.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 SPIR-V
.label = "Triangle and lines white colored fragment shader SPIR-V",
.file = "shaders/conservative_raster/triangle_and_lines_white.frag.spv",
},
.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
render_pipelines.lines = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Lines render pipeline",
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.multisample = multisample_state,
});
ASSERT(render_pipelines.lines != NULL);
// Partial cleanup
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static void setup_pipeline_layout(wgpu_context_t* wgpu_context)
{
// Bind group layout
WGPUBindGroupLayoutEntry bgl_entries[2] = {
[0] = (WGPUBindGroupLayoutEntry) {
// Binding 0: Texture view (Fragment shader)
.binding = 0,
.visibility = WGPUShaderStage_Fragment,
.texture = (WGPUTextureBindingLayout) {
.sampleType = WGPUTextureSampleType_Float,
.viewDimension = WGPUTextureViewDimension_2D,
.multisampled = false,
},
.storageTexture = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
// Binding 1: Sampler (Fragment shader)
.binding = 1,
.visibility = WGPUShaderStage_Fragment,
.sampler = (WGPUSamplerBindingLayout){
.type = WGPUSamplerBindingType_Filtering,
},
.texture = {0},
}
};
bind_group_layout_upscale = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device, &(WGPUBindGroupLayoutDescriptor){
.label = "Upscale bind group",
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
});
ASSERT(bind_group_layout_upscale != NULL);
// Create the pipeline layout
pipeline_layout = wgpuDeviceCreatePipelineLayout(
wgpu_context->device, &(WGPUPipelineLayoutDescriptor){
.label = "Render pipeline layout",
.bindGroupLayoutCount = 1,
.bindGroupLayouts = &bind_group_layout_upscale,
});
ASSERT(pipeline_layout != NULL);
}
static void prepare_pipeline_upscale(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 SPIR-V
.label = "Upscale vertex shader SPIR-V",
.file = "shaders/conservative_raster/upscale.vert.spv",
},
.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 SPIR-V
.label = "Upscale fragment shader SPIR-V",
.file = "shaders/conservative_raster/upscale.frag.spv",
},
.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
render_pipelines.upscale = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Upscale rendering pipeline",
.layout = pipeline_layout,
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.multisample = multisample_state,
});
ASSERT(render_pipelines.upscale != 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_pipeline_triangle_conservative(context->wgpu_context);
prepare_pipeline_triangle_regular(context->wgpu_context);
prepare_pipeline_lines(context->wgpu_context);
setup_pipeline_layout(context->wgpu_context);
prepare_pipeline_upscale(context->wgpu_context);
create_low_res_target(context->wgpu_context);
prepared = true;
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
static WGPUCommandBuffer build_command_buffer(wgpu_context_t* wgpu_context)
{
WGPUTextureView view = wgpu_context->swap_chain.frame_buffer;
/* Create command encoder */
wgpu_context->cmd_enc
= wgpuDeviceCreateCommandEncoder(wgpu_context->device, NULL);
{
/* Render pass descriptor */
WGPURenderPassDescriptor render_pass_desc = (WGPURenderPassDescriptor){
.label = "Low resolution render pass",
.colorAttachmentCount = 1,
.colorAttachments = &(WGPURenderPassColorAttachment) {
.view = low_res_target_texture.view,
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = 0.0f,
.g = 0.0f,
.b = 0.0f,
.a = 1.0f,
},
},
.depthStencilAttachment = NULL,
};
/* Render pass */
WGPURenderPassEncoder rpass = wgpuCommandEncoderBeginRenderPass(
wgpu_context->cmd_enc, &render_pass_desc);
wgpuRenderPassEncoderSetPipeline(rpass,
render_pipelines.triangle_conservative);
wgpuRenderPassEncoderDraw(rpass, 3, 1, 0, 0);
wgpuRenderPassEncoderSetPipeline(rpass, render_pipelines.triangle_regular);
wgpuRenderPassEncoderDraw(rpass, 3, 1, 0, 0);
wgpuRenderPassEncoderEnd(rpass);
WGPU_RELEASE_RESOURCE(RenderPassEncoder, rpass)
}
{
/* Render pass descriptor */
WGPURenderPassDescriptor render_pass_desc = (WGPURenderPassDescriptor){
.label = "Full resolution render pass",
.colorAttachmentCount = 1,
.colorAttachments = &(WGPURenderPassColorAttachment) {
.view = view,
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = 0.0f,
.g = 0.0f,
.b = 0.0f,
.a = 1.0f,
},
},
.depthStencilAttachment = NULL,
};
/* Render pass */
WGPURenderPassEncoder rpass = wgpuCommandEncoderBeginRenderPass(
wgpu_context->cmd_enc, &render_pass_desc);
wgpuRenderPassEncoderSetPipeline(rpass, render_pipelines.upscale);
wgpuRenderPassEncoderSetBindGroup(rpass, 0, bind_group_upscale, 0, 0);
wgpuRenderPassEncoderDraw(rpass, 3, 1, 0, 0);
wgpuRenderPassEncoderSetPipeline(rpass, render_pipelines.lines);
wgpuRenderPassEncoderDraw(rpass, 4, 1, 0, 0);
wgpuRenderPassEncoderEnd(rpass);
WGPU_RELEASE_RESOURCE(RenderPassEncoder, rpass)
}
/* 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)
{
/* Prepare frame */
prepare_frame(context);
/* Command buffer to be submitted to the queue */
wgpu_context_t* wgpu_context = context->wgpu_context;
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_destroy_texture(&low_res_target_texture);
WGPU_RELEASE_RESOURCE(BindGroupLayout, bind_group_layout_upscale)
WGPU_RELEASE_RESOURCE(PipelineLayout, pipeline_layout)
WGPU_RELEASE_RESOURCE(BindGroup, bind_group_upscale)
WGPU_RELEASE_RESOURCE(RenderPipeline, render_pipelines.triangle_conservative)
WGPU_RELEASE_RESOURCE(RenderPipeline, render_pipelines.triangle_regular)
WGPU_RELEASE_RESOURCE(RenderPipeline, render_pipelines.lines)
WGPU_RELEASE_RESOURCE(RenderPipeline, render_pipelines.upscale)
}
void example_conservative_raster(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
}