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
|
// This file is part of the 64k demo project.
// It implements the ParticlesEffect.
#include "effects/particles_effect.h"
#include "gpu/gpu.h"
#include "gpu/shaders.h"
#include <vector>
ParticlesEffect::ParticlesEffect(const GpuContext& ctx,
const std::vector<std::string>& inputs,
const std::vector<std::string>& outputs)
: Effect(ctx, inputs, outputs) {
// Initialize uniforms
uniforms_.init(ctx_.device);
// Initialize particles buffer
std::vector<Particle> init_p(NUM_PARTICLES);
for (int i = 0; i < NUM_PARTICLES; ++i) {
float x = (float)(i % 100) / 50.0f - 1.0f;
float y = (float)(i / 100) / 100.0f * 3.0f - 1.5f;
init_p[i].pos[0] = x;
init_p[i].pos[1] = y;
init_p[i].pos[2] = ((float)i / NUM_PARTICLES) * 0.5f;
init_p[i].pos[3] = 1.0f;
init_p[i].vel[0] = 0.0f;
init_p[i].vel[1] = 0.0f;
init_p[i].vel[2] = 0.0f;
init_p[i].vel[3] = 0.0f;
init_p[i].rot[0] = 0.0f;
init_p[i].rot[1] = ((float)(i % 7) / 7.0f) * 3.14159f;
init_p[i].rot[2] = 0.0f;
init_p[i].rot[3] = 0.0f;
float hue = (float)(i % 100) / 100.0f;
init_p[i].color[0] = 0.5f + 0.5f * hue;
init_p[i].color[1] = 0.3f + 0.7f * (1.0f - hue);
init_p[i].color[2] = 0.8f;
init_p[i].color[3] = 0.8f;
}
particles_buffer_ = gpu_create_buffer(
ctx_.device, sizeof(Particle) * NUM_PARTICLES,
WGPUBufferUsage_Storage | WGPUBufferUsage_Vertex, init_p.data());
// Create compute shader (particle simulation)
ResourceBinding compute_bindings[] = {
{particles_buffer_, WGPUBufferBindingType_Storage},
{uniforms_.get(), WGPUBufferBindingType_Uniform}};
compute_pass_ = gpu_create_compute_pass(ctx_.device, particle_compute_v2_wgsl,
compute_bindings, 2);
compute_pass_.workgroup_size_x = (NUM_PARTICLES + 63) / 64;
// Create render shader (particle rendering)
ResourceBinding render_bindings[] = {
{particles_buffer_, WGPUBufferBindingType_ReadOnlyStorage},
{uniforms_.get(), WGPUBufferBindingType_Uniform}};
render_pass_ = gpu_create_render_pass(ctx_.device, WGPUTextureFormat_RGBA8Unorm,
particle_render_v2_wgsl, render_bindings, 2);
render_pass_.vertex_count = 6;
render_pass_.instance_count = NUM_PARTICLES;
}
void ParticlesEffect::render(WGPUCommandEncoder encoder,
const UniformsSequenceParams& params,
NodeRegistry& nodes) {
// Update uniforms
uniforms_.update(ctx_.queue, params);
// Run compute pass (particle simulation)
WGPUComputePassEncoder compute = wgpuCommandEncoderBeginComputePass(encoder, nullptr);
wgpuComputePassEncoderSetPipeline(compute, compute_pass_.pipeline);
wgpuComputePassEncoderSetBindGroup(compute, 0, compute_pass_.bind_group, 0, nullptr);
wgpuComputePassEncoderDispatchWorkgroups(compute, compute_pass_.workgroup_size_x, 1, 1);
wgpuComputePassEncoderEnd(compute);
// Run render pass (draw particles to output)
WGPUTextureView output_view = nodes.get_view(output_nodes_[0]);
WGPURenderPassColorAttachment color_attachment = {
.view = output_view,
#if !defined(DEMO_CROSS_COMPILE_WIN32)
.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED,
#endif
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = {0.0, 0.0, 0.0, 1.0}};
WGPURenderPassDescriptor render_desc = {
.colorAttachmentCount = 1,
.colorAttachments = &color_attachment};
WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &render_desc);
wgpuRenderPassEncoderSetPipeline(pass, render_pass_.pipeline);
wgpuRenderPassEncoderSetBindGroup(pass, 0, render_pass_.bind_group, 0, nullptr);
wgpuRenderPassEncoderDraw(pass, render_pass_.vertex_count, render_pass_.instance_count, 0, 0);
wgpuRenderPassEncoderEnd(pass);
}
|