blob: 29bd9c5e9522870bcb334d2613e10f2382b2db7a [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 Wallez7aec4ae2021-03-24 15:55:32 +000098 wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
James Price9e0debd2021-04-13 14:52:44 +000099 struct VertexIn {
100 [[location(0)]] a_particlePos : vec2<f32>;
101 [[location(1)]] a_particleVel : vec2<f32>;
102 [[location(2)]] a_pos : vec2<f32>;
103 };
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000104
105 [[stage(vertex)]]
James Price9e0debd2021-04-13 14:52:44 +0000106 fn main(input : VertexIn) -> [[builtin(position)]] vec4<f32> {
107 var angle : f32 = -atan2(input.a_particleVel.x, input.a_particleVel.y);
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000108 var pos : vec2<f32> = vec2<f32>(
James Price9e0debd2021-04-13 14:52:44 +0000109 (input.a_pos.x * cos(angle)) - (input.a_pos.y * sin(angle)),
110 (input.a_pos.x * sin(angle)) + (input.a_pos.y * cos(angle)));
111 return vec4<f32>(pos + input.a_particlePos, 0.0, 1.0);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400112 }
113 )");
114
Corentin Wallez7aec4ae2021-03-24 15:55:32 +0000115 wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000116 [[stage(fragment)]]
James Price9e0debd2021-04-13 14:52:44 +0000117 fn main() -> [[location(0)]] vec4<f32> {
118 return vec4<f32>(1.0, 1.0, 1.0, 1.0);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400119 }
120 )");
121
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700122 depthStencilView = CreateDefaultDepthStencilView(device);
123
Corentin Wallez9895c272021-03-18 16:46:58 +0000124 utils::ComboRenderPipelineDescriptor2 descriptor;
Yunchao He889d7432019-03-27 18:08:50 +0000125
Corentin Wallez9895c272021-03-18 16:46:58 +0000126 descriptor.vertex.module = vsModule;
127 descriptor.vertex.bufferCount = 2;
128 descriptor.cBuffers[0].arrayStride = sizeof(Particle);
129 descriptor.cBuffers[0].stepMode = wgpu::InputStepMode::Instance;
130 descriptor.cBuffers[0].attributeCount = 2;
131 descriptor.cAttributes[0].offset = offsetof(Particle, pos);
132 descriptor.cAttributes[0].format = wgpu::VertexFormat::Float32x2;
133 descriptor.cAttributes[1].shaderLocation = 1;
134 descriptor.cAttributes[1].offset = offsetof(Particle, vel);
135 descriptor.cAttributes[1].format = wgpu::VertexFormat::Float32x2;
136 descriptor.cBuffers[1].arrayStride = sizeof(glm::vec2);
137 descriptor.cBuffers[1].attributeCount = 1;
138 descriptor.cBuffers[1].attributes = &descriptor.cAttributes[2];
139 descriptor.cAttributes[2].shaderLocation = 2;
140 descriptor.cAttributes[2].format = wgpu::VertexFormat::Float32x2;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000141
Corentin Wallez9895c272021-03-18 16:46:58 +0000142 descriptor.cFragment.module = fsModule;
143 descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8);
144 descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat();
145
146 renderPipeline = device.CreateRenderPipeline2(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400147}
148
149void initSim() {
Corentin Wallez7aec4ae2021-03-24 15:55:32 +0000150 wgpu::ShaderModule module = utils::CreateShaderModule(device, R"(
Stephen Whited6754472021-02-23 20:30:39 +0000151 struct Particle {
Ben Claytonc5686842021-03-17 09:48:19 +0000152 pos : vec2<f32>;
153 vel : vec2<f32>;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400154 };
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000155 [[block]] struct SimParams {
Ben Claytonc5686842021-03-17 09:48:19 +0000156 deltaT : f32;
157 rule1Distance : f32;
158 rule2Distance : f32;
159 rule3Distance : f32;
160 rule1Scale : f32;
161 rule2Scale : f32;
162 rule3Scale : f32;
163 particleCount : u32;
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000164 };
165 [[block]] struct Particles {
Ben Claytonc5686842021-03-17 09:48:19 +0000166 particles : array<Particle>;
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000167 };
dan sinclair0f9c2d72021-01-19 14:18:51 +0000168 [[binding(0), group(0)]] var<uniform> params : SimParams;
Ryan Harrisonbbabda32021-03-18 17:20:48 +0000169 [[binding(1), group(0)]] var<storage> particlesA : [[access(read)]] Particles;
170 [[binding(2), group(0)]] var<storage> particlesB : [[access(read_write)]] Particles;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400171
dan sinclaire6ca2542021-01-12 22:11:14 +0000172 // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000173 [[stage(compute)]]
James Price9e0debd2021-04-13 14:52:44 +0000174 fn main([[builtin(global_invocation_id)]] GlobalInvocationID : vec3<u32>) {
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000175 var index : u32 = GlobalInvocationID.x;
176 if (index >= params.particleCount) {
177 return;
178 }
179 var vPos : vec2<f32> = particlesA.particles[index].pos;
180 var vVel : vec2<f32> = particlesA.particles[index].vel;
181 var cMass : vec2<f32> = vec2<f32>(0.0, 0.0);
182 var cVel : vec2<f32> = vec2<f32>(0.0, 0.0);
183 var colVel : vec2<f32> = vec2<f32>(0.0, 0.0);
184 var cMassCount : u32 = 0u;
185 var cVelCount : u32 = 0u;
186 var pos : vec2<f32>;
187 var vel : vec2<f32>;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400188
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000189 for (var i : u32 = 0u; i < params.particleCount; i = i + 1u) {
190 if (i == index) {
191 continue;
192 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400193
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700194 pos = particlesA.particles[i].pos.xy;
195 vel = particlesA.particles[i].vel.xy;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400196 if (distance(pos, vPos) < params.rule1Distance) {
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000197 cMass = cMass + pos;
198 cMassCount = cMassCount + 1u;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400199 }
200 if (distance(pos, vPos) < params.rule2Distance) {
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000201 colVel = colVel - (pos - vPos);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400202 }
203 if (distance(pos, vPos) < params.rule3Distance) {
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000204 cVel = cVel + vel;
205 cVelCount = cVelCount + 1u;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400206 }
207 }
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000208
209 if (cMassCount > 0u) {
210 cMass = (cMass / vec2<f32>(f32(cMassCount), f32(cMassCount))) - vPos;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400211 }
212
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000213 if (cVelCount > 0u) {
214 cVel = cVel / vec2<f32>(f32(cVelCount), f32(cVelCount));
215 }
216 vVel = vVel + (cMass * params.rule1Scale) + (colVel * params.rule2Scale) +
217 (cVel * params.rule3Scale);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400218
dan sinclaire6ca2542021-01-12 22:11:14 +0000219 // clamp velocity for a more pleasing simulation
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400220 vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
dan sinclaire6ca2542021-01-12 22:11:14 +0000221 // kinematic update
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000222 vPos = vPos + (vVel * params.deltaT);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400223
dan sinclaire6ca2542021-01-12 22:11:14 +0000224 // Wrap around boundary
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000225 if (vPos.x < -1.0) {
226 vPos.x = 1.0;
227 }
228 if (vPos.x > 1.0) {
229 vPos.x = -1.0;
230 }
231 if (vPos.y < -1.0) {
232 vPos.y = 1.0;
233 }
234 if (vPos.y > 1.0) {
235 vPos.y = -1.0;
236 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400237
dan sinclaire6ca2542021-01-12 22:11:14 +0000238 // Write back
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700239 particlesB.particles[index].pos = vPos;
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700240 particlesB.particles[index].vel = vVel;
Corentin Wallez4814bdb2020-11-26 16:39:46 +0000241 return;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400242 }
243 )");
244
Kai Ninomiya234becf2018-07-10 12:23:50 -0700245 auto bgl = utils::MakeBindGroupLayout(
246 device, {
Corentin Wallez9895c272021-03-18 16:46:58 +0000247 {0, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Uniform},
248 {1, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage},
249 {2, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700250 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400251
Corentin Wallez04863c42019-10-25 11:36:47 +0000252 wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400253
Corentin Wallez04863c42019-10-25 11:36:47 +0000254 wgpu::ComputePipelineDescriptor csDesc;
Corentin Wallezaa7109c2018-10-25 10:42:49 +0000255 csDesc.layout = pl;
Corentin Walleza900cce2019-09-05 09:41:17 +0000256 csDesc.computeStage.module = module;
257 csDesc.computeStage.entryPoint = "main";
Corentin Wallez8e335a52018-08-27 23:12:56 +0200258 updatePipeline = device.CreateComputePipeline(&csDesc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400259
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400260 for (uint32_t i = 0; i < 2; ++i) {
Kai Ninomiya2afea0c2020-07-10 20:33:08 +0000261 updateBGs[i] = utils::MakeBindGroup(
262 device, bgl,
263 {
264 {0, updateParams, 0, sizeof(SimParams)},
265 {1, particleBuffers[i], 0, kNumParticles * sizeof(Particle)},
266 {2, particleBuffers[(i + 1) % 2], 0, kNumParticles * sizeof(Particle)},
267 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400268 }
269}
270
Corentin Wallez604072b2019-11-12 18:30:11 +0000271wgpu::CommandBuffer createCommandBuffer(const wgpu::TextureView backbufferView, size_t i) {
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700272 auto& bufferDst = particleBuffers[(i + 1) % 2];
Corentin Wallez04863c42019-10-25 11:36:47 +0000273 wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400274
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000275 {
Corentin Wallez04863c42019-10-25 11:36:47 +0000276 wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
Yan, Shaobo300eec02018-12-21 10:40:26 +0000277 pass.SetPipeline(updatePipeline);
Corentin Wallez70c8c102019-10-09 16:08:42 +0000278 pass.SetBindGroup(0, updateBGs[i]);
Corentin Wallez3da19b82020-03-31 16:23:35 +0000279 pass.Dispatch(kNumParticles);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000280 pass.EndPass();
281 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400282
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000283 {
Corentin Wallez604072b2019-11-12 18:30:11 +0000284 utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView);
Corentin Wallez04863c42019-10-25 11:36:47 +0000285 wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000286 pass.SetPipeline(renderPipeline);
François Beaufort91b21422019-10-10 07:29:58 +0000287 pass.SetVertexBuffer(0, bufferDst);
288 pass.SetVertexBuffer(1, modelBuffer);
Corentin Wallez67b1ad72020-03-31 16:21:35 +0000289 pass.Draw(3, kNumParticles);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000290 pass.EndPass();
291 }
292
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000293 return encoder.Finish();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400294}
295
296void init() {
Corentin Wallez39039fa2018-07-18 14:06:10 +0200297 device = CreateCppDawnDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400298
Corentin Wallez6d315da2021-02-04 15:33:42 +0000299 queue = device.GetQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700300 swapchain = GetSwapChain(device);
Corentin Wallez6b087812020-10-27 15:35:56 +0000301 swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment,
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000302 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400303
304 initBuffers();
305 initRender();
306 initSim();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400307}
308
309void frame() {
Corentin Wallez604072b2019-11-12 18:30:11 +0000310 wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700311
Corentin Wallez604072b2019-11-12 18:30:11 +0000312 wgpu::CommandBuffer commandBuffer = createCommandBuffer(backbufferView, pingpong);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700313 queue.Submit(1, &commandBuffer);
Corentin Wallez604072b2019-11-12 18:30:11 +0000314 swapchain.Present();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700315 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400316
317 pingpong = (pingpong + 1) % 2;
318}
319
320int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400321 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400322 return 1;
323 }
324 init();
325
326 while (!ShouldQuit()) {
327 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400328 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400329 }
330
331 // TODO release stuff
332}