blob: 2e7bedad3fd8240fd2d4f0a5529a2a7e92b2f40f [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 Wallezf6840402018-07-18 14:00:56 +020018#include "utils/DawnHelpers.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040019#include "utils/SystemUtils.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 Wallez4828d922018-07-18 13:45:46 +020027dawn::Device device;
28dawn::Queue queue;
29dawn::SwapChain swapchain;
30dawn::TextureView depthStencilView;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040031
Corentin Wallez4828d922018-07-18 13:45:46 +020032dawn::Buffer modelBuffer;
33std::array<dawn::Buffer, 2> particleBuffers;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040034
Corentin Wallez4828d922018-07-18 13:45:46 +020035dawn::RenderPipeline renderPipeline;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040036
Corentin Wallez4828d922018-07-18 13:45:46 +020037dawn::Buffer updateParams;
38dawn::ComputePipeline updatePipeline;
39std::array<dawn::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 =
68 utils::CreateBufferFromData(device, model, sizeof(model), dawn::BufferUsage::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040069
Corentin Wallez83e779d2017-07-10 21:44:06 -040070 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 =
72 utils::CreateBufferFromData(device, &params, sizeof(params), dawn::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);
78 for (auto& p : initialParticles)
79 {
80 p.pos = glm::vec2(dist(generator), dist(generator));
81 p.vel = glm::vec2(dist(generator), dist(generator)) * 0.1f;
82 }
83 }
84
Kai Ninomiya78c8b832017-07-21 17:00:22 -070085 for (size_t i = 0; i < 2; i++) {
Corentin Wallez82b65732018-08-22 15:37:29 +020086 dawn::BufferDescriptor descriptor;
87 descriptor.size = sizeof(Particle) * kNumParticles;
Corentin Wallez9e9e29f2019-08-27 08:21:39 +000088 descriptor.usage =
89 dawn::BufferUsage::CopyDst | dawn::BufferUsage::Vertex | dawn::BufferUsage::Storage;
Corentin Wallez82b65732018-08-22 15:37:29 +020090 particleBuffers[i] = device.CreateBuffer(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040091
92 particleBuffers[i].SetSubData(0,
Stephen Whitee5ae3272018-02-04 11:07:02 -050093 sizeof(Particle) * kNumParticles,
94 reinterpret_cast<uint8_t*>(initialParticles.data()));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040095 }
96}
97
98void initRender() {
Corentin Wallezb9b088f2019-08-27 08:42:29 +000099 dawn::ShaderModule vsModule =
100 utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400101 #version 450
102 layout(location = 0) in vec2 a_particlePos;
103 layout(location = 1) in vec2 a_particleVel;
104 layout(location = 2) in vec2 a_pos;
105 void main() {
106 float angle = -atan(a_particleVel.x, a_particleVel.y);
107 vec2 pos = vec2(a_pos.x * cos(angle) - a_pos.y * sin(angle),
108 a_pos.x * sin(angle) + a_pos.y * cos(angle));
109 gl_Position = vec4(pos + a_particlePos, 0, 1);
110 }
111 )");
112
Corentin Wallez2a1d8c22019-07-12 17:52:22 +0000113 dawn::ShaderModule fsModule =
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000114 utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400115 #version 450
Corentin Wallezb6fb5f32017-08-29 13:37:45 -0400116 layout(location = 0) out vec4 fragColor;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400117 void main() {
118 fragColor = vec4(1.0);
119 }
120 )");
121
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700122 depthStencilView = CreateDefaultDepthStencilView(device);
123
Yan, Shaoboa4924272018-12-10 19:47:22 +0000124 utils::ComboRenderPipelineDescriptor descriptor(device);
Corentin Wallezc6c7a422019-09-05 09:35:07 +0000125 descriptor.vertexStage.module = vsModule;
Yan, Shaoboa4924272018-12-10 19:47:22 +0000126 descriptor.cFragmentStage.module = fsModule;
Yunchao He889d7432019-03-27 18:08:50 +0000127
Yunchao He2d4b5292019-06-06 17:54:30 +0000128 descriptor.cVertexInput.bufferCount = 2;
Yunchao He97c08852019-06-06 01:56:57 +0000129 descriptor.cVertexInput.cBuffers[0].stride = sizeof(Particle);
130 descriptor.cVertexInput.cBuffers[0].stepMode = dawn::InputStepMode::Instance;
Yunchao He2d4b5292019-06-06 17:54:30 +0000131 descriptor.cVertexInput.cBuffers[0].attributeCount = 2;
Yunchao Heeea20912019-05-22 22:46:32 +0000132 descriptor.cVertexInput.cAttributes[0].offset = offsetof(Particle, pos);
133 descriptor.cVertexInput.cAttributes[0].format = dawn::VertexFormat::Float2;
134 descriptor.cVertexInput.cAttributes[1].shaderLocation = 1;
135 descriptor.cVertexInput.cAttributes[1].offset = offsetof(Particle, vel);
136 descriptor.cVertexInput.cAttributes[1].format = dawn::VertexFormat::Float2;
Yunchao Heeea20912019-05-22 22:46:32 +0000137 descriptor.cVertexInput.cBuffers[1].stride = sizeof(glm::vec2);
Yunchao He2d4b5292019-06-06 17:54:30 +0000138 descriptor.cVertexInput.cBuffers[1].attributeCount = 1;
Yunchao He97c08852019-06-06 01:56:57 +0000139 descriptor.cVertexInput.cBuffers[1].attributes = &descriptor.cVertexInput.cAttributes[2];
140 descriptor.cVertexInput.cAttributes[2].shaderLocation = 2;
141 descriptor.cVertexInput.cAttributes[2].format = dawn::VertexFormat::Float2;
Yunchao He108bcbd2019-02-15 02:20:57 +0000142 descriptor.depthStencilState = &descriptor.cDepthStencilState;
Corentin Wallez77fa31c2019-06-19 09:26:07 +0000143 descriptor.cDepthStencilState.format = dawn::TextureFormat::Depth24PlusStencil8;
Yunchao He938811e2019-02-20 13:00:36 +0000144 descriptor.cColorStates[0]->format = GetPreferredSwapChainTextureFormat();
Yan, Shaoboa4924272018-12-10 19:47:22 +0000145
146 renderPipeline = device.CreateRenderPipeline(&descriptor);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400147}
148
149void initSim() {
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000150 dawn::ShaderModule module =
151 utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400152 #version 450
153
154 struct Particle {
155 vec2 pos;
156 vec2 vel;
157 };
158
159 layout(std140, set = 0, binding = 0) uniform SimParams {
160 float deltaT;
161 float rule1Distance;
162 float rule2Distance;
163 float rule3Distance;
164 float rule1Scale;
165 float rule2Scale;
166 float rule3Scale;
167 int particleCount;
168 } params;
169
170 layout(std140, set = 0, binding = 1) buffer ParticlesA {
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700171 Particle particles[1000];
172 } particlesA;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400173
174 layout(std140, set = 0, binding = 2) buffer ParticlesB {
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700175 Particle particles[1000];
176 } particlesB;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400177
178 void main() {
179 // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
180
181 uint index = gl_GlobalInvocationID.x;
182 if (index >= params.particleCount) { return; }
183
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700184 vec2 vPos = particlesA.particles[index].pos;
185 vec2 vVel = particlesA.particles[index].vel;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400186
187 vec2 cMass = vec2(0.0, 0.0);
188 vec2 cVel = vec2(0.0, 0.0);
189 vec2 colVel = vec2(0.0, 0.0);
190 int cMassCount = 0;
191 int cVelCount = 0;
192
193 vec2 pos;
194 vec2 vel;
195 for (int i = 0; i < params.particleCount; ++i) {
196 if (i == index) { continue; }
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700197 pos = particlesA.particles[i].pos.xy;
198 vel = particlesA.particles[i].vel.xy;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400199
200 if (distance(pos, vPos) < params.rule1Distance) {
201 cMass += pos;
202 cMassCount++;
203 }
204 if (distance(pos, vPos) < params.rule2Distance) {
205 colVel -= (pos - vPos);
206 }
207 if (distance(pos, vPos) < params.rule3Distance) {
208 cVel += vel;
209 cVelCount++;
210 }
211 }
212 if (cMassCount > 0) {
213 cMass = cMass / cMassCount - vPos;
214 }
215 if (cVelCount > 0) {
216 cVel = cVel / cVelCount;
217 }
218
219 vVel += cMass * params.rule1Scale + colVel * params.rule2Scale + cVel * params.rule3Scale;
220
221 // clamp velocity for a more pleasing simulation.
222 vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
223
224 // kinematic update
225 vPos += vVel * params.deltaT;
226
227 // Wrap around boundary
228 if (vPos.x < -1.0) vPos.x = 1.0;
229 if (vPos.x > 1.0) vPos.x = -1.0;
230 if (vPos.y < -1.0) vPos.y = 1.0;
231 if (vPos.y > 1.0) vPos.y = -1.0;
232
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700233 particlesB.particles[index].pos = vPos;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400234
235 // Write back
Kai Ninomiya7883e7e2018-07-10 11:01:28 -0700236 particlesB.particles[index].vel = vVel;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400237 }
238 )");
239
Kai Ninomiya234becf2018-07-10 12:23:50 -0700240 auto bgl = utils::MakeBindGroupLayout(
241 device, {
Corentin Wallezb9b088f2019-08-27 08:42:29 +0000242 {0, dawn::ShaderStage::Compute, dawn::BindingType::UniformBuffer},
243 {1, dawn::ShaderStage::Compute, dawn::BindingType::StorageBuffer},
244 {2, dawn::ShaderStage::Compute, dawn::BindingType::StorageBuffer},
Kai Ninomiya234becf2018-07-10 12:23:50 -0700245 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400246
Corentin Wallez4828d922018-07-18 13:45:46 +0200247 dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400248
Corentin Wallez8e335a52018-08-27 23:12:56 +0200249 dawn::ComputePipelineDescriptor csDesc;
Corentin Wallezaa7109c2018-10-25 10:42:49 +0000250 csDesc.layout = pl;
Corentin Walleza900cce2019-09-05 09:41:17 +0000251 csDesc.computeStage.module = module;
252 csDesc.computeStage.entryPoint = "main";
Corentin Wallez8e335a52018-08-27 23:12:56 +0200253 updatePipeline = device.CreateComputePipeline(&csDesc);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400254
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400255 for (uint32_t i = 0; i < 2; ++i) {
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000256 updateBGs[i] = utils::MakeBindGroup(device, bgl, {
Corentin Wallez6f0e1f92018-12-07 12:31:53 +0000257 {0, updateParams, 0, sizeof(SimParams)},
258 {1, particleBuffers[i], 0, kNumParticles * sizeof(Particle)},
259 {2, particleBuffers[(i + 1) % 2], 0, kNumParticles * sizeof(Particle)},
Corentin Wallez6f9d21e2018-12-05 07:18:30 +0000260 });
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400261 }
262}
263
Jiawei Shaob2c50232019-02-27 09:21:56 +0000264dawn::CommandBuffer createCommandBuffer(const dawn::Texture backbuffer, size_t i) {
Austin Engcf52d712019-04-05 20:51:29 +0000265 static const uint64_t zeroOffsets[1] = {0};
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700266 auto& bufferDst = particleBuffers[(i + 1) % 2];
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000267 dawn::CommandEncoder encoder = device.CreateCommandEncoder();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400268
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000269 {
Corentin Walleze1f0d4e2019-02-15 12:54:08 +0000270 dawn::ComputePassEncoder pass = encoder.BeginComputePass();
Yan, Shaobo300eec02018-12-21 10:40:26 +0000271 pass.SetPipeline(updatePipeline);
Yan, Shaobo991ab982019-03-18 06:01:37 +0000272 pass.SetBindGroup(0, updateBGs[i], 0, nullptr);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000273 pass.Dispatch(kNumParticles, 1, 1);
274 pass.EndPass();
275 }
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400276
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000277 {
Kai Ninomiya4078ed82019-08-27 17:56:23 +0000278 utils::ComboRenderPassDescriptor renderPass({backbuffer.CreateView()}, depthStencilView);
Jiawei Shaob2c50232019-02-27 09:21:56 +0000279 dawn::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
Yan, Shaobo300eec02018-12-21 10:40:26 +0000280 pass.SetPipeline(renderPipeline);
Corentin Wallez82fbccb2018-09-21 00:24:37 +0000281 pass.SetVertexBuffers(0, 1, &bufferDst, zeroOffsets);
282 pass.SetVertexBuffers(1, 1, &modelBuffer, zeroOffsets);
Jiawei Shaoc789b842018-12-10 05:20:19 +0000283 pass.Draw(3, kNumParticles, 0, 0);
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 Wallezb703def2018-06-14 20:26:27 -0400293 queue = device.CreateQueue();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700294 swapchain = GetSwapChain(device);
Corentin Wallez9e9e29f2019-08-27 08:21:39 +0000295 swapchain.Configure(GetPreferredSwapChainTextureFormat(), dawn::TextureUsage::OutputAttachment,
296 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() {
Jiawei Shaob2c50232019-02-27 09:21:56 +0000304 dawn::Texture backbuffer = swapchain.GetNextTexture();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700305
Jiawei Shaob2c50232019-02-27 09:21:56 +0000306 dawn::CommandBuffer commandBuffer = createCommandBuffer(backbuffer, pingpong);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700307 queue.Submit(1, &commandBuffer);
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700308 swapchain.Present(backbuffer);
309 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}