2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-06-19 17:15:13 +00:00
|
|
|
#include "SampleUtils.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-12-10 19:47:22 +00:00
|
|
|
#include "utils/ComboRenderPipelineDescriptor.h"
|
2017-07-17 21:13:57 +00:00
|
|
|
#include "utils/SystemUtils.h"
|
2019-10-25 11:36:47 +00:00
|
|
|
#include "utils/WGPUHelpers.h"
|
2017-06-19 17:09:41 +00:00
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Device device;
|
|
|
|
wgpu::Queue queue;
|
|
|
|
wgpu::SwapChain swapchain;
|
|
|
|
wgpu::TextureView depthStencilView;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Buffer modelBuffer;
|
|
|
|
std::array<wgpu::Buffer, 2> particleBuffers;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::RenderPipeline renderPipeline;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::Buffer updateParams;
|
|
|
|
wgpu::ComputePipeline updatePipeline;
|
|
|
|
std::array<wgpu::BindGroup, 2> updateBGs;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
size_t pingpong = 0;
|
|
|
|
|
|
|
|
static const uint32_t kNumParticles = 1000;
|
|
|
|
|
|
|
|
struct Particle {
|
|
|
|
glm::vec2 pos;
|
|
|
|
glm::vec2 vel;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SimParams {
|
|
|
|
float deltaT;
|
|
|
|
float rule1Distance;
|
|
|
|
float rule2Distance;
|
|
|
|
float rule3Distance;
|
|
|
|
float rule1Scale;
|
|
|
|
float rule2Scale;
|
|
|
|
float rule3Scale;
|
|
|
|
int particleCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
void initBuffers() {
|
|
|
|
glm::vec2 model[3] = {
|
|
|
|
{-0.01, -0.02},
|
|
|
|
{0.01, -0.02},
|
|
|
|
{0.00, 0.02},
|
|
|
|
};
|
2019-08-27 08:21:39 +00:00
|
|
|
modelBuffer =
|
2019-10-25 11:36:47 +00:00
|
|
|
utils::CreateBufferFromData(device, model, sizeof(model), wgpu::BufferUsage::Vertex);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-07-10 20:33:08 +00:00
|
|
|
SimParams params = {0.04f, 0.1f, 0.025f, 0.025f, 0.02f, 0.05f, 0.005f, kNumParticles};
|
2019-08-27 08:21:39 +00:00
|
|
|
updateParams =
|
2019-10-25 11:36:47 +00:00
|
|
|
utils::CreateBufferFromData(device, ¶ms, sizeof(params), wgpu::BufferUsage::Uniform);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
std::vector<Particle> initialParticles(kNumParticles);
|
|
|
|
{
|
|
|
|
std::mt19937 generator;
|
|
|
|
std::uniform_real_distribution<float> dist(-1.0f, 1.0f);
|
2020-07-10 20:33:08 +00:00
|
|
|
for (auto& p : initialParticles) {
|
2017-04-20 18:38:20 +00:00
|
|
|
p.pos = glm::vec2(dist(generator), dist(generator));
|
|
|
|
p.vel = glm::vec2(dist(generator), dist(generator)) * 0.1f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 00:00:22 +00:00
|
|
|
for (size_t i = 0; i < 2; i++) {
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::BufferDescriptor descriptor;
|
2018-08-22 13:37:29 +00:00
|
|
|
descriptor.size = sizeof(Particle) * kNumParticles;
|
2019-08-27 08:21:39 +00:00
|
|
|
descriptor.usage =
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Vertex | wgpu::BufferUsage::Storage;
|
2018-08-22 13:37:29 +00:00
|
|
|
particleBuffers[i] = device.CreateBuffer(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2020-06-02 09:24:39 +00:00
|
|
|
queue.WriteBuffer(particleBuffers[i], 0,
|
|
|
|
reinterpret_cast<uint8_t*>(initialParticles.data()),
|
|
|
|
sizeof(Particle) * kNumParticles);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void initRender() {
|
2021-03-24 15:55:32 +00:00
|
|
|
wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
|
2021-04-13 14:52:44 +00:00
|
|
|
struct VertexIn {
|
|
|
|
[[location(0)]] a_particlePos : vec2<f32>;
|
|
|
|
[[location(1)]] a_particleVel : vec2<f32>;
|
|
|
|
[[location(2)]] a_pos : vec2<f32>;
|
|
|
|
};
|
2020-11-26 16:39:46 +00:00
|
|
|
|
|
|
|
[[stage(vertex)]]
|
2021-04-13 14:52:44 +00:00
|
|
|
fn main(input : VertexIn) -> [[builtin(position)]] vec4<f32> {
|
|
|
|
var angle : f32 = -atan2(input.a_particleVel.x, input.a_particleVel.y);
|
2020-11-26 16:39:46 +00:00
|
|
|
var pos : vec2<f32> = vec2<f32>(
|
2021-04-13 14:52:44 +00:00
|
|
|
(input.a_pos.x * cos(angle)) - (input.a_pos.y * sin(angle)),
|
|
|
|
(input.a_pos.x * sin(angle)) + (input.a_pos.y * cos(angle)));
|
|
|
|
return vec4<f32>(pos + input.a_particlePos, 0.0, 1.0);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
2021-03-24 15:55:32 +00:00
|
|
|
wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
|
2020-11-26 16:39:46 +00:00
|
|
|
[[stage(fragment)]]
|
2021-04-13 14:52:44 +00:00
|
|
|
fn main() -> [[location(0)]] vec4<f32> {
|
|
|
|
return vec4<f32>(1.0, 1.0, 1.0, 1.0);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
depthStencilView = CreateDefaultDepthStencilView(device);
|
|
|
|
|
2021-05-21 05:01:38 +00:00
|
|
|
utils::ComboRenderPipelineDescriptor descriptor;
|
2021-03-18 16:46:58 +00:00
|
|
|
|
|
|
|
descriptor.vertex.module = vsModule;
|
|
|
|
descriptor.vertex.bufferCount = 2;
|
|
|
|
descriptor.cBuffers[0].arrayStride = sizeof(Particle);
|
|
|
|
descriptor.cBuffers[0].stepMode = wgpu::InputStepMode::Instance;
|
|
|
|
descriptor.cBuffers[0].attributeCount = 2;
|
|
|
|
descriptor.cAttributes[0].offset = offsetof(Particle, pos);
|
|
|
|
descriptor.cAttributes[0].format = wgpu::VertexFormat::Float32x2;
|
|
|
|
descriptor.cAttributes[1].shaderLocation = 1;
|
|
|
|
descriptor.cAttributes[1].offset = offsetof(Particle, vel);
|
|
|
|
descriptor.cAttributes[1].format = wgpu::VertexFormat::Float32x2;
|
|
|
|
descriptor.cBuffers[1].arrayStride = sizeof(glm::vec2);
|
|
|
|
descriptor.cBuffers[1].attributeCount = 1;
|
|
|
|
descriptor.cBuffers[1].attributes = &descriptor.cAttributes[2];
|
|
|
|
descriptor.cAttributes[2].shaderLocation = 2;
|
|
|
|
descriptor.cAttributes[2].format = wgpu::VertexFormat::Float32x2;
|
|
|
|
|
|
|
|
descriptor.cFragment.module = fsModule;
|
|
|
|
descriptor.EnableDepthStencil(wgpu::TextureFormat::Depth24PlusStencil8);
|
|
|
|
descriptor.cTargets[0].format = GetPreferredSwapChainTextureFormat();
|
|
|
|
|
2021-05-21 05:01:38 +00:00
|
|
|
renderPipeline = device.CreateRenderPipeline(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initSim() {
|
2021-03-24 15:55:32 +00:00
|
|
|
wgpu::ShaderModule module = utils::CreateShaderModule(device, R"(
|
2021-02-23 20:30:39 +00:00
|
|
|
struct Particle {
|
2021-03-17 09:48:19 +00:00
|
|
|
pos : vec2<f32>;
|
|
|
|
vel : vec2<f32>;
|
2020-11-26 16:39:46 +00:00
|
|
|
};
|
|
|
|
[[block]] struct SimParams {
|
2021-03-17 09:48:19 +00:00
|
|
|
deltaT : f32;
|
|
|
|
rule1Distance : f32;
|
|
|
|
rule2Distance : f32;
|
|
|
|
rule3Distance : f32;
|
|
|
|
rule1Scale : f32;
|
|
|
|
rule2Scale : f32;
|
|
|
|
rule3Scale : f32;
|
|
|
|
particleCount : u32;
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
2020-11-26 16:39:46 +00:00
|
|
|
[[block]] struct Particles {
|
2021-03-17 09:48:19 +00:00
|
|
|
particles : array<Particle>;
|
2020-11-26 16:39:46 +00:00
|
|
|
};
|
2021-01-19 14:18:51 +00:00
|
|
|
[[binding(0), group(0)]] var<uniform> params : SimParams;
|
2021-06-08 15:36:44 +00:00
|
|
|
[[binding(1), group(0)]] var<storage, read> particlesA : Particles;
|
|
|
|
[[binding(2), group(0)]] var<storage, read_write> particlesB : Particles;
|
2020-11-26 16:39:46 +00:00
|
|
|
|
2021-01-12 22:11:14 +00:00
|
|
|
// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
|
2020-11-26 16:39:46 +00:00
|
|
|
[[stage(compute)]]
|
2021-04-13 14:52:44 +00:00
|
|
|
fn main([[builtin(global_invocation_id)]] GlobalInvocationID : vec3<u32>) {
|
2020-11-26 16:39:46 +00:00
|
|
|
var index : u32 = GlobalInvocationID.x;
|
|
|
|
if (index >= params.particleCount) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var vPos : vec2<f32> = particlesA.particles[index].pos;
|
|
|
|
var vVel : vec2<f32> = particlesA.particles[index].vel;
|
|
|
|
var cMass : vec2<f32> = vec2<f32>(0.0, 0.0);
|
|
|
|
var cVel : vec2<f32> = vec2<f32>(0.0, 0.0);
|
|
|
|
var colVel : vec2<f32> = vec2<f32>(0.0, 0.0);
|
|
|
|
var cMassCount : u32 = 0u;
|
|
|
|
var cVelCount : u32 = 0u;
|
|
|
|
var pos : vec2<f32>;
|
|
|
|
var vel : vec2<f32>;
|
|
|
|
|
|
|
|
for (var i : u32 = 0u; i < params.particleCount; i = i + 1u) {
|
|
|
|
if (i == index) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-10 18:01:28 +00:00
|
|
|
pos = particlesA.particles[i].pos.xy;
|
|
|
|
vel = particlesA.particles[i].vel.xy;
|
2017-04-20 18:38:20 +00:00
|
|
|
if (distance(pos, vPos) < params.rule1Distance) {
|
2020-11-26 16:39:46 +00:00
|
|
|
cMass = cMass + pos;
|
|
|
|
cMassCount = cMassCount + 1u;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
if (distance(pos, vPos) < params.rule2Distance) {
|
2020-11-26 16:39:46 +00:00
|
|
|
colVel = colVel - (pos - vPos);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
if (distance(pos, vPos) < params.rule3Distance) {
|
2020-11-26 16:39:46 +00:00
|
|
|
cVel = cVel + vel;
|
|
|
|
cVelCount = cVelCount + 1u;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 16:39:46 +00:00
|
|
|
|
|
|
|
if (cMassCount > 0u) {
|
|
|
|
cMass = (cMass / vec2<f32>(f32(cMassCount), f32(cMassCount))) - vPos;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 16:39:46 +00:00
|
|
|
if (cVelCount > 0u) {
|
|
|
|
cVel = cVel / vec2<f32>(f32(cVelCount), f32(cVelCount));
|
|
|
|
}
|
|
|
|
vVel = vVel + (cMass * params.rule1Scale) + (colVel * params.rule2Scale) +
|
|
|
|
(cVel * params.rule3Scale);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-01-12 22:11:14 +00:00
|
|
|
// clamp velocity for a more pleasing simulation
|
2017-04-20 18:38:20 +00:00
|
|
|
vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
|
2021-01-12 22:11:14 +00:00
|
|
|
// kinematic update
|
2020-11-26 16:39:46 +00:00
|
|
|
vPos = vPos + (vVel * params.deltaT);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-01-12 22:11:14 +00:00
|
|
|
// Wrap around boundary
|
2020-11-26 16:39:46 +00:00
|
|
|
if (vPos.x < -1.0) {
|
|
|
|
vPos.x = 1.0;
|
|
|
|
}
|
|
|
|
if (vPos.x > 1.0) {
|
|
|
|
vPos.x = -1.0;
|
|
|
|
}
|
|
|
|
if (vPos.y < -1.0) {
|
|
|
|
vPos.y = 1.0;
|
|
|
|
}
|
|
|
|
if (vPos.y > 1.0) {
|
|
|
|
vPos.y = -1.0;
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-01-12 22:11:14 +00:00
|
|
|
// Write back
|
2018-07-10 18:01:28 +00:00
|
|
|
particlesB.particles[index].pos = vPos;
|
|
|
|
particlesB.particles[index].vel = vVel;
|
2020-11-26 16:39:46 +00:00
|
|
|
return;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
2018-07-10 19:23:50 +00:00
|
|
|
auto bgl = utils::MakeBindGroupLayout(
|
|
|
|
device, {
|
2021-03-18 16:46:58 +00:00
|
|
|
{0, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Uniform},
|
|
|
|
{1, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage},
|
|
|
|
{2, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage},
|
2018-07-10 19:23:50 +00:00
|
|
|
});
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::ComputePipelineDescriptor csDesc;
|
2018-10-25 10:42:49 +00:00
|
|
|
csDesc.layout = pl;
|
2021-06-09 18:07:32 +00:00
|
|
|
csDesc.compute.module = module;
|
|
|
|
csDesc.compute.entryPoint = "main";
|
2018-08-27 21:12:56 +00:00
|
|
|
updatePipeline = device.CreateComputePipeline(&csDesc);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 2; ++i) {
|
2020-07-10 20:33:08 +00:00
|
|
|
updateBGs[i] = utils::MakeBindGroup(
|
|
|
|
device, bgl,
|
|
|
|
{
|
|
|
|
{0, updateParams, 0, sizeof(SimParams)},
|
|
|
|
{1, particleBuffers[i], 0, kNumParticles * sizeof(Particle)},
|
|
|
|
{2, particleBuffers[(i + 1) % 2], 0, kNumParticles * sizeof(Particle)},
|
|
|
|
});
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 18:30:11 +00:00
|
|
|
wgpu::CommandBuffer createCommandBuffer(const wgpu::TextureView backbufferView, size_t i) {
|
2017-07-28 01:30:57 +00:00
|
|
|
auto& bufferDst = particleBuffers[(i + 1) % 2];
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
{
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
|
2018-12-21 10:40:26 +00:00
|
|
|
pass.SetPipeline(updatePipeline);
|
2019-10-09 16:08:42 +00:00
|
|
|
pass.SetBindGroup(0, updateBGs[i]);
|
2020-03-31 16:23:35 +00:00
|
|
|
pass.Dispatch(kNumParticles);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.EndPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-11-12 18:30:11 +00:00
|
|
|
utils::ComboRenderPassDescriptor renderPass({backbufferView}, depthStencilView);
|
2019-10-25 11:36:47 +00:00
|
|
|
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass);
|
2018-12-21 10:40:26 +00:00
|
|
|
pass.SetPipeline(renderPipeline);
|
2019-10-10 07:29:58 +00:00
|
|
|
pass.SetVertexBuffer(0, bufferDst);
|
|
|
|
pass.SetVertexBuffer(1, modelBuffer);
|
2020-03-31 16:21:35 +00:00
|
|
|
pass.Draw(3, kNumParticles);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.EndPass();
|
|
|
|
}
|
|
|
|
|
2019-02-15 12:54:08 +00:00
|
|
|
return encoder.Finish();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init() {
|
2018-07-18 12:06:10 +00:00
|
|
|
device = CreateCppDawnDevice();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2021-02-04 15:33:42 +00:00
|
|
|
queue = device.GetQueue();
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain = GetSwapChain(device);
|
2020-10-27 15:35:56 +00:00
|
|
|
swapchain.Configure(GetPreferredSwapChainTextureFormat(), wgpu::TextureUsage::RenderAttachment,
|
2019-08-27 08:21:39 +00:00
|
|
|
640, 480);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
initBuffers();
|
|
|
|
initRender();
|
|
|
|
initSim();
|
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2019-11-12 18:30:11 +00:00
|
|
|
wgpu::TextureView backbufferView = swapchain.GetCurrentTextureView();
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2019-11-12 18:30:11 +00:00
|
|
|
wgpu::CommandBuffer commandBuffer = createCommandBuffer(backbufferView, pingpong);
|
2017-07-28 01:30:57 +00:00
|
|
|
queue.Submit(1, &commandBuffer);
|
2019-11-12 18:30:11 +00:00
|
|
|
swapchain.Present();
|
2017-07-28 01:30:57 +00:00
|
|
|
DoFlush();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
pingpong = (pingpong + 1) % 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2017-06-19 17:15:13 +00:00
|
|
|
if (!InitSample(argc, argv)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
init();
|
|
|
|
|
|
|
|
while (!ShouldQuit()) {
|
|
|
|
frame();
|
2017-07-17 21:13:57 +00:00
|
|
|
utils::USleep(16000);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO release stuff
|
|
|
|
}
|