-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsampler_parameters.c
829 lines (736 loc) · 27 KB
/
sampler_parameters.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
#include "example_base.h"
#include <string.h>
#include "../webgpu/imgui_overlay.h"
/* -------------------------------------------------------------------------- *
* WebGPU Example - Sampler Parameters
*
* Visualizes what all the sampler parameters do. Shows a textured plane at
* various scales (rotated, head-on, in perspective, and in vanishing
* perspective). The bottom-right view shows the raw contents of the 4 mipmap
* levels of the test texture (16x16, 8x8, 4x4, and 2x2).
*
* Ref:
* https://github.com/webgpu/webgpu-samples/tree/main/src/sample/samplerParameters
* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
static const char* show_texture_wgsl;
static const char* textured_square_wgsl;
/* -------------------------------------------------------------------------- *
* Sampler Parameters example
* -------------------------------------------------------------------------- */
#define CANVAS_SIZE 600u
static struct {
float flange_log_size;
bool highlight_flange;
float animation;
} config = {
.flange_log_size = 1.0f,
.highlight_flange = false,
.animation = 0.1f,
};
// Uniform buffer
static wgpu_buffer_t buf_config = {0};
// Storage buffer
static wgpu_buffer_t buf_matrices = {0};
// Checkerboard texture
static texture_t checkerboard = {0};
// Render pipelines
static WGPURenderPipeline show_texture_render_pipeline = NULL;
static WGPURenderPipeline textured_square_render_pipeline = NULL;
// Bind groups
static WGPUBindGroup show_texture_bind_group = NULL;
static WGPUBindGroup textured_square_bind_group = NULL;
// Render pass descriptor for frame buffer writes
static struct {
WGPURenderPassColorAttachment color_attachments[1];
WGPURenderPassDescriptor descriptor;
} render_pass = {0};
// Viewport properties
static uint32_t texture_base_size = 0;
static uint32_t viewport_size = 0;
static uint32_t viewport_grid_size = 0;
static uint32_t viewport_grid_stride = 0;
// Other variables
static const char* example_title = "Sampler Parameters";
static bool prepared = false;
//
// GUI controls
//
static WGPUSamplerDescriptor sampler_descriptor = {
.label = "Checkerboard texture sampler",
.addressModeU = WGPUAddressMode_ClampToEdge,
.addressModeV = WGPUAddressMode_ClampToEdge,
.addressModeW = WGPUAddressMode_ClampToEdge,
.magFilter = WGPUFilterMode_Linear,
.minFilter = WGPUFilterMode_Linear,
.mipmapFilter = WGPUMipmapFilterMode_Linear,
.lodMinClamp = 0.0f,
.lodMaxClamp = 4.0f,
.maxAnisotropy = 1,
};
typedef enum preset_enum_t {
Preset_Initial = 0,
Preset_Checkerboard = 1,
Preset_Smooth = 2,
Preset_Crunchy = 3,
Preset_Count = 4,
} preset_enum_t;
static WGPUSamplerDescriptor presets[4] = {
[Preset_Checkerboard] = {
.addressModeU = WGPUAddressMode_Repeat,
.addressModeV = WGPUAddressMode_Repeat,
},
};
static void prepare_canvas(void)
{
const uint32_t canvas_size = CANVAS_SIZE;
viewport_grid_size = 4;
viewport_grid_stride = floor(canvas_size / (float)viewport_grid_size);
viewport_size = viewport_grid_stride - 2;
}
static void prepare_uniform_buffer(wgpu_context_t* wgpu_context)
{
/* Create uniform buffer */
buf_config = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Uniform bufer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = 128,
});
ASSERT(buf_config.buffer != NULL);
/* View-projection matrix set up so it doesn't transform anything at z=0. */
const uint32_t camera_dist = 3;
mat4 view_proj = GLM_MAT4_IDENTITY_INIT;
glm_perspective(2.0f * atan(1.0f / camera_dist), 1, 0.1f, 100.0f, view_proj);
glm_translate(view_proj, (vec3){0.0f, 0.0f, -camera_dist});
/* Update uniform buffer data */
wgpu_queue_write_buffer(wgpu_context, buf_config.buffer, 0, view_proj,
sizeof(mat4));
}
static void update_config_buffer(wgpu_example_context_t* context)
{
const float t = (context->frame.timestamp_millis / 1000.0f) * 0.5f;
const float data[4] = {
cos(t) * config.animation, //
sin(t) * config.animation, //
(pow(2, config.flange_log_size) - 1) / 2.0f, //
config.highlight_flange, //
};
wgpu_queue_write_buffer(context->wgpu_context, buf_config.buffer, 64,
&data[0], sizeof(data));
}
static void prepare_storage_buffer(wgpu_context_t* wgpu_context)
{
mat4 matrices[15] = {0};
/* Row 1: Scale by 2 */
{
/* Column 1 */
glm_mat4_identity(matrices[0]);
glm_rotate_z(matrices[0], PI / 16, matrices[0]);
glm_scale(matrices[0], (vec3){2, 2, 1});
/* Column 2 */
glm_mat4_identity(matrices[1]);
glm_scale(matrices[1], (vec3){2, 2, 1});
/* Column 3 */
glm_mat4_identity(matrices[2]);
glm_rotate_x(matrices[2], -PI * 0.3f, matrices[2]);
glm_scale(matrices[2], (vec3){2, 2, 1});
/* Column 4 */
glm_mat4_identity(matrices[3]);
glm_rotate_x(matrices[3], -PI * 0.42f, matrices[3]);
glm_scale(matrices[3], (vec3){2, 2, 1});
}
/* Row 2: Scale by 1 */
{
/* Column 1 */
glm_mat4_identity(matrices[4]);
glm_rotate_z(matrices[4], PI / 16, matrices[4]);
/* Column 2 */
glm_mat4_identity(matrices[5]);
/* Column 3 */
glm_mat4_identity(matrices[6]);
glm_rotate_x(matrices[6], -PI * 0.3f, matrices[6]);
/* Column 4 */
glm_mat4_identity(matrices[7]);
glm_rotate_x(matrices[7], -PI * 0.42f, matrices[7]);
}
/* Row 3: Scale by 0.9 */
{
/* Column 1 */
glm_mat4_identity(matrices[8]);
glm_rotate_z(matrices[8], PI / 16, matrices[8]);
glm_scale(matrices[8], (vec3){0.9f, 0.9f, 1.0f});
/* Column 2 */
glm_mat4_identity(matrices[9]);
glm_scale(matrices[9], (vec3){0.9f, 0.9f, 1});
/* Column 3 */
glm_mat4_identity(matrices[10]);
glm_rotate_x(matrices[10], -PI * 0.3f, matrices[10]);
glm_scale(matrices[10], (vec3){0.9f, 0.9f, 1.0f});
/* Column 4 */
glm_mat4_identity(matrices[11]);
glm_rotate_x(matrices[11], -PI * 0.42f, matrices[11]);
glm_scale(matrices[11], (vec3){0.9f, 0.9f, 1.0f});
}
/* Row 4: Scale by 0.3 */
{
/* Column 1 */
glm_mat4_identity(matrices[12]);
glm_rotate_z(matrices[12], PI / 16, matrices[12]);
glm_scale(matrices[12], (vec3){0.3f, 0.3f, 1.0f});
/* Column 2 */
glm_mat4_identity(matrices[13]);
glm_scale(matrices[13], (vec3){0.3f, 0.3f, 1.0f});
/* Column 3 */
glm_mat4_identity(matrices[14]);
glm_rotate_x(matrices[14], -PI * 0.3f, matrices[14]);
glm_scale(matrices[14], (vec3){0.3f, 0.3f, 1.0f});
}
buf_matrices = wgpu_create_buffer(
wgpu_context, &(wgpu_buffer_desc_t){
.label = "Matrices buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Storage,
.size = sizeof(matrices),
.initial.data = matrices,
});
}
//
// Initialize test texture
//
// Set up a texture with 4 mip levels, each containing a differently-colored
// checkerboard with 1x1 pixels (so when rendered the checkerboards are
// different sizes). This is different from a normal mipmap where each level
// would look like a lower-resolution version of the previous one.
// Level 0 is 16x16 white/black
// Level 1 is 8x8 blue/black
// Level 2 is 4x4 yellow/black
// Level 3 is 2x2 pink/black
static void initialize_test_texture(wgpu_context_t* wgpu_context)
{
const uint32_t texture_mip_levels = 4;
texture_base_size = 16;
/* Checkerboard texture */
WGPUTextureDescriptor texture_desc = {
.label = "Checkerboard texture",
.usage = WGPUTextureUsage_CopyDst | WGPUTextureUsage_TextureBinding,
.format = WGPUTextureFormat_RGBA8Unorm,
.dimension = WGPUTextureDimension_2D,
.mipLevelCount = texture_mip_levels,
.sampleCount = 1,
.size = (WGPUExtent3D) {
.width = texture_base_size,
.height = texture_base_size,
.depthOrArrayLayers = 1,
},
};
checkerboard.texture
= wgpuDeviceCreateTexture(wgpu_context->device, &texture_desc);
ASSERT(checkerboard.texture != NULL);
/* Checkerboard texture view */
WGPUTextureViewDescriptor texture_view_dec = {
.label = "Checkerboard texture view",
.format = texture_desc.format,
.dimension = WGPUTextureViewDimension_2D,
.baseMipLevel = 0,
.mipLevelCount = texture_desc.mipLevelCount,
.baseArrayLayer = 0,
.arrayLayerCount = 1,
.aspect = WGPUTextureAspect_All,
};
checkerboard.view
= wgpuTextureCreateView(checkerboard.texture, &texture_view_dec);
ASSERT(checkerboard.view != NULL);
/* Checkerboard texture data */
const uint8_t color_for_level[4][4] = {
{255, 255, 255, 255}, /* */
{30, 136, 229, 255}, /* blue */
{255, 193, 7, 255}, /* yellow */
{216, 27, 96, 255}, /* pink */
};
const uint8_t color_black[4] = {0, 0, 0, 255};
uint32_t index = 0;
for (uint32_t mip_level = 0; mip_level < texture_mip_levels; ++mip_level) {
/* Sizes: 16, 8, 4, 2 */
const uint32_t size = pow(2, texture_mip_levels - mip_level);
const uint32_t data_size = size * size * 4 * sizeof(uint8_t);
uint8_t* data = (uint8_t*)malloc(data_size);
memset(data, 0, sizeof(data_size));
for (uint32_t y = 0; y < size; ++y) {
for (uint32_t x = 0; x < size; ++x) {
index = (y * size + x) * 4;
for (uint8_t c = 0; c < 4; ++c) {
data[index + c]
= (x + y) % 2 ? color_for_level[mip_level][c] : color_black[c];
}
}
}
wgpuQueueWriteTexture(wgpu_context->queue,
&(WGPUImageCopyTexture) {
.texture = checkerboard.texture,
.mipLevel = mip_level,
.origin = (WGPUOrigin3D) {
.x = 0,
.y = 0,
.z = 0,
},
.aspect = WGPUTextureAspect_All,
},
data, data_size,
&(WGPUTextureDataLayout){
.offset = 0,
.bytesPerRow = size * 4,
.rowsPerImage = size,
},
&(WGPUExtent3D){
.width = size,
.height = size,
.depthOrArrayLayers = 1,
});
free(data);
}
}
static void update_textured_square_sampler(wgpu_context_t* wgpu_context)
{
/* Destroy current sampler */
WGPU_RELEASE_RESOURCE(Sampler, checkerboard.sampler)
/* Create sampler */
sampler_descriptor.maxAnisotropy
= (sampler_descriptor.minFilter == WGPUFilterMode_Linear
&& sampler_descriptor.magFilter == WGPUFilterMode_Linear
&& sampler_descriptor.minFilter == WGPUFilterMode_Linear) ?
sampler_descriptor.maxAnisotropy :
1;
checkerboard.sampler
= wgpuDeviceCreateSampler(wgpu_context->device, &sampler_descriptor);
ASSERT(checkerboard.sampler != NULL);
}
static void update_textured_square_bind_group(wgpu_context_t* wgpu_context)
{
/* Destroy current bind group */
WGPU_RELEASE_RESOURCE(BindGroup, textured_square_bind_group)
/* Create bind group */
WGPUBindGroupEntry bg_entries[4] = {
[0] = (WGPUBindGroupEntry) {
.binding = 0,
.buffer = buf_config.buffer,
.size = buf_config.size,
},
[1] = (WGPUBindGroupEntry) {
.binding = 1,
.buffer = buf_matrices.buffer,
.size = buf_matrices.size,
},
[2] = (WGPUBindGroupEntry) {
.binding = 2,
.sampler = checkerboard.sampler,
},
[3] = (WGPUBindGroupEntry) {
.binding = 3,
.textureView = checkerboard.view
},
};
textured_square_bind_group = wgpuDeviceCreateBindGroup(
wgpu_context->device, &(WGPUBindGroupDescriptor){
.label = "Textured square bind group",
.layout = wgpuRenderPipelineGetBindGroupLayout(
textured_square_render_pipeline, 0),
.entryCount = (uint32_t)ARRAY_SIZE(bg_entries),
.entries = bg_entries,
});
ASSERT(textured_square_bind_group != NULL);
}
static void setup_render_pass(void)
{
/* Color attachment */
render_pass.color_attachments[0] = (WGPURenderPassColorAttachment) {
.view = NULL, /* Assigned later */
.depthSlice = ~0,
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = (WGPUColor) {
.r = 0.2f,
.g = 0.2f,
.b = 0.2f,
.a = 1.0f,
},
};
/* Render pass descriptor */
render_pass.descriptor = (WGPURenderPassDescriptor){
.label = "Render pass descriptor",
.colorAttachmentCount = 1,
.colorAttachments = render_pass.color_attachments,
.depthStencilAttachment = NULL,
};
}
//
// "Debug" view of the actual texture contents
//
static void prepare_debug_view_render_pipeline(wgpu_context_t* wgpu_context)
{
/* Primitive state */
WGPUPrimitiveState primitive_state = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_None,
};
/* Color target state */
WGPUBlendState blend_state = wgpu_create_blend_state(true);
WGPUColorTargetState color_target_state = (WGPUColorTargetState){
.format = wgpu_context->swap_chain.format,
.blend = &blend_state,
.writeMask = WGPUColorWriteMask_All,
};
/* Vertex state */
WGPUVertexState vertex_state = wgpu_create_vertex_state(
wgpu_context, &(wgpu_vertex_state_t){
.shader_desc = (wgpu_shader_desc_t){
// Vertex shader WGSL
.label = "Debug view vertex shader WGSL",
.wgsl_code.source = show_texture_wgsl,
.entry = "vmain"
},
});
/* Fragment state */
WGPUFragmentState fragment_state = wgpu_create_fragment_state(
wgpu_context, &(wgpu_fragment_state_t){
.shader_desc = (wgpu_shader_desc_t){
// Fragment shader WGSL
.label = "Debug view fragment shader WGSL",
.wgsl_code.source = show_texture_wgsl,
.entry = "fmain"
},
.target_count = 1,
.targets = &color_target_state,
});
// Multisample state */
WGPUMultisampleState multisample_state
= wgpu_create_multisample_state_descriptor(
&(create_multisample_state_desc_t){
.sample_count = 1,
});
/* Create rendering pipeline using the specified states */
show_texture_render_pipeline = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Show texture render pipeline",
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.multisample = multisample_state,
});
ASSERT(show_texture_render_pipeline != NULL);
/* Partial cleanup */
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static void prepare_show_texture_bind_group(wgpu_context_t* wgpu_context)
{
WGPUBindGroupEntry bg_entry = (WGPUBindGroupEntry){
.binding = 0,
.textureView = checkerboard.view,
};
show_texture_bind_group = wgpuDeviceCreateBindGroup(
wgpu_context->device, &(WGPUBindGroupDescriptor){
.label = "Show texture bind group",
.layout = wgpuRenderPipelineGetBindGroupLayout(
show_texture_render_pipeline, 0),
.entryCount = 1,
.entries = &bg_entry,
});
ASSERT(show_texture_bind_group != NULL);
}
//
// Pipeline for drawing the test squares
//
static void
prepare_textured_square_render_pipeline(wgpu_context_t* wgpu_context)
{
// Primitive state
WGPUPrimitiveState primitive_state = {
.topology = WGPUPrimitiveTopology_TriangleList,
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_None,
};
// Color target state
WGPUBlendState blend_state = wgpu_create_blend_state(true);
WGPUColorTargetState color_target_state = (WGPUColorTargetState){
.format = wgpu_context->swap_chain.format,
.blend = &blend_state,
.writeMask = WGPUColorWriteMask_All,
};
// Constants
WGPUConstantEntry constant_entries[2] = {
[0] = (WGPUConstantEntry){
.key = "kTextureBaseSize",
.value = (double)texture_base_size,
},
[1] = (WGPUConstantEntry){
.key = "kViewportSize",
.value =(double)viewport_size,
},
};
// Vertex state
WGPUVertexState vertex_state = wgpu_create_vertex_state(
wgpu_context, &(wgpu_vertex_state_t){
.shader_desc = (wgpu_shader_desc_t){
// Vertex shader WGSL
.label = "Textured square vertex shader WGSL",
.wgsl_code.source = textured_square_wgsl,
.entry = "vmain"
},
.constant_count = (uint32_t)ARRAY_SIZE(constant_entries),
.constants = constant_entries,
});
// Fragment state
WGPUFragmentState fragment_state = wgpu_create_fragment_state(
wgpu_context, &(wgpu_fragment_state_t){
.shader_desc = (wgpu_shader_desc_t){
// Fragment shader WGSL
.label = "Textured square fragment shader WGSL",
.wgsl_code.source = textured_square_wgsl,
.entry = "fmain"
},
.target_count = 1,
.targets = &color_target_state,
});
// Multisample state
WGPUMultisampleState multisample_state
= wgpu_create_multisample_state_descriptor(
&(create_multisample_state_desc_t){
.sample_count = 1,
});
// Create rendering pipeline using the specified states
textured_square_render_pipeline = wgpuDeviceCreateRenderPipeline(
wgpu_context->device, &(WGPURenderPipelineDescriptor){
.label = "Textured square render pipeline",
.primitive = primitive_state,
.vertex = vertex_state,
.fragment = &fragment_state,
.multisample = multisample_state,
});
ASSERT(textured_square_render_pipeline != NULL);
// Partial cleanup
WGPU_RELEASE_RESOURCE(ShaderModule, vertex_state.module);
WGPU_RELEASE_RESOURCE(ShaderModule, fragment_state.module);
}
static void example_on_update_ui_overlay(wgpu_example_context_t* context)
{
if (imgui_overlay_header("Settings")) {
imgui_overlay_checkBox(context->imgui_overlay, "Paused", &context->paused);
}
}
static WGPUCommandBuffer build_command_buffer(wgpu_context_t* wgpu_context)
{
/* Set target frame buffer */
render_pass.color_attachments[0].view = wgpu_context->swap_chain.frame_buffer;
wgpu_context->cmd_enc
= wgpuDeviceCreateCommandEncoder(wgpu_context->device, NULL);
wgpu_context->rpass_enc = wgpuCommandEncoderBeginRenderPass(
wgpu_context->cmd_enc, &render_pass.descriptor);
/* Draw test squares */
{
wgpuRenderPassEncoderSetPipeline(wgpu_context->rpass_enc,
textured_square_render_pipeline);
wgpuRenderPassEncoderSetBindGroup(wgpu_context->rpass_enc, 0,
textured_square_bind_group, 0, 0);
for (uint32_t i = 0; i < pow(viewport_grid_size, 2) - 1; ++i) {
const uint32_t vp_x = viewport_grid_stride * (i % viewport_grid_size) + 1;
const uint32_t vp_y
= viewport_grid_stride * floor(i / (float)viewport_grid_size) + 1;
wgpuRenderPassEncoderSetViewport(wgpu_context->rpass_enc, vp_x, vp_y,
viewport_size, viewport_size, 0.0f,
1.0f);
wgpuRenderPassEncoderDraw(wgpu_context->rpass_enc, 6, 1, 0, i);
}
}
/* Show texture contents */
{
wgpuRenderPassEncoderSetPipeline(wgpu_context->rpass_enc,
show_texture_render_pipeline);
wgpuRenderPassEncoderSetBindGroup(wgpu_context->rpass_enc, 0,
show_texture_bind_group, 0, 0);
const uint32_t last_viewport
= (viewport_grid_size - 1) * viewport_grid_stride + 1;
wgpuRenderPassEncoderSetViewport(wgpu_context->rpass_enc, last_viewport,
last_viewport, 32, 32, 0.0f, 1.0f);
wgpuRenderPassEncoderDraw(wgpu_context->rpass_enc, 6, 1, 0, 0);
wgpuRenderPassEncoderSetViewport(wgpu_context->rpass_enc,
last_viewport + 32, last_viewport, 16, 16,
0.0f, 1.0f);
wgpuRenderPassEncoderDraw(wgpu_context->rpass_enc, 6, 1, 0, 1);
wgpuRenderPassEncoderSetViewport(wgpu_context->rpass_enc,
last_viewport + 32, last_viewport + 16, 8,
8, 0.0f, 1.0f);
wgpuRenderPassEncoderDraw(wgpu_context->rpass_enc, 6, 1, 0, 2);
wgpuRenderPassEncoderSetViewport(wgpu_context->rpass_enc,
last_viewport + 32, last_viewport + 24, 4,
4, 0.0f, 1.0f);
wgpuRenderPassEncoderDraw(wgpu_context->rpass_enc, 6, 1, 0, 3);
}
/* End render pass */
wgpuRenderPassEncoderEnd(wgpu_context->rpass_enc);
WGPU_RELEASE_RESOURCE(RenderPassEncoder, wgpu_context->rpass_enc)
/* Draw ui overlay */
draw_ui(wgpu_context->context, example_on_update_ui_overlay);
/* Get command buffer */
WGPUCommandBuffer command_buffer
= wgpu_get_command_buffer(wgpu_context->cmd_enc);
WGPU_RELEASE_RESOURCE(CommandEncoder, wgpu_context->cmd_enc)
return command_buffer;
}
static int example_initialize(wgpu_example_context_t* context)
{
if (context) {
prepare_canvas();
initialize_test_texture(context->wgpu_context);
prepare_debug_view_render_pipeline(context->wgpu_context);
prepare_show_texture_bind_group(context->wgpu_context);
prepare_textured_square_render_pipeline(context->wgpu_context);
prepare_uniform_buffer(context->wgpu_context);
prepare_storage_buffer(context->wgpu_context);
update_config_buffer(context);
update_textured_square_sampler(context->wgpu_context);
update_textured_square_bind_group(context->wgpu_context);
setup_render_pass();
prepared = true;
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
static int example_draw(wgpu_context_t* wgpu_context)
{
// Get next image in the swap chain (back/front buffer)
wgpu_swap_chain_get_current_image(wgpu_context);
// Create command buffer
WGPUCommandBuffer command_buffer = build_command_buffer(wgpu_context);
ASSERT(command_buffer != NULL);
// Submit command buffer to the queue
wgpu_flush_command_buffers(wgpu_context, &command_buffer, 1);
// Present the current buffer to the swap chain
wgpu_swap_chain_present(wgpu_context);
return 0;
}
static int example_render(wgpu_example_context_t* context)
{
if (!prepared) {
return EXIT_FAILURE;
}
if (!context->paused) {
update_config_buffer(context);
}
return example_draw(context->wgpu_context);
}
// Clean up used resources
static void example_destroy(wgpu_example_context_t* context)
{
UNUSED_VAR(context);
wgpu_destroy_texture(&checkerboard);
WGPU_RELEASE_RESOURCE(Buffer, buf_config.buffer)
WGPU_RELEASE_RESOURCE(Buffer, buf_matrices.buffer)
WGPU_RELEASE_RESOURCE(RenderPipeline, show_texture_render_pipeline)
WGPU_RELEASE_RESOURCE(RenderPipeline, textured_square_render_pipeline)
WGPU_RELEASE_RESOURCE(BindGroup, show_texture_bind_group)
WGPU_RELEASE_RESOURCE(BindGroup, textured_square_bind_group)
}
void example_sampler_parameters(int argc, char* argv[])
{
// clang-format off
example_run(argc, argv, &(refexport_t){
.example_settings = (wgpu_example_settings_t){
.title = example_title,
.overlay = true,
},
.example_window_config = (window_config_t){
.width = CANVAS_SIZE,
.height = CANVAS_SIZE,
},
.example_initialize_func = &example_initialize,
.example_render_func = &example_render,
.example_destroy_func = &example_destroy
});
// clang-format on
}
/* -------------------------------------------------------------------------- *
* WGSL Shaders
* -------------------------------------------------------------------------- */
// clang-format off
static const char* show_texture_wgsl = CODE(
@group(0) @binding(0) var tex: texture_2d<f32>;
struct Varying {
@builtin(position) pos: vec4f,
@location(0) texelCoord: vec2f,
@location(1) mipLevel: f32,
}
const kMipLevels = 4;
const baseMipSize: u32 = 16;
@vertex
fn vmain(
@builtin(instance_index) instance_index: u32, // used as mipLevel
@builtin(vertex_index) vertex_index: u32,
) -> Varying {
var square = array(
vec2f(0, 0), vec2f(0, 1), vec2f(1, 0),
vec2f(1, 0), vec2f(0, 1), vec2f(1, 1),
);
let uv = square[vertex_index];
let pos = vec4(uv * 2 - vec2(1, 1), 0.0, 1.0);
let mipLevel = instance_index;
let mipSize = f32(1 << (kMipLevels - mipLevel));
let texelCoord = uv * mipSize;
return Varying(pos, texelCoord, f32(mipLevel));
}
@fragment
fn fmain(vary: Varying) -> @location(0) vec4f {
return textureLoad(tex, vec2u(vary.texelCoord), u32(vary.mipLevel));
}
);
static const char* textured_square_wgsl = CODE(
struct Config {
viewProj: mat4x4f,
animationOffset: vec2f,
flangeSize: f32,
highlightFlange: f32,
};
@group(0) @binding(0) var<uniform> config: Config;
@group(0) @binding(1) var<storage, read> matrices: array<mat4x4f>;
@group(0) @binding(2) var samp: sampler;
@group(0) @binding(3) var tex: texture_2d<f32>;
struct Varying {
@builtin(position) pos: vec4f,
@location(0) uv: vec2f,
}
override kTextureBaseSize: f32;
override kViewportSize: f32;
@vertex
fn vmain(
@builtin(instance_index) instance_index: u32,
@builtin(vertex_index) vertex_index: u32,
) -> Varying {
let flange = config.flangeSize;
var uvs = array(
vec2(-flange, -flange), vec2(-flange, 1 + flange), vec2(1 + flange, -flange),
vec2(1 + flange, -flange), vec2(-flange, 1 + flange), vec2(1 + flange, 1 + flange),
);
// Default size (if matrix is the identity) makes 1 texel = 1 pixel.
let radius = (1 + 2 * flange) * kTextureBaseSize / kViewportSize;
var positions = array(
vec2(-radius, -radius), vec2(-radius, radius), vec2(radius, -radius),
vec2(radius, -radius), vec2(-radius, radius), vec2(radius, radius),
);
let modelMatrix = matrices[instance_index];
let pos = config.viewProj * modelMatrix * vec4f(positions[vertex_index] + config.animationOffset, 0, 1);
return Varying(pos, uvs[vertex_index]);
}
@fragment
fn fmain(vary: Varying) -> @location(0) vec4f {
let uv = vary.uv;
var color = textureSample(tex, samp, uv);
let outOfBounds = uv.x < 0 || uv.x > 1 || uv.y < 0 || uv.y > 1;
if config.highlightFlange > 0 && outOfBounds {
color += vec4(0.7, 0, 0, 0);
}
return color;
}
);
// clang-format on