From c686954c7712c518398638c7a45904fface54a25 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Sun, 20 Nov 2022 19:03:12 +0800 Subject: [PATCH] Implemented Text Rendering - The Text Renderer Component serialization and reflection onto inspector is not in yet, but there is basic functionality. - Updated shaders - Topology for text rendering is changed to triangle fan. Front facing for primitives is clockwise. Vertex state has both the positionOffset and glyphIndex attribute set as instanced. - Freetype instance is now initialized in InitBoilerplate - SHCamera no longer calling SetPerspective/Ortho inside HandleResize - Font Geometry is now loaded in SHFontLoader into SHFontAsset fontGeometry variable. --- Assets/Fonts/SegoeUI.shfont | Bin 559876 -> 559876 bytes Assets/Shaders/Text_FS.glsl | 13 ++++++------- Assets/Shaders/Text_FS.shshaderb | Bin 2941 -> 2801 bytes Assets/Shaders/Text_VS.glsl | 9 ++++----- Assets/Shaders/Text_VS.shshaderb | Bin 4877 -> 4921 bytes .../src/Application/SBApplication.cpp | 2 +- .../Assets/Libraries/Loaders/SHFontLoader.cpp | 8 ++++++++ .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 8 ++++---- .../TextRendering/SHTextRendererComponent.cpp | 2 +- .../SHTextRenderingSubSystem.cpp | 15 +++++++++++---- 10 files changed, 35 insertions(+), 22 deletions(-) diff --git a/Assets/Fonts/SegoeUI.shfont b/Assets/Fonts/SegoeUI.shfont index 3f709c2ae0d358bca8aa02a837630d2b279423f4..321f62c853e249cbb78261997b870b07c717f7fc 100644 GIT binary patch delta 1238 zcmYk)FK<*q6b10vcH2H+f!BZV1cEdO1Y$%*MfbfG6%`0X1)`#Y4{$^H0GqH0$R;mo zkSmenYFAbwkto65b8FBN!RDW8E^|s+dgg) zO&7w4t~X(cigkSwE>XEaMz|=5+suTrfe8!Xq3u(tpe-OHL{x5Z&XAcoupIX$aGbZu z2rJ+k86o&NJIrt#Ol?9gNb3q`6DnlRyFTu2W`uvppe?O|w!8)^agP~qGl8?!$Osbv zx03}9Iz*;ohJ()A-UP2CffvHrge$atM&=1K@5`Vj@Ov;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l +++ b/Assets/Shaders/Text_FS.glsl @@ -45,15 +45,14 @@ void main() float screenPxDistance = 2 * (sd - 0.5f); float opacity = clamp (screenPxDistance + 0.5f, 0.0f, 2.0f); - vec4 fragColor = vec4 (1.0f); + vec4 fragColor; - if (opacity > 0.02f && opacity < 1.9f) - { - fragColor = vec4(0.0f, 1.0f, 1.0f, 1.0f); - } - - fragColor = mix(vec4(0.0f), vec4(In2.textColor, 1.0f), min (opacity, 1.0f)); + if (opacity < 0.2f) + discard; + else + fragColor = mix(vec4(0.0f), vec4(In2.textColor, 1.0f), min (opacity, 1.0f)); + // fragColor = vec4 (1.0f); color = fragColor; outEntityID = In2.eid; diff --git a/Assets/Shaders/Text_FS.shshaderb b/Assets/Shaders/Text_FS.shshaderb index 96c8c587e91f6fdf6d32e7ef7b514e953109bbf6..d9b47d6ee781fc9da896109c90deb842550c2a77 100644 GIT binary patch delta 899 zcmZ9K%W6|m6o%JHny8_Q1C2O|O+`h#fdeOE#Y?HNMN_T!TWg!d1Zki|&{2E^>CmT; z40PnwiM~UJP90T6!S6dgDZveY*1!IBIeV?0zvUnM%X`0aYv2EUHuizI`KOxp@yzY(G*tNELmin=@ z`|@_bLNthVU-hO}d+pwv+SQmL=MdUt#QH|=_$h1m0Cjl<U)eg2Iul0ll}>4 z5$PKE_H}M*U*w_&pFqEfW#p)WlQ8W(nEnqHavk*yI0&r0jZJ|1`ONo9YR2zu@lIMW zyu^LPh_@F_Sb}-?DzJgRGpHF~$+#aiuJ3+~+wUm02|VVv)+@#L1xKn6J{P|v{R@ar zJ_9a;PpsU@uV}Ll+85C+JPmwzKWhFITk-F{2!1iS8*&3$FHyxV19Qyv+4S8`3;O~rOz`9TSl}DB PfdLDA$M&FEI*|MWxi(o* delta 1022 zcmYk4%T5$Q7={0yp+_0f7^6ejfP)cZyd{#j@j_GxjG%xQ1aE*aU~`#CNNaS}zJi&h zOBb%qEF`>uZvY7^UctCPOiVamr)tuboYeWxf2poh{rk-SQt4!T%+i(5ojYO{SkaDK z?MrF2eMa(<5dB!$50Vk0jrryK^|ktjcxZWhuANn|wid5Oi;4?r94Hr8KgQV}rdDd# z)!&v*Uuew>EmE%jJ|3#YQT(YgXv3=Mk=AS0*vnnKLhlIy9o{YEUCfsfv&cuy8~L9> za#nO$lCEZD;gaHl_KINkdEl}#=nn+6EPU;l^y1F9Tc<5&55>JcA>QBrr#0i5;vpe3 zqiFtssYZ9$nc&zpp-bS?rnTb|`b`&eGc@uq9QXBA#oV|oP*2Rw(WrmtIA@S2j+~~? zK4pChRsRq7w6U;s^y@!0t3$rRE+VO>C{o}Xd8 Jx!c|u90Hg5WLN+I diff --git a/Assets/Shaders/Text_VS.glsl b/Assets/Shaders/Text_VS.glsl index 6263aba6..3501a13d 100644 --- a/Assets/Shaders/Text_VS.glsl +++ b/Assets/Shaders/Text_VS.glsl @@ -68,7 +68,7 @@ void main() // Generate UV coords and vertex positions Out.uv = CreateQuad(gl_VertexIndex); - vec3 vertexPos = vec3(Out.uv, 1.0f); + vec3 vertexPos = vec3(Out.uv, 0.0f); // Get the local matrices mat4 localModel = testPushConstant.worldTransform; @@ -87,16 +87,15 @@ void main() toFontSpace[2][0] = positionalOffset.x; toFontSpace[2][1] = positionalOffset.y; - mat4 PVMatrix = cameraData.vpMat; - // Initialize variables for use in FS //characterIndex = gl_InstanceID; // Transform the vertices to font space - vertexPos = toFontSpace * vertexPos; + vertexPos = toFontSpace * vec3(vertexPos.xy, 1.0f); Out2.textColor = testPushConstant.textColor; // transform the vertex position to font space - gl_Position = PVMatrix * localModel * vec4(vertexPos, 1.0f); + gl_Position = cameraData.orthoMat * localModel * vec4(vertexPos, 1.0f); + // gl_Position = vec4(vertexPos, 1.0f); } \ No newline at end of file diff --git a/Assets/Shaders/Text_VS.shshaderb b/Assets/Shaders/Text_VS.shshaderb index 3f61265468da3180337a6ae798606126ca0a420b..25eff84a998699690af58db696ddca8c8bc20864 100644 GIT binary patch delta 1344 zcmZ9K%WD%+6vpr5X%ng~g$P!PS}bZoDnd76MH6T@Y6KC)g$!{h1F;Rs7{yH%KI*I5 zbn2r%+v-}B;KF~wm0SNCzu#nT8gj#zd(ZcsbI)V$>eTP4f-^H_qLoZcW{q)7&J3Gx z?ZOQw{8|5%IGr(V$&1ySWv_nCs|Vgayfga2v(T0se&8=JiOrgJm`jbfP!nCXSoLcq zzY(}g6|d_dJ{fCws@0yB78{CZrS;p;UUYAJb+_aOZZ|6&t7cQ`oQZuMotw3bZeYx8 zkEDpE@7;x_JT4%4&8s(RUM29GUKBw~8{zll{=ILcR(feWmDH>gwIkZ(`O{iZN?If( z9J}Sn2uBvnafV9?^HS`1&}~bT(pU@fg5%C~WT#s_0;iEK+2i!xVy+uV0dZjDgH2pn zunFM?<0!H}4x6|&)7lM$91NtZ{y=O3{|YuCoQ-1wbVw2N>XwNg8L$t_W{t@!(J5pf zBwL5%`drkIa8$s4shyIr56WKCzDH;Z6Z)hK`b5m30pixR6Ni2#wt0JS6Y(=4Zh9Bt zkKE9y*Rm;_@`=Nye7I@Z`?S9mXqP-Y;f4H4=U@Jo;~ePB4d;`svtqAHeJE4~Y^r}G zToNuTI~_ibIe3;MT+x{6Zbd+6S?-C>aQKzgId?u4@I@OJ+D1n+a delta 1276 zcmZ9KTWb?h6ot=Za%l~fN})bPidrl-1s_EaBa%YMneE;L?C2F8sYO=;E3;cE5-IWk#l02z2C+Aj#KKDCyH)-NoRFY>@$JkCWQ;i zWJAvd_=+kuQ=ixk*)N2;fX~1$g)2fw_3&Q_O#RJgIgoq_Y0MYdaJ&-B veN8#8ucoW#D!vKm$tPYCI00gN0t1l4hW51{2;9uPz=jTZUVFWNN2<<$u=|f! diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index a8871cd8..3b7115a2 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -141,7 +141,7 @@ namespace Sandbox //SHComponentManager::CreateComponentSparseSet(); SHAssetManager::Load(); - auto font = SHAssetManager::GetData(176667660); + //auto font = SHAssetManager::GetData(176667660); SHSystemManager::RegisterRoutine(); diff --git a/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.cpp b/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.cpp index 9bbd3a71..7217b551 100644 --- a/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.cpp +++ b/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.cpp @@ -22,6 +22,14 @@ namespace SHADE return nullptr; } + // Attempt to load font geometry for advance data + auto ttfFilePath = path.string(); + ttfFilePath = ttfFilePath.substr(0, ttfFilePath.find_last_of('.')); + ttfFilePath += TTF_EXTENSION.data(); + msdfgen::FontHandle* fontHandle = nullptr; + fontHandle = msdfgen::loadFont(SHFreetypeInstance::GetFreetypeHandle(), ttfFilePath.c_str()); + newFontAsset->fontGeometry.loadCharset(fontHandle, 1.0f, msdf_atlas::Charset::ASCII); + uint32_t numGlyphs = 0; // read how many glyphs we have diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 3932298c..439d4cb8 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -117,6 +117,8 @@ namespace SHADE // Create generic command buffer graphicsCmdPool = device->CreateCommandPool(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS, SH_CMD_POOL_RESET::POOL_BASED, true); + SHFreetypeInstance::Init(); + SHAssetManager::CompileAsset("../../Assets/Shaders/Text_VS.glsl", false); SHAssetManager::CompileAsset("../../Assets/Shaders/Text_FS.glsl", false); SHAssetManager::CompileAsset("../../Assets/Shaders/TestCube_VS.glsl", false); @@ -379,7 +381,6 @@ namespace SHADE screenRenderer->BindDescSet(cmdBuffer, frameIndex); }); - SHFreetypeInstance::Init(); } void SHGraphicsSystem::InitBuiltInResources(void) @@ -1041,7 +1042,8 @@ namespace SHADE worldViewport->SetWidth(static_cast(resizeWidth)); worldViewport->SetHeight(static_cast(resizeHeight)); - worldCamera->SetPerspective(90.0f, static_cast(resizeWidth), static_cast(resizeHeight), 0.0f, 100.0f); + //worldCamera->SetPerspective(90.0f, static_cast(resizeWidth), static_cast(resizeHeight), 0.0f, 100.0f); + //screenCamera->SetOrthographic(static_cast(resizeWidth), static_cast(resizeHeight), 0.01f, 100.0f); auto cameraSystem = SHSystemManager::GetSystem(); #ifdef SHEDITOR @@ -1053,8 +1055,6 @@ namespace SHADE for (auto& semaHandle : graphSemaphores) semaHandle = device->CreateSemaphore(); - - } void SHGraphicsSystem::AwaitGraphicsExecution() diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.cpp index ac1dd73f..af30c8e8 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.cpp @@ -24,7 +24,7 @@ namespace SHADE /***************************************************************************/ void SHTextRendererComponent::OnCreate(void) { - text = "Text"; + text = "My name is Brandon."; requiresRecompute = true; // Default white color. diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp index ed7314ba..8fbdd33b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp @@ -16,7 +16,7 @@ namespace SHADE { void SHTextRenderingSubSystem::RecomputePositions(SHTextRendererComponent& textComp) noexcept { - if (textComp.text.empty() || textComp.fontHandle) + if (textComp.text.empty() || !textComp.fontHandle) return; // Create the buffer @@ -24,7 +24,7 @@ namespace SHADE textComp.indexingDataBuffer = logicalDevice->CreateBuffer(SHTextRendererComponent::MAX_CHARACTERS * sizeof(uint32_t), nullptr, SHTextRendererComponent::MAX_CHARACTERS * sizeof(uint32_t), vk::BufferUsageFlagBits::eVertexBuffer, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); if (!textComp.charPositionDataBuffer) - textComp.indexingDataBuffer = logicalDevice->CreateBuffer(SHTextRendererComponent::MAX_CHARACTERS * sizeof(SHVec4), nullptr, SHTextRendererComponent::MAX_CHARACTERS * sizeof(SHVec4), vk::BufferUsageFlagBits::eVertexBuffer, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); + textComp.charPositionDataBuffer = logicalDevice->CreateBuffer(SHTextRendererComponent::MAX_CHARACTERS * sizeof(SHVec4), nullptr, SHTextRendererComponent::MAX_CHARACTERS * sizeof(SHVec4), vk::BufferUsageFlagBits::eVertexBuffer, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); // For indexing font transformation in the shader std::vector indexingData; @@ -115,8 +115,8 @@ namespace SHADE SHVertexInputState vertexInputState; // Configure vertex attributes - vertexInputState.AddBinding(false, false, { SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); // location = 0 (character position data) - vertexInputState.AddBinding(false, false, { SHVertexAttribute(SHAttribFormat::UINT32_1D) }); // location = 1 (glyph index to index matrices) + vertexInputState.AddBinding(true, false, { SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); // location = 0 (character position data) + vertexInputState.AddBinding(true, false, { SHVertexAttribute(SHAttribFormat::UINT32_1D) }); // location = 1 (glyph index to index matrices) // Set vertex state for new pipeline pipeline->GetPipelineState().SetVertexInputState(vertexInputState); @@ -146,6 +146,13 @@ namespace SHADE pipeline->GetPipelineState().SetColorBlenState(colorBlendState); + SHInputAssemblyState inputAssembly{}; + inputAssembly.topology = vk::PrimitiveTopology::eTriangleFan; + pipeline->GetPipelineState().SetInputAssemblyState(inputAssembly); + + SHRasterizationState rasterState{}; + rasterState.frontFacingOrientation = vk::FrontFace::eClockwise; + pipeline->GetPipelineState().SetRasterizationState(rasterState); // Construct pipeline pipeline->ConstructPipeline();