-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputeBoids.cpp
326 lines (269 loc) · 10.6 KB
/
ComputeBoids.cpp
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
// Copyright 2017 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "utils/SampleUtils.h"
#include "utils/ComboRenderPipelineDescriptor.h"
#include "utils/DawnHelpers.h"
#include "utils/SystemUtils.h"
#include <array>
#include <cstring>
#include <random>
#include <glm/glm.hpp>
dawn::Device device;
dawn::Queue queue;
dawn::SwapChain swapchain;
dawn::TextureView depthStencilView;
dawn::Buffer modelBuffer;
std::array<dawn::Buffer, 2> particleBuffers;
dawn::RenderPipeline renderPipeline;
dawn::Buffer updateParams;
dawn::ComputePipeline updatePipeline;
std::array<dawn::BindGroup, 2> updateBGs;
size_t pingpong = 0;
static const uint32_t kNumParticles = 1000;
struct Particle {
glm::vec2 pos;
glm::vec2 vel;
};
struct SimParams {
float deltaT;
float rule1Distance;
float rule2Distance;
float rule3Distance;
float rule1Scale;
float rule2Scale;
float rule3Scale;
int particleCount;
};
void initBuffers() {
glm::vec2 model[3] = {
{-0.01, -0.02},
{0.01, -0.02},
{0.00, 0.02},
};
modelBuffer =
utils::CreateBufferFromData(device, model, sizeof(model), dawn::BufferUsage::Vertex);
SimParams params = { 0.04f, 0.1f, 0.025f, 0.025f, 0.02f, 0.05f, 0.005f, kNumParticles };
updateParams =
utils::CreateBufferFromData(device, ¶ms, sizeof(params), dawn::BufferUsage::Uniform);
std::vector<Particle> initialParticles(kNumParticles);
{
std::mt19937 generator;
std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
for (auto& p : initialParticles)
{
p.pos = glm::vec2(dist(generator), dist(generator));
p.vel = glm::vec2(dist(generator), dist(generator)) * 0.1f;
}
}
for (size_t i = 0; i < 2; i++) {
dawn::BufferDescriptor descriptor;
descriptor.size = sizeof(Particle) * kNumParticles;
descriptor.usage =
dawn::BufferUsage::CopyDst | dawn::BufferUsage::Vertex | dawn::BufferUsage::Storage;
particleBuffers[i] = device.CreateBuffer(&descriptor);
particleBuffers[i].SetSubData(0,
sizeof(Particle) * kNumParticles,
reinterpret_cast<uint8_t*>(initialParticles.data()));
}
}
void initRender() {
dawn::ShaderModule vsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
#version 450
layout(location = 0) in vec2 a_particlePos;
layout(location = 1) in vec2 a_particleVel;
layout(location = 2) in vec2 a_pos;
void main() {
float angle = -atan(a_particleVel.x, a_particleVel.y);
vec2 pos = vec2(a_pos.x * cos(angle) - a_pos.y * sin(angle),
a_pos.x * sin(angle) + a_pos.y * cos(angle));
gl_Position = vec4(pos + a_particlePos, 0, 1);
}
)");
dawn::ShaderModule fsModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vec4(1.0);
}
)");
depthStencilView = CreateDefaultDepthStencilView(device);
utils::ComboRenderPipelineDescriptor descriptor(device);
descriptor.vertexStage.module = vsModule;
descriptor.cFragmentStage.module = fsModule;
descriptor.cVertexInput.bufferCount = 2;
descriptor.cVertexInput.cBuffers[0].stride = sizeof(Particle);
descriptor.cVertexInput.cBuffers[0].stepMode = dawn::InputStepMode::Instance;
descriptor.cVertexInput.cBuffers[0].attributeCount = 2;
descriptor.cVertexInput.cAttributes[0].offset = offsetof(Particle, pos);
descriptor.cVertexInput.cAttributes[0].format = dawn::VertexFormat::Float2;
descriptor.cVertexInput.cAttributes[1].shaderLocation = 1;
descriptor.cVertexInput.cAttributes[1].offset = offsetof(Particle, vel);
descriptor.cVertexInput.cAttributes[1].format = dawn::VertexFormat::Float2;
descriptor.cVertexInput.cBuffers[1].stride = sizeof(glm::vec2);
descriptor.cVertexInput.cBuffers[1].attributeCount = 1;
descriptor.cVertexInput.cBuffers[1].attributes = &descriptor.cVertexInput.cAttributes[2];
descriptor.cVertexInput.cAttributes[2].shaderLocation = 2;
descriptor.cVertexInput.cAttributes[2].format = dawn::VertexFormat::Float2;
descriptor.depthStencilState = &descriptor.cDepthStencilState;
descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
renderPipeline = device.CreateRenderPipeline(&descriptor);
}
void initSim() {
dawn::ShaderModule module =
utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, R"(
#version 450
struct Particle {
vec2 pos;
vec2 vel;
};
layout(std140, set = 0, binding = 0) uniform SimParams {
float deltaT;
float rule1Distance;
float rule2Distance;
float rule3Distance;
float rule1Scale;
float rule2Scale;
float rule3Scale;
int particleCount;
} params;
layout(std140, set = 0, binding = 1) buffer ParticlesA {
Particle particles[1000];
} particlesA;
layout(std140, set = 0, binding = 2) buffer ParticlesB {
Particle particles[1000];
} particlesB;
void main() {
// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
uint index = gl_GlobalInvocationID.x;
if (index >= params.particleCount) { return; }
vec2 vPos = particlesA.particles[index].pos;
vec2 vVel = particlesA.particles[index].vel;
vec2 cMass = vec2(0.0, 0.0);
vec2 cVel = vec2(0.0, 0.0);
vec2 colVel = vec2(0.0, 0.0);
int cMassCount = 0;
int cVelCount = 0;
vec2 pos;
vec2 vel;
for (int i = 0; i < params.particleCount; ++i) {
if (i == index) { continue; }
pos = particlesA.particles[i].pos.xy;
vel = particlesA.particles[i].vel.xy;
if (distance(pos, vPos) < params.rule1Distance) {
cMass += pos;
cMassCount++;
}
if (distance(pos, vPos) < params.rule2Distance) {
colVel -= (pos - vPos);
}
if (distance(pos, vPos) < params.rule3Distance) {
cVel += vel;
cVelCount++;
}
}
if (cMassCount > 0) {
cMass = cMass / cMassCount - vPos;
}
if (cVelCount > 0) {
cVel = cVel / cVelCount;
}
vVel += cMass * params.rule1Scale + colVel * params.rule2Scale + cVel * params.rule3Scale;
// clamp velocity for a more pleasing simulation.
vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
// kinematic update
vPos += vVel * params.deltaT;
// Wrap around boundary
if (vPos.x < -1.0) vPos.x = 1.0;
if (vPos.x > 1.0) vPos.x = -1.0;
if (vPos.y < -1.0) vPos.y = 1.0;
if (vPos.y > 1.0) vPos.y = -1.0;
particlesB.particles[index].pos = vPos;
// Write back
particlesB.particles[index].vel = vVel;
}
)");
auto bgl = utils::MakeBindGroupLayout(
device, {
{0, dawn::ShaderStage::Compute, dawn::BindingType::UniformBuffer},
{1, dawn::ShaderStage::Compute, dawn::BindingType::StorageBuffer},
{2, dawn::ShaderStage::Compute, dawn::BindingType::StorageBuffer},
});
dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
dawn::ComputePipelineDescriptor csDesc;
csDesc.layout = pl;
csDesc.computeStage.module = module;
csDesc.computeStage.entryPoint = "main";
updatePipeline = device.CreateComputePipeline(&csDesc);
for (uint32_t i = 0; i < 2; ++i) {
updateBGs[i] = utils::MakeBindGroup(device, bgl, {
{0, updateParams, 0, sizeof(SimParams)},
{1, particleBuffers[i], 0, kNumParticles * sizeof(Particle)},
{2, particleBuffers[(i + 1) % 2], 0, kNumParticles * sizeof(Particle)},
});
}
}
dawn::CommandBuffer createCommandBuffer(const dawn::Texture backbuffer, size_t i) {
static const uint64_t zeroOffsets[1] = {0};
auto& bufferDst = particleBuffers[(i + 1) % 2];
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
{
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
pass.SetPipeline(updatePipeline);
pass.SetBindGroup(0, updateBGs[i], 0, nullptr);
pass.Dispatch(kNumParticles, 1, 1);
pass.EndPass();
}
{
utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView);
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
pass.SetPipeline(renderPipeline);
pass.SetVertexBuffers(0, 1, &bufferDst, zeroOffsets);
pass.SetVertexBuffers(1, 1, &modelBuffer, zeroOffsets);
pass.Draw(3, kNumParticles, 0, 0);
pass.EndPass();
}
return encoder.Finish();
}
void init() {
device = CreateCppDawnDevice();
queue = device.CreateQueue();
swapchain = GetSwapChain(device);
swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
640, 480);
initBuffers();
initRender();
initSim();
}
void frame() {
dawn::Texture backbuffer = swapchain.GetNextTexture();
dawn::CommandBuffer commandBuffer = createCommandBuffer(backbuffer, pingpong);
queue.Submit(1, &commandBuffer);
swapchain.Present(backbuffer);
DoFlush();
pingpong = (pingpong + 1) % 2;
}
int main(int argc, const char* argv[]) {
if (!InitSample(argc, argv)) {
return 1;
}
init();
while (!ShouldQuit()) {
frame();
utils::USleep(16000);
}
// TODO release stuff
}