blob: 1ad62e31d57d3a191db3a3dbc9c9f64ab0ac32b6 [file] [log] [blame]
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04001// Copyright 2017 The NXT Authors
2//
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
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040017#include "utils/NXTHelpers.h"
Corentin Wallez134e0802017-07-17 17:13:57 -040018#include "utils/SystemUtils.h"
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040019
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040020#include <array>
21#include <cstring>
22#include <random>
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040023
24#include <glm/glm.hpp>
25
26nxt::Device device;
27nxt::Queue queue;
Kai Ninomiyac16a67a2017-07-27 18:30:57 -070028nxt::SwapChain swapchain;
29nxt::TextureView depthStencilView;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040030
31nxt::Buffer modelBuffer;
32std::array<nxt::Buffer, 2> particleBuffers;
33
Corentin Wallez66ff4472017-07-14 11:32:57 -040034nxt::RenderPipeline renderPipeline;
Kai Ninomiya68df8b02017-05-16 14:04:22 -070035nxt::RenderPass renderpass;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040036
37nxt::Buffer updateParams;
Corentin Wallez66ff4472017-07-14 11:32:57 -040038nxt::ComputePipeline updatePipeline;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040039std::array<nxt::BindGroup, 2> updateBGs;
40
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 Wallez5ee7afd2017-06-19 13:09:41 -040067 modelBuffer = utils::CreateFrozenBufferFromData(device, model, sizeof(model), nxt::BufferUsageBit::Vertex);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040068
Corentin Wallez83e779d2017-07-10 21:44:06 -040069 SimParams params = { 0.04f, 0.1f, 0.025f, 0.025f, 0.02f, 0.05f, 0.005f, kNumParticles };
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040070 updateParams = utils::CreateFrozenBufferFromData(device, &params, sizeof(params), nxt::BufferUsageBit::Uniform);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040071
72 std::vector<Particle> initialParticles(kNumParticles);
73 {
74 std::mt19937 generator;
75 std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
76 for (auto& p : initialParticles)
77 {
78 p.pos = glm::vec2(dist(generator), dist(generator));
79 p.vel = glm::vec2(dist(generator), dist(generator)) * 0.1f;
80 }
81 }
82
Kai Ninomiya78c8b832017-07-21 17:00:22 -070083 for (size_t i = 0; i < 2; i++) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040084 particleBuffers[i] = device.CreateBufferBuilder()
Austin Eng39c901d2017-06-12 17:33:44 -040085 .SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Vertex | nxt::BufferUsageBit::Storage)
86 .SetInitialUsage(nxt::BufferUsageBit::TransferDst)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040087 .SetSize(sizeof(Particle) * kNumParticles)
88 .GetResult();
89
90 particleBuffers[i].SetSubData(0,
91 sizeof(Particle) * kNumParticles / sizeof(uint32_t),
92 reinterpret_cast<uint32_t*>(initialParticles.data()));
93 }
94}
95
96void initRender() {
Corentin Wallez5ee7afd2017-06-19 13:09:41 -040097 nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040098 #version 450
99 layout(location = 0) in vec2 a_particlePos;
100 layout(location = 1) in vec2 a_particleVel;
101 layout(location = 2) in vec2 a_pos;
102 void main() {
103 float angle = -atan(a_particleVel.x, a_particleVel.y);
104 vec2 pos = vec2(a_pos.x * cos(angle) - a_pos.y * sin(angle),
105 a_pos.x * sin(angle) + a_pos.y * cos(angle));
106 gl_Position = vec4(pos + a_particlePos, 0, 1);
107 }
108 )");
109
Corentin Wallez5ee7afd2017-06-19 13:09:41 -0400110 nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400111 #version 450
112 out vec4 fragColor;
113 void main() {
114 fragColor = vec4(1.0);
115 }
116 )");
117
118 nxt::InputState inputState = device.CreateInputStateBuilder()
119 .SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32, offsetof(Particle, pos))
120 .SetAttribute(1, 0, nxt::VertexFormat::FloatR32G32, offsetof(Particle, vel))
121 .SetInput(0, sizeof(Particle), nxt::InputStepMode::Instance)
122 .SetAttribute(2, 1, nxt::VertexFormat::FloatR32G32, 0)
123 .SetInput(1, sizeof(glm::vec2), nxt::InputStepMode::Vertex)
124 .GetResult();
125
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700126 renderpass = CreateDefaultRenderPass(device);
127 depthStencilView = CreateDefaultDepthStencilView(device);
128
Corentin Wallez66ff4472017-07-14 11:32:57 -0400129 renderPipeline = device.CreateRenderPipelineBuilder()
Kai Ninomiya68df8b02017-05-16 14:04:22 -0700130 .SetSubpass(renderpass, 0)
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400131 .SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
132 .SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
133 .SetInputState(inputState)
134 .GetResult();
135}
136
137void initSim() {
Corentin Wallez5ee7afd2017-06-19 13:09:41 -0400138 nxt::ShaderModule module = utils::CreateShaderModule(device, nxt::ShaderStage::Compute, R"(
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400139 #version 450
140
141 struct Particle {
142 vec2 pos;
143 vec2 vel;
144 };
145
146 layout(std140, set = 0, binding = 0) uniform SimParams {
147 float deltaT;
148 float rule1Distance;
149 float rule2Distance;
150 float rule3Distance;
151 float rule1Scale;
152 float rule2Scale;
153 float rule3Scale;
154 int particleCount;
155 } params;
156
157 layout(std140, set = 0, binding = 1) buffer ParticlesA {
Austin Engdc6bb4a2017-07-05 17:29:34 -0400158 Particle particle;
159 } particlesA[1000];
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400160
161 layout(std140, set = 0, binding = 2) buffer ParticlesB {
Austin Engdc6bb4a2017-07-05 17:29:34 -0400162 Particle particle;
163 } particlesB[1000];
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400164
165 void main() {
166 // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
167
168 uint index = gl_GlobalInvocationID.x;
169 if (index >= params.particleCount) { return; }
170
Austin Engdc6bb4a2017-07-05 17:29:34 -0400171 vec2 vPos = particlesA[index].particle.pos;
172 vec2 vVel = particlesA[index].particle.vel;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400173
174 vec2 cMass = vec2(0.0, 0.0);
175 vec2 cVel = vec2(0.0, 0.0);
176 vec2 colVel = vec2(0.0, 0.0);
177 int cMassCount = 0;
178 int cVelCount = 0;
179
180 vec2 pos;
181 vec2 vel;
182 for (int i = 0; i < params.particleCount; ++i) {
183 if (i == index) { continue; }
Austin Engdc6bb4a2017-07-05 17:29:34 -0400184 pos = particlesA[i].particle.pos.xy;
185 vel = particlesA[i].particle.vel.xy;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400186
187 if (distance(pos, vPos) < params.rule1Distance) {
188 cMass += pos;
189 cMassCount++;
190 }
191 if (distance(pos, vPos) < params.rule2Distance) {
192 colVel -= (pos - vPos);
193 }
194 if (distance(pos, vPos) < params.rule3Distance) {
195 cVel += vel;
196 cVelCount++;
197 }
198 }
199 if (cMassCount > 0) {
200 cMass = cMass / cMassCount - vPos;
201 }
202 if (cVelCount > 0) {
203 cVel = cVel / cVelCount;
204 }
205
206 vVel += cMass * params.rule1Scale + colVel * params.rule2Scale + cVel * params.rule3Scale;
207
208 // clamp velocity for a more pleasing simulation.
209 vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
210
211 // kinematic update
212 vPos += vVel * params.deltaT;
213
214 // Wrap around boundary
215 if (vPos.x < -1.0) vPos.x = 1.0;
216 if (vPos.x > 1.0) vPos.x = -1.0;
217 if (vPos.y < -1.0) vPos.y = 1.0;
218 if (vPos.y > 1.0) vPos.y = -1.0;
219
Austin Engdc6bb4a2017-07-05 17:29:34 -0400220 particlesB[index].particle.pos = vPos;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400221
222 // Write back
Austin Engdc6bb4a2017-07-05 17:29:34 -0400223 particlesB[index].particle.vel = vVel;
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400224 }
225 )");
226
227 nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
228 .SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::UniformBuffer, 0, 1)
229 .SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::StorageBuffer, 1, 2)
230 .GetResult();
231
232 nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
233 .SetBindGroupLayout(0, bgl)
234 .GetResult();
235
Corentin Wallez66ff4472017-07-14 11:32:57 -0400236 updatePipeline = device.CreateComputePipelineBuilder()
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400237 .SetLayout(pl)
238 .SetStage(nxt::ShaderStage::Compute, module, "main")
239 .GetResult();
240
241 nxt::BufferView updateParamsView = updateParams.CreateBufferViewBuilder()
242 .SetExtent(0, sizeof(SimParams))
243 .GetResult();
244
245 std::array<nxt::BufferView, 2> views;
246 for (uint32_t i = 0; i < 2; ++i) {
247 views[i] = particleBuffers[i].CreateBufferViewBuilder()
248 .SetExtent(0, kNumParticles * sizeof(Particle))
249 .GetResult();
250 }
251
252 for (uint32_t i = 0; i < 2; ++i) {
253 updateBGs[i] = device.CreateBindGroupBuilder()
254 .SetLayout(bgl)
255 .SetUsage(nxt::BindGroupUsage::Frozen)
256 .SetBufferViews(0, 1, &updateParamsView)
257 .SetBufferViews(1, 1, &views[i])
258 .SetBufferViews(2, 1, &views[(i + 1) % 2])
259 .GetResult();
260 }
261}
262
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700263nxt::CommandBuffer createCommandBuffer(const nxt::Framebuffer& framebuffer, size_t i) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400264 static const uint32_t zeroOffsets[1] = {0};
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700265 auto& bufferSrc = particleBuffers[i];
266 auto& bufferDst = particleBuffers[(i + 1) % 2];
267 return device.CreateCommandBufferBuilder()
268 .BeginComputePass()
269 .SetComputePipeline(updatePipeline)
270 .TransitionBufferUsage(bufferSrc, nxt::BufferUsageBit::Storage)
271 .TransitionBufferUsage(bufferDst, nxt::BufferUsageBit::Storage)
272 .SetBindGroup(0, updateBGs[i])
273 .Dispatch(kNumParticles, 1, 1)
274 .EndComputePass()
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400275
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700276 .BeginRenderPass(renderpass, framebuffer)
277 .BeginRenderSubpass()
278 .SetRenderPipeline(renderPipeline)
279 .TransitionBufferUsage(bufferDst, nxt::BufferUsageBit::Vertex)
280 .SetVertexBuffers(0, 1, &bufferDst, zeroOffsets)
281 .SetVertexBuffers(1, 1, &modelBuffer, zeroOffsets)
282 .DrawArrays(3, kNumParticles, 0, 0)
283 .EndRenderSubpass()
284 .EndRenderPass()
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400285
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700286 .GetResult();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400287}
288
289void init() {
Corentin Wallez583e9a82017-05-29 11:30:29 -0700290 device = CreateCppNXTDevice();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400291
292 queue = device.CreateQueueBuilder().GetResult();
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700293 swapchain = GetSwapChain(device);
294 swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400295
296 initBuffers();
297 initRender();
298 initSim();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400299}
300
301void frame() {
Kai Ninomiyac16a67a2017-07-27 18:30:57 -0700302 nxt::Texture backbuffer;
303 nxt::Framebuffer framebuffer;
304 GetNextFramebuffer(device, renderpass, swapchain, depthStencilView, &backbuffer, &framebuffer);
305
306 nxt::CommandBuffer commandBuffer = createCommandBuffer(framebuffer, pingpong);
307 queue.Submit(1, &commandBuffer);
308 backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
309 swapchain.Present(backbuffer);
310 DoFlush();
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400311
312 pingpong = (pingpong + 1) % 2;
313}
314
315int main(int argc, const char* argv[]) {
Corentin Wallez9347e8f2017-06-19 13:15:13 -0400316 if (!InitSample(argc, argv)) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400317 return 1;
318 }
319 init();
320
321 while (!ShouldQuit()) {
322 frame();
Corentin Wallez134e0802017-07-17 17:13:57 -0400323 utils::USleep(16000);
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400324 }
325
326 // TODO release stuff
327}