From 49f9f3bbe05bfe9a798367ce5bee69a22b75780a Mon Sep 17 00:00:00 2001 From: antopilo Date: Wed, 8 Jan 2025 20:01:24 -0500 Subject: [PATCH] Bindless textures now working --- Nuake/src/Rendering/Textures/Material.h | 57 +++++++++++++++++-- .../Rendering/Vulkan/VulkanSceneRenderer.cpp | 48 +++++++++------- .../Rendering/Vulkan/VulkanSceneRenderer.h | 6 +- 3 files changed, 85 insertions(+), 26 deletions(-) diff --git a/Nuake/src/Rendering/Textures/Material.h b/Nuake/src/Rendering/Textures/Material.h index db4e588c..1cbfe7dc 100644 --- a/Nuake/src/Rendering/Textures/Material.h +++ b/Nuake/src/Rendering/Textures/Material.h @@ -61,6 +61,11 @@ namespace Nuake } public: UUID AlbedoImage; + UUID AOImage; + UUID MetalnessImage; + UUID RoughnessImage; + UUID NormalImage; + Ref m_Albedo; Ref m_AO; Ref m_Metalness; @@ -227,28 +232,72 @@ namespace Nuake if (j.contains("Normal")) { - const std::string absolutePath = FileSystem::RelativeToAbsolute(j["Normal"]["Path"]); + const auto& texturePath = j["Normal"]["Path"]; + const std::string absolutePath = FileSystem::RelativeToAbsolute(texturePath); + if (FileSystem::FileExists(texturePath)) + { + GPUResources& resources = GPUResources::Get(); + Ref image = CreateRef(absolutePath); + if (resources.AddTexture(image)) + { + NormalImage = image->GetID(); + } + } + Ref normalTexture = TextureManager::Get()->GetTexture(absolutePath); SetMetalness(normalTexture); } if (j.contains("AO")) { - const std::string absolutePath = FileSystem::RelativeToAbsolute(j["AO"]["Path"]); + const auto& texturePath = j["AO"]["Path"]; + const std::string absolutePath = FileSystem::RelativeToAbsolute(texturePath); + if (FileSystem::FileExists(texturePath)) + { + GPUResources& resources = GPUResources::Get(); + Ref image = CreateRef(absolutePath); + if (resources.AddTexture(image)) + { + AOImage = image->GetID(); + } + } + Ref aoTexture = TextureManager::Get()->GetTexture(absolutePath); SetAO(aoTexture); } if (j.contains("Metalness")) { - const std::string absolutePath = FileSystem::RelativeToAbsolute(j["Metalness"]["Path"]); + const auto& texturePath = j["Metalness"]["Path"]; + const std::string absolutePath = FileSystem::RelativeToAbsolute(texturePath); + if (FileSystem::FileExists(texturePath)) + { + GPUResources& resources = GPUResources::Get(); + Ref image = CreateRef(absolutePath); + if (resources.AddTexture(image)) + { + MetalnessImage = image->GetID(); + } + } + Ref metalTexture = TextureManager::Get()->GetTexture(absolutePath); SetMetalness(metalTexture); } if (j.contains("Roughness")) { - const std::string absolutePath = FileSystem::RelativeToAbsolute(j["Roughness"]["Path"]); + const auto& texturePath = j["Roughness"]["Path"]; + const std::string absolutePath = FileSystem::RelativeToAbsolute(texturePath); + if (FileSystem::FileExists(texturePath)) + { + GPUResources& resources = GPUResources::Get(); + Ref image = CreateRef(absolutePath); + if (resources.AddTexture(image)) + { + RoughnessImage = image->GetID(); + } + } + Ref metalTexture = TextureManager::Get()->GetTexture(absolutePath); SetRoughness(metalTexture); } diff --git a/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.cpp b/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.cpp index 4520838e..8cebc1ab 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.cpp +++ b/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.cpp @@ -52,8 +52,6 @@ void VkSceneRenderer::BeginScene(RenderContext inContext) BuildMatrixBuffer(); UpdateTransformBuffer(); - - auto& cmd = Context.CommandBuffer; auto& scene = Context.CurrentScene; auto& vk = VkRenderer::Get(); @@ -395,6 +393,26 @@ void VkSceneRenderer::BuildMatrixBuffer() ZoneScopedN("Build Matrix Buffer"); auto& scene = Context.CurrentScene; + std::map TextureIDMapping; + auto allTextures = GPUResources::Get().GetAllTextures(); + std::vector imageInfos(allTextures.size()); + for (size_t i = 0; i < allTextures.size(); i++) { + imageInfos[i].imageView = allTextures[i]->GetImageView(); + imageInfos[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + TextureIDMapping[allTextures[i]->GetID()] = i; + } + + VkWriteDescriptorSet write{}; + write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + write.dstSet = TextureBufferDescriptor; + write.dstBinding = 0; // Binding 0 + write.dstArrayElement = 0; + write.descriptorCount = static_cast(imageInfos.size()); + write.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; + write.pImageInfo = imageInfos.data(); + + vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &write, 0, nullptr); + std::array allTransforms; std::array allMaterials; @@ -441,6 +459,11 @@ void VkSceneRenderer::BuildMatrixBuffer() materialBuffer.aoValue = material->data.u_AOValue; materialBuffer.hasRoughness = material->HasRoughness(); materialBuffer.roughnessValue = material->data.u_RoughnessValue; + materialBuffer.albedoTextureId = material->HasAlbedo() ? TextureIDMapping[material->AlbedoImage] : 0; + materialBuffer.normalTextureId = material->HasNormal() ? TextureIDMapping[material->NormalImage] : 0; + materialBuffer.metalnessTextureId = material->HasMetalness() ? TextureIDMapping[material->MetalnessImage] : 0; + materialBuffer.aoTextureId = material->HasAO() ? TextureIDMapping[material->AOImage] : 0; + materialBuffer.roughnessTextureId = material->HasRoughness() ? TextureIDMapping[material->RoughnessImage] : 0; allMaterials[currentMaterialIndex] = materialBuffer; MeshMaterialMapping[m->GetVkMesh()->GetID()] = currentMaterialIndex; @@ -524,22 +547,5 @@ void VkSceneRenderer::UpdateTransformBuffer() bufferWrite.pImageInfo = VK_NULL_HANDLE; vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &bufferWrite, 0, nullptr); } - - auto allTextures = GPUResources::Get().GetAllTextures(); - std::vector imageInfos(allTextures.size()); - for (size_t i = 0; i < allTextures.size(); i++) { - imageInfos[i].imageView = allTextures[i]->GetImageView(); - imageInfos[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - } - - VkWriteDescriptorSet write{}; - write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write.dstSet = TextureBufferDescriptor; - write.dstBinding = 0; // Binding 0 - write.dstArrayElement = 0; - write.descriptorCount = static_cast(imageInfos.size()); - write.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; - write.pImageInfo = imageInfos.data(); - - vkUpdateDescriptorSets(VkRenderer::Get().GetDevice(), 1, &write, 0, nullptr); -} \ No newline at end of file +} + \ No newline at end of file diff --git a/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.h b/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.h index 3b81dc4b..df6baab9 100644 --- a/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.h +++ b/Nuake/src/Rendering/Vulkan/VulkanSceneRenderer.h @@ -42,7 +42,11 @@ namespace Nuake float metalnessValue; float roughnessValue; float aoValue; - float pad; + int albedoTextureId; + int normalTextureId; + int metalnessTextureId; + int roughnessTextureId; + int aoTextureId; }; // This is the *whole* buffer