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"
|
2018-07-18 12:00:56 +00:00
|
|
|
#include "utils/DawnHelpers.h"
|
2017-07-17 21:13:57 +00:00
|
|
|
#include "utils/SystemUtils.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>
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Device device;
|
|
|
|
dawn::Queue queue;
|
|
|
|
dawn::SwapChain swapchain;
|
|
|
|
dawn::TextureView depthStencilView;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Buffer modelBuffer;
|
|
|
|
std::array<dawn::Buffer, 2> particleBuffers;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::RenderPipeline renderPipeline;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Buffer updateParams;
|
|
|
|
dawn::ComputePipeline updatePipeline;
|
|
|
|
std::array<dawn::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},
|
|
|
|
};
|
2018-07-18 11:45:46 +00:00
|
|
|
modelBuffer = utils::CreateBufferFromData(device, model, sizeof(model), dawn::BufferUsageBit::Vertex);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-11 01:44:06 +00:00
|
|
|
SimParams params = { 0.04f, 0.1f, 0.025f, 0.025f, 0.02f, 0.05f, 0.005f, kNumParticles };
|
2018-07-18 11:45:46 +00:00
|
|
|
updateParams = utils::CreateBufferFromData(device, ¶ms, sizeof(params), dawn::BufferUsageBit::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);
|
|
|
|
for (auto& p : initialParticles)
|
|
|
|
{
|
|
|
|
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++) {
|
2018-08-22 13:37:29 +00:00
|
|
|
dawn::BufferDescriptor descriptor;
|
|
|
|
descriptor.size = sizeof(Particle) * kNumParticles;
|
|
|
|
descriptor.usage = dawn::BufferUsageBit::TransferDst | dawn::BufferUsageBit::Vertex | dawn::BufferUsageBit::Storage;
|
|
|
|
particleBuffers[i] = device.CreateBuffer(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
particleBuffers[i].SetSubData(0,
|
2018-02-04 16:07:02 +00:00
|
|
|
sizeof(Particle) * kNumParticles,
|
|
|
|
reinterpret_cast<uint8_t*>(initialParticles.data()));
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void initRender() {
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::ShaderModule vsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Vertex, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
layout(location = 0) in vec2 a_particlePos;
|
|
|
|
layout(location = 1) in vec2 a_particleVel;
|
|
|
|
layout(location = 2) in vec2 a_pos;
|
|
|
|
void main() {
|
|
|
|
float angle = -atan(a_particleVel.x, a_particleVel.y);
|
|
|
|
vec2 pos = vec2(a_pos.x * cos(angle) - a_pos.y * sin(angle),
|
|
|
|
a_pos.x * sin(angle) + a_pos.y * cos(angle));
|
|
|
|
gl_Position = vec4(pos + a_particlePos, 0, 1);
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::ShaderModule fsModule = utils::CreateShaderModule(device, dawn::ShaderStage::Fragment, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
2017-08-29 17:37:45 +00:00
|
|
|
layout(location = 0) out vec4 fragColor;
|
2017-04-20 18:38:20 +00:00
|
|
|
void main() {
|
|
|
|
fragColor = vec4(1.0);
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
2019-02-14 00:35:39 +00:00
|
|
|
dawn::VertexAttributeDescriptor attribute1;
|
|
|
|
attribute1.shaderLocation = 0;
|
|
|
|
attribute1.inputSlot = 0;
|
|
|
|
attribute1.offset = offsetof(Particle, pos);
|
|
|
|
attribute1.format = dawn::VertexFormat::FloatR32G32;
|
|
|
|
|
|
|
|
dawn::VertexAttributeDescriptor attribute2;
|
|
|
|
attribute2.shaderLocation = 1;
|
|
|
|
attribute2.inputSlot = 0;
|
|
|
|
attribute2.offset = offsetof(Particle, vel);
|
|
|
|
attribute2.format = dawn::VertexFormat::FloatR32G32;
|
|
|
|
|
2019-02-14 23:56:07 +00:00
|
|
|
dawn::VertexInputDescriptor input1;
|
|
|
|
input1.inputSlot = 0;
|
|
|
|
input1.stride = sizeof(Particle);
|
|
|
|
input1.stepMode = dawn::InputStepMode::Instance;
|
|
|
|
|
2019-02-14 00:35:39 +00:00
|
|
|
dawn::VertexAttributeDescriptor attribute3;
|
|
|
|
attribute3.shaderLocation = 2;
|
|
|
|
attribute3.inputSlot = 1;
|
|
|
|
attribute3.offset = 0;
|
|
|
|
attribute3.format = dawn::VertexFormat::FloatR32G32;
|
|
|
|
|
2019-02-14 23:56:07 +00:00
|
|
|
dawn::VertexInputDescriptor input2;
|
|
|
|
input2.inputSlot = 1;
|
|
|
|
input2.stride = sizeof(glm::vec2);
|
|
|
|
input2.stepMode = dawn::InputStepMode::Vertex;
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::InputState inputState = device.CreateInputStateBuilder()
|
2019-02-14 00:35:39 +00:00
|
|
|
.SetAttribute(&attribute1)
|
|
|
|
.SetAttribute(&attribute2)
|
2019-02-14 23:56:07 +00:00
|
|
|
.SetInput(&input1)
|
2019-02-14 00:35:39 +00:00
|
|
|
.SetAttribute(&attribute3)
|
2019-02-14 23:56:07 +00:00
|
|
|
.SetInput(&input2)
|
2019-02-14 00:35:39 +00:00
|
|
|
.GetResult();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
depthStencilView = CreateDefaultDepthStencilView(device);
|
|
|
|
|
2018-12-10 19:47:22 +00:00
|
|
|
utils::ComboRenderPipelineDescriptor descriptor(device);
|
|
|
|
descriptor.cVertexStage.module = vsModule;
|
|
|
|
descriptor.cFragmentStage.module = fsModule;
|
|
|
|
descriptor.inputState = inputState;
|
2019-02-15 02:20:57 +00:00
|
|
|
descriptor.depthStencilState = &descriptor.cDepthStencilState;
|
|
|
|
descriptor.cDepthStencilState.format = dawn::TextureFormat::D32FloatS8Uint;
|
|
|
|
descriptor.cColorStates[0].format = GetPreferredSwapChainTextureFormat();
|
2018-12-10 19:47:22 +00:00
|
|
|
|
|
|
|
renderPipeline = device.CreateRenderPipeline(&descriptor);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initSim() {
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::ShaderModule module = utils::CreateShaderModule(device, dawn::ShaderStage::Compute, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
struct Particle {
|
|
|
|
vec2 pos;
|
|
|
|
vec2 vel;
|
|
|
|
};
|
|
|
|
|
|
|
|
layout(std140, set = 0, binding = 0) uniform SimParams {
|
|
|
|
float deltaT;
|
|
|
|
float rule1Distance;
|
|
|
|
float rule2Distance;
|
|
|
|
float rule3Distance;
|
|
|
|
float rule1Scale;
|
|
|
|
float rule2Scale;
|
|
|
|
float rule3Scale;
|
|
|
|
int particleCount;
|
|
|
|
} params;
|
|
|
|
|
|
|
|
layout(std140, set = 0, binding = 1) buffer ParticlesA {
|
2018-07-10 18:01:28 +00:00
|
|
|
Particle particles[1000];
|
|
|
|
} particlesA;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
layout(std140, set = 0, binding = 2) buffer ParticlesB {
|
2018-07-10 18:01:28 +00:00
|
|
|
Particle particles[1000];
|
|
|
|
} particlesB;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
// https://github.com/austinEng/Project6-Vulkan-Flocking/blob/master/data/shaders/computeparticles/particle.comp
|
|
|
|
|
|
|
|
uint index = gl_GlobalInvocationID.x;
|
|
|
|
if (index >= params.particleCount) { return; }
|
|
|
|
|
2018-07-10 18:01:28 +00:00
|
|
|
vec2 vPos = particlesA.particles[index].pos;
|
|
|
|
vec2 vVel = particlesA.particles[index].vel;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
vec2 cMass = vec2(0.0, 0.0);
|
|
|
|
vec2 cVel = vec2(0.0, 0.0);
|
|
|
|
vec2 colVel = vec2(0.0, 0.0);
|
|
|
|
int cMassCount = 0;
|
|
|
|
int cVelCount = 0;
|
|
|
|
|
|
|
|
vec2 pos;
|
|
|
|
vec2 vel;
|
|
|
|
for (int i = 0; i < params.particleCount; ++i) {
|
|
|
|
if (i == index) { continue; }
|
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) {
|
|
|
|
cMass += pos;
|
|
|
|
cMassCount++;
|
|
|
|
}
|
|
|
|
if (distance(pos, vPos) < params.rule2Distance) {
|
|
|
|
colVel -= (pos - vPos);
|
|
|
|
}
|
|
|
|
if (distance(pos, vPos) < params.rule3Distance) {
|
|
|
|
cVel += vel;
|
|
|
|
cVelCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cMassCount > 0) {
|
|
|
|
cMass = cMass / cMassCount - vPos;
|
|
|
|
}
|
|
|
|
if (cVelCount > 0) {
|
|
|
|
cVel = cVel / cVelCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
vVel += cMass * params.rule1Scale + colVel * params.rule2Scale + cVel * params.rule3Scale;
|
|
|
|
|
|
|
|
// clamp velocity for a more pleasing simulation.
|
|
|
|
vVel = normalize(vVel) * clamp(length(vVel), 0.0, 0.1);
|
|
|
|
|
|
|
|
// kinematic update
|
|
|
|
vPos += vVel * params.deltaT;
|
|
|
|
|
|
|
|
// Wrap around boundary
|
|
|
|
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;
|
|
|
|
|
2018-07-10 18:01:28 +00:00
|
|
|
particlesB.particles[index].pos = vPos;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
// Write back
|
2018-07-10 18:01:28 +00:00
|
|
|
particlesB.particles[index].vel = vVel;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
2018-07-10 19:23:50 +00:00
|
|
|
auto bgl = utils::MakeBindGroupLayout(
|
|
|
|
device, {
|
2018-07-18 11:45:46 +00:00
|
|
|
{0, dawn::ShaderStageBit::Compute, dawn::BindingType::UniformBuffer},
|
|
|
|
{1, dawn::ShaderStageBit::Compute, dawn::BindingType::StorageBuffer},
|
|
|
|
{2, dawn::ShaderStageBit::Compute, dawn::BindingType::StorageBuffer},
|
2018-07-10 19:23:50 +00:00
|
|
|
});
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::PipelineLayout pl = utils::MakeBasicPipelineLayout(device, &bgl);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-08-27 21:12:56 +00:00
|
|
|
dawn::ComputePipelineDescriptor csDesc;
|
2018-10-25 10:42:49 +00:00
|
|
|
csDesc.module = module;
|
2018-08-27 21:12:56 +00:00
|
|
|
csDesc.entryPoint = "main";
|
2018-10-25 10:42:49 +00:00
|
|
|
csDesc.layout = pl;
|
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) {
|
2018-12-05 07:18:30 +00:00
|
|
|
updateBGs[i] = utils::MakeBindGroup(device, bgl, {
|
2018-12-07 12:31:53 +00:00
|
|
|
{0, updateParams, 0, sizeof(SimParams)},
|
|
|
|
{1, particleBuffers[i], 0, kNumParticles * sizeof(Particle)},
|
|
|
|
{2, particleBuffers[(i + 1) % 2], 0, kNumParticles * sizeof(Particle)},
|
2018-12-05 07:18:30 +00:00
|
|
|
});
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::CommandBuffer createCommandBuffer(const dawn::RenderPassDescriptor& renderPass, size_t i) {
|
2017-04-20 18:38:20 +00:00
|
|
|
static const uint32_t zeroOffsets[1] = {0};
|
2017-07-28 01:30:57 +00:00
|
|
|
auto& bufferDst = particleBuffers[(i + 1) % 2];
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::CommandEncoder encoder = device.CreateCommandEncoder();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-09-21 00:24:37 +00:00
|
|
|
{
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::ComputePassEncoder pass = encoder.BeginComputePass();
|
2018-12-21 10:40:26 +00:00
|
|
|
pass.SetPipeline(updatePipeline);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.SetBindGroup(0, updateBGs[i]);
|
|
|
|
pass.Dispatch(kNumParticles, 1, 1);
|
|
|
|
pass.EndPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-02-15 12:54:08 +00:00
|
|
|
dawn::RenderPassEncoder pass = encoder.BeginRenderPass(renderPass);
|
2018-12-21 10:40:26 +00:00
|
|
|
pass.SetPipeline(renderPipeline);
|
2018-09-21 00:24:37 +00:00
|
|
|
pass.SetVertexBuffers(0, 1, &bufferDst, zeroOffsets);
|
|
|
|
pass.SetVertexBuffers(1, 1, &modelBuffer, zeroOffsets);
|
2018-12-10 05:20:19 +00:00
|
|
|
pass.Draw(3, kNumParticles, 0, 0);
|
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
|
|
|
|
2018-06-15 00:26:27 +00:00
|
|
|
queue = device.CreateQueue();
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain = GetSwapChain(device);
|
2017-09-21 16:54:53 +00:00
|
|
|
swapchain.Configure(GetPreferredSwapChainTextureFormat(),
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::TextureUsageBit::OutputAttachment, 640, 480);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
initBuffers();
|
|
|
|
initRender();
|
|
|
|
initSim();
|
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::Texture backbuffer;
|
|
|
|
dawn::RenderPassDescriptor renderPass;
|
2018-05-11 17:04:44 +00:00
|
|
|
GetNextRenderPassDescriptor(device, swapchain, depthStencilView, &backbuffer, &renderPass);
|
2017-07-28 01:30:57 +00:00
|
|
|
|
2018-07-18 11:45:46 +00:00
|
|
|
dawn::CommandBuffer commandBuffer = createCommandBuffer(renderPass, pingpong);
|
2017-07-28 01:30:57 +00:00
|
|
|
queue.Submit(1, &commandBuffer);
|
|
|
|
swapchain.Present(backbuffer);
|
|
|
|
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
|
|
|
|
}
|