-
Notifications
You must be signed in to change notification settings - Fork 24
/
fluid_simulation.c
1788 lines (1591 loc) · 61.6 KB
/
fluid_simulation.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 - Fluid Simulation
*
* WebGPU demo featuring an implementation of Jos Stam's "Real-Time Fluid
* Dynamics for Games" paper.
*
* Ref:
* JavaScript version: https://github.com/indiana-dev/WebGPU-Fluid-Simulation
* Jos Stam Paper :
* https://www.dgp.toronto.edu/public_user/stam/reality/Research/pdf/GDC03.pdf
* Nvidia GPUGem's Chapter 38 :
* https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-38-fast-fluid-dynamics-simulation-gpu
* Jamie Wong's Fluid simulation :
* https://jamie-wong.com/2016/08/05/webgl-fluid-simulation/ PavelDoGreat's
* Fluid simulation : https://github.com/PavelDoGreat/WebGL-Fluid-Simulation
* -------------------------------------------------------------------------- */
#define MAX_DIMENSIONS 3u
typedef enum {
RENDER_MODE_CLASSIC,
RENDER_MODE_SMOKE_2D,
RENDER_MODE_SMOKE_3D_SHADOWS,
RENDER_MODE_DEBUG_VELOCITY,
RENDER_MODE_DEBUG_DIVERGENCE,
RENDER_MODE_DEBUG_PRESSURE,
RENDER_MODE_DEBUG_VORTICITY,
} render_modes_t;
typedef enum {
DYNAMIC_BUFFER_VELOCITY,
DYNAMIC_BUFFER_DYE,
DYNAMIC_BUFFER_DIVERGENCE,
DYNAMIC_BUFFER_PRESSURE,
DYNAMIC_BUFFER_VORTICITY,
DYNAMIC_BUFFER_RGB,
} dynamic_buffer_type_t;
typedef enum {
INPUT_SYMMETRY_NONE,
INPUT_SYMMETRY_HORIZONTAL,
INPUT_SYMMETRY_VERTICAL,
INPUT_SYMMETRY_BOTH,
INPUT_SYMMETRY_CENTER,
} input_symmetry_t;
static struct {
float render_mode;
float grid_size;
uint32_t grid_w;
uint32_t grid_h;
uint32_t dye_size;
uint32_t dye_w;
uint32_t dye_h;
uint32_t rdx;
uint32_t dye_rdx;
float dx;
float sim_speed;
float contain_fluid;
float velocity_add_intensity;
float velocity_add_radius;
float velocity_diffusion;
float dye_add_intensity;
float dye_add_radius;
float dye_diffusion;
float viscosity;
float vorticity;
float render_intensity_multiplier;
float render_dye_buffer;
int32_t pressure_iterations;
dynamic_buffer_type_t buffer_view;
float input_symmetry;
int32_t raymarch_steps;
float smoke_density;
float enable_shadows;
float shadow_intensity;
float smoke_height;
float light_height;
float light_intensity;
float light_falloff;
float dt;
float time;
vec4 mouse;
} settings = {
.render_mode = (float)RENDER_MODE_CLASSIC,
.grid_size = 128.0f,
.dye_size = 1024,
.sim_speed = 5.0f,
.contain_fluid = 1.0f,
.velocity_add_intensity = 0.2f,
.velocity_add_radius = 0.0002f,
.velocity_diffusion = 0.9999f,
.dye_add_intensity = 1.0f,
.dye_add_radius = 0.001f,
.dye_diffusion = 0.98f,
.viscosity = 0.8f,
.vorticity = 2.0f,
.render_intensity_multiplier = 1.0f,
.render_dye_buffer = 1.0f,
.pressure_iterations = 20,
.buffer_view = DYNAMIC_BUFFER_DYE,
.input_symmetry = (float)INPUT_SYMMETRY_NONE,
.raymarch_steps = 12.0f,
.smoke_density = 40.0f,
.enable_shadows = 1.0f,
.shadow_intensity = 25.0f,
.smoke_height = 0.2f,
.light_height = 1.0f,
.light_intensity = 1.0f,
.light_falloff = 1.0f,
.dt = 0.0f,
.time = 0.0f,
.mouse = GLM_VEC4_ZERO_INIT,
};
static struct {
vec2 current;
vec2 last;
vec2 velocity;
} mouse_infos = {
.current = GLM_VEC2_ZERO_INIT,
.last = {0.0f, 1.0f}, /* y position needs to be inverted */
.velocity = GLM_VEC2_ZERO_INIT,
};
/* -------------------------------------------------------------------------- *
* Dynamic buffers
* -------------------------------------------------------------------------- */
/**
* Creates and manage multi-dimensional buffers by creating a buffer for each
* dimension
*/
typedef struct {
wgpu_context_t* wgpu_context; /* The WebGPU context*/
uint32_t dims; /* Number of dimensions */
uint32_t buffer_size; /* Size of the buffer in bytes */
uint32_t w; /* Buffer width */
uint32_t h; /* Buffer height */
wgpu_buffer_t buffers[MAX_DIMENSIONS]; /* Multi-dimensional buffers */
} dynamic_buffer_t;
static void dynamic_buffer_init_defaults(dynamic_buffer_t* this)
{
memset(this, 0, sizeof(*this));
}
static void dynamic_buffer_init(dynamic_buffer_t* this,
wgpu_context_t* wgpu_context, uint32_t dims,
uint32_t w, uint32_t h)
{
dynamic_buffer_init_defaults(this);
this->wgpu_context = wgpu_context;
this->dims = dims;
this->buffer_size = w * h * 4;
this->w = w;
this->h = h;
assert(dims <= MAX_DIMENSIONS);
for (uint32_t dim = 0; dim < dims; ++dim) {
WGPUBufferDescriptor buffer_desc = {
.label = "Dynamic buffer",
.usage = WGPUBufferUsage_Storage | WGPUBufferUsage_CopySrc
| WGPUBufferUsage_CopyDst,
.size = this->buffer_size,
.mappedAtCreation = false,
};
this->buffers[dim] = (wgpu_buffer_t){
.buffer = wgpuDeviceCreateBuffer(wgpu_context->device, &buffer_desc),
.usage = buffer_desc.usage,
.size = buffer_desc.size,
};
}
}
static void dynamic_buffer_destroy(dynamic_buffer_t* this)
{
for (uint32_t i = 0; i < this->dims; ++i) {
wgpu_destroy_buffer(&this->buffers[i]);
}
}
/**
* Copy each buffer to another DynamicBuffer's buffers.
* If the dimensions don't match, the last non-empty dimension will be copied
* instead
*/
static void dynamic_buffer_copy_to(dynamic_buffer_t* this,
dynamic_buffer_t* buffer,
WGPUCommandEncoder command_encoder)
{
for (uint32_t i = 0; i < MAX(this->dims, buffer->dims); ++i) {
wgpuCommandEncoderCopyBufferToBuffer(
command_encoder, this->buffers[MIN(i, this->dims - 1)].buffer, 0,
buffer->buffers[MIN(i, buffer->dims - 1)].buffer, 0, this->buffer_size);
}
}
/* Reset all the buffers */
static void dynamic_buffer_clear(dynamic_buffer_t* this)
{
float* empty_buffer = (float*)malloc(this->buffer_size);
memset(empty_buffer, 0, this->buffer_size);
for (uint32_t i = 0; i < this->dims; ++i) {
wgpu_queue_write_buffer(this->wgpu_context, this->buffers[i].buffer, 0,
empty_buffer, this->buffer_size);
}
free(empty_buffer);
}
/* Buffers */
static struct {
dynamic_buffer_t velocity;
dynamic_buffer_t velocity0;
dynamic_buffer_t dye;
dynamic_buffer_t dye0;
dynamic_buffer_t divergence;
dynamic_buffer_t divergence0;
dynamic_buffer_t pressure;
dynamic_buffer_t pressure0;
dynamic_buffer_t vorticity;
/* The r,g,b buffer containing the data to render */
dynamic_buffer_t rgb_buffer;
} dynamic_buffers = {0};
/* Initialize dynamic buffers */
static void dynamic_buffers_init(wgpu_context_t* wgpu_context)
{
dynamic_buffer_init(&dynamic_buffers.velocity, wgpu_context, 2,
settings.grid_w, settings.grid_h);
dynamic_buffer_init(&dynamic_buffers.velocity0, wgpu_context, 2,
settings.grid_w, settings.grid_h);
dynamic_buffer_init(&dynamic_buffers.dye, wgpu_context, 3, settings.dye_w,
settings.dye_h);
dynamic_buffer_init(&dynamic_buffers.dye0, wgpu_context, 3, settings.dye_w,
settings.dye_h);
dynamic_buffer_init(&dynamic_buffers.divergence, wgpu_context, 1,
settings.grid_w, settings.grid_h);
dynamic_buffer_init(&dynamic_buffers.divergence0, wgpu_context, 1,
settings.grid_w, settings.grid_h);
dynamic_buffer_init(&dynamic_buffers.pressure, wgpu_context, 1,
settings.grid_w, settings.grid_h);
dynamic_buffer_init(&dynamic_buffers.pressure0, wgpu_context, 1,
settings.grid_w, settings.grid_h);
dynamic_buffer_init(&dynamic_buffers.vorticity, wgpu_context, 1,
settings.grid_w, settings.grid_h);
}
static void dynamic_buffers_destroy(void)
{
dynamic_buffer_destroy(&dynamic_buffers.velocity);
dynamic_buffer_destroy(&dynamic_buffers.velocity0);
dynamic_buffer_destroy(&dynamic_buffers.dye);
dynamic_buffer_destroy(&dynamic_buffers.dye0);
dynamic_buffer_destroy(&dynamic_buffers.divergence);
dynamic_buffer_destroy(&dynamic_buffers.divergence0);
dynamic_buffer_destroy(&dynamic_buffers.pressure);
dynamic_buffer_destroy(&dynamic_buffers.pressure0);
dynamic_buffer_destroy(&dynamic_buffers.vorticity);
}
/* -------------------------------------------------------------------------- *
* Uniforms
* -------------------------------------------------------------------------- */
typedef enum {
UNIFORM_RENDER_MODE, /* render_mode */
UNIFORM_TIME, /* time */
UNIFORM_DT, /* dt */
UNIFORM_MOUSE_INFOS, /* mouseInfos */
UNIFORM_GRID_SIZE, /* gridSize */
UNIFORM_SIM_SPEED, /* sim_speed */
UNIFORM_VELOCITY_ADD_INTENSITY, /* velocity_add_intensity */
UNIFORM_VELOCITY_ADD_RADIUS, /* velocity_add_radius */
UNIFORM_VELOCITY_DIFFUSION, /* velocity_diffusion */
UNIFORM_DYE_ADD_INTENSITY, /* dye_add_intensity */
UNIFORM_DYE_ADD_RADIUS, /* dye_add_radius */
UNIFORM_DYE_ADD_DIFFUSION, /* dye_diffusion */
UNIFORM_VISCOSITY, /* viscosity */
UNIFORM_VORTICITY, /* vorticity */
UNIFORM_CONTAIN_FLUID, /* contain_fluid */
UNIFORM_MOUSE_TYPE, /* mouse_type */
UNIFORM_SMOKE_PARAMETERS, /* smoke_parameters */
UNIFORM_RENDER_INTENSITY_MULTIPLIER, /* render_intensity_multiplier */
UNIFORM_COUNT,
} uniform_type_t;
#define MAX_UNIFORM_VALUE_COUNT 8u
/* Manage uniform buffers relative to the compute shaders & the gui */
typedef struct {
uniform_type_t type;
float values[MAX_UNIFORM_VALUE_COUNT];
size_t size;
bool always_update;
bool needs_update;
wgpu_buffer_t buffer;
} uniform_t;
static struct {
uniform_t render_mode;
uniform_t time;
uniform_t dt;
uniform_t mouse;
uniform_t grid;
uniform_t sim_speed;
uniform_t vel_force;
uniform_t vel_radius;
uniform_t vel_diff;
uniform_t dye_force;
uniform_t dye_radius;
uniform_t dye_diff;
uniform_t viscosity;
uniform_t vorticity;
uniform_t contain_fluid;
uniform_t symmetry;
uniform_t smoke_parameters;
uniform_t render_intensity;
} uniforms = {0};
static uniform_t* global_uniforms[UNIFORM_COUNT] = {0};
static void uniform_init_defaults(uniform_t* this)
{
memset(this, 0, sizeof(*this));
}
static float* uniform_get_setting_value(uniform_type_t type)
{
switch (type) {
case UNIFORM_RENDER_MODE: /* render_mode */
return &settings.render_mode;
case UNIFORM_TIME: /* time */
return &settings.time;
case UNIFORM_DT: /* dt */
return &settings.dt;
case UNIFORM_MOUSE_INFOS: /* mouseInfos */
return settings.mouse;
case UNIFORM_SIM_SPEED: /* sim_speed */
return &settings.sim_speed;
case UNIFORM_VELOCITY_ADD_INTENSITY: /* velocity_add_intensity */
return &settings.velocity_add_intensity;
case UNIFORM_VELOCITY_ADD_RADIUS: /* velocity_add_radius */
return &settings.velocity_add_radius;
case UNIFORM_VELOCITY_DIFFUSION: /* velocity_diffusion */
return &settings.velocity_diffusion;
case UNIFORM_DYE_ADD_INTENSITY: /* dye_add_intensity */
return &settings.dye_add_intensity;
case UNIFORM_DYE_ADD_RADIUS: /* dye_add_radius */
return &settings.dye_add_radius;
case UNIFORM_DYE_ADD_DIFFUSION: /* dye_diffusion */
return &settings.dye_diffusion;
case UNIFORM_VISCOSITY: /* viscosity */
return &settings.viscosity;
case UNIFORM_VORTICITY: /* vorticity */
return &settings.vorticity;
case UNIFORM_CONTAIN_FLUID: /* contain_fluid */
return &settings.contain_fluid;
case UNIFORM_MOUSE_TYPE: /* mouse_type */
return &settings.input_symmetry;
case UNIFORM_RENDER_INTENSITY_MULTIPLIER: /* render_intensity_multiplier */
return &settings.render_intensity_multiplier;
default:
return NULL;
}
}
static void uniform_init(uniform_t* this, wgpu_context_t* wgpu_context,
uniform_type_t type, uint32_t size, float const* value)
{
uniform_init_defaults(this);
this->type = type;
this->size = size;
this->needs_update = false;
this->always_update = (size == 1);
if (this->size == 1 || value != NULL) {
float const* buff_value = value ? value : uniform_get_setting_value(type);
this->buffer
= wgpu_create_buffer(wgpu_context, &(wgpu_buffer_desc_t){
.label = "Uniform buffer",
.usage = WGPUBufferUsage_Uniform
| WGPUBufferUsage_CopyDst,
.size = this->size * sizeof(float),
.initial.data = buff_value,
});
if (buff_value) {
memcpy(this->values, buff_value, this->size * sizeof(float));
}
}
else {
this->buffer
= wgpu_create_buffer(wgpu_context, &(wgpu_buffer_desc_t){
.label = "Uniform buffer",
.usage = WGPUBufferUsage_Uniform
| WGPUBufferUsage_CopyDst,
.size = this->size * sizeof(float),
});
}
global_uniforms[type] = this;
}
static void uniform_destroy(uniform_t* this)
{
wgpu_destroy_buffer(&this->buffer);
}
/* Update the GPU buffer if the value has changed */
static void uniform_update(uniform_t* this, wgpu_context_t* wgpu_context,
float const* value, uint32_t value_count)
{
if (this->needs_update || this->always_update || value != NULL) {
float const* buff_value
= value ? value : uniform_get_setting_value(this->type);
if (value_count == 1 && buff_value[0] == value[0]) {
return;
}
if (buff_value) {
memcpy(this->values, buff_value, this->size * sizeof(float));
}
uint32_t buff_size
= (value_count ? MIN(this->size, value_count) : this->size)
* sizeof(float);
wgpu_queue_write_buffer(wgpu_context, this->buffer.buffer, 0, this->values,
buff_size);
this->needs_update = false;
}
}
/* Initialize uniforms */
static void uniforms_buffers_init(wgpu_context_t* wgpu_context)
{
float uniform_value = settings.render_mode;
uniform_init(&uniforms.render_mode, wgpu_context, UNIFORM_RENDER_MODE, 1,
&uniform_value);
uniform_init(&uniforms.time, wgpu_context, UNIFORM_TIME, 1, NULL);
uniform_init(&uniforms.dt, wgpu_context, UNIFORM_DT, 1, NULL);
uniform_init(&uniforms.mouse, wgpu_context, UNIFORM_MOUSE_INFOS, 4, NULL);
const float grid_values[7] = {
settings.grid_w, settings.grid_h, settings.dye_w, settings.dye_h,
settings.dx, settings.rdx, settings.dye_rdx,
};
uniform_init(&uniforms.grid, wgpu_context, UNIFORM_GRID_SIZE, 7, grid_values);
uniform_init(&uniforms.sim_speed, wgpu_context, UNIFORM_SIM_SPEED, 1, NULL);
uniform_init(&uniforms.vel_force, wgpu_context,
UNIFORM_VELOCITY_ADD_INTENSITY, 1, NULL);
uniform_init(&uniforms.vel_radius, wgpu_context, UNIFORM_VELOCITY_ADD_RADIUS,
1, NULL);
uniform_init(&uniforms.vel_diff, wgpu_context, UNIFORM_VELOCITY_DIFFUSION, 1,
NULL);
uniform_init(&uniforms.dye_force, wgpu_context, UNIFORM_DYE_ADD_INTENSITY, 1,
NULL);
uniform_init(&uniforms.dye_radius, wgpu_context, UNIFORM_DYE_ADD_RADIUS, 1,
NULL);
uniform_init(&uniforms.dye_diff, wgpu_context, UNIFORM_DYE_ADD_DIFFUSION, 1,
NULL);
uniform_init(&uniforms.viscosity, wgpu_context, UNIFORM_VISCOSITY, 1, NULL);
uniform_init(&uniforms.vorticity, wgpu_context, UNIFORM_VORTICITY, 1, NULL);
uniform_init(&uniforms.contain_fluid, wgpu_context, UNIFORM_CONTAIN_FLUID, 1,
NULL);
uniform_init(&uniforms.symmetry, wgpu_context, UNIFORM_MOUSE_TYPE, 1, NULL);
const float smoke_parameter_values[8] = {
settings.raymarch_steps, settings.smoke_density, settings.enable_shadows,
settings.shadow_intensity, settings.smoke_height, settings.light_height,
settings.light_intensity, settings.light_falloff,
};
uniform_init(&uniforms.smoke_parameters, wgpu_context,
UNIFORM_SMOKE_PARAMETERS, 8, smoke_parameter_values);
uniform_value = 1;
uniform_init(&uniforms.render_intensity, wgpu_context,
UNIFORM_RENDER_INTENSITY_MULTIPLIER, 1, &uniform_value);
}
/* Destruct uniforms */
static void uniforms_buffers_destroy(void)
{
uniform_destroy(&uniforms.render_mode);
uniform_destroy(&uniforms.time);
uniform_destroy(&uniforms.dt);
uniform_destroy(&uniforms.mouse);
uniform_destroy(&uniforms.grid);
uniform_destroy(&uniforms.sim_speed);
uniform_destroy(&uniforms.vel_force);
uniform_destroy(&uniforms.vel_radius);
uniform_destroy(&uniforms.vel_diff);
uniform_destroy(&uniforms.dye_force);
uniform_destroy(&uniforms.dye_radius);
uniform_destroy(&uniforms.dye_diff);
uniform_destroy(&uniforms.viscosity);
uniform_destroy(&uniforms.vorticity);
uniform_destroy(&uniforms.contain_fluid);
uniform_destroy(&uniforms.symmetry);
uniform_destroy(&uniforms.smoke_parameters);
uniform_destroy(&uniforms.render_intensity);
}
/* -------------------------------------------------------------------------- *
* Programs
* -------------------------------------------------------------------------- */
#define PROGRAM_MAX_BUFFER_COUNT (3u * 3u)
#define PROGRAM_MAX_UNIFORM_COUNT 8u
/* Creates a shader module, compute pipeline & bind group to use with the GPU */
typedef struct {
uint32_t dispatch_x; /* Dispatch workers width */
uint32_t dispatch_y; /* Dispatch workers height */
WGPUComputePipeline compute_pipeline;
WGPUBindGroup bind_group;
} program_t;
static struct {
program_t checker_program;
program_t update_dye_program;
program_t update_program;
program_t advect_program;
program_t boundary_program;
program_t divergence_program;
program_t boundary_div_program;
program_t pressure_program;
program_t boundary_pressure_program;
program_t gradient_subtract_program;
program_t advect_dye_program;
program_t clear_pressure_program;
program_t vorticity_program;
program_t vorticity_confinment_program;
program_t render_program;
} programs = {0};
static void program_init_defaults(program_t* this)
{
memset(this, 0, sizeof(*this));
}
static void program_init(program_t* this, wgpu_context_t* wgpu_context,
dynamic_buffer_t** buffers, uint32_t buffer_count,
uniform_t** uniforms, uint32_t uniform_count,
const char* shader_wgsl_filename, uint32_t dispatch_x,
uint32_t dispatch_y)
{
program_init_defaults(this);
/*
* Create the shader module using the WGSL string and use it to create a
* compute pipeline with 'auto' binding layout
*/
{
char shader_wgsl_path[STRMAX] = {0};
snprintf(shader_wgsl_path, strlen(shader_wgsl_filename) + 25 + 1,
"shaders/fluid_simulation/%s", shader_wgsl_filename);
wgpu_shader_t comp_shader = wgpu_shader_create(
wgpu_context, &(wgpu_shader_desc_t){
/* Compute shader WGSL */
.label = "Fluid simulation compute shader WGSL",
.file = shader_wgsl_path,
.entry = "main",
});
this->compute_pipeline = wgpuDeviceCreateComputePipeline(
wgpu_context->device,
&(WGPUComputePipelineDescriptor){
.compute = comp_shader.programmable_stage_descriptor,
});
ASSERT(this->compute_pipeline != NULL);
wgpu_shader_release(&comp_shader);
}
/*
* Concat the buffer & uniforms and format the entries to the right WebGPU
* format
*/
WGPUBindGroupEntry
bg_entries[PROGRAM_MAX_BUFFER_COUNT + PROGRAM_MAX_UNIFORM_COUNT];
uint32_t bge_i = 0;
{
for (uint32_t i = 0; i < buffer_count; ++i) {
for (uint32_t d = 0; d < buffers[i]->dims; ++d) {
bg_entries[bge_i] = (WGPUBindGroupEntry){
.binding = bge_i,
.buffer = buffers[i]->buffers[d].buffer,
.offset = 0,
.size = buffers[i]->buffers[d].size,
};
++bge_i;
}
}
for (uint32_t i = 0; i < uniform_count; ++i, ++bge_i) {
bg_entries[bge_i] = (WGPUBindGroupEntry){
.binding = bge_i,
.buffer = uniforms[i]->buffer.buffer,
.offset = 0,
.size = uniforms[i]->buffer.size,
};
}
}
/* Create the bind group using these entries & auto-layout detection */
{
WGPUBindGroupDescriptor bg_desc = {
.label = "Bind group",
.layout = wgpuComputePipelineGetBindGroupLayout(this->compute_pipeline,
0 /* index */),
.entryCount = bge_i,
.entries = bg_entries,
};
this->bind_group
= wgpuDeviceCreateBindGroup(wgpu_context->device, &bg_desc);
ASSERT(this->bind_group != NULL);
}
this->dispatch_x = dispatch_x;
this->dispatch_y = dispatch_y;
}
static void program_destroy(program_t* this)
{
WGPU_RELEASE_RESOURCE(ComputePipeline, this->compute_pipeline)
WGPU_RELEASE_RESOURCE(BindGroup, this->bind_group)
}
/* Dispatch the compute pipeline to the GPU */
static void program_dispatch(program_t* this,
WGPUComputePassEncoder pass_encoder)
{
wgpuComputePassEncoderSetPipeline(pass_encoder, this->compute_pipeline);
wgpuComputePassEncoderSetBindGroup(pass_encoder, 0, this->bind_group, 0,
NULL);
wgpuComputePassEncoderDispatchWorkgroups(
pass_encoder, (uint32_t)ceil((float)this->dispatch_x / 8.0f),
(uint32_t)ceil((float)this->dispatch_y / 8.0f), 1);
}
static void init_advect_dye_program(program_t* this,
wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[3] = {
&dynamic_buffers.dye0, /* in_quantity */
&dynamic_buffers.velocity, /* in_velocity */
&dynamic_buffers.dye, /* out_quantity */
};
uniform_t* program_uniforms[2] = {
&uniforms.grid, /* */
&uniforms.dt, /* */
};
const char* shader_wgsl_filename = "advect_dye_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.dye_w, settings.dye_h);
}
static void init_advect_program(program_t* this, wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[3] = {
&dynamic_buffers.velocity0, /* in_quantity */
&dynamic_buffers.velocity0, /* in_velocity */
&dynamic_buffers.velocity, /* out_quantity */
};
uniform_t* program_uniforms[2] = {
&uniforms.grid, /* */
&uniforms.dt, /* */
};
const char* shader_wgsl_filename = "advect_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_boundary_div_program(program_t* this,
wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[2] = {
&dynamic_buffers.divergence0, /* in_quantity */
&dynamic_buffers.divergence, /* out_quantity */
};
uniform_t* program_uniforms[1] = {
&uniforms.grid, /* */
};
const char* shader_wgsl_filename = "boundary_pressure_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_boundary_pressure_program(program_t* this,
wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[2] = {
&dynamic_buffers.pressure0, /* in_quantity */
&dynamic_buffers.pressure, /* out_quantity */
};
uniform_t* program_uniforms[1] = {
&uniforms.grid, /* */
};
const char* shader_wgsl_filename = "boundary_pressure_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_boundary_program(program_t* this, wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[2] = {
&dynamic_buffers.velocity, /* in_quantity */
&dynamic_buffers.velocity0, /* out_quantity */
};
uniform_t* program_uniforms[2] = {
&uniforms.grid, /* */
&uniforms.contain_fluid, /* */
};
const char* shader_wgsl_filename = "boundary_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_clear_pressure_program(program_t* this,
wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[2] = {
&dynamic_buffers.pressure, /* in_quantity */
&dynamic_buffers.pressure0, /* out_quantity */
};
uniform_t* program_uniforms[2] = {
&uniforms.grid, /* */
&uniforms.viscosity, /* */
};
const char* shader_wgsl_filename = "clear_pressure_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_checker_program(program_t* this, wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[1] = {
&dynamic_buffers.dye, /* */
};
uniform_t* program_uniforms[2] = {
&uniforms.grid, /* */
&uniforms.time, /* */
};
const char* shader_wgsl_filename = "checkerboard_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.dye_w, settings.dye_h);
}
static void init_divergence_program(program_t* this,
wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[2] = {
&dynamic_buffers.velocity0, /* in_velocity */
&dynamic_buffers.divergence0, /* out_divergence */
};
uniform_t* program_uniforms[1] = {
&uniforms.grid, /* */
};
const char* shader_wgsl_filename = "divergence_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_gradient_subtract_program(program_t* this,
wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[3] = {
&dynamic_buffers.pressure, /* in_pressure */
&dynamic_buffers.velocity0, /* in_velocity */
&dynamic_buffers.velocity, /* out_velocity */
};
uniform_t* program_uniforms[1] = {
&uniforms.grid, /* */
};
const char* shader_wgsl_filename = "gradient_subtract_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_pressure_program(program_t* this, wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[3] = {
&dynamic_buffers.pressure, /* in_pressure */
&dynamic_buffers.divergence, /* in_divergence */
&dynamic_buffers.pressure0, /* out_pressure */
};
uniform_t* program_uniforms[1] = {
&uniforms.grid, /* */
};
const char* shader_wgsl_filename = "pressure_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_update_dye_program(program_t* this,
wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[2] = {
&dynamic_buffers.dye, /* in_quantity */
&dynamic_buffers.dye0, /* out_quantity */
};
uniform_t* program_uniforms[8] = {
&uniforms.grid, /* */
&uniforms.mouse, /* */
&uniforms.dye_force, /* */
&uniforms.dye_radius, /* */
&uniforms.dye_diff, /* */
&uniforms.time, /* */
&uniforms.dt, /* */
&uniforms.symmetry, /* */
};
const char* shader_wgsl_filename = "update_dye_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.dye_w, settings.dye_h);
}
static void init_update_program(program_t* this, wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[2] = {
&dynamic_buffers.velocity, /* in_quantity */
&dynamic_buffers.velocity0, /* out_quantity */
};
uniform_t* program_uniforms[8] = {
&uniforms.grid, /* */
&uniforms.mouse, /* */
&uniforms.vel_force, /* */
&uniforms.vel_radius, /* */
&uniforms.vel_diff, /* */
&uniforms.dt, /* */
&uniforms.time, /* */
&uniforms.symmetry, /* */
};
const char* shader_wgsl_filename = "update_velocity_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_vorticity_confinment_program(program_t* this,
wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[3] = {
&dynamic_buffers.velocity, /* in_velocity */
&dynamic_buffers.vorticity, /* out_vorticity */
&dynamic_buffers.velocity0, /* out_vorticity */
};
uniform_t* program_uniforms[3] = {
&uniforms.grid, /* */
&uniforms.dt, /* */
&uniforms.vorticity, /* */
};
const char* shader_wgsl_filename = "vorticity_confinment_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
static void init_vorticity_program(program_t* this,
wgpu_context_t* wgpu_context)
{
dynamic_buffer_t* program_buffers[2] = {
&dynamic_buffers.velocity, /* in_velocity */
&dynamic_buffers.vorticity, /* out_vorticity */
};
uniform_t* program_uniforms[1] = {
&uniforms.grid, /* */
};
const char* shader_wgsl_filename = "vorticity_shader.wgsl";
program_init(this, wgpu_context, program_buffers,
(uint32_t)ARRAY_SIZE(program_buffers), program_uniforms,
(uint32_t)ARRAY_SIZE(program_uniforms), shader_wgsl_filename,
settings.grid_w, settings.grid_h);
}
/* Init programs */
static void programs_init(wgpu_context_t* wgpu_context)
{
init_advect_dye_program(&programs.advect_dye_program, wgpu_context);
init_advect_program(&programs.advect_program, wgpu_context);
init_boundary_div_program(&programs.boundary_div_program, wgpu_context);
init_boundary_pressure_program(&programs.boundary_pressure_program,
wgpu_context);
init_boundary_program(&programs.boundary_program, wgpu_context);
init_clear_pressure_program(&programs.clear_pressure_program, wgpu_context);
init_checker_program(&programs.checker_program, wgpu_context);
init_divergence_program(&programs.divergence_program, wgpu_context);
init_gradient_subtract_program(&programs.gradient_subtract_program,
wgpu_context);
init_pressure_program(&programs.pressure_program, wgpu_context);
init_update_dye_program(&programs.update_dye_program, wgpu_context);
init_update_program(&programs.update_program, wgpu_context);
init_vorticity_confinment_program(&programs.vorticity_confinment_program,
wgpu_context);
init_vorticity_program(&programs.vorticity_program, wgpu_context);
}
static void programs_destroy(void)
{
program_destroy(&programs.advect_dye_program);
program_destroy(&programs.advect_program);
program_destroy(&programs.boundary_div_program);
program_destroy(&programs.boundary_pressure_program);
program_destroy(&programs.boundary_program);
program_destroy(&programs.clear_pressure_program);
program_destroy(&programs.checker_program);
program_destroy(&programs.divergence_program);
program_destroy(&programs.gradient_subtract_program);
program_destroy(&programs.pressure_program);
program_destroy(&programs.update_dye_program);
program_destroy(&programs.update_program);
program_destroy(&programs.vorticity_confinment_program);
program_destroy(&programs.vorticity_program);
}
/* -------------------------------------------------------------------------- *
* Initialization
* -------------------------------------------------------------------------- */
/* Downscale if necessary to prevent crashes */
static WGPUExtent3D get_valid_dimensions(uint32_t w, uint32_t h,
uint64_t max_buffer_size,
uint64_t max_canvas_size)
{
float down_ratio = 1.0f;
/* Prevent buffer size overflow */
if (w * h * 4 >= max_buffer_size) {
down_ratio = sqrt(max_buffer_size / (float)(w * h * 4));
}
/* Prevent canvas size overflow */
if (w > max_canvas_size) {
down_ratio = max_canvas_size / (float)w;
}
else if (h > max_canvas_size) {
down_ratio = max_canvas_size / (float)h;
}
return (WGPUExtent3D){
.width = floor(w * down_ratio),
.height = floor(h * down_ratio),
};
}
/* Fit to screen while keeping the aspect ratio */
static WGPUExtent3D get_preferred_dimensions(uint32_t size,
wgpu_context_t* wgpu_context,
uint64_t max_buffer_size,
uint64_t max_canvas_size)
{
const float aspect_ratio
= (float)wgpu_context->surface.width / (float)wgpu_context->surface.height;
uint32_t w = 0, h = 0;
if (wgpu_context->surface.height < wgpu_context->surface.width) {
w = floor(size * aspect_ratio);
h = size;
}
else {