2017-04-20 18:38:20 +00:00
|
|
|
// Copyright 2017 The NXT Authors
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
#include "utils/NXTHelpers.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>
|
|
|
|
|
|
|
|
nxt::Device device;
|
|
|
|
nxt::Queue queue;
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::SwapChain swapchain;
|
|
|
|
nxt::TextureView depthStencilView;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
nxt::Buffer modelBuffer;
|
|
|
|
std::array<nxt::Buffer, 2> particleBuffers;
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
nxt::RenderPipeline renderPipeline;
|
2017-05-16 21:04:22 +00:00
|
|
|
nxt::RenderPass renderpass;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
nxt::Buffer updateParams;
|
2017-07-14 15:32:57 +00:00
|
|
|
nxt::ComputePipeline updatePipeline;
|
2017-04-20 18:38:20 +00:00
|
|
|
std::array<nxt::BindGroup, 2> updateBGs;
|
|
|
|
|
|
|
|
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},
|
|
|
|
};
|
2017-06-19 17:09:41 +00:00
|
|
|
modelBuffer = utils::CreateFrozenBufferFromData(device, model, sizeof(model), nxt::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 };
|
2017-06-19 17:09:41 +00:00
|
|
|
updateParams = utils::CreateFrozenBufferFromData(device, ¶ms, sizeof(params), nxt::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++) {
|
2017-04-20 18:38:20 +00:00
|
|
|
particleBuffers[i] = device.CreateBufferBuilder()
|
2017-06-12 21:33:44 +00:00
|
|
|
.SetAllowedUsage(nxt::BufferUsageBit::TransferDst | nxt::BufferUsageBit::Vertex | nxt::BufferUsageBit::Storage)
|
|
|
|
.SetInitialUsage(nxt::BufferUsageBit::TransferDst)
|
2017-04-20 18:38:20 +00:00
|
|
|
.SetSize(sizeof(Particle) * kNumParticles)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
particleBuffers[i].SetSubData(0,
|
|
|
|
sizeof(Particle) * kNumParticles / sizeof(uint32_t),
|
|
|
|
reinterpret_cast<uint32_t*>(initialParticles.data()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void initRender() {
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule vsModule = utils::CreateShaderModule(device, nxt::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);
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule fsModule = utils::CreateShaderModule(device, nxt::ShaderStage::Fragment, R"(
|
2017-04-20 18:38:20 +00:00
|
|
|
#version 450
|
|
|
|
out vec4 fragColor;
|
|
|
|
void main() {
|
|
|
|
fragColor = vec4(1.0);
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
|
|
|
nxt::InputState inputState = device.CreateInputStateBuilder()
|
|
|
|
.SetAttribute(0, 0, nxt::VertexFormat::FloatR32G32, offsetof(Particle, pos))
|
|
|
|
.SetAttribute(1, 0, nxt::VertexFormat::FloatR32G32, offsetof(Particle, vel))
|
|
|
|
.SetInput(0, sizeof(Particle), nxt::InputStepMode::Instance)
|
|
|
|
.SetAttribute(2, 1, nxt::VertexFormat::FloatR32G32, 0)
|
|
|
|
.SetInput(1, sizeof(glm::vec2), nxt::InputStepMode::Vertex)
|
|
|
|
.GetResult();
|
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
renderpass = CreateDefaultRenderPass(device);
|
|
|
|
depthStencilView = CreateDefaultDepthStencilView(device);
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
renderPipeline = device.CreateRenderPipelineBuilder()
|
2017-05-16 21:04:22 +00:00
|
|
|
.SetSubpass(renderpass, 0)
|
2017-04-20 18:38:20 +00:00
|
|
|
.SetStage(nxt::ShaderStage::Vertex, vsModule, "main")
|
|
|
|
.SetStage(nxt::ShaderStage::Fragment, fsModule, "main")
|
|
|
|
.SetInputState(inputState)
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
void initSim() {
|
2017-06-19 17:09:41 +00:00
|
|
|
nxt::ShaderModule module = utils::CreateShaderModule(device, nxt::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 {
|
2017-07-05 21:29:34 +00:00
|
|
|
Particle particle;
|
|
|
|
} particlesA[1000];
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
layout(std140, set = 0, binding = 2) buffer ParticlesB {
|
2017-07-05 21:29:34 +00:00
|
|
|
Particle particle;
|
|
|
|
} particlesB[1000];
|
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; }
|
|
|
|
|
2017-07-05 21:29:34 +00:00
|
|
|
vec2 vPos = particlesA[index].particle.pos;
|
|
|
|
vec2 vVel = particlesA[index].particle.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; }
|
2017-07-05 21:29:34 +00:00
|
|
|
pos = particlesA[i].particle.pos.xy;
|
|
|
|
vel = particlesA[i].particle.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;
|
|
|
|
|
2017-07-05 21:29:34 +00:00
|
|
|
particlesB[index].particle.pos = vPos;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
// Write back
|
2017-07-05 21:29:34 +00:00
|
|
|
particlesB[index].particle.vel = vVel;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
)");
|
|
|
|
|
|
|
|
nxt::BindGroupLayout bgl = device.CreateBindGroupLayoutBuilder()
|
|
|
|
.SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::UniformBuffer, 0, 1)
|
|
|
|
.SetBindingsType(nxt::ShaderStageBit::Compute, nxt::BindingType::StorageBuffer, 1, 2)
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
nxt::PipelineLayout pl = device.CreatePipelineLayoutBuilder()
|
|
|
|
.SetBindGroupLayout(0, bgl)
|
|
|
|
.GetResult();
|
|
|
|
|
2017-07-14 15:32:57 +00:00
|
|
|
updatePipeline = device.CreateComputePipelineBuilder()
|
2017-04-20 18:38:20 +00:00
|
|
|
.SetLayout(pl)
|
|
|
|
.SetStage(nxt::ShaderStage::Compute, module, "main")
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
nxt::BufferView updateParamsView = updateParams.CreateBufferViewBuilder()
|
|
|
|
.SetExtent(0, sizeof(SimParams))
|
|
|
|
.GetResult();
|
|
|
|
|
|
|
|
std::array<nxt::BufferView, 2> views;
|
|
|
|
for (uint32_t i = 0; i < 2; ++i) {
|
|
|
|
views[i] = particleBuffers[i].CreateBufferViewBuilder()
|
|
|
|
.SetExtent(0, kNumParticles * sizeof(Particle))
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 2; ++i) {
|
|
|
|
updateBGs[i] = device.CreateBindGroupBuilder()
|
|
|
|
.SetLayout(bgl)
|
|
|
|
.SetUsage(nxt::BindGroupUsage::Frozen)
|
|
|
|
.SetBufferViews(0, 1, &updateParamsView)
|
|
|
|
.SetBufferViews(1, 1, &views[i])
|
|
|
|
.SetBufferViews(2, 1, &views[(i + 1) % 2])
|
|
|
|
.GetResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::CommandBuffer createCommandBuffer(const nxt::Framebuffer& framebuffer, 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& bufferSrc = particleBuffers[i];
|
|
|
|
auto& bufferDst = particleBuffers[(i + 1) % 2];
|
|
|
|
return device.CreateCommandBufferBuilder()
|
|
|
|
.BeginComputePass()
|
|
|
|
.SetComputePipeline(updatePipeline)
|
|
|
|
.TransitionBufferUsage(bufferSrc, nxt::BufferUsageBit::Storage)
|
|
|
|
.TransitionBufferUsage(bufferDst, nxt::BufferUsageBit::Storage)
|
|
|
|
.SetBindGroup(0, updateBGs[i])
|
|
|
|
.Dispatch(kNumParticles, 1, 1)
|
|
|
|
.EndComputePass()
|
|
|
|
|
|
|
|
.BeginRenderPass(renderpass, framebuffer)
|
|
|
|
.BeginRenderSubpass()
|
|
|
|
.SetRenderPipeline(renderPipeline)
|
|
|
|
.TransitionBufferUsage(bufferDst, nxt::BufferUsageBit::Vertex)
|
|
|
|
.SetVertexBuffers(0, 1, &bufferDst, zeroOffsets)
|
|
|
|
.SetVertexBuffers(1, 1, &modelBuffer, zeroOffsets)
|
|
|
|
.DrawArrays(3, kNumParticles, 0, 0)
|
|
|
|
.EndRenderSubpass()
|
|
|
|
.EndRenderPass()
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-28 01:30:57 +00:00
|
|
|
.GetResult();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init() {
|
2017-05-29 18:30:29 +00:00
|
|
|
device = CreateCppNXTDevice();
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
queue = device.CreateQueueBuilder().GetResult();
|
2017-07-28 01:30:57 +00:00
|
|
|
swapchain = GetSwapChain(device);
|
2017-08-30 23:53:27 +00:00
|
|
|
swapchain.Configure(nxt::TextureFormat::R8G8B8A8Unorm, nxt::TextureUsageBit::OutputAttachment, 640, 480);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
initBuffers();
|
|
|
|
initRender();
|
|
|
|
initSim();
|
|
|
|
}
|
|
|
|
|
|
|
|
void frame() {
|
2017-07-28 01:30:57 +00:00
|
|
|
nxt::Texture backbuffer;
|
|
|
|
nxt::Framebuffer framebuffer;
|
|
|
|
GetNextFramebuffer(device, renderpass, swapchain, depthStencilView, &backbuffer, &framebuffer);
|
|
|
|
|
|
|
|
nxt::CommandBuffer commandBuffer = createCommandBuffer(framebuffer, pingpong);
|
|
|
|
queue.Submit(1, &commandBuffer);
|
|
|
|
backbuffer.TransitionUsage(nxt::TextureUsageBit::Present);
|
|
|
|
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
|
|
|
|
}
|