-
Notifications
You must be signed in to change notification settings - Fork 24
/
post_processing.c
2199 lines (1966 loc) · 77.5 KB
/
post_processing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "example_base.h"
#include <string.h>
#include "../webgpu/imgui_overlay.h"
#include "../webgpu/texture.h"
/* -------------------------------------------------------------------------- *
* WebGPU Example - Post-processing
*
* This example shows how to use a post-processing effect to blend between two
* scenes.
*
* Ref:
* https://github.com/gnikoloff/webgpu-dojo/tree/master/src/examples/postprocessing-01
* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- *
* Global variables
* -------------------------------------------------------------------------- */
#define INSTANCES_COUNT 500u
#define WORLD_SIZE_X 20u
#define WORLD_SIZE_Y 20u
#define WORLD_SIZE_Z 20u
/* -------------------------------------------------------------------------- *
* 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_copy_from_matrix(transform_t* transform, mat4 matrix)
{
glm_mat4_copy(matrix, transform->model_matrix);
transform->should_update = false;
}
/**
* @brief Sets position
*/
static void transform_set_position(transform_t* transform, vec3 position)
{
glm_vec3_copy(position, transform->position);
transform->should_update = true;
}
/**
* @brief Sets scale
*/
static void transform_set_scale(transform_t* transform, vec3 scale)
{
glm_vec3_copy(scale, transform->scale);
transform->should_update = true;
}
/**
* @brief Sets rotation
*/
static void transform_set_rotation(transform_t* transform, vec3 rotation)
{
glm_vec3_copy(rotation, transform->rotation);
transform->should_update = true;
}
/**
* @brief Update model matrix with scale, rotation and translation.
*/
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)
{
UNUSED_FUNCTION(transform_copy_from_matrix);
UNUSED_FUNCTION(transform_set_position);
UNUSED_FUNCTION(transform_set_scale);
glm_vec3_zero(transform->position);
glm_vec3_zero(transform->rotation);
glm_vec3_one(transform->scale);
transform->should_update = true;
}
/* -------------------------------------------------------------------------- *
* Orthographic Camera
*
* Ref:
* https://github.com/gnikoloff/hwoa-rang-gl/blob/main/src/camera/orthographic-camera.ts
* -------------------------------------------------------------------------- */
typedef struct {
vec3 UP_VECTOR;
float left;
float right;
float top;
float bottom;
float near;
float far;
float zoom;
vec3 position;
vec3 look_at_position;
mat4 projection_matrix;
mat4 view_matrix;
} orthographic_camera_t;
static void orthographic_camera_set_position(orthographic_camera_t* camera,
vec3 target)
{
glm_vec3_copy(target, camera->position);
}
static void
orthographic_camera_update_view_matrix(orthographic_camera_t* camera)
{
glm_lookat(camera->position, /* eye */
camera->look_at_position, /* center */
camera->UP_VECTOR, /* up */
camera->view_matrix /* dest */
);
}
static void
orthographic_camera_update_projection_matrix(orthographic_camera_t* camera)
{
glm_ortho(camera->left, /* left */
camera->right, /* right */
camera->bottom, /* bottom */
camera->top, /* top */
camera->near, /* nearZ */
camera->far, // farZ */
camera->projection_matrix /* dest */
);
}
static void orthographic_camera_look_at(orthographic_camera_t* camera,
vec3 target)
{
glm_vec3_copy(target, camera->look_at_position);
orthographic_camera_update_view_matrix(camera);
}
static void orthographic_camera_init_defaults(orthographic_camera_t* camera)
{
glm_vec3_copy((vec3){0.0f, 1.0f, 0.0f}, camera->UP_VECTOR);
camera->left = -1.0f;
camera->right = 1.0f;
camera->top = 1.0f;
camera->bottom = -1.0f;
camera->near = 0.1f;
camera->far = 2000.0f;
camera->zoom = 1.0f;
glm_vec3_zero(camera->position);
glm_vec3_zero(camera->look_at_position);
glm_mat4_zero(camera->projection_matrix);
glm_mat4_zero(camera->view_matrix);
}
static void orthographic_camera_init(orthographic_camera_t* camera, float left,
float right, float top, float bottom,
float near, float far)
{
orthographic_camera_init_defaults(camera);
camera->left = left;
camera->right = right;
camera->top = top;
camera->bottom = bottom;
camera->near = near;
camera->far = far;
orthographic_camera_update_projection_matrix(camera);
}
/* -------------------------------------------------------------------------- *
* Perspective Camera
*
* Ref:
* https://github.com/gnikoloff/hwoa-rang-gl/blob/main/src/camera/perspective-camera.ts
* -------------------------------------------------------------------------- */
typedef struct {
vec3 UP_VECTOR;
vec3 position;
vec3 look_at_position;
mat4 projection_matrix;
mat4 view_matrix;
float zoom;
float field_of_view;
float aspect;
float near;
float far;
} perspective_camera_t;
static void perspective_camera_set_position(perspective_camera_t* camera,
vec3 target)
{
glm_vec3_copy(target, camera->position);
}
static void perspective_camera_update_view_matrix(perspective_camera_t* camera)
{
glm_lookat(camera->position, /* eye */
camera->look_at_position, /* center */
camera->UP_VECTOR, /* up */
camera->view_matrix /* dest */
);
}
static void
perspective_camera_update_projection_matrix(perspective_camera_t* camera)
{
glm_perspective(camera->field_of_view, /* fovy */
camera->aspect, /* aspect */
camera->near, /* nearZ */
camera->far, /* farZ */
camera->projection_matrix /* dest */
);
}
static void perspective_camera_look_at(perspective_camera_t* camera,
vec3 target)
{
glm_vec3_copy(target, camera->look_at_position);
perspective_camera_update_view_matrix(camera);
}
static void perspective_camera_init_defaults(perspective_camera_t* camera)
{
glm_vec3_copy((vec3){0.0f, 1.0f, 0.0f}, camera->UP_VECTOR);
glm_vec3_zero(camera->position);
glm_vec3_zero(camera->look_at_position);
glm_mat4_zero(camera->projection_matrix);
glm_mat4_zero(camera->view_matrix);
camera->zoom = 1.0f;
}
static void perspective_camera_init(perspective_camera_t* camera,
float field_of_view, float aspect,
float near, float far)
{
perspective_camera_init_defaults(camera);
camera->field_of_view = field_of_view;
camera->aspect = aspect;
camera->near = near;
camera->far = far;
perspective_camera_update_projection_matrix(camera);
}
/* -------------------------------------------------------------------------- *
* Geometry class
* -------------------------------------------------------------------------- */
typedef struct {
struct {
float* data;
size_t data_size;
size_t count;
} positions;
struct {
float* data;
size_t data_size;
size_t count;
} normals;
struct {
float* data;
size_t data_size;
size_t count;
} uvs;
struct {
uint32_t* data;
size_t data_size;
size_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* geometry)
{
if ((geometry->positions.data != NULL) && geometry->positions.data_size > 0) {
free(geometry->positions.data);
geometry->positions.data = NULL;
geometry->positions.data_size = 0;
geometry->positions.count = 0;
}
if ((geometry->normals.data != NULL) && geometry->normals.data_size > 0) {
free(geometry->normals.data);
geometry->normals.data = NULL;
geometry->normals.data_size = 0;
geometry->normals.count = 0;
}
if ((geometry->uvs.data != NULL) && geometry->uvs.data_size > 0) {
free(geometry->uvs.data);
geometry->uvs.data = NULL;
geometry->uvs.data_size = 0;
geometry->uvs.count = 0;
}
if ((geometry->indices.data != NULL) && geometry->indices.data_size > 0) {
free(geometry->indices.data);
geometry->indices.data = NULL;
geometry->indices.data_size = 0;
geometry->indices.count = 0;
}
}
/* -------------------------------------------------------------------------- *
* Plane Geometry
*
* Ref:
* https://github.com/gnikoloff/hwoa-rang-gl/blob/0f865ca0d47f9d0e1fd527ee6f30a6ade32edcd7/src/geometry-utils/create-plane.ts
* https://github.com/gnikoloff/hwoa-rang-gl/blob/0f865ca0d47f9d0e1fd527ee6f30a6ade32edcd7/src/geometry-utils/build-plane.ts
* -------------------------------------------------------------------------- */
typedef struct {
uint32_t width;
uint32_t height;
uint32_t width_segments;
uint32_t height_segments;
} plane_desc_t;
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.f;
normal[i * 3 + v] = 0.f;
normal[i * 3 + w] = depth >= 0 ? 1.f : -1.f;
uv[i * 2] = ix / w_segs;
uv[i * 2 + 1] = 1 - iy / 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;
}
}
}
/**
* @brief Generates geometry data for a quad.
* @param plane plane geometry
* @param plane_desc params
* @return pointer to the generated geometry data
*/
static geometry_t* create_plane(geometry_t* plane, plane_desc_t* plane_desc)
{
const uint32_t width = (plane_desc != NULL) ? plane_desc->width : 1;
const uint32_t height = (plane_desc != NULL) ? plane_desc->height : 1;
const uint32_t w_segs = (plane_desc != NULL) ? plane_desc->width_segments : 1;
const uint32_t h_segs
= (plane_desc != NULL) ? plane_desc->height_segments : 1;
// Determine length of arrays
const uint32_t num = (w_segs + 1) * (h_segs + 1);
const uint32_t num_indices = w_segs * h_segs * 6;
// Set array sizes
plane->positions.count = num;
plane->normals.count = num;
plane->uvs.count = num;
plane->indices.count = num_indices;
// Set array size (in bytes)
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);
// Generate empty arrays once
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, /* vertices */
plane->normals.data, /* normal */
plane->uvs.data, /* uv */
plane->indices.data, /* indices */
width, /* width */
height, /* height */
0, /* depth */
w_segs, /* w_segs */
h_segs, /* h_segs */
0, /* u */
1, /* v */
2, /* w */
1, /* u_dir */
-1, /* v_dir */
0, /* i */
0 /* ii */
);
return plane;
}
/* -------------------------------------------------------------------------- *
* Box Geometry
*
* Ref:
* https://github.com/gnikoloff/hwoa-rang-gl/blob/0f865ca0d47f9d0e1fd527ee6f30a6ade32edcd7/src/geometry-utils/create-box.ts
* -------------------------------------------------------------------------- */
typedef struct {
uint32_t width;
uint32_t height;
uint32_t depth;
uint32_t width_segments;
uint32_t height_segments;
uint32_t depth_segments;
bool separate_faces;
} box_desc_t;
/**
* @brief Generates geometry data for a box.
* @param box box geometry
* @param box_desc params
* @return pointer to the generated geometry data
*/
static geometry_t* create_box(geometry_t* box, box_desc_t* box_desc)
{
const uint32_t width = (box_desc != NULL) ? box_desc->width : 1;
const uint32_t height = (box_desc != NULL) ? box_desc->height : 1;
const uint32_t depth = (box_desc != NULL) ? box_desc->depth : 1;
const uint32_t w_segs = (box_desc != NULL) ? box_desc->width_segments : 1;
const uint32_t h_segs = (box_desc != NULL) ? box_desc->height_segments : 1;
const uint32_t d_segs = (box_desc != NULL) ? box_desc->depth_segments : 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;
// Set array count
box->positions.count = num;
box->normals.count = num;
box->uvs.count = num;
box->indices.count = num_indices;
// Set array size (in bytes)
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);
// Generate empty arrays once
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, // vertices
box->normals.data, // normal
box->uvs.data, // uv
box->indices.data, // indices
depth, // width
height, // height
width, // depth
d_segs, // w_segs
h_segs, // h_segs
2, // u
1, // v
0, // w
-1, // u_dir
-1, // v_dir
i, // i
ii // ii
);
}
/* LEFT */
{
build_plane(box->positions.data, // vertices
box->normals.data, // normal
box->uvs.data, // uv
box->indices.data, // indices
depth, // width
height, // height
-((int)width), // depth
d_segs, // w_segs
h_segs, // h_segs
2, // u
1, // v
0, // w
1, // u_dir
-1, // v_dir
(i += (d_segs + 1) * (h_segs + 1)), // i
(ii += d_segs * h_segs) // ii
);
}
/* TOP */
{
build_plane(box->positions.data, // vertices
box->normals.data, // normal
box->uvs.data, // uv
box->indices.data, // indices
width, // width
depth, // height
height, // depth
d_segs, // w_segs
h_segs, // h_segs
0, // u
2, // v
1, // w
1, // u_dir
1, // v_dir
(i += (d_segs + 1) * (h_segs + 1)), // i
(ii += d_segs * h_segs) // ii
);
}
/* BOTTOM */
{
build_plane(box->positions.data, // vertices
box->normals.data, // normal
box->uvs.data, // uv
box->indices.data, // indices
width, // width
depth, // height
-((int)height), // depth
d_segs, // w_segs
h_segs, // h_segs
0, // u
2, // v
1, // w
1, // u_dir
-1, // v_dir
(i += (w_segs + 1) * (d_segs + 1)), // i
(ii += w_segs * d_segs) // ii
);
}
/* BACK */
{
build_plane(box->positions.data, // vertices
box->normals.data, // normal
box->uvs.data, // uv
box->indices.data, // indices
width, // width
height, // height
-((int)depth), // depth
w_segs, // w_segs
h_segs, // h_segs
0, // u
1, // v
2, // w
-1, // u_dir
-1, // v_dir
(i += (w_segs + 1) * (d_segs + 1)), // i
(ii += w_segs * d_segs) // ii
);
}
/* FRONT */
{
build_plane(box->positions.data, // vertices
box->normals.data, // normal
box->uvs.data, // uv
box->indices.data, // indices
width, // width
height, // height
depth, // depth
w_segs, // w_segs
h_segs, // h_segs
0, // u
1, // v
2, // w
1, // u_dir
-1, // v_dir
(i += (w_segs + 1) * (h_segs + 1)), // i
(ii += w_segs * h_segs) // ii
);
}
return box;
}
/* -------------------------------------------------------------------------- *
* Sphere Geometry
*
* Ref:
* https://github.com/gnikoloff/hwoa-rang-gl/blob/0f865ca0d47f9d0e1fd527ee6f30a6ade32edcd7/src/geometry-utils/create-sphere.ts
* -------------------------------------------------------------------------- */
typedef struct {
float radius;
uint32_t width_segments;
uint32_t height_segments;
float phi_start;
float phi_length;
float theta_start;
float theta_length;
} sphere_desc_t;
/**
* @brief Generates geometry data for a sphere.
* @param sphere sphere geometry
* @param sphere_desc sphere creation parameters
* @return pointer to the generated geometry data
*/
static geometry_t* create_sphere(geometry_t* sphere, sphere_desc_t* sphere_desc)
{
const float radius = (sphere_desc != NULL) ? sphere_desc->radius : 0.5f;
const uint32_t w_segs
= (sphere_desc != NULL) ? sphere_desc->width_segments : 16u;
const uint32_t h_segs = (sphere_desc != NULL) ? sphere_desc->height_segments :
(uint32_t)ceil(w_segs * 0.5f);
const float p_start = (sphere_desc != NULL) ? sphere_desc->phi_start : 0.0f;
const float p_length = (sphere_desc != NULL) ? sphere_desc->phi_length : PI2;
const float t_start = (sphere_desc != NULL) ? sphere_desc->theta_start : 0.0f;
const float t_length = (sphere_desc != NULL) ? sphere_desc->theta_length : PI;
const uint32_t num = (w_segs + 1) * (h_segs + 1);
const uint32_t num_indices = w_segs * h_segs * 6;
/* Set array count */
sphere->positions.count = num;
sphere->normals.count = num;
sphere->uvs.count = num;
sphere->indices.count = num_indices;
/* Set array size (in bytes) */
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);
/* Generate empty arrays once */
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 i = 0;
uint32_t iv = 0;
uint32_t ii = 0;
const uint32_t te = t_start + t_length;
uint32_t* grid = (uint32_t*)malloc(num * sizeof(uint32_t));
vec3 n = GLM_VEC3_ZERO_INIT;
float v = 0.0f, u = 0.0f, x = 0.0f, y = 0.0f, z = 0.0f;
for (uint32_t iy = 0; iy <= h_segs; ++iy) {
v = iy / (float)h_segs;
for (uint32_t ix = 0; ix <= w_segs; ++ix, ++i) {
u = ix / (float)w_segs;
x = -radius * //
cos(p_start + u * p_length) * //
sin(t_start + v * t_length);
y = radius * cos(t_start + v * t_length);
z = radius * sin(p_start + u * p_length) * sin(t_start + v * t_length);
sphere->positions.data[i * 3] = x;
sphere->positions.data[i * 3 + 1] = y;
sphere->positions.data[i * 3 + 2] = z;
glm_vec3_copy((vec3){x, y, z}, n);
glm_vec3_normalize(n);
sphere->normals.data[i * 3] = n[0];
sphere->normals.data[i * 3 + 1] = n[1];
sphere->normals.data[i * 3 + 2] = n[2];
sphere->uvs.data[i * 2] = u;
sphere->uvs.data[i * 2 + 1] = 1 - v;
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;
}
}
}
free(grid);
return sphere;
}
/* -------------------------------------------------------------------------- *
* Helper Functions
*
* Ref:
* https://github.com/gnikoloff/webgpu-dojo/blob/master/src/examples/postprocessing-01/helpers.ts
* -------------------------------------------------------------------------- */
typedef struct {
wgpu_buffer_t vertices;
wgpu_buffer_t normals;
wgpu_buffer_t uvs;
wgpu_buffer_t indices;
} geometry_gpu_buffers_t;
static void
geometry_gpu_buffers_destroy(geometry_gpu_buffers_t* geometry_gpu_buffers)
{
WGPU_RELEASE_RESOURCE(Buffer, geometry_gpu_buffers->vertices.buffer)
WGPU_RELEASE_RESOURCE(Buffer, geometry_gpu_buffers->normals.buffer)
WGPU_RELEASE_RESOURCE(Buffer, geometry_gpu_buffers->uvs.buffer)
WGPU_RELEASE_RESOURCE(Buffer, geometry_gpu_buffers->indices.buffer)
}
typedef struct {
wgpu_buffer_t model_matrix;
wgpu_buffer_t normal_matrix;
} instanced_geometry_gpu_buffers_t;
static void instanced_geometry_gpu_buffers_destroy(
instanced_geometry_gpu_buffers_t* instanced_geometry_gpu_buffers)
{
WGPU_RELEASE_RESOURCE(Buffer,
instanced_geometry_gpu_buffers->model_matrix.buffer)
WGPU_RELEASE_RESOURCE(Buffer,
instanced_geometry_gpu_buffers->normal_matrix.buffer)
}
typedef void (*generate_instance_on_item_callback)(vec3* position,
vec3* rotation, vec3* scale,
bool scale_uniformly);
static instanced_geometry_t*
generate_instance_matrices(instanced_geometry_t* instanced_model,
generate_instance_on_item_callback on_item_callback,
bool scale_uniformly)
{
uint32_t count = ARRAY_SIZE(instanced_model->model_matrix_data) / 16;
vec3 instance_move_vector = GLM_VEC3_ZERO_INIT;
mat4 instance_model_matrix = GLM_MAT4_ZERO_INIT,
normal_matrix = GLM_MAT4_ZERO_INIT;
vec3 position = GLM_VEC3_ZERO_INIT, rotation = GLM_VEC3_ZERO_INIT,
scale = GLM_VEC3_ZERO_INIT;
uint32_t r = 0, c = 0;
for (uint32_t i = 0; i < count * 16; i += 16) {
on_item_callback(&position, &rotation, &scale, scale_uniformly);
glm_mat4_identity(instance_model_matrix);
glm_vec3_copy((vec3){position[0], position[1], position[2]},
instance_move_vector);
glm_translate(instance_model_matrix, instance_move_vector);
glm_rotate(instance_model_matrix, rotation[0], (vec3){1.0f, 0.0f, 0.0f});
glm_rotate(instance_model_matrix, rotation[1], (vec3){0.0f, 1.0f, 0.0f});
glm_rotate(instance_model_matrix, rotation[2], (vec3){0.0f, 0.0f, 1.0f});
glm_vec3_copy((vec3){scale[0], scale[1], scale[2]}, instance_move_vector);
glm_scale(instance_model_matrix, instance_move_vector);
glm_mat4_inv(instance_model_matrix, normal_matrix);
glm_mat4_transpose(normal_matrix);
for (uint32_t n = 0; n < 16; ++n) {
r = n / 4;
c = n % 4;
instanced_model->model_matrix_data[i + n] = instance_model_matrix[r][c];
instanced_model->normal_matrix_data[i + n] = normal_matrix[r][c];
}
}
return instanced_model;
}
static void
generate_gpu_buffers_from_geometry(wgpu_context_t* wgpu_context,
geometry_t* geometry,
geometry_gpu_buffers_t* gpu_buffers)
{
/* Vertices */
gpu_buffers->vertices = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Vertices buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = geometry->positions.data_size,
.initial.data = geometry->positions.data,
});
/* Normals */
gpu_buffers->normals = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Normals buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = geometry->normals.data_size,
.initial.data = geometry->normals.data,
});
/* UVs */
gpu_buffers->uvs = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "UVs buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = geometry->uvs.data_size,
.initial.data = geometry->uvs.data,
});
/* Indices */
uint32_t index_count
= geometry->indices.data_size / sizeof(geometry->indices.data[0]);
gpu_buffers->indices = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Indices buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Index,
.size = geometry->indices.data_size,
.count = index_count,
.initial.data = geometry->indices.data,
});
}
static void generate_gpu_buffers_from_instanced_geometry(
wgpu_context_t* wgpu_context, instanced_geometry_t* geometry,
instanced_geometry_gpu_buffers_t* gpu_buffers)
{
/* Instance model matrix */
gpu_buffers->model_matrix = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Instance - Model matrix",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = sizeof(geometry->model_matrix_data),
.initial.data = geometry->model_matrix_data,
});
/* Instance model matrix */
gpu_buffers->normal_matrix = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Instance - Model matrix",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = sizeof(geometry->normal_matrix_data),
.initial.data = geometry->normal_matrix_data,
});
}
/* -------------------------------------------------------------------------- *
* Post-processing example
* -------------------------------------------------------------------------- */
static struct {
bool animatable;
float tween_factor;
float tween_factor_target;
vec3 light_position;
vec3 base_colors[2];
} 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},
};
static transform_t quad_transform = {0};
static struct {
perspective_camera_t perspective_camera;
orthographic_camera_t orthographic_camera;
} cameras = {0};
static struct {
geometry_t quad;
geometry_t cube;
geometry_t sphere;
} geometries = {0};
static struct {
instanced_geometry_t cube;
instanced_geometry_t sphere;
} instanced_geometries = {0};
static struct {
geometry_gpu_buffers_t quad;
geometry_gpu_buffers_t cube;
geometry_gpu_buffers_t sphere;
instanced_geometry_gpu_buffers_t instanced_cube;
instanced_geometry_gpu_buffers_t instanced_sphere;
} vertex_buffers = {0};
static struct {
wgpu_buffer_t persp_camera, ortho_camera, quad_transform, quad_tween_factor,
light_position, base_colors[2];
} uniform_buffers = {0};
static struct {
struct {
WGPUTexture texture;
WGPUTextureView view;
} post_fx0, post_fx1;
WGPUSampler post_fx_sampler;
texture_t cutoff_mask;
} textures = {0};
/* Framebuffer for offscreen rendering */
static struct {
struct {
WGPUTexture texture;
WGPUTextureView texture_view;
} color, depth_stencil;
} offscreen_framebuffer = {0};
static 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 = {0};
static struct {
WGPURenderPipeline fullscreen_quad;
WGPURenderPipeline scene_meshes;
} pipelines = {0};
/* Render pass descriptor for frame buffer writes */
typedef struct {
WGPURenderPassColorAttachment color_attachments[1];