-
Notifications
You must be signed in to change notification settings - Fork 24
/
hdr.c
1320 lines (1180 loc) · 47.2 KB
/
hdr.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/gltf_model.h"
#include "../webgpu/imgui_overlay.h"
/* -------------------------------------------------------------------------- *
* WebGPU Example - High Dynamic Range Rendering
*
* Implements a high dynamic range rendering pipeline using 16/32 bit floating
* point precision for all internal formats, textures and calculations,
* including a bloom pass, manual exposure and tone mapping.
*
* Ref:
* https://github.com/SaschaWillems/Vulkan/tree/master/examples/hdr
* -------------------------------------------------------------------------- */
#define NUMBER_OF_CONSTANTS 3u
#define ALIGNMENT 256u /* 256-byte alignment */
static bool bloom = false;
static bool display_skybox = true;
static struct {
texture_t envmap;
} textures = {0};
static struct {
struct gltf_model_t* skybox;
struct {
const char* name;
const char* filelocation;
struct gltf_model_t* object;
} objects[4];
int32_t object_index;
} models = {
.objects = {
// clang-format off
{ .name = "Sphere", .filelocation = "models/sphere.gltf" },
{ .name = "Teapot", .filelocation = "models/teapot.gltf" },
{ .name = "Torusknot", .filelocation = "models/torusknot.gltf" },
{ .name = "Venus", .filelocation = "models/venus.gltf" },
// clang-format on
},
.object_index = 1,
};
static const char* object_names[4] = {"Sphere", "Teapot", "Torusknot", "Venus"};
static struct {
wgpu_buffer_t matrices;
wgpu_buffer_t params;
struct {
WGPUBuffer buffer;
uint64_t buffer_size;
uint64_t model_size;
} dynamic;
} uniform_buffers = {0};
static struct {
mat4 projection;
mat4 model_view;
mat4 inverse_modelview;
} ubo_matrices = {0};
static struct {
float exposure;
} ubo_params = {
.exposure = 1.0f,
};
static struct {
int value;
uint8_t padding[252];
} ubo_constants[NUMBER_OF_CONSTANTS] = {0};
static struct {
WGPURenderPipeline skybox;
WGPURenderPipeline reflect;
WGPURenderPipeline composition;
WGPURenderPipeline bloom[2];
} pipelines = {0};
static struct {
WGPUPipelineLayout models;
WGPUPipelineLayout composition;
WGPUPipelineLayout bloom_filter;
} pipeline_layouts = {0};
static struct {
WGPUBindGroup object;
WGPUBindGroup skybox;
WGPUBindGroup composition;
WGPUBindGroup bloom_filter;
} bind_groups = {0};
static struct {
WGPUBindGroupLayout models;
WGPUBindGroupLayout composition;
WGPUBindGroupLayout bloom_filter;
} bind_group_layouts = {0};
typedef enum wgpu_render_pass_attachment_type_t {
WGPU_RENDER_PASS_COLOR_ATTACHMENT_TYPE = 0x00000001,
WGPU_RENDER_PASS_DEPTH_STENCIL_ATTACHMENT_TYPE = 0x00000002,
} wgpu_render_pass_attachment_type_t;
/* Framebuffer for offscreen rendering */
typedef struct {
WGPUTexture texture;
WGPUTextureView texture_view;
WGPUTextureFormat format;
} frame_buffer_attachment_t;
static struct {
uint32_t width, height;
frame_buffer_attachment_t color[2];
frame_buffer_attachment_t depth;
struct {
WGPURenderPassColorAttachment color_attachment[2];
WGPURenderPassDepthStencilAttachment depth_stencil_attachment;
WGPURenderPassDescriptor render_pass_descriptor;
} render_pass_desc;
WGPUSampler sampler;
} offscreen_pass = {0};
static struct {
uint32_t width, height;
frame_buffer_attachment_t color[1];
struct {
WGPURenderPassColorAttachment color_attachment[1];
WGPURenderPassDescriptor render_pass_descriptor;
} render_pass_desc;
WGPUSampler sampler;
} filter_pass = {0};
static WGPURenderPassColorAttachment rp_color_att_descriptors[1] = {0};
static WGPURenderPassDescriptor render_pass_desc = {0};
static WGPUTextureFormat depth_format = WGPUTextureFormat_Depth24PlusStencil8;
static const char* example_title = "High Dynamic Range Rendering";
static bool prepared = false;
static void setup_camera(wgpu_example_context_t* context)
{
context->camera = camera_create();
context->camera->type = CameraType_LookAt;
camera_set_position(context->camera, (vec3){0.0f, 0.0f, -6.0f});
camera_set_rotation(context->camera, (vec3){0.0f, 0.0f, 0.0f});
camera_set_perspective(context->camera, 60.0f,
context->window_size.aspect_ratio, 0.1f, 256.0f);
}
static void load_assets(wgpu_context_t* wgpu_context)
{
/* Load glTF models */
const uint32_t gltf_loading_flags
= WGPU_GLTF_FileLoadingFlags_PreTransformVertices
| WGPU_GLTF_FileLoadingFlags_FlipY;
models.skybox
= wgpu_gltf_model_load_from_file(&(wgpu_gltf_model_load_options_t){
.wgpu_context = wgpu_context,
.filename = "models/cube.gltf",
.file_loading_flags = gltf_loading_flags,
});
for (uint8_t i = 0; i < (uint8_t)ARRAY_SIZE(models.objects); ++i) {
models.objects[i].object
= wgpu_gltf_model_load_from_file(&(wgpu_gltf_model_load_options_t){
.wgpu_context = wgpu_context,
.filename = models.objects[i].filelocation,
.file_loading_flags = gltf_loading_flags,
});
}
/* Load cube map */
static const char* cubemap[6] = {
"textures/cubemaps/uffizi_cube_px.png", /* Right */
"textures/cubemaps/uffizi_cube_nx.png", /* Left */
"textures/cubemaps/uffizi_cube_py.png", /* Top */
"textures/cubemaps/uffizi_cube_ny.png", /* Bottom */
"textures/cubemaps/uffizi_cube_pz.png", /* Back */
"textures/cubemaps/uffizi_cube_nz.png", /* Front */
};
textures.envmap = wgpu_create_texture_cubemap_from_files(
wgpu_context, cubemap,
&(struct wgpu_texture_load_options_t){
.flip_y = true, /* Flip y to match uffizi_cube_nz.ktx hdr cubemap */
});
ASSERT(textures.envmap.texture != NULL);
}
void create_attachment(wgpu_context_t* wgpu_context, const char* texture_label,
WGPUTextureFormat format,
wgpu_render_pass_attachment_type_t attachment_type,
frame_buffer_attachment_t* attachment)
{
/* Create the texture extent */
WGPUExtent3D texture_extent = {
.width = offscreen_pass.width,
.height = offscreen_pass.height,
.depthOrArrayLayers = 1,
};
/* Texture usage flags */
WGPUTextureUsageFlags usage_flags = WGPUTextureUsage_RenderAttachment;
if (attachment_type == WGPU_RENDER_PASS_COLOR_ATTACHMENT_TYPE) {
usage_flags = usage_flags | WGPUTextureUsage_TextureBinding;
}
else if (attachment_type == WGPU_RENDER_PASS_DEPTH_STENCIL_ATTACHMENT_TYPE) {
usage_flags = usage_flags | WGPUTextureUsage_CopySrc;
}
/* Texture format */
attachment->format = format;
/* Create the texture */
WGPUTextureDescriptor texture_desc = {
.label = texture_label,
.size = texture_extent,
.mipLevelCount = 1,
.sampleCount = 1,
.dimension = WGPUTextureDimension_2D,
.format = attachment->format,
.usage = usage_flags,
};
attachment->texture
= wgpuDeviceCreateTexture(wgpu_context->device, &texture_desc);
ASSERT(attachment->texture);
/* Create the texture view */
WGPUTextureViewDescriptor texture_view_dec = {
.label = "Texture view",
.dimension = WGPUTextureViewDimension_2D,
.format = texture_desc.format,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
.aspect = WGPUTextureAspect_All,
};
attachment->texture_view
= wgpuTextureCreateView(attachment->texture, &texture_view_dec);
ASSERT(attachment->texture_view);
}
/**
* Prepare a new framebuffer and attachments for offscreen rendering (G-Buffer)
*/
static void prepare_offscreen(wgpu_context_t* wgpu_context)
{
/* Offscreen render pass */
{
offscreen_pass.width = wgpu_context->surface.width;
offscreen_pass.height = wgpu_context->surface.height;
/* Color attachments */
/* Two floating point color buffers */
create_attachment(
wgpu_context, "Offscreen - Color texure 1", WGPUTextureFormat_RGBA8Unorm,
WGPU_RENDER_PASS_COLOR_ATTACHMENT_TYPE, &offscreen_pass.color[0]);
create_attachment(
wgpu_context, "Offscreen - Color texure 2", WGPUTextureFormat_RGBA8Unorm,
WGPU_RENDER_PASS_COLOR_ATTACHMENT_TYPE, &offscreen_pass.color[1]);
/* Depth attachment */
create_attachment(wgpu_context, "Offscreen - Depth texture", depth_format,
WGPU_RENDER_PASS_DEPTH_STENCIL_ATTACHMENT_TYPE,
&offscreen_pass.depth);
/* Init attachment properties */
/* Color attachment */
for (uint32_t i = 0; i < 2; ++i) {
offscreen_pass.render_pass_desc.color_attachment[i]
= (WGPURenderPassColorAttachment) {
.view = offscreen_pass.color[i].texture_view,
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = 0.0f,
.g = 0.0f,
.b = 0.0f,
.a = 0.0f,
},
};
}
/* Depth stencil attachment */
offscreen_pass.render_pass_desc.depth_stencil_attachment
= (WGPURenderPassDepthStencilAttachment){
.view = offscreen_pass.depth.texture_view,
.depthLoadOp = WGPULoadOp_Clear,
.depthStoreOp = WGPUStoreOp_Store,
.depthClearValue = 1.0f,
.stencilLoadOp = WGPULoadOp_Clear,
.stencilStoreOp = WGPUStoreOp_Store,
.stencilClearValue = 0,
};
/* Render pass descriptor */
offscreen_pass.render_pass_desc.render_pass_descriptor
= (WGPURenderPassDescriptor){
.label = "Offscreen - Render pass descriptor",
.colorAttachmentCount = 2,
.colorAttachments = offscreen_pass.render_pass_desc.color_attachment,
.depthStencilAttachment
= &offscreen_pass.render_pass_desc.depth_stencil_attachment,
};
/* Create sampler to sample from the color attachments */
offscreen_pass.sampler = wgpuDeviceCreateSampler(
wgpu_context->device, &(WGPUSamplerDescriptor){
.label = "Texture - Sampler",
.addressModeU = WGPUAddressMode_ClampToEdge,
.addressModeV = WGPUAddressMode_ClampToEdge,
.addressModeW = WGPUAddressMode_ClampToEdge,
.minFilter = WGPUFilterMode_Nearest,
.magFilter = WGPUFilterMode_Nearest,
.mipmapFilter = WGPUMipmapFilterMode_Linear,
.lodMinClamp = 0.0f,
.lodMaxClamp = 1.0f,
.maxAnisotropy = 1,
});
ASSERT(offscreen_pass.sampler != NULL);
}
/* Bloom separable filter pass */
{
filter_pass.width = wgpu_context->surface.width;
filter_pass.height = wgpu_context->surface.height;
/* Color attachments */
/* Floating point color buffer */
create_attachment(
wgpu_context, "Bloom color - Texture", WGPUTextureFormat_RGBA8Unorm,
WGPU_RENDER_PASS_COLOR_ATTACHMENT_TYPE, &filter_pass.color[0]);
/* Init attachment properties */
/* Color attachment */
filter_pass.render_pass_desc.color_attachment[0]
= (WGPURenderPassColorAttachment) {
.view = filter_pass.color[0].texture_view,
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = 0.0f,
.g = 0.0f,
.b = 0.0f,
.a = 0.0f,
},
};
/* Render pass descriptor */
filter_pass.render_pass_desc.render_pass_descriptor
= (WGPURenderPassDescriptor){
.label = "Filter - Render pass descriptor",
.colorAttachmentCount = 1,
.colorAttachments = filter_pass.render_pass_desc.color_attachment,
.depthStencilAttachment = NULL,
};
/* Create sampler to sample from the color attachment */
filter_pass.sampler = wgpuDeviceCreateSampler(
wgpu_context->device, &(WGPUSamplerDescriptor){
.label = "Filter pass - Texture sampler",
.addressModeU = WGPUAddressMode_ClampToEdge,
.addressModeV = WGPUAddressMode_ClampToEdge,
.addressModeW = WGPUAddressMode_ClampToEdge,
.minFilter = WGPUFilterMode_Nearest,
.magFilter = WGPUFilterMode_Nearest,
.mipmapFilter = WGPUMipmapFilterMode_Linear,
.lodMinClamp = 0.0f,
.lodMaxClamp = 1.0f,
.maxAnisotropy = 1,
});
}
}
static void setup_pipeline_layout(wgpu_context_t* wgpu_context)
{
/* Bind group layout for models */
{
WGPUBindGroupLayoutEntry bgl_entries[5] = {
[0] = (WGPUBindGroupLayoutEntry) {
/* Binding 0: Vertex / fragment shader uniform buffer */
.binding = 0,
.visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.hasDynamicOffset = false,
.minBindingSize = sizeof(ubo_matrices),
},
.sampler = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
/* Binding 1: Fragment shader image view */
.binding = 1,
.visibility = WGPUShaderStage_Fragment,
.texture = (WGPUTextureBindingLayout) {
.sampleType = WGPUTextureSampleType_Float,
.viewDimension = WGPUTextureViewDimension_Cube,
.multisampled = false,
},
.storageTexture = {0},
},
[2] = (WGPUBindGroupLayoutEntry) {
/* Binding 2: Fragment shader image sampler */
.binding = 2,
.visibility = WGPUShaderStage_Fragment,
.sampler = (WGPUSamplerBindingLayout){
.type = WGPUSamplerBindingType_Filtering,
},
.texture = {0},
},
[3] = (WGPUBindGroupLayoutEntry) {
/* Binding 3: Fragment shader uniform buffer */
.binding = 3,
.visibility = WGPUShaderStage_Fragment,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.hasDynamicOffset = false,
.minBindingSize = sizeof(ubo_params),
},
.sampler = {0},
},
[4] = (WGPUBindGroupLayoutEntry) {
/* Binding 4: Vertex / fragment shader dynamic uniform buffer */
.binding = 4,
.visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.hasDynamicOffset = true,
.minBindingSize = sizeof(ubo_constants[0].value),
},
.sampler = {0},
},
};
/* Create the bind group layout */
bind_group_layouts.models = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device, &(WGPUBindGroupLayoutDescriptor){
.label = "Models - Bind group layout",
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
});
ASSERT(bind_group_layouts.models != NULL);
/* Create the pipeline layout */
pipeline_layouts.models = wgpuDeviceCreatePipelineLayout(
wgpu_context->device, &(WGPUPipelineLayoutDescriptor){
.label = "Models - Pipeline layout",
.bindGroupLayoutCount = 1,
.bindGroupLayouts = &bind_group_layouts.models,
});
ASSERT(pipeline_layouts.models != NULL);
}
/* Bind group layout for bloom filter & G-Buffer composition */
{
WGPUBindGroupLayoutEntry bgl_entries[5] = {
[0] = (WGPUBindGroupLayoutEntry) {
/* Binding 0: Fragment shader image view */
.binding = 0,
.visibility = WGPUShaderStage_Fragment,
.texture = (WGPUTextureBindingLayout) {
.sampleType = WGPUTextureSampleType_Float,
.viewDimension = WGPUTextureViewDimension_2D,
.multisampled = false,
},
.storageTexture = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
/* Binding 1: Fragment shader image sampler */
.binding = 1,
.visibility = WGPUShaderStage_Fragment,
.sampler = (WGPUSamplerBindingLayout){
.type = WGPUSamplerBindingType_Filtering,
},
.texture = {0},
},
[2] = (WGPUBindGroupLayoutEntry) {
/* Binding 2: Fragment shader image view */
.binding = 2,
.visibility = WGPUShaderStage_Fragment,
.texture = (WGPUTextureBindingLayout) {
.sampleType = WGPUTextureSampleType_Float,
.viewDimension = WGPUTextureViewDimension_2D,
.multisampled = false,
},
.storageTexture = {0},
},
[3] = (WGPUBindGroupLayoutEntry) {
/* Binding 3: Fragment shader image sampler */
.binding = 3,
.visibility = WGPUShaderStage_Fragment,
.sampler = (WGPUSamplerBindingLayout){
.type = WGPUSamplerBindingType_Filtering,
},
.texture = {0},
},
[4] = (WGPUBindGroupLayoutEntry) {
/* Binding 4: fragment shader dynamic uniform buffer */
.binding = 4,
.visibility = WGPUShaderStage_Fragment,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.hasDynamicOffset = true,
.minBindingSize = sizeof(ubo_constants[0].value),
},
.sampler = {0},
},
};
/* Bloom filter pipeline layout */
{
/* Create the bind group layout */
bind_group_layouts.bloom_filter = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device, &(WGPUBindGroupLayoutDescriptor){
.label = "Bloom filter - Bind group layout",
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
});
ASSERT(bind_group_layouts.bloom_filter != NULL);
/* Create the pipeline layout */
pipeline_layouts.bloom_filter = wgpuDeviceCreatePipelineLayout(
wgpu_context->device,
&(WGPUPipelineLayoutDescriptor){
.label = "Bloom filter - Pipeline layout",
.bindGroupLayoutCount = 1,
.bindGroupLayouts = &bind_group_layouts.bloom_filter,
});
ASSERT(pipeline_layouts.bloom_filter != NULL);
}
/* G-Buffer composition */
{
/* Create the bind group layout */
bind_group_layouts.composition = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device,
&(WGPUBindGroupLayoutDescriptor){
.label = "G-Buffer composition - Bind group layout",
.entryCount = 4u,
.entries = bgl_entries,
});
ASSERT(bind_group_layouts.composition != NULL);
/* Create the pipeline layout */
pipeline_layouts.composition = wgpuDeviceCreatePipelineLayout(
wgpu_context->device,
&(WGPUPipelineLayoutDescriptor){
.label = "G-Buffer composition - Pipeline layout",
.bindGroupLayoutCount = 1,
.bindGroupLayouts = &bind_group_layouts.composition,
});
ASSERT(pipeline_layouts.composition != NULL);
}
}
}
static void setup_bind_groups(wgpu_context_t* wgpu_context)
{
/* Model bind groups */
{
WGPUBindGroupEntry bg_entries[5] = {
[0] = (WGPUBindGroupEntry) {
/* Binding 0: Vertex / fragment shader uniform buffer */
.binding = 0,
.buffer = uniform_buffers.matrices.buffer,
.offset = 0,
.size = uniform_buffers.matrices.size,
},
[1] = (WGPUBindGroupEntry) {
/* Binding 1: Fragment shader image view */
.binding = 1,
.textureView = textures.envmap.view,
},
[2] = (WGPUBindGroupEntry) {
/* Binding 2: Fragment shader image sampler */
.binding = 2,
.sampler = textures.envmap.sampler,
},
[3] = (WGPUBindGroupEntry) {
/* Binding 3: Fragment shader uniform buffer */
.binding = 3,
.buffer = uniform_buffers.params.buffer,
.offset = 0,
.size = uniform_buffers.params.size,
},
[4] = (WGPUBindGroupEntry) {
/* Binding 4: Vertex / fragment shader dynamic uniform buffer */
.binding = 4,
.buffer = uniform_buffers.dynamic.buffer,
.offset = 0,
.size = sizeof(ubo_constants[0].value),
},
};
/* 3D object bind group */
{
WGPUBindGroupDescriptor bg_desc = {
.label = "3D object - Bind group",
.layout = bind_group_layouts.models,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
};
bind_groups.object
= wgpuDeviceCreateBindGroup(wgpu_context->device, &bg_desc);
ASSERT(bind_groups.object != NULL);
}
/* Skybox bind group */
{
WGPUBindGroupDescriptor bg_desc = {
.label = "Skybox - Bind group",
.layout = bind_group_layouts.models,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
};
bind_groups.skybox
= wgpuDeviceCreateBindGroup(wgpu_context->device, &bg_desc);
ASSERT(bind_groups.skybox != NULL);
}
}
/* Bloom filter bind group */
{
WGPUBindGroupEntry bg_entries[5] = {
[0] = (WGPUBindGroupEntry) {
/* Binding 0: Fragment shader image view */
.binding = 0,
.textureView = offscreen_pass.color[0].texture_view
},
[1] = (WGPUBindGroupEntry) {
/* Binding 1: Fragment shader image sampler */
.binding = 1,
.sampler = offscreen_pass.sampler,
},
[2] = (WGPUBindGroupEntry) {
/* Binding 2: Fragment shader image view */
.binding = 2,
.textureView = offscreen_pass.color[1].texture_view
},
[3] = (WGPUBindGroupEntry) {
/* Binding 3: Fragment shader image sampler */
.binding = 3,
.sampler = offscreen_pass.sampler,
},
[4] = (WGPUBindGroupEntry) {
/* Binding 4: fragment shader dynamic uniform buffer */
.binding = 4,
.buffer = uniform_buffers.dynamic.buffer,
.offset = 0,
.size = sizeof(ubo_constants[0].value),
},
};
WGPUBindGroupDescriptor bg_desc = {
.label = "Bloom filter - Bind group",
.layout = bind_group_layouts.bloom_filter,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
};
bind_groups.bloom_filter
= wgpuDeviceCreateBindGroup(wgpu_context->device, &bg_desc);
ASSERT(bind_groups.bloom_filter != NULL);
}
/* Composition bind group */
{
WGPUBindGroupEntry bg_entries[4] = {
[0] = (WGPUBindGroupEntry) {
/* Binding 0: Fragment shader image view */
.binding = 0,
.textureView = offscreen_pass.color[0].texture_view
},
[1] = (WGPUBindGroupEntry) {
/* Binding 1: Fragment shader image sampler */
.binding = 1,
.sampler = offscreen_pass.sampler,
},
[2] = (WGPUBindGroupEntry) {
/* Binding 2: Fragment shader image view */
.binding = 2,
.textureView = filter_pass.color[0].texture_view
},
[3] = (WGPUBindGroupEntry) {
/*Binding 3: Fragment shader image sampler */
.binding = 3,
.sampler = filter_pass.sampler,
},
};
WGPUBindGroupDescriptor bg_desc = {
.label = "Composition - Bind group",
.layout = bind_group_layouts.composition,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
};
bind_groups.composition
= wgpuDeviceCreateBindGroup(wgpu_context->device, &bg_desc);
ASSERT(bind_groups.composition != NULL);
}
}
static void setup_render_pass(wgpu_context_t* wgpu_context)
{
/* Color attachment */
rp_color_att_descriptors[0] = (WGPURenderPassColorAttachment) {
.view = NULL, /* Assigned later */
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = 0.025f,
.g = 0.025f,
.b = 0.025f,
.a = 1.000f,
},
};
/* Depth attachment */
wgpu_setup_deph_stencil(wgpu_context, NULL);
/* Render pass descriptor */
render_pass_desc = (WGPURenderPassDescriptor){
.label = "Render pass descriptor",
.colorAttachmentCount = (uint32_t)ARRAY_SIZE(rp_color_att_descriptors),
.colorAttachments = rp_color_att_descriptors,
.depthStencilAttachment = &wgpu_context->depth_stencil.att_desc,
};
}
static void prepare_pipelines(wgpu_context_t* wgpu_context)
{
/* Primitive state */
WGPUPrimitiveState primitive_state_desc = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_Back,
};
/* Depth stencil state */
WGPUDepthStencilState depth_stencil_state_desc
= wgpu_create_depth_stencil_state(&(create_depth_stencil_state_desc_t){
.format = WGPUTextureFormat_Depth24PlusStencil8,
.depth_write_enabled = true,
});
/* Multisample state */
WGPUMultisampleState multisample_state_desc
= wgpu_create_multisample_state_descriptor(
&(create_multisample_state_desc_t){
.sample_count = 1,
});
/* Full screen pipelines */
/* Final fullscreen composition pass pipeline */
{
WGPUBlendState blend_state = wgpu_create_blend_state(false);
WGPUColorTargetState color_target_state_desc = (WGPUColorTargetState){
.format = wgpu_context->swap_chain.format,
.blend = &blend_state,
.writeMask = WGPUColorWriteMask_All,
};
/* Vertex state */
WGPUVertexState vertex_state_desc = wgpu_create_vertex_state(
wgpu_context, &(wgpu_vertex_state_t){
.shader_desc = (wgpu_shader_desc_t){
/* Vertex shader SPIR-V */
.label = "Composition - Vertex shader SPIR-V",
.file = "shaders/hdr/composition.vert.spv",
},
/* Empty vertex input state, full screen triangles are generated by the vertex shader */
.buffer_count = 0,
.buffers = NULL,
});
/* Fragment state */
WGPUFragmentState fragment_state_desc = wgpu_create_fragment_state(
wgpu_context, &(wgpu_fragment_state_t){
.shader_desc = (wgpu_shader_desc_t){
/* Fragment shader SPIR-V */
.label = "Composition - Fragment shader SPIR-V",
.file = "shaders/hdr/composition.frag.spv",
},
.target_count = 1,
.targets = &color_target_state_desc,
});
/* Create rendering pipeline using the specified states */
pipelines.composition = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Composition - Render pipeline",
.layout = pipeline_layouts.composition,
.primitive = primitive_state_desc,
.vertex = vertex_state_desc,
.fragment = &fragment_state_desc,
.depthStencil = &depth_stencil_state_desc,
.multisample = multisample_state_desc,
});
ASSERT(pipelines.composition != NULL);
/* Partial cleanup */
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state_desc.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state_desc.module);
}
/* Bloom pass */
{
/* Additive blending */
WGPUBlendState blend_state_radial_blur = {
.color.operation = WGPUBlendOperation_Add,
.color.srcFactor = WGPUBlendFactor_One,
.color.dstFactor = WGPUBlendFactor_One,
.alpha.operation = WGPUBlendOperation_Add,
.alpha.srcFactor = WGPUBlendFactor_SrcAlpha,
.alpha.dstFactor = WGPUBlendFactor_DstAlpha,
};
WGPUColorTargetState color_target_state_desc = (WGPUColorTargetState){
.blend = &blend_state_radial_blur,
.writeMask = WGPUColorWriteMask_All,
};
/* Vertex state */
WGPUVertexState vertex_state_desc = wgpu_create_vertex_state(
wgpu_context, &(wgpu_vertex_state_t){
.shader_desc = (wgpu_shader_desc_t){
/* Vertex shader SPIR-V */
.label = "Bloom - Vertex shader SPIR-V",
.file = "shaders/hdr/bloom.vert.spv",
},
/* Empty vertex input state, full screen triangles are generated by the vertex shader */
.buffer_count = 0,
.buffers = NULL,
});
/* Fragment state */
WGPUFragmentState fragment_state_desc = wgpu_create_fragment_state(
wgpu_context, &(wgpu_fragment_state_t){
.shader_desc = (wgpu_shader_desc_t){
/* Fragment shader SPIR-V */
.label = "Bloom - Fragment shader SPIR-V",
.file = "shaders/hdr/bloom.frag.spv",
},
.target_count = 1,
.targets = &color_target_state_desc,
});
/* First bloom filter pass (into separate framebuffer) */
color_target_state_desc.format = wgpu_context->swap_chain.format;
pipelines.bloom[0] = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Bloom 1 - Render pipeline",
.layout = pipeline_layouts.bloom_filter,
.primitive = primitive_state_desc,
.vertex = vertex_state_desc,
.fragment = &fragment_state_desc,
.depthStencil = &depth_stencil_state_desc,
.multisample = multisample_state_desc,
});
ASSERT(pipelines.bloom[0] != NULL);
/* Second bloom filter pass (into separate framebuffer) */
color_target_state_desc.format = filter_pass.color[0].format;
pipelines.bloom[1] = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Bloom 2 - render pipeline",
.layout = pipeline_layouts.bloom_filter,
.primitive = primitive_state_desc,
.vertex = vertex_state_desc,
.fragment = &fragment_state_desc,
.depthStencil = NULL,
.multisample = multisample_state_desc,
});
ASSERT(pipelines.bloom[1] != NULL);
}
/* Object rendering pipelines */
{
/* Use vertex input state from glTF model setup */
WGPU_GLTF_VERTEX_BUFFER_LAYOUT(
gltf_model,
/* Location 0: Position */
WGPU_GLTF_VERTATTR_DESC(0, WGPU_GLTF_VertexComponent_Position),
/* Location 1: Vertex normal */
WGPU_GLTF_VERTATTR_DESC(1, WGPU_GLTF_VertexComponent_Normal));
/* Color target state */
WGPUBlendState blend_state = wgpu_create_blend_state(false);
WGPUColorTargetState color_target_state_desc[2] = {
[0] = (WGPUColorTargetState){
.format = offscreen_pass.color[0].format,
.blend = &blend_state,
.writeMask = WGPUColorWriteMask_All,
},
[1] = (WGPUColorTargetState){
.format = offscreen_pass.color[1].format,
.blend = &blend_state,
.writeMask = WGPUColorWriteMask_All,
},
};
/* Vertex state */
WGPUVertexState vertex_state_desc = wgpu_create_vertex_state(
wgpu_context, &(wgpu_vertex_state_t){
.shader_desc = (wgpu_shader_desc_t){
/* Vertex shader SPIR-V */
.label = "G-Buffer - Vertex shader SPIR-V",
.file = "shaders/hdr/gbuffer.vert.spv",
},
.buffer_count = 1,
.buffers = &gltf_model_vertex_buffer_layout,
});
/* Fragment state */
WGPUFragmentState fragment_state_desc = wgpu_create_fragment_state(
wgpu_context, &(wgpu_fragment_state_t){
.shader_desc = (wgpu_shader_desc_t){
/* Fragment shader SPIR-V */
.label = "G-Buffer - Fragment shader SPIR-V",
.file = "shaders/hdr/gbuffer.frag.spv",
},
.target_count = 2,
.targets = color_target_state_desc,
});
/* Skybox pipeline (background cube) */
{
primitive_state_desc.cullMode = WGPUCullMode_Back;
depth_stencil_state_desc.depthWriteEnabled = false;
/* Create rendering pipeline using the specified */
pipelines.skybox = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Skybox - Render pipeline",
.layout = pipeline_layouts.models,
.primitive = primitive_state_desc,
.vertex = vertex_state_desc,
.fragment = &fragment_state_desc,
.depthStencil = &depth_stencil_state_desc,
.multisample = multisample_state_desc,
});
ASSERT(pipelines.skybox);
}
/* Object rendering pipeline */
{
/* Enable depth write */
depth_stencil_state_desc.depthWriteEnabled = true;
/* Flip cull mode */
primitive_state_desc.cullMode = WGPUCullMode_Front;
/* Create rendering pipeline using the specified states */
pipelines.reflect = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Reflect - Render pipeline",
.layout = pipeline_layouts.models,
.primitive = primitive_state_desc,
.vertex = vertex_state_desc,
.fragment = &fragment_state_desc,
.depthStencil = &depth_stencil_state_desc,
.multisample = multisample_state_desc,
});
ASSERT(pipelines.reflect);
}
/* Partial cleanup */
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state_desc.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state_desc.module);
}
}
static void update_uniform_buffers(wgpu_example_context_t* context)
{
camera_t* camera = context->camera;
glm_mat4_copy(camera->matrices.perspective, ubo_matrices.projection);
glm_mat4_copy(camera->matrices.view, ubo_matrices.model_view);
glm_mat4_inv(camera->matrices.view, ubo_matrices.inverse_modelview);
wgpu_queue_write_buffer(context->wgpu_context,
uniform_buffers.matrices.buffer, 0, &ubo_matrices,
uniform_buffers.matrices.size);
}
static void update_params(wgpu_example_context_t* context)
{
wgpu_queue_write_buffer(context->wgpu_context, uniform_buffers.params.buffer,
0, &ubo_params, uniform_buffers.params.size);
}
static void update_dynamic_uniform_buffers(wgpu_example_context_t* context)
{