-
Notifications
You must be signed in to change notification settings - Fork 22
/
reversed_z.c
1235 lines (1107 loc) · 45.3 KB
/
reversed_z.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 <string.h>
#include "../webgpu/imgui_overlay.h"
/* -------------------------------------------------------------------------- *
* WebGPU Example - Reversed Z
*
* This example shows the use of reversed z technique for better utilization of
* depth buffer precision. The left column uses regular method, while the right
* one uses reversed z technique. Both are using depth32float as their depth
* buffer format. A set of red and green planes are positioned very close to
* each other. Higher sets are placed further from camera (and are scaled for
* better visual purpose). To use reversed z to render your scene, you will need
* depth store value to be 0.0, depth compare function to be greater, and remap
* depth range by multiplying an additional matrix to your projection matrix.
*
* Related reading:
* https://developer.nvidia.com/content/depth-precision-visualized
* https://web.archive.org/web/20220724174000/https://thxforthefish.com/posts/reverse_z/
*
* Ref:
* https://github.com/austinEng/webgpu-samples/blob/main/src/sample/reversedZ/main.ts
* https://developer.nvidia.com/content/depth-precision-visualized
* -------------------------------------------------------------------------- */
#define DEFAULT_CANVAS_WIDTH 600
#define DEFAULT_CANVAS_HEIGHT 600
#define X_COUNT 1
#define Y_COUNT 5
#define NUM_INSTANCES (X_COUNT * Y_COUNT)
#define MATRIX_FLOAT_COUNT sizeof(mat4)
// Two planes close to each other for depth precision test
static const uint32_t geometry_vertex_size
= 4 * 8; // Byte size of one geometry vertex.
static const uint32_t geometry_position_offset = 0;
static const uint32_t geometry_color_offset
= 4 * 4; // Byte offset of geometry vertex color attribute.
static const uint32_t geometry_draw_count = 6 * 2;
static const float d = 0.0001f; // half distance between two planes
static const float o
= 0.5f; // half x offset to shift planes so they are only partially overlaping
static const uint32_t default_canvas_width = (uint32_t)DEFAULT_CANVAS_WIDTH;
static const uint32_t default_canvas_height = (uint32_t)DEFAULT_CANVAS_HEIGHT;
static const uint32_t viewport_width = default_canvas_width / 2;
const uint32_t x_count = (uint32_t)X_COUNT;
const uint32_t y_count = (uint32_t)Y_COUNT;
const uint32_t num_instances = (uint32_t)NUM_INSTANCES;
const uint32_t matrix_float_count = (uint32_t)MATRIX_FLOAT_COUNT; // 4x4 matrix
const uint32_t matrix_stride = 4 * matrix_float_count;
static mat4 model_matrices[NUM_INSTANCES] = {0};
static float mvp_matrices_data[NUM_INSTANCES * MATRIX_FLOAT_COUNT] = {0};
static mat4 depth_range_remap_matrix = {
// clang-format off
{1.0f, 0.0f, 0.0f, 0.0f}, //
{0.0f, 1.0f, 0.0f, 0.0f}, //
{0.0f, 0.0f, -1.0f, 0.0f}, //
{0.0f, 0.0f, 1.0f, 1.0f}, //
// clang-format on
};
static mat4 tmp_mat4 = GLM_MAT4_IDENTITY_INIT;
static const WGPUTextureFormat depth_buffer_format
= WGPUTextureFormat_Depth32Float;
// Vertex buffer and attributes
static struct wgpu_buffer_t vertices = {0};
static struct {
WGPUPipelineLayout depth_prepass_render;
WGPUPipelineLayout precision_pass_render;
WGPUPipelineLayout color_pass_render;
WGPUPipelineLayout texture_quad_pass;
} pipline_layouts = {0};
static struct {
WGPURenderPipeline depth_pre_pass[2];
WGPURenderPipeline precision_pass[2];
WGPURenderPipeline color_pass[2];
WGPURenderPipeline texture_quad_pass;
} render_pipelines = {0};
static struct {
texture_t depth;
texture_t default_depth;
} textures = {0};
static WGPURenderPassDescriptor depth_pre_pass_descriptor = {0};
static WGPURenderPassDepthStencilAttachment dppd_rp_ds_att_descriptor = {0};
static WGPURenderPassColorAttachment dpd_rp_color_att_descriptors[2][1] = {0};
static WGPURenderPassDepthStencilAttachment dpd_rp_ds_att_descriptors[2] = {0};
static WGPURenderPassDescriptor draw_pass_descriptors[2] = {0};
static WGPURenderPassColorAttachment tqd_rp_color_att_descriptors[2][1] = {0};
static WGPURenderPassDescriptor texture_quad_pass_descriptors[2] = {0};
static struct {
WGPUBindGroupLayout depth_texture;
WGPUBindGroupLayout uniform;
} bind_group_layouts = {0};
static struct {
WGPUBindGroup depth_texture;
WGPUBindGroup uniform[2];
} bind_groups = {0};
static struct {
wgpu_buffer_t uniform;
wgpu_buffer_t camera_matrix;
wgpu_buffer_t camera_matrix_reversed_depth;
} uniform_buffers = {0};
static uint32_t uniform_buffer_size = num_instances * matrix_stride;
// Other variables
static const char* example_title = "Reversed Z";
static bool prepared = false;
typedef enum render_mode_enum {
RenderMode_Color = 0,
RenderMode_Precision_Error = 1,
RenderMode_Depth_Texture_Quad = 2,
} render_mode_enum;
static render_mode_enum current_render_mode = RenderMode_Color;
typedef enum depth_buffer_mode_enum {
DepthBufferMode_Default = 0,
DepthBufferMode_Reversed = 1,
} depth_buffer_mode_enum;
static const depth_buffer_mode_enum depth_buffer_modes[2] = {
DepthBufferMode_Default, // Default
DepthBufferMode_Reversed, // Reversed
};
static const WGPUCompareFunction depth_compare_funcs[2] = {
WGPUCompareFunction_Less, // Default
WGPUCompareFunction_Greater, // Reversed
};
static const float depth_clear_values[2] = {
1.0f, // Default
0.0f, // Reversed
};
static void prepare_vertex_buffer(wgpu_context_t* wgpu_context)
{
static const float geometry_vertex_array[(4 + 4) * 6 * 2] = {
// float4 position, float4 color
-1 - o, -1, d, 1, 1, 0, 0, 1, //
1 - o, -1, d, 1, 1, 0, 0, 1, //
-1 - o, 1, d, 1, 1, 0, 0, 1, //
1 - o, -1, d, 1, 1, 0, 0, 1, //
1 - o, 1, d, 1, 1, 0, 0, 1, //
-1 - o, 1, d, 1, 1, 0, 0, 1, //
-1 + o, -1, -d, 1, 0, 1, 0, 1, //
1 + o, -1, -d, 1, 0, 1, 0, 1, //
-1 + o, 1, -d, 1, 0, 1, 0, 1, //
1 + o, -1, -d, 1, 0, 1, 0, 1, //
1 + o, 1, -d, 1, 0, 1, 0, 1, //
-1 + o, 1, -d, 1, 0, 1, 0, 1, //
};
// Create vertex buffer
vertices = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Vertex buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = sizeof(geometry_vertex_array),
.count = (uint32_t)ARRAY_SIZE(geometry_vertex_array),
.initial.data = geometry_vertex_array,
});
}
// depthPrePass is used to render scene to the depth texture
// this is not needed if you just want to use reversed z to render a scene
static void prepare_depth_pre_pass_render_pipeline(wgpu_context_t* wgpu_context)
{
// Primitive state
WGPUPrimitiveState primitive_state = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_Back,
};
// Depth stencil state
WGPUDepthStencilState depth_stencil_state
= wgpu_create_depth_stencil_state(&(create_depth_stencil_state_desc_t){
.format = depth_buffer_format,
.depth_write_enabled = true,
});
depth_stencil_state.depthCompare = WGPUCompareFunction_Less;
// Vertex buffer layout
WGPU_VERTEX_BUFFER_LAYOUT(
depth_pre_pass, geometry_vertex_size,
/* Attribute descriptions */
// Attribute location 0: Position
WGPU_VERTATTR_DESC(0, WGPUVertexFormat_Float32x4, geometry_position_offset))
// 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 depth pre pass vertex shader",
.file = "shaders/reversed_z/vertexDepthPrePass.wgsl",
.entry = "main",
},
.buffer_count = 1,
.buffers = &depth_pre_pass_vertex_buffer_layout,
});
// Multisample state
WGPUMultisampleState multisample_state
= wgpu_create_multisample_state_descriptor(
&(create_multisample_state_desc_t){
.sample_count = 1,
});
// depthPrePass is used to render scene to the depth texture
// this is not needed if you just want to use reversed z to render a scene
WGPURenderPipelineDescriptor depth_pre_pass_render_pipeline_descriptor_base
= (WGPURenderPipelineDescriptor){
.label = "depth_pre_pass_render_pipeline",
.layout = pipline_layouts.depth_prepass_render,
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = NULL,
.depthStencil = &depth_stencil_state,
.multisample = multisample_state,
};
// we need the depthCompare to fit the depth buffer mode we are using.
// this is the same for other passes
/* Default */
depth_stencil_state.depthCompare
= depth_compare_funcs[(uint32_t)DepthBufferMode_Default];
render_pipelines.depth_pre_pass[(uint32_t)DepthBufferMode_Default]
= wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &depth_pre_pass_render_pipeline_descriptor_base);
/* Reversed */
depth_stencil_state.depthCompare
= depth_compare_funcs[(uint32_t)DepthBufferMode_Reversed];
render_pipelines.depth_pre_pass[(uint32_t)DepthBufferMode_Reversed]
= wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &depth_pre_pass_render_pipeline_descriptor_base);
// Shader modules are no longer needed once the graphics pipeline has been
// created
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
}
// precisionPass is to draw precision error as color of depth value stored in
// depth buffer compared to that directly calcualated in the shader
static void prepare_precision_pass_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(true);
WGPUColorTargetState color_target_state = (WGPUColorTargetState){
.format = wgpu_context->swap_chain.format,
.blend = &blend_state,
.writeMask = WGPUColorWriteMask_All,
};
// Depth stencil state
WGPUDepthStencilState depth_stencil_state
= wgpu_create_depth_stencil_state(&(create_depth_stencil_state_desc_t){
.format = depth_buffer_format,
.depth_write_enabled = true,
});
depth_stencil_state.depthCompare = WGPUCompareFunction_Less;
// Vertex buffer layout
WGPU_VERTEX_BUFFER_LAYOUT(
precision_error_pass, geometry_vertex_size,
/* Attribute descriptions */
// Attribute location 0: Position
WGPU_VERTATTR_DESC(0, WGPUVertexFormat_Float32x4, geometry_position_offset))
// 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_precision_error_pass_vertex_shader",
.file = "shaders/reversed_z/vertexPrecisionErrorPass.wgsl",
.entry = "main",
},
.buffer_count = 1,
.buffers = &precision_error_pass_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 = "fragment_precision_error_pass_fragment_shader",
.file = "shaders/reversed_z/fragmentPrecisionErrorPass.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,
});
// precisionPass is to draw precision error as color of depth value stored in
// depth buffer compared to that directly calcualated in the shader
WGPURenderPipelineDescriptor precision_pass_render_pipeline_descriptor_base
= (WGPURenderPipelineDescriptor){
.label = "precision_error_pass_render_pipeline",
.layout = pipline_layouts.precision_pass_render,
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.depthStencil = &depth_stencil_state,
.multisample = multisample_state,
};
/* Default */
depth_stencil_state.depthCompare
= depth_compare_funcs[(uint32_t)DepthBufferMode_Default];
render_pipelines.precision_pass[(uint32_t)DepthBufferMode_Default]
= wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &precision_pass_render_pipeline_descriptor_base);
/* Reversed */
depth_stencil_state.depthCompare
= depth_compare_funcs[(uint32_t)DepthBufferMode_Reversed];
render_pipelines.precision_pass[(uint32_t)DepthBufferMode_Reversed]
= wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &precision_pass_render_pipeline_descriptor_base);
// Shader modules are no longer needed once the graphics pipeline has been
// created
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
// colorPass is the regular render pass to render the scene
static void prepare_color_pass_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(true);
WGPUColorTargetState color_target_state = (WGPUColorTargetState){
.format = wgpu_context->swap_chain.format,
.blend = &blend_state,
.writeMask = WGPUColorWriteMask_All,
};
// Depth stencil state
WGPUDepthStencilState depth_stencil_state
= wgpu_create_depth_stencil_state(&(create_depth_stencil_state_desc_t){
.format = depth_buffer_format,
.depth_write_enabled = true,
});
depth_stencil_state.depthCompare = WGPUCompareFunction_Less;
// Vertex buffer layout
WGPU_VERTEX_BUFFER_LAYOUT(
color_pass, geometry_vertex_size,
/* Attribute descriptions */
// Attribute location 0: Position
WGPU_VERTATTR_DESC(0, WGPUVertexFormat_Float32x4, geometry_position_offset),
// Attribute location 1: Color
WGPU_VERTATTR_DESC(1, WGPUVertexFormat_Float32x4, geometry_color_offset))
// 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 = "color_pass_vertex_shader",
.file = "shaders/reversed_z/vertex.wgsl",
.entry = "main",
},
.buffer_count = 1,
.buffers = &color_pass_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 = "color_pass_fragment_shader",
.file = "shaders/reversed_z/fragment.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,
});
// colorPass is the regular render pass to render the scene
WGPURenderPipelineDescriptor color_passRender_pipeline_descriptor_base
= (WGPURenderPipelineDescriptor){
.label = "color_pass_render_pipeline",
.layout = pipline_layouts.color_pass_render,
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.depthStencil = &depth_stencil_state,
.multisample = multisample_state,
};
/* Default */
depth_stencil_state.depthCompare
= depth_compare_funcs[(uint32_t)DepthBufferMode_Default];
render_pipelines.color_pass[(uint32_t)DepthBufferMode_Default]
= wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &color_passRender_pipeline_descriptor_base);
/* Reversed */
depth_stencil_state.depthCompare
= depth_compare_funcs[(uint32_t)DepthBufferMode_Reversed];
render_pipelines.color_pass[(uint32_t)DepthBufferMode_Reversed]
= wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &color_passRender_pipeline_descriptor_base);
// Shader modules are no longer needed once the graphics pipeline has been
// created
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
// textureQuadPass is draw a full screen quad of depth texture
// to see the difference of depth value using reversed z compared to default
// depth buffer usage 0.0 will be the furthest and 1.0 will be the closest
static void
prepare_texture_quad_pass_render_pipeline(wgpu_context_t* wgpu_context)
{
// Primitive state
WGPUPrimitiveState primitive_state = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_None,
};
// Color blend state
WGPUBlendState blend_state = wgpu_create_blend_state(true);
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 = "vertex_texture_quad_vertex_shader",
.file = "shaders/reversed_z/vertexTextureQuad.wgsl",
.entry = "main",
},
.buffer_count = 0,
});
// 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 = "fragment_texture_quad_fragment_shader",
.file = "shaders/reversed_z/fragmentTextureQuad.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,
});
// textureQuadPass is draw a full screen quad of depth texture
// to see the difference of depth value using reversed z compared to default
// depth buffer usage 0.0 will be the furthest and 1.0 will be the closest
render_pipelines.texture_quad_pass = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "texture_quad_pass_render_pipeline",
.layout = pipline_layouts.texture_quad_pass,
.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_depth_textures(wgpu_context_t* wgpu_context)
{
// Create the depth texture.
{
WGPUTextureDescriptor texture_desc = {
.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding,
.dimension = WGPUTextureDimension_2D,
.format = depth_buffer_format,
.mipLevelCount = 1,
.sampleCount = 1,
.size = (WGPUExtent3D) {
.width = wgpu_context->surface.width,
.height = wgpu_context->surface.height,
.depthOrArrayLayers = 1,
},
};
textures.depth.texture
= wgpuDeviceCreateTexture(wgpu_context->device, &texture_desc);
// Create the texture view
WGPUTextureViewDescriptor texture_view_dec = {
.dimension = WGPUTextureViewDimension_2D,
.format = texture_desc.format,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
};
textures.depth.view
= wgpuTextureCreateView(textures.depth.texture, &texture_view_dec);
}
// Create the default depth texture.
{
WGPUTextureDescriptor texture_desc = {
.usage = WGPUTextureUsage_RenderAttachment,
.dimension = WGPUTextureDimension_2D,
.format = depth_buffer_format,
.mipLevelCount = 1,
.sampleCount = 1,
.size = (WGPUExtent3D) {
.width = wgpu_context->surface.width,
.height = wgpu_context->surface.height,
.depthOrArrayLayers = 1,
},
};
textures.default_depth.texture
= wgpuDeviceCreateTexture(wgpu_context->device, &texture_desc);
// Create the texture view
WGPUTextureViewDescriptor texture_view_dec = {
.dimension = WGPUTextureViewDimension_2D,
.format = texture_desc.format,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
};
textures.default_depth.view = wgpuTextureCreateView(
textures.default_depth.texture, &texture_view_dec);
}
}
static void prepare_depth_pre_pass_descriptor(void)
{
dppd_rp_ds_att_descriptor = (WGPURenderPassDepthStencilAttachment){
.view = textures.depth.view,
.depthLoadOp = WGPULoadOp_Clear,
.depthStoreOp = WGPUStoreOp_Store,
.depthClearValue = 1.0f,
};
depth_pre_pass_descriptor = (WGPURenderPassDescriptor){
.colorAttachmentCount = 0,
.colorAttachments = NULL,
.depthStencilAttachment = &dppd_rp_ds_att_descriptor,
};
}
// drawPassDescriptor and drawPassLoadDescriptor are used for drawing
// the scene twice using different depth buffer mode on splitted viewport
// of the same canvas
// see the difference of the loadValue of the colorAttachments
static void prepare_draw_pass_descriptors(void)
{
// drawPassDescriptor
{
// Color attachment
dpd_rp_color_att_descriptors[0][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.5f,
.a = 1.0f,
},
};
dpd_rp_ds_att_descriptors[0] = (WGPURenderPassDepthStencilAttachment){
.view = textures.default_depth.view,
.depthLoadOp = WGPULoadOp_Clear,
.depthStoreOp = WGPUStoreOp_Store,
.depthClearValue = 1.0f,
};
draw_pass_descriptors[0] = (WGPURenderPassDescriptor){
.colorAttachmentCount = 1,
.colorAttachments = dpd_rp_color_att_descriptors[0],
.depthStencilAttachment = &dpd_rp_ds_att_descriptors[0],
};
}
// drawPassLoadDescriptor
{
dpd_rp_color_att_descriptors[1][0] = (WGPURenderPassColorAttachment){
.view = NULL, // view is acquired and set in render loop.
.depthSlice = ~0,
.loadOp = WGPULoadOp_Load,
.storeOp = WGPUStoreOp_Store,
};
dpd_rp_ds_att_descriptors[1] = (WGPURenderPassDepthStencilAttachment){
.view = textures.default_depth.view,
.depthLoadOp = WGPULoadOp_Clear,
.depthStoreOp = WGPUStoreOp_Store,
.depthClearValue = 1.0f,
};
draw_pass_descriptors[1] = (WGPURenderPassDescriptor){
.colorAttachmentCount = 1,
.colorAttachments = dpd_rp_color_att_descriptors[1],
.depthStencilAttachment = &dpd_rp_ds_att_descriptors[1],
};
}
}
static void prepare_texture_quad_pass_descriptors(void)
{
// textureQuadPassDescriptor
{
tqd_rp_color_att_descriptors[0][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.5f,
.a = 1.0f,
},
};
texture_quad_pass_descriptors[0] = (WGPURenderPassDescriptor){
.colorAttachmentCount = 1,
.colorAttachments = tqd_rp_color_att_descriptors[0],
};
}
// textureQuadPassLoadDescriptor
{
tqd_rp_color_att_descriptors[1][0] = (WGPURenderPassColorAttachment){
.view = NULL, // attachment is acquired and set in render loop.
.depthSlice = ~0,
.loadOp = WGPULoadOp_Load,
.storeOp = WGPUStoreOp_Store,
};
texture_quad_pass_descriptors[1] = (WGPURenderPassDescriptor){
.colorAttachmentCount = 1,
.colorAttachments = tqd_rp_color_att_descriptors[1],
};
}
}
static void
prepare_depth_texture_bind_group_layout(wgpu_context_t* wgpu_context)
{
WGPUBindGroupLayoutEntry bgl_entries[1] = {
[0] = (WGPUBindGroupLayoutEntry) {
// Texture view
.binding = 0,
.visibility = WGPUShaderStage_Fragment,
.texture = (WGPUTextureBindingLayout) {
.sampleType = WGPUTextureSampleType_Depth,
.viewDimension = WGPUTextureViewDimension_2D,
.multisampled = false,
},
.storageTexture = {0},
}
};
WGPUBindGroupLayoutDescriptor bgl_desc = {
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
};
bind_group_layouts.depth_texture
= wgpuDeviceCreateBindGroupLayout(wgpu_context->device, &bgl_desc);
ASSERT(bind_group_layouts.depth_texture != NULL)
}
// Model, view, projection matrices
static void prepare_uniform_bind_group_layout(wgpu_context_t* wgpu_context)
{
WGPUBindGroupLayoutEntry bgl_entries[2] = {
[0] = (WGPUBindGroupLayoutEntry) {
// Uniform buffer
.binding = 0,
.visibility = WGPUShaderStage_Vertex,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = uniform_buffer_size,
},
.sampler = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
// Uniform buffer
.binding = 1,
.visibility = WGPUShaderStage_Vertex,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = sizeof(mat4), // 4x4 matrix
},
.sampler = {0},
}
};
WGPUBindGroupLayoutDescriptor bgl_desc = {
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
};
bind_group_layouts.uniform
= wgpuDeviceCreateBindGroupLayout(wgpu_context->device, &bgl_desc);
ASSERT(bind_group_layouts.uniform != NULL)
}
static void setup_pipeline_layout(wgpu_context_t* wgpu_context)
{
// Depth Pre-pass render pipeline layout
{
WGPUBindGroupLayout bind_group_layout_array[1] = {
bind_group_layouts.uniform,
};
pipline_layouts.depth_prepass_render = wgpuDeviceCreatePipelineLayout(
wgpu_context->device,
&(WGPUPipelineLayoutDescriptor){
.bindGroupLayoutCount = (uint32_t)ARRAY_SIZE(bind_group_layout_array),
.bindGroupLayouts = bind_group_layout_array,
});
ASSERT(pipline_layouts.depth_prepass_render != NULL)
}
// Precision pass render pipeline layout
{
WGPUBindGroupLayout bind_group_layout_array[2] = {
bind_group_layouts.uniform, // Group 0
bind_group_layouts.depth_texture, // Group 1
};
pipline_layouts.precision_pass_render = wgpuDeviceCreatePipelineLayout(
wgpu_context->device,
&(WGPUPipelineLayoutDescriptor){
.bindGroupLayoutCount = (uint32_t)ARRAY_SIZE(bind_group_layout_array),
.bindGroupLayouts = bind_group_layout_array,
});
ASSERT(pipline_layouts.precision_pass_render != NULL)
}
// Color pass render pipeline layout
{
WGPUBindGroupLayout bind_group_layout_array[1] = {
bind_group_layouts.uniform,
};
pipline_layouts.color_pass_render = wgpuDeviceCreatePipelineLayout(
wgpu_context->device,
&(WGPUPipelineLayoutDescriptor){
.bindGroupLayoutCount = (uint32_t)ARRAY_SIZE(bind_group_layout_array),
.bindGroupLayouts = bind_group_layout_array,
});
ASSERT(pipline_layouts.color_pass_render != NULL)
}
// Texture quad pass pipline layout
{
WGPUBindGroupLayout bind_group_layout_array[1] = {
bind_group_layouts.depth_texture,
};
pipline_layouts.texture_quad_pass = wgpuDeviceCreatePipelineLayout(
wgpu_context->device,
&(WGPUPipelineLayoutDescriptor){
.bindGroupLayoutCount = (uint32_t)ARRAY_SIZE(bind_group_layout_array),
.bindGroupLayouts = bind_group_layout_array,
});
ASSERT(pipline_layouts.texture_quad_pass != NULL)
}
}
static void prepare_depth_texture_bind_group(wgpu_context_t* wgpu_context)
{
WGPUBindGroupEntry bg_entries[1] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,
.textureView = textures.depth.view,
},
};
WGPUBindGroupDescriptor bg_desc = {
.layout = bind_group_layouts.depth_texture,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
};
bind_groups.depth_texture
= wgpuDeviceCreateBindGroup(wgpu_context->device, &bg_desc);
ASSERT(bind_groups.depth_texture != NULL)
}
static void prepare_uniform_buffers(wgpu_context_t* wgpu_context)
{
uniform_buffers.uniform = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = uniform_buffer_size,
});
uniform_buffers.camera_matrix = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = sizeof(mat4), // 4x4 matrix
});
uniform_buffers.camera_matrix_reversed_depth = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = sizeof(mat4), // 4x4 matrix
});
}
static void setup_uniform_bind_groups(wgpu_context_t* wgpu_context)
{
// 1st uniform bind group
{
WGPUBindGroupEntry bg_entries[2] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,
.buffer = uniform_buffers.uniform.buffer,
.size = uniform_buffer_size,
},
[1] = (WGPUBindGroupEntry) {
.binding = 1,
.buffer = uniform_buffers.camera_matrix.buffer,
.size = sizeof(mat4), // 4x4 matrix
}
};
bind_groups.uniform[0] = wgpuDeviceCreateBindGroup(
wgpu_context->device, &(WGPUBindGroupDescriptor){
.layout = bind_group_layouts.uniform,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
});
}
// 2nd uniform bind group
{
WGPUBindGroupEntry bg_entries[2] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,
.buffer = uniform_buffers.uniform.buffer,
.size = uniform_buffer_size,
},
[1] = (WGPUBindGroupEntry) {
.binding = 1,
.buffer = uniform_buffers.camera_matrix_reversed_depth.buffer,
.size = sizeof(mat4), // 4x4 matrix
}
};
bind_groups.uniform[1] = wgpuDeviceCreateBindGroup(
wgpu_context->device, &(WGPUBindGroupDescriptor){
.layout = bind_group_layouts.uniform,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
});
}
}
static void init_uniform_buffers(wgpu_context_t* wgpu_context)
{
uint32_t m = 0;
float z = 0.0f, s = 0.0f;
for (uint32_t x = 0; x < x_count; ++x) {
for (uint32_t y = 0; y < y_count; ++y) {
z = -800.0f * m;
s = 1.0f + 50.0f * m;
glm_mat4_identity(model_matrices[m]);
glm_translate(model_matrices[m], //
(vec3){
x - x_count / 2.0f + 0.5f, // x
(4.0f - 0.2f * z) * (y - y_count / 2.0f + 1.0f), // y
z, // z
});
glm_scale(model_matrices[m], (vec3){s, s, s});
++m;
}
}
mat4 view_matrix = GLM_MAT4_IDENTITY_INIT;
glm_translate(view_matrix, (vec3){0.0f, 0.0f, -12.0f});
const float aspect = (0.5f * (float)wgpu_context->surface.width)
/ (float)wgpu_context->surface.height;
mat4 projection_matrix = GLM_MAT4_IDENTITY_INIT;
float far = INFINITY;
perspective_zo(&projection_matrix, PI2 / 5.0f, aspect, 5.0f, &far);
mat4 view_projection_matrix = GLM_MAT4_IDENTITY_INIT;
glm_mat4_mul(projection_matrix, view_matrix, view_projection_matrix);
mat4 reversed_range_view_projection_matrix = GLM_MAT4_IDENTITY_INIT;
// to use 1/z we just multiple depthRangeRemapMatrix to our default camera
// view projection matrix
glm_mat4_mul(depth_range_remap_matrix, view_projection_matrix,
reversed_range_view_projection_matrix);
wgpu_queue_write_buffer(wgpu_context, uniform_buffers.camera_matrix.buffer, 0,
view_projection_matrix, sizeof(mat4));
wgpu_queue_write_buffer(
wgpu_context, uniform_buffers.camera_matrix_reversed_depth.buffer, 0,
reversed_range_view_projection_matrix, sizeof(mat4));
}
static void update_transformation_matrix(wgpu_example_context_t* context)
{
const float now = context->frame.timestamp_millis / 1000.0f;
const float sin_now = sin(now), cos_now = cos(now);
for (uint32_t i = 0, m = 0; i < num_instances; ++i, m += matrix_float_count) {
glm_mat4_copy(model_matrices[i], tmp_mat4);
glm_rotate(tmp_mat4, (PI / 180.f) * 30.0f, (vec3){sin_now, cos_now, 0.0f});
memcpy(&mvp_matrices_data[m], tmp_mat4, sizeof(mat4));
}
}
static void update_uniform_buffers(wgpu_example_context_t* context)
{
update_transformation_matrix(context);
wgpu_queue_write_buffer(context->wgpu_context, uniform_buffers.uniform.buffer,
0, &mvp_matrices_data, sizeof(mvp_matrices_data));
}
static int example_initialize(wgpu_example_context_t* context)
{
UNUSED_VAR(depth_buffer_modes);
if (context) {
prepare_vertex_buffer(context->wgpu_context);
prepare_depth_texture_bind_group_layout(context->wgpu_context);
prepare_uniform_bind_group_layout(context->wgpu_context);
setup_pipeline_layout(context->wgpu_context);
prepare_depth_pre_pass_render_pipeline(context->wgpu_context);
prepare_precision_pass_render_pipeline(context->wgpu_context);
prepare_color_pass_render_pipeline(context->wgpu_context);
prepare_texture_quad_pass_render_pipeline(context->wgpu_context);
prepare_depth_textures(context->wgpu_context);
prepare_depth_pre_pass_descriptor();
prepare_draw_pass_descriptors();
prepare_texture_quad_pass_descriptors();
prepare_depth_texture_bind_group(context->wgpu_context);
prepare_uniform_buffers(context->wgpu_context);