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
|
// This file is part of the 64k demo project.
// It implements the WebGPU rendering pipeline and shader management.
// Driven by audio peaks for synchronized visual effects.
#include "gpu.h"
#include "platform.h"
#include <GLFW/glfw3.h>
#include <webgpu.h>
#include <wgpu.h>
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <vector>
#ifndef STRIP_ALL
#include <iostream>
#endif
static WGPUInstance g_instance = nullptr;
static WGPUAdapter g_adapter = nullptr;
static WGPUDevice g_device = nullptr;
static WGPUQueue g_queue = nullptr;
static WGPUSurface g_surface = nullptr;
static WGPUSurfaceConfiguration g_config = {};
static WGPURenderPipeline g_render_pipeline = nullptr;
static WGPUBuffer g_uniform_buffer = nullptr;
static WGPUBindGroup g_bind_group = nullptr;
static WGPUStringView label_view(const char *str) {
#ifndef STRIP_ALL
if (!str)
return {nullptr, 0};
return {str, strlen(str)};
#else
(void)str;
return {nullptr, 0};
#endif
}
static WGPUStringView str_view(const char *str) {
if (!str)
return {nullptr, 0};
return {str, strlen(str)};
}
#ifndef STRIP_ALL
static void handle_request_adapter(WGPURequestAdapterStatus status,
WGPUAdapter adapter, WGPUStringView message,
void *userdata1, void *userdata2) {
if (status == WGPURequestAdapterStatus_Success) {
*((WGPUAdapter *)userdata1) = adapter;
} else {
printf("Request adapter failed: %.*s\n", (int)message.length, message.data);
}
}
#else
static void handle_request_adapter(WGPURequestAdapterStatus status,
WGPUAdapter adapter, WGPUStringView message,
void *userdata1, void *userdata2) {
if (status == WGPURequestAdapterStatus_Success) {
*((WGPUAdapter *)userdata1) = adapter;
}
}
#endif
#ifndef STRIP_ALL
static void handle_request_device(WGPURequestDeviceStatus status,
WGPUDevice device, WGPUStringView message,
void *userdata1, void *userdata2) {
if (status == WGPURequestDeviceStatus_Success) {
*((WGPUDevice *)userdata1) = device;
} else {
printf("Request device failed: %.*s\n", (int)message.length, message.data);
}
}
#else
static void handle_request_device(WGPURequestDeviceStatus status,
WGPUDevice device, WGPUStringView message,
void *userdata1, void *userdata2) {
if (status == WGPURequestDeviceStatus_Success) {
*((WGPUDevice *)userdata1) = device;
}
}
#endif
#ifndef STRIP_ALL
static void handle_device_error(WGPUDevice const *device, WGPUErrorType type,
WGPUStringView message, void *userdata1,
void *userdata2) {
printf("WebGPU Error: %.*s\n", (int)message.length, message.data);
}
#endif
const char *shader_wgsl_code = R"(
struct Uniforms {
audio_peak : f32,
aspect_ratio: f32,
};
@group(0) @binding(0) var<uniform> uniforms : Uniforms;
@vertex
fn vs_main(@builtin(vertex_index) vertex_index: u32) -> @builtin(position) vec4<f32> {
let PI = 3.14159265;
let num_sides = 7.0;
let base_scale = 0.5;
let pulse_scale = 0.2 * uniforms.audio_peak;
let scale = base_scale + pulse_scale;
let tri_idx = f32(vertex_index / 3u);
let sub_idx = vertex_index % 3u;
if (sub_idx == 0u) {
return vec4<f32>(0.0, 0.0, 0.0, 1.0);
}
let i = tri_idx + f32(sub_idx - 1u);
let angle = i * 2.0 * PI / num_sides;
let x = scale * cos(angle) / uniforms.aspect_ratio;
let y = scale * sin(angle);
return vec4<f32>(x, y, 0.0, 1.0);
}
@fragment
fn fs_main() -> @location(0) vec4<f32> {
let hue = uniforms.audio_peak * 0.5;
let r = sin(hue + 0.0) * 0.5 + 0.5;
let g = sin(hue + 2.0) * 0.5 + 0.5;
let b = sin(hue + 4.0) * 0.5 + 0.5;
return vec4<f32>(r, g, b, 1.0);
}
)";
void gpu_init(GLFWwindow *window) {
g_instance = wgpuCreateInstance(nullptr);
assert(g_instance);
g_surface = platform_create_wgpu_surface(g_instance);
assert(g_surface);
WGPURequestAdapterOptions adapter_opts = {};
adapter_opts.compatibleSurface = g_surface;
adapter_opts.powerPreference = WGPUPowerPreference_HighPerformance;
wgpuInstanceRequestAdapter(g_instance, &adapter_opts,
{nullptr, WGPUCallbackMode_WaitAnyOnly,
handle_request_adapter, &g_adapter, nullptr});
while (!g_adapter) {
wgpuInstanceWaitAny(g_instance, 0, nullptr, 0);
}
WGPUDeviceDescriptor device_desc = {};
device_desc.label = label_view("Demo Device");
#ifndef STRIP_ALL
device_desc.uncapturedErrorCallbackInfo.callback = handle_device_error;
#endif
wgpuAdapterRequestDevice(g_adapter, &device_desc,
{nullptr, WGPUCallbackMode_WaitAnyOnly,
handle_request_device, &g_device, nullptr});
while (!g_device) {
wgpuInstanceWaitAny(g_instance, 0, nullptr, 0);
}
g_queue = wgpuDeviceGetQueue(g_device);
WGPUSurfaceCapabilities caps = {};
wgpuSurfaceGetCapabilities(g_surface, g_adapter, &caps);
WGPUTextureFormat swap_chain_format = caps.formats[0];
WGPUShaderSourceWGSL wgsl_src = {};
wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL;
wgsl_src.code = str_view(shader_wgsl_code);
WGPUShaderModuleDescriptor shader_module_desc = {};
shader_module_desc.nextInChain = &wgsl_src.chain;
shader_module_desc.label = label_view("Demo Shader");
WGPUShaderModule shader_module =
wgpuDeviceCreateShaderModule(g_device, &shader_module_desc);
WGPUBufferDescriptor uniform_buffer_desc = {};
uniform_buffer_desc.label = label_view("Uniform Buffer");
uniform_buffer_desc.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst;
uniform_buffer_desc.size = sizeof(float) * 2;
g_uniform_buffer = wgpuDeviceCreateBuffer(g_device, &uniform_buffer_desc);
WGPUBindGroupLayoutEntry bgl_entry = {};
bgl_entry.binding = 0;
bgl_entry.visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment;
bgl_entry.buffer.type = WGPUBufferBindingType_Uniform;
bgl_entry.buffer.minBindingSize = sizeof(float) * 2;
WGPUBindGroupLayoutDescriptor bgl_desc = {};
bgl_desc.label = label_view("Uniform Bind Group Layout");
bgl_desc.entryCount = 1;
bgl_desc.entries = &bgl_entry;
WGPUBindGroupLayout bind_group_layout =
wgpuDeviceCreateBindGroupLayout(g_device, &bgl_desc);
WGPUBindGroupEntry bg_entry = {};
bg_entry.binding = 0;
bg_entry.buffer = g_uniform_buffer;
bg_entry.size = sizeof(float) * 2;
WGPUBindGroupDescriptor bg_desc = {};
bg_desc.label = label_view("Uniform Bind Group");
bg_desc.layout = bind_group_layout;
bg_desc.entryCount = 1;
bg_desc.entries = &bg_entry;
g_bind_group = wgpuDeviceCreateBindGroup(g_device, &bg_desc);
WGPUPipelineLayoutDescriptor pl_desc = {};
pl_desc.label = label_view("Render Pipeline Layout");
pl_desc.bindGroupLayoutCount = 1;
pl_desc.bindGroupLayouts = &bind_group_layout;
WGPUPipelineLayout pipeline_layout =
wgpuDeviceCreatePipelineLayout(g_device, &pl_desc);
WGPUColorTargetState color_target = {};
color_target.format = swap_chain_format;
color_target.writeMask = WGPUColorWriteMask_All;
WGPUFragmentState fragment_state = {};
fragment_state.module = shader_module;
fragment_state.entryPoint = str_view("fs_main");
fragment_state.targetCount = 1;
fragment_state.targets = &color_target;
WGPURenderPipelineDescriptor pipeline_desc = {};
pipeline_desc.label = label_view("Render Pipeline");
pipeline_desc.layout = pipeline_layout;
pipeline_desc.vertex.module = shader_module;
pipeline_desc.vertex.entryPoint = str_view("vs_main");
pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList;
pipeline_desc.multisample.count = 1;
pipeline_desc.multisample.mask = 0xFFFFFFFF;
pipeline_desc.fragment = &fragment_state;
g_render_pipeline = wgpuDeviceCreateRenderPipeline(g_device, &pipeline_desc);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
g_config.device = g_device;
g_config.format = swap_chain_format;
g_config.usage = WGPUTextureUsage_RenderAttachment;
g_config.width = width;
g_config.height = height;
g_config.presentMode = WGPUPresentMode_Fifo;
g_config.alphaMode = WGPUCompositeAlphaMode_Opaque;
wgpuSurfaceConfigure(g_surface, &g_config);
}
void gpu_draw(float audio_peak, float aspect_ratio) {
WGPUSurfaceTexture surface_texture;
wgpuSurfaceGetCurrentTexture(g_surface, &surface_texture);
if (surface_texture.status !=
WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal &&
surface_texture.status !=
WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal)
return;
WGPUTextureView view =
wgpuTextureCreateView(surface_texture.texture, nullptr);
struct {
float audio_peak;
float aspect_ratio;
} uniforms = {audio_peak, aspect_ratio};
wgpuQueueWriteBuffer(g_queue, g_uniform_buffer, 0, &uniforms,
sizeof(uniforms));
WGPUCommandEncoderDescriptor encoder_desc = {};
encoder_desc.label = label_view("Command Encoder");
WGPUCommandEncoder encoder =
wgpuDeviceCreateCommandEncoder(g_device, &encoder_desc);
WGPURenderPassColorAttachment color_attachment = {};
color_attachment.view = view;
color_attachment.resolveTarget = nullptr;
color_attachment.loadOp = WGPULoadOp_Clear;
color_attachment.storeOp = WGPUStoreOp_Store;
color_attachment.clearValue = {0.1, 0.2, 0.3, 1.0};
color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
WGPURenderPassDescriptor render_pass_desc = {};
render_pass_desc.label = label_view("Render Pass");
render_pass_desc.colorAttachmentCount = 1;
render_pass_desc.colorAttachments = &color_attachment;
WGPURenderPassEncoder pass =
wgpuCommandEncoderBeginRenderPass(encoder, &render_pass_desc);
wgpuRenderPassEncoderSetPipeline(pass, g_render_pipeline);
wgpuRenderPassEncoderSetBindGroup(pass, 0, g_bind_group, 0, nullptr);
// Drawing a heptagon with TriangleList: 7 triangles * 3 vertices = 21
wgpuRenderPassEncoderDraw(pass, 21, 1, 0, 0);
wgpuRenderPassEncoderEnd(pass);
WGPUCommandBufferDescriptor cmd_desc = {};
cmd_desc.label = label_view("Command Buffer");
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, &cmd_desc);
wgpuQueueSubmit(g_queue, 1, &commands);
wgpuSurfacePresent(g_surface);
wgpuTextureViewRelease(view);
wgpuTextureRelease(surface_texture.texture);
}
void gpu_shutdown() {
}
|