diff --git a/Assets/Shaders/Text_FS.glsl b/Assets/Shaders/Text_FS.glsl index d6f88687..f3884973 100644 --- a/Assets/Shaders/Text_FS.glsl +++ b/Assets/Shaders/Text_FS.glsl @@ -22,29 +22,43 @@ layout(location = 0) in struct // material stuff layout(location = 3) flat in struct { - int materialIndex; uint eid; - uint lightLayerIndex; } In2; -layout (set = 0, binding = 1) uniform sampler2D textures[]; // for textures (global) -layout (std430, set = 3, binding = 0) buffer MaterialProperties // For materials +// push constants +layout(std140, push_constant) uniform TestPushConstant { - MatPropData data[]; -} MatProp; + vec3 textColor; -layout(location = 0) out vec4 position; +} testPushConstant; + +layout(set = 6, binding = 0) uniform sampler2D fontBitmap; + +layout(location = 0) out vec4 color; layout(location = 1) out uint outEntityID; -layout(location = 2) out uint lightLayerIndices; -layout(location = 3) out vec4 normals; -layout(location = 4) out vec4 albedo; + +float median(float r, float g, float b) +{ + return max(min(r, g), min(max(r, g), b)); +} void main() { - position = In.vertPos; - normals = In.normal; - albedo = texture(textures[nonuniformEXT(MatProp.data[In2.materialIndex].textureIndex)], In.uv) * MatProp.data[In2.materialIndex].color; + vec3 msd = texture (fontBitmap, In.uv).rgb; + float sd = median (msd.r, msd.g, msd.b); + float screenPxDistance = 2 * (sd - 0.5f); + float opacity = clamp (screenPxDistance + 0.5f, 0.0f, 2.0f); + vec4 fragColor = vec4 (1.0f); + + if (opacity > 0.02f && opacity < 1.9f) + { + fragColor = vec4(0.0f, 1.0f, 1.0f, 1.0f); + } + + fragColor = mix(vec4(0.0f), vec4(testPushConstant.textColor, 1.0f), min (opacity, 1.0f)); + + + color = fragColor; outEntityID = In2.eid; - lightLayerIndices = In2.lightLayerIndex; } \ No newline at end of file diff --git a/Assets/Shaders/Text_FS.shshaderb b/Assets/Shaders/Text_FS.shshaderb new file mode 100644 index 00000000..9121f960 Binary files /dev/null and b/Assets/Shaders/Text_FS.shshaderb differ diff --git a/Assets/Shaders/Text_FS.shshaderb.shmeta b/Assets/Shaders/Text_FS.shshaderb.shmeta new file mode 100644 index 00000000..af5a44b7 --- /dev/null +++ b/Assets/Shaders/Text_FS.shshaderb.shmeta @@ -0,0 +1,3 @@ +Name: Text_FS +ID: 38024754 +Type: 2 diff --git a/Assets/Shaders/Text_VS.glsl b/Assets/Shaders/Text_VS.glsl index bfc220d4..369e185e 100644 --- a/Assets/Shaders/Text_VS.glsl +++ b/Assets/Shaders/Text_VS.glsl @@ -4,10 +4,12 @@ //#include "ShaderDescriptorDefinitions.glsl" +// vertex inputs layout(location = 0) in vec4 positionalOffset; -layout(location = 1) in unsigned int glyphIndex; - +layout(location = 1) in uint glyphIndex; +layout(location = 2) in uvec2 integerData; +// between shader stages layout(location = 0) out struct { vec4 vertPos; // location 0 @@ -22,6 +24,7 @@ layout(location = 3) out struct uint eid; } Out2; +// Camera data layout(set = 2, binding = 0) uniform CameraData { vec4 position; @@ -30,10 +33,65 @@ layout(set = 2, binding = 0) uniform CameraData mat4 projMat; } cameraData; +// push constants +layout(std140, push_constant) uniform TestPushConstant +{ + mat4 worldTransform; + +} testPushConstant; + +// Descriptor sets +layout(std430, set = 6, binding = 1) buffer GlyphTransforms +{ + mat4 matrices[]; +} glyphTransforms; + +vec2 CreateQuad(in uint vertexID) +{ + uint b = 1 << vertexID; + return vec2 ((0x3 & b) != 0, (0x9 & b) != 0); +} + void main() { + // write EID data to FS Out2.eid = integerData[0]; - - // clip space for rendering - gl_Position = cameraData.vpMat * worldTransform * vec4 (aVertexPos, 1.0f); + + // local variable for font index + uint fontIndex = glyphIndex; + + // get font data + mat4 fontData = glyphTransforms.matrices[fontIndex]; + + // Generate UV coords and vertex positions + Out.uv = CreateQuad(gl_VertexIndex); + vec3 vertexPos = vec3(Out.uv, 1.0f); + + // Get the local matrices + mat4 localModel = testPushConstant.worldTransform; + + //mat4 uiScale = mat4(1.0f); + //uiScale[0][0] = cameraData.cameraRight / 20.49f; + //uiScale[1][1] = cameraData.cameraTop / 11.323f; + + // transform the UV to atlas space to sample the font bitmap correctly + Out.uv = vec2(mat3(fontData) * vec3(Out.uv, 1.0f)); + + // Matrix to transform the quad from local to font space (for a font to be of correct size) + mat3 toFontSpace = mat3(1.0f); + toFontSpace[0][0] = fontData[3][0]; + toFontSpace[1][1] = fontData[3][1]; + 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; + + // transform the vertex position to font space + gl_Position = PVMatrix * localModel * vec4(vertexPos, 1.0f); } \ No newline at end of file diff --git a/Assets/Shaders/Text_VS.shshaderb b/Assets/Shaders/Text_VS.shshaderb new file mode 100644 index 00000000..c74dddbe Binary files /dev/null and b/Assets/Shaders/Text_VS.shshaderb differ diff --git a/Assets/Shaders/Text_VS.shshaderb.shmeta b/Assets/Shaders/Text_VS.shshaderb.shmeta new file mode 100644 index 00000000..17df2e98 --- /dev/null +++ b/Assets/Shaders/Text_VS.shshaderb.shmeta @@ -0,0 +1,3 @@ +Name: Text_VS +ID: 39816727 +Type: 2 diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 26befb28..f7656e66 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -116,6 +116,9 @@ namespace SHADE // Create generic command buffer graphicsCmdPool = device->CreateCommandPool(SH_QUEUE_FAMILY_ARRAY_INDEX::GRAPHICS, SH_CMD_POOL_RESET::POOL_BASED, true); + SHAssetManager::CompileAsset("../../Assets/Shaders/Text_VS.glsl"); + SHAssetManager::CompileAsset("../../Assets/Shaders/Text_FS.glsl"); + // Load Built In Shaders static constexpr AssetID VS_DEFAULT = 39210065; defaultVertShader = SHResourceManager::LoadOrGet(VS_DEFAULT); static constexpr AssetID FS_DEFAULT = 46377769; defaultFragShader = SHResourceManager::LoadOrGet(FS_DEFAULT); @@ -123,7 +126,8 @@ namespace SHADE static constexpr AssetID FS_DEBUG = 36671027; debugFragShader = SHResourceManager::LoadOrGet(FS_DEBUG); static constexpr AssetID CS_COMPOSITE = 45072428; deferredCompositeShader = SHResourceManager::LoadOrGet(CS_COMPOSITE); static constexpr AssetID SSAO = 38430899; ssaoShader = SHResourceManager::LoadOrGet(SSAO); - static constexpr AssetID SSAO_BLUR = 39760835; ssaoBlurShader = SHResourceManager::LoadOrGet(SSAO_BLUR); + static constexpr AssetID TEXT_VS = 39816727; textVS = SHResourceManager::LoadOrGet(TEXT_VS); + static constexpr AssetID TEXT_FS = 38024754; textFS = SHResourceManager::LoadOrGet(TEXT_FS); } void SHGraphicsSystem::InitSceneRenderGraph(void) noexcept diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index a5a5ada0..a550ea22 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -412,6 +412,8 @@ namespace SHADE Handle deferredCompositeShader; Handle ssaoShader; Handle ssaoBlurShader; + Handle textVS; + Handle textFS; // Built-In Materials diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp index 0db0b785..1e1551d5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp @@ -6,6 +6,7 @@ #include "Graphics/Devices/SHVkLogicalDevice.h" #include "Graphics/MiddleEnd/TextRendering/SHFont.h" #include "Graphics/Buffers/SHVkBuffer.h" +#include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h" namespace SHADE { @@ -86,11 +87,21 @@ namespace SHADE } - void SHTextRenderingSubSystem::Init(Handle device, Handle descPool) noexcept + void SHTextRenderingSubSystem::Init(Handle device, Handle compatibleRenderpass, Handle subpass, Handle descPool, Handle textVS, Handle textFS) noexcept { logicalDevice = device; - + // prepare pipeline layout params + SHPipelineLayoutParams plParams + { + .shaderModules = {textVS, textFS}, + .globalDescSetLayouts = SHGraphicsGlobalData::GetDescSetLayouts() + }; + + pipelineLayout = logicalDevice->CreatePipelineLayout(plParams); + + // Create pipeline + pipeline = logicalDevice->CreateGraphicsPipeline(pipelineLayout, nullptr, compatibleRenderpass, subpass); } void SHTextRenderingSubSystem::Run(uint32_t frameIndex) noexcept diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h index 271d3b99..26db3046 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h @@ -14,6 +14,9 @@ namespace SHADE class SHTextRendererComponent; class SHVkPipeline; class SHVkPipelineLayout; + class SHVkRenderpass; + class SHSubpass; + class SHVkShaderModule; class SHTextRenderingSubSystem { @@ -32,7 +35,7 @@ namespace SHADE void RecomputePositions(SHTextRendererComponent& textComp) noexcept; public: - void Init(Handle device, Handle descPool) noexcept; + void Init(Handle device, Handle compatibleRenderpass, Handle subpass, Handle descPool, Handle textVS, Handle textFS) noexcept; void Run(uint32_t frameIndex) noexcept; void Render (void) noexcept; void Exit(void) noexcept;