diff --git a/include/application.hpp b/include/application.hpp index 6f53d59..4682322 100644 --- a/include/application.hpp +++ b/include/application.hpp @@ -18,7 +18,7 @@ class Instance; class Swapchain; -class Pipeline; +class GraphicsPipeline; class Buffer; class CommandPool; class Image; @@ -26,6 +26,7 @@ class ComputePipeline; class Fence; class Semaphore; class Camera; +class DescriptorPool; class Application { public: @@ -34,7 +35,8 @@ public: ~Application(); protected: Swapchain* swapchain = nullptr; - Pipeline* graphicsPipeline = nullptr; + DescriptorPool* descriptorPool = nullptr; + GraphicsPipeline* graphicsPipeline = nullptr; Buffer* uniformBuffer = nullptr; void updateUniformBuffer(); diff --git a/include/vulkan/descriptor_pool.hpp b/include/vulkan/descriptor_pool.hpp new file mode 100644 index 0000000..ab122b0 --- /dev/null +++ b/include/vulkan/descriptor_pool.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include + +class Buffer; + +enum class DescriptorSet { + WORLD = 0, + MESH = 1 +}; + +class DescriptorPool { +public: + DescriptorPool(); + ~DescriptorPool(); + + void bindBuffer(Buffer* buffer, VkDescriptorType type, DescriptorSet set, uint32_t binding); + std::map sets; + std::map setLayouts; +private: + VkDescriptorPool handle = VK_NULL_HANDLE; + + void createLayout(DescriptorSet set, const std::vector &bindings); +}; \ No newline at end of file diff --git a/include/vulkan/pipeline.hpp b/include/vulkan/pipeline.hpp index 598f953..342dc21 100644 --- a/include/vulkan/pipeline.hpp +++ b/include/vulkan/pipeline.hpp @@ -4,7 +4,6 @@ #include class Instance; -class Buffer; struct UniformBufferObject { alignas(16) glm::mat4 model; @@ -12,32 +11,30 @@ struct UniformBufferObject { glm::mat4 projection; }; - class Pipeline { public: - explicit Pipeline(); - explicit Pipeline(VkRenderPass renderPass); - ~Pipeline(); VkPipeline handle = VK_NULL_HANDLE; VkPipelineLayout layout = VK_NULL_HANDLE; - VkDescriptorSet descriptorSet = VK_NULL_HANDLE; - - void updateDescriptor(uint32_t binding, Buffer* buffer, VkDescriptorType type); protected: + explicit Pipeline(const std::vector &descriptorSetLayouts, + const std::vector &pushConstantRanges); + virtual ~Pipeline(); +protected: + static VkShaderModule createShaderModule(const std::vector &code); +}; - VkShaderModule createShaderModule(const std::vector &code); - - VkDescriptorPool descriptorPool = VK_NULL_HANDLE; - VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE; - - void createDescriptorSetLayout(); - void createDescriptorPool(VkDescriptorType type, uint32_t descriptorCount); - void allocateDescriptorSet(); +class GraphicsPipeline : public Pipeline { +public: + explicit GraphicsPipeline(const std::string& vertexShaderPath, const std::string& fragmentShaderPath, + VkRenderPass renderPass, + const std::vector &descriptorSetLayouts={}, + const std::vector &pushConstantRanges={}); }; class ComputePipeline : public Pipeline { public: - explicit ComputePipeline(const std::string& shaderFile, uint32_t bindings); - void updateDescriptor(uint32_t binding, Buffer* buffer); + explicit ComputePipeline(const std::string& shaderFile, + const std::vector &descriptorSetLayouts={}, + const std::vector &pushConstantRanges={}); }; \ No newline at end of file diff --git a/shaders/normal.comp b/shaders/normal.comp index f6d21e6..5647617 100644 --- a/shaders/normal.comp +++ b/shaders/normal.comp @@ -20,13 +20,10 @@ struct Face { layout (std430, set = 0, binding = 0) buffer VertexBuffer { Vertex vertices[]; }; -layout (std430, set = 0, binding = 1) buffer FaceBuffer { +layout (std430, set = 0, binding = 3) buffer FaceBuffer { Face faces[]; }; -layout (std140, set = 0, binding = 2) uniform UniformBuffer { - uint vertexCount; - uint faceCount; -}; + layout (push_constant) uniform PushConstants { uint state; diff --git a/shaders/shader.vert b/shaders/shader.vert index 7c572d9..12bebbd 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -7,7 +7,7 @@ layout (location = 2) in vec3 inNormal; layout (location = 0) out vec3 fragColor; layout (location = 1) out vec3 normal; -layout (binding = 0) uniform UniformBufferObject { +layout (set = 0, binding = 0) uniform UniformBufferObject { mat4 model; mat4 view; mat4 projection; diff --git a/src/simulation.cpp b/src/simulation.cpp index 62de945..c775095 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -6,6 +6,7 @@ #include "soft_body.hpp" #include "mesh.hpp" #include "constraints.hpp" +#include "vulkan/descriptor_pool.hpp" Simulation::Simulation() { Mesh sphere("models/sphere_high.ply"); @@ -54,12 +55,24 @@ Simulation::Simulation() { faceBuffer = new SimulationBuffer(faces.data(), faces.size() * sizeof(Face), VK_BUFFER_USAGE_INDEX_BUFFER_BIT); tetrahedronBuffer = new SimulationBuffer(tetrahedra.data(), tetrahedra.size() * sizeof(Tetrahedron)); - pbdPipeline = new ComputePipeline("shaders/pbd.spv", 1); - pbdPipeline->updateDescriptor(0, vertexBuffer); + descriptorPool->bindBuffer(vertexBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 0); + descriptorPool->bindBuffer(edgeBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 1); + descriptorPool->bindBuffer(triangleBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 2); + descriptorPool->bindBuffer(faceBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 3); + descriptorPool->bindBuffer(tetrahedronBuffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, DescriptorSet::MESH, 4); - normalPipeline = new ComputePipeline("shaders/normal.spv", 2); - normalPipeline->updateDescriptor(0, vertexBuffer); - normalPipeline->updateDescriptor(1, faceBuffer); + pbdPipeline = new ComputePipeline("shaders/pbd.spv", {descriptorPool->setLayouts[DescriptorSet::MESH]}); + + + normalPipeline = new ComputePipeline("shaders/normal.spv", { + descriptorPool->setLayouts[DescriptorSet::MESH] + },{ + { + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, + .offset = 0, + .size = sizeof(uint32_t) + } + }); } void Simulation::recordDrawCommands() { @@ -69,7 +82,7 @@ void Simulation::recordDrawCommands() { VkDeviceSize offsets[] = {0}; vkCmdBindVertexBuffers(cmdBuffer, 0, 1, buffers, offsets); vkCmdBindIndexBuffer(cmdBuffer, faceBuffer->handle, 0, VK_INDEX_TYPE_UINT32); - vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline->layout, 0, 1, &graphicsPipeline->descriptorSet, 0, nullptr); + vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline->layout, 0, 1, &descriptorPool->sets[DescriptorSet::WORLD], 0, nullptr); vkCmdDrawIndexed(cmdBuffer, faceBuffer->size / sizeof(Face) * 3, 1, 0, 0, 0); } @@ -82,7 +95,7 @@ void Simulation::recordComputeCommands(VkCommandBuffer cmdBuffer) { barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT; barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; - vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pbdPipeline->layout, 0, 1, &pbdPipeline->descriptorSet, 0, nullptr); + vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pbdPipeline->layout, 0, 1, &descriptorPool->sets[DescriptorSet::MESH], 0, nullptr); size_t subSteps = 1; @@ -102,7 +115,7 @@ void Simulation::recordComputeCommands(VkCommandBuffer cmdBuffer) { } vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, normalPipeline->handle); - vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, normalPipeline->layout, 0, 1, &normalPipeline->descriptorSet, 0, nullptr); + vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, normalPipeline->layout, 0, 1, &descriptorPool->sets[DescriptorSet::MESH], 0, nullptr); #define BlOCK_SIZE 256 diff --git a/src/vulkan/application.cpp b/src/vulkan/application.cpp index c916ce0..aebcad7 100644 --- a/src/vulkan/application.cpp +++ b/src/vulkan/application.cpp @@ -8,6 +8,7 @@ #include "vulkan/synchronization.hpp" #include "camera.hpp" #include "input.hpp" +#include "vulkan/descriptor_pool.hpp" Application::Application() { new Instance; @@ -15,7 +16,10 @@ Application::Application() { createSyncObjects(); swapchain = new Swapchain(); - graphicsPipeline = new Pipeline(swapchain->renderPass); + descriptorPool = new DescriptorPool(); + graphicsPipeline = new GraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", + swapchain->renderPass, + {descriptorPool->setLayouts[DescriptorSet::WORLD]}); VkDeviceSize bufferSize = sizeof(UniformBufferObject); @@ -24,7 +28,7 @@ Application::Application() { VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); - graphicsPipeline->updateDescriptor(0, uniformBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); + descriptorPool->bindBuffer(uniformBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, DescriptorSet::WORLD, 0); camera = new Camera(swapchain->extent); char* stats; @@ -189,6 +193,7 @@ void Application::update() { Application::~Application() { delete swapchain; + delete descriptorPool; delete imageAvailable; delete renderFinished; delete computeFinished; diff --git a/src/vulkan/buffer.cpp b/src/vulkan/buffer.cpp index 649907e..2a08d26 100644 --- a/src/vulkan/buffer.cpp +++ b/src/vulkan/buffer.cpp @@ -17,7 +17,7 @@ Buffer::Buffer(VkDeviceSize size, VkBufferUsageFlags bufferUsage, VmaMemoryUsage allocationCreateInfo.usage = memoryUsage; allocationCreateInfo.flags = flags; - vmaCreateBuffer(Instance::instance->allocator, &bufferCreateInfo, &allocationCreateInfo, &handle, &allocation, &allocationInfo);; + vmaCreateBuffer(Instance::instance->allocator, &bufferCreateInfo, &allocationCreateInfo, &handle, &allocation, &allocationInfo); } Buffer::Buffer(VkDeviceSize size, void *data, VkDeviceSize dataSize, VkBufferUsageFlags bufferUsage, diff --git a/src/vulkan/descriptor_pool.cpp b/src/vulkan/descriptor_pool.cpp new file mode 100644 index 0000000..8b82f74 --- /dev/null +++ b/src/vulkan/descriptor_pool.cpp @@ -0,0 +1,81 @@ +#include "vulkan/descriptor_pool.hpp" +#include "vulkan/instance.hpp" +#include "vulkan/buffer.hpp" + + +DescriptorPool::DescriptorPool() { + VkDescriptorPoolSize poolSizes[] = { + {VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 20}, + {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 20} + }; + + VkDescriptorPoolCreateInfo poolInfo {}; + poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; + poolInfo.pPoolSizes = poolSizes; + poolInfo.poolSizeCount = sizeof(poolSizes) / sizeof(VkDescriptorPoolSize); + poolInfo.maxSets = 10; + + vkCreateDescriptorPool(Instance::instance->device, &poolInfo, nullptr, &handle); + + std::map> setBindings; + setBindings[DescriptorSet::WORLD].push_back({ + .binding = 0, + .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_VERTEX_BIT + }); + for (uint32_t i = 0; i < 5; i++){ + setBindings[DescriptorSet::MESH].push_back({ + .binding = i, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT + }); + } + + for (const auto &[set, bindings] : setBindings) + createLayout(set, bindings); + + for (const auto &[set, layout] : setLayouts){ + VkDescriptorSetAllocateInfo allocateInfo {}; + allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + allocateInfo.descriptorPool = handle; + allocateInfo.descriptorSetCount = 1; + allocateInfo.pSetLayouts = &layout; + + vkAllocateDescriptorSets(Instance::instance->device, &allocateInfo, &sets[set]); + } +} + +DescriptorPool::~DescriptorPool() { + vkDestroyDescriptorPool(Instance::instance->device, handle, nullptr); + for (const auto &[type, layout] : setLayouts) + vkDestroyDescriptorSetLayout(Instance::instance->device, layout, nullptr); +} + +void DescriptorPool::bindBuffer(Buffer *buffer, VkDescriptorType type, DescriptorSet set, uint32_t binding) { + VkDescriptorBufferInfo bufferInfo {}; + bufferInfo.buffer = buffer->handle; + bufferInfo.offset = 0; + bufferInfo.range = buffer->size; + + VkWriteDescriptorSet descriptorWrite {}; + descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrite.dstSet = sets[set]; + descriptorWrite.dstBinding = binding; + descriptorWrite.dstArrayElement = 0; + descriptorWrite.descriptorType = type; + descriptorWrite.descriptorCount = 1; + descriptorWrite.pBufferInfo = &bufferInfo; + + vkUpdateDescriptorSets(Instance::instance->device, 1, &descriptorWrite, 0, nullptr); +} + +void DescriptorPool::createLayout(DescriptorSet set, const std::vector &bindings) { + VkDescriptorSetLayoutCreateInfo layoutCreateInfo {}; + layoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + layoutCreateInfo.bindingCount = bindings.size(); + layoutCreateInfo.pBindings = bindings.data(); + + vkCreateDescriptorSetLayout(Instance::instance->device, &layoutCreateInfo, nullptr, &setLayouts[set]); +} diff --git a/src/vulkan/pipeline.cpp b/src/vulkan/pipeline.cpp index c592bc6..d071aed 100644 --- a/src/vulkan/pipeline.cpp +++ b/src/vulkan/pipeline.cpp @@ -3,7 +3,6 @@ #include "vulkan/pipeline.hpp" #include "vulkan/vertex.hpp" #include "vulkan/instance.hpp" -#include "vulkan/buffer.hpp" std::vector readFile(const std::string& fileName){ std::ifstream file(fileName, std::ios::ate | std::ios::binary); @@ -22,17 +21,42 @@ std::vector readFile(const std::string& fileName){ return buffer; } -Pipeline::Pipeline() { +VkShaderModule Pipeline::createShaderModule(const std::vector &code) { + VkShaderModuleCreateInfo createInfo {}; + createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + createInfo.codeSize = code.size(); + createInfo.pCode = reinterpret_cast(code.data()); + + VkShaderModule shaderModule; + vkCreateShaderModule(Instance::instance->device, &createInfo, nullptr, &shaderModule); + + return shaderModule; +} + +Pipeline::Pipeline(const std::vector &descriptorSetLayouts, + const std::vector &pushConstantRanges) { + VkPipelineLayoutCreateInfo pipelineLayoutInfo {}; + pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + pipelineLayoutInfo.setLayoutCount = descriptorSetLayouts.size(); + pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data(); + pipelineLayoutInfo.pushConstantRangeCount = pushConstantRanges.size(); + pipelineLayoutInfo.pPushConstantRanges = pushConstantRanges.data(); + + vkCreatePipelineLayout(Instance::instance->device, &pipelineLayoutInfo, nullptr, &layout); +} +Pipeline::~Pipeline() { + vkDestroyPipelineLayout(Instance::instance->device, layout, nullptr); + vkDestroyPipeline(Instance::instance->device, handle, nullptr); } -Pipeline::Pipeline(VkRenderPass renderPass) { - createDescriptorSetLayout(); - createDescriptorPool(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1); - allocateDescriptorSet(); - auto vertShaderCode = readFile("shaders/vert.spv"); - auto fragShaderCode = readFile("shaders/frag.spv"); +GraphicsPipeline::GraphicsPipeline(const std::string& vertexShaderPath, const std::string& fragmentShaderPath, + VkRenderPass renderPass, + const std::vector &descriptorSetLayouts, + const std::vector &pushConstantRanges) : Pipeline(descriptorSetLayouts, pushConstantRanges) { + auto vertShaderCode = readFile(vertexShaderPath); + auto fragShaderCode = readFile(fragmentShaderPath); VkShaderModule vertShaderModule = createShaderModule(vertShaderCode); VkShaderModule fragShaderModule = createShaderModule(fragShaderCode); @@ -114,13 +138,6 @@ Pipeline::Pipeline(VkRenderPass renderPass) { depthStencil.depthCompareOp = VK_COMPARE_OP_LESS; depthStencil.stencilTestEnable = VK_FALSE; - VkPipelineLayoutCreateInfo pipelineLayoutInfo {}; - pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - pipelineLayoutInfo.setLayoutCount = 1; - pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout; - - vkCreatePipelineLayout(Instance::instance->device, &pipelineLayoutInfo, nullptr, &layout); - VkGraphicsPipelineCreateInfo pipelineInfo {}; { pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; @@ -149,104 +166,9 @@ Pipeline::Pipeline(VkRenderPass renderPass) { vkDestroyShaderModule(Instance::instance->device, fragShaderModule, nullptr); } -VkShaderModule Pipeline::createShaderModule(const std::vector &code) { - VkShaderModuleCreateInfo createInfo {}; - createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - createInfo.codeSize = code.size(); - createInfo.pCode = reinterpret_cast(code.data()); - - VkShaderModule shaderModule; - vkCreateShaderModule(Instance::instance->device, &createInfo, nullptr, &shaderModule); - - return shaderModule; -} - -Pipeline::~Pipeline() { - vkDestroyDescriptorPool(Instance::instance->device, descriptorPool, nullptr); - vkDestroyDescriptorSetLayout(Instance::instance->device, descriptorSetLayout, nullptr); - vkDestroyPipeline(Instance::instance->device, handle, nullptr); - vkDestroyPipelineLayout(Instance::instance->device, layout, nullptr); -} - -void Pipeline::createDescriptorSetLayout() { - VkDescriptorSetLayoutBinding uboLayoutBinding {}; - uboLayoutBinding.binding = 0; - uboLayoutBinding.descriptorCount = 1; - uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; - - VkDescriptorSetLayoutCreateInfo layoutCreateInfo {}; - layoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; - layoutCreateInfo.bindingCount = 1; - layoutCreateInfo.pBindings = &uboLayoutBinding; - - vkCreateDescriptorSetLayout(Instance::instance->device, &layoutCreateInfo, nullptr, &descriptorSetLayout); -} - -void Pipeline::createDescriptorPool(VkDescriptorType type, uint32_t descriptorCount) { - VkDescriptorPoolSize poolSize {}; - poolSize.descriptorCount = descriptorCount; - poolSize.type = type; - - VkDescriptorPoolCreateInfo poolInfo {}; - poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - poolInfo.pPoolSizes = &poolSize; - poolInfo.poolSizeCount = 1; - poolInfo.maxSets = 1; - - vkCreateDescriptorPool(Instance::instance->device, &poolInfo, nullptr, &descriptorPool); -} - -void Pipeline::allocateDescriptorSet() { - VkDescriptorSetAllocateInfo allocateInfo {}; - allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - allocateInfo.descriptorPool = descriptorPool; - allocateInfo.descriptorSetCount = 1; - allocateInfo.pSetLayouts = &descriptorSetLayout; - - vkAllocateDescriptorSets(Instance::instance->device, &allocateInfo, &descriptorSet); -} - -void Pipeline::updateDescriptor(uint32_t binding, Buffer *buffer, VkDescriptorType type) { - VkDescriptorBufferInfo bufferInfo {}; - bufferInfo.buffer = buffer->handle; - bufferInfo.offset = 0; - bufferInfo.range = buffer->size; - - VkWriteDescriptorSet descriptorWrite {}; - descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptorWrite.dstSet = descriptorSet; - descriptorWrite.dstBinding = binding; - descriptorWrite.dstArrayElement = 0; - descriptorWrite.descriptorType = type; - descriptorWrite.descriptorCount = 1; - descriptorWrite.pBufferInfo = &bufferInfo; - - vkUpdateDescriptorSets(Instance::instance->device, 1, &descriptorWrite, 0, nullptr); -} - -ComputePipeline::ComputePipeline(const std::string& shaderFile, uint32_t bindings) { - { - std::vector layoutBindings(bindings, VkDescriptorSetLayoutBinding()); - for (uint32_t binding = 0; binding < bindings; binding++){ - VkDescriptorSetLayoutBinding& layoutBinding = layoutBindings[binding]; - layoutBinding.binding = binding; - layoutBinding.descriptorCount = 1; - layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - layoutBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; - } - - VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo {}; - descriptorSetLayoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; - descriptorSetLayoutInfo.bindingCount = bindings; - descriptorSetLayoutInfo.pBindings = layoutBindings.data(); - - vkCreateDescriptorSetLayout(Instance::instance->device, &descriptorSetLayoutInfo, nullptr, &descriptorSetLayout); - - createDescriptorPool(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, bindings); - allocateDescriptorSet(); - } - +ComputePipeline::ComputePipeline(const std::string& shaderFile, + const std::vector &descriptorSetLayouts, + const std::vector &pushConstantRanges) : Pipeline(descriptorSetLayouts, pushConstantRanges) { VkShaderModule module = createShaderModule(readFile(shaderFile)); VkPipelineShaderStageCreateInfo stageInfo {}; @@ -255,20 +177,6 @@ ComputePipeline::ComputePipeline(const std::string& shaderFile, uint32_t binding stageInfo.pName = "main"; stageInfo.module = module; - VkPushConstantRange pushConstantRange {}; - pushConstantRange.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; - pushConstantRange.offset = 0; - pushConstantRange.size = sizeof(uint32_t); - - VkPipelineLayoutCreateInfo layoutInfo {}; - layoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - layoutInfo.setLayoutCount = 1; - layoutInfo.pSetLayouts = &descriptorSetLayout; - layoutInfo.pushConstantRangeCount = 1; - layoutInfo.pPushConstantRanges = &pushConstantRange; - - vkCreatePipelineLayout(Instance::instance->device, &layoutInfo, nullptr, &layout); - VkComputePipelineCreateInfo pipelineInfo {}; pipelineInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; pipelineInfo.layout = layout; @@ -279,6 +187,4 @@ ComputePipeline::ComputePipeline(const std::string& shaderFile, uint32_t binding vkDestroyShaderModule(Instance::instance->device, module, nullptr); } -void ComputePipeline::updateDescriptor(uint32_t binding, Buffer *buffer) { - Pipeline::updateDescriptor(binding, buffer, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); -} +