From d9213fe35d4421961fb50de1339823b17d94ba3e Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Tue, 20 Sep 2022 10:39:52 +0800 Subject: [PATCH] Moved a bunch of global data from Graphics System to new class New class in SHGraphicsGlobalData --- .../GlobalData/SHGraphicsGlobalData.cpp | 103 ++++++++++++++++++ .../GlobalData/SHGraphicsGlobalData.h | 38 +++++++ .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 80 +------------- .../MiddleEnd/Interface/SHGraphicsSystem.h | 19 +--- .../MiddleEnd/Pipeline/SHPipelineLibrary.cpp | 10 +- .../MiddleEnd/Pipeline/SHPipelineLibrary.h | 9 +- .../Graphics/RenderGraph/SHRenderGraph.cpp | 11 +- .../src/Graphics/RenderGraph/SHRenderGraph.h | 14 +-- 8 files changed, 172 insertions(+), 112 deletions(-) create mode 100644 SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp create mode 100644 SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp new file mode 100644 index 00000000..46480a6a --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp @@ -0,0 +1,103 @@ +#include "SHpch.h" +#include "SHGraphicsGlobalData.h" +#include "Graphics/Devices/SHVkLogicalDevice.h" +#include "Graphics/Pipeline/SHPipelineState.h" +#include "Graphics/Descriptors/SHVkDescriptorSetLayout.h" + +namespace SHADE +{ + + void SHGraphicsGlobalData::InitDescSetLayouts(Handle logicalDevice) noexcept + { + SHVkDescriptorSetLayout::Binding genericDataBinding + { + .Type = vk::DescriptorType::eUniformBufferDynamic, + .Stage = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment | vk::ShaderStageFlagBits::eCompute, + .BindPoint = 0, + .DescriptorCount = 1, + }; + + SHVkDescriptorSetLayout::Binding texturesBinding + { + .Type = vk::DescriptorType::eCombinedImageSampler, + .Stage = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment | vk::ShaderStageFlagBits::eCompute, + .BindPoint = 1, + .DescriptorCount = 2000, // we can have up to 2000 textures for now + .flags = vk::DescriptorBindingFlagBits::eVariableDescriptorCount, + }; + + // For global data (generic data and textures) + Handle staticGlobalLayout = logicalDevice->CreateDescriptorSetLayout({ genericDataBinding, texturesBinding }); + + SHVkDescriptorSetLayout::Binding lightBinding + { + .Type = vk::DescriptorType::eStorageBufferDynamic, + .Stage = vk::ShaderStageFlagBits::eFragment, + .BindPoint = 0, + .DescriptorCount = 1, + }; + + // For Dynamic global data (lights) + Handle dynamicGlobalLayout = logicalDevice->CreateDescriptorSetLayout({ lightBinding }); + + SHVkDescriptorSetLayout::Binding cameraDataBinding + { + .Type = vk::DescriptorType::eUniformBufferDynamic, + .Stage = vk::ShaderStageFlagBits::eVertex, + .BindPoint = 0, + .DescriptorCount = 1, + }; + + // For High frequency global data (camera) + Handle cameraDataGlobalLayout = logicalDevice->CreateDescriptorSetLayout({ cameraDataBinding }); + + SHVkDescriptorSetLayout::Binding materialDataBinding + { + .Type = vk::DescriptorType::eStorageBufferDynamic, + .Stage = vk::ShaderStageFlagBits::eFragment, + .BindPoint = 0, + .DescriptorCount = 1, + }; + + // For High frequency global data (camera) + Handle materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout({ materialDataBinding }); + + globalDescSetLayouts.push_back(staticGlobalLayout); + globalDescSetLayouts.push_back(dynamicGlobalLayout); + globalDescSetLayouts.push_back(cameraDataGlobalLayout); + globalDescSetLayouts.push_back(materialDataPerInstanceLayout); + } + + + void SHGraphicsGlobalData::InitGlobalDescSets(Handle logicalDevice) noexcept + { + //globalDescSets = logicalDevice->set + } + + void SHGraphicsGlobalData::InitDefaultVertexInputState(void) noexcept + { + defaultVertexInputState.AddBinding(false, false, { SHVertexAttribute(SHAttribFormat::FLOAT_3D) }); // positions at binding 0 + defaultVertexInputState.AddBinding(false, false, { SHVertexAttribute(SHAttribFormat::FLOAT_2D) }); // UVs at binding 1 + defaultVertexInputState.AddBinding(false, false, {SHVertexAttribute(SHAttribFormat::FLOAT_3D)}); // Normals at binding 2 + defaultVertexInputState.AddBinding(false, false, {SHVertexAttribute(SHAttribFormat::FLOAT_3D)}); // Tangents at binding 3 + defaultVertexInputState.AddBinding(true, true, {SHVertexAttribute(SHAttribFormat::MAT_4D)}); // Transform at binding 4 - 7 (4 slots) + } + + void SHGraphicsGlobalData::Init(Handle logicalDevice) noexcept + { + InitDescSetLayouts(logicalDevice); + InitDefaultVertexInputState(); + } + + std::vector> const& SHGraphicsGlobalData::GetDescSetLayouts(void) const noexcept + { + return globalDescSetLayouts; + } + + + SHVertexInputState const& SHGraphicsGlobalData::GetDefaultViState(void) const noexcept + { + return defaultVertexInputState; + } + +} diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h new file mode 100644 index 00000000..59ca7f63 --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h @@ -0,0 +1,38 @@ +#pragma once + +#include "SH_API.h" +#include "Graphics/Pipeline/SHPipelineState.h" + +namespace SHADE +{ + class SHVkLogicalDevice; + class SHVkDescriptorSetLayout; + + class SH_API SHGraphicsGlobalData + { + private: + //! Global descriptor set layouts. Used to allocate descriptor sets + std::vector> globalDescSetLayouts; + + //! Global Descriptor sets + Handle globalDescSets; + + //! Default vertex input state (used by everything). + SHVertexInputState defaultVertexInputState; + + void InitDescSetLayouts (Handle logicalDevice) noexcept; + void InitGlobalDescSets (Handle logicalDevice) noexcept; + void InitDefaultVertexInputState(void) noexcept; + public: + /*-----------------------------------------------------------------------*/ + /* PUBLIC MEMBER FUNCTIONS */ + /*-----------------------------------------------------------------------*/ + void Init (Handle logicalDevice) noexcept; + + /*-----------------------------------------------------------------------*/ + /* SETTERS AND GETTERS */ + /*-----------------------------------------------------------------------*/ + std::vector> const& GetDescSetLayouts (void) const noexcept; + SHVertexInputState const& GetDefaultViState (void) const noexcept; + }; +} diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index ea18b817..e4f3e69e 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -28,6 +28,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Graphics/MiddleEnd/Batching/SHSuperBatch.h" #include "SHGraphicsConstants.h" +#include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h" namespace SHADE { @@ -100,10 +101,11 @@ namespace SHADE - Render graph in renderers - Render graph command buffer semaphores - Default vertex input state - - Global descriptor set layouts + - Global data /*-----------------------------------------------------------------------*/ - ConfigureGlobalDescLayouts(); - ConfigureDefaultVertexInputState(); + + globalData = resourceManager.Create(); + globalData->Init(device); // Set Up Cameras screenCamera = resourceManager.Create(); @@ -120,7 +122,7 @@ namespace SHADE auto worldRenderGraph = resourceManager.Create(); // Initialize world render graph - worldRenderGraph->Init(device, swapchain, &defaultVertexInputState, &globalDescSetLayouts); + 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); @@ -420,76 +422,6 @@ namespace SHADE window = wind; } - void SHGraphicsSystem::ConfigureGlobalDescLayouts(void) noexcept - { - SHVkDescriptorSetLayout::Binding genericDataBinding - { - .Type = vk::DescriptorType::eUniformBufferDynamic, - .Stage = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment | vk::ShaderStageFlagBits::eCompute, - .BindPoint = 0, - .DescriptorCount = 1, - }; - - SHVkDescriptorSetLayout::Binding texturesBinding - { - .Type = vk::DescriptorType::eCombinedImageSampler, - .Stage = vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment | vk::ShaderStageFlagBits::eCompute, - .BindPoint = 1, - .DescriptorCount = 2000, // we can have up to 2000 textures for now - .flags = vk::DescriptorBindingFlagBits::eVariableDescriptorCount, - }; - - // For global data (generic data and textures) - Handle staticGlobalLayout = device->CreateDescriptorSetLayout({genericDataBinding, texturesBinding}); - - SHVkDescriptorSetLayout::Binding lightBinding - { - .Type = vk::DescriptorType::eStorageBufferDynamic, - .Stage = vk::ShaderStageFlagBits::eFragment, - .BindPoint = 0, - .DescriptorCount = 1, - }; - - // For Dynamic global data (lights) - Handle dynamicGlobalLayout = device->CreateDescriptorSetLayout({ lightBinding }); - - SHVkDescriptorSetLayout::Binding cameraDataBinding - { - .Type = vk::DescriptorType::eUniformBufferDynamic, - .Stage = vk::ShaderStageFlagBits::eVertex, - .BindPoint = 0, - .DescriptorCount = 1, - }; - - // For High frequency global data (camera) - Handle cameraDataGlobalLayout = device->CreateDescriptorSetLayout({ cameraDataBinding }); - - SHVkDescriptorSetLayout::Binding materialDataBinding - { - .Type = vk::DescriptorType::eStorageBufferDynamic, - .Stage = vk::ShaderStageFlagBits::eFragment, - .BindPoint = 0, - .DescriptorCount = 1, - }; - - // For High frequency global data (camera) - Handle materialDataPerInstanceLayout = device->CreateDescriptorSetLayout({ materialDataBinding }); - - globalDescSetLayouts.push_back(staticGlobalLayout); - globalDescSetLayouts.push_back(dynamicGlobalLayout); - globalDescSetLayouts.push_back(cameraDataGlobalLayout); - globalDescSetLayouts.push_back(materialDataPerInstanceLayout); - } - - void SHGraphicsSystem::ConfigureDefaultVertexInputState(void) noexcept - { - defaultVertexInputState.AddBinding(false, false, {SHVertexAttribute(SHAttribFormat::FLOAT_3D)}); // positions at binding 0 - defaultVertexInputState.AddBinding(false, false, {SHVertexAttribute(SHAttribFormat::FLOAT_2D)}); // UVs at binding 1 - //defaultVertexInputState.AddBinding(false, false, {SHVertexAttribute(SHAttribFormat::FLOAT_3D)}); // Normals at binding 2 - //defaultVertexInputState.AddBinding(false, false, {SHVertexAttribute(SHAttribFormat::FLOAT_3D)}); // Tangents at binding 3 - //defaultVertexInputState.AddBinding(true, true, {SHVertexAttribute(SHAttribFormat::MAT_4D)}); // Transform at binding 4 - 7 (4 slots) - } - void SHGraphicsSystem::BeginRoutine::Execute(double) noexcept { reinterpret_cast(system)->BeginRender(); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 2475550b..5b40c3b9 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -223,11 +223,12 @@ namespace SHADE // Not Owned Resources SHWindow* window; - // Global descriptor set layouts - std::vector> globalDescSetLayouts; + // global data (descriptor sets as well) + Handle globalData; + // Middle End Resources - ResourceManager resourceManager; - SHMeshLibrary meshLibrary; + ResourceManager resourceManager; + SHMeshLibrary meshLibrary; // Viewports Handle defaultViewport; // Whole screen std::vector> viewports; // Additional viewports @@ -243,21 +244,11 @@ namespace SHADE Handle worldCamera; Handle screenCamera; - // Default vertex input state (used by everything). - SHVertexInputState defaultVertexInputState; - // TODO: Temporary only until resource library from Xiao Qi is implemented SHShaderSourceLibrary shaderSourceLibrary; SHShaderModuleLibrary shaderModuleLibrary; // Temp Materials Handle defaultMaterial; - - - /*-----------------------------------------------------------------------------*/ - /* Private member functions */ - /*-----------------------------------------------------------------------------*/ - void ConfigureGlobalDescLayouts (void) noexcept; - void ConfigureDefaultVertexInputState (void) noexcept; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp index 636b8e68..e09a4945 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.cpp @@ -1,6 +1,7 @@ #include "SHpch.h" #include "SHPipelineLibrary.h" #include "Graphics/Devices/SHVkLogicalDevice.h" +#include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h" namespace SHADE { @@ -10,7 +11,7 @@ namespace SHADE SHPipelineLayoutParams params { .shaderModules = {vsFsPair.first, vsFsPair.second}, - .globalDescSetLayouts = *globalDescSetLayouts + .globalDescSetLayouts = globalData->GetDescSetLayouts() }; // Create the pipeline layout @@ -18,7 +19,7 @@ namespace SHADE // Create the pipeline and configure the default vertex input state auto newPipeline = logicalDevice->CreatePipeline(pipelineLayout, nullptr, renderpass, subpass, SH_PIPELINE_TYPE::GRAPHICS); - newPipeline->GetPipelineState().SetVertexInputState(*vertexInputState); + newPipeline->GetPipelineState().SetVertexInputState(globalData->GetDefaultViState()); // Actually construct the pipeline newPipeline->ConstructPipeline(); @@ -29,11 +30,10 @@ namespace SHADE return newPipeline; } - void SHPipelineLibrary::Init(Handle device, SHVertexInputState const* viState, std::vector> const* globalLayouts) noexcept + void SHPipelineLibrary::Init(Handle device, Handle inGlobalData) noexcept { logicalDevice = device; - vertexInputState = viState; - globalDescSetLayouts = globalLayouts; + globalData = inGlobalData; } Handle SHPipelineLibrary::GetDrawPipline(std::pair, Handle> const& vsFsPair) noexcept diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h index 8f41781a..a16976f7 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h @@ -10,7 +10,7 @@ namespace SHADE class SHVkDescriptorSetLayouts; class SHVkPipeline; class SHSubpass; - + class SHGraphicsGlobalData; // Pipeline library is a PURELY MIDDLE END SYSTEM. It is responsible for only creating pipelines from shaders and caching // them so that they don't need to be recreated again. @@ -25,14 +25,13 @@ namespace SHADE //! a map of pipelines that are hashed using a pair of shader module handles std::unordered_map, Handle>, Handle> pipelines; - //! Default vertex input state for pipeline creation - SHVertexInputState const* vertexInputState; + // Global data + Handle globalData; - std::vector> const* globalDescSetLayouts; public: - void Init (Handle device, SHVertexInputState const* viState, std::vector> const* globalLayouts) noexcept; + void Init (Handle device, Handle inGlobalData) noexcept; // Draw pipeline functions. used only when creating pipelines for drawing using a vertex and fragment shader Handle CreateDrawPipeline ( diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index d5fd1d4c..d91c474c 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -446,7 +446,7 @@ namespace SHADE */ /***************************************************************************/ - SHRenderGraphNode::SHRenderGraphNode(ResourceManager& rm, Handle const& logicalDevice, Handle const& swapchain, std::vector> attRes, std::vector> predecessors, std::unordered_map> const* resources, SHVertexInputState const* defaultViState, std::vector> const* globalLayouts) noexcept + SHRenderGraphNode::SHRenderGraphNode(ResourceManager& rm, Handle const& logicalDevice, Handle const& swapchain, std::vector> attRes, std::vector> predecessors, std::unordered_map> const* resources, Handle globalData) noexcept : logicalDeviceHdl{ logicalDevice } , renderpass{} , framebuffers{} @@ -461,7 +461,7 @@ namespace SHADE , ptrToResources{ resources } { // pipeline library initialization - pipelineLibrary.Init(logicalDeviceHdl, defaultViState, globalLayouts); + pipelineLibrary.Init(logicalDeviceHdl, globalData); attachmentDescriptions.resize(attResources.size()); @@ -969,12 +969,11 @@ namespace SHADE */ /***************************************************************************/ - void SHRenderGraph::Init(Handle const& logicalDevice, Handle const& swapchain, SHVertexInputState const* defaultViState, std::vector> const* globalLayouts) noexcept + void SHRenderGraph::Init(Handle const& logicalDevice, Handle const& swapchain, Handle inGlobalData) noexcept { logicalDeviceHdl = logicalDevice; swapchainHdl = swapchain; - vertexInputState = defaultViState; - globalDescSetLayouts = globalLayouts; + globalData = inGlobalData; } /***************************************************************************/ @@ -1052,7 +1051,7 @@ namespace SHADE } } - nodes.emplace_back(resourceManager.Create(resourceManager, logicalDeviceHdl, swapchainHdl, std::move(resources), std::move(predecessors), &graphResources, vertexInputState, globalDescSetLayouts)); + nodes.emplace_back(resourceManager.Create(resourceManager, logicalDeviceHdl, swapchainHdl, std::move(resources), std::move(predecessors), &graphResources, globalData)); nodeIndexing.emplace(nodeName, static_cast(nodes.size()) - 1u); return nodes.at(nodeIndexing[nodeName]); } diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index baf38c5a..bfeb8faf 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -6,10 +6,10 @@ #include "SH_API.h" #include "Graphics/MiddleEnd/Pipeline/SHPipelineLibrary.h" #include "Graphics/MiddleEnd/Batching/SHSuperBatch.h" +#include "../MiddleEnd/Batching/SHBatcher.h" #include #include -#include "../MiddleEnd/Batching/SHBatcher.h" namespace SHADE { @@ -22,6 +22,7 @@ namespace SHADE class SHVkCommandPool; class SHVkCommandBuffer; class SHRenderGraphNode; + class SHGraphicsGlobalData; // Used for attachment description creation for renderpass node enum class SH_ATT_DESC_TYPE @@ -218,7 +219,7 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ /*-----------------------------------------------------------------------*/ - SHRenderGraphNode (ResourceManager& rm, Handle const& logicalDevice, Handle const& swapchain, std::vector> attRes, std::vector> predecessors, std::unordered_map> const* resources, SHVertexInputState const* defaultViState, std::vector> const* globalLayouts) noexcept; + SHRenderGraphNode (ResourceManager& rm, Handle const& logicalDevice, Handle const& swapchain, std::vector> attRes, std::vector> predecessors, std::unordered_map> const* resources, Handle globalData) noexcept; SHRenderGraphNode(SHRenderGraphNode&& rhs) noexcept; SHRenderGraphNode& operator= (SHRenderGraphNode&& rhs) noexcept; @@ -277,11 +278,8 @@ namespace SHADE //! Command buffers for the render graph std::vector> commandBuffers; - //! Default vertex input state for pipeline creation - SHVertexInputState const* vertexInputState; - - //! Global descriptor set layouts to use for pipeline creation - std::vector> const* globalDescSetLayouts; + //! Handle to global data + Handle globalData; public: /*-----------------------------------------------------------------------*/ @@ -292,7 +290,7 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - void Init (Handle const& logicalDevice, Handle const& swapchain, SHVertexInputState const* defaultViState, std::vector> const* globalLayouts) 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;