-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcompute_metaballs.c
6163 lines (5517 loc) · 215 KB
/
compute_metaballs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "example_base.h"
#include <string.h>
#include "../webgpu/imgui_overlay.h"
/* -------------------------------------------------------------------------- *
* WebGPU Example - Compute Metaballs
*
* WebGPU demo featuring marching cubes and bloom post-processing via compute
* shaders, physically based shading, deferred rendering, gamma correction and
* shadow mapping.
*
* Ref:
* https://github.com/gnikoloff/webgpu-compute-metaballs
* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- *
* Constants
*
* Ref:
* https://github.com/gnikoloff/webgpu-compute-metaballs/blob/master/src/constants.ts
* -------------------------------------------------------------------------- */
#define MAX_METABALLS 256u
static const WGPUTextureFormat DEPTH_FORMAT = WGPUTextureFormat_Depth24Plus;
static const uint32_t SHADOW_MAP_SIZE = 128;
static const uint32_t METABALLS_COMPUTE_WORKGROUP_SIZE[3] = {4, 4, 4};
static const vec4 BACKGROUND_COLOR = {0.1f, 0.1f, 0.1f, 1.0f};
/* -------------------------------------------------------------------------- *
* Protocol
*
* Ref:
* https://github.com/gnikoloff/webgpu-compute-metaballs/blob/master/src/protocol.ts
* -------------------------------------------------------------------------- */
typedef struct {
float x_min;
float y_min;
float z_min;
float x_step;
float y_step;
float z_step;
uint32_t width;
uint32_t height;
uint32_t depth;
float iso_level;
} ivolume_settings_t;
typedef struct {
float x;
float y;
float z;
float vx;
float vy;
float vz;
float speed;
} imetaball_pos_t;
typedef struct {
const char* fragment_shader_file;
struct {
WGPUBindGroupLayout* items;
uint32_t item_count;
} bind_group_layouts;
struct {
WGPUBindGroup* items;
uint32_t item_count;
} bind_groups;
WGPUTextureFormat presentation_format;
const char* label;
} iscreen_effect_t;
typedef struct {
vec3 position;
vec3 direction;
vec3 color;
float cut_off;
float outer_cut_off;
float intensity;
} ispot_light_t;
typedef enum quality_settings_enum {
QualitySettings_Low = 0,
QualitySettings_Medium = 1,
QualitySettings_High = 2,
} quality_settings_enum;
typedef struct {
bool bloom_toggle;
uint32_t shadow_res;
uint32_t point_lights_count;
float output_scale;
bool update_metaballs;
} quality_option_t;
/* -------------------------------------------------------------------------- *
* Shared Chunks
*
* Ref:
* https://github.com/gnikoloff/webgpu-compute-metaballs/blob/master/src/shaders/shared-chunks.ts
* -------------------------------------------------------------------------- */
typedef struct {
mat4 matrix; /* matrix */
mat4 inverse_matrix; /* inverse matrix */
vec2 output_size; /* screen size */
float z_near; /* near */
float z_far; /* far */
} projection_uniforms_t;
typedef struct {
mat4 matrix; /* matrix */
mat4 inverse_matrix; /* inverse matrix */
vec3 position; /* camera position */
float time; /* time */
float delta_time; /* delta time */
vec3 padding; /* padding required for struct alignment: size % 8 == 0 */
} view_uniforms_t;
typedef struct {
mat4 matrix; // matrix
} screen_projection_uniforms_t;
typedef struct {
mat4 matrix; // matrix
} screen_view_uniforms_t;
/* -------------------------------------------------------------------------- *
* Settings
*
* Ref:
* https://github.com/gnikoloff/webgpu-compute-metaballs/blob/master/src/settings.ts
* -------------------------------------------------------------------------- */
static const quality_option_t QUALITIES[3] = {
[QualitySettings_Low] = {
.bloom_toggle = false,
.shadow_res = 512,
.point_lights_count = 32,
.output_scale = 1.0f,
.update_metaballs = false,
},
[QualitySettings_Medium] = {
.bloom_toggle = true,
.shadow_res = 512,
.point_lights_count = 32,
.output_scale = 1.0f,
.update_metaballs = true,
},
[QualitySettings_High] = {
.bloom_toggle = true,
.shadow_res = 512,
.point_lights_count = 128,
.output_scale = 1.0f,
.update_metaballs = true,
},
};
static quality_settings_enum _quality = QualitySettings_Low;
static quality_option_t settings_get_quality_level(void)
{
return QUALITIES[_quality];
}
static quality_settings_enum settings_get_quality(void)
{
return _quality;
}
static void settings_set_quality(quality_settings_enum v)
{
_quality = v;
}
/* -------------------------------------------------------------------------- *
* Orthographic Camera
*
* Ref:
* https://github.com/gnikoloff/webgpu-compute-metaballs/blob/master/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* this,
vec3 target)
{
glm_vec3_copy(target, this->position);
}
static void orthographic_camera_update_view_matrix(orthographic_camera_t* this)
{
glm_lookat(this->position, /* eye */
this->look_at_position, /* center */
this->UP_VECTOR, /* up */
this->view_matrix /* dest */
);
}
static void
orthographic_camera_update_projection_matrix(orthographic_camera_t* this)
{
glm_ortho(this->left, /* left */
this->right, /* right */
this->bottom, /* bottom */
this->top, /* top */
this->near, /* nearZ */
this->far, /* farZ */
this->projection_matrix /* dest */
);
}
static void orthographic_camera_look_at(orthographic_camera_t* this,
vec3 target)
{
glm_vec3_copy(target, this->look_at_position);
orthographic_camera_update_view_matrix(this);
}
static void orthographic_camera_init_defaults(orthographic_camera_t* this)
{
memset(this, 0, sizeof(*this));
glm_vec3_copy((vec3){0.0f, 1.0f, 0.0f}, this->UP_VECTOR);
this->left = -1.0f;
this->right = 1.0f;
this->top = 1.0f;
this->bottom = -1.0f;
this->near = 0.1f;
this->far = 2000.0f;
this->zoom = 1.0f;
glm_vec3_zero(this->position);
glm_vec3_zero(this->look_at_position);
glm_mat4_identity(this->projection_matrix);
glm_mat4_identity(this->view_matrix);
}
static void orthographic_camera_init(orthographic_camera_t* this, float left,
float right, float top, float bottom,
float near, float far)
{
orthographic_camera_init_defaults(this);
this->left = left;
this->right = right;
this->top = top;
this->bottom = bottom;
this->near = near;
this->far = far;
orthographic_camera_update_projection_matrix(this);
}
/* -------------------------------------------------------------------------- *
* Perspective Camera
*
* Ref:
* https://github.com/gnikoloff/webgpu-compute-metaballs/blob/master/src/camera/perspective-camera.ts
* -------------------------------------------------------------------------- */
typedef struct {
vec3 UP_VECTOR;
vec3 position;
vec3 look_at_position;
mat4 projection_matrix;
mat4 projection_inv_matrix;
mat4 view_matrix;
mat4 view_inv_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* this,
vec3 target)
{
glm_vec3_copy(target, this->position);
}
static void perspective_camera_update_view_matrix(perspective_camera_t* this)
{
glm_lookat(this->position, /* eye */
this->look_at_position, /* center */
this->UP_VECTOR, /* up */
this->view_matrix /* dest */
);
glm_mat4_inv(this->view_matrix, this->view_inv_matrix);
}
static void
perspective_camera_update_projection_matrix(perspective_camera_t* this)
{
glm_perspective(this->field_of_view, /* fovy */
this->aspect, /* aspect */
this->near, /* nearZ */
this->far, /* farZ */
this->projection_matrix /* dest */
);
glm_mat4_inv(this->projection_matrix, this->projection_inv_matrix);
}
static void perspective_camera_look_at(perspective_camera_t* this, vec3 target)
{
glm_vec3_copy(target, this->look_at_position);
perspective_camera_update_view_matrix(this);
}
static void perspective_camera_init_defaults(perspective_camera_t* this)
{
memset(this, 0, sizeof(*this));
glm_vec3_copy((vec3){0.0f, 1.0f, 0.0f}, this->UP_VECTOR);
glm_vec3_zero(this->position);
glm_vec3_zero(this->look_at_position);
glm_mat4_identity(this->projection_matrix);
glm_mat4_identity(this->projection_inv_matrix);
glm_mat4_identity(this->view_matrix);
glm_mat4_identity(this->view_inv_matrix);
this->zoom = 1.0f;
}
static void perspective_camera_init(perspective_camera_t* this,
float field_of_view, float aspect,
float near, float far)
{
perspective_camera_init_defaults(this);
this->field_of_view = field_of_view;
this->aspect = aspect;
this->near = near;
this->far = far;
perspective_camera_update_projection_matrix(this);
}
/* -------------------------------------------------------------------------- *
* Camera Controller
*
* Ref:
* https://github.com/gnikoloff/webgpu-compute-metaballs/blob/master/src/camera/camera-controller.ts
* -------------------------------------------------------------------------- */
typedef struct {
float value;
float damping;
} damped_action_t;
static void damped_action_add_force(damped_action_t* this, float force)
{
this->value += force;
}
/**
* @brief Stops the damping.
*/
static void damped_action_stop(damped_action_t* this)
{
this->value = 0.0f;
}
/**
* @brief Updates the damping and calls.
*/
static float damped_action_update(damped_action_t* this)
{
const bool is_active = this->value * this->value > 0.000001f;
if (is_active) {
this->value *= this->damping;
}
else {
damped_action_stop(this);
}
return this->value;
}
static void damped_action_init_defaults(damped_action_t* this)
{
this->value = 0.0f;
this->damping = 0.5f;
}
static void damped_action_init(damped_action_t* this)
{
damped_action_init_defaults(this);
}
typedef enum {
CAMERA_ACTION_STATE_IDLE,
CAMERA_ACTION_STATE_ROTATE,
CAMERA_ACTION_STATE_PAN,
CAMERA_ACTION_STATE_ZOOM,
} camera_action_state_t;
typedef struct {
perspective_camera_t* camera;
vec3 target;
float min_distance;
float max_distance;
bool is_enabled;
bool is_damping;
float damping_factor;
bool is_zoom;
float zoom_speed;
bool is_rotate;
float rotate_speed;
bool is_pan;
float key_pan_speed;
bool enable_keys;
vec3 origin_target;
vec3 origin_position;
damped_action_t target_x_damped_action;
damped_action_t target_y_damped_action;
damped_action_t target_z_damped_action;
damped_action_t target_theta_damped_action;
damped_action_t target_phi_damped_action;
damped_action_t target_radius_damped_action;
bool _is_shift_down;
struct {
vec2 start;
vec2 end;
vec2 delta;
} _rotate;
struct {
float radius;
float theta;
float phi;
} _spherical;
float _zoom_distance_end;
float _zoom_distance;
camera_action_state_t state;
uint64_t loop_id;
struct {
vec2 start;
vec2 end;
vec2 delta;
} _pan;
struct {
vec2 start;
vec2 end;
vec2 delta;
} _zoom;
bool _paused;
bool _is_debug;
vec3 camera_position_debug;
float mouse_wheel_force;
} camera_controller_t;
static void camera_controller_init_defaults(camera_controller_t* this)
{
memset(this, 0, sizeof(*this));
this->min_distance = 0;
this->max_distance = INFINITY;
this->is_enabled = true;
damped_action_init(&this->target_x_damped_action);
damped_action_init(&this->target_y_damped_action);
damped_action_init(&this->target_z_damped_action);
damped_action_init(&this->target_theta_damped_action);
damped_action_init(&this->target_phi_damped_action);
damped_action_init(&this->target_radius_damped_action);
this->_rotate.start[0] = 9999.0f;
this->_rotate.start[1] = 9999.0f;
this->_rotate.end[0] = 9999.0f;
this->_rotate.end[1] = 9999.0f;
this->_rotate.delta[0] = 9999.0f;
this->_rotate.delta[1] = 9999.0f;
this->_zoom_distance_end = 0.0f;
this->_zoom_distance = 0.0f;
this->loop_id = 0;
this->_paused = false;
this->_is_debug = false;
this->mouse_wheel_force = 1.0f;
}
static void camera_controller_create(camera_controller_t* this,
perspective_camera_t* camera,
bool is_debug, float mouse_wheel_force)
{
camera_controller_init_defaults(this);
this->mouse_wheel_force = mouse_wheel_force;
ASSERT(camera);
this->camera = camera;
// Set to true to enable damping (inertia)
// If damping is enabled, you must call controls.update() in your animation
// loop
this->is_damping = false;
this->damping_factor = 0.25f;
// This option actually enables dollying in and out; left as "zoom" for
// backwards compatibility. Set to false to disable zooming
this->is_zoom = true;
this->zoom_speed = 1.0f;
// Set to false to disable rotating
this->is_rotate = true;
this->rotate_speed = 1.0f;
// Set to false to disable panning
this->is_pan = true;
this->key_pan_speed = 7.0f; // pixels moved per arrow key push
// Set to false to disable use of the keys
this->enable_keys = true;
// for reset
this->origin_position[0] = camera->position[0];
this->origin_position[1] = camera->position[0];
this->origin_position[2] = camera->position[0];
const float dx = this->camera->position[0];
const float dy = this->camera->position[1];
const float dz = this->camera->position[2];
const float radius = sqrt(dx * dx + dy * dy + dz * dz);
const float theta
= atan2(this->camera->position[0],
this->camera->position[2]); // equator angle around y-up axis
const float phi = acos(
CLAMP(this->camera->position[1] / radius, -1.0f, 1.0f)); // polar angle
this->_spherical.radius = radius;
this->_spherical.theta = theta;
this->_spherical.phi = phi;
this->_is_debug = is_debug;
}
static void camera_controller_look_at(camera_controller_t* this, vec3 target)
{
glm_vec3_copy(target, this->target);
}
static void camera_controller_pause(camera_controller_t* this)
{
this->_paused = true;
}
static void camera_controller_start(camera_controller_t* this)
{
this->_paused = false;
}
static void camera_controller_update_pan_handler(camera_controller_t* this)
{
vec3 x_dir = GLM_VEC3_ZERO_INIT;
vec3 y_dir = GLM_VEC3_ZERO_INIT;
vec3 z_dir = GLM_VEC3_ZERO_INIT;
glm_vec3_sub(this->target, this->camera->position, z_dir);
glm_vec3_normalize(z_dir);
glm_vec3_cross(z_dir, (vec3){0.0f, 1.0f, 0.0f}, x_dir);
glm_vec3_cross(x_dir, z_dir, y_dir);
const float scale = MAX(this->_spherical.radius / 2000.0f, 0.001f);
damped_action_add_force(
&this->target_x_damped_action,
(x_dir[0] * this->_pan.delta[0] + y_dir[0] * this->_pan.delta[1]) * scale);
damped_action_add_force(
&this->target_y_damped_action,
(x_dir[1] * this->_pan.delta[0] + y_dir[1] * this->_pan.delta[1]) * scale);
damped_action_add_force(
&this->target_z_damped_action,
(x_dir[2] * this->_pan.delta[0] + y_dir[2] * this->_pan.delta[1]) * scale);
}
static void
camera_controller_update_rotate_handler(camera_controller_t* this,
wgpu_example_context_t* context)
{
damped_action_add_force(&this->target_theta_damped_action,
-this->_rotate.delta[0]
/ (float)context->wgpu_context->surface.width);
damped_action_add_force(&this->target_phi_damped_action,
-this->_rotate.delta[1]
/ (float)context->wgpu_context->surface.height);
}
static void camera_controller_update_zoom_handler(camera_controller_t* this)
{
const float force = this->mouse_wheel_force;
if (this->_zoom.delta[1] > 0.0f) {
damped_action_add_force(&this->target_radius_damped_action, force);
}
else if (this->_zoom.delta[1] < 0.0f) {
damped_action_add_force(&this->target_radius_damped_action, -force);
}
}
static void
camera_controller_handle_input_events(camera_controller_t* this,
wgpu_example_context_t* context)
{
if (!this->is_enabled) {
return;
}
/* Camera rotation handling */
if (context->mouse_buttons.left) {
if (this->state != CAMERA_ACTION_STATE_ROTATE) {
this->state = CAMERA_ACTION_STATE_ROTATE;
/* Rotation start */
glm_vec2_copy(context->mouse_position, this->_rotate.start);
}
else if (this->state == CAMERA_ACTION_STATE_ROTATE
&& context->mouse_buttons.left) {
/* Rotation end */
glm_vec2_copy(context->mouse_position, this->_rotate.end);
/* Rotation delta */
glm_vec2_sub(this->_rotate.end, this->_rotate.start, this->_rotate.delta);
/* Update camera rotation */
camera_controller_update_rotate_handler(this, context);
/* Rotation start */
glm_vec2_copy(context->mouse_position, this->_rotate.start);
}
}
else if (this->state == CAMERA_ACTION_STATE_ROTATE
&& !context->mouse_buttons.left) {
this->state = CAMERA_ACTION_STATE_IDLE;
}
/* Camera pan handling */
if (context->mouse_buttons.middle) {
if (this->state != CAMERA_ACTION_STATE_PAN) {
this->state = CAMERA_ACTION_STATE_PAN;
/* Pan start */
glm_vec2_copy(context->mouse_position, this->_pan.start);
}
else if (this->state == CAMERA_ACTION_STATE_PAN
&& context->mouse_buttons.middle) {
/* Pan end */
glm_vec2_copy(context->mouse_position, this->_pan.end);
/* Pan delta */
this->_pan.delta[0] = -0.5f * (this->_pan.end[0] - this->_pan.start[0]);
this->_pan.delta[1] = 0.5f * (this->_pan.end[1] - this->_pan.start[1]);
/* Update camera panning */
camera_controller_update_pan_handler(this);
/* Pan start */
glm_vec2_copy(context->mouse_position, this->_pan.start);
}
}
else if (this->state == CAMERA_ACTION_STATE_PAN
&& !context->mouse_buttons.middle) {
this->state = CAMERA_ACTION_STATE_IDLE;
}
/* Camera zoom handling */
if (context->mouse_buttons.right) {
if (this->state != CAMERA_ACTION_STATE_ZOOM) {
this->state = CAMERA_ACTION_STATE_ZOOM;
/* Zoom start */
glm_vec2_copy(context->mouse_position, this->_zoom.start);
}
else if (this->state == CAMERA_ACTION_STATE_ZOOM
&& context->mouse_buttons.right) {
/* Zoom end */
glm_vec2_copy(context->mouse_position, this->_zoom.end);
/* Zoom delta */
glm_vec2_sub(this->_zoom.end, this->_zoom.start, this->_zoom.delta);
/* Update camera zoom */
camera_controller_update_zoom_handler(this);
/* Zoom start */
glm_vec2_copy(context->mouse_position, this->_zoom.start);
}
}
else if (this->state == CAMERA_ACTION_STATE_ZOOM
&& !context->mouse_buttons.right) {
this->state = CAMERA_ACTION_STATE_IDLE;
}
}
static void camera_controller_update_damped_action(camera_controller_t* this)
{
this->target[0] += damped_action_update(&this->target_x_damped_action);
this->target[1] += damped_action_update(&this->target_y_damped_action);
this->target[2] += damped_action_update(&this->target_z_damped_action);
this->_spherical.theta
+= damped_action_update(&this->target_theta_damped_action);
this->_spherical.phi += damped_action_update(&this->target_phi_damped_action);
this->_spherical.radius
+= damped_action_update(&this->target_radius_damped_action);
}
static void camera_controller_update_camera(camera_controller_t* this)
{
const float s_radius = this->_spherical.radius;
const float s_theta = this->_spherical.theta;
const float s_phi = this->_spherical.phi;
const float sin_phi_radius = sin(s_phi) * s_radius;
this->camera->position[0] = sin_phi_radius * sin(s_theta) + this->target[0];
this->camera->position[1] = cos(s_phi) * s_radius + this->target[1];
this->camera->position[2] = sin_phi_radius * cos(s_theta) + this->target[2];
this->camera->look_at_position[0] = this->target[0];
this->camera->look_at_position[1] = this->target[1];
this->camera->look_at_position[2] = this->target[2];
perspective_camera_update_view_matrix(this->camera);
}
static void camera_controller_tick(camera_controller_t* this)
{
if (!this->_paused) {
camera_controller_update_damped_action(this);
camera_controller_update_camera(this);
if (this->_is_debug) {
this->camera_position_debug[0]
= round(this->camera->position[0] * 100) / 100.0f;
this->camera_position_debug[1]
= round(this->camera->position[1] * 100) / 100.0f;
this->camera_position_debug[2]
= round(this->camera->position[2] * 100) / 100.0f;
}
}
this->loop_id++;
}
/* -------------------------------------------------------------------------- *
* WebGPU Renderer
*
* Ref:
* https://github.com/gnikoloff/webgpu-compute-metaballs/blob/master/src/webgpu-renderer.ts
* -------------------------------------------------------------------------- */
typedef struct {
wgpu_context_t* wgpu_context;
uint32_t output_size[2];
float device_pixel_ratio;
struct {
WGPUBindGroupLayout frame;
} bind_group_layouts;
struct {
WGPUBindGroup frame;
} bind_groups;
struct {
wgpu_buffer_t projection_ubo;
wgpu_buffer_t view_ubo;
wgpu_buffer_t screen_projection_ubo;
wgpu_buffer_t screen_view_ubo;
} ubos;
struct {
projection_uniforms_t projection_ubo;
view_uniforms_t view_ubo;
screen_projection_uniforms_t screen_projection_ubo;
screen_view_uniforms_t screen_view_ubo;
} ubos_data;
struct {
struct {
WGPUTexture texture;
WGPUTextureView view;
} depth_texture;
} textures;
WGPUSampler default_sampler;
WGPUTextureFormat presentation_format;
struct {
WGPURenderPassColorAttachment color_attachments[1];
WGPURenderPassDepthStencilAttachment depth_stencil_attachment;
WGPURenderPassDescriptor descriptor;
} framebuffer;
} webgpu_renderer_t;
static WGPUTextureFormat
webgpu_renderer_get_presentation_Format(webgpu_renderer_t* this)
{
return this->wgpu_context ? this->wgpu_context->swap_chain.format :
WGPUTextureFormat_BGRA8Unorm;
}
static void webgpu_renderer_set_output_size(webgpu_renderer_t* this,
uint32_t width, uint32_t height)
{
this->output_size[0] = width;
this->output_size[1] = height;
}
static void webgpu_renderer_get_output_size(webgpu_renderer_t* this,
uint32_t* width, uint32_t* height)
{
*width = this->output_size[0];
*height = this->output_size[1];
}
static void webgpu_renderer_init_defaults(webgpu_renderer_t* this)
{
memset(this, 0, sizeof(*this));
this->output_size[0] = 512;
this->output_size[1] = 512;
this->device_pixel_ratio = 1.0f;
}
static void webgpu_renderer_create(webgpu_renderer_t* this,
wgpu_context_t* wgpu_context)
{
webgpu_renderer_init_defaults(this);
this->wgpu_context = wgpu_context;
}
static void webgpu_renderer_init(webgpu_renderer_t* this)
{
wgpu_context_t* wgpu_context = this->wgpu_context;
/* Set presentation format */
this->presentation_format = webgpu_renderer_get_presentation_Format(this);
/* default sampler */
this->default_sampler = wgpuDeviceCreateSampler(
wgpu_context->device, &(WGPUSamplerDescriptor){
.label = "Default WebGPU renderer sampler",
.addressModeU = WGPUAddressMode_Repeat,
.addressModeV = WGPUAddressMode_Repeat,
.addressModeW = WGPUAddressMode_Repeat,
.minFilter = WGPUFilterMode_Linear,
.magFilter = WGPUFilterMode_Linear,
.mipmapFilter = WGPUMipmapFilterMode_Linear,
.lodMinClamp = 0.0f,
.lodMaxClamp = 1.0f,
.maxAnisotropy = 1,
});
ASSERT(this->default_sampler != NULL);
/* Projection UBO */
this->ubos.projection_ubo = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Projection UBO",
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = sizeof(projection_uniforms_t),
});
/* View UBO */
this->ubos.view_ubo = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "View UBO",
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = sizeof(view_uniforms_t),
});
/* Screen projection UBO*/
this->ubos.screen_projection_ubo = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Screen projection UBO",
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = sizeof(screen_projection_uniforms_t),
});
/* Screen view UBO*/
this->ubos.screen_view_ubo = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Screen view UBO",
.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst,
.size = sizeof(screen_view_uniforms_t),
});
/* Frame buffer Color attachment */
this->framebuffer.color_attachments[0] =
(WGPURenderPassColorAttachment) {
.view = NULL,
.resolveTarget = NULL,
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = BACKGROUND_COLOR[0],
.g = BACKGROUND_COLOR[1],
.b = BACKGROUND_COLOR[2],
.a = BACKGROUND_COLOR[3],
},
};
/* Depth texture */
WGPUExtent3D texture_extent = {
.width = this->output_size[0],
.height = this->output_size[1],
.depthOrArrayLayers = 1,
};
WGPUTextureDescriptor texture_desc = {
.label = "WebGPU renderer depth texture",
.size = texture_extent,
.mipLevelCount = 1,
.sampleCount = 1,
.dimension = WGPUTextureDimension_2D,
.format = DEPTH_FORMAT,
.usage
= WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding,
};
this->textures.depth_texture.texture
= wgpuDeviceCreateTexture(wgpu_context->device, &texture_desc);
ASSERT(this->textures.depth_texture.texture != NULL);
/* Depth texture view */
WGPUTextureViewDescriptor texture_view_dec = {
.label = "WebGPU renderer depth texture view",
.dimension = WGPUTextureViewDimension_2D,
.format = texture_desc.format,
.baseMipLevel = 0,
.mipLevelCount = 1,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
};
this->textures.depth_texture.view = wgpuTextureCreateView(
this->textures.depth_texture.texture, &texture_view_dec);
ASSERT(this->textures.depth_texture.view != NULL);
/* Frame buffer depth stencil attachment */
this->framebuffer.depth_stencil_attachment
= (WGPURenderPassDepthStencilAttachment){
.view = this->textures.depth_texture.view,
.depthLoadOp = WGPULoadOp_Clear,
.depthClearValue = 1.0f,
.depthStoreOp = WGPUStoreOp_Discard,
};
/* Frame buffer descriptor */
this->framebuffer.descriptor = (WGPURenderPassDescriptor){
.colorAttachmentCount = 1,
.colorAttachments = &this->framebuffer.color_attachments[0],
.depthStencilAttachment = NULL,
};
/* Frame bind group layout */
WGPUBindGroupLayoutEntry bgl_entries[3] = {
[0] = (WGPUBindGroupLayoutEntry) {
.binding = 0,
.visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment | WGPUShaderStage_Compute,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = this->ubos.projection_ubo.size,
},
.sampler = {0},
},
[1] = (WGPUBindGroupLayoutEntry) {
.binding = 1,
.visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment | WGPUShaderStage_Compute,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = this->ubos.view_ubo.size,
},
.sampler = {0},
},
[2] = (WGPUBindGroupLayoutEntry) {
.binding = 2,
.visibility = WGPUShaderStage_Fragment,
.sampler = (WGPUSamplerBindingLayout){
.type = WGPUSamplerBindingType_Filtering,
},
},
};
this->bind_group_layouts.frame = wgpuDeviceCreateBindGroupLayout(
wgpu_context->device, &(WGPUBindGroupLayoutDescriptor){
.label = "Frame bind group layout",
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
});
ASSERT(this->bind_group_layouts.frame != NULL);
/* Frame bind group */
{
WGPUBindGroupEntry bg_entries[3] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,