Descriptor set fix and camera descriptor set

Fixed SHVkDescriptorSetGroup bug with vkLayouts inside the ctor
Added descriptor set to renderer for camera
Logical Device now stores a buffer alignment variable for UBOs
This commit is contained in:
Brandon Mak 2022-09-21 15:41:30 +08:00
parent c194765889
commit 8d2b6aec46
13 changed files with 93 additions and 35 deletions

View File

@ -8,6 +8,7 @@
#include "Graphics/Framebuffer/SHVkFramebuffer.h"
#include "Graphics/Pipeline/SHVkPipeline.h"
#include "Graphics/Buffers/SHVkBuffer.h"
#include "Graphics/Descriptors/SHVkDescriptorSetGroup.h"
namespace SHADE
@ -353,6 +354,11 @@ namespace SHADE
}
}
void SHVkCommandBuffer::BindDescriptorSet(Handle<SHVkDescriptorSetGroup> descSetGroup, vk::PipelineBindPoint bindPoint, uint32_t firstSet, std::span<uint32_t> dynamicOffsets)
{
vkCommandBuffer.bindDescriptorSets (bindPoint, boundPipelineLayoutHdl->GetVkPipelineLayout(), firstSet, descSetGroup->GetVkHandle(), dynamicOffsets);
}
/***************************************************************************/
/*!

View File

@ -15,6 +15,7 @@ namespace SHADE
class SHVkFramebuffer;
class SHVkPipeline;
class SHVkBuffer;
class SHVkDescriptorSetGroup;
enum class SH_CMD_BUFFER_TYPE
{
@ -110,6 +111,7 @@ namespace SHADE
void BindPipeline (Handle<SHVkPipeline> const& pipelineHdl) noexcept;
void BindVertexBuffer (uint32_t bindingPoint, Handle<SHVkBuffer> const& buffer, vk::DeviceSize offset) noexcept;
void BindIndexBuffer (Handle<SHVkBuffer> const& buffer, uint32_t startingIndex) const noexcept;
void BindDescriptorSet (Handle<SHVkDescriptorSetGroup> descSetGroup, vk::PipelineBindPoint bindPoint, uint32_t firstSet, std::span<uint32_t> dynamicOffsets);
// Draw Commands
void DrawArrays (uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) const noexcept;

View File

@ -46,12 +46,15 @@ namespace SHADE
//for (auto& layout : layouts)
for (uint32_t i = 0; i < layouts.size(); ++i)
{
vkLayouts.push_back(layouts[i]->GetVkHandle());
vkLayouts[i] = layouts[i]->GetVkHandle();
}
// Check for variable descriptor count
if (variableDescCounts.size() != layouts.size())
{
SHLOG_ERROR("Number of variable descriptor counts does not match number of layouts. If a layout does not use variable counts, pass in 0. ");
return;
}
// Prepare variable descriptor counts
vk::DescriptorSetVariableDescriptorCountAllocateInfo variableAllocInfo{};

View File

@ -56,10 +56,10 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Descriptor set writing */
/*-----------------------------------------------------------------------------*/
void ModifyWriteDescImage (uint32_t set, uint32_t binding, std::vector<std::pair<vk::ImageView, vk::Sampler>> const& imageViewsAndSamplers) noexcept;
void UpdateDescriptorSet (void) noexcept;
void ModifyWriteDescImage(uint32_t set, uint32_t binding, std::vector<std::pair<vk::ImageView, vk::Sampler>> const& imageViewsAndSamplers) noexcept;
void UpdateDescriptorSet(void) noexcept;
void UpdateSingleDescriptorSetImages (uint32_t set, uint32_t binding) noexcept;
void UpdateSingleDescriptorSetImages(uint32_t set, uint32_t binding) noexcept;
/*-----------------------------------------------------------------------------*/
/* Getter Functions */
@ -70,6 +70,7 @@ namespace SHADE
/// <returns>Handle to the Vulkan Descriptor Set.</returns>
[[nodiscard]]
inline const std::vector<vk::DescriptorSet>& GetVkHandle() { return descSets; }
inline const uint32_t GetNumDescriptorSets(void) const noexcept { return static_cast<uint32_t>(descSets.size()); }
private:
/*-----------------------------------------------------------------------------*/
/* Data Members */

View File

@ -208,21 +208,8 @@ namespace SHADE
InitializeVMA();
// TODO: Create pipeline caches
// TODO: Create Descriptor pools
//auto poolSizes = std::array
//{
// SHDescriptorPoolSize {SHDescriptorType::COMBINED_SAMPLER, 1000} // hard coded descriptor count
//};
//SHDescriptorPoolParams poolParams
//{
// .poolSizes = poolSizes,
// .maxDescriptorSets = 1000,
//};
//descriptorPool.Initialize(*this, poolParams);
//deviceStorage.Init(*this, queueFamilyIndices.indices[static_cast<uint32_t>(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS)].value());
uboBufferMemoryAlignment = parentPhysicalDeviceHdl->GetDeviceProperties().limits.minUniformBufferOffsetAlignment;
ssboBufferMemoryAlignment = parentPhysicalDeviceHdl->GetDeviceProperties().limits.minStorageBufferOffsetAlignment;
}
SHVkLogicalDevice::SHVkLogicalDevice(SHVkLogicalDevice&& rhs) noexcept
@ -299,6 +286,17 @@ namespace SHADE
return VkMemoryPropertyFlagBits::VK_MEMORY_PROPERTY_FLAG_BITS_MAX_ENUM;
}
uint32_t SHVkLogicalDevice::PadUBOSize(uint32_t originalSize) const noexcept
{
uint32_t alignedSize = originalSize;
//uint32_t minBuffer
if (uboBufferMemoryAlignment > 0)
{
alignedSize = (alignedSize + uboBufferMemoryAlignment - 1) & ~(uboBufferMemoryAlignment - 1);
}
return alignedSize;
}
/***************************************************************************/
/*!
@ -672,6 +670,11 @@ namespace SHADE
return static_cast<SHQueueFamilyIndex>(SH_Q_FAM::INVALID);
}
Handle<SHVkPhysicalDevice> SHVkLogicalDevice::GetParentPhysicalDevice(void) const noexcept
{
return parentPhysicalDeviceHdl;
}
//SHDescriptorPool const& SHLogicalDevice::GetDescriptorPool(void) const noexcept
//{
// return descriptorPool;

View File

@ -69,7 +69,11 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
/* PRIVATE MEMBER VARIABLES */
/*-----------------------------------------------------------------------*/
//vk::DeviceSize bufferMemoryAlignment{ 64 };
//! UBO alignment
vk::DeviceSize uboBufferMemoryAlignment;
//! SSBO alignment
vk::DeviceSize ssboBufferMemoryAlignment;
//! Vulkan handle
vk::Device vkLogicalDevice;
@ -113,9 +117,12 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
/* PUBLIC MEMBER VARIABLES */
/*-----------------------------------------------------------------------*/
// Miscellaneous functions
void WaitIdle (void) noexcept;
uint32_t FindMemoryType (uint32_t typeFilter, vk::MemoryPropertyFlags properties);
uint32_t PadUBOSize (uint32_t originalSize) const noexcept;
// creation functions
Handle<SHVkSurface> CreateSurface (HWND const& windowHandle) const noexcept;
Handle<SHVkSwapchain> CreateSwapchain (
Handle<SHVkSurface> const& surfaceHdl,
@ -190,10 +197,12 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
/* SETTERS AND GETTERS */
/*-----------------------------------------------------------------------*/
vk::Device const& GetVkLogicalDevice (void) const noexcept;
Handle<SHVkQueue> const& GetQueue (SH_Q_FAM queueFamArrayIndex, uint8_t queueIndex) const;
VmaAllocator const& GetVMAAllocator (void) noexcept;
SHQueueFamilyIndex GetQueueFamilyIndex (SH_Q_FAM family) const noexcept;
vk::Device const& GetVkLogicalDevice (void) const noexcept;
Handle<SHVkQueue> const& GetQueue (SH_Q_FAM queueFamArrayIndex, uint8_t queueIndex) const;
VmaAllocator const& GetVMAAllocator (void) noexcept;
SHQueueFamilyIndex GetQueueFamilyIndex (SH_Q_FAM family) const noexcept;
Handle<SHVkPhysicalDevice> GetParentPhysicalDevice (void) const noexcept;
//vk::DeviceSize GetBufferAlignment (void) const noexcept;
//SHDescriptorPool const& GetDescriptorPool(void) const noexcept;

View File

@ -27,6 +27,7 @@ namespace SHADE
queueFamilyProperties = vkPhysicalDevice.getQueueFamilyProperties();
deviceFeatures = vkPhysicalDevice.getFeatures2();
}
SHVkPhysicalDevice::SHVkPhysicalDevice(SHVkPhysicalDevice&& rhs) noexcept

View File

@ -167,7 +167,7 @@ namespace SHADE
debugWorldRenderer->SetCamera(worldCamera);*/
// Add world renderer to default viewport
worldRenderer = defaultViewport->AddRenderer(resourceManager, worldRenderGraph);
worldRenderer = defaultViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), descPool, globalData->GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph);
worldRenderer->SetCamera(worldCamera);

View File

@ -18,16 +18,23 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/Buffers/SHVkBuffer.h"
#include "Graphics/Framebuffer/SHVkFramebuffer.h"
#include "SHMaterial.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsConstants.h"
#include "Graphics/Descriptors/SHVkDescriptorSetGroup.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Constructor/Destructors */
/*-----------------------------------------------------------------------------------*/
SHRenderer::SHRenderer(Handle<SHViewport> viewport, Handle<SHRenderGraph> renderGraph)
SHRenderer::SHRenderer(Handle<SHVkLogicalDevice> logicalDevice, uint32_t numFrames, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHViewport> viewport, Handle<SHRenderGraph> renderGraph)
: viewport { viewport }
, renderGraph { renderGraph }
{}
{
cameraDescriptorSet = logicalDevice->CreateDescriptorSetGroup(descriptorPool, { cameraDescLayout }, { 1 });
cpuCameraData.resize(numFrames);
cameraDataAlignedSize = logicalDevice->PadUBOSize(sizeof(SHShaderCameraData));
}
/*-----------------------------------------------------------------------------------*/
/* Camera Registration */
@ -45,6 +52,13 @@ namespace SHADE
renderGraph->Execute(frameIndex, graphScopeBuffers);
}
void SHRenderer::BindDescriptorSet(Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept
{
std::array<uint32_t, 1> dynamicOffsets{ frameIndex * cameraDataAlignedSize };
cmdBuffer->BindDescriptorSet(cameraDescriptorSet, vk::PipelineBindPoint::eGraphics, SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, std::span{ dynamicOffsets.data(), 1 });
}
Handle<SHRenderGraph> SHRenderer::GetRenderGraph(void) const noexcept
{
return renderGraph;

View File

@ -19,6 +19,8 @@ of DigiPen Institute of Technology is prohibited.
#include "SHCamera.h"
#include "Resource/Handle.h"
#include "Graphics/RenderGraph/SHRenderGraph.h"
#include "Math/SHMath.h"
#include <vector>
namespace SHADE
{
@ -36,6 +38,13 @@ namespace SHADE
class SHCamera;
class SHVkDescriptorSetGroup;
class SHGraphicsGlobalData;
class SHVkDescriptorPool;
struct SHShaderCameraData
{
SHVec4 cameraPosition;
SHMatrix viewProjectionMatrix;
};
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
@ -54,7 +63,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Constructor/Destructors */
/*-----------------------------------------------------------------------------*/
SHRenderer(Handle<SHViewport> viewport, Handle<SHRenderGraph> renderGraph);
SHRenderer(Handle<SHVkLogicalDevice> logicalDevice, uint32_t numFrames, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHViewport> viewport, Handle<SHRenderGraph> renderGraph);
/*-----------------------------------------------------------------------------*/
/* Camera Registration */
@ -65,6 +74,7 @@ namespace SHADE
/* Drawing Functions */
/*-----------------------------------------------------------------------------*/
void Draw(uint32_t frameIndex, std::initializer_list<std::pair<Handle<SHVkBuffer>, uint32_t>> graphScopeBuffers) noexcept;
void BindDescriptorSet (Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept;
/*-----------------------------------------------------------------------------*/
/* Setters and Getters */
@ -75,8 +85,14 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
Handle<SHViewport> viewport;
Handle<SHCamera> camera;
Handle<SHRenderGraph> renderGraph;
//! Vulkan UBOs need to be aligned, this is pad SHShaderCameraData struct
uint32_t cameraDataAlignedSize;
Handle<SHViewport> viewport;
Handle<SHCamera> camera;
Handle<SHRenderGraph> renderGraph;
Handle<SHVkDescriptorSetGroup> cameraDescriptorSet;
std::vector<SHShaderCameraData> cpuCameraData;
};
}

View File

@ -49,10 +49,10 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Renderer Registration Functions */
/*---------------------------------------------------------------------------------*/
Handle<SHRenderer> SHViewport::AddRenderer(ResourceManager& resourceManager, Handle<SHRenderGraph> renderGraph)
Handle<SHRenderer> SHViewport::AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph)
{
// Create the renderer
auto renderer = resourceManager.Create<SHRenderer>(GetHandle(), renderGraph);
auto renderer = resourceManager.Create<SHRenderer>(device, numFrames, descriptorPool, cameraDescLayout, GetHandle(), renderGraph);
// Store
renderers.emplace_back(renderer);

View File

@ -30,6 +30,8 @@ namespace SHADE
class SHVkImageView;
class ResourceManager;
class SHRenderGraph;
class SHVkDescriptorPool;
class SHVkDescriptorSetLayout;
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
@ -56,7 +58,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Renderers Registration Functions */
/*-----------------------------------------------------------------------------*/
Handle<SHRenderer> AddRenderer(ResourceManager& resourceManager, Handle<SHRenderGraph> renderGraph);
Handle<SHRenderer> AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph);
void RemoveRenderer(Handle<SHRenderer> renderer);
/*-----------------------------------------------------------------------------*/

View File

@ -31,5 +31,6 @@
#include <sstream>
#include <iomanip>
#include <cstddef>
#include <span>
#include "Common/SHCommonTypes.h"