From ba181eb9c908ec63cf55bdaf543f77479832d451 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Thu, 22 Sep 2022 09:23:40 +0800 Subject: [PATCH 1/2] Shifted Command buffers outside of render graph Command buffers now exist in renderer instead Graphics system that calls the renderer, begins and ends the renderer recording in it's main loop as well --- SHADE_Engine/SHADE_Engine.vcxproj | 34 +++++ SHADE_Engine/SHADE_Engine.vcxproj.filters | 143 +++++++++++++++++- .../Descriptors/SHVkDescriptorPool.cpp | 5 +- .../Graphics/Descriptors/SHVkDescriptorPool.h | 2 +- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 36 ++++- .../MiddleEnd/Interface/SHRenderer.cpp | 21 ++- .../Graphics/MiddleEnd/Interface/SHRenderer.h | 12 +- .../MiddleEnd/Interface/SHViewport.cpp | 4 +- .../Graphics/MiddleEnd/Interface/SHViewport.h | 3 +- .../Graphics/RenderGraph/SHRenderGraph.cpp | 44 +----- .../src/Graphics/RenderGraph/SHRenderGraph.h | 12 +- SHADE_Engine/src/Math/SHMath.h | 2 +- 12 files changed, 239 insertions(+), 79 deletions(-) diff --git a/SHADE_Engine/SHADE_Engine.vcxproj b/SHADE_Engine/SHADE_Engine.vcxproj index 37ddf8f5..2aa3b608 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj +++ b/SHADE_Engine/SHADE_Engine.vcxproj @@ -138,6 +138,7 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" + @@ -167,13 +168,31 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" + + + + + + + + + + + + + + + + + + @@ -250,6 +269,7 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" + @@ -273,12 +293,26 @@ xcopy /s /r /y /q "$(SolutionDir)/Dependencies/dotnet/bin" "$(OutDir)" + + + + + + + + + + + + + + diff --git a/SHADE_Engine/SHADE_Engine.vcxproj.filters b/SHADE_Engine/SHADE_Engine.vcxproj.filters index f52aebb0..74be0df1 100644 --- a/SHADE_Engine/SHADE_Engine.vcxproj.filters +++ b/SHADE_Engine/SHADE_Engine.vcxproj.filters @@ -28,6 +28,9 @@ {078AA1A3-F318-2B6D-9C37-3F6888A53B13} + + {8C1A20B0-78BC-4A86-6177-5EDA4DB8D1D6} + {DBC7D3B0-C769-FE86-B024-12DB9C6585D7} @@ -70,15 +73,30 @@ {4B204703-3704-0859-A064-02AC8C67F2DA} + + {7A02D7B0-E60F-0597-6FF6-0082DB02D14D} + + + {85EFB65D-F107-9E87-BAB4-2D21268C3221} + {EBA1D3FF-D75C-C3AB-8014-3CF66CAE0D3C} + + {1F603FFC-8B22-7386-D4D2-011340D44B64} + {8CDBA7C9-F8E8-D5AF-81CF-D19AEDDBA166} + + {D04C14FB-3C5A-42E1-C540-3ECC314D0E98} + {2460C057-1070-6C28-7929-D14665585BC1} + + {7E76F08B-EA83-1E72-736A-1A5DDF76EA28} + {FBD334F8-67EA-328E-B061-BEAF1CB70316} @@ -121,6 +139,9 @@ {EAD6C33D-5697-3F74-1FD2-88F18B518450} + + {3AB383AD-2681-77B3-0F15-E8D9FB815318} + {F1B75745-5D6D-D03A-E661-CA115216C73E} @@ -207,6 +228,9 @@ ECS_Base\UnitTesting + + Editor + Engine @@ -294,18 +318,69 @@ Graphics\Instance + + Graphics\MiddleEnd\Batching + + + Graphics\MiddleEnd\Batching + + + Graphics\MiddleEnd\Batching + + + Graphics\MiddleEnd\GlobalData + + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + Graphics\MiddleEnd\Interface + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + Graphics\MiddleEnd\Interface + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Meshes + + + Graphics\MiddleEnd\Meshes + Graphics\MiddleEnd\PerFrame Graphics\MiddleEnd\PerFrame + + Graphics\MiddleEnd\Pipeline + Graphics\MiddleEnd\Shaders @@ -315,6 +390,9 @@ Graphics\MiddleEnd\Shaders + + Graphics\MiddleEnd\Textures + Graphics\Pipeline @@ -421,7 +499,13 @@ Math - Math + Math\Transform + + + Math\Transform + + + Math\Transform Math\Vector @@ -498,8 +582,6 @@ Tools - - @@ -529,6 +611,9 @@ ECS_Base\UnitTesting + + Editor + Engine @@ -598,24 +683,66 @@ Graphics\Instance + + Graphics\MiddleEnd\Batching + + + Graphics\MiddleEnd\Batching + + + Graphics\MiddleEnd\Batching + + + Graphics\MiddleEnd\GlobalData + + + Graphics\MiddleEnd\Interface + Graphics\MiddleEnd\Interface + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + Graphics\MiddleEnd\Interface + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Interface + + + Graphics\MiddleEnd\Meshes + Graphics\MiddleEnd\PerFrame Graphics\MiddleEnd\PerFrame + + Graphics\MiddleEnd\Pipeline + Graphics\MiddleEnd\Shaders Graphics\MiddleEnd\Shaders + + Graphics\MiddleEnd\Textures + Graphics\Pipeline @@ -698,7 +825,13 @@ Math - Math + Math\Transform + + + Math\Transform + + + Math\Transform Math\Vector @@ -740,7 +873,5 @@ Tools - - \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.cpp b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.cpp index 77663ab8..6b770c3d 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.cpp +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.cpp @@ -50,9 +50,8 @@ namespace SHADE return *this; } - std::vector> SHVkDescriptorPool::Allocate(const std::vector>& layouts, std::vector const& variableDescCounts) + Handle SHVkDescriptorPool::Allocate(const std::vector>& layouts, std::vector const& variableDescCounts) { - SHVkInstance::GetResourceManager().Create(device, GetHandle(), layouts, variableDescCounts); - return {}; + return SHVkInstance::GetResourceManager().Create(device, GetHandle(), layouts, variableDescCounts); } } diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.h b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.h index 9314d940..b862ec09 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.h +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorPool.h @@ -101,7 +101,7 @@ namespace SHADE /// Handles to the created Descriptor Sets. If this DescriptorPool has run out of /// space, lesser number of Handles will be returned. /// - std::vector> Allocate(const std::vector>& layouts, std::vector const& variableDescCounts); + Handle Allocate(const std::vector>& layouts, std::vector const& variableDescCounts); private: /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 1130a02b..b38fa275 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -29,6 +29,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/Batching/SHSuperBatch.h" #include "SHGraphicsConstants.h" #include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h" +#include "Graphics/Buffers/SHVkBuffer.h" namespace SHADE { @@ -129,7 +130,7 @@ namespace SHADE } // Initialize world render graph - worldRenderGraph->Init(device, swapchain, renderContextCmdPools, globalData); + worldRenderGraph->Init(device, swapchain, globalData); //worldRenderGraph->AddResource("Position", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat); //worldRenderGraph->AddResource("Normals", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat); //worldRenderGraph->AddResource("Composite", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat); @@ -167,7 +168,7 @@ namespace SHADE debugWorldRenderer->SetCamera(worldCamera);*/ // Add world renderer to default viewport - worldRenderer = defaultViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), descPool, globalData->GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph); + worldRenderer = defaultViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, globalData->GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph); worldRenderer->SetCamera(worldCamera); @@ -228,16 +229,37 @@ namespace SHADE // For every renderer for (int renIndex = 0; renIndex < static_cast(renderers.size()); ++renIndex) { - // Draw first - renderers[renIndex]->Draw(renderContext.GetCurrentFrame(), MESH_DATA); + /*-----------------------------------------------------------------------*/ + /* Renderer start */ + /*-----------------------------------------------------------------------*/ + // get command buffer of the renderer in the current frame + auto currentCmdBuffer = renderers[renIndex]->GetCommandBuffer(renderContext.GetCurrentFrame()); - // get render graph - auto rg = renderers[renIndex]->GetRenderGraph(); + // Begin recording the command buffer + currentCmdBuffer->BeginRecording(); + + // Bind all the buffers required for meshes + for (auto& [buffer, bindingPoint] : MESH_DATA) + { + if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eVertexBuffer) + currentCmdBuffer->BindVertexBuffer(bindingPoint, buffer, 0); + else if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eIndexBuffer) + currentCmdBuffer->BindIndexBuffer(buffer, 0); + } + + // Draw first + renderers[renIndex]->Draw(renderContext.GetCurrentFrame()); + + // End the command buffer recording + currentCmdBuffer->EndRecording(); + /*-----------------------------------------------------------------------*/ + /* Renderer end */ + /*-----------------------------------------------------------------------*/ // submit a command buffer from the current render graph and make it wait for the previous render graph before submitting it to GPU. graphicsQueue->SubmitCommandBuffer ( - { rg->GetCommandBuffer(renderContext.GetCurrentFrame()) }, + { currentCmdBuffer }, { (vpIndex == viewports.size() - 1 && renIndex == renderers.size() - 1) ? frameData.semRenderFinishHdl : graphSemaphores[!semIndex] }, { (vpIndex == 0 && renIndex == 0) ? frameData.semImgAvailableHdl : graphSemaphores[semIndex] }, { vk::PipelineStageFlagBits::eColorAttachmentOutput }, diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp index 90e137fe..223ceb4f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp @@ -20,20 +20,28 @@ of DigiPen Institute of Technology is prohibited. #include "SHMaterial.h" #include "Graphics/MiddleEnd/Interface/SHGraphicsConstants.h" #include "Graphics/Descriptors/SHVkDescriptorSetGroup.h" +#include "Graphics/Buffers/SHVkBuffer.h" namespace SHADE { /*-----------------------------------------------------------------------------------*/ /* Constructor/Destructors */ /*-----------------------------------------------------------------------------------*/ - SHRenderer::SHRenderer(Handle logicalDevice, uint32_t numFrames, Handle descriptorPool, Handle cameraDescLayout, Handle viewport, Handle renderGraph) + SHRenderer::SHRenderer(Handle logicalDevice, uint32_t numFrames, std::vector>& cmdPools, Handle descriptorPool, Handle cameraDescLayout, Handle viewport, Handle renderGraph) : viewport { viewport } , renderGraph { renderGraph } { - cameraDescriptorSet = logicalDevice->CreateDescriptorSetGroup(descriptorPool, { cameraDescLayout }, { 1 }); + commandBuffers.resize(static_cast(numFrames)); + + for (uint32_t i = 0; i < commandBuffers.size(); ++i) + commandBuffers[i] = cmdPools[i]->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + + cameraDescriptorSet = descriptorPool->Allocate({ cameraDescLayout }, { 1 }); cpuCameraData.resize(numFrames); cameraDataAlignedSize = logicalDevice->PadUBOSize(sizeof(SHShaderCameraData)); + + cameraBuffer = logicalDevice->CreateBuffer(cameraDataAlignedSize * numFrames, nullptr, cameraDataAlignedSize * numFrames, vk::BufferUsageFlagBits::eUniformBuffer, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT); } /*-----------------------------------------------------------------------------------*/ @@ -47,9 +55,9 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ /* Drawing Functions */ /*-----------------------------------------------------------------------------------*/ - void SHRenderer::Draw(uint32_t frameIndex, std::initializer_list, uint32_t>> graphScopeBuffers) noexcept + void SHRenderer::Draw(uint32_t frameIndex) noexcept { - renderGraph->Execute(frameIndex, graphScopeBuffers); + renderGraph->Execute(frameIndex, commandBuffers[frameIndex]); } void SHRenderer::BindDescriptorSet(Handle cmdBuffer, uint32_t frameIndex) noexcept @@ -64,4 +72,9 @@ namespace SHADE return renderGraph; } + Handle SHRenderer::GetCommandBuffer(uint32_t frameIndex) const noexcept + { + return commandBuffers[frameIndex]; + } + } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h index 843ad6bf..bbb7773b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h @@ -39,6 +39,7 @@ namespace SHADE class SHVkDescriptorSetGroup; class SHGraphicsGlobalData; class SHVkDescriptorPool; + class SHVkBuffer; struct SHShaderCameraData { @@ -63,7 +64,7 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ /* Constructor/Destructors */ /*-----------------------------------------------------------------------------*/ - SHRenderer(Handle logicalDevice, uint32_t numFrames, Handle descriptorPool, Handle cameraDescLayout, Handle viewport, Handle renderGraph); + SHRenderer(Handle logicalDevice, uint32_t numFrames, std::vector>& cmdPools, Handle descriptorPool, Handle cameraDescLayout, Handle viewport, Handle renderGraph); /*-----------------------------------------------------------------------------*/ /* Camera Registration */ @@ -73,13 +74,14 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ /* Drawing Functions */ /*-----------------------------------------------------------------------------*/ - void Draw(uint32_t frameIndex, std::initializer_list, uint32_t>> graphScopeBuffers) noexcept; + void Draw(uint32_t frameIndex) noexcept; void BindDescriptorSet (Handle cmdBuffer, uint32_t frameIndex) noexcept; /*-----------------------------------------------------------------------------*/ /* Setters and Getters */ /*-----------------------------------------------------------------------------*/ Handle GetRenderGraph (void) const noexcept; + Handle GetCommandBuffer(uint32_t frameIndex) const noexcept; private: /*-----------------------------------------------------------------------------*/ @@ -92,7 +94,13 @@ namespace SHADE Handle camera; Handle renderGraph; Handle cameraDescriptorSet; + Handle cameraBuffer; std::vector cpuCameraData; + + //! Command buffers for the render graph + std::vector> commandBuffers; + + }; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp index 1e38acf1..25c5bca2 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.cpp @@ -49,10 +49,10 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Renderer Registration Functions */ /*---------------------------------------------------------------------------------*/ - Handle SHViewport::AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, Handle descriptorPool, Handle cameraDescLayout, Handle renderGraph) + Handle SHViewport::AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, std::vector>& cmdPools, Handle descriptorPool, Handle cameraDescLayout, Handle renderGraph) { // Create the renderer - auto renderer = resourceManager.Create(device, numFrames, descriptorPool, cameraDescLayout, GetHandle(), renderGraph); + auto renderer = resourceManager.Create(device, numFrames, cmdPools, descriptorPool, cameraDescLayout, GetHandle(), renderGraph); // Store renderers.emplace_back(renderer); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h index 608446a3..d97d62ce 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHViewport.h @@ -32,6 +32,7 @@ namespace SHADE class SHRenderGraph; class SHVkDescriptorPool; class SHVkDescriptorSetLayout; + class SHVkCommandPool; /*---------------------------------------------------------------------------------*/ /* Type Definitions */ @@ -58,7 +59,7 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ /* Renderers Registration Functions */ /*-----------------------------------------------------------------------------*/ - Handle AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, Handle descriptorPool, Handle cameraDescLayout, Handle renderGraph); + Handle AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, std::vector>& cmdPools, Handle descriptorPool, Handle cameraDescLayout, Handle renderGraph); void RemoveRenderer(Handle renderer); /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index d7404db2..d02e8f7d 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -946,25 +946,6 @@ namespace SHADE } } - /***************************************************************************/ - /*! - - \brief - Configures command pools and command buffers. - - */ - /***************************************************************************/ - void SHRenderGraph::ConfigureCommands(void) noexcept - { - //commandPools.resize (static_cast(swapchainHdl->GetNumImages())); - commandBuffers.resize(static_cast(swapchainHdl->GetNumImages())); - - for (uint32_t i = 0; i < commandBuffers.size(); ++i) - { - commandBuffers[i] = commandPools[i]->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); - } - } - /***************************************************************************/ /*! @@ -980,12 +961,11 @@ namespace SHADE */ /***************************************************************************/ - void SHRenderGraph::Init(Handle const& logicalDevice, Handle const& swapchain, std::vector> const& cmdPools, Handle inGlobalData) noexcept + void SHRenderGraph::Init(Handle const& logicalDevice, Handle const& swapchain, Handle inGlobalData) noexcept { logicalDeviceHdl = logicalDevice; swapchainHdl = swapchain; globalData = inGlobalData; - commandPools = cmdPools; } /***************************************************************************/ @@ -1087,33 +1067,17 @@ namespace SHADE ConfigureSubpasses(); ConfigureRenderpasses(); ConfigureFramebuffers(); - ConfigureCommands(); } // TODO: The graph scope buffers were meant to bind vertex buffers and index buffers for meshes. Find a // better way to manage these - void SHRenderGraph::Execute(uint32_t frameIndex, std::initializer_list, uint32_t>> graphScopeBuffers) noexcept + void SHRenderGraph::Execute(uint32_t frameIndex, Handle cmdBuffer) noexcept { - //commandPools[frameIndex]->Reset(); - - auto& cmdBuffer = commandBuffers[frameIndex]; - cmdBuffer->BeginRecording(); - // TODO: DON'T HARDCODE THIS cmdBuffer->SetViewportScissor(1920.0f, 1080.0f, 1920, 1080); - for (auto& [buffer, bindingPoint]: graphScopeBuffers) - { - if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eVertexBuffer) - cmdBuffer->BindVertexBuffer(bindingPoint, buffer, 0); - else if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eIndexBuffer) - cmdBuffer->BindIndexBuffer(buffer, 0); - } - for (auto& node : nodes) node->Execute(cmdBuffer, frameIndex); - - cmdBuffer->EndRecording(); } void SHRenderGraph::FinaliseBatch() @@ -1132,9 +1096,5 @@ namespace SHADE return {}; } - Handle const& SHRenderGraph::GetCommandBuffer(uint32_t frameIndex) const noexcept - { - return commandBuffers[frameIndex]; - } } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index 0ecda65a..cbb9586d 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -250,7 +250,6 @@ namespace SHADE void ConfigureSubpasses (void) noexcept; void ConfigureRenderpasses (void) noexcept; void ConfigureFramebuffers (void) noexcept; - void ConfigureCommands (void) noexcept; /*-----------------------------------------------------------------------*/ /* PRIVATE MEMBER VARIABLES */ @@ -271,12 +270,6 @@ namespace SHADE //! Resource library for graph handles ResourceManager resourceManager; - - //! Command pool for the render graph. DO NOT RESET OR FREE THESE, THEY ARE NON-OWNING. TODO: If application is multithreaded, we need more pools. - std::vector> commandPools; - - //! Command buffers for the render graph - std::vector> commandBuffers; //! Handle to global data Handle globalData; @@ -290,18 +283,17 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - void Init (Handle const& logicalDevice, Handle const& swapchain, std::vector> const& cmdPools, Handle inGlobalData) noexcept; + void Init (Handle const& logicalDevice, Handle const& swapchain, Handle inGlobalData) noexcept; void AddResource (std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint8_t levels = 1, vk::ImageCreateFlagBits createFlags = {}); Handle AddNode (std::string nodeName, std::initializer_list resourceNames, std::initializer_list predecessorNodes) noexcept; void Generate (void) noexcept; - void Execute (uint32_t frameIndex, std::initializer_list, uint32_t>> graphScopeBuffers) noexcept; + void Execute (uint32_t frameIndex, Handle cmdBuffer) noexcept; void FinaliseBatch(); /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ /*-----------------------------------------------------------------------*/ Handle GetNode (std::string const& nodeName) const noexcept; - Handle const& GetCommandBuffer (uint32_t frameIndex) const noexcept; }; } diff --git a/SHADE_Engine/src/Math/SHMath.h b/SHADE_Engine/src/Math/SHMath.h index 55bf73a9..5fcea9fc 100644 --- a/SHADE_Engine/src/Math/SHMath.h +++ b/SHADE_Engine/src/Math/SHMath.h @@ -9,4 +9,4 @@ #include "SHQuaternion.h" #include "SHMatrix.h" -#include "SHTransform.h" \ No newline at end of file +#include "Transform/SHTransform.h" \ No newline at end of file From 7ddf665460b326c3872338da35ec8e8051cb83ff Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Thu, 22 Sep 2022 12:12:44 +0800 Subject: [PATCH 2/2] Updated Descriptor sets Descriptor set layouts now have set index stored in them It should be noted that when passed into a pipeline layout as global descriptor sets, these don't matter since the pipeline layout will use these in the order that they are passed in. --- .../Descriptors/SHDescriptorSetUpdater.cpp | 22 ---- .../Descriptors/SHDescriptorSetUpdater.h | 6 -- .../Descriptors/SHVkDescriptorSetGroup.cpp | 102 +++++++++++++----- .../Descriptors/SHVkDescriptorSetGroup.h | 22 +++- .../Descriptors/SHVkDescriptorSetLayout.cpp | 10 +- .../Descriptors/SHVkDescriptorSetLayout.h | 4 +- .../Graphics/Devices/SHVkLogicalDevice.cpp | 4 +- .../src/Graphics/Devices/SHVkLogicalDevice.h | 2 +- .../src/Graphics/Images/SHVkSampler.cpp | 5 + .../src/Graphics/Images/SHVkSampler.h | 1 + .../GlobalData/SHGraphicsGlobalData.cpp | 8 +- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 8 +- .../MiddleEnd/Interface/SHRenderer.cpp | 6 ++ .../Graphics/Pipeline/SHVkPipelineLayout.cpp | 2 +- SHADE_Engine/src/Graphics/SHVkUtil.cpp | 10 ++ SHADE_Engine/src/Graphics/SHVkUtil.h | 2 + SHADE_Engine/src/Graphics/SHVulkanDefines.h | 2 + SHADE_Engine/src/Graphics/SHVulkanIncludes.h | 1 + .../src/Graphics/Shaders/SHShaderReflected.h | 2 - .../ShaderDescriptorDefinitions.glsl | 11 ++ TempShaderFolder/TestCubeVs.glsl | 8 ++ TempShaderFolder/TestCubeVs.spv | Bin 1428 -> 1680 bytes 22 files changed, 163 insertions(+), 75 deletions(-) create mode 100644 TempShaderFolder/ShaderDescriptorDefinitions.glsl diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.cpp b/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.cpp index a1b1cbc2..6cd7ddfd 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.cpp +++ b/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.cpp @@ -20,23 +20,6 @@ namespace SHADE } - /***************************************************************************/ - /*! - - \brief - Links the write infos to the vulkan write descriptor sets. - - */ - /***************************************************************************/ - void SHDescriptorSetUpdater::LinkInfoToWriteDescSet(void) noexcept - { - for (uint32_t i = 0; i < writeInfos.size(); ++i) - { - writeDescSets[i].pImageInfo = writeInfos[i].descImageInfos.data(); - writeDescSets[i].pBufferInfo = writeInfos[i].descBufferInfos.data(); - writeDescSets[i].pTexelBufferView = writeInfos[i].descTexelBufferInfos.data(); - } - } SHDescriptorWriteInfo& SHDescriptorWriteInfo::operator=(SHDescriptorWriteInfo&& rhs) noexcept { @@ -65,11 +48,6 @@ namespace SHADE } - std::vector const& SHDescriptorSetUpdater::GetWriteDescriptorSets(void) const noexcept - { - return writeDescSets; - } - SHDescriptorSetUpdater& SHDescriptorSetUpdater::operator=(SHDescriptorSetUpdater&& rhs) noexcept { if (&rhs == this) diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.h b/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.h index 7a5ae967..8595d2a0 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.h +++ b/SHADE_Engine/src/Graphics/Descriptors/SHDescriptorSetUpdater.h @@ -41,10 +41,6 @@ namespace SHADE //! When we want to update a write, we need to use this to identify the index of the write. std::unordered_map writeHashMap; - //! We keep this here because we want this to be immediately passable to vkUpdateDescriptorSets - std::vector writeDescSets; - - void LinkInfoToWriteDescSet(void) noexcept; public: SHDescriptorSetUpdater (void) noexcept; @@ -52,8 +48,6 @@ namespace SHADE SHDescriptorSetUpdater& operator= (SHDescriptorSetUpdater&& rhs) noexcept; public: - std::vector const& GetWriteDescriptorSets (void) const noexcept; - friend class SHVkDescriptorSetGroup; }; } diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp index 4e83d319..f579d377 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp @@ -7,6 +7,11 @@ #include "Graphics/Devices/SHVkLogicalDevice.h" #include "Graphics/Descriptors/SHVkDescriptorSetLayout.h" #include "Tools/SHLogger.h" +#include "Graphics/Images/SHVkImage.h" +#include "Graphics/Images/SHVkImageView.h" +#include "Graphics/Images/SHVkSampler.h" +#include "Graphics/Buffers/SHVkBuffer.h" +#include "Graphics/SHVkUtil.h" namespace SHADE { @@ -39,6 +44,7 @@ namespace SHADE : device{deviceHdl} , descPool {pool} , descSets{} + , layoutsUsed {layouts} { // Create the layout for each concurrent frame std::vector vkLayouts{ layouts.size() }; @@ -47,6 +53,7 @@ namespace SHADE for (uint32_t i = 0; i < layouts.size(); ++i) { vkLayouts[i] = layouts[i]->GetVkHandle(); + setIndexing.emplace(layouts[i]->GetSetIndex(), i); } // Check for variable descriptor count @@ -73,34 +80,22 @@ namespace SHADE // allocate descriptor sets descSets = device->GetVkLogicalDevice().allocateDescriptorSets(DESC_SET_LAYOUT_CREATE_INFO); - - - // Now we want to prepare the write descriptor sets for writing later. + // Now we want to prepare the write descriptor sets info for writing later. for (uint32_t i = 0; i < layouts.size(); ++i) { auto const& bindings = layouts[i]->GetBindings(); for (auto& binding : bindings) { BindingAndSetHash writeHash = binding.BindPoint; - writeHash |= static_cast(i) << 32; + writeHash |= static_cast(layouts[i]->GetSetIndex()) << 32; // new write for the binding updater.writeInfos.emplace_back(); updater.writeHashMap.try_emplace(writeHash, updater.writeInfos.size() - 1); auto& writeInfo = updater.writeInfos.back(); - updater.writeDescSets.emplace_back(); - auto& writeDescSet = updater.writeDescSets.back(); - - // Initialize info for write - writeDescSet.descriptorType = binding.Type; - writeDescSet.dstArrayElement = 0; - writeDescSet.dstSet = descSets[i]; - writeDescSet.dstBinding = binding.BindPoint; - // Descriptor count for the write descriptor set. Usually this is set to 1, but if binding is variable sized, set to info passed in uint32_t descriptorCount = (binding.flags & vk::DescriptorBindingFlagBits::eVariableDescriptorCount) ? variableDescCounts[i] : 1; - writeDescSet.descriptorCount = descriptorCount; switch (binding.Type) { @@ -114,8 +109,10 @@ namespace SHADE case vk::DescriptorType::eUniformTexelBuffer: case vk::DescriptorType::eStorageTexelBuffer: case vk::DescriptorType::eUniformBuffer: + case vk::DescriptorType::eUniformBufferDynamic: case vk::DescriptorType::eStorageBuffer: - writeInfo.descImageInfos.resize (descriptorCount); + case vk::DescriptorType::eStorageBufferDynamic: + writeInfo.descBufferInfos.resize (descriptorCount); break; //case vk::DescriptorType::eUniformBufferDynamic: // break; @@ -130,8 +127,6 @@ namespace SHADE } } } - // Link all the writeDescSet data for vkUpdateDescriptorSets to write to the linked descriptors - updater.LinkInfoToWriteDescSet(); } /***************************************************************************/ @@ -160,7 +155,7 @@ namespace SHADE */ /***************************************************************************/ - void SHVkDescriptorSetGroup::ModifyWriteDescImage(uint32_t set, uint32_t binding, std::vector> const& imageViewsAndSamplers) noexcept + void SHVkDescriptorSetGroup::ModifyWriteDescImage(uint32_t set, uint32_t binding, std::span, Handle>> const& imageViewsAndSamplers) noexcept { // Find the target writeDescSet BindingAndSetHash writeHash = binding; @@ -176,32 +171,81 @@ namespace SHADE { // write sampler and image view auto& ivs = imageViewsAndSamplers[i]; - writeInfo.descImageInfos[i].imageView = ivs.first; - writeInfo.descImageInfos[i].sampler = ivs.second; + writeInfo.descImageInfos[i].imageView = ivs.first->GetImageView(); + writeInfo.descImageInfos[i].sampler = ivs.second->GetVkSampler(); } } - - void SHVkDescriptorSetGroup::UpdateDescriptorSet(void) noexcept + void SHVkDescriptorSetGroup::ModifyWriteDescBuffer(uint32_t set, uint32_t binding, std::span> const& buffers) noexcept { - device->UpdateDescriptorSets(updater.GetWriteDescriptorSets()); + // Find the target writeDescSet + BindingAndSetHash writeHash = binding; + writeHash |= static_cast(set) << 32; + auto& writeInfo = updater.writeInfos[updater.writeHashMap.at(writeHash)]; + + if (buffers.size() > writeInfo.descBufferInfos.size()) + { + SHLOG_ERROR("Attempting write too many descriptors into descriptor set. Failed to write to vk::WriteDescriptorSet. "); + } + + for (uint32_t i = 0; i < buffers.size(); ++i) + { + // write sampler and image view + auto& buffer = buffers[i]; + writeInfo.descBufferInfos[i].buffer = buffer->GetVkBuffer(); + } + } - - void SHVkDescriptorSetGroup::UpdateSingleDescriptorSetImages(uint32_t set, uint32_t binding) noexcept + void SHVkDescriptorSetGroup::UpdateDescriptorSetImages(uint32_t set, uint32_t binding) noexcept { vk::WriteDescriptorSet writeDescSet{}; + // Get binding + set hash + BindingAndSetHash bsHash = SHVkUtil::GenBindingSetHash(set, binding); + + // to index a set + uint32_t setIndex = setIndexing[bsHash]; + + // to index a write for a binding + uint32_t writeInfoIndex = updater.writeHashMap[bsHash]; + // Initialize info for write - writeDescSet.descriptorType = vk::DescriptorType::eCombinedImageSampler; + writeDescSet.descriptorType = layoutsUsed[setIndex]->GetBindings()[binding].Type; writeDescSet.dstArrayElement = 0; - writeDescSet.dstSet = descSets[set]; + writeDescSet.dstSet = descSets[setIndex]; writeDescSet.dstBinding = binding; - writeDescSet.pImageInfo = updater.writeInfos[set].descImageInfos.data(); - writeDescSet.descriptorCount = static_cast(updater.writeInfos[set].descImageInfos.size()); + writeDescSet.pImageInfo = updater.writeInfos[writeInfoIndex].descImageInfos.data(); + writeDescSet.descriptorCount = static_cast(updater.writeInfos[writeInfoIndex].descImageInfos.size()); device->UpdateDescriptorSet(writeDescSet); } + void SHVkDescriptorSetGroup::UpdateDescriptorSetBuffer(uint32_t set, uint32_t binding) noexcept + { + vk::WriteDescriptorSet writeDescSet{}; + + // Get binding + set hash + BindingAndSetHash bsHash = SHVkUtil::GenBindingSetHash(set, binding); + + // to index a set + uint32_t setIndex = setIndexing[bsHash]; + + // to index a write for a binding + uint32_t writeInfoIndex = updater.writeHashMap[bsHash]; + + // Initialize info for write + writeDescSet.descriptorType = layoutsUsed[setIndex]->GetBindings()[binding].Type; + writeDescSet.dstArrayElement = 0; + writeDescSet.dstSet = descSets[setIndex]; + writeDescSet.dstBinding = binding; + + writeDescSet.pBufferInfo = updater.writeInfos[writeInfoIndex].descBufferInfos.data(); + writeDescSet.descriptorCount = static_cast(updater.writeInfos[writeInfoIndex].descBufferInfos.size()); + + device->UpdateDescriptorSet(writeDescSet); + + } + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h index b65fb1e6..bc87ff1d 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h @@ -14,6 +14,10 @@ namespace SHADE class SHVkLogicalDevice; class SHVkDescriptorPool; class SHVkDescriptorSetLayout; + class SHVkSampler; + class SHVkImage; + class SHVkImageView; + class SHVkBuffer; /*---------------------------------------------------------------------------------*/ @@ -54,12 +58,14 @@ namespace SHADE SHVkDescriptorSetGroup& operator=(SHVkDescriptorSetGroup&& rhs) noexcept = default; /*-----------------------------------------------------------------------------*/ - /* Descriptor set writing */ + /* Public member functions */ /*-----------------------------------------------------------------------------*/ - void ModifyWriteDescImage(uint32_t set, uint32_t binding, std::vector> const& imageViewsAndSamplers) noexcept; - void UpdateDescriptorSet(void) noexcept; + void UpdateDescriptorSetImages(uint32_t set, uint32_t binding) noexcept; + void UpdateDescriptorSetBuffer(uint32_t set, uint32_t binding) noexcept; + + void ModifyWriteDescImage(uint32_t set, uint32_t binding, std::span, Handle>> const& imageViewsAndSamplers) noexcept; + void ModifyWriteDescBuffer (uint32_t set, uint32_t binding, std::span> const& buffers) noexcept; - void UpdateSingleDescriptorSetImages(uint32_t set, uint32_t binding) noexcept; /*-----------------------------------------------------------------------------*/ /* Getter Functions */ @@ -81,9 +87,17 @@ namespace SHADE //! Descriptor pool to allocate descriptor sets Handle descPool; + //! Sometimes when we pass in a layout, the set of the layout used in the + //! shader cannot be used to index into descSets. This is to mitigate that issue + //! when we update descriptor sets. + std::unordered_map setIndexing; + //! Descriptor sets std::vector descSets; + //! Layouts used to create this descriptor set group + std::vector> layoutsUsed; + //! for updating descriptor sets. We want to cache this so that we don't create the //! write structs at runtime. SHDescriptorSetUpdater updater; diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.cpp b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.cpp index da1a3645..002aa29d 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.cpp +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.cpp @@ -7,9 +7,10 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Constructor/Destructor */ /*---------------------------------------------------------------------------------*/ - SHVkDescriptorSetLayout::SHVkDescriptorSetLayout(Handle device, const std::vector& bindings) + SHVkDescriptorSetLayout::SHVkDescriptorSetLayout(Handle device, SetIndex set, const std::vector& bindings) : device { device } , layoutDesc { bindings } + , setIndex {set} { // Check if auto-binding point calculation configuration is valid bool autoCalc = false; @@ -74,6 +75,7 @@ namespace SHADE : device {rhs.device} , setLayout {rhs.setLayout} , layoutDesc{std::move (rhs.layoutDesc)} + , setIndex {rhs.setIndex} { rhs.setLayout = VK_NULL_HANDLE; } @@ -90,6 +92,11 @@ namespace SHADE return layoutDesc; } + SetIndex SHVkDescriptorSetLayout::GetSetIndex(void) const noexcept + { + return setIndex; + } + SHVkDescriptorSetLayout& SHVkDescriptorSetLayout::operator=(SHVkDescriptorSetLayout&& rhs) noexcept { if (&rhs == this) @@ -98,6 +105,7 @@ namespace SHADE device = rhs.device; setLayout = rhs.setLayout; layoutDesc = std::move(rhs.layoutDesc); + setIndex = rhs.setIndex; rhs.setLayout = VK_NULL_HANDLE; diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.h b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.h index 1acba189..9b436026 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.h +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetLayout.h @@ -74,7 +74,7 @@ namespace SHADE /// /// /// - SHVkDescriptorSetLayout(Handle device, const std::vector& bindings); + SHVkDescriptorSetLayout(Handle device, SetIndex setIndex, const std::vector& bindings); SHVkDescriptorSetLayout(const SHVkDescriptorSetLayout&) = delete; SHVkDescriptorSetLayout(SHVkDescriptorSetLayout&& rhs) noexcept; /// @@ -97,6 +97,7 @@ namespace SHADE /// Handle to the Vulkan Descriptor Set Layout handle. inline const vk::DescriptorSetLayout& GetVkHandle() const { return setLayout; } std::vector const& GetBindings (void) const noexcept; + SetIndex GetSetIndex (void) const noexcept; private: /*-----------------------------------------------------------------------------*/ @@ -105,5 +106,6 @@ namespace SHADE Handle device; vk::DescriptorSetLayout setLayout; std::vector layoutDesc; // Stores description of the layout + SetIndex setIndex; // Index of the set }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp index a956af2c..64508a2b 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp @@ -510,9 +510,9 @@ namespace SHADE } - Handle SHVkLogicalDevice::CreateDescriptorSetLayout(std::vector const& bindings) noexcept + Handle SHVkLogicalDevice::CreateDescriptorSetLayout(SetIndex setIndex, std::vector const& bindings) noexcept { - return SHVkInstance::GetResourceManager().Create (GetHandle(), bindings); + return SHVkInstance::GetResourceManager().Create (GetHandle(), setIndex, bindings); } diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h index 6c59572a..365e68c9 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h @@ -182,7 +182,7 @@ namespace SHADE Handle CreateRenderpass (std::span const vkDescriptions, std::vector const& subpasses) noexcept; Handle CreateRenderpass (std::span const vkDescriptions, std::span const spDescs, std::span const spDeps) noexcept; Handle CreateFramebuffer (Handle const& renderpassHdl, std::vector> const& attachments, uint32_t inWidth, uint32_t inHeight) noexcept; - Handle CreateDescriptorSetLayout (std::vector const& bindings) noexcept; + Handle CreateDescriptorSetLayout (SetIndex setIndex, std::vector const& bindings) noexcept; Handle CreateDescriptorPools (const SHVkDescriptorPool::Config& config = {}) noexcept; Handle CreateDescriptorSetGroup(Handle pool, std::vector> const& layouts, diff --git a/SHADE_Engine/src/Graphics/Images/SHVkSampler.cpp b/SHADE_Engine/src/Graphics/Images/SHVkSampler.cpp index e7a97126..b4bbf89b 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkSampler.cpp +++ b/SHADE_Engine/src/Graphics/Images/SHVkSampler.cpp @@ -4,4 +4,9 @@ namespace SHADE { + vk::Sampler SHVkSampler::GetVkSampler(void) const noexcept + { + return vkSampler; + } + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/Images/SHVkSampler.h b/SHADE_Engine/src/Graphics/Images/SHVkSampler.h index d58edb8f..eae23adf 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkSampler.h +++ b/SHADE_Engine/src/Graphics/Images/SHVkSampler.h @@ -22,6 +22,7 @@ namespace SHADE SHVkSampler (SHVkSampler&& rhs) noexcept; SHVkSampler&& operator=(SHVkSampler&& rhs) noexcept; + vk::Sampler GetVkSampler (void) const noexcept; }; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp index 5289b635..d9ffa6b5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp @@ -27,7 +27,7 @@ namespace SHADE }; // For global data (generic data and textures) - Handle staticGlobalLayout = logicalDevice->CreateDescriptorSetLayout({ genericDataBinding, texturesBinding }); + Handle staticGlobalLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::STATIC_GLOBALS,{ genericDataBinding, texturesBinding }); SHVkDescriptorSetLayout::Binding lightBinding { @@ -38,7 +38,7 @@ namespace SHADE }; // For Dynamic global data (lights) - Handle dynamicGlobalLayout = logicalDevice->CreateDescriptorSetLayout({ lightBinding }); + Handle dynamicGlobalLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS, { lightBinding }); SHVkDescriptorSetLayout::Binding cameraDataBinding { @@ -49,7 +49,7 @@ namespace SHADE }; // For High frequency global data (camera) - Handle cameraDataGlobalLayout = logicalDevice->CreateDescriptorSetLayout({ cameraDataBinding }); + Handle cameraDataGlobalLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, { cameraDataBinding }); SHVkDescriptorSetLayout::Binding materialDataBinding { @@ -60,7 +60,7 @@ namespace SHADE }; // For High frequency global data (camera) - Handle materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout({ materialDataBinding }); + Handle materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE, { materialDataBinding }); globalDescSetLayouts.push_back(staticGlobalLayout); globalDescSetLayouts.push_back(dynamicGlobalLayout); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index b38fa275..1026d01b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -204,6 +204,7 @@ namespace SHADE { // Frame data for the current frame auto const& frameData = renderContext.GetCurrentFrameData(); + uint32_t frameIndex = renderContext.GetCurrentFrame(); // semaphore index. This is for render graphs to have semaphores to signal that the next render graph will use to wait on. bool semIndex = 0; @@ -233,7 +234,7 @@ namespace SHADE /* Renderer start */ /*-----------------------------------------------------------------------*/ // get command buffer of the renderer in the current frame - auto currentCmdBuffer = renderers[renIndex]->GetCommandBuffer(renderContext.GetCurrentFrame()); + auto currentCmdBuffer = renderers[renIndex]->GetCommandBuffer(frameIndex); // Begin recording the command buffer currentCmdBuffer->BeginRecording(); @@ -247,8 +248,11 @@ namespace SHADE currentCmdBuffer->BindIndexBuffer(buffer, 0); } + // bind camera data + renderers[renIndex]->BindDescriptorSet(currentCmdBuffer, frameIndex); + // Draw first - renderers[renIndex]->Draw(renderContext.GetCurrentFrame()); + renderers[renIndex]->Draw(frameIndex); // End the command buffer recording currentCmdBuffer->EndRecording(); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp index 223ceb4f..fd702968 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp @@ -42,6 +42,12 @@ namespace SHADE cameraDataAlignedSize = logicalDevice->PadUBOSize(sizeof(SHShaderCameraData)); cameraBuffer = logicalDevice->CreateBuffer(cameraDataAlignedSize * numFrames, nullptr, cameraDataAlignedSize * numFrames, vk::BufferUsageFlagBits::eUniformBuffer, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT); + + std::array cameraBufferArray{cameraBuffer}; + + cameraDescriptorSet->ModifyWriteDescBuffer(SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, SHGraphicsConstants::DescriptorSetBindings::CAMERA_DATA, std::span>{ cameraBufferArray.data(), cameraBufferArray.size()}); + + cameraDescriptorSet->UpdateDescriptorSetBuffer(SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, SHGraphicsConstants::DescriptorSetBindings::CAMERA_DATA); } /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp index cc738aed..fc40f394 100644 --- a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp +++ b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp @@ -198,7 +198,7 @@ namespace SHADE // 1 descriptor set layout for every descriptor set detected. for (auto const& set : setsWithBindings) { - auto newDescriptorSetLayout = logicalDeviceHdl->CreateDescriptorSetLayout(set.second); + auto newDescriptorSetLayout = logicalDeviceHdl->CreateDescriptorSetLayout(set.first, set.second); descriptorSetLayoutsAllocate.push_back(newDescriptorSetLayout); } diff --git a/SHADE_Engine/src/Graphics/SHVkUtil.cpp b/SHADE_Engine/src/Graphics/SHVkUtil.cpp index c570caf2..e4f9f37e 100644 --- a/SHADE_Engine/src/Graphics/SHVkUtil.cpp +++ b/SHADE_Engine/src/Graphics/SHVkUtil.cpp @@ -71,4 +71,14 @@ namespace SHADE ); } } + + BindingAndSetHash SHVkUtil::GenBindingSetHash(uint32_t set, uint32_t binding) noexcept + { + // Find the target writeDescSet + BindingAndSetHash writeHash = binding; + writeHash |= static_cast(set) << 32; + + return writeHash; + } + } diff --git a/SHADE_Engine/src/Graphics/SHVkUtil.h b/SHADE_Engine/src/Graphics/SHVkUtil.h index 46ebf096..cba5b062 100644 --- a/SHADE_Engine/src/Graphics/SHVkUtil.h +++ b/SHADE_Engine/src/Graphics/SHVkUtil.h @@ -76,6 +76,8 @@ namespace SHADE */ /***********************************************************************************/ static void EnsureBufferAndCopyHostVisibleData(Handle device, Handle& bufferHandle, void* src, uint32_t size, vk::BufferUsageFlagBits usage); + + static BindingAndSetHash GenBindingSetHash (uint32_t set, uint32_t binding) noexcept; }; } diff --git a/SHADE_Engine/src/Graphics/SHVulkanDefines.h b/SHADE_Engine/src/Graphics/SHVulkanDefines.h index 542f379d..2f625f7e 100644 --- a/SHADE_Engine/src/Graphics/SHVulkanDefines.h +++ b/SHADE_Engine/src/Graphics/SHVulkanDefines.h @@ -6,6 +6,8 @@ namespace SHADE { using SHQueueFamilyIndex = uint32_t; + using BindingAndSetHash = uint64_t; + using SetIndex = uint32_t; } diff --git a/SHADE_Engine/src/Graphics/SHVulkanIncludes.h b/SHADE_Engine/src/Graphics/SHVulkanIncludes.h index 472226a0..63aa672b 100644 --- a/SHADE_Engine/src/Graphics/SHVulkanIncludes.h +++ b/SHADE_Engine/src/Graphics/SHVulkanIncludes.h @@ -6,5 +6,6 @@ #define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1 #define VULKAN_HPP_NO_NODISCARD_WARNINGS #include +#include "Graphics/SHVulkanDefines.h" #endif diff --git a/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.h b/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.h index 88c7a2e1..3986f823 100644 --- a/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.h +++ b/SHADE_Engine/src/Graphics/Shaders/SHShaderReflected.h @@ -9,8 +9,6 @@ namespace SHADE { - using BindingAndSetHash = uint64_t; - struct SHShaderDescriptorBindingInfo { private: diff --git a/TempShaderFolder/ShaderDescriptorDefinitions.glsl b/TempShaderFolder/ShaderDescriptorDefinitions.glsl new file mode 100644 index 00000000..c70a9f5b --- /dev/null +++ b/TempShaderFolder/ShaderDescriptorDefinitions.glsl @@ -0,0 +1,11 @@ +#define SET_STATIC_GLOBALS 0 +#define SET_DYNAMIC_GLOBALS 1 +#define SET_HIGH_FREQUENCY_GLOBALS 2 + +#define BINDING_GENERIC_DATA 0 +#define BINDING_IMAGE_AND_SAMPLERS_DATA 1 +#define BINDING_LIGHTS_DATA 0 +#define BINDING_CAMERA_DATA 0 +#define BINDING_BATCHED_PER_INST_DATA 0 + + diff --git a/TempShaderFolder/TestCubeVs.glsl b/TempShaderFolder/TestCubeVs.glsl index f7dce787..08c465a0 100644 --- a/TempShaderFolder/TestCubeVs.glsl +++ b/TempShaderFolder/TestCubeVs.glsl @@ -1,6 +1,8 @@ #version 450 #extension GL_KHR_vulkan_glsl : enable +//#include "ShaderDescriptorDefinitions.glsl" + layout(location = 0) in vec3 aVertexPos; layout(location = 1) in vec2 aUV; layout(location = 2) in vec3 aNormal; @@ -28,6 +30,12 @@ layout(location = 0) out struct } Out; +layout(set = 2, binding = 0) uniform CameraData +{ + vec4 position; + mat4 vpMat; +} cameraData; + void main() { //const float gamma = testPushConstant.eyePosition.w; diff --git a/TempShaderFolder/TestCubeVs.spv b/TempShaderFolder/TestCubeVs.spv index 2b0d3b93c0c4a570f997eae1f3763e05cb6cbbab..be24aa3a944eff08f83a27869e46ef93dcc93eae 100644 GIT binary patch delta 293 zcmbQjJ%N{(nMs+Qfq{{Mi-DIxe#voP(Rt6mg1_tNE+|;5(m&B4p1_m}D1c`w_ zL4I*&NoIZ?h|LC7%Lrtb75FBW0BMkZJs_P7*FX6kw9PW?IXrD+jd@ dWRNBhD**YrKpLbGp;Bdp1`vZ(-aV!)n3^0DNT#AOHXW