-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpbr_basic.c
832 lines (732 loc) · 29 KB
/
pbr_basic.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
#include "example_base.h"
#include <string.h>
#include "../webgpu/gltf_model.h"
#include "../webgpu/imgui_overlay.h"
/* -------------------------------------------------------------------------- *
* WebGPU Example - Physical Based Shading Basics
*
* Demonstrates a basic specular BRDF implementation with solid materials and
* fixed light sources on a grid of objects with varying material parameters,
* demonstrating how metallic reflectance and surface roughness affect the
* appearance of pbr lit objects.
*
* Ref:
* https://github.com/SaschaWillems/Vulkan/blob/master/examples/pbrbasic/pbrbasic.cpp
* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
static const char* pbr_vertex_shader_wgsl;
static const char* pbr_fragment_shader_wgsl;
/* -------------------------------------------------------------------------- *
* Physical Based Shading Basics example
* -------------------------------------------------------------------------- */
#define GRID_DIM 7u
#define ALIGNMENT 256u // 256-byte alignment
static struct {
const char* name;
// Parameter block used as uniforms block
struct {
float roughness;
float metallic;
vec3 color;
} params;
} materials[11] = {
// clang-format off
// Setup some default materials (source:
// https://seblagarde.wordpress.com/2011/08/17/feeding-a-physical-based-lighting-mode/)
{ .name = "Gold", . params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 1.000000f, 0.765557f, 0.336057f } } },
{ .name = "Copper", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 0.955008f, 0.637427f, 0.538163f } } },
{ .name = "Chromium", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 0.549585f, 0.556114f, 0.554256f } } },
{ .name = "Nickel", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 0.659777f, 0.608679f, 0.525649f } } },
{ .name = "Titanium", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 0.541931f, 0.496791f, 0.449419f } } },
{ .name = "Cobalt", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 0.662124f, 0.654864f, 0.633732f } } },
{ .name = "Platinum", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 0.672411f, 0.637331f, 0.585456f } } },
// Testing materials
{ .name = "White", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 1.0f, 1.0f, 1.0f } } },
{ .name = "Red", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 1.0f, 0.0f, 0.0f } } },
{ .name = "Blue", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 0.0f, 0.0f, 1.0f } } },
{ .name = "Black", .params = { .roughness = 0.1f, .metallic = 1.0f, .color = { 0.0f, 0.0f, 0.0f } } },
// clang-format on
};
static struct {
const char* name;
const char* filelocation;
struct gltf_model_t* object;
} models[4] = {
// 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
};
// Arrays used for GUI
static const char* material_names[11] = {
// Default materials
"Gold", "Copper", "Chromium", "Nickel", "Titanium", "Cobalt", "Platinum", //
// Testing materials
"White", "Red", "Blue", "Black", //
};
static const char* object_names[4] = {"Sphere", "Teapot", "Torusknot", "Venus"};
static int32_t current_material_index = 0;
static int32_t current_object_index = 0;
static struct {
// Object vertex shader uniform buffer
wgpu_buffer_t ubo_matrices;
// Shared parameter uniform buffer
wgpu_buffer_t ubo_params;
// Material parameter uniform buffer
struct {
WGPUBuffer buffer;
uint64_t buffer_size;
uint64_t model_size;
} material_params;
// Object parameter uniform buffer
struct {
WGPUBuffer buffer;
uint64_t buffer_size;
uint64_t model_size;
} object_params;
} uniform_buffers = {0};
static struct {
mat4 projection;
mat4 model;
mat4 view;
vec3 cam_pos;
} ubo_matrices = {0};
static struct {
vec4 lights[4];
} ubo_params = {0};
static struct matrial_params_dynamic_t {
float roughness;
float metallic;
vec3 color;
uint8_t padding[236];
} material_params_dynamic[GRID_DIM * GRID_DIM] = {0};
static struct object_params_dynamic_t {
vec3 position;
uint8_t padding[244];
} object_params_dynamic[GRID_DIM * GRID_DIM] = {0};
static WGPURenderPassColorAttachment rp_color_att_descriptors[1] = {0};
static WGPURenderPassDescriptor render_pass_desc = {0};
static WGPUPipelineLayout pipeline_layout = NULL;
static WGPURenderPipeline pipeline = NULL;
static WGPUBindGroupLayout bind_group_layout = NULL;
static WGPUBindGroup bind_group = NULL;
// Other variables
static const char* example_title = "Physical Based Shading Basics";
static bool prepared = false;
static void setup_camera(wgpu_example_context_t* context)
{
context->timer_speed *= 0.5f;
context->camera = camera_create();
context->camera->type = CameraType_FirstPerson;
camera_set_position(context->camera, (vec3){10.0f, 13.0f, 1.8f});
camera_set_rotation(context->camera, (vec3){62.5f, 90.0f, 0.0f});
camera_set_movement_speed(context->camera, 4.0f);
camera_set_perspective(context->camera, 60.0f,
context->window_size.aspect_ratio, 0.1f, 256.0f);
camera_set_rotation_speed(context->camera, 0.25f);
context->paused = true;
context->timer_speed *= 0.25f;
}
static void load_assets(wgpu_context_t* wgpu_context)
{
const uint32_t gltf_loading_flags
= WGPU_GLTF_FileLoadingFlags_PreTransformVertices
| WGPU_GLTF_FileLoadingFlags_DontLoadImages;
for (uint8_t i = 0; i < (uint8_t)ARRAY_SIZE(models); ++i) {
models[i].object
= wgpu_gltf_model_load_from_file(&(wgpu_gltf_model_load_options_t){
.wgpu_context = wgpu_context,
.filename = models[i].filelocation,
.file_loading_flags = gltf_loading_flags,
});
}
}
static void setup_pipeline_layout(wgpu_context_t* wgpu_context)
{
/* Bind group layout */
WGPUBindGroupLayoutEntry bgl_entries[4] = {
[0] = (WGPUBindGroupLayoutEntry) {
/* Binding 0: Uniform buffer (Vertex shader & Fragment shader) */
.binding = 0,
.visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = uniform_buffers.ubo_matrices.size,
},
.sampler = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
/* Binding 1: Uniform buffer (Fragment shader) */
.binding = 1,
.visibility = WGPUShaderStage_Fragment,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = uniform_buffers.ubo_params.size,
},
.sampler = {0},
},
[2] = (WGPUBindGroupLayoutEntry) {
/* Binding 2: Dynamic uniform buffer (Fragment shader) */
.binding = 2,
.visibility = WGPUShaderStage_Fragment,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.hasDynamicOffset = true,
.minBindingSize = uniform_buffers.material_params.model_size,
},
.sampler = {0},
},
[3] = (WGPUBindGroupLayoutEntry) {
/* Binding 3: Dynamic uniform buffer (Vertex shader) */
.binding = 3,
.visibility = WGPUShaderStage_Vertex,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.hasDynamicOffset = true,
.minBindingSize = uniform_buffers.object_params.model_size,
},
.sampler = {0},
},
};
bind_group_layout = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device, &(WGPUBindGroupLayoutDescriptor){
.label = "Bind group layout",
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
});
ASSERT(bind_group_layout != NULL);
/* Create the pipeline layout */
pipeline_layout = wgpuDeviceCreatePipelineLayout(
wgpu_context->device, &(WGPUPipelineLayoutDescriptor){
.label = "Pipeline layout",
.bindGroupLayoutCount = 1,
.bindGroupLayouts = &bind_group_layout,
});
ASSERT(pipeline_layout != NULL);
}
static void setup_bind_group(wgpu_context_t* wgpu_context)
{
// Bind Group
WGPUBindGroupEntry bg_entries[4] = {
[0] = (WGPUBindGroupEntry) {
// Binding 0: Uniform buffer (Vertex shader & Fragment shader)
.binding = 0,
.buffer = uniform_buffers.ubo_matrices.buffer,
.offset = 0,
.size = uniform_buffers.ubo_matrices.size,
},
[1] = (WGPUBindGroupEntry) {
// Binding 1: Uniform buffer (Fragment shader)
.binding = 1,
.buffer = uniform_buffers.ubo_params.buffer,
.offset = 0,
.size = uniform_buffers.ubo_params.size,
},
[2] = (WGPUBindGroupEntry) {
// Binding 2: Dynamic uniform buffer (Fragment shader)
.binding = 2,
.buffer = uniform_buffers.material_params.buffer,
.offset = 0,
.size = uniform_buffers.material_params.model_size,
},
[3] = (WGPUBindGroupEntry) {
// Binding 3: Dynamic uniform buffer (Vertex shader)
.binding = 3,
.buffer = uniform_buffers.object_params.buffer,
.offset = 0,
.size = uniform_buffers.object_params.model_size,
},
};
bind_group = wgpuDeviceCreateBindGroup(
wgpu_context->device, &(WGPUBindGroupDescriptor){
.label = "Bind group",
.layout = bind_group_layout,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
});
ASSERT(bind_group != 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.0f,
.g = 0.0f,
.b = 0.0f,
.a = 1.0f,
},
};
/* Depth attachment */
wgpu_setup_deph_stencil(wgpu_context, NULL);
/* Render pass descriptor */
render_pass_desc = (WGPURenderPassDescriptor){
.label = "Render pass descriptor",
.colorAttachmentCount = 1,
.colorAttachments = rp_color_att_descriptors,
.depthStencilAttachment = &wgpu_context->depth_stencil.att_desc,
};
}
static void prepare_pipelines(wgpu_context_t* wgpu_context)
{
// Construct the different states making up the pipeline
// 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,
};
// Depth stencil state
WGPUDepthStencilState depth_stencil_state
= wgpu_create_depth_stencil_state(&(create_depth_stencil_state_desc_t){
.format = WGPUTextureFormat_Depth24PlusStencil8,
.depth_write_enabled = true,
});
// Vertex buffer layout
WGPU_GLTF_VERTEX_BUFFER_LAYOUT(
sphere,
// 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));
// Shaders - PBR pipeline
// 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 = "PBR Basic - Vertex shader WGSL",
.wgsl_code.source = pbr_vertex_shader_wgsl,
.entry = "main",
},
.buffer_count = 1,
.buffers = &sphere_vertex_buffer_layout,
});
// Fragment state
WGPUFragmentState fragment_state = wgpu_create_fragment_state(
wgpu_context, &(wgpu_fragment_state_t){
.shader_desc = (wgpu_shader_desc_t){
// Vertex shader WGSL
.label = "PBR Basic - Fragment shader WGSL",
.wgsl_code.source = pbr_fragment_shader_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
pipeline = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "PBR Basic - Render pipeline",
.layout = pipeline_layout,
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.depthStencil = &depth_stencil_state,
.multisample = multisample_state,
});
ASSERT(pipeline != NULL);
// Partial cleanup
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static void update_uniform_buffers(wgpu_example_context_t* context)
{
// 3D object
camera_t* camera = context->camera;
glm_mat4_copy(camera->matrices.perspective, ubo_matrices.projection);
glm_mat4_copy(camera->matrices.view, ubo_matrices.view);
glm_mat4_identity(ubo_matrices.model);
glm_rotate(ubo_matrices.model,
glm_rad(-90.0f + (current_object_index == 1 ? 45.0f : 0.0f)),
(vec3){0.0f, 1.0f, 0.0f});
glm_vec3_scale(camera->position, -1.0f, ubo_matrices.cam_pos);
wgpu_queue_write_buffer(context->wgpu_context,
uniform_buffers.ubo_matrices.buffer, 0, &ubo_matrices,
uniform_buffers.ubo_matrices.size);
}
static void update_dynamic_uniform_buffer(wgpu_context_t* wgpu_context)
{
/* Set objects positions and material properties */
uint32_t index = 0;
for (uint32_t y = 0; y < GRID_DIM; y++) {
for (uint32_t x = 0; x < GRID_DIM; x++) {
/* Set object position */
vec3* pos = &object_params_dynamic[index].position;
glm_vec3_copy((vec3){(float)(x - (GRID_DIM / 2.0f)) * 2.5f, 0.0f,
(float)(y - (GRID_DIM / 2.0f)) * 2.5f},
*pos);
/* Set material metallic and roughness properties */
struct matrial_params_dynamic_t* mat_params
= &material_params_dynamic[index];
mat_params->metallic
= glm_clamp((float)x / (float)(GRID_DIM - 1), 0.1f, 1.0f);
mat_params->roughness
= glm_clamp((float)y / (float)(GRID_DIM - 1), 0.05f, 1.0f);
glm_vec3_copy(materials[current_material_index].params.color,
(*mat_params).color);
++index;
}
}
/* Update buffers */
wgpu_queue_write_buffer(wgpu_context, uniform_buffers.object_params.buffer, 0,
&object_params_dynamic,
uniform_buffers.object_params.buffer_size);
wgpu_queue_write_buffer(wgpu_context, uniform_buffers.material_params.buffer,
0, &material_params_dynamic,
uniform_buffers.material_params.buffer_size);
}
static void update_lights(wgpu_example_context_t* context)
{
const float p = 15.0f;
// clang-format off
glm_vec4_copy((vec4){-p, -p * 0.5f, -p, 1.0f}, ubo_params.lights[0]);
glm_vec4_copy((vec4){-p, -p * 0.5f, p, 1.0f}, ubo_params.lights[1]);
glm_vec4_copy((vec4){ p, -p * 0.5f, p, 1.0f}, ubo_params.lights[2]);
glm_vec4_copy((vec4){ p, -p * 0.5f, -p, 1.0f}, ubo_params.lights[3]);
// clang-format on
if (!context->paused) {
float timer = context->timer;
ubo_params.lights[0][0] = sin(glm_rad(timer * 360.0f)) * 20.0f;
ubo_params.lights[0][2] = cos(glm_rad(timer * 360.0f)) * 20.0f;
ubo_params.lights[1][0] = cos(glm_rad(timer * 360.0f)) * 20.0f;
ubo_params.lights[1][1] = sin(glm_rad(timer * 360.0f)) * 20.0f;
}
wgpu_queue_write_buffer(context->wgpu_context,
uniform_buffers.ubo_params.buffer, 0, &ubo_params,
uniform_buffers.ubo_params.size);
}
/* Prepare and initialize uniform buffer containing shader uniforms */
static void prepare_uniform_buffers(wgpu_example_context_t* context)
{
/* Object vertex shader uniform buffer */
uniform_buffers.ubo_matrices = wgpu_create_buffer(
context->wgpu_context,
&(wgpu_buffer_desc_t){
.label = "Object vertex shader - Uniform buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = sizeof(ubo_matrices),
});
/* Shared parameters uniform buffer */
uniform_buffers.ubo_params = wgpu_create_buffer(
context->wgpu_context,
&(wgpu_buffer_desc_t){
.label = "Shared parameters - Uniform buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = sizeof(ubo_params),
});
/* Material parameter uniform buffer */
{
uniform_buffers.material_params.model_size = sizeof(vec2) + sizeof(vec3);
uniform_buffers.material_params.buffer_size
= calc_constant_buffer_byte_size(sizeof(material_params_dynamic));
WGPUBufferDescriptor ubo_desc = {
.label = "Material parameter - Uniform buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = uniform_buffers.material_params.buffer_size,
.mappedAtCreation = false,
};
uniform_buffers.material_params.buffer
= wgpuDeviceCreateBuffer(context->wgpu_context->device, &ubo_desc);
ASSERT(uniform_buffers.material_params.buffer != NULL);
}
/* Object parameter uniform buffer */
{
uniform_buffers.object_params.model_size = sizeof(vec4);
uniform_buffers.object_params.buffer_size
= calc_constant_buffer_byte_size(sizeof(object_params_dynamic));
WGPUBufferDescriptor ubo_desc = {
.label = "Object parameter - Uniform buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = uniform_buffers.object_params.buffer_size,
.mappedAtCreation = false,
};
uniform_buffers.object_params.buffer
= wgpuDeviceCreateBuffer(context->wgpu_context->device, &ubo_desc);
ASSERT(uniform_buffers.object_params.buffer != NULL);
}
update_uniform_buffers(context);
update_dynamic_uniform_buffer(context->wgpu_context);
update_lights(context);
}
static int example_initialize(wgpu_example_context_t* context)
{
if (context) {
setup_camera(context);
load_assets(context->wgpu_context);
prepare_uniform_buffers(context);
setup_pipeline_layout(context->wgpu_context);
prepare_pipelines(context->wgpu_context);
setup_bind_group(context->wgpu_context);
setup_render_pass(context->wgpu_context);
prepared = true;
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
static void example_on_update_ui_overlay(wgpu_example_context_t* context)
{
if (imgui_overlay_header("Settings")) {
imgui_overlay_checkBox(context->imgui_overlay, "Paused", &context->paused);
if (imgui_overlay_combo_box(context->imgui_overlay, "Material",
¤t_material_index, material_names, 11)) {
update_dynamic_uniform_buffer(context->wgpu_context);
}
if (imgui_overlay_combo_box(context->imgui_overlay, "Object Type",
¤t_object_index, object_names, 4)) {
update_dynamic_uniform_buffer(context->wgpu_context);
}
}
}
static WGPUCommandBuffer build_command_buffer(wgpu_context_t* wgpu_context)
{
/* Set target frame buffer */
rp_color_att_descriptors[0].view = wgpu_context->swap_chain.frame_buffer;
/* Create command encoder */
wgpu_context->cmd_enc
= wgpuDeviceCreateCommandEncoder(wgpu_context->device, NULL);
/* Create render pass encoder for encoding drawing commands */
wgpu_context->rpass_enc = wgpuCommandEncoderBeginRenderPass(
wgpu_context->cmd_enc, &render_pass_desc);
/* Set viewport */
wgpuRenderPassEncoderSetViewport(
wgpu_context->rpass_enc, 0.0f, 0.0f, (float)wgpu_context->surface.width,
(float)wgpu_context->surface.height, 0.0f, 1.0f);
/* Set scissor rectangle */
wgpuRenderPassEncoderSetScissorRect(wgpu_context->rpass_enc, 0u, 0u,
wgpu_context->surface.width,
wgpu_context->surface.height);
/* Bind the rendering pipeline */
wgpuRenderPassEncoderSetPipeline(wgpu_context->rpass_enc, pipeline);
for (uint32_t i = 0; i < GRID_DIM * GRID_DIM; ++i) {
uint32_t dynamic_offset = i * (uint32_t)ALIGNMENT;
uint32_t dynamic_offsets[2] = {dynamic_offset, dynamic_offset};
/* Bind the bind group for rendering a mesh using the dynamic offset */
wgpuRenderPassEncoderSetBindGroup(wgpu_context->rpass_enc, 0, bind_group, 2,
dynamic_offsets);
/* Draw object */
wgpu_gltf_model_draw(models[current_object_index].object,
(wgpu_gltf_model_render_options_t){0});
}
/* End render pass */
wgpuRenderPassEncoderEnd(wgpu_context->rpass_enc);
WGPU_RELEASE_RESOURCE(RenderPassEncoder, wgpu_context->rpass_enc)
/* Draw ui overlay */
draw_ui(wgpu_context->context, example_on_update_ui_overlay);
/* Get command buffer */
WGPUCommandBuffer command_buffer
= wgpu_get_command_buffer(wgpu_context->cmd_enc);
WGPU_RELEASE_RESOURCE(CommandEncoder, wgpu_context->cmd_enc)
return command_buffer;
}
static int example_draw(wgpu_example_context_t* context)
{
/* Prepare frame */
prepare_frame(context);
/* Command buffer to be submitted to the queue */
wgpu_context_t* wgpu_context = context->wgpu_context;
wgpu_context->submit_info.command_buffer_count = 1;
wgpu_context->submit_info.command_buffers[0]
= build_command_buffer(context->wgpu_context);
/* Submit to queue */
submit_command_buffers(context);
/* Submit frame */
submit_frame(context);
return EXIT_SUCCESS;
}
static int example_render(wgpu_example_context_t* context)
{
if (!prepared) {
return EXIT_FAILURE;
}
const int draw_result = example_draw(context);
if (!context->paused) {
update_lights(context);
}
return draw_result;
}
static void example_on_view_changed(wgpu_example_context_t* context)
{
update_uniform_buffers(context);
}
static void example_destroy(wgpu_example_context_t* context)
{
camera_release(context->camera);
for (uint8_t i = 0; i < (uint8_t)ARRAY_SIZE(models); ++i) {
wgpu_gltf_model_destroy(models[i].object);
}
WGPU_RELEASE_RESOURCE(Buffer, uniform_buffers.ubo_matrices.buffer)
WGPU_RELEASE_RESOURCE(Buffer, uniform_buffers.ubo_params.buffer)
WGPU_RELEASE_RESOURCE(Buffer, uniform_buffers.material_params.buffer)
WGPU_RELEASE_RESOURCE(Buffer, uniform_buffers.object_params.buffer)
WGPU_RELEASE_RESOURCE(PipelineLayout, pipeline_layout)
WGPU_RELEASE_RESOURCE(RenderPipeline, pipeline)
WGPU_RELEASE_RESOURCE(BindGroupLayout, bind_group_layout)
WGPU_RELEASE_RESOURCE(BindGroup, bind_group)
}
void example_pbr_basic(int argc, char* argv[])
{
// clang-format off
example_run(argc, argv, &(refexport_t){
.example_settings = (wgpu_example_settings_t){
.title = example_title,
.overlay = true,
.vsync = true,
},
.example_initialize_func = &example_initialize,
.example_render_func = &example_render,
.example_destroy_func = &example_destroy,
.example_on_view_changed_func = &example_on_view_changed,
});
// clang-format on
}
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
// clang-format off
static const char* pbr_vertex_shader_wgsl = CODE(
struct UBO {
projection : mat4x4<f32>,
model : mat4x4<f32>,
view : mat4x4<f32>,
camPos : vec3<f32>,
};
struct ObjectParams {
objPos : vec3<f32>,
};
@group(0) @binding(0) var<uniform> ubo : UBO;
@group(0) @binding(3) var<uniform> object : ObjectParams;
struct Output {
@builtin(position) position : vec4<f32>,
@location(0) outWorldPos : vec3<f32>,
@location(1) outNormal : vec3<f32>,
};
@vertex
fn main(
@location(0) inPos: vec3<f32>,
@location(1) inNormal: vec3<f32>
) -> Output {
var output: Output;
let locPos : vec3<f32> = (ubo.model * vec4<f32>(inPos, 1.0)).xyz;
output.outWorldPos = locPos + object.objPos;
output.outNormal = normalize((ubo.model * vec4<f32>(inNormal, 1.0)).xyz);
output.position = ubo.projection * ubo.view * vec4<f32>(output.outWorldPos, 1.0);
return output;
}
);
static const char* pbr_fragment_shader_wgsl = CODE(
struct UBO {
projection : mat4x4<f32>,
model : mat4x4<f32>,
view : mat4x4<f32>,
camPos : vec3<f32>,
};
const LIGHTS_ARRAY_LENGTH = 4;
struct UBOShared {
lights : array<vec4<f32>, LIGHTS_ARRAY_LENGTH>,
};
struct MaterialParams {
roughness : f32,
metallic : f32,
r : f32,
g : f32,
b : f32,
};
@group(0) @binding(0) var<uniform> ubo : UBO;
@group(0) @binding(1) var<uniform> uboParams : UBOShared;
@group(0) @binding(2) var<uniform> material : MaterialParams;
const PI = 3.14159265359;
fn materialcolor() -> vec3f {
return vec3<f32>(material.r, material.g, material.b);
}
// Normal Distribution function ----------------------------------------------
fn D_GGX(dotNH : f32, roughness : f32) -> f32 {
let alpha : f32 = roughness * roughness;
let alpha2 : f32 = alpha * alpha;
let denom : f32 = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
return (alpha2)/(PI * denom*denom);
}
// Geometric Shadowing function ----------------------------------------------
fn G_SchlicksmithGGX(dotNL : f32, dotNV : f32, roughness : f32) -> f32 {
let r : f32 = (roughness + 1.0);
let k : f32 = (r*r) / 8.0;
let GL : f32 = dotNL / (dotNL * (1.0 - k) + k);
let GV : f32 = dotNV / (dotNV * (1.0 - k) + k);
return GL * GV;
}
// Fresnel function ----------------------------------------------------------
fn F_Schlick(cosTheta : f32, metallic : f32) -> vec3f {
let F0 : vec3<f32> = mix(vec3(0.04), materialcolor(), metallic);
let F : vec3<f32> = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
return F;
}
// Specular BRDF composition -------------------------------------------------
fn BRDF(L : vec3<f32>, V : vec3<f32>, N : vec3<f32>, metallic : f32,
roughness : f32) -> vec3f {
// Precalculate vectors and dot products
let H : vec3<f32> = normalize(V + L);
let dotNV : f32 = clamp(dot(N, V), 0.0, 1.0);
let dotNL : f32 = clamp(dot(N, L), 0.0, 1.0);
let dotLH : f32 = clamp(dot(L, H), 0.0, 1.0);
let dotNH : f32 = clamp(dot(N, H), 0.0, 1.0);
// Light color fixed
let lightColor : vec3<f32> = vec3(1.0);
var color : vec3<f32> = vec3(0.0);
if (dotNL > 0.0) {
let rroughness : f32 = max(0.05, roughness);
// D = Normal distribution (Distribution of the microfacets)
let D : f32 = D_GGX(dotNH, roughness);
// G = Geometric shadowing term (Microfacets shadowing)
let G : f32 = G_SchlicksmithGGX(dotNL, dotNV, roughness);
// F = Fresnel factor (Reflectance depending on angle of incidence)
let F : vec3<f32> = F_Schlick(dotNV, metallic);
let spec = D * F * G / (4.0 * dotNL * dotNV);
color += spec * dotNL * lightColor;
}
return color;
}
// Main ----------------------------------------------------------------------
@fragment
fn main(
@location(0) inWorldPos: vec3<f32>,
@location(1) inNormal: vec3<f32>
) -> @location(0) vec4<f32> {
let N : vec3<f32> = normalize(inNormal);
let V : vec3<f32> = normalize(ubo.camPos - inWorldPos);
let roughness : f32 = material.roughness;
// Specular contribution
var Lo : vec3<f32> = vec3(0.0);
for (var i : i32 = 0; i < LIGHTS_ARRAY_LENGTH; i++) {
let L : vec3<f32> = normalize(uboParams.lights[i].xyz - inWorldPos);
Lo += BRDF(L, V, N, material.metallic, roughness);
};
// Combine with ambient
var color : vec3<f32> = materialcolor() * 0.02;
color += Lo;
// Gamma correct
color = pow(color, vec3<f32>(0.4545));
return vec4<f32>(color, 1.0);
}
);
// clang-format on