-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathanimometer.c
More file actions
673 lines (593 loc) · 22.5 KB
/
animometer.c
File metadata and controls
673 lines (593 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#include "webgpu/imgui_overlay.h"
#include "webgpu/wgpu_common.h"
#include <cglm/cglm.h>
#define SOKOL_TIME_IMPL
#include <sokol_time.h>
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#endif
#include <cimgui.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#include <stdio.h>
/* -------------------------------------------------------------------------- *
* WebGPU Example - Animometer
*
* A WebGPU port of the Animometer MotionMark benchmark.
*
* Ref:
* https://github.com/austinEng/webgpu-samples/blob/main/src/pages/samples/animometer.ts
* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
static const char* vertex_shader_wgsl;
static const char* fragment_shader_wgsl;
/* -------------------------------------------------------------------------- *
* Animometer example
* -------------------------------------------------------------------------- */
/* State struct */
static struct {
struct {
WGPUBuffer buffer;
uint32_t count;
} vertices;
WGPUBuffer uniform_buffer;
uint64_t time_offset;
float uniform_time[1];
uint64_t uniform_bytes;
uint64_t aligned_uniform_bytes;
uint64_t aligned_uniform_floats;
WGPUPipelineLayout pipeline_layout;
WGPUPipelineLayout dynamic_pipeline_layout;
WGPURenderPipeline pipeline;
WGPURenderPipeline dynamic_pipeline;
WGPURenderPassColorAttachment color_attachment;
WGPURenderPassDescriptor render_pass_descriptor;
WGPURenderBundle render_bundle;
WGPUBindGroupLayout time_bind_group_layout;
WGPUBindGroupLayout bind_group_layout;
WGPUBindGroupLayout dynamic_bind_group_layout;
WGPUBindGroup* bind_groups;
WGPUBindGroup dynamic_bind_group;
WGPUBindGroup time_bind_group;
struct {
uint64_t num_triangles;
WGPUBool render_bundles;
WGPUBool dynamic_offsets;
} settings;
/* Performance tracking */
uint64_t last_frame_time;
uint64_t previous_frame_timestamp;
double js_time_avg;
double frame_time_avg;
WGPUBool update_display;
WGPUBool initialized;
} state = {
.color_attachment = {
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = {0.0, 0.0, 0.0, 1.0},
.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED,
},
.render_pass_descriptor = {
.colorAttachmentCount = 1,
.colorAttachments = &state.color_attachment,
},
.settings = {
.num_triangles = 20000,
.render_bundles = 1,
.dynamic_offsets = 0,
},
.update_display = 1,
};
/* Initialize vertex buffers */
static void init_vertex_buffer(wgpu_context_t* wgpu_context)
{
// clang-format off
static const float vertices_data[(4 + 4) * 3] = {
// position data /**/ color data /**/
0.0f, 0.1f, 0.0f, 1.0f, /**/ 1.0f, 0.0f, 0.0f, 1.0f, /**/
-0.1f, -0.1f, 0.0f, 1.0f, /**/ 0.0f, 1.0f, 0.0f, 1.0f, /**/
0.1f, -0.1f, 0.0f, 1.0f, /**/ 0.0f, 0.0f, 1.0f, 1.0f, /**/
};
// clang-format on
state.vertices.count = 3u;
uint64_t vertex_buffer_size = (uint64_t)sizeof(vertices_data);
/* Vertex buffer */
state.vertices.buffer = wgpu_create_buffer_from_data(
wgpu_context, vertices_data, vertex_buffer_size, WGPUBufferUsage_Vertex);
}
static void init_pipeline_layouts(wgpu_context_t* wgpu_context)
{
/* Time bind group layout */
{
WGPUBindGroupLayoutEntry bgl_entries[1] = {
[0] = (WGPUBindGroupLayoutEntry) {
.binding = 0,
.visibility = WGPUShaderStage_Vertex,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = 4,
},
},
};
WGPUBindGroupLayoutDescriptor bgl_desc = {
.label = STRVIEW("Time - Bind group layout"),
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
};
state.time_bind_group_layout
= wgpuDeviceCreateBindGroupLayout(wgpu_context->device, &bgl_desc);
ASSERT(state.time_bind_group_layout != NULL);
}
/* Bind group layout */
{
WGPUBindGroupLayoutEntry bgl_entries[1] = {
[0] = (WGPUBindGroupLayoutEntry) {
.binding = 0,
.visibility = WGPUShaderStage_Vertex,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.minBindingSize = 20,
},
},
};
WGPUBindGroupLayoutDescriptor bgl_desc = {
.label = STRVIEW("Bind group layout"),
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
};
state.bind_group_layout
= wgpuDeviceCreateBindGroupLayout(wgpu_context->device, &bgl_desc);
ASSERT(state.bind_group_layout != NULL);
}
/* Dynamic bind group layout */
{
WGPUBindGroupLayoutEntry bgl_entries[1] = {
[0] = (WGPUBindGroupLayoutEntry) {
.binding = 0,
.visibility = WGPUShaderStage_Vertex,
.buffer = (WGPUBufferBindingLayout) {
.type = WGPUBufferBindingType_Uniform,
.hasDynamicOffset = true,
.minBindingSize = 20,
},
},
};
WGPUBindGroupLayoutDescriptor bgl_desc = {
.label = STRVIEW("Dynamic - Bind group layout"),
.entryCount = (uint32_t)ARRAY_SIZE(bgl_entries),
.entries = bgl_entries,
};
state.dynamic_bind_group_layout
= wgpuDeviceCreateBindGroupLayout(wgpu_context->device, &bgl_desc);
ASSERT(state.dynamic_bind_group_layout != NULL);
}
/* Pipeline layout */
{
WGPUBindGroupLayout bgl_pipeline[2]
= {state.time_bind_group_layout, state.bind_group_layout};
WGPUPipelineLayoutDescriptor pipeline_layout_desc = {
.label = STRVIEW("Render - Pipeline layout"),
.bindGroupLayoutCount = (uint32_t)ARRAY_SIZE(bgl_pipeline),
.bindGroupLayouts = bgl_pipeline,
};
state.pipeline_layout = wgpuDeviceCreatePipelineLayout(
wgpu_context->device, &pipeline_layout_desc);
ASSERT(state.pipeline_layout != NULL);
}
/* Dynamic pipeline layout */
{
WGPUBindGroupLayout bgl_pipeline[2]
= {state.time_bind_group_layout, state.dynamic_bind_group_layout};
WGPUPipelineLayoutDescriptor pipeline_layout_desc = {
.label = STRVIEW("Dynamic - Pipeline layout"),
.bindGroupLayoutCount = (uint32_t)ARRAY_SIZE(bgl_pipeline),
.bindGroupLayouts = bgl_pipeline,
};
state.dynamic_pipeline_layout = wgpuDeviceCreatePipelineLayout(
wgpu_context->device, &pipeline_layout_desc);
ASSERT(state.dynamic_pipeline_layout != NULL);
}
}
static void update_uniform_buffers(wgpu_context_t* wgpu_context)
{
/* Update uniforms */
state.uniform_time[0] = stm_sec(stm_now());
wgpuQueueWriteBuffer(wgpu_context->queue, state.uniform_buffer,
state.time_offset, &state.uniform_time,
sizeof(state.uniform_time));
}
static void init_uniform_buffers(wgpu_context_t* wgpu_context)
{
state.uniform_bytes = 5 * sizeof(float);
state.aligned_uniform_bytes = ceil(state.uniform_bytes / 256.0f) * 256;
state.aligned_uniform_floats = state.aligned_uniform_bytes / sizeof(float);
state.uniform_buffer = wgpuDeviceCreateBuffer(
wgpu_context->device,
&(WGPUBufferDescriptor){
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = MAX(state.settings.num_triangles, 1) * state.aligned_uniform_bytes
+ sizeof(float),
});
float uniform_buffer_data[state.settings.num_triangles
* state.aligned_uniform_floats];
state.bind_groups
= malloc(state.settings.num_triangles * sizeof(WGPUBindGroup));
for (uint64_t i = 0; i < state.settings.num_triangles; ++i) {
uniform_buffer_data[state.aligned_uniform_floats * i + 0]
= random_float_min_max(0.0f, 1.0f) * 0.2f + 0.2f; /* scale */
uniform_buffer_data[state.aligned_uniform_floats * i + 1]
= 0.9f * 2.0f * (random_float_min_max(0.0f, 1.0f) - 0.5f); /* offsetX */
uniform_buffer_data[state.aligned_uniform_floats * i + 2]
= 0.9f * 2.0f * (random_float_min_max(0.0f, 1.0f) - 0.5f); /* offsetY */
uniform_buffer_data[state.aligned_uniform_floats * i + 3]
= random_float_min_max(0.0f, 1.0f) * 1.5f + 0.5f; /* scalar */
uniform_buffer_data[state.aligned_uniform_floats * i + 4]
= random_float_min_max(0.0f, 1.0f) * 10.0f; /* scalarOffset */
WGPUBindGroupEntry bg_entries[1] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,
.buffer = state.uniform_buffer,
.offset = i * state.aligned_uniform_bytes,
.size = 6 * sizeof(float),
},
};
state.bind_groups[i] = wgpuDeviceCreateBindGroup(
wgpu_context->device,
(&(WGPUBindGroupDescriptor){
.label = STRVIEW("Uniform buffer - Bind group layout"),
.layout = state.bind_group_layout,
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
}));
}
WGPUBindGroupEntry dynamic_bg_entries[1] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,
.buffer = state.uniform_buffer,
.offset = 0,
.size = 6 * sizeof(float),
},
};
state.dynamic_bind_group = wgpuDeviceCreateBindGroup(
wgpu_context->device,
(&(WGPUBindGroupDescriptor){
.label = STRVIEW("Dynamic - Bind group"),
.layout = state.dynamic_bind_group_layout,
.entryCount = (uint32_t)ARRAY_SIZE(dynamic_bg_entries),
.entries = dynamic_bg_entries,
}));
state.time_offset
= state.settings.num_triangles * state.aligned_uniform_bytes;
WGPUBindGroupEntry time_bg_entries[1] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,
.buffer = state.uniform_buffer,
.offset = state.time_offset,
.size = sizeof(float),
},
};
state.time_bind_group = wgpuDeviceCreateBindGroup(
wgpu_context->device, (&(WGPUBindGroupDescriptor){
.label = STRVIEW("Time - Bind group layout"),
.layout = state.time_bind_group_layout,
.entryCount = (uint32_t)ARRAY_SIZE(time_bg_entries),
.entries = time_bg_entries,
}));
const uint64_t max_mapping_length = (14 * 1024 * 1024) / sizeof(float);
for (uint64_t offset = 0; offset < ARRAY_SIZE(uniform_buffer_data);
offset += max_mapping_length) {
const uint64_t upload_count
= MIN(ARRAY_SIZE(uniform_buffer_data) - offset, max_mapping_length);
wgpuQueueWriteBuffer(wgpu_context->queue, state.uniform_buffer,
offset * sizeof(float), &uniform_buffer_data[offset],
upload_count * sizeof(float));
}
}
#define RECORD_RENDER_PASS(Type, rpass_enc) \
if (rpass_enc) { \
if (state.settings.dynamic_offsets) { \
wgpu##Type##SetPipeline(rpass_enc, state.dynamic_pipeline); \
} \
else { \
wgpu##Type##SetPipeline(rpass_enc, state.pipeline); \
} \
wgpu##Type##SetVertexBuffer(rpass_enc, 0, state.vertices.buffer, 0, \
WGPU_WHOLE_SIZE); \
wgpu##Type##SetBindGroup(rpass_enc, 0, state.time_bind_group, 0, 0); \
uint32_t dynamic_offsets[1] = {0}; \
for (uint64_t i = 0; i < state.settings.num_triangles; ++i) { \
if (state.settings.dynamic_offsets) { \
dynamic_offsets[0] = i * state.aligned_uniform_bytes; \
wgpu##Type##SetBindGroup(rpass_enc, 1, state.dynamic_bind_group, 1, \
dynamic_offsets); \
} \
else { \
wgpu##Type##SetBindGroup(rpass_enc, 1, state.bind_groups[i], 0, 0); \
} \
wgpu##Type##Draw(rpass_enc, 3, 1, 0, 0); \
} \
}
static void init_pipelines(wgpu_context_t* wgpu_context)
{
WGPUShaderModule vert_shader_module
= wgpu_create_shader_module(wgpu_context->device, vertex_shader_wgsl);
WGPUShaderModule frag_shader_module
= wgpu_create_shader_module(wgpu_context->device, fragment_shader_wgsl);
/* Color blend state */
WGPUBlendState blend_state = wgpu_create_blend_state(false);
/* Vertex buffer layout */
const size_t vec4_size = sizeof(vec4);
WGPU_VERTEX_BUFFER_LAYOUT(
animometer, 2 * vec4_size,
/* Attribute location 0: Vertex positions */
WGPU_VERTATTR_DESC(0, WGPUVertexFormat_Float32x4, 0),
/* Attribute location 1: Vertex colors */
WGPU_VERTATTR_DESC(1, WGPUVertexFormat_Float32x4, vec4_size))
WGPURenderPipelineDescriptor rp_desc = {
.label = STRVIEW("Animometer - Render pipeline"),
.vertex = {
.module = vert_shader_module,
.entryPoint = STRVIEW("vert_main"),
.bufferCount = 1,
.buffers = &animometer_vertex_buffer_layout,
},
.fragment = &(WGPUFragmentState) {
.entryPoint = STRVIEW("frag_main"),
.module = frag_shader_module,
.targetCount = 1,
.targets = &(WGPUColorTargetState) {
.format = wgpu_context->render_format,
.blend = &blend_state,
.writeMask = WGPUColorWriteMask_All,
},
},
.primitive = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_None,
},
.multisample = {
.count = 1,
.mask = 0xffffffff
},
};
/* Create render pipelines */
rp_desc.layout = state.pipeline_layout;
state.pipeline
= wgpuDeviceCreateRenderPipeline(wgpu_context->device, &rp_desc);
ASSERT(state.pipeline != NULL);
rp_desc.layout = state.dynamic_pipeline_layout;
state.dynamic_pipeline
= wgpuDeviceCreateRenderPipeline(wgpu_context->device, &rp_desc);
ASSERT(state.dynamic_pipeline != NULL);
/* Partial cleanup */
wgpuShaderModuleRelease(vert_shader_module);
wgpuShaderModuleRelease(frag_shader_module);
}
static void init_render_bundle_encoder(wgpu_context_t* wgpu_context)
{
WGPUTextureFormat color_formats[1] = {wgpu_context->render_format};
WGPURenderBundleEncoder render_bundle_encoder
= wgpuDeviceCreateRenderBundleEncoder(
wgpu_context->device, &(WGPURenderBundleEncoderDescriptor){
.label = STRVIEW("Render bundle encoder"),
.colorFormatCount = 1,
.colorFormats = color_formats,
.sampleCount = 1,
});
RECORD_RENDER_PASS(RenderBundleEncoder, render_bundle_encoder)
state.render_bundle
= wgpuRenderBundleEncoderFinish(render_bundle_encoder, NULL);
WGPU_RELEASE_RESOURCE(RenderBundleEncoder, render_bundle_encoder)
}
static int init(struct wgpu_context_t* wgpu_context)
{
if (wgpu_context) {
stm_setup();
init_vertex_buffer(wgpu_context);
init_pipeline_layouts(wgpu_context);
init_uniform_buffers(wgpu_context);
init_pipelines(wgpu_context);
init_render_bundle_encoder(wgpu_context);
imgui_overlay_init(wgpu_context);
state.initialized = 1;
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
/* Render GUI */
static void render_gui(wgpu_context_t* wgpu_context)
{
const uint64_t now = stm_now();
const float dt_sec = (float)stm_sec(stm_diff(now, state.last_frame_time));
state.last_frame_time = now;
imgui_overlay_new_frame(wgpu_context, dt_sec);
/* Set window position */
igSetNextWindowPos((ImVec2){10.0f, 10.0f}, ImGuiCond_FirstUseEver,
(ImVec2){0.0f, 0.0f});
igBegin("Settings", NULL, ImGuiWindowFlags_AlwaysAutoResize);
/* Simulation settings */
igText("Simulation Settings");
igSeparator();
int num_triangles = (int)state.settings.num_triangles;
if (igSliderInt("numTriangles", &num_triangles, 0, 200000, "%d")) {
/* Note: In the TypeScript version, changing this triggers reconfiguration.
* Here we just update the value - full reconfiguration would require
* recreating bind groups and uniform buffers */
if (num_triangles != (int)state.settings.num_triangles) {
state.settings.num_triangles = (uint64_t)num_triangles;
/* TODO: Implement reconfiguration if needed */
}
}
igCheckbox("renderBundles", (bool*)&state.settings.render_bundles);
igCheckbox("dynamicOffsets", (bool*)&state.settings.dynamic_offsets);
igSpacing();
igSeparator();
/* Performance statistics */
igText("Performance Statistics");
igSeparator();
if (state.js_time_avg > 0.0) {
igText("Avg Javascript: %.2f ms", state.js_time_avg);
igText("Avg Frame: %.2f ms", state.frame_time_avg);
if (state.frame_time_avg > 0.0) {
double fps = 1000.0 / state.frame_time_avg;
igText("FPS: %.1f", fps);
}
}
else {
igText("Performance data collecting...");
}
igEnd();
}
static int frame(struct wgpu_context_t* wgpu_context)
{
if (!state.initialized) {
return EXIT_FAILURE;
}
/* Performance tracking */
const uint64_t frame_start_time = stm_now();
const uint64_t current_timestamp = stm_now();
double frame_time = 0.0;
if (state.previous_frame_timestamp != 0) {
frame_time
= stm_ms(stm_diff(current_timestamp, state.previous_frame_timestamp));
}
state.previous_frame_timestamp = current_timestamp;
/* Update matrix data */
update_uniform_buffers(wgpu_context);
/* Render GUI */
render_gui(wgpu_context);
WGPUDevice device = wgpu_context->device;
WGPUQueue queue = wgpu_context->queue;
state.color_attachment.view = wgpu_context->swapchain_view;
WGPUCommandEncoder cmd_enc = wgpuDeviceCreateCommandEncoder(device, NULL);
{
WGPURenderPassEncoder rpass_enc = wgpuCommandEncoderBeginRenderPass(
cmd_enc, &state.render_pass_descriptor);
if (state.settings.render_bundles) {
const WGPURenderBundle render_bundles[1] = {state.render_bundle};
wgpuRenderPassEncoderExecuteBundles(rpass_enc, 1, render_bundles);
}
else {
RECORD_RENDER_PASS(RenderPassEncoder, rpass_enc)
}
wgpuRenderPassEncoderEnd(rpass_enc);
WGPU_RELEASE_RESOURCE(RenderPassEncoder, rpass_enc)
}
WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(cmd_enc, NULL);
/* Submit and present. */
wgpuQueueSubmit(queue, 1, &cmd_buffer);
/* Render imgui overlay */
imgui_overlay_render(wgpu_context);
/* Cleanup */
wgpuCommandBufferRelease(cmd_buffer);
wgpuCommandEncoderRelease(cmd_enc);
/* Calculate performance metrics */
const double js_time = stm_ms(stm_diff(stm_now(), frame_start_time));
/* Exponential moving average with weight */
const double w = 0.2;
if (state.frame_time_avg == 0.0) {
state.frame_time_avg = frame_time;
}
if (state.js_time_avg == 0.0) {
state.js_time_avg = js_time;
}
state.frame_time_avg = (1.0 - w) * state.frame_time_avg + w * frame_time;
state.js_time_avg = (1.0 - w) * state.js_time_avg + w * js_time;
return EXIT_SUCCESS;
}
static void shutdown(struct wgpu_context_t* wgpu_context)
{
UNUSED_VAR(wgpu_context);
WGPU_RELEASE_RESOURCE(Buffer, state.vertices.buffer)
WGPU_RELEASE_RESOURCE(Buffer, state.uniform_buffer)
WGPU_RELEASE_RESOURCE(PipelineLayout, state.pipeline_layout)
WGPU_RELEASE_RESOURCE(PipelineLayout, state.dynamic_pipeline_layout)
WGPU_RELEASE_RESOURCE(RenderPipeline, state.pipeline)
WGPU_RELEASE_RESOURCE(RenderPipeline, state.dynamic_pipeline)
WGPU_RELEASE_RESOURCE(RenderBundle, state.render_bundle)
WGPU_RELEASE_RESOURCE(BindGroupLayout, state.time_bind_group_layout)
WGPU_RELEASE_RESOURCE(BindGroupLayout, state.bind_group_layout)
WGPU_RELEASE_RESOURCE(BindGroupLayout, state.dynamic_bind_group_layout);
for (uint64_t i = 0; i < state.settings.num_triangles; ++i) {
WGPU_RELEASE_RESOURCE(BindGroup, state.bind_groups[i])
}
free(state.bind_groups);
WGPU_RELEASE_RESOURCE(BindGroup, state.dynamic_bind_group)
WGPU_RELEASE_RESOURCE(BindGroup, state.time_bind_group)
imgui_overlay_shutdown();
}
static void input_event_cb(struct wgpu_context_t* wgpu_context,
const input_event_t* input_event)
{
imgui_overlay_handle_input(wgpu_context, input_event);
}
int main(void)
{
wgpu_start(&(wgpu_desc_t){
.title = "Animometer",
.init_cb = init,
.frame_cb = frame,
.input_event_cb = input_event_cb,
.shutdown_cb = shutdown,
});
return EXIT_SUCCESS;
}
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
// clang-format off
static const char* vertex_shader_wgsl = CODE(
struct Time {
value : f32,
};
struct Uniforms {
scale : f32,
offsetX : f32,
offsetY : f32,
scalar : f32,
scalarOffset : f32,
};
@binding(0) @group(0) var<uniform> time : Time;
@binding(0) @group(1) var<uniform> uniforms : Uniforms;
struct VertexOutput {
@builtin(position) Position : vec4<f32>,
@location(0) v_color : vec4<f32>,
};
@vertex
fn vert_main(
@location(0) position : vec4<f32>,
@location(1) color : vec4<f32>
) -> VertexOutput {
var fade = (uniforms.scalarOffset + time.value * uniforms.scalar / 10.0) % 1.0;
if (fade < 0.5) {
fade = fade * 2.0;
} else {
fade = (1.0 - fade) * 2.0;
}
var xpos = position.x * uniforms.scale;
var ypos = position.y * uniforms.scale;
var angle = 3.14159 * 2.0 * fade;
var xrot = xpos * cos(angle) - ypos * sin(angle);
var yrot = xpos * sin(angle) + ypos * cos(angle);
xpos = xrot + uniforms.offsetX;
ypos = yrot + uniforms.offsetY;
var output : VertexOutput;
output.v_color = vec4(fade, 1.0 - fade, 0.0, 1.0) + color;
output.Position = vec4(xpos, ypos, 0.0, 1.0);
return output;
}
);
static const char* fragment_shader_wgsl = CODE(
@fragment
fn frag_main(@location(0) v_color : vec4<f32>) -> @location(0) vec4<f32> {
return v_color;
}
);
// clang-format on