-
Notifications
You must be signed in to change notification settings - Fork 24
/
deferred_rendering.c
1729 lines (1581 loc) · 60.5 KB
/
deferred_rendering.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "example_base.h"
#include "meshes.h"
#include <string.h>
#include "../webgpu/imgui_overlay.h"
/* -------------------------------------------------------------------------- *
* WebGPU Example - Deferred Rendering
*
* This example shows how to do deferred rendering with webgpu. Render geometry
* info to multiple targets in the gBuffers in the first pass. In this sample we
* have 2 gBuffers for normals and albedo, along with a depth texture. And then
* do the lighting in a second pass with per fragment data read from gBuffers so
* it's independent of scene complexity. World-space positions are reconstructed
* from the depth texture and camera matrix. We also update light position in a
* compute shader, where further operations like tile/cluster culling could
* happen. The debug view shows the depth buffer on the left (flipped and scaled
* a bit to make it more visible), the normal G buffer in the middle, and the
* albedo G-buffer on the right side of the screen.
*
* Ref:
* https://github.com/austinEng/webgpu-samples/tree/main/src/sample/deferredRendering
* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
static const char* fragment_deferred_rendering_wgsl;
static const char* fragment_gbuffers_debug_view_wgsl;
static const char* fragment_write_gbuffers_wgsl;
static const char* light_update_wgsl;
static const char* vertex_texture_quad_wgsl;
static const char* vertex_write_gbuffers_wgsl;
/* -------------------------------------------------------------------------- *
* Deferred Rendering example
* -------------------------------------------------------------------------- */
// Constants
#define MAX_NUM_LIGHTS 1024u
static const uint32_t max_num_lights = (uint32_t)MAX_NUM_LIGHTS;
static const uint8_t light_data_stride = 8;
static vec3 light_extent_min = {-50.f, -30.f, -50.f};
static vec3 light_extent_max = {50.f, 30.f, 50.f};
/* Scene matrices */
static struct {
vec4 eye_position;
vec3 up_vector;
vec3 origin;
mat4 projection_matrix;
mat4 view_proj_matrix;
} view_matrices = {
.eye_position = {0.0f, 50.0f, -100.0f, 0.0f},
.up_vector = {0.0f, 1.0f, 0.0f},
.origin = GLM_VEC3_ZERO_INIT,
};
static stanford_dragon_mesh_t stanford_dragon_mesh = {0};
// Vertex and index buffers
static WGPUBuffer vertex_buffer = NULL;
static WGPUBuffer index_buffer = NULL;
static uint32_t index_count = 0;
// GBuffer
static struct {
WGPUTexture texture_2d_float16;
WGPUTexture texture_albedo;
WGPUTexture texture_depth;
WGPUTextureView texture_views[3];
} gbuffer = {0};
// Uniform buffers
static wgpu_buffer_t model_uniform_buffer = {0};
static wgpu_buffer_t camera_uniform_buffer = {0};
static uint64_t camera_uniform_buffer_size = 0;
// Lights
static struct {
WGPUBuffer buffer;
uint64_t buffer_size;
WGPUBuffer extent_buffer;
uint64_t extent_buffer_size;
WGPUBuffer config_uniform_buffer;
uint64_t config_uniform_buffer_size;
WGPUBindGroup buffer_bind_group;
WGPUBindGroupLayout buffer_bind_group_layout;
WGPUBindGroup buffer_compute_bind_group;
WGPUBindGroupLayout buffer_compute_bind_group_layout;
} lights = {0};
// Bind groups
static WGPUBindGroup scene_uniform_bind_group = NULL;
static WGPUBindGroup gbuffer_textures_bind_group = NULL;
// Bind group layouts
static WGPUBindGroupLayout scene_uniform_bind_group_layout = NULL;
static WGPUBindGroupLayout gbuffer_textures_bind_group_layout = NULL;
// Pipelines
static WGPURenderPipeline write_gbuffers_pipeline = NULL;
static WGPURenderPipeline gbuffers_debug_view_pipeline = NULL;
static WGPURenderPipeline deferred_render_pipeline = NULL;
static WGPUComputePipeline light_update_compute_pipeline = NULL;
// Pipeline layouts
static WGPUPipelineLayout write_gbuffers_pipeline_layout = NULL;
static WGPUPipelineLayout gbuffers_debug_view_pipeline_layout = NULL;
static WGPUPipelineLayout deferred_render_pipeline_layout = NULL;
static WGPUPipelineLayout light_update_compute_pipeline_layout = NULL;
// Render pass descriptor
static struct {
WGPURenderPassColorAttachment color_attachments[2];
WGPURenderPassDepthStencilAttachment depth_stencil_attachment;
WGPURenderPassDescriptor descriptor;
} write_gbuffer_pass = {0};
static struct {
WGPURenderPassColorAttachment color_attachments[1];
WGPURenderPassDescriptor descriptor;
} texture_quad_pass = {0};
typedef enum render_mode_enum {
RenderMode_Rendering = 0,
RenderMode_GBuffer_View = 1,
} render_mode_enum;
static struct {
render_mode_enum current_render_mode;
int32_t num_lights;
} settings = {
.current_render_mode = RenderMode_Rendering,
.num_lights = 128,
};
// Other variables
static const char* example_title = "Deferred Rendering";
static bool prepared = false;
// Prepare vertex and index buffers for the Stanford dragon mesh
static void
prepare_vertex_and_index_buffers(wgpu_context_t* wgpu_context,
stanford_dragon_mesh_t* dragon_mesh)
{
/* Create the model vertex buffer */
{
const uint8_t ground_plane_vertex_count = 4;
// position: vec3, normal: vec3, uv: vec2
const uint8_t vertex_stride = 8;
uint64_t vertex_buffer_size
= (dragon_mesh->positions.count + ground_plane_vertex_count)
* vertex_stride * sizeof(float);
WGPUBufferDescriptor buffer_desc = {
.label = "Vertex buffer",
.usage = WGPUBufferUsage_Vertex,
.size = vertex_buffer_size,
.mappedAtCreation = true,
};
vertex_buffer = wgpuDeviceCreateBuffer(wgpu_context->device, &buffer_desc);
ASSERT(vertex_buffer);
float* mapping
= (float*)wgpuBufferGetMappedRange(vertex_buffer, 0, vertex_buffer_size);
ASSERT(mapping);
for (uint64_t i = 0; i < dragon_mesh->positions.count; ++i) {
memcpy(&mapping[vertex_stride * i], dragon_mesh->positions.data[i],
sizeof(vec3));
memcpy(&mapping[vertex_stride * i + 3], dragon_mesh->normals.data[i],
sizeof(vec3));
memcpy(&mapping[vertex_stride * i + 6], dragon_mesh->uvs.data[i],
sizeof(vec2));
}
// Push vertex attributes for an additional ground plane
// clang-format off
static const vec3 ground_plane_positions[4] = {
{-100.0f, 20.0f, -100.0f}, //
{ 100.0f, 20.0f, 100.0f}, //
{-100.0f, 20.0f, 100.0f}, //
{ 100.0f, 20.0f, -100.0f}, //
};
// clang-format on
static const vec3 ground_plane_normals[4] = {
{0.0f, 1.0f, 0.0f}, //
{0.0f, 1.0f, 0.0f}, //
{0.0f, 1.0f, 0.0f}, //
{0.0f, 1.0f, 0.0f}, //
};
static const vec2 ground_plane_uvs[4] = {
{0.0f, 0.0f}, //
{1.0f, 1.0f}, //
{0.0f, 1.0f}, //
{1.0f, 0.0f}, //
};
const uint64_t offset = dragon_mesh->positions.count * vertex_stride;
for (uint64_t i = 0; i < ground_plane_vertex_count; ++i) {
memcpy(&mapping[offset + vertex_stride * i], ground_plane_positions[i],
sizeof(vec3));
memcpy(&mapping[offset + vertex_stride * i + 3], ground_plane_normals[i],
sizeof(vec3));
memcpy(&mapping[offset + vertex_stride * i + 6], ground_plane_uvs[i],
sizeof(vec2));
}
wgpuBufferUnmap(vertex_buffer);
}
/* Create the model index buffer */
{
const uint8_t ground_plane_index_count = 2;
index_count = (dragon_mesh->triangles.count + ground_plane_index_count) * 3;
uint64_t index_buffer_size = index_count * sizeof(uint16_t);
WGPUBufferDescriptor buffer_desc = {
.label = "Index buffer",
.usage = WGPUBufferUsage_Index,
.size = index_buffer_size,
.mappedAtCreation = true,
};
index_buffer = wgpuDeviceCreateBuffer(wgpu_context->device, &buffer_desc);
ASSERT(index_buffer);
uint16_t* mapping
= (uint16_t*)wgpuBufferGetMappedRange(index_buffer, 0, index_buffer_size);
ASSERT(mapping);
for (uint64_t i = 0; i < dragon_mesh->triangles.count; ++i) {
memcpy(&mapping[3 * i], dragon_mesh->triangles.data[i],
sizeof(uint16_t) * 3);
}
// Push indices for an additional ground plane
static const uint16_t ground_plane_indices[2][3] = {
{STANFORD_DRAGON_POSITION_COUNT_RES_4,
STANFORD_DRAGON_POSITION_COUNT_RES_4 + 2,
STANFORD_DRAGON_POSITION_COUNT_RES_4 + 1},
{STANFORD_DRAGON_POSITION_COUNT_RES_4,
STANFORD_DRAGON_POSITION_COUNT_RES_4 + 1,
STANFORD_DRAGON_POSITION_COUNT_RES_4 + 3},
};
const uint64_t offset = dragon_mesh->triangles.count * 3;
for (uint64_t i = 0; i < ground_plane_index_count; ++i) {
memcpy(&mapping[offset + 3 * i], ground_plane_indices[i],
sizeof(uint16_t) * 3);
}
wgpuBufferUnmap(index_buffer);
}
}
// GBuffer texture render targets
static void prepare_gbuffer_texture_render_targets(wgpu_context_t* wgpu_context)
{
{
WGPUTextureDescriptor texture_desc = {
.label = "GBuffer texture view",
.size = (WGPUExtent3D) {
.width = wgpu_context->surface.width,
.height = wgpu_context->surface.height,
.depthOrArrayLayers = 2,
},
.mipLevelCount = 1,
.sampleCount = 1,
.dimension = WGPUTextureDimension_2D,
.format = WGPUTextureFormat_RGBA16Float,
.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding,
};
gbuffer.texture_2d_float16
= wgpuDeviceCreateTexture(wgpu_context->device, &texture_desc);
ASSERT(gbuffer.texture_2d_float16 != NULL);
}
{
WGPUTextureDescriptor texture_desc = {
.label = "GBuffer albedo texture",
.size = (WGPUExtent3D) {
.width = wgpu_context->surface.width,
.height = wgpu_context->surface.height,
.depthOrArrayLayers = 1,
},
.mipLevelCount = 1,
.sampleCount = 1,
.dimension = WGPUTextureDimension_2D,
.format = WGPUTextureFormat_BGRA8Unorm,
.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding,
};
gbuffer.texture_albedo
= wgpuDeviceCreateTexture(wgpu_context->device, &texture_desc);
ASSERT(gbuffer.texture_albedo != NULL);
}
{
WGPUTextureDescriptor texture_desc = {
.label = "GBuffer depth texture",
.size = (WGPUExtent3D) {
.width = wgpu_context->surface.width,
.height = wgpu_context->surface.height,
.depthOrArrayLayers = 2,
},
.mipLevelCount = 1,
.sampleCount = 1,
.dimension = WGPUTextureDimension_2D,
.format = WGPUTextureFormat_Depth24Plus,
.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding,
};
gbuffer.texture_depth
= wgpuDeviceCreateTexture(wgpu_context->device, &texture_desc);
ASSERT(gbuffer.texture_depth != NULL);
}
{
WGPUTextureViewDescriptor texture_view_dec = {
.label = "GBuffer albedo texture view",
.dimension = WGPUTextureViewDimension_2D,
.format = WGPUTextureFormat_Undefined,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
.aspect = WGPUTextureAspect_All,
};
texture_view_dec.format = WGPUTextureFormat_RGBA16Float;
gbuffer.texture_views[0]
= wgpuTextureCreateView(gbuffer.texture_2d_float16, &texture_view_dec);
ASSERT(gbuffer.texture_views[0] != NULL);
texture_view_dec.format = WGPUTextureFormat_BGRA8Unorm;
gbuffer.texture_views[1]
= wgpuTextureCreateView(gbuffer.texture_albedo, &texture_view_dec);
ASSERT(gbuffer.texture_views[1] != NULL);
texture_view_dec.format = WGPUTextureFormat_Depth24Plus;
gbuffer.texture_views[2]
= wgpuTextureCreateView(gbuffer.texture_depth, &texture_view_dec);
ASSERT(gbuffer.texture_views[2] != NULL);
}
}
static void prepare_bind_group_layouts(wgpu_context_t* wgpu_context)
{
// GBuffer textures bind group layout
{
WGPUBindGroupLayoutEntry bgl_entries[3] = {
[0] = (WGPUBindGroupLayoutEntry) {
// Binding 0: Position texture view
.binding = 0,
.visibility = WGPUShaderStage_Fragment,
.texture = (WGPUTextureBindingLayout) {
.sampleType = WGPUTextureSampleType_UnfilterableFloat,
.viewDimension = WGPUTextureViewDimension_2D,
},
.storageTexture = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
// Binding 1: Normal texture view
.binding = 1,
.visibility = WGPUShaderStage_Fragment,
.texture = (WGPUTextureBindingLayout) {
.sampleType = WGPUTextureSampleType_UnfilterableFloat,
.viewDimension = WGPUTextureViewDimension_2D,
},
.storageTexture = {0},
},
[2] = (WGPUBindGroupLayoutEntry) {
// Binding 2: depth texture view
.binding = 2,
.visibility = WGPUShaderStage_Fragment,
.texture = (WGPUTextureBindingLayout) {
.sampleType = WGPUTextureSampleType_Depth,
.viewDimension = WGPUTextureViewDimension_2D,
},
.storageTexture = {0},
}
};
gbuffer_textures_bind_group_layout = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device, &(WGPUBindGroupLayoutDescriptor){
.label = "GBuffer textures bind group layout",
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
});
ASSERT(gbuffer_textures_bind_group_layout != NULL);
}
// Lights buffer bind group layout
{
WGPUBindGroupLayoutEntry bgl_entries[3] = {
[0] = (WGPUBindGroupLayoutEntry) {
// Binding 0: Storage buffer (Fragment shader) - LightsBuffer
.binding = 0,
.visibility = WGPUShaderStage_Fragment | WGPUShaderStage_Compute,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_ReadOnlyStorage,
.minBindingSize = sizeof(float) * light_data_stride * max_num_lights,
},
.sampler = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
// Binding 1: Uniform buffer (Fragment shader) - Config
.binding = 1,
.visibility = WGPUShaderStage_Fragment | WGPUShaderStage_Compute,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = sizeof(uint32_t),
},
.storageTexture = {0},
},
[2] = (WGPUBindGroupLayoutEntry) {
// Binding 2: Uniform buffer (Fragment shader) - LightExtent
.binding = 2,
.visibility = WGPUShaderStage_Fragment,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = sizeof(mat4) * 2,
},
.storageTexture = {0},
},
};
lights.buffer_bind_group_layout = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device, &(WGPUBindGroupLayoutDescriptor){
.label = "Lights buffer bind group layout",
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
});
ASSERT(lights.buffer_bind_group_layout != NULL);
}
// Scene uniform bind group layout
{
WGPUBindGroupLayoutEntry bgl_entries[2] = {
[0] = (WGPUBindGroupLayoutEntry) {
// Binding 0: Uniform buffer (Vertex shader) - Uniforms
.binding = 0,
.visibility = WGPUShaderStage_Vertex,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = 4 * 16 * 2, // two 4x4 matrix
},
.sampler = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
// Binding 1: Uniform buffer (Vertex shader) - Camera
.binding = 1,
.visibility = WGPUShaderStage_Vertex,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = 4 * 16 * 2, // two 4x4 matrix
},
.storageTexture = {0},
},
};
scene_uniform_bind_group_layout = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device, &(WGPUBindGroupLayoutDescriptor){
.label = "Scene uniform bind group layout",
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
});
ASSERT(scene_uniform_bind_group_layout != NULL);
}
// Lights buffer compute bind group layout
{
WGPUBindGroupLayoutEntry bgl_entries[3] = {
[0] = (WGPUBindGroupLayoutEntry) {
// Binding 0: Storage buffer (Compute shader) - LightsBuffer
.binding = 0,
.visibility = WGPUShaderStage_Compute,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Storage,
.minBindingSize = sizeof(float) * light_data_stride * max_num_lights,
},
.sampler = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
// Binding 1: Uniform buffer (Compute shader) - Config
.binding = 1,
.visibility = WGPUShaderStage_Compute,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = sizeof(uint32_t),
},
.storageTexture = {0},
},
[2] = (WGPUBindGroupLayoutEntry) {
// Binding 2: Uniform buffer (Compute shader) - LightExtent
.binding = 2,
.visibility = WGPUShaderStage_Compute,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = camera_uniform_buffer_size,
},
.storageTexture = {0},
},
};
lights.buffer_compute_bind_group_layout = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device,
&(WGPUBindGroupLayoutDescriptor){
.label = "Lights buffer compute bind group layout",
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
});
ASSERT(lights.buffer_compute_bind_group_layout != NULL);
}
}
static void prepare_render_pipeline_layouts(wgpu_context_t* wgpu_context)
{
/* Write GBuffers pipeline layout */
{
write_gbuffers_pipeline_layout = wgpuDeviceCreatePipelineLayout(
wgpu_context->device,
&(WGPUPipelineLayoutDescriptor){
.label = "Write gbuffers pipeline layout",
.bindGroupLayoutCount = 1,
.bindGroupLayouts = &scene_uniform_bind_group_layout,
});
ASSERT(write_gbuffers_pipeline_layout != NULL);
}
/* GBuffers debug view pipeline layout */
{
WGPUBindGroupLayout bind_group_layouts[1] = {
gbuffer_textures_bind_group_layout, /* set 0 */
};
gbuffers_debug_view_pipeline_layout = wgpuDeviceCreatePipelineLayout(
wgpu_context->device,
&(WGPUPipelineLayoutDescriptor){
.label = "GBuffers debug view pipeline layout",
.bindGroupLayoutCount = (uint32_t)ARRAY_SIZE(bind_group_layouts),
.bindGroupLayouts = bind_group_layouts,
});
ASSERT(gbuffers_debug_view_pipeline_layout != NULL);
}
/* Deferred render pipeline layout */
{
WGPUBindGroupLayout bind_group_layouts[2] = {
gbuffer_textures_bind_group_layout, /* set 0 */
lights.buffer_bind_group_layout, /* set 1 */
};
deferred_render_pipeline_layout = wgpuDeviceCreatePipelineLayout(
wgpu_context->device,
&(WGPUPipelineLayoutDescriptor){
.label = "Deferred render pipeline layout",
.bindGroupLayoutCount = (uint32_t)ARRAY_SIZE(bind_group_layouts),
.bindGroupLayouts = bind_group_layouts,
});
ASSERT(deferred_render_pipeline_layout != NULL);
}
}
static void prepare_write_gbuffers_pipeline(wgpu_context_t* wgpu_context)
{
// Primitive state
WGPUPrimitiveState primitive_state = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_Back,
};
// Color target state
WGPUColorTargetState color_target_states[2] = {
// Normal
[0] = (WGPUColorTargetState){
.format = WGPUTextureFormat_RGBA16Float,
.writeMask = WGPUColorWriteMask_All,
},
// Albedo
[1] = (WGPUColorTargetState){
.format = WGPUTextureFormat_BGRA8Unorm,
.writeMask = WGPUColorWriteMask_All,
},
};
// Depth stencil state
WGPUDepthStencilState depth_stencil_state
= wgpu_create_depth_stencil_state(&(create_depth_stencil_state_desc_t){
.format = WGPUTextureFormat_Depth24Plus,
.depth_write_enabled = true,
});
depth_stencil_state.depthCompare = WGPUCompareFunction_Less;
// Vertex buffer layout
WGPU_VERTEX_BUFFER_LAYOUT(
write_gbuffers, sizeof(float) * 8,
// Attribute location 0: Position
WGPU_VERTATTR_DESC(0, WGPUVertexFormat_Float32x3, 0),
// Attribute location 1: Normal
WGPU_VERTATTR_DESC(1, WGPUVertexFormat_Float32x3, sizeof(float) * 3),
// Attribute location 2: uv
WGPU_VERTATTR_DESC(2, WGPUVertexFormat_Float32x2, sizeof(float) * 6))
// 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 = "Write GBuffers - Vertex shader WGSL",
.wgsl_code.source = vertex_write_gbuffers_wgsl,
.entry = "main",
},
.buffer_count = 1,
.buffers = &write_gbuffers_vertex_buffer_layout,
});
// 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 = "Write GBuffers - Fragment shader WGSL",
.wgsl_code.source = fragment_write_gbuffers_wgsl,
.entry = "main",
},
.target_count = (uint32_t)ARRAY_SIZE(color_target_states),
.targets = color_target_states,
});
// 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
write_gbuffers_pipeline = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Write GBuffers render pipeline",
.layout = write_gbuffers_pipeline_layout,
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.depthStencil = &depth_stencil_state,
.multisample = multisample_state,
});
// Partial cleanup
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static void prepare_gbuffers_debug_view_pipeline(wgpu_context_t* wgpu_context)
{
// Primitive state
WGPUPrimitiveState primitive_state = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_Back,
};
// 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,
};
// Constants
WGPUConstantEntry constant_entries[2] = {
[0] = (WGPUConstantEntry){
.key = "canvasSizeWidth",
.value = wgpu_context->surface.width,
},
[1] = (WGPUConstantEntry){
.key = "canvasSizeHeight",
.value = wgpu_context->surface.height,
},
};
// 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 = "Textured quad vertex shader WGSL",
.wgsl_code.source = vertex_texture_quad_wgsl,
.entry = "main",
},
.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 = "GBuffers debug view fragment shader WGSL",
.wgsl_code.source = fragment_gbuffers_debug_view_wgsl,
.entry = "main",
},
.constant_count = (uint32_t)ARRAY_SIZE(constant_entries),
.constants = constant_entries,
.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
gbuffers_debug_view_pipeline = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "GBuffers debug view render pipeline",
.layout = gbuffers_debug_view_pipeline_layout,
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.multisample = multisample_state,
});
// Partial cleanup
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static void prepare_deferred_render_pipeline(wgpu_context_t* wgpu_context)
{
// Primitive state
WGPUPrimitiveState primitive_state = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_Back,
};
// Color target state
WGPUBlendState blend_state = wgpu_create_blend_state(false);
WGPUColorTargetState color_target_state = (WGPUColorTargetState){
.format = WGPUTextureFormat_BGRA8Unorm,
.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 = "Textured quad vertex shader WGSL",
.wgsl_code.source = vertex_texture_quad_wgsl,
.entry = "main",
},
.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 = "Deferred rendering fragment shader WGSL",
.wgsl_code.source = fragment_deferred_rendering_wgsl,
.entry = "main",
},
.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
deferred_render_pipeline = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Deferred render pipeline",
.layout = deferred_render_pipeline_layout,
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.multisample = multisample_state,
});
// Partial cleanup
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static void setup_render_passes(void)
{
/* Write GBuffer pass */
{
// Color attachments
write_gbuffer_pass.color_attachments[0] =
(WGPURenderPassColorAttachment) {
.view = gbuffer.texture_views[0],
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = 0.0f,
.g = 0.0f,
.b = 1.0f,
.a = 1.0f,
},
};
write_gbuffer_pass.color_attachments[1] =
(WGPURenderPassColorAttachment) {
.view = gbuffer.texture_views[1],
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = 0.0f,
.g = 0.0f,
.b = 0.0f,
.a = 1.0f,
},
};
// Render pass depth stencil attachment descriptor
write_gbuffer_pass.depth_stencil_attachment
= (WGPURenderPassDepthStencilAttachment){
.view = gbuffer.texture_views[2],
.depthLoadOp = WGPULoadOp_Clear,
.depthStoreOp = WGPUStoreOp_Store,
.depthClearValue = 1.0f,
.stencilClearValue = 1,
};
// Render pass descriptor
write_gbuffer_pass.descriptor = (WGPURenderPassDescriptor){
.label = "Write GBuffer render pass",
.colorAttachmentCount
= (uint32_t)ARRAY_SIZE(write_gbuffer_pass.color_attachments),
.colorAttachments = write_gbuffer_pass.color_attachments,
.depthStencilAttachment = &write_gbuffer_pass.depth_stencil_attachment,
};
}
/* Texture Quad Pass */
{
// Color attachment
texture_quad_pass.color_attachments[0] =
(WGPURenderPassColorAttachment) {
.view = NULL, // view is acquired and set in render loop.
.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
texture_quad_pass.descriptor = (WGPURenderPassDescriptor){
.label = "Textured Quad render pass",
.colorAttachmentCount = 1,
.colorAttachments = texture_quad_pass.color_attachments,
};
}
}
static void prepare_uniform_buffers(wgpu_context_t* wgpu_context)
{
// Config uniform buffer
{
lights.config_uniform_buffer_size = sizeof(uint32_t);
lights.config_uniform_buffer = wgpuDeviceCreateBuffer(
wgpu_context->device,
&(WGPUBufferDescriptor){
.label = "Config uniform buffer",
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = lights.config_uniform_buffer_size,
.mappedAtCreation = true,
});
ASSERT(lights.config_uniform_buffer);
uint32_t* config_data = (uint32_t*)wgpuBufferGetMappedRange(
lights.config_uniform_buffer, 0, lights.config_uniform_buffer_size);
ASSERT(config_data);
config_data[0] = settings.num_lights;
wgpuBufferUnmap(lights.config_uniform_buffer);
}
// Model uniform buffer
{
model_uniform_buffer = wgpu_create_buffer(
wgpu_context,
&(wgpu_buffer_desc_t){
.label = "Model uniform buffer",
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = sizeof(mat4) * 2, // two 4x4 matrix
});
ASSERT(model_uniform_buffer.buffer);
}
// Camera uniform buffer
{
camera_uniform_buffer = wgpu_create_buffer(
wgpu_context,
&(wgpu_buffer_desc_t){
.label = "Camera uniform buffer",
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = sizeof(mat4) * 2, // two 4x4 matrix
});
ASSERT(camera_uniform_buffer.buffer);
}
// Scene uniform bind group
{
WGPUBindGroupEntry bg_entries[2] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,
.buffer = model_uniform_buffer.buffer,
.size = model_uniform_buffer.size, // two 4x4 matrix
},
[1] = (WGPUBindGroupEntry) {
.binding = 1,
.buffer = camera_uniform_buffer.buffer,
.size = camera_uniform_buffer.size, // two 4x4 matrix
},
};
scene_uniform_bind_group = wgpuDeviceCreateBindGroup(
wgpu_context->device, &(WGPUBindGroupDescriptor){
.label = "Scene uniform bind group",
.layout = wgpuRenderPipelineGetBindGroupLayout(
write_gbuffers_pipeline, 0),
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
});
ASSERT(scene_uniform_bind_group != NULL);
}
// GBuffer textures bind group
{
WGPUBindGroupEntry bg_entries[3] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,
.textureView = gbuffer.texture_views[0],
},
[1] = (WGPUBindGroupEntry) {
.binding = 1,
.textureView = gbuffer.texture_views[1],
},
[2] = (WGPUBindGroupEntry) {
.binding = 2,
.textureView = gbuffer.texture_views[2],
},
};
gbuffer_textures_bind_group = wgpuDeviceCreateBindGroup(
wgpu_context->device, &(WGPUBindGroupDescriptor){
.label = "GBuffer textures bind group",
.layout = gbuffer_textures_bind_group_layout,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
});
ASSERT(gbuffer_textures_bind_group != NULL);
}
}
static void prepare_compute_pipeline_layout(wgpu_context_t* wgpu_context)
{
// Light update compute pipeline layout
{
WGPUPipelineLayoutDescriptor compute_pipeline_layout_desc = {
.label = "Light update compute pipeline layout",
.bindGroupLayoutCount = 1,
.bindGroupLayouts = &lights.buffer_compute_bind_group_layout,
};
light_update_compute_pipeline_layout = wgpuDeviceCreatePipelineLayout(
wgpu_context->device, &compute_pipeline_layout_desc);
ASSERT(light_update_compute_pipeline_layout != NULL);
}
}
static void prepare_light_update_compute_pipeline(wgpu_context_t* wgpu_context)
{
/* Compute shader */
wgpu_shader_t light_update_comp_shader = wgpu_shader_create(
wgpu_context, &(wgpu_shader_desc_t){
// Compute shader WGSL
.label = "Light update compute shader WGSL",
.wgsl_code.source = light_update_wgsl,
.entry = "main",
});
/* Create pipeline */
light_update_compute_pipeline = wgpuDeviceCreateComputePipeline(
wgpu_context->device,
&(WGPUComputePipelineDescriptor){
.label = "Light update compute pipeline",
.layout = light_update_compute_pipeline_layout,
.compute = light_update_comp_shader.programmable_stage_descriptor,
});
/* Partial clean-up */
wgpu_shader_release(&light_update_comp_shader);
}