From 8bbeb183adc338f917e34b13b8b0efb988b77c55 Mon Sep 17 00:00:00 2001 From: PancakeTAS Date: Sun, 29 Jun 2025 15:34:47 +0200 Subject: [PATCH] add descriptor set layouts to shadermoduel --- include/core/shadermodule.hpp | 8 +++++++- src/core/shadermodule.cpp | 31 ++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/core/shadermodule.hpp b/include/core/shadermodule.hpp index 18b40dc..cdcf3f6 100644 --- a/include/core/shadermodule.hpp +++ b/include/core/shadermodule.hpp @@ -4,6 +4,7 @@ #include "device.hpp" #include +#include #include #include @@ -22,15 +23,19 @@ namespace Vulkan::Core { /// /// @param device Vulkan device /// @param path Path to the shader file. + /// @param descriptorTypes Descriptor types used in the shader. /// /// @throws std::invalid_argument if the device is invalid. /// @throws std::system_error if the shader file cannot be opened or read. /// @throws ls::vulkan_error if object creation fails. /// - ShaderModule(const Device& device, const std::string& path); + ShaderModule(const Device& device, const std::string& path, + std::vector descriptorTypes); /// Get the Vulkan handle. [[nodiscard]] auto handle() const { return *this->shaderModule; } + /// Get the descriptor set layout. + [[nodiscard]] auto getDescriptorSetLayout() const { return *this->descriptorSetLayout; } /// Check whether the object is valid. [[nodiscard]] bool isValid() const { return static_cast(this->shaderModule); } @@ -45,6 +50,7 @@ namespace Vulkan::Core { ~ShaderModule() = default; private: std::shared_ptr shaderModule; + std::shared_ptr descriptorSetLayout; }; } diff --git a/src/core/shadermodule.cpp b/src/core/shadermodule.cpp index f3edcd8..eb7ecb8 100644 --- a/src/core/shadermodule.cpp +++ b/src/core/shadermodule.cpp @@ -6,7 +6,8 @@ using namespace Vulkan::Core; -ShaderModule::ShaderModule(const Device& device, const std::string& path) { +ShaderModule::ShaderModule(const Device& device, const std::string& path, + std::vector descriptorTypes) { if (!device) throw std::invalid_argument("Invalid Vulkan device"); @@ -43,4 +44,32 @@ ShaderModule::ShaderModule(const Device& device, const std::string& path) { vkDestroyShaderModule(dev, *shaderModuleHandle, nullptr); } ); + + // create descriptor set layout + std::vector layoutBindings(descriptorTypes.size()); + for (size_t i = 0; i < descriptorTypes.size(); ++i) + layoutBindings.at(i) = { + .binding = static_cast(i), + .descriptorType = descriptorTypes.at(i), + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT + }; + + const VkDescriptorSetLayoutCreateInfo layoutDesc{ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = static_cast(layoutBindings.size()), + .pBindings = layoutBindings.data() + }; + VkDescriptorSetLayout descriptorSetLayout{}; + res = vkCreateDescriptorSetLayout(device.handle(), &layoutDesc, nullptr, &descriptorSetLayout); + if (res != VK_SUCCESS || !descriptorSetLayout) + throw ls::vulkan_error(res, "Failed to create descriptor set layout"); + + // store layout in shared ptr + this->descriptorSetLayout = std::shared_ptr( + new VkDescriptorSetLayout(descriptorSetLayout), + [dev = device.handle()](VkDescriptorSetLayout* layout) { + vkDestroyDescriptorSetLayout(dev, *layout, nullptr); + } + ); }