add descriptor set layouts to shadermoduel

This commit is contained in:
PancakeTAS 2025-06-29 15:34:47 +02:00
parent 40d7d032a3
commit 8bbeb183ad
No known key found for this signature in database
2 changed files with 37 additions and 2 deletions

View file

@ -4,6 +4,7 @@
#include "device.hpp"
#include <string>
#include <vector>
#include <vulkan/vulkan_core.h>
#include <memory>
@ -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<VkDescriptorType> 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<bool>(this->shaderModule); }
@ -45,6 +50,7 @@ namespace Vulkan::Core {
~ShaderModule() = default;
private:
std::shared_ptr<VkShaderModule> shaderModule;
std::shared_ptr<VkDescriptorSetLayout> descriptorSetLayout;
};
}

View file

@ -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<VkDescriptorType> 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<VkDescriptorSetLayoutBinding> layoutBindings(descriptorTypes.size());
for (size_t i = 0; i < descriptorTypes.size(); ++i)
layoutBindings.at(i) = {
.binding = static_cast<uint32_t>(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<uint32_t>(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<VkDescriptorSetLayout>(
new VkDescriptorSetLayout(descriptorSetLayout),
[dev = device.handle()](VkDescriptorSetLayout* layout) {
vkDestroyDescriptorSetLayout(dev, *layout, nullptr);
}
);
}