From cb31628e663a0f0fb1ef7eb829d41f06740c1f4e Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Thu, 22 Sep 2022 19:38:43 +0800 Subject: [PATCH] Dummy pipeline layout ctor created Renderer now can update and bind descriptor set --- SHADE_Application/src/Scenes/SBTestScene.cpp | 18 +++++-- SHADE_Application/src/Scenes/SBTestScene.h | 2 +- .../src/Graphics/Buffers/SHVkBuffer.cpp | 49 ++++++----------- .../src/Graphics/Buffers/SHVkBuffer.h | 4 +- .../Graphics/Commands/SHVkCommandBuffer.cpp | 5 ++ .../src/Graphics/Commands/SHVkCommandBuffer.h | 1 + .../Descriptors/SHVkDescriptorSetGroup.cpp | 4 +- .../Descriptors/SHVkDescriptorSetGroup.h | 2 +- .../Graphics/Devices/SHVkLogicalDevice.cpp | 7 ++- .../src/Graphics/Devices/SHVkLogicalDevice.h | 3 +- .../GlobalData/SHGraphicsGlobalData.cpp | 9 ++++ .../GlobalData/SHGraphicsGlobalData.h | 6 +++ .../Graphics/MiddleEnd/Interface/SHCamera.cpp | 34 ++++++------ .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 7 +-- .../MiddleEnd/Interface/SHRenderer.cpp | 16 ++++-- .../Graphics/MiddleEnd/Interface/SHRenderer.h | 8 ++- .../Pipeline/SHPipelineLayoutParams.h | 9 ++++ .../src/Graphics/Pipeline/SHPipelineState.h | 2 +- .../Graphics/Pipeline/SHVkPipelineLayout.cpp | 50 +++++++++++++++--- .../Graphics/Pipeline/SHVkPipelineLayout.h | 3 +- TempShaderFolder/TestCubeVs.glsl | 3 +- TempShaderFolder/TestCubeVs.spv | Bin 1680 -> 1768 bytes 22 files changed, 161 insertions(+), 81 deletions(-) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index 873d0fed..aef39ddd 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -7,6 +7,7 @@ #include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Scene/SHSceneManager.h" #include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h" +#include "Math/Transform/SHTransformComponent.h" using namespace SHADE; @@ -28,6 +29,8 @@ namespace Sandbox } void SBTestScene::Init() { + SHComponentManager::CreateComponentSparseSet(); + SHADE::SHGraphicsSystem* graphicsSystem = static_cast(SHADE::SHSystemManager::GetSystem()); // Create temp meshes const auto CUBE_MESH = SHADE::SHPrimitiveGenerator::Cube(*graphicsSystem); @@ -37,16 +40,25 @@ namespace Sandbox auto matInst = graphicsSystem->AddMaterialInstance(); // Create entity and add mesh - auto entity = SHADE::SHEntityManager::CreateEntity(); - auto& renderable = *SHADE::SHComponentManager::GetComponent_s(entity); + testEntity = SHADE::SHEntityManager::CreateEntity(); + SHComponentManager::AddComponent(testEntity); + auto& renderable = *SHADE::SHComponentManager::GetComponent_s(testEntity); + auto& transform = *SHADE::SHComponentManager::GetComponent_s(testEntity); + renderable.Mesh = CUBE_MESH; renderable.SetMaterial(matInst); renderable.TransformMatrix.Translate(0.0f, 0.0f, 2.0f); + + transform.SetWorldPosition (SHVec3 (0.0f, 0.0f, 2.0f)); } void SBTestScene::Update(float dt) { - (void)dt; + static float rotation = 0.0f; + auto& transform = *SHADE::SHComponentManager::GetComponent_s(testEntity); + transform.SetWorldRotation (rotation, 0.0f, 0.0f); + + rotation += dt * 10.0f; } void SBTestScene::Render() diff --git a/SHADE_Application/src/Scenes/SBTestScene.h b/SHADE_Application/src/Scenes/SBTestScene.h index 6776c671..bb382477 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.h +++ b/SHADE_Application/src/Scenes/SBTestScene.h @@ -9,7 +9,7 @@ namespace Sandbox { private: EntityID camera; - + unsigned int testEntity; public: virtual void Load(); diff --git a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp index 4bfc1e4b..59916731 100644 --- a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp @@ -49,6 +49,11 @@ namespace SHADE Init(newSize, data, srcSize, bufferUsageFlags, allocCreateInfo.usage, allocCreateInfo.flags); } + void SHVkBuffer::FlushAllocation(uint32_t srcOffset, uint32_t dstOffset) noexcept + { + vmaFlushAllocation(vmaAllocator, alloc, srcOffset, dstOffset); + } + vk::Buffer SHVkBuffer::GetVkBuffer(void) const noexcept { return vkBuffer; @@ -72,8 +77,7 @@ namespace SHADE /***************************************************************************/ void SHVkBuffer::Map(void) noexcept { - if (!boundToCoherent) - vmaMapMemory(vmaAllocator, alloc, &mappedPtr); + vmaMapMemory(vmaAllocator, alloc, &mappedPtr); } /***************************************************************************/ @@ -90,11 +94,8 @@ namespace SHADE /***************************************************************************/ void SHVkBuffer::Unmap(void) noexcept { - if (!boundToCoherent) - { - vmaUnmapMemory(vmaAllocator, alloc); - mappedPtr = nullptr; - } + vmaUnmapMemory(vmaAllocator, alloc); + mappedPtr = nullptr; } /***************************************************************************/ @@ -132,9 +133,7 @@ namespace SHADE Otherwise, only the copying is carried out. In the instance where memory is non-coherent but HOST_VISIBLE, we want to - write to data and then unmap and flush it immediately. If you want to write - to memory in random-access fashion, consider, mapping, writing a few - things, unmapping then flushing. + write to data and then unmap and flush it immediately. \param vmaAllocator The VMA allocator object. @@ -155,18 +154,11 @@ namespace SHADE /***************************************************************************/ void SHVkBuffer::MapWriteUnmap(void* data, uint32_t sizeToWrite, uint32_t srcOffset, uint32_t dstOffset) noexcept { - if (!boundToCoherent) - { - // map from host visible memory to pointer, do a DMA, and then unmap - Map(); - WriteToMemory(data, sizeToWrite, srcOffset, dstOffset); - Unmap(); - } - else - { - if (mappedPtr) - std::memcpy(static_cast(mappedPtr) + dstOffset, static_cast(data) + srcOffset, sizeToWrite); - } + // map from host visible memory to pointer, do a DMA, and then unmap + Map(); + WriteToMemory(data, sizeToWrite, srcOffset, dstOffset); + Unmap(); + FlushAllocation(srcOffset, dstOffset); } /***************************************************************************/ @@ -279,7 +271,6 @@ namespace SHADE , mappedPtr{ nullptr } , alloc {nullptr} , randomAccessOptimized{false} - , boundToCoherent {false} , vmaAllocator{allocator} {} @@ -304,7 +295,6 @@ namespace SHADE , mappedPtr{ std::move(rhs.mappedPtr) } , alloc{ std::move (rhs.alloc) } , randomAccessOptimized{ rhs.randomAccessOptimized } - , boundToCoherent{ rhs.boundToCoherent} , vmaAllocator{ rhs.vmaAllocator } , bufferUsageFlags {rhs.bufferUsageFlags} , bufferCreateInfo { rhs.bufferCreateInfo } @@ -325,7 +315,6 @@ namespace SHADE mappedPtr = std::move(rhs.mappedPtr); alloc = std::move(rhs.alloc); randomAccessOptimized = rhs.randomAccessOptimized; - boundToCoherent = rhs.boundToCoherent; vmaAllocator = std::move (rhs.vmaAllocator); rhs.vkBuffer = VK_NULL_HANDLE; bufferCreateInfo = rhs.bufferCreateInfo; @@ -430,16 +419,12 @@ namespace SHADE // mainly host visible. Can be cached (need to flush/invalidate), uncached (always coherent) and coherent (virtual). if(memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { - // If memory is marked to be coherent between CPU and GPU (no need flush/invalidate) (TODO: Verify if VMA_ALLOCATION_CREATE_MAPPED_BIT is used when VMA_MEMORY_USAGE_AUTO is set) - // TODO: also verify that coherent bit = pointer is already mapped - if (memPropFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) - { - boundToCoherent = true; - mappedPtr = allocInfo.pMappedData; - } + if (allocFlags | VMA_ALLOCATION_CREATE_MAPPED_BIT) + mappedPtr = allocInfo.pMappedData; else mappedPtr = nullptr; + if (data) MapWriteUnmap(data, srcSize, 0, 0); } diff --git a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h index 49f22e37..648a07e8 100644 --- a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h +++ b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h @@ -41,9 +41,6 @@ namespace SHADE //! If initialized with vma random access flag, this is true bool randomAccessOptimized; - //! Whether or not this buffer is bound to coherent memory - bool boundToCoherent; - //! buffer usage info flags vk::BufferUsageFlags bufferUsageFlags; @@ -100,6 +97,7 @@ namespace SHADE void TransferToDeviceResource(Handle const& cmdBufferHdl) noexcept; void ResizeNoCopy (uint32_t newSize); void ResizeReplace (uint32_t newSize, void* data, uint32_t srcSize); + void FlushAllocation (uint32_t srcOffset, uint32_t dstOffset) noexcept; /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp index cddb6cdb..83095371 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.cpp @@ -491,6 +491,11 @@ namespace SHADE // //vkCommandBuffer.pipelineBarrier() //} + void SHVkCommandBuffer::ForceSetPipelineLayout(Handle pipelineLayout) noexcept + { + boundPipelineLayoutHdl = pipelineLayout; + } + /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h index f780638a..c18527b3 100644 --- a/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h +++ b/SHADE_Engine/src/Graphics/Commands/SHVkCommandBuffer.h @@ -141,6 +141,7 @@ namespace SHADE { memcpy (static_cast(pushConstantData) + boundPipelineLayoutHdl->GetPushConstantInterface().GetOffset(variableName), &data, sizeof (T)); }; + void ForceSetPipelineLayout (Handle pipelineLayout) noexcept; void SubmitPushConstants (void) const noexcept; diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp index f579d377..c1732535 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.cpp @@ -176,7 +176,7 @@ namespace SHADE } } - void SHVkDescriptorSetGroup::ModifyWriteDescBuffer(uint32_t set, uint32_t binding, std::span> const& buffers) noexcept + void SHVkDescriptorSetGroup::ModifyWriteDescBuffer(uint32_t set, uint32_t binding, std::span> const& buffers, uint32_t offset, uint32_t range) noexcept { // Find the target writeDescSet BindingAndSetHash writeHash = binding; @@ -193,6 +193,8 @@ namespace SHADE // write sampler and image view auto& buffer = buffers[i]; writeInfo.descBufferInfos[i].buffer = buffer->GetVkBuffer(); + writeInfo.descBufferInfos[i].offset = offset; + writeInfo.descBufferInfos[i].range = range; } } diff --git a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h index bc87ff1d..d92d55e9 100644 --- a/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h +++ b/SHADE_Engine/src/Graphics/Descriptors/SHVkDescriptorSetGroup.h @@ -64,7 +64,7 @@ namespace SHADE 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 ModifyWriteDescBuffer (uint32_t set, uint32_t binding, std::span> const& buffers, uint32_t offset, uint32_t range) noexcept; /*-----------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp index 64508a2b..65b0d9ca 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp @@ -467,7 +467,12 @@ namespace SHADE */ /***************************************************************************/ - Handle SHVkLogicalDevice::CreatePipelineLayout(SHPipelineLayoutParams& pipelineLayoutParams) noexcept + Handle SHVkLogicalDevice::CreatePipelineLayout(SHPipelineLayoutParams const& pipelineLayoutParams) noexcept + { + return SHVkInstance::GetResourceManager().Create (GetHandle(), pipelineLayoutParams); + } + + Handle SHVkLogicalDevice::CreatePipelineLayoutDummy(SHPipelineLayoutParamsDummy const& pipelineLayoutParams) noexcept { return SHVkInstance::GetResourceManager().Create (GetHandle(), pipelineLayoutParams); } diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h index 365e68c9..3a27e4b1 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h @@ -187,7 +187,8 @@ namespace SHADE Handle CreateDescriptorSetGroup(Handle pool, std::vector> const& layouts, std::vector const& variableDescCounts) noexcept; - Handle CreatePipelineLayout (SHPipelineLayoutParams& pipelineLayoutParams) noexcept; + Handle CreatePipelineLayout(SHPipelineLayoutParams const& pipelineLayoutParams) noexcept; + Handle CreatePipelineLayoutDummy(SHPipelineLayoutParamsDummy const& pipelineLayoutParams) noexcept; Handle CreateFence (void) const noexcept; Handle CreateSemaphore (void) const noexcept; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp index d9ffa6b5..ee6d0e8c 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp @@ -2,6 +2,7 @@ #include "SHGraphicsGlobalData.h" #include "Graphics/Devices/SHVkLogicalDevice.h" #include "Graphics/Pipeline/SHPipelineState.h" +#include "Graphics/Pipeline/SHVkPipelineLayout.h" #include "Graphics/Descriptors/SHVkDescriptorSetLayout.h" namespace SHADE @@ -66,6 +67,9 @@ namespace SHADE globalDescSetLayouts.push_back(dynamicGlobalLayout); globalDescSetLayouts.push_back(cameraDataGlobalLayout); globalDescSetLayouts.push_back(materialDataPerInstanceLayout); + + + dummyPipelineLayout = logicalDevice->CreatePipelineLayoutDummy(SHPipelineLayoutParamsDummy{globalDescSetLayouts}); } void SHGraphicsGlobalData::InitDefaultVertexInputState(void) noexcept @@ -94,4 +98,9 @@ namespace SHADE return defaultVertexInputState; } + Handle SHGraphicsGlobalData::GetDummyPipelineLayout(void) const noexcept + { + return dummyPipelineLayout; + } + } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h index fb595250..9333d0ab 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h @@ -9,6 +9,7 @@ namespace SHADE class SHVkLogicalDevice; class SHVkDescriptorSetLayout; class SHVkDescriptorSetGroup; + class SHVkPipelineLayout; class SH_API SHGraphicsGlobalData { @@ -22,6 +23,10 @@ namespace SHADE //! Default vertex input state (used by everything). SHVertexInputState defaultVertexInputState; + //! Since we want to bind global data but can't do so without a pipeline layout, + //! we create a dummy pipeline layout to use it for binding. + Handle dummyPipelineLayout; + void InitDescSetLayouts (Handle logicalDevice) noexcept; void InitDefaultVertexInputState(void) noexcept; public: @@ -35,5 +40,6 @@ namespace SHADE /*-----------------------------------------------------------------------*/ std::vector> const& GetDescSetLayouts (void) const noexcept; SHVertexInputState const& GetDefaultViState (void) const noexcept; + Handle GetDummyPipelineLayout (void) const noexcept; }; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.cpp index 7e7412b9..992aff05 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHCamera.cpp @@ -22,24 +22,24 @@ namespace SHADE void SHCamera::SetLookAt(const SHVec3& pos, const SHVec3& target, const SHVec3& up) { SHVec3 view = target - pos; view = SHVec3::Normalise(view); - SHVec3 right = SHVec3::Cross(view, up); right = SHVec3::Normalise(right); - const SHVec3 UP = SHVec3::Cross(right, view); + SHVec3 right = SHVec3::Cross(view, up); right = SHVec3::Normalise(right); + const SHVec3 UP = SHVec3::Cross(right, view); - viewMatrix = SHMatrix::Identity; - viewMatrix(0, 0) = right[0]; - viewMatrix(1, 0) = right[1]; - viewMatrix(2, 0) = right[2]; - viewMatrix(0, 1) = UP[0]; - viewMatrix(1, 1) = UP[1]; - viewMatrix(2, 1) = UP[2]; - viewMatrix(0, 2) = -view[0]; - viewMatrix(1, 2) = -view[1]; - viewMatrix(2, 2) = -view[2]; - viewMatrix(3, 0) = -right.Dot(pos); - viewMatrix(3, 1) = -UP.Dot(pos); - viewMatrix(3, 2) = view.Dot(pos); - - isDirty = true; + viewMatrix = SHMatrix::Identity; + viewMatrix(0, 0) = UP[0]; + viewMatrix(1, 0) = UP[1]; + viewMatrix(2, 0) = UP[2]; + viewMatrix(0, 1) = right[0]; + viewMatrix(1, 1) = right[1]; + viewMatrix(2, 1) = right[2]; + viewMatrix(0, 2) = view[0]; + viewMatrix(1, 2) = view[1]; + viewMatrix(2, 2) = view[2]; + viewMatrix(3, 0) = -UP.Dot(pos); + viewMatrix(3, 1) = -right.Dot(pos); + viewMatrix(3, 2) = -view.Dot(pos); + + isDirty = true; } /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 1026d01b..cee00c9b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -114,7 +114,7 @@ namespace SHADE screenCamera->SetLookAt(SHVec3(0.0f, 0.0f, -1.0f), SHVec3(0.0f, 0.0f, 1.0f), SHVec3(0.0f, 1.0f, 0.0f)); screenCamera->SetOrthographic(static_cast(windowDims.first), static_cast(windowDims.second), 0.01f, 100.0f); worldCamera = resourceManager.Create(); - worldCamera->SetLookAt(SHVec3(0.0f, 0.0f, -1.0f), SHVec3(0.0f, 0.0f, 1.0f), SHVec3(0.0f, 1.0f, 0.0f)); + worldCamera->SetLookAt(SHVec3(1.0f, 0.0f, -1.0f), SHVec3(0.0f, 0.0f, 2.0f), SHVec3(0.0f, 1.0f, 0.0f)); worldCamera->SetPerspective(90.0f, static_cast(windowDims.first), static_cast(windowDims.second), 0.0f, 100.0f); // Create Default Viewport @@ -221,7 +221,6 @@ namespace SHADE renderContext.ResetFence(); - // For every viewport for (int vpIndex = 0; vpIndex < static_cast(viewports.size()); ++vpIndex) { @@ -239,6 +238,8 @@ namespace SHADE // Begin recording the command buffer currentCmdBuffer->BeginRecording(); + currentCmdBuffer->ForceSetPipelineLayout(globalData->GetDummyPipelineLayout()); + // Bind all the buffers required for meshes for (auto& [buffer, bindingPoint] : MESH_DATA) { @@ -249,7 +250,7 @@ namespace SHADE } // bind camera data - renderers[renIndex]->BindDescriptorSet(currentCmdBuffer, frameIndex); + renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); // Draw first renderers[renIndex]->Draw(frameIndex); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp index fd702968..c7e2a86d 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp @@ -37,15 +37,14 @@ namespace SHADE 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); + cameraBuffer = logicalDevice->CreateBuffer(cameraDataAlignedSize * numFrames, nullptr, cameraDataAlignedSize * numFrames, vk::BufferUsageFlagBits::eUniformBuffer, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_MAPPED_BIT | 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->ModifyWriteDescBuffer(SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, SHGraphicsConstants::DescriptorSetBindings::CAMERA_DATA, std::span>{ cameraBufferArray.data(), cameraBufferArray.size()}, 0, sizeof (SHShaderCameraData)); cameraDescriptorSet->UpdateDescriptorSetBuffer(SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, SHGraphicsConstants::DescriptorSetBindings::CAMERA_DATA); } @@ -66,13 +65,20 @@ namespace SHADE renderGraph->Execute(frameIndex, commandBuffers[frameIndex]); } - void SHRenderer::BindDescriptorSet(Handle cmdBuffer, uint32_t frameIndex) noexcept + void SHRenderer::UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex) noexcept { - std::array dynamicOffsets{ frameIndex * cameraDataAlignedSize }; + cpuCameraData.viewProjectionMatrix = camera->GetViewProjectionMatrix(); + cameraBuffer->WriteToMemory(&cpuCameraData, sizeof(SHShaderCameraData), 0, cameraDataAlignedSize * frameIndex); + + std::array dynamicOffsets{ frameIndex * cameraDataAlignedSize }; cmdBuffer->BindDescriptorSet(cameraDescriptorSet, vk::PipelineBindPoint::eGraphics, SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, std::span{ dynamicOffsets.data(), 1 }); } + void SHRenderer::UpdateCameraDataToBuffer(void) noexcept + { + } + Handle SHRenderer::GetRenderGraph(void) const noexcept { return renderGraph; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h index bbb7773b..255ab289 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h @@ -75,7 +75,8 @@ namespace SHADE /* Drawing Functions */ /*-----------------------------------------------------------------------------*/ void Draw(uint32_t frameIndex) noexcept; - void BindDescriptorSet (Handle cmdBuffer, uint32_t frameIndex) noexcept; + void UpdateDataAndBind (Handle cmdBuffer, uint32_t frameIndex) noexcept; + void UpdateCameraDataToBuffer (void) noexcept; /*-----------------------------------------------------------------------------*/ /* Setters and Getters */ @@ -95,7 +96,10 @@ namespace SHADE Handle renderGraph; Handle cameraDescriptorSet; Handle cameraBuffer; - std::vector cpuCameraData; + + // we really only need 1 copy even though we need %swapchainImages copies for + // GPU. + SHShaderCameraData cpuCameraData; //! Command buffers for the render graph std::vector> commandBuffers; diff --git a/SHADE_Engine/src/Graphics/Pipeline/SHPipelineLayoutParams.h b/SHADE_Engine/src/Graphics/Pipeline/SHPipelineLayoutParams.h index 493bd114..093e03d4 100644 --- a/SHADE_Engine/src/Graphics/Pipeline/SHPipelineLayoutParams.h +++ b/SHADE_Engine/src/Graphics/Pipeline/SHPipelineLayoutParams.h @@ -25,6 +25,15 @@ namespace SHADE //! want to use it for allocating descriptor sets. std::vector> const& globalDescSetLayouts = {}; }; + + struct SHPipelineLayoutParamsDummy + { + /*-----------------------------------------------------------------------*/ + /* MEMBER VARIABLES */ + /*-----------------------------------------------------------------------*/ + + std::vector> const& globalDescSetLayouts = {}; + }; } #endif diff --git a/SHADE_Engine/src/Graphics/Pipeline/SHPipelineState.h b/SHADE_Engine/src/Graphics/Pipeline/SHPipelineState.h index a43035f5..2769d6cc 100644 --- a/SHADE_Engine/src/Graphics/Pipeline/SHPipelineState.h +++ b/SHADE_Engine/src/Graphics/Pipeline/SHPipelineState.h @@ -64,7 +64,7 @@ namespace SHADE vk::CullModeFlags cull_mode{ vk::CullModeFlagBits::eBack }; //! CW or CCW - vk::FrontFace frontFacingOrientation{ vk::FrontFace::eClockwise }; + vk::FrontFace frontFacingOrientation{ vk::FrontFace::eCounterClockwise }; bool depthBias{ VK_FALSE }; }; diff --git a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp index fc40f394..25be112e 100644 --- a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp +++ b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp @@ -254,12 +254,6 @@ namespace SHADE // Clear pc ranges vkPcRanges.clear(); - // Kill all descriptor set layouts - for (auto& layout : descriptorSetLayoutsGlobal) - SHVkInstance::GetResourceManager().Free(layout); - - descriptorSetLayoutsGlobal.clear(); - for (auto& layout : descriptorSetLayoutsAllocate) SHVkInstance::GetResourceManager().Free(layout); @@ -285,9 +279,9 @@ namespace SHADE */ /***************************************************************************/ - SHVkPipelineLayout::SHVkPipelineLayout(Handle const& inLogicalDeviceHdl, SHPipelineLayoutParams& pipelineLayoutParams) noexcept + SHVkPipelineLayout::SHVkPipelineLayout(Handle const& inLogicalDeviceHdl, SHPipelineLayoutParams const& pipelineLayoutParams) noexcept : vkPipelineLayout {VK_NULL_HANDLE} - , shaderModules{std::move (pipelineLayoutParams.shaderModules)} + , shaderModules{pipelineLayoutParams.shaderModules} , logicalDeviceHdl {inLogicalDeviceHdl} , pushConstantInterface{} , vkPcRanges{} @@ -308,6 +302,46 @@ namespace SHADE RecreateIfNeeded (); } + + SHVkPipelineLayout::SHVkPipelineLayout(Handle const& inLogicalDeviceHdl, SHPipelineLayoutParamsDummy const& pipelineLayoutParams) noexcept + : vkPipelineLayout{ VK_NULL_HANDLE } + , shaderModules{ } + , logicalDeviceHdl{ inLogicalDeviceHdl } + , pushConstantInterface{} + , vkPcRanges{} + , descriptorSetLayoutsGlobal{} + , descriptorSetLayoutsAllocate{} + , vkDescriptorSetLayoutsAllocate{} + , vkDescriptorSetLayoutsPipeline{} + + { + vkDescriptorSetLayoutsPipeline.resize(pipelineLayoutParams.globalDescSetLayouts.size()); + for (uint32_t i = 0; auto& layout : vkDescriptorSetLayoutsPipeline) + { + layout = pipelineLayoutParams.globalDescSetLayouts[i]->GetVkHandle(); + ++i; + } + + vk::PipelineLayoutCreateInfo plCreateInfo{}; + + // Set push constant data to pipeline layout + plCreateInfo.pushConstantRangeCount = 0; + plCreateInfo.pPushConstantRanges = nullptr; + + // To initialize the descriptor set layouts for the pipeline layout. + plCreateInfo.setLayoutCount = static_cast(vkDescriptorSetLayoutsPipeline.size()); + plCreateInfo.pSetLayouts = vkDescriptorSetLayoutsPipeline.data(); + + if (auto const RESULT = logicalDeviceHdl->GetVkLogicalDevice().createPipelineLayout(&plCreateInfo, nullptr, &vkPipelineLayout); RESULT != vk::Result::eSuccess) + { + SHVulkanDebugUtil::ReportVkError(RESULT, "Failed to create Pipeline Layout. "); + return; + } + else + SHVulkanDebugUtil::ReportVkSuccess("Successfully created Pipeline Layout. "); + + } + /***************************************************************************/ /*! diff --git a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.h b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.h index bce827a7..e43ceb73 100644 --- a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.h +++ b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.h @@ -57,7 +57,8 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ /*-----------------------------------------------------------------------*/ - SHVkPipelineLayout (Handle const& inLogicalDeviceHdl, SHPipelineLayoutParams& pipelineLayoutParams) noexcept; + SHVkPipelineLayout(Handle const& inLogicalDeviceHdl, SHPipelineLayoutParams const& pipelineLayoutParams) noexcept; + SHVkPipelineLayout(Handle const& inLogicalDeviceHdl, SHPipelineLayoutParamsDummy const& pipelineLayoutParams) noexcept; SHVkPipelineLayout (SHVkPipelineLayout&& rhs) noexcept; ~SHVkPipelineLayout (void) noexcept; SHVkPipelineLayout& operator= (SHVkPipelineLayout&& rhs) noexcept; diff --git a/TempShaderFolder/TestCubeVs.glsl b/TempShaderFolder/TestCubeVs.glsl index 08c465a0..1e3a11d7 100644 --- a/TempShaderFolder/TestCubeVs.glsl +++ b/TempShaderFolder/TestCubeVs.glsl @@ -56,6 +56,7 @@ void main() //Out.uv = aUV; // render NDC first - gl_Position = vec4(aVertexPos, 1.0); + //gl_Position = vec4(aVertexPos, 1.0f); + gl_Position = cameraData.vpMat * vec4 (aVertexPos, 1.0f); Out.vertColor = vec4 (aVertexPos, 1.0f); } \ No newline at end of file diff --git a/TempShaderFolder/TestCubeVs.spv b/TempShaderFolder/TestCubeVs.spv index be24aa3a944eff08f83a27869e46ef93dcc93eae..f0b31ea23f835273164024fc22e42f8e3c4cc918 100644 GIT binary patch literal 1768 zcmZ9LYflqF6ovA)6cQnt1#>m=i~9Dg9|GoNw`AM0r!p4p%3jC{`b}#7qs2w-+Gw^LtyZIX*zODl z?P2^Q@Y;bNdhMp&^2?c)S}q13ZLFE~OPV{m^>FmqrpMD+&=t&MughQqHC z8*`)x{bsn?ahwB=*>Us|j=nh#AC7x$D1wiUxa(6*m~kW(963t~PBmaO*Di5-;0Z@w z){(cN9Q1Va65bGI5U0PD1f`h1Vcsen`^;;u z%E-$;-~}0DO_=_Ym%1wY(o@%C@?RI`Jn-ZP(`z`F?}mKxqhHQqt!GM$e`xGVtCUZ^rbH=GI~e9-b$JFDCNQQR(m)Fo0uO+Y5Fpf|^dXR_|oJT=)`F;hZ}nK#o;x7x?;<~Z#)pTBqp zW66{p5zew%aCTY$0~^LTi<0Y-JCYsAuH=DaPg2rvO8U1pmsPdZ?zTFeR{KM@HyU-v z>5nMrMqwOu`(YCF((pWhH(TI^aW9(mi-d|8#rhTXJ+B&PQTHfFPJ<*3zRMMw;n>86 z<1h_}vCtXE=R6J%T_^ZfSFOP-q+d+`n1Do2cJ{W7{ha!7gS-_dg`RJo1F}0e~y~6%O zVa%K5wa3^ChV~e<1Y>RBFHHh!! zKDDUt&)$&FIuysByW;%+bAWU05~dHxVf4*DUza&;U-IknS*v2ft7{K^VXw=Ymaa)Q z)B}0(>2pp0v(hyQYcKb&%I4f#^5>-CgH>fQL!W~fQWI}MnwrF4=RR*0j5CDIwxo%Hkqe*u2BY>p zY4R~I`1hr$0}da5C&zp*yO(_41A8{be<)3DW=y_E(!{{okI#DntIFqo;S-1dM4Dc} z;p21fU@!Hy(Bx)uhCf1hQ*VSauT=@N`Ye=lt>u{11-71J^x%0LIY#~?bwp2_686I< z&dlE{FY~@7;f}e-x9S8RzVCzYqu9q?`Cu$xvnCs?*hf9bihXPgV}DITyx2!WKKJSS Mz=!`=Szk&10e+KzwEzGB