Skip to content
Snippets Groups Projects
Commit f16cf13c authored by Sebastian Gaida's avatar Sebastian Gaida
Browse files

[#69] added computePipeline

parent 40f10345
No related branches found
No related tags found
1 merge request!56Resolve "Partikelsystem"
Pipeline #25735 passed
File added
%VULKAN_SDK%\Bin32\glslc.exe shader.vert -o vert.spv %VULKAN_SDK%\Bin32\glslc.exe shader.vert -o vert.spv
%VULKAN_SDK%\Bin32\glslc.exe shader.frag -o frag.spv %VULKAN_SDK%\Bin32\glslc.exe shader.frag -o frag.spv
%VULKAN_SDK%\Bin32\glslc.exe shader1.comp -o comp1.spv
pause pause
\ No newline at end of file
...@@ -3,6 +3,19 @@ ...@@ -3,6 +3,19 @@
layout(location = 0) in vec3 particle; layout(location = 0) in vec3 particle;
struct Particle
{
vec3 position;
float lifeTime;
vec3 velocity;
float padding_2;
};
layout(std430, binding = 2) coherent buffer buffer_inParticle
{
Particle inParticle[];
};
layout( push_constant ) uniform constants{ layout( push_constant ) uniform constants{
mat4 mvp; mat4 mvp;
}; };
......
#version 450 core
#extension GL_ARB_separate_shader_objects : enable
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
struct Particle
{
vec3 position;
float lifeTime;
vec3 velocity;
float padding_2;
};
layout(std430, binding = 0) coherent buffer buffer_inParticle
{
Particle inParticle[];
};
void main() {
uint id = gl_GlobalInvocationID.x;
inParticle[id].position = inParticle[id].position + vec3(0.f,0.01f,0.f);
}
No preview for this file type
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include "Particle.hpp" #include "Particle.hpp"
#include <random> #include <random>
#include "vkcv/Buffer.hpp"
class ParticleSystem { class ParticleSystem {
......
...@@ -47,12 +47,28 @@ int main(int argc, const char** argv) { ...@@ -47,12 +47,28 @@ int main(int argc, const char** argv) {
vkcv::PassConfig particlePassDefinition({ present_color_attachment }); vkcv::PassConfig particlePassDefinition({ present_color_attachment });
vkcv::PassHandle particlePass = core.createPass(particlePassDefinition); vkcv::PassHandle particlePass = core.createPass(particlePassDefinition);
if (!particlePass) vkcv::PassConfig computePassDefinition({});
vkcv::PassHandle computePass = core.createPass(computePassDefinition);
if (!particlePass || !computePass)
{ {
std::cout << "Error. Could not create renderpass. Exiting." << std::endl; std::cout << "Error. Could not create renderpass. Exiting." << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
vkcv::ShaderProgram computeShaderProgram{};
computeShaderProgram.addShader(vkcv::ShaderStage::COMPUTE, std::filesystem::path("shaders/comp1.spv"));
vkcv::DescriptorSetHandle computeDescriptorSet = core.createDescriptorSet(computeShaderProgram.getReflectedDescriptors()[0]);
const std::vector<vkcv::VertexAttachment> computeVertexAttachments = computeShaderProgram.getVertexAttachments();
std::vector<vkcv::VertexBinding> computeBindings;
for (size_t i = 0; i < computeVertexAttachments.size(); i++) {
computeBindings.push_back(vkcv::VertexBinding(i, { computeVertexAttachments[i] }));
}
const vkcv::VertexLayout computeLayout(computeBindings);
vkcv::ShaderProgram particleShaderProgram{}; vkcv::ShaderProgram particleShaderProgram{};
particleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("shaders/vert.spv")); particleShaderProgram.addShader(vkcv::ShaderStage::VERTEX, std::filesystem::path("shaders/vert.spv"));
particleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("shaders/frag.spv")); particleShaderProgram.addShader(vkcv::ShaderStage::FRAGMENT, std::filesystem::path("shaders/frag.spv"));
...@@ -65,6 +81,9 @@ int main(int argc, const char** argv) { ...@@ -65,6 +81,9 @@ int main(int argc, const char** argv) {
); );
const std::vector<vkcv::VertexAttachment> vertexAttachments = particleShaderProgram.getVertexAttachments(); const std::vector<vkcv::VertexAttachment> vertexAttachments = particleShaderProgram.getVertexAttachments();
const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = {
vkcv::VertexBufferBinding(0, vertexBuffer.getVulkanHandle())};
std::vector<vkcv::VertexBinding> bindings; std::vector<vkcv::VertexBinding> bindings;
for (size_t i = 0; i < vertexAttachments.size(); i++) { for (size_t i = 0; i < vertexAttachments.size(); i++) {
bindings.push_back(vkcv::VertexBinding(i, { vertexAttachments[i] })); bindings.push_back(vkcv::VertexBinding(i, { vertexAttachments[i] }));
...@@ -88,6 +107,9 @@ int main(int argc, const char** argv) { ...@@ -88,6 +107,9 @@ int main(int argc, const char** argv) {
vertexBuffer.fill(vertices); vertexBuffer.fill(vertices);
vkcv::PipelineHandle particlePipeline = core.createGraphicsPipeline(particlePipelineDefinition); vkcv::PipelineHandle particlePipeline = core.createGraphicsPipeline(particlePipelineDefinition);
vkcv::PipelineHandle computePipeline = core.createComputePipeline(computeShaderProgram, {core.getDescriptorSet(computeDescriptorSet).layout} );
vkcv::Buffer<glm::vec4> color = core.createBuffer<glm::vec4>( vkcv::Buffer<glm::vec4> color = core.createBuffer<glm::vec4>(
vkcv::BufferType::UNIFORM, vkcv::BufferType::UNIFORM,
1 1
...@@ -98,17 +120,29 @@ int main(int argc, const char** argv) { ...@@ -98,17 +120,29 @@ int main(int argc, const char** argv) {
1 1
); );
const std::vector<vkcv::VertexBufferBinding> vertexBufferBindings = { glm::vec3 minVelocity = glm::vec3(-0.0f,-0.0f,0.f);
vkcv::VertexBufferBinding(0, vertexBuffer.getVulkanHandle()) glm::vec3 maxVelocity = glm::vec3(0.0f,0.0f,0.f);
}; glm::vec2 lifeTime = glm::vec2(0.f,5.f);
ParticleSystem particleSystem = ParticleSystem( 100 , minVelocity, maxVelocity, lifeTime);
vkcv::Buffer<Particle> particleBuffer = core.createBuffer<Particle>(
vkcv::BufferType::STORAGE,
particleSystem.getParticles().size()
);
particleBuffer.fill(particleSystem.getParticles());
vkcv::DescriptorWrites setWrites; vkcv::DescriptorWrites setWrites;
setWrites.uniformBufferWrites = {vkcv::UniformBufferDescriptorWrite(0,color.getHandle()), setWrites.uniformBufferWrites = {vkcv::UniformBufferDescriptorWrite(0,color.getHandle()),
vkcv::UniformBufferDescriptorWrite(1,position.getHandle())}; vkcv::UniformBufferDescriptorWrite(1,position.getHandle())};
setWrites.storageBufferWrites = { vkcv::StorageBufferDescriptorWrite(2,particleBuffer.getHandle())};
core.writeDescriptorSet(descriptorSet, setWrites); core.writeDescriptorSet(descriptorSet, setWrites);
if (!particlePipeline) vkcv::DescriptorWrites computeWrites;
computeWrites.storageBufferWrites = { vkcv::StorageBufferDescriptorWrite(0,particleBuffer.getHandle())};
core.writeDescriptorSet(computeDescriptorSet, computeWrites);
if (!particlePipeline || !computePipeline)
{ {
std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl; std::cout << "Error. Could not create graphics pipeline. Exiting." << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
...@@ -118,7 +152,6 @@ int main(int argc, const char** argv) { ...@@ -118,7 +152,6 @@ int main(int argc, const char** argv) {
const vkcv::Mesh renderMesh({vertexBufferBindings}, particleIndexBuffer.getVulkanHandle(), particleIndexBuffer.getCount()); const vkcv::Mesh renderMesh({vertexBufferBindings}, particleIndexBuffer.getVulkanHandle(), particleIndexBuffer.getCount());
vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle); vkcv::DescriptorSetUsage descriptorUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle);
//vkcv::DrawcallInfo drawcalls(renderMesh, {vkcv::DescriptorSetUsage(0, core.getDescriptorSet(descriptorSet).vulkanHandle)});
glm::vec2 pos = glm::vec2(1.f); glm::vec2 pos = glm::vec2(1.f);
...@@ -126,15 +159,9 @@ int main(int argc, const char** argv) { ...@@ -126,15 +159,9 @@ int main(int argc, const char** argv) {
pos = glm::vec2(static_cast<float>(offsetX), static_cast<float>(offsetY)); pos = glm::vec2(static_cast<float>(offsetX), static_cast<float>(offsetY));
}); });
glm::vec3 minVelocity = glm::vec3(-0.5f,-0.5f,0.f);
glm::vec3 maxVelocity = glm::vec3(0.5f,0.5f,0.f);
glm::vec2 lifeTime = glm::vec2(0.f,5.f);
ParticleSystem particleSystem = ParticleSystem( 100 , minVelocity, maxVelocity, lifeTime);
std::vector<glm::mat4> modelMatrices; std::vector<glm::mat4> modelMatrices;
std::vector<vkcv::DrawcallInfo> drawcalls; std::vector<vkcv::DrawcallInfo> drawcalls;
for(auto particle : particleSystem.getParticles()) { for(auto particle : particleSystem.getParticles()) {
modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition()));
drawcalls.push_back(vkcv::DrawcallInfo(renderMesh, {descriptorUsage})); drawcalls.push_back(vkcv::DrawcallInfo(renderMesh, {descriptorUsage}));
} }
...@@ -164,10 +191,7 @@ int main(int argc, const char** argv) { ...@@ -164,10 +191,7 @@ int main(int argc, const char** argv) {
modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition())); modelMatrices.push_back(glm::translate(glm::mat4(1.f), particle.getPosition()));
} }
//modelmatrix = glm::rotate(modelmatrix, angle, glm::vec3(0,0,1));
cameraManager.getCamera().updateView(deltatime); cameraManager.getCamera().updateView(deltatime);
//const glm::mat4 mvp = modelmatrix * cameraManager.getCamera().getProjection() * cameraManager.getCamera().getView();
std::vector<glm::mat4> mvp; std::vector<glm::mat4> mvp;
mvp.clear(); mvp.clear();
for(const auto& m : modelMatrices){ for(const auto& m : modelMatrices){
...@@ -177,6 +201,13 @@ int main(int argc, const char** argv) { ...@@ -177,6 +201,13 @@ int main(int argc, const char** argv) {
vkcv::PushConstantData pushConstantData((void*)mvp.data(), sizeof(glm::mat4)); vkcv::PushConstantData pushConstantData((void*)mvp.data(), sizeof(glm::mat4));
auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics); auto cmdStream = core.createCommandStream(vkcv::QueueType::Graphics);
uint32_t computeDispatchCount[3] = {static_cast<uint32_t> (std::ceil(particleSystem.getParticles().size()/64.f)),1,1};
core.recordComputeDispatchToCmdStream(cmdStream,
computePipeline,
computeDispatchCount,
{vkcv::DescriptorSetUsage(0,core.getDescriptorSet(computeDescriptorSet).vulkanHandle)},
vkcv::PushConstantData(nullptr, 0));
core.recordDrawcallsToCmdStream( core.recordDrawcallsToCmdStream(
cmdStream, cmdStream,
particlePass, particlePass,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment