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