blob: 08487da14271648a9aa80c160fb58b221cd6cf12 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Corentin Wallez9347e8f2017-06-19 13:15:13 -040015#include "SampleUtils.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040016
Yan, Shaoboa4924272018-12-10 19:47:22 +000017#include "utils/ComboRenderPipelineDescriptor.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040018#include "utils/SystemUtils.h"
Corentin Wallez04863c42019-10-25 11:36:47 +000019#include "utils/WGPUHelpers.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040020
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040021#include <array>
22#include <cstring>
23#include <random>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040024
25#include <glm/glm.hpp>
26
Corentin Wallez04863c42019-10-25 11:36:47 +000027wgpu::Device device;
28wgpu::Queue queue;
29wgpu::SwapChain swapchain;
30wgpu::TextureView depthStencilView;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040031
Corentin Wallez04863c42019-10-25 11:36:47 +000032wgpu::Buffer modelBuffer;
33std::array<wgpu::Buffer, 2> particleBuffers;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040034
Corentin Wallez04863c42019-10-25 11:36:47 +000035wgpu::RenderPipeline renderPipeline;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040036
Corentin Wallez04863c42019-10-25 11:36:47 +000037wgpu::Buffer updateParams;
38wgpu::ComputePipeline updatePipeline;
39std::array<wgpu::BindGroup, 2> updateBGs;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040040
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040041size_t pingpong = 0;
42
43static const uint32_t kNumParticles = 1000;
44
45struct Particle {
46 glm::vec2 pos;
47 glm::vec2 vel;
48};
49
50struct SimParams {
51 float deltaT;
52 float rule1Distance;
53 float rule2Distance;
54 float rule3Distance;
55 float rule1Scale;
56 float rule2Scale;
57 float rule3Scale;
58 int particleCount;
59};
60
61void initBuffers() {
62 glm::vec2 model[3] = {
63 {-0.01, -0.02},
64 {0.01, -0.02},
65 {0.00, 0.02},
66 };
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000067 modelBuffer =
Corentin Wallez04863c42019-10-25 11:36:47 +000068 utils::CreateBufferFromData(device, model, sizeof(model), wgpu::BufferUsage::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040069
Kai Ninomiya2afea0c2020-07-10 20:33:08 +000070 SimParams params = {0.04f, 0.1f, 0.025f, 0.025f, 0.02f, 0.05f, 0.005f, kNumParticles};
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000071 updateParams =
Corentin Wallez04863c42019-10-25 11:36:47 +000072 utils::CreateBufferFromData(device, &params, sizeof(params), wgpu::BufferUsage::Uniform);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040073
74 std::vector<Particle> initialParticles(kNumParticles);
75 {
76 std::mt19937 generator;
77 std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
Kai Ninomiya2afea0c2020-07-10 20:33:08 +000078 for (auto& p : initialParticles) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040079 p.pos = glm::vec2(dist(generator), dist(generator));
80 p.vel = glm::vec2(dist(generator), dist(generator)) * 0.1f;
81 }
82 }
83
Kai Ninomiya78c8b832017-07-21 17:00:22 -070084 for (size_t i = 0; i < 2; i++) {
Corentin Wallez04863c42019-10-25 11:36:47 +000085 wgpu::BufferDescriptor descriptor;
Corentin Wallez82b65732018-08-22 15:37:29 +020086 descriptor.size = sizeof(Particle) * kNumParticles;
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000087 descriptor.usage =
Corentin Wallez04863c42019-10-25 11:36:47 +000088 wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Vertex | wgpu::BufferUsage::Storage;
Corentin Wallez82b65732018-08-22 15:37:29 +020089 particleBuffers[i] = device.CreateBuffer(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040090
Corentin Wallez47a33412020-06-02 09:24:39 +000091 queue.WriteBuffer(particleBuffers[i], 0,
92 reinterpret_cast<uint8_t*>(initialParticles.data()),
93 sizeof(Particle) * kNumParticles);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040094 }
95}
96
97void initRender() {
Corentin Wallez04863c42019-10-25 11:36:47 +000098 wgpu::ShaderModule vsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +000099 utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400100 #version 450
101 layout(location = 0) in vec2 a_particlePos;
102 layout(location = 1) in vec2 a_particleVel;
103 layout(location = 2) in vec2 a_pos;
104 void main() {
105 float angle = -atan(a_particleVel.x, a_particleVel.y);
106 vec2 pos = vec2(a_pos.x * cos(angle) - a_pos.y * sin(angle),
107 a_pos.x * sin(angle) + a_pos.y * cos(angle));
108 gl_Position = vec4(pos + a_particlePos, 0, 1);
109 }
110 )");
111
Corentin Wallez04863c42019-10-25 11:36:47 +0000112 wgpu::ShaderModule fsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000113 utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400114 #version 450
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400115 layout(location = 0) out vec4 fragColor;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400116 void main() {
117 fragColor = vec4(1.0);
118 }
119 )");
120
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700121 depthStencilView = CreateDefaultDepthStencilView(device);
122
Yan, Shaoboa4924272018-12-10 19:47:22 +0000123 utils::ComboRenderPipelineDescriptor descriptor(device);
Corentin Wallezc6c7a422019-09-05 09:35:07 +0000124 descriptor.vertexStage.module = vsModule;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000125 descriptor.cFragmentStage.module = fsModule;
Yunchao He889d7432019-03-27 18:08:50 +0000126
Kai Ninomiyaae1f25f2019-11-07 22:23:29 +0000127 descriptor.cVertexState.vertexBufferCount = 2;
128 descriptor.cVertexState.cVertexBuffers[0].arrayStride = sizeof(Particle);
129 descriptor.cVertexState.cVertexBuffers[0].stepMode = wgpu::InputStepMode::Instance;
130 descriptor.cVertexState.cVertexBuffers[0].attributeCount = 2;
131 descriptor.cVertexState.cAttributes[0].offset = offsetof(Particle, pos);
132 descriptor.cVertexState.cAttributes[0].format = wgpu::VertexFormat::Float2;
133 descriptor.cVertexState.cAttributes[1].shaderLocation = 1;
134 descriptor.cVertexState.cAttributes[1].offset = offsetof(Particle, vel);
135 descriptor.cVertexState.cAttributes[1].format = wgpu::VertexFormat::Float2;
136 descriptor.cVertexState.cVertexBuffers[1].arrayStride = sizeof(glm::vec2);
137 descriptor.cVertexState.cVertexBuffers[1].attributeCount = 1;
138 descriptor.cVertexState.cVertexBuffers[1].attributes = &descriptor.cVertexState.cAttributes[2];
139 descriptor.cVertexState.cAttributes[2].shaderLocation = 2;
140 descriptor.cVertexState.cAttributes[2].format = wgpu::VertexFormat::Float2;
Yunchao He108bcbd2019-02-15 02:20:57 +0000141 descriptor.depthStencilState = &descriptor.cDepthStencilState;
Corentin Wallez04863c42019-10-25 11:36:47 +0000142 descriptor.cDepthStencilState.format = wgpu::TextureFormat::Depth24PlusStencil8;
Corentin Wallezc81a7172019-09-20 23:22:27 +0000143 descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
Yan, Shaoboa4924272018-12-10 19:47:22 +0000144
145 renderPipeline = device.CreateRenderPipeline(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400146}
147
148void initSim() {
Corentin Wallez04863c42019-10-25 11:36:47 +0000149 wgpu::ShaderModule module =
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000150 utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400151 #version 450
152
153 struct Particle {
154 vec2 pos;
155 vec2 vel;
156 };
157
158 layout(std140, set = 0, binding = 0) uniform SimParams {
159 float deltaT;
160 float rule1Distance;
161 float rule2Distance;
162 float rule3Distance;
163 float rule1Scale;
164 float rule2Scale;
165 float rule3Scale;
166 int particleCount;
167 } params;
168
169 layout(std140, set = 0, binding = 1) buffer ParticlesA {
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700170 Particle particles[1000];
171 } particlesA;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400172
173 layout(std140, set = 0, binding = 2) buffer ParticlesB {
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700174 Particle particles[1000];
175 } particlesB;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400176
177 void main() {
178 // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
179
180 uint index = gl_GlobalInvocationID.x;
181 if (index >= params.particleCount) { return; }
182
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700183 vec2 vPos = particlesA.particles[index].pos;
184 vec2 vVel = particlesA.particles[index].vel;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400185
186 vec2 cMass = vec2(0.0, 0.0);
187 vec2 cVel = vec2(0.0, 0.0);
188 vec2 colVel = vec2(0.0, 0.0);
189 int cMassCount = 0;
190 int cVelCount = 0;
191
192 vec2 pos;
193 vec2 vel;
194 for (int i = 0; i < params.particleCount; ++i) {
195 if (i == index) { continue; }
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700196 pos = particlesA.particles[i].pos.xy;
197 vel = particlesA.particles[i].vel.xy;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400198
199 if (distance(pos, vPos) < params.rule1Distance) {
200 cMass += pos;
201 cMassCount++;
202 }
203 if (distance(pos, vPos) < params.rule2Distance) {
204 colVel -= (pos - vPos);
205 }
206 if (distance(pos, vPos) < params.rule3Distance) {
207 cVel += vel;
208 cVelCount++;
209 }
210 }
211 if (cMassCount > 0) {
212 cMass = cMass / cMassCount - vPos;
213 }
214 if (cVelCount > 0) {
215 cVel = cVel / cVelCount;
216 }
217
218 vVel += cMass * params.rule1Scale + colVel * params.rule2Scale + cVel * params.rule3Scale;
219
220 // clamp velocity for a more pleasing simulation.
221 vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
222
223 // kinematic update
224 vPos += vVel * params.deltaT;
225
226 // Wrap around boundary
227 if (vPos.x < -1.0) vPos.x = 1.0;
228 if (vPos.x > 1.0) vPos.x = -1.0;
229 if (vPos.y < -1.0) vPos.y = 1.0;
230 if (vPos.y > 1.0) vPos.y = -1.0;
231
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700232 particlesB.particles[index].pos = vPos;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400233
234 // Write back
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700235 particlesB.particles[index].vel = vVel;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400236 }
237 )");
238
Kai Ninomiya234becf2018-07-10 12:23:50 -0700239 auto bgl = utils::MakeBindGroupLayout(
240 device, {
Corentin Wallez04863c42019-10-25 11:36:47 +0000241 {0, wgpu::ShaderStage::Compute, wgpu::BindingType::UniformBuffer},
242 {1, wgpu::ShaderStage::Compute, wgpu::BindingType::StorageBuffer},
243 {2, wgpu::ShaderStage::Compute, wgpu::BindingType::StorageBuffer},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700244 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400245
Corentin Wallez04863c42019-10-25 11:36:47 +0000246 wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400247
Corentin Wallez04863c42019-10-25 11:36:47 +0000248 wgpu::ComputePipelineDescriptor csDesc;
Corentin Wallezaa7109c2018-10-25 10:42:49 +0000249 csDesc.layout = pl;
Corentin Walleza900cce2019-09-05 09:41:17 +0000250 csDesc.computeStage.module = module;
251 csDesc.computeStage.entryPoint = "main";
Corentin Wallez8e335a52018-08-27 23:12:56 +0200252 updatePipeline = device.CreateComputePipeline(&csDesc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400253
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400254 for (uint32_t i = 0; i < 2; ++i) {
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000255 updateBGs[i] = utils::MakeBindGroup(
256 device, bgl,
257 {
258 {0, updateParams, 0, sizeof(SimParams)},
259 {1, particleBuffers[i], 0, kNumParticles * sizeof(Particle)},
260 {2, particleBuffers[(i + 1) % 2], 0, kNumParticles * sizeof(Particle)},
261 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400262 }
263}
264
Corentin Wallez604072b2019-11-12 18:30:11 +0000265wgpu::CommandBuffer createCommandBuffer(const wgpu::TextureView backbufferView, size_t i) {
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700266 auto& bufferDst = particleBuffers[(i + 1) % 2];
Corentin Wallez04863c42019-10-25 11:36:47 +0000267 wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400268
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000269 {
Corentin Wallez04863c42019-10-25 11:36:47 +0000270 wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
Yan, Shaobo300eec02018-12-21 10:40:26 +0000271 pass.SetPipeline(updatePipeline);
Corentin Wallez70c8c102019-10-09 16:08:42 +0000272 pass.SetBindGroup(0, updateBGs[i]);
Corentin Wallez3da19b82020-03-31 16:23:35 +0000273 pass.Dispatch(kNumParticles);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000274 pass.EndPass();
275 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400276
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000277 {
Corentin Wallez604072b2019-11-12 18:30:11 +0000278 utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView);
Corentin Wallez04863c42019-10-25 11:36:47 +0000279 wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000280 pass.SetPipeline(renderPipeline);
François Beaufort91b21422019-10-10 07:29:58 +0000281 pass.SetVertexBuffer(0, bufferDst);
282 pass.SetVertexBuffer(1, modelBuffer);
Corentin Wallez67b1ad72020-03-31 16:21:35 +0000283 pass.Draw(3, kNumParticles);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000284 pass.EndPass();
285 }
286
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000287 return encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400288}
289
290void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +0200291 device = CreateCppDawnDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400292
Corentin Wallez8a437942020-04-17 16:45:17 +0000293 queue = device.GetDefaultQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700294 swapchain = GetSwapChain(device);
Corentin Wallez6b087812020-10-27 15:35:56 +0000295 swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment,
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000296 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400297
298 initBuffers();
299 initRender();
300 initSim();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400301}
302
303void frame() {
Corentin Wallez604072b2019-11-12 18:30:11 +0000304 wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700305
Corentin Wallez604072b2019-11-12 18:30:11 +0000306 wgpu::CommandBuffer commandBuffer = createCommandBuffer(backbufferView, pingpong);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700307 queue.Submit(1, &commandBuffer);
Corentin Wallez604072b2019-11-12 18:30:11 +0000308 swapchain.Present();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700309 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400310
311 pingpong = (pingpong + 1) % 2;
312}
313
314int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400315 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400316 return 1;
317 }
318 init();
319
320 while (!ShouldQuit()) {
321 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400322 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400323 }
324
325 // TODO release stuff
326}