-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathpost_processing.c
More file actions
1890 lines (1641 loc) · 66.7 KB
/
post_processing.c
File metadata and controls
1890 lines (1641 loc) · 66.7 KB
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 "webgpu/imgui_overlay.h"
#include "webgpu/wgpu_common.h"
#include <cglm/cglm.h>
#include <string.h>
#define SOKOL_FETCH_IMPL
#include <sokol_fetch.h>
#define SOKOL_LOG_IMPL
#include <sokol_log.h>
#define SOKOL_TIME_IMPL
#include <sokol_time.h>
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#endif
#include <cimgui.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#include "core/image_loader.h"
/* -------------------------------------------------------------------------- *
* WebGPU Example - Post-processing
*
* This example shows how to use a post-processing effect to blend between two
* scenes. Two instanced scenes (cubes and spheres) are rendered to offscreen
* framebuffers, then blended together on a fullscreen quad using a cutoff mask
* texture for transition effects.
*
* Ref:
* https://github.com/gnikoloff/webgpu-dojo/tree/master/src/examples/postprocessing-01
* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
static const char* instanced_vertex_shader_wgsl;
static const char* instanced_fragment_shader_wgsl;
static const char* quad_vertex_shader_wgsl;
static const char* quad_fragment_shader_wgsl;
/* -------------------------------------------------------------------------- *
* Constants
* -------------------------------------------------------------------------- */
#define INSTANCES_COUNT 500u
#define WORLD_SIZE_X 20u
#define WORLD_SIZE_Y 20u
#define WORLD_SIZE_Z 20u
#define PP_EPSILON 0.00001f
/* -------------------------------------------------------------------------- *
* Base transform class to handle vectors and matrices
*
* Ref:
* https://github.com/gnikoloff/hwoa-rang-gl/blob/main/src/core/transform.ts
* -------------------------------------------------------------------------- */
typedef struct {
vec3 position;
vec3 rotation;
vec3 scale;
mat4 model_matrix;
bool should_update;
} transform_t;
static void transform_set_rotation(transform_t* transform, vec3 rotation)
{
glm_vec3_copy(rotation, transform->rotation);
transform->should_update = true;
}
static void transform_update_model_matrix(transform_t* transform)
{
glm_mat4_identity(transform->model_matrix);
glm_translate(transform->model_matrix, transform->position);
glm_rotate(transform->model_matrix, transform->rotation[0],
(vec3){1.0f, 0.0f, 0.0f});
glm_rotate(transform->model_matrix, transform->rotation[1],
(vec3){0.0f, 1.0f, 0.0f});
glm_rotate(transform->model_matrix, transform->rotation[2],
(vec3){0.0f, 0.0f, 1.0f});
glm_scale(transform->model_matrix, transform->scale);
transform->should_update = false;
}
static void transform_init(transform_t* transform)
{
glm_vec3_zero(transform->position);
glm_vec3_zero(transform->rotation);
glm_vec3_one(transform->scale);
transform->should_update = true;
}
/* -------------------------------------------------------------------------- *
* Orthographic Camera
* -------------------------------------------------------------------------- */
typedef struct {
vec3 up;
float left, right, top, bottom, near, far;
vec3 position;
vec3 look_at_position;
mat4 projection_matrix;
mat4 view_matrix;
} orthographic_camera_t;
static void orthographic_camera_update_view_matrix(orthographic_camera_t* cam)
{
glm_lookat(cam->position, cam->look_at_position, cam->up, cam->view_matrix);
}
static void
orthographic_camera_update_projection_matrix(orthographic_camera_t* cam)
{
glm_ortho(cam->left, cam->right, cam->bottom, cam->top, cam->near, cam->far,
cam->projection_matrix);
}
static void orthographic_camera_init(orthographic_camera_t* cam, float left,
float right, float top, float bottom,
float near_val, float far_val)
{
glm_vec3_copy((vec3){0.0f, 1.0f, 0.0f}, cam->up);
cam->left = left;
cam->right = right;
cam->top = top;
cam->bottom = bottom;
cam->near = near_val;
cam->far = far_val;
glm_vec3_zero(cam->position);
glm_vec3_zero(cam->look_at_position);
glm_mat4_zero(cam->projection_matrix);
glm_mat4_zero(cam->view_matrix);
orthographic_camera_update_projection_matrix(cam);
}
/* -------------------------------------------------------------------------- *
* Perspective Camera
* -------------------------------------------------------------------------- */
typedef struct {
vec3 up;
vec3 position;
vec3 look_at_position;
mat4 projection_matrix;
mat4 view_matrix;
float field_of_view;
float aspect;
float near, far;
} perspective_camera_t;
static void perspective_camera_update_view_matrix(perspective_camera_t* cam)
{
glm_lookat(cam->position, cam->look_at_position, cam->up, cam->view_matrix);
}
static void
perspective_camera_update_projection_matrix(perspective_camera_t* cam)
{
glm_perspective(cam->field_of_view, cam->aspect, cam->near, cam->far,
cam->projection_matrix);
}
static void perspective_camera_init(perspective_camera_t* cam, float fov,
float aspect, float near_val, float far_val)
{
glm_vec3_copy((vec3){0.0f, 1.0f, 0.0f}, cam->up);
glm_vec3_zero(cam->position);
glm_vec3_zero(cam->look_at_position);
glm_mat4_zero(cam->projection_matrix);
glm_mat4_zero(cam->view_matrix);
cam->field_of_view = fov;
cam->aspect = aspect;
cam->near = near_val;
cam->far = far_val;
perspective_camera_update_projection_matrix(cam);
}
/* -------------------------------------------------------------------------- *
* Geometry helpers
* -------------------------------------------------------------------------- */
typedef struct {
struct {
float* data;
uint32_t data_size;
uint32_t count;
} positions;
struct {
float* data;
uint32_t data_size;
uint32_t count;
} normals;
struct {
float* data;
uint32_t data_size;
uint32_t count;
} uvs;
struct {
uint32_t* data;
uint32_t data_size;
uint32_t count;
} indices;
} geometry_t;
typedef struct {
float model_matrix_data[INSTANCES_COUNT * 16];
float normal_matrix_data[INSTANCES_COUNT * 16];
} instanced_geometry_t;
static void geometry_destroy(geometry_t* geo)
{
if (geo->positions.data) {
free(geo->positions.data);
geo->positions.data = NULL;
}
if (geo->normals.data) {
free(geo->normals.data);
geo->normals.data = NULL;
}
if (geo->uvs.data) {
free(geo->uvs.data);
geo->uvs.data = NULL;
}
if (geo->indices.data) {
free(geo->indices.data);
geo->indices.data = NULL;
}
}
/* Build a plane face for box/plane geometry */
static void build_plane(float* vertices, float* normal, float* uv,
uint32_t* indices, int32_t width, int32_t height,
int32_t depth, uint32_t w_segs, uint32_t h_segs,
uint32_t u, uint32_t v, uint32_t w, int32_t u_dir,
int32_t v_dir, uint32_t i, uint32_t ii)
{
const uint32_t io = i;
const float seg_w = (float)width / (float)w_segs;
const float seg_h = (float)height / (float)h_segs;
uint32_t a = 0, b = 0, c = 0, d = 0;
float x = 0.0f, y = 0.0f;
for (uint32_t iy = 0; iy <= h_segs; ++iy) {
y = iy * seg_h - height / 2.0f;
for (uint32_t ix = 0; ix <= w_segs; ++ix, ++i) {
x = ix * seg_w - width / 2.0f;
vertices[i * 3 + u] = x * u_dir;
vertices[i * 3 + v] = y * v_dir;
vertices[i * 3 + w] = depth / 2.0f;
normal[i * 3 + u] = 0.0f;
normal[i * 3 + v] = 0.0f;
normal[i * 3 + w] = depth >= 0 ? 1.0f : -1.0f;
uv[i * 2] = (float)ix / (float)w_segs;
uv[i * 2 + 1] = 1.0f - (float)iy / (float)h_segs;
if (iy == h_segs || ix == w_segs) {
continue;
}
a = io + ix + iy * (w_segs + 1);
b = io + ix + (iy + 1) * (w_segs + 1);
c = io + ix + (iy + 1) * (w_segs + 1) + 1;
d = io + ix + iy * (w_segs + 1) + 1;
indices[ii * 6] = a;
indices[ii * 6 + 1] = b;
indices[ii * 6 + 2] = d;
indices[ii * 6 + 3] = b;
indices[ii * 6 + 4] = c;
indices[ii * 6 + 5] = d;
++ii;
}
}
}
/* Generate a plane geometry */
static void create_plane(geometry_t* plane, uint32_t width, uint32_t height,
uint32_t w_segs, uint32_t h_segs)
{
const uint32_t num = (w_segs + 1) * (h_segs + 1);
const uint32_t num_indices = w_segs * h_segs * 6;
plane->positions.count = num;
plane->normals.count = num;
plane->uvs.count = num;
plane->indices.count = num_indices;
plane->positions.data_size = num * 3 * sizeof(float);
plane->normals.data_size = num * 3 * sizeof(float);
plane->uvs.data_size = num * 2 * sizeof(float);
plane->indices.data_size = num_indices * sizeof(uint32_t);
plane->positions.data = (float*)malloc(plane->positions.data_size);
plane->normals.data = (float*)malloc(plane->normals.data_size);
plane->uvs.data = (float*)malloc(plane->uvs.data_size);
plane->indices.data = (uint32_t*)malloc(plane->indices.data_size);
build_plane(plane->positions.data, plane->normals.data, plane->uvs.data,
plane->indices.data, (int32_t)width, (int32_t)height, 0, w_segs,
h_segs, 0, 1, 2, 1, -1, 0, 0);
}
/* Generate a box geometry */
static void create_box(geometry_t* box)
{
const uint32_t w_segs = 1, h_segs = 1, d_segs = 1;
const uint32_t width = 1, height = 1, depth = 1;
const uint32_t num = (w_segs + 1) * (h_segs + 1) * 2
+ (w_segs + 1) * (d_segs + 1) * 2
+ (h_segs + 1) * (d_segs + 1) * 2;
const uint32_t num_indices
= (w_segs * h_segs * 2 + w_segs * d_segs * 2 + h_segs * d_segs * 2) * 6;
box->positions.count = num;
box->normals.count = num;
box->uvs.count = num;
box->indices.count = num_indices;
box->positions.data_size = num * 3 * sizeof(float);
box->normals.data_size = num * 3 * sizeof(float);
box->uvs.data_size = num * 2 * sizeof(float);
box->indices.data_size = num_indices * sizeof(uint32_t);
box->positions.data = (float*)malloc(box->positions.data_size);
box->normals.data = (float*)malloc(box->normals.data_size);
box->uvs.data = (float*)malloc(box->uvs.data_size);
box->indices.data = (uint32_t*)malloc(box->indices.data_size);
uint32_t i = 0, ii = 0;
/* RIGHT */
build_plane(box->positions.data, box->normals.data, box->uvs.data,
box->indices.data, depth, height, width, d_segs, h_segs, 2, 1, 0,
-1, -1, i, ii);
/* LEFT */
i += (d_segs + 1) * (h_segs + 1);
ii += d_segs * h_segs;
build_plane(box->positions.data, box->normals.data, box->uvs.data,
box->indices.data, depth, height, -((int32_t)width), d_segs,
h_segs, 2, 1, 0, 1, -1, i, ii);
/* TOP */
i += (d_segs + 1) * (h_segs + 1);
ii += d_segs * h_segs;
build_plane(box->positions.data, box->normals.data, box->uvs.data,
box->indices.data, width, depth, height, d_segs, h_segs, 0, 2, 1,
1, 1, i, ii);
/* BOTTOM */
i += (w_segs + 1) * (d_segs + 1);
ii += w_segs * d_segs;
build_plane(box->positions.data, box->normals.data, box->uvs.data,
box->indices.data, width, depth, -((int32_t)height), d_segs,
h_segs, 0, 2, 1, 1, -1, i, ii);
/* BACK */
i += (w_segs + 1) * (d_segs + 1);
ii += w_segs * d_segs;
build_plane(box->positions.data, box->normals.data, box->uvs.data,
box->indices.data, width, height, -((int32_t)depth), w_segs,
h_segs, 0, 1, 2, -1, -1, i, ii);
/* FRONT */
i += (w_segs + 1) * (h_segs + 1);
ii += w_segs * h_segs;
build_plane(box->positions.data, box->normals.data, box->uvs.data,
box->indices.data, width, height, depth, w_segs, h_segs, 0, 1, 2,
1, -1, i, ii);
}
/* Generate a sphere geometry */
static void create_sphere(geometry_t* sphere)
{
const float radius = 0.5f;
const uint32_t w_segs = 16u;
const uint32_t h_segs = (uint32_t)ceilf(w_segs * 0.5f);
const float p_start = 0.0f;
const float p_length = PI2;
const float t_start = 0.0f;
const float t_length = PI;
const float te = t_start + t_length;
const uint32_t num = (w_segs + 1) * (h_segs + 1);
const uint32_t num_indices = w_segs * h_segs * 6;
sphere->positions.count = num;
sphere->normals.count = num;
sphere->uvs.count = num;
sphere->indices.count = num_indices;
sphere->positions.data_size = num * 3 * sizeof(float);
sphere->normals.data_size = num * 3 * sizeof(float);
sphere->uvs.data_size = num * 2 * sizeof(float);
sphere->indices.data_size = num_indices * sizeof(uint32_t);
sphere->positions.data = (float*)malloc(sphere->positions.data_size);
sphere->normals.data = (float*)malloc(sphere->normals.data_size);
sphere->uvs.data = (float*)malloc(sphere->uvs.data_size);
sphere->indices.data = (uint32_t*)malloc(sphere->indices.data_size);
uint32_t vi = 0;
uint32_t iv = 0;
uint32_t ii = 0;
uint32_t* grid = (uint32_t*)malloc(num * sizeof(uint32_t));
vec3 n = GLM_VEC3_ZERO_INIT;
for (uint32_t iy = 0; iy <= h_segs; ++iy) {
float vf = iy / (float)h_segs;
for (uint32_t ix = 0; ix <= w_segs; ++ix, ++vi) {
float uf = ix / (float)w_segs;
float x = -radius * cosf(p_start + uf * p_length)
* sinf(t_start + vf * t_length);
float y = radius * cosf(t_start + vf * t_length);
float z = radius * sinf(p_start + uf * p_length)
* sinf(t_start + vf * t_length);
sphere->positions.data[vi * 3] = x;
sphere->positions.data[vi * 3 + 1] = y;
sphere->positions.data[vi * 3 + 2] = z;
glm_vec3_copy((vec3){x, y, z}, n);
glm_vec3_normalize(n);
sphere->normals.data[vi * 3] = n[0];
sphere->normals.data[vi * 3 + 1] = n[1];
sphere->normals.data[vi * 3 + 2] = n[2];
sphere->uvs.data[vi * 2] = uf;
sphere->uvs.data[vi * 2 + 1] = 1.0f - vf;
grid[(iy * (w_segs + 1)) + ix] = iv++;
}
}
uint32_t a = 0, b = 0, c = 0, d = 0;
for (uint32_t iy = 0; iy < h_segs; ++iy) {
for (uint32_t ix = 0; ix < w_segs; ++ix) {
a = grid[(iy * (w_segs + 1)) + (ix + 1)];
b = grid[(iy * (w_segs + 1)) + ix];
c = grid[((iy + 1) * (w_segs + 1)) + ix];
d = grid[((iy + 1) * (w_segs + 1)) + (ix + 1)];
if (iy != 0 || t_start > 0) {
sphere->indices.data[ii * 3] = a;
sphere->indices.data[ii * 3 + 1] = b;
sphere->indices.data[ii * 3 + 2] = d;
++ii;
}
if (iy != h_segs - 1 || te < PI) {
sphere->indices.data[ii * 3] = b;
sphere->indices.data[ii * 3 + 1] = c;
sphere->indices.data[ii * 3 + 2] = d;
++ii;
}
}
}
sphere->indices.count = ii * 3;
free(grid);
}
/* -------------------------------------------------------------------------- *
* GPU buffer helpers
* -------------------------------------------------------------------------- */
typedef struct {
wgpu_buffer_t vertices;
wgpu_buffer_t normals;
wgpu_buffer_t uvs;
wgpu_buffer_t indices;
} geometry_gpu_buffers_t;
typedef struct {
wgpu_buffer_t model_matrix;
wgpu_buffer_t normal_matrix;
} instanced_gpu_buffers_t;
typedef void (*instance_callback_t)(vec3* position, vec3* rotation, vec3* scale,
bool scale_uniformly);
static void get_rand_position_scale_rotation(vec3* position, vec3* rotation,
vec3* scale, bool scale_uniformly)
{
(*position)[0] = (random_float() * 2.0f - 1.0f) * WORLD_SIZE_X;
(*position)[1] = (random_float() * 2.0f - 1.0f) * WORLD_SIZE_Y;
(*position)[2] = (random_float() * 2.0f - 1.0f) * WORLD_SIZE_Z;
(*rotation)[0] = random_float() * PI2;
(*rotation)[1] = random_float() * PI2;
(*rotation)[2] = random_float() * PI2;
(*scale)[0] = random_float() + 0.25f;
(*scale)[1] = scale_uniformly ? (*scale)[0] : random_float() + 0.25f;
(*scale)[2] = scale_uniformly ? (*scale)[0] : random_float() + 0.25f;
}
static void generate_instance_matrices(instanced_geometry_t* inst,
instance_callback_t callback,
bool scale_uniformly)
{
const uint32_t count = ARRAY_SIZE(inst->model_matrix_data) / 16;
mat4 model = GLM_MAT4_ZERO_INIT;
mat4 norm = GLM_MAT4_ZERO_INIT;
vec3 pos = GLM_VEC3_ZERO_INIT, rot = GLM_VEC3_ZERO_INIT,
scl = GLM_VEC3_ZERO_INIT;
for (uint32_t idx = 0; idx < count; ++idx) {
uint32_t off = idx * 16;
callback(&pos, &rot, &scl, scale_uniformly);
glm_mat4_identity(model);
glm_translate(model, pos);
glm_rotate(model, rot[0], (vec3){1.0f, 0.0f, 0.0f});
glm_rotate(model, rot[1], (vec3){0.0f, 1.0f, 0.0f});
glm_rotate(model, rot[2], (vec3){0.0f, 0.0f, 1.0f});
glm_scale(model, scl);
glm_mat4_inv(model, norm);
glm_mat4_transpose(norm);
for (uint32_t n = 0; n < 16; ++n) {
uint32_t r = n / 4;
uint32_t c = n % 4;
inst->model_matrix_data[off + n] = model[r][c];
inst->normal_matrix_data[off + n] = norm[r][c];
}
}
}
static void create_geo_gpu_buffers(wgpu_context_t* ctx, geometry_t* geo,
geometry_gpu_buffers_t* buf)
{
buf->vertices = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Vertices buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = geo->positions.data_size,
.initial.data = geo->positions.data,
});
buf->normals = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Normals buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = geo->normals.data_size,
.initial.data = geo->normals.data,
});
buf->uvs = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "UVs buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = geo->uvs.data_size,
.initial.data = geo->uvs.data,
});
buf->indices = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Indices buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Index,
.size = geo->indices.data_size,
.count = geo->indices.count,
.initial.data = geo->indices.data,
});
}
static void create_inst_gpu_buffers(wgpu_context_t* ctx,
instanced_geometry_t* inst,
instanced_gpu_buffers_t* buf)
{
buf->model_matrix = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Instance model - Matrix buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = sizeof(inst->model_matrix_data),
.initial.data = inst->model_matrix_data,
});
buf->normal_matrix = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Instance normal - Matrix buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = sizeof(inst->normal_matrix_data),
.initial.data = inst->normal_matrix_data,
});
}
/* -------------------------------------------------------------------------- *
* Render pass helper
* -------------------------------------------------------------------------- */
typedef struct {
WGPURenderPassColorAttachment color_attachments[1];
WGPURenderPassDepthStencilAttachment depth_stencil_attachment;
WGPURenderPassDescriptor descriptor;
} render_pass_t;
/* -------------------------------------------------------------------------- *
* State struct
* -------------------------------------------------------------------------- */
static struct {
/* Options / GUI state */
struct {
bool animatable;
float tween_factor;
float tween_factor_target;
vec3 light_position;
vec3 base_colors[2];
} options;
/* Cameras */
perspective_camera_t persp_camera;
orthographic_camera_t ortho_camera;
/* Transform for fullscreen quad */
transform_t quad_transform;
/* Geometries (CPU data) */
geometry_t quad_geo;
geometry_t cube_geo;
geometry_t sphere_geo;
/* Instanced geometry data */
instanced_geometry_t instanced_cube;
instanced_geometry_t instanced_sphere;
/* GPU buffers */
geometry_gpu_buffers_t quad_buf;
geometry_gpu_buffers_t cube_buf;
geometry_gpu_buffers_t sphere_buf;
instanced_gpu_buffers_t inst_cube_buf;
instanced_gpu_buffers_t inst_sphere_buf;
/* Uniform buffers */
struct {
wgpu_buffer_t persp_camera;
wgpu_buffer_t ortho_camera;
wgpu_buffer_t quad_transform;
wgpu_buffer_t quad_tween_factor;
wgpu_buffer_t light_position;
wgpu_buffer_t base_colors[2];
} uniforms;
/* Textures */
struct {
WGPUTexture post_fx0_tex;
WGPUTextureView post_fx0_view;
WGPUTexture post_fx1_tex;
WGPUTextureView post_fx1_view;
WGPUSampler sampler;
wgpu_texture_t cutoff_mask;
} textures;
/* Offscreen framebuffer */
struct {
WGPUTexture color_tex;
WGPUTextureView color_view;
WGPUTexture depth_tex;
WGPUTextureView depth_view;
} offscreen;
/* Bind groups */
struct {
WGPUBindGroup persp_camera;
WGPUBindGroup ortho_camera;
WGPUBindGroup quad_transform;
WGPUBindGroup quad_sampler;
WGPUBindGroup quad_tween;
WGPUBindGroup light_position;
WGPUBindGroup base_colors[2];
} bind_groups;
/* Pipelines */
WGPURenderPipeline quad_pipeline;
WGPURenderPipeline scene_pipeline;
/* Render passes */
render_pass_t scene_rpass;
render_pass_t postfx_rpass;
/* Timing */
float old_time;
float last_tween_change_time;
uint64_t last_frame_time;
/* Async texture loading buffer */
uint8_t file_buffer[2048 * 2048 * 4];
WGPUBool initialized;
} state = {
.options = {
.animatable = true,
.tween_factor = 0.0f,
.tween_factor_target = 0.0f,
.light_position = {0.5f, 0.5f, 0.50f},
.base_colors[0] = {0.3f, 0.6f, 0.70f},
.base_colors[1] = {1.0f, 0.2f, 0.25f},
},
};
/* -------------------------------------------------------------------------- *
* Setup cameras
* -------------------------------------------------------------------------- */
static void setup_cameras(wgpu_context_t* ctx)
{
const float w = (float)ctx->width;
const float h = (float)ctx->height;
perspective_camera_init(&state.persp_camera, (45.0f * PI) / 180.0f, w / h,
0.1f, 100.0f);
orthographic_camera_t* cam = &state.ortho_camera;
orthographic_camera_init(cam, -w / 2.0f, w / 2.0f, h / 2.0f, -h / 2.0f, 0.1f,
3.0f);
glm_vec3_copy((vec3){0.0f, 0.0f, 2.0f}, cam->position);
glm_vec3_copy(GLM_VEC3_ZERO, cam->look_at_position);
orthographic_camera_update_projection_matrix(cam);
orthographic_camera_update_view_matrix(cam);
}
/* -------------------------------------------------------------------------- *
* Prepare geometries
* -------------------------------------------------------------------------- */
static void prepare_geometries(wgpu_context_t* ctx)
{
/* Fullscreen quad */
create_plane(&state.quad_geo, (uint32_t)ctx->width, (uint32_t)ctx->height, 1,
1);
create_geo_gpu_buffers(ctx, &state.quad_geo, &state.quad_buf);
/* Cube */
create_box(&state.cube_geo);
create_geo_gpu_buffers(ctx, &state.cube_geo, &state.cube_buf);
/* Instanced cube matrices */
generate_instance_matrices(&state.instanced_cube,
get_rand_position_scale_rotation, false);
create_inst_gpu_buffers(ctx, &state.instanced_cube, &state.inst_cube_buf);
/* Sphere */
create_sphere(&state.sphere_geo);
create_geo_gpu_buffers(ctx, &state.sphere_geo, &state.sphere_buf);
/* Instanced sphere matrices */
generate_instance_matrices(&state.instanced_sphere,
get_rand_position_scale_rotation, false);
create_inst_gpu_buffers(ctx, &state.instanced_sphere, &state.inst_sphere_buf);
}
/* -------------------------------------------------------------------------- *
* Prepare uniform buffers
* -------------------------------------------------------------------------- */
static void prepare_uniform_buffers(wgpu_context_t* ctx)
{
transform_init(&state.quad_transform);
state.uniforms.persp_camera = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Perspective camera uniform",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = 16 * 2 * sizeof(float),
});
state.uniforms.ortho_camera = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Orthographic camera uniform",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = 16 * 2 * sizeof(float),
});
state.uniforms.quad_transform = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Quad transform uniform",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = 16 * sizeof(float),
});
state.uniforms.quad_tween_factor = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Tween factor uniform",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = sizeof(float),
.initial.data = &state.options.tween_factor,
});
state.uniforms.light_position = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Light position uniform",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = 16,
.initial.data = state.options.light_position,
});
for (uint32_t i = 0; i < 2; ++i) {
state.uniforms.base_colors[i] = wgpu_create_buffer(
ctx, &(wgpu_buffer_desc_t){
.label = "Base color uniform",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = 16,
.initial.data = state.options.base_colors[i],
});
}
}
/* -------------------------------------------------------------------------- *
* Prepare offscreen framebuffer
* -------------------------------------------------------------------------- */
static void prepare_offscreen_framebuffer(wgpu_context_t* ctx)
{
WGPU_RELEASE_RESOURCE(Texture, state.offscreen.color_tex)
WGPU_RELEASE_RESOURCE(TextureView, state.offscreen.color_view)
WGPU_RELEASE_RESOURCE(Texture, state.offscreen.depth_tex)
WGPU_RELEASE_RESOURCE(TextureView, state.offscreen.depth_view)
WGPUExtent3D size = {
.width = (uint32_t)ctx->width,
.height = (uint32_t)ctx->height,
.depthOrArrayLayers = 1,
};
/* Color */
{
WGPUTextureDescriptor desc = {
.label = STRVIEW("Offscreen color texture"),
.size = size,
.mipLevelCount = 1,
.sampleCount = 1,
.dimension = WGPUTextureDimension_2D,
.format = ctx->render_format,
.usage = WGPUTextureUsage_RenderAttachment
| WGPUTextureUsage_TextureBinding | WGPUTextureUsage_CopySrc,
};
state.offscreen.color_tex = wgpuDeviceCreateTexture(ctx->device, &desc);
ASSERT(state.offscreen.color_tex != NULL);
state.offscreen.color_view = wgpuTextureCreateView(
state.offscreen.color_tex, &(WGPUTextureViewDescriptor){
.label = STRVIEW("Offscreen color view"),
.dimension = WGPUTextureViewDimension_2D,
.format = desc.format,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
});
ASSERT(state.offscreen.color_view != NULL);
}
/* Depth stencil */
{
WGPUTextureDescriptor desc = {
.label = STRVIEW("Offscreen depth texture"),
.size = size,
.mipLevelCount = 1,
.sampleCount = 1,
.dimension = WGPUTextureDimension_2D,
.format = WGPUTextureFormat_Depth24PlusStencil8,
.usage = WGPUTextureUsage_RenderAttachment,
};
state.offscreen.depth_tex = wgpuDeviceCreateTexture(ctx->device, &desc);
ASSERT(state.offscreen.depth_tex != NULL);
state.offscreen.depth_view = wgpuTextureCreateView(
state.offscreen.depth_tex, &(WGPUTextureViewDescriptor){
.label = STRVIEW("Offscreen depth view"),
.dimension = WGPUTextureViewDimension_2D,
.format = desc.format,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
.aspect = WGPUTextureAspect_All,
});
ASSERT(state.offscreen.depth_view != NULL);
}
}
/* -------------------------------------------------------------------------- *
* Prepare post-processing textures
* -------------------------------------------------------------------------- */
static void prepare_post_fx_textures(wgpu_context_t* ctx)
{
WGPU_RELEASE_RESOURCE(Texture, state.textures.post_fx0_tex)
WGPU_RELEASE_RESOURCE(TextureView, state.textures.post_fx0_view)
WGPU_RELEASE_RESOURCE(Texture, state.textures.post_fx1_tex)
WGPU_RELEASE_RESOURCE(TextureView, state.textures.post_fx1_view)
WGPUExtent3D size = {
.width = (uint32_t)ctx->width,
.height = (uint32_t)ctx->height,
.depthOrArrayLayers = 1,
};
/* Post-fx0 */
{
WGPUTextureDescriptor desc = {
.label = STRVIEW("Post-fx0 texture"),
.usage = WGPUTextureUsage_CopyDst | WGPUTextureUsage_TextureBinding,
.dimension = WGPUTextureDimension_2D,
.size = size,
.format = ctx->render_format,
.mipLevelCount = 1,
.sampleCount = 1,
};
state.textures.post_fx0_tex = wgpuDeviceCreateTexture(ctx->device, &desc);
ASSERT(state.textures.post_fx0_tex);
state.textures.post_fx0_view = wgpuTextureCreateView(
state.textures.post_fx0_tex, &(WGPUTextureViewDescriptor){
.label = STRVIEW("Post-fx0 view"),
.format = desc.format,
.dimension = WGPUTextureViewDimension_2D,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
});
ASSERT(state.textures.post_fx0_view);
}
/* Post-fx1 */
{
WGPUTextureDescriptor desc = {
.label = STRVIEW("Post-fx1 texture"),
.usage = WGPUTextureUsage_CopyDst | WGPUTextureUsage_TextureBinding,
.dimension = WGPUTextureDimension_2D,
.size = size,
.format = ctx->render_format,
.mipLevelCount = 1,
.sampleCount = 1,
};
state.textures.post_fx1_tex = wgpuDeviceCreateTexture(ctx->device, &desc);
ASSERT(state.textures.post_fx1_tex);
state.textures.post_fx1_view = wgpuTextureCreateView(
state.textures.post_fx1_tex, &(WGPUTextureViewDescriptor){
.label = STRVIEW("Post-fx1 view"),
.format = desc.format,
.dimension = WGPUTextureViewDimension_2D,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
});
ASSERT(state.textures.post_fx1_view);
}
/* Sampler (only create once) */
if (!state.textures.sampler) {
state.textures.sampler = wgpuDeviceCreateSampler(
ctx->device, &(WGPUSamplerDescriptor){
.label = STRVIEW("Post-fx sampler"),
.addressModeU = WGPUAddressMode_Repeat,
.addressModeV = WGPUAddressMode_Repeat,
.addressModeW = WGPUAddressMode_Repeat,
.magFilter = WGPUFilterMode_Linear,
.minFilter = WGPUFilterMode_Linear,
.mipmapFilter = WGPUMipmapFilterMode_Linear,
.lodMinClamp = 0.0f,
.lodMaxClamp = 1.0f,
.maxAnisotropy = 1,
});
ASSERT(state.textures.sampler);
}
}
/* -------------------------------------------------------------------------- *
* Async cutoff mask texture loading via sokol_fetch
* -------------------------------------------------------------------------- */
static void cutoff_mask_fetch_cb(const sfetch_response_t* response)
{
if (!response->fetched) {
return;
}
int w = 0, h = 0, ch = 0;
const int desired_channels = 4;
uint8_t* pixels
= image_pixels_from_memory(response->data.ptr, (int)response->data.size, &w,
&h, &ch, desired_channels);
if (pixels) {
wgpu_texture_t* tex = *(wgpu_texture_t**)response->user_data;
tex->desc = (wgpu_texture_desc_t){
.extent
= {.width = (uint32_t)w, .height = (uint32_t)h, .depthOrArrayLayers = 1},
.format = WGPUTextureFormat_RGBA8Unorm,
.pixels = {.ptr = pixels, .size = (size_t)(w * h * 4)},
};
tex->desc.is_dirty = true;
}
}
static void prepare_cutoff_mask_texture(wgpu_context_t* ctx)