From d3f5d87d613a7a2190c3bc86234c49c274e13b3d Mon Sep 17 00:00:00 2001 From: PancakeTAS Date: Sun, 29 Jun 2025 16:52:01 +0200 Subject: [PATCH] more comfortable way to initialize shader --- include/core/shadermodule.hpp | 3 ++- src/core/shadermodule.cpp | 20 +++++++++++--------- src/main.cpp | 17 ++++------------- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/include/core/shadermodule.hpp b/include/core/shadermodule.hpp index 4ef8390..9971f56 100644 --- a/include/core/shadermodule.hpp +++ b/include/core/shadermodule.hpp @@ -3,6 +3,7 @@ #include "device.hpp" +#include #include #include @@ -30,7 +31,7 @@ namespace Vulkan::Core { /// @throws ls::vulkan_error if object creation fails. /// ShaderModule(const Device& device, const std::string& path, - std::vector descriptorTypes); + const std::vector>& descriptorTypes); /// Get the Vulkan handle. [[nodiscard]] auto handle() const { return *this->shaderModule; } diff --git a/src/core/shadermodule.cpp b/src/core/shadermodule.cpp index f7258d6..703b609 100644 --- a/src/core/shadermodule.cpp +++ b/src/core/shadermodule.cpp @@ -7,7 +7,7 @@ using namespace Vulkan::Core; ShaderModule::ShaderModule(const Device& device, const std::string& path, - std::vector descriptorTypes) { + const std::vector>& descriptorTypes) { if (!device) throw std::invalid_argument("Invalid Vulkan device"); @@ -46,14 +46,16 @@ ShaderModule::ShaderModule(const Device& device, const std::string& path, ); // 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 - }; + std::vector layoutBindings; + size_t bindIdx = 0; + for (const auto &[count, type] : descriptorTypes) + for (size_t i = 0; i < count; i++, bindIdx++) + layoutBindings.emplace_back(VkDescriptorSetLayoutBinding { + .binding = static_cast(bindIdx), + .descriptorType = type, + .descriptorCount = 1, + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT + }); const VkDescriptorSetLayoutCreateInfo layoutDesc{ .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, diff --git a/src/main.cpp b/src/main.cpp index b4df0a9..d55ef02 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,19 +11,10 @@ int main() { const Vulkan::Device device(instance); const Vulkan::Core::ShaderModule computeShader(device, "shaders/downsample.spv", - { - VK_DESCRIPTOR_TYPE_SAMPLER, - VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, - VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, - VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, - VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, - VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, - VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, - VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, - VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, - VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER - } - ); + { { 1, VK_DESCRIPTOR_TYPE_SAMPLER}, + { 1, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE}, + { 7, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE}, + { 1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER} }); const Vulkan::Core::Pipeline computePipeline(device, computeShader); std::cerr << "Application finished" << '\n';