Text Rendering WIP

- Added font library to store font resources (not yet synced with resource manager)
This commit is contained in:
Brandon Mak 2022-11-14 14:14:11 +08:00
parent 898b7fbf2c
commit 7209785f9b
11 changed files with 82 additions and 22 deletions

View File

@ -413,4 +413,9 @@ namespace SHADE
return height; return height;
} }
uint32_t SHVkImage::GetMipLevels(void) const noexcept
{
return mipLevelCount;
}
} }

View File

@ -150,6 +150,7 @@ namespace SHADE
vk::Format GetImageFormat (void) const noexcept; vk::Format GetImageFormat (void) const noexcept;
uint32_t GetWidth (void) const noexcept; uint32_t GetWidth (void) const noexcept;
uint32_t GetHeight (void) const noexcept; uint32_t GetHeight (void) const noexcept;
uint32_t GetMipLevels (void) const noexcept;
}; };
} }

View File

@ -831,9 +831,17 @@ namespace SHADE
return texLibrary.GetTextureHandle(textureId); return texLibrary.GetTextureHandle(textureId);
} }
Handle<SHFont> SHGraphicsSystem::AddFont(SHFontAsset const& fontAsset) const noexcept /*---------------------------------------------------------------------------------*/
/* Font Registration Functions */
/*---------------------------------------------------------------------------------*/
Handle<SHFont> SHGraphicsSystem::AddFont(SHFontAsset const& fontAsset) noexcept
{ {
return fontLibrary.AddFont(device, resourceManager, fontAsset);
}
void SHGraphicsSystem::BuildFonts(void) noexcept
{
fontLibrary.BuildFonts(device, graphicsQueue, graphicsCmdPool, descPool, resourceManager);
} }
#pragma endregion ADD_REMOVE #pragma endregion ADD_REMOVE

View File

@ -336,7 +336,8 @@ namespace SHADE
*/ */
/***************************************************************************/ /***************************************************************************/
Handle<SHFont> AddFont (SHFontAsset const& fontAsset) const noexcept; Handle<SHFont> AddFont (SHFontAsset const& fontAsset) noexcept;
void BuildFonts (void) noexcept;
void PrepareResize(uint32_t newWidth, uint32_t newHeight) noexcept; void PrepareResize(uint32_t newWidth, uint32_t newHeight) noexcept;
void HandleResize(void) noexcept; void HandleResize(void) noexcept;

View File

@ -22,7 +22,7 @@ namespace SHADE
*/ */
/***************************************************************************/ /***************************************************************************/
SHFont::SHFont(Handle<SHVkLogicalDevice> inLogicalDeviceHdl, SHFontAsset& asset) noexcept SHFont::SHFont(Handle<SHVkLogicalDevice> inLogicalDeviceHdl, SHFontAsset const& asset) noexcept
{ {
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* PREPARE GPU DATA */ /* PREPARE GPU DATA */
@ -88,8 +88,6 @@ namespace SHADE
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* COMMANDS TO TRANSFER TO DEVICE MEMORY */ /* COMMANDS TO TRANSFER TO DEVICE MEMORY */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
// Transfer to device memory // Transfer to device memory
bitmapDataImage->TransferToDeviceResource(commandBuffer); bitmapDataImage->TransferToDeviceResource(commandBuffer);
@ -151,4 +149,9 @@ namespace SHADE
return bitmapDataImage; return bitmapDataImage;
} }
Handle<SHVkBuffer> SHFont::GetMatrixBuffer(void) const noexcept
{
return matrixDataBuffer;
}
} }

View File

@ -50,7 +50,7 @@ namespace SHADE
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* PUBLIC MEMBER FUNCTIONS */ /* PUBLIC MEMBER FUNCTIONS */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
SHFont (Handle<SHVkLogicalDevice> inLogicalDeviceHdl, SHFontAsset& asset) noexcept; SHFont (Handle<SHVkLogicalDevice> inLogicalDeviceHdl, SHFontAsset const& asset) noexcept;
void TransferToGPU (Handle<SHVkCommandBuffer> commandBuffer) noexcept; void TransferToGPU (Handle<SHVkCommandBuffer> commandBuffer) noexcept;
void DoPostTransfer (Handle<SHVkDescriptorPool> descPool) noexcept; void DoPostTransfer (Handle<SHVkDescriptorPool> descPool) noexcept;
@ -61,6 +61,7 @@ namespace SHADE
std::unordered_map<msdfgen::unicode_t, uint32_t> GetUnicodeIndexing (void) const noexcept; std::unordered_map<msdfgen::unicode_t, uint32_t> GetUnicodeIndexing (void) const noexcept;
SHFontAsset const& GetFontAsset (void) const noexcept; SHFontAsset const& GetFontAsset (void) const noexcept;
Handle<SHVkImage> GetImage (void) const noexcept; Handle<SHVkImage> GetImage (void) const noexcept;
Handle<SHVkBuffer> GetMatrixBuffer (void) const noexcept;
}; };
} }

View File

@ -2,6 +2,9 @@
#include "SHFontLibrary.h" #include "SHFontLibrary.h"
#include "Graphics/Images/SHVkImage.h" #include "Graphics/Images/SHVkImage.h"
#include "Graphics/Commands/SHVkCommandPool.h" #include "Graphics/Commands/SHVkCommandPool.h"
#include "Graphics/Devices/SHVkLogicalDevice.h"
#include "Graphics/Synchronization/SHVkFence.h"
#include "Graphics/Buffers/SHVkBuffer.h"
namespace SHADE namespace SHADE
{ {
@ -26,6 +29,10 @@ namespace SHADE
postTransferBarriers.emplace_back(); postTransferBarriers.emplace_back();
newFont->GetImage()->PrepareImageTransitionInfo(vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, preTransferBarriers[preTransferBarriers.size() - 1]); newFont->GetImage()->PrepareImageTransitionInfo(vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, preTransferBarriers[preTransferBarriers.size() - 1]);
newFont->GetImage()->PrepareImageTransitionInfo(vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal, postTransferBarriers[postTransferBarriers.size() - 1]); newFont->GetImage()->PrepareImageTransitionInfo(vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal, postTransferBarriers[postTransferBarriers.size() - 1]);
unpreparedFonts.emplace_back (newFont);
return newFont;
} }
/***************************************************************************/ /***************************************************************************/
@ -43,30 +50,54 @@ namespace SHADE
*/ */
/***************************************************************************/ /***************************************************************************/
void SHFontLibrary::BuildFonts(Handle<SHVkCommandPool> cmdPool, Handle<SHVkDescriptorPool> descPool, SHResourceHub& resourceHub) noexcept void SHFontLibrary::BuildFonts(Handle<SHVkLogicalDevice> logicalDevice, Handle<SHVkQueue> queue, Handle<SHVkCommandPool> cmdPool, Handle<SHVkDescriptorPool> descPool, SHResourceHub& resourceHub) noexcept
{ {
//std::vector // create fence to wait on after transfer
Handle<SHVkFence> finishCopyFence = resourceHub.Create<SHVkFence>(logicalDevice);
// allocate new command buffer // allocate new command buffer
Handle<SHVkCommandBuffer> transferCommandBuffer = cmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); Handle<SHVkCommandBuffer> transferCommandBuffer = cmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY);
// Begin recording transfer ops // Begin recording transfer ops
transferCommandBuffer->BeginRecording(); transferCommandBuffer->BeginRecording();
{
// Transition image to dst
transferCommandBuffer->PipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer, {}, {}, {}, preTransferBarriers);
// Transition image to dst // Transfer data from staging to image
transferCommandBuffer->PipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer, {}, {}, {}, preTransferBarriers); for (auto& font : unpreparedFonts)
font->TransferToGPU(transferCommandBuffer);
// Transition dst to shader read
transferCommandBuffer->PipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, {}, {}, {}, postTransferBarriers);
// Transition dst to shader read
transferCommandBuffer->PipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, {}, {}, {}, postTransferBarriers);
}
// End recording for transfer ops // End recording for transfer ops
transferCommandBuffer->EndRecording(); transferCommandBuffer->EndRecording();
// Free it // Submit command buffer to queue
queue->SubmitCommandBuffer({transferCommandBuffer}, {}, {}, vk::PipelineStageFlagBits::eTransfer, finishCopyFence);
// wait for copy to finish
finishCopyFence->Wait(true, std::numeric_limits<uint32_t>::max());
// Prepare image views and desc sets
for (auto& font : unpreparedFonts)
font->DoPostTransfer(descPool);
// Free the command buffer and fence
resourceHub.Free(transferCommandBuffer); resourceHub.Free(transferCommandBuffer);
resourceHub.Free(finishCopyFence);
// Once unprepared fonts are now ready for use, push them into container
uint32_t i = static_cast<uint32_t> (unpreparedFonts.size());
std::copy (unpreparedFonts.begin(), unpreparedFonts.end(), std::back_inserter(fonts));
// All fonts have been prepared for GPU usage
unpreparedFonts.clear();
preTransferBarriers.clear();
postTransferBarriers.clear();
} }
} }

View File

@ -18,8 +18,8 @@ namespace SHADE
//! Handles to all the fonts usable in SHTextRendererComponents //! Handles to all the fonts usable in SHTextRendererComponents
std::vector<Handle<SHFont>> fonts; std::vector<Handle<SHFont>> fonts;
//! for indexing //! Fonts that have yet to be properly prepared for usage
std::unordered_map<decltype (std::declval<Handle<SHFont>>().GetId().Raw), uint32_t> fontIndexing; std::vector<Handle<SHFont>> unpreparedFonts;
//! For transitioning images for transfer operations //! For transitioning images for transfer operations
std::vector<vk::ImageMemoryBarrier> preTransferBarriers; std::vector<vk::ImageMemoryBarrier> preTransferBarriers;
@ -29,6 +29,6 @@ namespace SHADE
public: public:
Handle<SHFont> AddFont (Handle<SHVkLogicalDevice> logicalDevice, SHResourceHub& resourceHub, SHFontAsset const& asset) noexcept; Handle<SHFont> AddFont (Handle<SHVkLogicalDevice> logicalDevice, SHResourceHub& resourceHub, SHFontAsset const& asset) noexcept;
void BuildFonts (Handle<SHVkCommandPool> cmdPool, Handle<SHVkDescriptorPool> descPool, SHResourceHub& resourceHub) noexcept; void BuildFonts (Handle<SHVkLogicalDevice> logicalDevice, Handle<SHVkQueue> queue, Handle<SHVkCommandPool> cmdPool, Handle<SHVkDescriptorPool> descPool, SHResourceHub& resourceHub) noexcept;
}; };
} }

View File

@ -25,7 +25,8 @@ namespace SHADE
std::unordered_map<std::type_index, std::function<void(AssetID)>> SHResourceManager::typedFreeFuncMap; std::unordered_map<std::type_index, std::function<void(AssetID)>> SHResourceManager::typedFreeFuncMap;
std::vector<AssetID> SHResourceManager::loadedAssetData; std::vector<AssetID> SHResourceManager::loadedAssetData;
bool SHResourceManager::textureChanged = false; bool SHResourceManager::textureChanged = false;
bool SHResourceManager::meshChanged = false; bool SHResourceManager::meshChanged = false;
bool SHResourceManager::fontChanged = false;
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Function Definitions */ /* Function Definitions */
@ -76,6 +77,11 @@ namespace SHADE
gfxSystem->BuildTextures(); gfxSystem->BuildTextures();
textureChanged = false; textureChanged = false;
} }
if (fontChanged)
{
gfxSystem->BuildFonts();
fontChanged = false;
}
// Free CPU Resources // Free CPU Resources
for (auto assetId : loadedAssetData) for (auto assetId : loadedAssetData)

View File

@ -139,6 +139,7 @@ namespace SHADE
// Dirty Flags // Dirty Flags
static bool meshChanged; static bool meshChanged;
static bool textureChanged; static bool textureChanged;
static bool fontChanged;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */

View File

@ -318,7 +318,10 @@ namespace SHADE
} }
else if constexpr (std::is_same_v<ResourceType, SHFontAsset>) else if constexpr (std::is_same_v<ResourceType, SHFontAsset>)
{ {
loadedAssetData.emplace_back(assetId);
textureChanged = true;
return gfxSystem->AddFont(assetData);
} }
} }
} }