Fixed some text shaders and changed some image parameters

- SHApplication has a font compiler exe call to test compiling a sample font
- SHVkImage now takes in a vector instead of a span for mip offsets (fuck it, copy the mip offset. The RAM can cry about it).
- Removed font data descriptor set layouts from graphics global data
- FONT_DATA set index is changed to 4 instead. This clashes with Render graph resource set index but it won't matter. 
- Added entity ID render graph resource to scene render graph
- SHFont format for images is now R8G8B8A8UNorm
This commit is contained in:
Brandon Mak 2022-11-16 16:57:08 +08:00
parent 292fdf4ee2
commit f991e7b227
21 changed files with 105 additions and 61 deletions

Binary file not shown.

View File

@ -23,16 +23,12 @@ layout(location = 0) in struct
layout(location = 3) flat in struct
{
uint eid;
vec3 textColor;
} In2;
// push constants
layout(std140, push_constant) uniform TestPushConstant
{
vec3 textColor;
} testPushConstant;
layout(set = 6, binding = 0) uniform sampler2D fontBitmap;
layout(set = 4, binding = 0) uniform sampler2D fontBitmap;
layout(location = 0) out vec4 color;
layout(location = 1) out uint outEntityID;
@ -56,7 +52,7 @@ void main()
fragColor = vec4(0.0f, 1.0f, 1.0f, 1.0f);
}
fragColor = mix(vec4(0.0f), vec4(testPushConstant.textColor, 1.0f), min (opacity, 1.0f));
fragColor = mix(vec4(0.0f), vec4(In2.textColor, 1.0f), min (opacity, 1.0f));
color = fragColor;

Binary file not shown.

View File

@ -21,6 +21,7 @@ layout(location = 0) out struct
layout(location = 3) out struct
{
uint eid;
vec3 textColor;
} Out2;
// Camera data
@ -37,11 +38,12 @@ layout(std140, push_constant) uniform TestPushConstant
{
mat4 worldTransform;
uint eid;
vec3 textColor;
} testPushConstant;
// Descriptor sets
layout(std430, set = 6, binding = 1) buffer GlyphTransforms
layout(std430, set = 4, binding = 1) buffer GlyphTransforms
{
mat4 matrices[];
} glyphTransforms;
@ -92,6 +94,8 @@ void main()
// Transform the vertices to font space
vertexPos = toFontSpace * vertexPos;
Out2.textColor = testPushConstant.textColor;
// transform the vertex position to font space
gl_Position = PVMatrix * localModel * vec4(vertexPos, 1.0f);
}

Binary file not shown.

View File

@ -78,6 +78,7 @@ namespace Sandbox
SHSystemManager::CreateSystem<SHAudioSystem>();
SHSystemManager::CreateSystem<SHCameraSystem>();
std::system("FontCompiler.exe ../../Assets/Fonts/SegoeUI.ttf");
SHSystemManager::CreateSystem<SHGraphicsSystem>();
SHGraphicsSystem* graphicsSystem = static_cast<SHGraphicsSystem*>(SHSystemManager::GetSystem<SHGraphicsSystem>());
@ -148,8 +149,6 @@ namespace Sandbox
SHFrameRateController::UpdateFRC();
std::system("FontCompiler.exe ../../Assets/Fonts/SegoeUI.ttf");
// Link up SHDebugDraw
SHDebugDraw::Init(SHSystemManager::GetSystem<SHDebugDrawSystem>());
}

View File

@ -459,7 +459,7 @@ namespace SHADE
return SHVkInstance::GetResourceManager().Create<SHVkImage>(GetHandle(), &vmaAllocator, w, h, levels, format, usage, create);
}
Handle<SHVkImage> SHVkLogicalDevice::CreateImage(SHImageCreateParams const& imageDetails, unsigned char* data, uint32_t dataSize, std::span<uint32_t> inMipOffsets, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags) noexcept
Handle<SHVkImage> SHVkLogicalDevice::CreateImage(SHImageCreateParams const& imageDetails, unsigned char* data, uint32_t dataSize, std::vector<uint32_t> const& inMipOffsets, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags) noexcept
{
return SHVkInstance::GetResourceManager().Create<SHVkImage>(GetHandle(), &vmaAllocator, imageDetails, data, dataSize, inMipOffsets, memUsage, allocFlags);
}

View File

@ -162,7 +162,7 @@ namespace SHADE
SHImageCreateParams const& imageDetails,
unsigned char* data,
uint32_t dataSize,
std::span<uint32_t> inMipOffsets,
std::vector<uint32_t> const& inMipOffsets,
VmaMemoryUsage memUsage,
VmaAllocationCreateFlags allocFlags
) noexcept;

View File

@ -123,7 +123,7 @@ namespace SHADE
SHImageCreateParams const& imageDetails,
const unsigned char* data,
uint32_t dataSize,
std::span<uint32_t> inMipOffsets,
std::vector<uint32_t> const& inMipOffsets,
VmaMemoryUsage memUsage,
VmaAllocationCreateFlags allocFlags
) noexcept

View File

@ -102,7 +102,7 @@ namespace SHADE
vk::Buffer stagingBuffer;
//! Mipmap offsets for initializing the vk::BufferImageCopy during transfer to GPU resource
std::span<uint32_t> mipOffsets;
std::vector<uint32_t> mipOffsets;
//! Handle to the device that creates these images
Handle<SHVkLogicalDevice> device;
@ -125,7 +125,7 @@ namespace SHADE
SHImageCreateParams const& imageDetails,
const unsigned char* data,
uint32_t dataSize,
std::span<uint32_t> inMipOffsets,
std::vector<uint32_t> const& inMipOffsets,
VmaMemoryUsage memUsage,
VmaAllocationCreateFlags allocFlags
) noexcept;

View File

@ -97,23 +97,6 @@ namespace SHADE
Handle<SHVkDescriptorSetLayout> materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE, { materialDataBinding });
SET_VK_OBJ_NAME(logicalDevice, vk::ObjectType::eDescriptorSetLayout, materialDataPerInstanceLayout->GetVkHandle(), "[Descriptor Set Layout] Material Globals");
SHVkDescriptorSetLayout::Binding fontBitmapBinding
{
.Type = vk::DescriptorType::eCombinedImageSampler,
.Stage = vk::ShaderStageFlagBits::eFragment,
.BindPoint = SHGraphicsConstants::DescriptorSetBindings::FONT_BITMAP_DATA,
.DescriptorCount = 1,
};
SHVkDescriptorSetLayout::Binding fontMatrixBinding
{
.Type = vk::DescriptorType::eStorageBuffer,
.Stage = vk::ShaderStageFlagBits::eVertex,
.BindPoint = SHGraphicsConstants::DescriptorSetBindings::FONT_MATRIX_DATA,
.DescriptorCount = 1,
};
Handle<SHVkDescriptorSetLayout> fontDataLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::FONT_DATA, { fontBitmapBinding, fontMatrixBinding });
globalDescSetLayouts.push_back(staticGlobalLayout);

View File

@ -66,7 +66,9 @@ namespace SHADE
/***************************************************************************/
/*!
\brief
DescriptorSet Index for render graph resources.
DescriptorSet Index for render graph resources. Unlike the sets from
1 to 3 and 6, this set index does not have hard coded bindings and is
NOT part of the layouts included in the global data.
*/
/***************************************************************************/
static constexpr uint32_t RENDERGRAPH_RESOURCE = 4;
@ -75,7 +77,7 @@ namespace SHADE
\brief
DescriptorSet Index for render graph node compute resources. For data
that we wish to pass to compute shaders in the render graph, this is
the set to use. Unlike the sets from 1 to 3, this set index does not have
the set to use. Unlike the sets from 1 to 3 and 6, this set index does not have
hard coded bindings and is NOT part of the layouts included in the global
data.
*/
@ -89,7 +91,7 @@ namespace SHADE
*/
/***************************************************************************/
static constexpr uint32_t FONT_DATA = 6;
static constexpr uint32_t FONT_DATA = 4;
};
struct DescriptorSetBindings

View File

@ -282,10 +282,12 @@ namespace SHADE
screenRenderGraph = resourceManager.Create<SHRenderGraph>();
screenRenderGraph->Init("Scene Render Graph", device, swapchain, &resourceManager);
screenRenderGraph->LinkNonOwningResource(worldRenderGraph, "Scene");
screenRenderGraph->LinkNonOwningResource(worldRenderGraph, "Entity ID");
auto screenSpaceNode = screenRenderGraph->AddNode("Screen Space Pass", { "Scene" }, {});
auto screenSpaceNode = screenRenderGraph->AddNode("Screen Space Pass", { "Scene", "Entity ID"}, {});
auto uiSubpass = screenSpaceNode->AddSubpass("UI");
uiSubpass->AddColorOutput("Scene");
uiSubpass->AddColorOutput("Entity ID");
{
// Dummy Node to transition scene render graph resource
@ -360,8 +362,8 @@ namespace SHADE
textRenderingSubSystem = resourceManager.Create<SHTextRenderingSubSystem>();
// initialize the text renderer
//auto uiNode = screenRenderGraph->GetNode("Screen Space Pass");
//textRenderingSubSystem->Init(device, uiNode->GetRenderpass(), uiNode->GetSubpass("UI"), descPool, textVS, textFS);
auto uiNode = screenRenderGraph->GetNode("Screen Space Pass");
textRenderingSubSystem->Init(device, uiNode->GetRenderpass(), uiNode->GetSubpass("UI"), descPool, textVS, textFS);
SHFreetypeInstance::Init();
}
@ -855,7 +857,7 @@ namespace SHADE
void SHGraphicsSystem::BuildFonts(void) noexcept
{
fontLibrary.BuildFonts(device, graphicsQueue, graphicsCmdPool, descPool, resourceManager);
fontLibrary.BuildFonts(device, graphicsQueue, graphicsCmdPool, descPool, textRenderingSubSystem->GetFontDataDescSetLayout(), resourceManager);
}
#pragma endregion ADD_REMOVE

View File

@ -76,7 +76,7 @@ namespace SHADE
};
uint32_t mipOffset = 0;
rotationVectorsImage = logicalDevice->CreateImage(imageDetails, reinterpret_cast<unsigned char*>( rotationVectors.data()), static_cast<uint32_t>(sizeof(rotationVectors)), {&mipOffset, 1}, VMA_MEMORY_USAGE_AUTO, {});
rotationVectorsImage = logicalDevice->CreateImage(imageDetails, reinterpret_cast<unsigned char*>(rotationVectors.data()), static_cast<uint32_t>(sizeof(rotationVectors)), { mipOffset }, VMA_MEMORY_USAGE_AUTO, {});
vk::ImageMemoryBarrier transferBarrier{};
rotationVectorsImage->PrepareImageTransitionInfo(vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, transferBarrier);

View File

@ -4,6 +4,7 @@
#include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h"
#include "Graphics/Descriptors/SHVkDescriptorSetGroup.h"
#include "Graphics/Buffers/SHVkBuffer.h"
#include "Graphics/Images/SHVkSampler.h"
namespace SHADE
{
@ -42,9 +43,9 @@ namespace SHADE
.depth = 1,
.levels = 1,
.arrayLayers = 1,
//.imageFormat = vk::Format::eR8G8B8Snorm,
.imageFormat = vk::Format::eR8G8B8A8Snorm,
//.imageFormat = vk::Format::eR32Sfloat,
.imageFormat = vk::Format::eR32G32B32Sfloat,
//.imageFormat = vk::Format::eR32G32B32Sfloat,
.usageFlags = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst,
.createFlags = {}
};
@ -53,7 +54,7 @@ namespace SHADE
uint32_t mipOffset = 0;
// Create the image
bitmapDataImage = logicalDevice->CreateImage(imageParams, fontAsset.bitmapData.data(), bytesRequired, { &mipOffset, 1 }, VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO, {});
bitmapDataImage = logicalDevice->CreateImage(imageParams, fontAsset.bitmapData.data(), bytesRequired, { mipOffset }, VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO, {});
// Amount of data required to hold matrices for all glyphs
uint32_t glyphDataSize = fontAsset.glyphTransformations.size() * sizeof (SHMatrix);
@ -65,9 +66,7 @@ namespace SHADE
// allocate GPU buffer for matrices
matrixDataBuffer = logicalDevice->CreateBuffer(glyphDataSize, fontAsset.glyphTransformations.data(), glyphDataSize, vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlagBits::eStorageBuffer, VMA_MEMORY_USAGE_AUTO, {});
//// Prepare pre and post transfer barrier
//bitmapDataImage->PrepareImageTransitionInfo(vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, preTransferBarrier);
//bitmapDataImage->PrepareImageTransitionInfo(vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal, postTransferBarrier);
sampler = logicalDevice->CreateSampler(SHVkSamplerParams{});
}
@ -97,7 +96,7 @@ namespace SHADE
matrixDataBuffer->TransferToDeviceResource(commandBuffer);
}
void SHFont::DoPostTransfer(Handle<SHVkDescriptorPool> descPool) noexcept
void SHFont::DoPostTransfer(Handle<SHVkDescriptorPool> descPool, Handle<SHVkDescriptorSetLayout> layout) noexcept
{
/*-----------------------------------------------------------------------*/
/* CREATE IMAGE VIEW */
@ -106,7 +105,7 @@ namespace SHADE
SHImageViewDetails viewDetails
{
.viewType = vk::ImageViewType::e2D,
.format = vk::Format::eR32G32B32Sfloat,
.format = bitmapDataImage->GetImageFormat(),
.imageAspectFlags = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.mipLevelCount = 1,
@ -118,14 +117,11 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
/* DESCRIPTORS */
/*-----------------------------------------------------------------------*/
// Font data desc set layout
auto fontDataLayout = SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetBindings::FONT_BITMAP_DATA];
// allocate desc set for the bitmap and matrix data
descSet = descPool->Allocate({ fontDataLayout }, { 1, 1 });
descSet = descPool->Allocate({ layout }, { 1 });
//auto viewLayoutSampler = std::make_tuple()
descSet->ModifyWriteDescImage(SHGraphicsConstants::DescriptorSetIndex::FONT_DATA, SHGraphicsConstants::DescriptorSetBindings::FONT_BITMAP_DATA, {});
auto viewLayoutSampler = std::make_tuple(bitmapDataImageView, sampler, vk::ImageLayout::eShaderReadOnlyOptimal);
descSet->ModifyWriteDescImage(SHGraphicsConstants::DescriptorSetIndex::FONT_DATA, SHGraphicsConstants::DescriptorSetBindings::FONT_BITMAP_DATA, {&viewLayoutSampler, 1});
descSet->ModifyWriteDescBuffer(SHGraphicsConstants::DescriptorSetIndex::FONT_DATA,
SHGraphicsConstants::DescriptorSetBindings::FONT_MATRIX_DATA, { &matrixDataBuffer, 1 }, 0, fontAsset.glyphTransformations.size() * sizeof(SHMatrix));

View File

@ -9,12 +9,14 @@ namespace SHADE
class SHVkLogicalDevice;
class SHVkDescriptorPool;
class SHVkDescriptorSetGroup;
class SHVkDescriptorSetLayout;
class SHVkCommandBuffer;
class SHVkCommandPool;
class SHVkImage;
class SHVkImageView;
class SHVkBuffer;
class SHVkQueue;
class SHVkSampler;
class SHFont
{
@ -37,6 +39,8 @@ namespace SHADE
//! Descriptor set required to store the bitmap AND matrix data for the UV and quad transformation
Handle<SHVkDescriptorSetGroup> descSet;
Handle<SHVkSampler> sampler;
//! Used for getting the correct indices into the matrix data buffer
std::unordered_map<msdfgen::unicode_t, uint32_t> unicodeIndexing;
@ -52,7 +56,7 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
SHFont (Handle<SHVkLogicalDevice> inLogicalDeviceHdl, SHFontAsset const& asset) noexcept;
void TransferToGPU (Handle<SHVkCommandBuffer> commandBuffer) noexcept;
void DoPostTransfer (Handle<SHVkDescriptorPool> descPool) noexcept;
void DoPostTransfer (Handle<SHVkDescriptorPool> descPool, Handle<SHVkDescriptorSetLayout> layout) noexcept;
/*-----------------------------------------------------------------------*/

View File

@ -50,11 +50,11 @@ namespace SHADE
*/
/***************************************************************************/
void SHFontLibrary::BuildFonts(Handle<SHVkLogicalDevice> logicalDevice, Handle<SHVkQueue> queue, Handle<SHVkCommandPool> cmdPool, Handle<SHVkDescriptorPool> descPool, SHResourceHub& resourceHub) noexcept
void SHFontLibrary::BuildFonts(Handle<SHVkLogicalDevice> logicalDevice, Handle<SHVkQueue> queue, Handle<SHVkCommandPool> cmdPool, Handle<SHVkDescriptorPool> descPool, Handle<SHVkDescriptorSetLayout> layout, SHResourceHub& resourceHub) noexcept
{
// create fence to wait on after transfer
Handle<SHVkFence> finishCopyFence = resourceHub.Create<SHVkFence>(logicalDevice);
finishCopyFence->Reset();
// allocate new command buffer
Handle<SHVkCommandBuffer> transferCommandBuffer = cmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY);
@ -84,10 +84,10 @@ namespace SHADE
// Prepare image views and desc sets
for (auto& font : unpreparedFonts)
font->DoPostTransfer(descPool);
font->DoPostTransfer(descPool, layout);
// Free the command buffer and fence
resourceHub.Free(transferCommandBuffer);
transferCommandBuffer.Free();
resourceHub.Free(finishCopyFence);
// Once unprepared fonts are now ready for use, push them into container

View File

@ -11,6 +11,7 @@ namespace SHADE
class SHVkCommandPool;
class SHVkCommandBuffer;
class SHVkQueue;
class SHVkDescriptorSetLayout;
class SHFontLibrary
{
@ -29,6 +30,6 @@ namespace SHADE
public:
Handle<SHFont> AddFont (Handle<SHVkLogicalDevice> logicalDevice, SHResourceHub& resourceHub, SHFontAsset const& asset) noexcept;
void BuildFonts (Handle<SHVkLogicalDevice> logicalDevice, Handle<SHVkQueue> queue, Handle<SHVkCommandPool> cmdPool, Handle<SHVkDescriptorPool> descPool, SHResourceHub& resourceHub) noexcept;
void BuildFonts (Handle<SHVkLogicalDevice> logicalDevice, Handle<SHVkQueue> queue, Handle<SHVkCommandPool> cmdPool, Handle<SHVkDescriptorPool> descPool, Handle<SHVkDescriptorSetLayout> layout, SHResourceHub& resourceHub) noexcept;
};
}

View File

@ -8,6 +8,8 @@
#include "Graphics/Buffers/SHVkBuffer.h"
#include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h"
#include "Graphics/Pipeline/SHVkPipeline.h"
#include "Graphics/SHVkUtil.h"
#include "Graphics/RenderGraph/SHSubpass.h"
namespace SHADE
{
@ -114,8 +116,53 @@ namespace SHADE
// Set vertex state for new pipeline
pipeline->GetPipelineState().SetVertexInputState(vertexInputState);
SHColorBlendState colorBlendState{};
colorBlendState.logic_op_enable = VK_FALSE;
colorBlendState.logic_op = vk::LogicOp::eCopy;
auto const& subpassColorReferences = subpass->GetColorAttachmentReferences();
colorBlendState.attachments.reserve(subpassColorReferences.size());
for (auto& att : subpassColorReferences)
{
colorBlendState.attachments.push_back(vk::PipelineColorBlendAttachmentState
{
.blendEnable = SHVkUtil::IsBlendCompatible(subpass->GetFormatFromAttachmentReference(att.attachment)) ? true : false,
.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha,
.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
.colorBlendOp = vk::BlendOp::eAdd,
.srcAlphaBlendFactor = vk::BlendFactor::eOne,
.dstAlphaBlendFactor = vk::BlendFactor::eZero,
.alphaBlendOp = vk::BlendOp::eAdd,
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA,
}
);
}
pipeline->GetPipelineState().SetColorBlenState(colorBlendState);
// Construct pipeline
pipeline->ConstructPipeline();
SHVkDescriptorSetLayout::Binding fontBitmapBinding
{
.Type = vk::DescriptorType::eCombinedImageSampler,
.Stage = vk::ShaderStageFlagBits::eFragment,
.BindPoint = SHGraphicsConstants::DescriptorSetBindings::FONT_BITMAP_DATA,
.DescriptorCount = 1,
};
SHVkDescriptorSetLayout::Binding fontMatrixBinding
{
.Type = vk::DescriptorType::eStorageBuffer,
.Stage = vk::ShaderStageFlagBits::eVertex,
.BindPoint = SHGraphicsConstants::DescriptorSetBindings::FONT_MATRIX_DATA,
.DescriptorCount = 1,
};
fontDataDescSetLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::FONT_DATA, { fontBitmapBinding, fontMatrixBinding });
}
void SHTextRenderingSubSystem::Run(uint32_t frameIndex) noexcept
@ -164,4 +211,9 @@ namespace SHADE
}
Handle<SHVkDescriptorSetLayout> SHTextRenderingSubSystem::GetFontDataDescSetLayout(void) const noexcept
{
return fontDataDescSetLayout;
}
}

View File

@ -32,6 +32,9 @@ namespace SHADE
//! Pipeline layout for the pipeline
Handle<SHVkPipelineLayout> pipelineLayout;
//! Descriptor set for font data access in shaders
Handle<SHVkDescriptorSetLayout> fontDataDescSetLayout;
private:
void RecomputePositions(SHTextRendererComponent& textComp) noexcept;
@ -41,6 +44,8 @@ namespace SHADE
void Render (Handle<SHVkCommandBuffer> cmdBuffer) noexcept;
void Exit(void) noexcept;
Handle<SHVkDescriptorSetLayout> GetFontDataDescSetLayout (void) const noexcept;
};
}

View File

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