Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 1 | // 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 Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 15 | #include "SampleUtils.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 17 | #include "utils/NXTHelpers.h" |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 18 | #include "utils/SystemUtils.h" |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 19 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 20 | #include <array> |
| 21 | #include <cstring> |
| 22 | #include <random> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 23 | |
| 24 | #include <glm/glm.hpp> |
| 25 | |
| 26 | nxt::Device device; |
| 27 | nxt::Queue queue; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 28 | nxt::SwapChain swapchain; |
| 29 | nxt::TextureView depthStencilView; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 30 | |
| 31 | nxt::Buffer modelBuffer; |
| 32 | std::array<nxt::Buffer, 2> particleBuffers; |
| 33 | |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 34 | nxt::RenderPipeline renderPipeline; |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 35 | nxt::RenderPass renderpass; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 36 | |
| 37 | nxt::Buffer updateParams; |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 38 | nxt::ComputePipeline updatePipeline; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 39 | std::array<nxt::BindGroup, 2> updateBGs; |
| 40 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 41 | size_t pingpong = 0; |
| 42 | |
| 43 | static const uint32_t kNumParticles = 1000; |
| 44 | |
| 45 | struct Particle { |
| 46 | glm::vec2 pos; |
| 47 | glm::vec2 vel; |
| 48 | }; |
| 49 | |
| 50 | struct 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 | |
| 61 | void initBuffers() { |
| 62 | glm::vec2 model[3] = { |
| 63 | {-0.01, -0.02}, |
| 64 | {0.01, -0.02}, |
| 65 | {0.00, 0.02}, |
| 66 | }; |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 67 | modelBuffer = utils::CreateFrozenBufferFromData(device, model, sizeof(model), nxt::BufferUsageBit::Vertex); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 68 | |
Corentin Wallez | 83e779d | 2017-07-10 21:44:06 -0400 | [diff] [blame] | 69 | SimParams params = { 0.04f, 0.1f, 0.025f, 0.025f, 0.02f, 0.05f, 0.005f, kNumParticles }; |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 70 | updateParams = utils::CreateFrozenBufferFromData(device, ¶ms, sizeof(params), nxt::BufferUsageBit::Uniform); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 71 | |
| 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 Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 83 | for (size_t i = 0; i < 2; i++) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 84 | particleBuffers[i] = device.CreateBufferBuilder() |
Austin Eng | 39c901d | 2017-06-12 17:33:44 -0400 | [diff] [blame] | 85 | .SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Vertex | nxt::BufferUsageBit::Storage) |
| 86 | .SetInitialUsage(nxt::BufferUsageBit::TransferDst) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 87 | .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 | |
| 96 | void initRender() { |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 97 | nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Vertex, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 98 | #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 Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 110 | nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 111 | #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 Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 126 | renderpass = CreateDefaultRenderPass(device); |
| 127 | depthStencilView = CreateDefaultDepthStencilView(device); |
| 128 | |
Corentin Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 129 | renderPipeline = device.CreateRenderPipelineBuilder() |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 130 | .SetSubpass(renderpass, 0) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 131 | .SetStage(nxt::ShaderStage::Vertex, vsModule, "main") |
| 132 | .SetStage(nxt::ShaderStage::Fragment, fsModule, "main") |
| 133 | .SetInputState(inputState) |
| 134 | .GetResult(); |
| 135 | } |
| 136 | |
| 137 | void initSim() { |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 138 | nxt::ShaderModule module = utils::CreateShaderModule(device, nxt::ShaderStage::Compute, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 139 | #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 Eng | dc6bb4a | 2017-07-05 17:29:34 -0400 | [diff] [blame^] | 158 | Particle particle; |
| 159 | } particlesA[1000]; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 160 | |
| 161 | layout(std140, set = 0, binding = 2) buffer ParticlesB { |
Austin Eng | dc6bb4a | 2017-07-05 17:29:34 -0400 | [diff] [blame^] | 162 | Particle particle; |
| 163 | } particlesB[1000]; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 164 | |
| 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 Eng | dc6bb4a | 2017-07-05 17:29:34 -0400 | [diff] [blame^] | 171 | vec2 vPos = particlesA[index].particle.pos; |
| 172 | vec2 vVel = particlesA[index].particle.vel; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 173 | |
| 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 Eng | dc6bb4a | 2017-07-05 17:29:34 -0400 | [diff] [blame^] | 184 | pos = particlesA[i].particle.pos.xy; |
| 185 | vel = particlesA[i].particle.vel.xy; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 186 | |
| 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 Eng | dc6bb4a | 2017-07-05 17:29:34 -0400 | [diff] [blame^] | 220 | particlesB[index].particle.pos = vPos; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 221 | |
| 222 | // Write back |
Austin Eng | dc6bb4a | 2017-07-05 17:29:34 -0400 | [diff] [blame^] | 223 | particlesB[index].particle.vel = vVel; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 224 | } |
| 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 Wallez | 66ff447 | 2017-07-14 11:32:57 -0400 | [diff] [blame] | 236 | updatePipeline = device.CreateComputePipelineBuilder() |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 237 | .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 Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 263 | nxt::CommandBuffer createCommandBuffer(const nxt::Framebuffer& framebuffer, size_t i) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 264 | static const uint32_t zeroOffsets[1] = {0}; |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 265 | 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 Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 275 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 276 | .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 Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 285 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 286 | .GetResult(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void init() { |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 290 | device = CreateCppNXTDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 291 | |
| 292 | queue = device.CreateQueueBuilder().GetResult(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 293 | swapchain = GetSwapChain(device); |
| 294 | swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, 640, 480); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 295 | |
| 296 | initBuffers(); |
| 297 | initRender(); |
| 298 | initSim(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void frame() { |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 302 | 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 Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 311 | |
| 312 | pingpong = (pingpong + 1) % 2; |
| 313 | } |
| 314 | |
| 315 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 316 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 317 | return 1; |
| 318 | } |
| 319 | init(); |
| 320 | |
| 321 | while (!ShouldQuit()) { |
| 322 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 323 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // TODO release stuff |
| 327 | } |