Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 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 | |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 17 | #include "utils/ComboRenderPipelineDescriptor.h" |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 18 | #include "utils/SystemUtils.h" |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 19 | #include "utils/WGPUHelpers.h" |
Corentin Wallez | 5ee7afd | 2017-06-19 13:09:41 -0400 | [diff] [blame] | 20 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 21 | #include <array> |
| 22 | #include <cstring> |
| 23 | #include <random> |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 24 | |
| 25 | #include <glm/glm.hpp> |
| 26 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 27 | wgpu::Device device; |
| 28 | wgpu::Queue queue; |
| 29 | wgpu::SwapChain swapchain; |
| 30 | wgpu::TextureView depthStencilView; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 31 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 32 | wgpu::Buffer modelBuffer; |
| 33 | std::array<wgpu::Buffer, 2> particleBuffers; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 34 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 35 | wgpu::RenderPipeline renderPipeline; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 36 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 37 | wgpu::Buffer updateParams; |
| 38 | wgpu::ComputePipeline updatePipeline; |
| 39 | std::array<wgpu::BindGroup, 2> updateBGs; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 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 | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 67 | modelBuffer = |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 68 | utils::CreateBufferFromData(device, model, sizeof(model), wgpu::BufferUsage::Vertex); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 69 | |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 70 | SimParams params = {0.04f, 0.1f, 0.025f, 0.025f, 0.02f, 0.05f, 0.005f, kNumParticles}; |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 71 | updateParams = |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 72 | utils::CreateBufferFromData(device, ¶ms, sizeof(params), wgpu::BufferUsage::Uniform); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 73 | |
| 74 | std::vector<Particle> initialParticles(kNumParticles); |
| 75 | { |
| 76 | std::mt19937 generator; |
| 77 | std::uniform_real_distribution<float> dist(-1.0f, 1.0f); |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 78 | for (auto& p : initialParticles) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 79 | p.pos = glm::vec2(dist(generator), dist(generator)); |
| 80 | p.vel = glm::vec2(dist(generator), dist(generator)) * 0.1f; |
| 81 | } |
| 82 | } |
| 83 | |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 84 | for (size_t i = 0; i < 2; i++) { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 85 | wgpu::BufferDescriptor descriptor; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 86 | descriptor.size = sizeof(Particle) * kNumParticles; |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 87 | descriptor.usage = |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 88 | wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Vertex | wgpu::BufferUsage::Storage; |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 89 | particleBuffers[i] = device.CreateBuffer(&descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 90 | |
Corentin Wallez | 47a3341 | 2020-06-02 09:24:39 +0000 | [diff] [blame] | 91 | queue.WriteBuffer(particleBuffers[i], 0, |
| 92 | reinterpret_cast<uint8_t*>(initialParticles.data()), |
| 93 | sizeof(Particle) * kNumParticles); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | void initRender() { |
shrekshao | 7faa362 | 2021-01-12 23:56:54 +0000 | [diff] [blame] | 98 | wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"( |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 99 | [[location(0)]] var<in> a_particlePos : vec2<f32>; |
| 100 | [[location(1)]] var<in> a_particleVel : vec2<f32>; |
| 101 | [[location(2)]] var<in> a_pos : vec2<f32>; |
| 102 | [[builtin(position)]] var<out> Position : vec4<f32>; |
| 103 | |
| 104 | [[stage(vertex)]] |
| 105 | fn main() -> void { |
| 106 | var angle : f32 = -atan2(a_particleVel.x, a_particleVel.y); |
| 107 | var pos : vec2<f32> = vec2<f32>( |
| 108 | (a_pos.x * cos(angle)) - (a_pos.y * sin(angle)), |
| 109 | (a_pos.x * sin(angle)) + (a_pos.y * cos(angle))); |
| 110 | Position = vec4<f32>(pos + a_particlePos, 0.0, 1.0); |
| 111 | return; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 112 | } |
| 113 | )"); |
| 114 | |
shrekshao | 7faa362 | 2021-01-12 23:56:54 +0000 | [diff] [blame] | 115 | wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"( |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 116 | [[location(0)]] var<out> FragColor : vec4<f32>; |
| 117 | [[stage(fragment)]] |
| 118 | fn main() -> void { |
| 119 | FragColor = vec4<f32>(1.0, 1.0, 1.0, 1.0); |
| 120 | return; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 121 | } |
| 122 | )"); |
| 123 | |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 124 | depthStencilView = CreateDefaultDepthStencilView(device); |
| 125 | |
Corentin Wallez | 9895c27 | 2021-03-18 16:46:58 +0000 | [diff] [blame] | 126 | utils::ComboRenderPipelineDescriptor2 descriptor; |
Yunchao He | 889d743 | 2019-03-27 18:08:50 +0000 | [diff] [blame] | 127 | |
Corentin Wallez | 9895c27 | 2021-03-18 16:46:58 +0000 | [diff] [blame] | 128 | descriptor.vertex.module = vsModule; |
| 129 | descriptor.vertex.bufferCount = 2; |
| 130 | descriptor.cBuffers[0].arrayStride = sizeof(Particle); |
| 131 | descriptor.cBuffers[0].stepMode = wgpu::InputStepMode::Instance; |
| 132 | descriptor.cBuffers[0].attributeCount = 2; |
| 133 | descriptor.cAttributes[0].offset = offsetof(Particle, pos); |
| 134 | descriptor.cAttributes[0].format = wgpu::VertexFormat::Float32x2; |
| 135 | descriptor.cAttributes[1].shaderLocation = 1; |
| 136 | descriptor.cAttributes[1].offset = offsetof(Particle, vel); |
| 137 | descriptor.cAttributes[1].format = wgpu::VertexFormat::Float32x2; |
| 138 | descriptor.cBuffers[1].arrayStride = sizeof(glm::vec2); |
| 139 | descriptor.cBuffers[1].attributeCount = 1; |
| 140 | descriptor.cBuffers[1].attributes = &descriptor.cAttributes[2]; |
| 141 | descriptor.cAttributes[2].shaderLocation = 2; |
| 142 | descriptor.cAttributes[2].format = wgpu::VertexFormat::Float32x2; |
Yan, Shaobo | a492427 | 2018-12-10 19:47:22 +0000 | [diff] [blame] | 143 | |
Corentin Wallez | 9895c27 | 2021-03-18 16:46:58 +0000 | [diff] [blame] | 144 | descriptor.cFragment.module = fsModule; |
| 145 | descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8); |
| 146 | descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat(); |
| 147 | |
| 148 | renderPipeline = device.CreateRenderPipeline2(&descriptor); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void initSim() { |
dan sinclair | e6ca254 | 2021-01-12 22:11:14 +0000 | [diff] [blame] | 152 | wgpu::ShaderModule module = utils::CreateShaderModuleFromWGSL(device, R"( |
Stephen White | d675447 | 2021-02-23 20:30:39 +0000 | [diff] [blame] | 153 | struct Particle { |
Ben Clayton | c568684 | 2021-03-17 09:48:19 +0000 | [diff] [blame] | 154 | pos : vec2<f32>; |
| 155 | vel : vec2<f32>; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 156 | }; |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 157 | [[block]] struct SimParams { |
Ben Clayton | c568684 | 2021-03-17 09:48:19 +0000 | [diff] [blame] | 158 | deltaT : f32; |
| 159 | rule1Distance : f32; |
| 160 | rule2Distance : f32; |
| 161 | rule3Distance : f32; |
| 162 | rule1Scale : f32; |
| 163 | rule2Scale : f32; |
| 164 | rule3Scale : f32; |
| 165 | particleCount : u32; |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 166 | }; |
| 167 | [[block]] struct Particles { |
Ben Clayton | c568684 | 2021-03-17 09:48:19 +0000 | [diff] [blame] | 168 | particles : array<Particle>; |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 169 | }; |
dan sinclair | 0f9c2d7 | 2021-01-19 14:18:51 +0000 | [diff] [blame] | 170 | [[binding(0), group(0)]] var<uniform> params : SimParams; |
Ryan Harrison | bbabda3 | 2021-03-18 17:20:48 +0000 | [diff] [blame^] | 171 | [[binding(1), group(0)]] var<storage> particlesA : [[access(read)]] Particles; |
| 172 | [[binding(2), group(0)]] var<storage> particlesB : [[access(read_write)]] Particles; |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 173 | [[builtin(global_invocation_id)]] var<in> GlobalInvocationID : vec3<u32>; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 174 | |
dan sinclair | e6ca254 | 2021-01-12 22:11:14 +0000 | [diff] [blame] | 175 | // https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 176 | [[stage(compute)]] |
| 177 | fn main() -> void { |
| 178 | var index : u32 = GlobalInvocationID.x; |
| 179 | if (index >= params.particleCount) { |
| 180 | return; |
| 181 | } |
| 182 | var vPos : vec2<f32> = particlesA.particles[index].pos; |
| 183 | var vVel : vec2<f32> = particlesA.particles[index].vel; |
| 184 | var cMass : vec2<f32> = vec2<f32>(0.0, 0.0); |
| 185 | var cVel : vec2<f32> = vec2<f32>(0.0, 0.0); |
| 186 | var colVel : vec2<f32> = vec2<f32>(0.0, 0.0); |
| 187 | var cMassCount : u32 = 0u; |
| 188 | var cVelCount : u32 = 0u; |
| 189 | var pos : vec2<f32>; |
| 190 | var vel : vec2<f32>; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 191 | |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 192 | for (var i : u32 = 0u; i < params.particleCount; i = i + 1u) { |
| 193 | if (i == index) { |
| 194 | continue; |
| 195 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 196 | |
Kai Ninomiya | 7883e7e | 2018-07-10 11:01:28 -0700 | [diff] [blame] | 197 | pos = particlesA.particles[i].pos.xy; |
| 198 | vel = particlesA.particles[i].vel.xy; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 199 | if (distance(pos, vPos) < params.rule1Distance) { |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 200 | cMass = cMass + pos; |
| 201 | cMassCount = cMassCount + 1u; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 202 | } |
| 203 | if (distance(pos, vPos) < params.rule2Distance) { |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 204 | colVel = colVel - (pos - vPos); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 205 | } |
| 206 | if (distance(pos, vPos) < params.rule3Distance) { |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 207 | cVel = cVel + vel; |
| 208 | cVelCount = cVelCount + 1u; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 209 | } |
| 210 | } |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 211 | |
| 212 | if (cMassCount > 0u) { |
| 213 | cMass = (cMass / vec2<f32>(f32(cMassCount), f32(cMassCount))) - vPos; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 216 | if (cVelCount > 0u) { |
| 217 | cVel = cVel / vec2<f32>(f32(cVelCount), f32(cVelCount)); |
| 218 | } |
| 219 | vVel = vVel + (cMass * params.rule1Scale) + (colVel * params.rule2Scale) + |
| 220 | (cVel * params.rule3Scale); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 221 | |
dan sinclair | e6ca254 | 2021-01-12 22:11:14 +0000 | [diff] [blame] | 222 | // clamp velocity for a more pleasing simulation |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 223 | vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1); |
dan sinclair | e6ca254 | 2021-01-12 22:11:14 +0000 | [diff] [blame] | 224 | // kinematic update |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 225 | vPos = vPos + (vVel * params.deltaT); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 226 | |
dan sinclair | e6ca254 | 2021-01-12 22:11:14 +0000 | [diff] [blame] | 227 | // Wrap around boundary |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 228 | if (vPos.x < -1.0) { |
| 229 | vPos.x = 1.0; |
| 230 | } |
| 231 | if (vPos.x > 1.0) { |
| 232 | vPos.x = -1.0; |
| 233 | } |
| 234 | if (vPos.y < -1.0) { |
| 235 | vPos.y = 1.0; |
| 236 | } |
| 237 | if (vPos.y > 1.0) { |
| 238 | vPos.y = -1.0; |
| 239 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 240 | |
dan sinclair | e6ca254 | 2021-01-12 22:11:14 +0000 | [diff] [blame] | 241 | // Write back |
Kai Ninomiya | 7883e7e | 2018-07-10 11:01:28 -0700 | [diff] [blame] | 242 | particlesB.particles[index].pos = vPos; |
Kai Ninomiya | 7883e7e | 2018-07-10 11:01:28 -0700 | [diff] [blame] | 243 | particlesB.particles[index].vel = vVel; |
Corentin Wallez | 4814bdb | 2020-11-26 16:39:46 +0000 | [diff] [blame] | 244 | return; |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 245 | } |
| 246 | )"); |
| 247 | |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 248 | auto bgl = utils::MakeBindGroupLayout( |
| 249 | device, { |
Corentin Wallez | 9895c27 | 2021-03-18 16:46:58 +0000 | [diff] [blame] | 250 | {0, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Uniform}, |
| 251 | {1, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage}, |
| 252 | {2, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage}, |
Kai Ninomiya | 234becf | 2018-07-10 12:23:50 -0700 | [diff] [blame] | 253 | }); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 254 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 255 | wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 256 | |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 257 | wgpu::ComputePipelineDescriptor csDesc; |
Corentin Wallez | aa7109c | 2018-10-25 10:42:49 +0000 | [diff] [blame] | 258 | csDesc.layout = pl; |
Corentin Wallez | a900cce | 2019-09-05 09:41:17 +0000 | [diff] [blame] | 259 | csDesc.computeStage.module = module; |
| 260 | csDesc.computeStage.entryPoint = "main"; |
Corentin Wallez | 8e335a5 | 2018-08-27 23:12:56 +0200 | [diff] [blame] | 261 | updatePipeline = device.CreateComputePipeline(&csDesc); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 262 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 263 | for (uint32_t i = 0; i < 2; ++i) { |
Kai Ninomiya | 2afea0c | 2020-07-10 20:33:08 +0000 | [diff] [blame] | 264 | updateBGs[i] = utils::MakeBindGroup( |
| 265 | device, bgl, |
| 266 | { |
| 267 | {0, updateParams, 0, sizeof(SimParams)}, |
| 268 | {1, particleBuffers[i], 0, kNumParticles * sizeof(Particle)}, |
| 269 | {2, particleBuffers[(i + 1) % 2], 0, kNumParticles * sizeof(Particle)}, |
| 270 | }); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 274 | wgpu::CommandBuffer createCommandBuffer(const wgpu::TextureView backbufferView, size_t i) { |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 275 | auto& bufferDst = particleBuffers[(i + 1) % 2]; |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 276 | wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 277 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 278 | { |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 279 | wgpu::ComputePassEncoder pass = encoder.BeginComputePass(); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 280 | pass.SetPipeline(updatePipeline); |
Corentin Wallez | 70c8c10 | 2019-10-09 16:08:42 +0000 | [diff] [blame] | 281 | pass.SetBindGroup(0, updateBGs[i]); |
Corentin Wallez | 3da19b8 | 2020-03-31 16:23:35 +0000 | [diff] [blame] | 282 | pass.Dispatch(kNumParticles); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 283 | pass.EndPass(); |
| 284 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 285 | |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 286 | { |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 287 | utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView); |
Corentin Wallez | 04863c4 | 2019-10-25 11:36:47 +0000 | [diff] [blame] | 288 | wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass); |
Yan, Shaobo | 300eec0 | 2018-12-21 10:40:26 +0000 | [diff] [blame] | 289 | pass.SetPipeline(renderPipeline); |
François Beaufort | 91b2142 | 2019-10-10 07:29:58 +0000 | [diff] [blame] | 290 | pass.SetVertexBuffer(0, bufferDst); |
| 291 | pass.SetVertexBuffer(1, modelBuffer); |
Corentin Wallez | 67b1ad7 | 2020-03-31 16:21:35 +0000 | [diff] [blame] | 292 | pass.Draw(3, kNumParticles); |
Corentin Wallez | 82fbccb | 2018-09-21 00:24:37 +0000 | [diff] [blame] | 293 | pass.EndPass(); |
| 294 | } |
| 295 | |
Corentin Wallez | e1f0d4e | 2019-02-15 12:54:08 +0000 | [diff] [blame] | 296 | return encoder.Finish(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | void init() { |
Corentin Wallez | 39039fa | 2018-07-18 14:06:10 +0200 | [diff] [blame] | 300 | device = CreateCppDawnDevice(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 301 | |
Corentin Wallez | 6d315da | 2021-02-04 15:33:42 +0000 | [diff] [blame] | 302 | queue = device.GetQueue(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 303 | swapchain = GetSwapChain(device); |
Corentin Wallez | 6b08781 | 2020-10-27 15:35:56 +0000 | [diff] [blame] | 304 | swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment, |
Corentin Wallez | 9e9e29f | 2019-08-27 08:21:39 +0000 | [diff] [blame] | 305 | 640, 480); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 306 | |
| 307 | initBuffers(); |
| 308 | initRender(); |
| 309 | initSim(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void frame() { |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 313 | wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 314 | |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 315 | wgpu::CommandBuffer commandBuffer = createCommandBuffer(backbufferView, pingpong); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 316 | queue.Submit(1, &commandBuffer); |
Corentin Wallez | 604072b | 2019-11-12 18:30:11 +0000 | [diff] [blame] | 317 | swapchain.Present(); |
Kai Ninomiya | c16a67a | 2017-07-27 18:30:57 -0700 | [diff] [blame] | 318 | DoFlush(); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 319 | |
| 320 | pingpong = (pingpong + 1) % 2; |
| 321 | } |
| 322 | |
| 323 | int main(int argc, const char* argv[]) { |
Corentin Wallez | 9347e8f | 2017-06-19 13:15:13 -0400 | [diff] [blame] | 324 | if (!InitSample(argc, argv)) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 325 | return 1; |
| 326 | } |
| 327 | init(); |
| 328 | |
| 329 | while (!ShouldQuit()) { |
| 330 | frame(); |
Corentin Wallez | 134e080 | 2017-07-17 17:13:57 -0400 | [diff] [blame] | 331 | utils::USleep(16000); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // TODO release stuff |
| 335 | } |