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 | |
| 15 | #include "Utils.h" |
| 16 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame^] | 17 | #include "utils/NXTHelpers.h" |
| 18 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 19 | #include <array> |
| 20 | #include <cstring> |
| 21 | #include <random> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 22 | |
| 23 | #include <glm/glm.hpp> |
| 24 | |
| 25 | nxt::Device device; |
| 26 | nxt::Queue queue; |
| 27 | |
| 28 | nxt::Buffer modelBuffer; |
| 29 | std::array<nxt::Buffer, 2> particleBuffers; |
| 30 | |
| 31 | nxt::Pipeline renderPipeline; |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 32 | nxt::RenderPass renderpass; |
| 33 | nxt::Framebuffer framebuffer; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 34 | |
| 35 | nxt::Buffer updateParams; |
| 36 | nxt::Pipeline updatePipeline; |
| 37 | std::array<nxt::BindGroup, 2> updateBGs; |
| 38 | |
| 39 | std::array<nxt::CommandBuffer, 2> commandBuffers; |
| 40 | |
| 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 | |
| 69 | SimParams params = { 0.04, 0.1, 0.025, 0.025, 0.02, 0.05, 0.005, 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 | |
| 83 | for (int i = 0; i < 2; i++) { |
| 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 | |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame^] | 126 | utils::CreateDefaultRenderPass(device, &renderpass, &framebuffer); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 127 | renderPipeline = device.CreatePipelineBuilder() |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 128 | .SetSubpass(renderpass, 0) |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 129 | .SetStage(nxt::ShaderStage::Vertex, vsModule, "main") |
| 130 | .SetStage(nxt::ShaderStage::Fragment, fsModule, "main") |
| 131 | .SetInputState(inputState) |
| 132 | .GetResult(); |
| 133 | } |
| 134 | |
| 135 | void initSim() { |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame^] | 136 | nxt::ShaderModule module = utils::CreateShaderModule(device, nxt::ShaderStage::Compute, R"( |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 137 | #version 450 |
| 138 | |
| 139 | struct Particle { |
| 140 | vec2 pos; |
| 141 | vec2 vel; |
| 142 | }; |
| 143 | |
| 144 | layout(std140, set = 0, binding = 0) uniform SimParams { |
| 145 | float deltaT; |
| 146 | float rule1Distance; |
| 147 | float rule2Distance; |
| 148 | float rule3Distance; |
| 149 | float rule1Scale; |
| 150 | float rule2Scale; |
| 151 | float rule3Scale; |
| 152 | int particleCount; |
| 153 | } params; |
| 154 | |
| 155 | layout(std140, set = 0, binding = 1) buffer ParticlesA { |
| 156 | Particle particlesA[1000]; |
| 157 | }; |
| 158 | |
| 159 | layout(std140, set = 0, binding = 2) buffer ParticlesB { |
| 160 | Particle particlesB[1000]; |
| 161 | }; |
| 162 | |
| 163 | void main() { |
| 164 | // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp |
| 165 | |
| 166 | uint index = gl_GlobalInvocationID.x; |
| 167 | if (index >= params.particleCount) { return; } |
| 168 | |
| 169 | vec2 vPos = particlesA[index].pos; |
| 170 | vec2 vVel = particlesA[index].vel; |
| 171 | |
| 172 | vec2 cMass = vec2(0.0, 0.0); |
| 173 | vec2 cVel = vec2(0.0, 0.0); |
| 174 | vec2 colVel = vec2(0.0, 0.0); |
| 175 | int cMassCount = 0; |
| 176 | int cVelCount = 0; |
| 177 | |
| 178 | vec2 pos; |
| 179 | vec2 vel; |
| 180 | for (int i = 0; i < params.particleCount; ++i) { |
| 181 | if (i == index) { continue; } |
| 182 | pos = particlesA[i].pos.xy; |
| 183 | vel = particlesA[i].vel.xy; |
| 184 | |
| 185 | if (distance(pos, vPos) < params.rule1Distance) { |
| 186 | cMass += pos; |
| 187 | cMassCount++; |
| 188 | } |
| 189 | if (distance(pos, vPos) < params.rule2Distance) { |
| 190 | colVel -= (pos - vPos); |
| 191 | } |
| 192 | if (distance(pos, vPos) < params.rule3Distance) { |
| 193 | cVel += vel; |
| 194 | cVelCount++; |
| 195 | } |
| 196 | } |
| 197 | if (cMassCount > 0) { |
| 198 | cMass = cMass / cMassCount - vPos; |
| 199 | } |
| 200 | if (cVelCount > 0) { |
| 201 | cVel = cVel / cVelCount; |
| 202 | } |
| 203 | |
| 204 | vVel += cMass * params.rule1Scale + colVel * params.rule2Scale + cVel * params.rule3Scale; |
| 205 | |
| 206 | // clamp velocity for a more pleasing simulation. |
| 207 | vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1); |
| 208 | |
| 209 | // kinematic update |
| 210 | vPos += vVel * params.deltaT; |
| 211 | |
| 212 | // Wrap around boundary |
| 213 | if (vPos.x < -1.0) vPos.x = 1.0; |
| 214 | if (vPos.x > 1.0) vPos.x = -1.0; |
| 215 | if (vPos.y < -1.0) vPos.y = 1.0; |
| 216 | if (vPos.y > 1.0) vPos.y = -1.0; |
| 217 | |
| 218 | particlesB[index].pos = vPos; |
| 219 | |
| 220 | // Write back |
| 221 | particlesB[index].vel = vVel; |
| 222 | } |
| 223 | )"); |
| 224 | |
| 225 | nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder() |
| 226 | .SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::UniformBuffer, 0, 1) |
| 227 | .SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::StorageBuffer, 1, 2) |
| 228 | .GetResult(); |
| 229 | |
| 230 | nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder() |
| 231 | .SetBindGroupLayout(0, bgl) |
| 232 | .GetResult(); |
| 233 | |
| 234 | updatePipeline = device.CreatePipelineBuilder() |
| 235 | .SetLayout(pl) |
| 236 | .SetStage(nxt::ShaderStage::Compute, module, "main") |
| 237 | .GetResult(); |
| 238 | |
| 239 | nxt::BufferView updateParamsView = updateParams.CreateBufferViewBuilder() |
| 240 | .SetExtent(0, sizeof(SimParams)) |
| 241 | .GetResult(); |
| 242 | |
| 243 | std::array<nxt::BufferView, 2> views; |
| 244 | for (uint32_t i = 0; i < 2; ++i) { |
| 245 | views[i] = particleBuffers[i].CreateBufferViewBuilder() |
| 246 | .SetExtent(0, kNumParticles * sizeof(Particle)) |
| 247 | .GetResult(); |
| 248 | } |
| 249 | |
| 250 | for (uint32_t i = 0; i < 2; ++i) { |
| 251 | updateBGs[i] = device.CreateBindGroupBuilder() |
| 252 | .SetLayout(bgl) |
| 253 | .SetUsage(nxt::BindGroupUsage::Frozen) |
| 254 | .SetBufferViews(0, 1, &updateParamsView) |
| 255 | .SetBufferViews(1, 1, &views[i]) |
| 256 | .SetBufferViews(2, 1, &views[(i + 1) % 2]) |
| 257 | .GetResult(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void initCommandBuffers() { |
| 262 | static const uint32_t zeroOffsets[1] = {0}; |
| 263 | for (int i = 0; i < 2; ++i) { |
| 264 | auto& bufferSrc = particleBuffers[i]; |
| 265 | auto& bufferDst = particleBuffers[(i + 1) % 2]; |
| 266 | commandBuffers[i] = device.CreateCommandBufferBuilder() |
| 267 | .SetPipeline(updatePipeline) |
| 268 | .TransitionBufferUsage(bufferSrc, nxt::BufferUsageBit::Storage) |
| 269 | .TransitionBufferUsage(bufferDst, nxt::BufferUsageBit::Storage) |
| 270 | .SetBindGroup(0, updateBGs[i]) |
| 271 | .Dispatch(kNumParticles, 1, 1) |
| 272 | |
Kai Ninomiya | 68df8b0 | 2017-05-16 14:04:22 -0700 | [diff] [blame] | 273 | .BeginRenderPass(renderpass, framebuffer) |
| 274 | .SetPipeline(renderPipeline) |
| 275 | .TransitionBufferUsage(bufferDst, nxt::BufferUsageBit::Vertex) |
| 276 | .SetVertexBuffers(0, 1, &bufferDst, zeroOffsets) |
| 277 | .SetVertexBuffers(1, 1, &modelBuffer, zeroOffsets) |
| 278 | .DrawArrays(3, kNumParticles, 0, 0) |
| 279 | .EndRenderPass() |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 280 | |
| 281 | .GetResult(); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void init() { |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 286 | device = CreateCppNXTDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 287 | |
| 288 | queue = device.CreateQueueBuilder().GetResult(); |
| 289 | |
| 290 | initBuffers(); |
| 291 | initRender(); |
| 292 | initSim(); |
| 293 | initCommandBuffers(); |
| 294 | } |
| 295 | |
| 296 | void frame() { |
| 297 | queue.Submit(1, &commandBuffers[pingpong]); |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 298 | DoSwapBuffers(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 299 | |
| 300 | pingpong = (pingpong + 1) % 2; |
| 301 | } |
| 302 | |
| 303 | int main(int argc, const char* argv[]) { |
| 304 | if (!InitUtils(argc, argv)) { |
| 305 | return 1; |
| 306 | } |
| 307 | init(); |
| 308 | |
| 309 | while (!ShouldQuit()) { |
| 310 | frame(); |
Corentin Wallez | 583e9a8 | 2017-05-29 11:30:29 -0700 | [diff] [blame] | 311 | USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // TODO release stuff |
| 315 | } |