diff --git a/Assets/Fonts/SegoeUI.shfont b/Assets/Fonts/SegoeUI.shfont new file mode 100644 index 00000000..3f709c2a Binary files /dev/null and b/Assets/Fonts/SegoeUI.shfont differ diff --git a/Assets/Fonts/SegoeUI.shfont.shmeta b/Assets/Fonts/SegoeUI.shfont.shmeta new file mode 100644 index 00000000..33ee953d --- /dev/null +++ b/Assets/Fonts/SegoeUI.shfont.shmeta @@ -0,0 +1,3 @@ +Name: SegoeUI +ID: 176667660 +Type: 10 diff --git a/Assets/Fonts/SegoeUI.ttf b/Assets/Fonts/SegoeUI.ttf new file mode 100644 index 00000000..46b3b993 Binary files /dev/null and b/Assets/Fonts/SegoeUI.ttf differ diff --git a/Assets/Materials/UIMat.shmat b/Assets/Materials/UIMat.shmat new file mode 100644 index 00000000..b31fed2e --- /dev/null +++ b/Assets/Materials/UIMat.shmat @@ -0,0 +1,8 @@ +- VertexShader: 46580970 + FragmentShader: 35983630 + SubPass: G-Buffer Write + Properties: + data.color: {x: 1, y: 1, z: 1, w: 1} + data.textureIndex: 0 + data.alpha: 0 + data.beta: {x: 1, y: 1, z: 1} \ No newline at end of file diff --git a/Assets/Materials/UIMat.shmat.shmeta b/Assets/Materials/UIMat.shmat.shmeta new file mode 100644 index 00000000..2a2d2572 --- /dev/null +++ b/Assets/Materials/UIMat.shmat.shmeta @@ -0,0 +1,3 @@ +Name: UIMat +ID: 129340704 +Type: 7 diff --git a/Assets/Models/Quad.gltf b/Assets/Models/Quad.gltf new file mode 100644 index 00000000..493c164d --- /dev/null +++ b/Assets/Models/Quad.gltf @@ -0,0 +1,104 @@ +{ + "asset" : { + "generator" : "Khronos glTF Blender I/O v3.3.27", + "version" : "2.0" + }, + "scene" : 0, + "scenes" : [ + { + "name" : "Scene", + "nodes" : [ + 0 + ] + } + ], + "nodes" : [ + { + "mesh" : 0, + "name" : "Quad" + } + ], + "meshes" : [ + { + "name" : "Plane", + "primitives" : [ + { + "attributes" : { + "POSITION" : 0, + "NORMAL" : 1, + "TEXCOORD_0" : 2 + }, + "indices" : 3 + } + ] + } + ], + "accessors" : [ + { + "bufferView" : 0, + "componentType" : 5126, + "count" : 4, + "max" : [ + 0.5, + 0.5, + 3.774895063202166e-08 + ], + "min" : [ + -0.5, + -0.5, + -3.774895063202166e-08 + ], + "type" : "VEC3" + }, + { + "bufferView" : 1, + "componentType" : 5126, + "count" : 4, + "type" : "VEC3" + }, + { + "bufferView" : 2, + "componentType" : 5126, + "count" : 4, + "type" : "VEC2" + }, + { + "bufferView" : 3, + "componentType" : 5123, + "count" : 6, + "type" : "SCALAR" + } + ], + "bufferViews" : [ + { + "buffer" : 0, + "byteLength" : 48, + "byteOffset" : 0, + "target" : 34962 + }, + { + "buffer" : 0, + "byteLength" : 48, + "byteOffset" : 48, + "target" : 34962 + }, + { + "buffer" : 0, + "byteLength" : 32, + "byteOffset" : 96, + "target" : 34962 + }, + { + "buffer" : 0, + "byteLength" : 12, + "byteOffset" : 128, + "target" : 34963 + } + ], + "buffers" : [ + { + "byteLength" : 140, + "uri" : "data:application/octet-stream;base64,AAAAvwAAAL9pISIzAAAAPwAAAL9pISIzAAAAvwAAAD9pISKzAAAAPwAAAD9pISKzAAAAAGkhojMAAIA/AAAAAGkhojMAAIA/AAAAAGkhojMAAIA/AAAAAGkhojMAAIA/AAAAAAAAgD8AAIA/AACAPwAAAAAAAAAAAACAPwAAAAAAAAEAAwAAAAMAAgA=" + } + ] +} diff --git a/Assets/Models/Quad.shmodel b/Assets/Models/Quad.shmodel new file mode 100644 index 00000000..aebbb0a6 Binary files /dev/null and b/Assets/Models/Quad.shmodel differ diff --git a/Assets/Models/Quad.shmodel.shmeta b/Assets/Models/Quad.shmodel.shmeta new file mode 100644 index 00000000..9fbd4b9c --- /dev/null +++ b/Assets/Models/Quad.shmodel.shmeta @@ -0,0 +1,7 @@ +Name: Quad +ID: 80501355 +Type: 4 +Sub Assets: +Name: Quad +ID: 141771688 +Type: 8 diff --git a/Assets/Shaders/DebugDraw_VS.glsl b/Assets/Shaders/DebugDraw_VS.glsl index cb0886d1..7b370730 100644 --- a/Assets/Shaders/DebugDraw_VS.glsl +++ b/Assets/Shaders/DebugDraw_VS.glsl @@ -15,6 +15,9 @@ layout(set = 2, binding = 0) uniform CameraData { vec4 position; mat4 vpMat; + mat4 viewMat; + mat4 perspectiveMat; + mat4 orthoMat; } cameraData; void main() diff --git a/Assets/Shaders/TestCube_VS.glsl b/Assets/Shaders/TestCube_VS.glsl index 0e055395..774bc580 100644 --- a/Assets/Shaders/TestCube_VS.glsl +++ b/Assets/Shaders/TestCube_VS.glsl @@ -34,7 +34,8 @@ layout(set = 2, binding = 0) uniform CameraData vec4 position; mat4 vpMat; mat4 viewMat; - mat4 projMat; + mat4 perspectiveMat; + mat4 orthoMat; } cameraData; void main() diff --git a/Assets/Shaders/TestCube_VS.shshaderb b/Assets/Shaders/TestCube_VS.shshaderb index fb282b4d..28c4655b 100644 Binary files a/Assets/Shaders/TestCube_VS.shshaderb and b/Assets/Shaders/TestCube_VS.shshaderb differ diff --git a/Assets/Shaders/Text_FS.glsl b/Assets/Shaders/Text_FS.glsl new file mode 100644 index 00000000..87e29de8 --- /dev/null +++ b/Assets/Shaders/Text_FS.glsl @@ -0,0 +1,60 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable +#extension GL_EXT_nonuniform_qualifier : require + +struct MatPropData +{ + vec4 color; + int textureIndex; + float alpha; + vec3 beta; +}; + +layout(location = 0) in struct +{ + vec4 vertPos; // location 0 + vec2 uv; // location = 1 + vec4 normal; // location = 2 + +} In; + +// material stuff +layout(location = 3) flat in struct +{ + uint eid; + vec3 textColor; +} In2; + + + +layout(set = 4, binding = 0) uniform sampler2D fontBitmap; + +layout(location = 0) out vec4 color; +layout(location = 1) out uint outEntityID; + +float median(float r, float g, float b) +{ + return max(min(r, g), min(max(r, g), b)); +} + +void main() +{ + 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(In2.textColor, 1.0f), min (opacity, 1.0f)); + + + color = fragColor; + outEntityID = In2.eid; +} \ 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..96c8c587 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 new file mode 100644 index 00000000..6263aba6 --- /dev/null +++ b/Assets/Shaders/Text_VS.glsl @@ -0,0 +1,102 @@ +#version 450 +#extension GL_KHR_vulkan_glsl : enable + +//#include "ShaderDescriptorDefinitions.glsl" + + +// vertex inputs +layout(location = 0) in vec4 positionalOffset; +layout(location = 1) in uint glyphIndex; + +// between shader stages +layout(location = 0) out struct +{ + vec4 vertPos; // location 0 + vec2 uv; // location = 1 + vec4 normal; // location = 2 + +} Out; + +// material stuff +layout(location = 3) out struct +{ + uint eid; + vec3 textColor; +} Out2; + +// Camera data +layout(set = 2, binding = 0) uniform CameraData +{ + vec4 position; + mat4 vpMat; + mat4 viewMat; + mat4 perspectiveMat; + mat4 orthoMat; +} cameraData; + +// push constants +layout(std140, push_constant) uniform TestPushConstant +{ + mat4 worldTransform; + uint eid; + vec3 textColor; + +} testPushConstant; + +// Descriptor sets +layout(std430, set = 4, 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 = testPushConstant.eid; + + // 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; + + Out2.textColor = testPushConstant.textColor; + + // 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..3f612654 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/Assets/Shaders/ToSwapchain_FS.glsl b/Assets/Shaders/ToSwapchain_FS.glsl new file mode 100644 index 00000000..9a35d1c4 --- /dev/null +++ b/Assets/Shaders/ToSwapchain_FS.glsl @@ -0,0 +1,19 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable +#extension GL_EXT_nonuniform_qualifier : require + +layout (input_attachment_index = 0, set = 4, binding = 0) uniform subpassInput vec4 sceneTexture; + +layout(location = 0) in struct +{ + vec2 uv; // location = 0 + +} In; + +layout(location = 0) out vec4 fragColor; + +void main() +{ + fragColor = vec4 (texture (sceneTexture, In.uv).rgb, 1.0f); +} \ No newline at end of file diff --git a/Assets/Shaders/ToSwapchain_VS.glsl b/Assets/Shaders/ToSwapchain_VS.glsl new file mode 100644 index 00000000..cc3e2c28 --- /dev/null +++ b/Assets/Shaders/ToSwapchain_VS.glsl @@ -0,0 +1,21 @@ +#version 450 +#extension GL_KHR_vulkan_glsl : enable + +layout(location = 0) out struct +{ + vec2 uv; // location = 0 + +} Out; + +vec2 CreateQuad(in uint vertexID) +{ + uint b = 1 << vertexID; + return vec2 ((0x3 & b) != 0, (0x9 & b) != 0); +} + +void main() +{ + vec2 texCoord = CreateQuad (gl_VertexIndex); + vec2 vertexPos = texCoord - vec2(0.5f); + gl_Position = vec4 (vertexPos, 0.0f, 1.0f); +} \ No newline at end of file diff --git a/Assets/Shaders/UI_FS.glsl b/Assets/Shaders/UI_FS.glsl new file mode 100644 index 00000000..4f919a04 --- /dev/null +++ b/Assets/Shaders/UI_FS.glsl @@ -0,0 +1,50 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable +#extension GL_EXT_nonuniform_qualifier : require + +struct MatPropData +{ + vec4 color; + int textureIndex; + float alpha; + vec3 beta; +}; + +layout(location = 0) in struct +{ + vec4 vertPos; // location 0 + vec2 uv; // location = 1 + vec4 normal; // location = 2 + +} In; + +// 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 +{ + MatPropData data[]; +} MatProp; + +layout(location = 0) out vec4 position; +layout(location = 1) out uint outEntityID; +layout(location = 2) out uint lightLayerIndices; +layout(location = 3) out vec4 normals; +layout(location = 4) out vec4 albedo; + +void main() +{ + position = In.vertPos; + normals = In.normal; + albedo = vec4(1.0f); + + outEntityID = In2.eid; + lightLayerIndices = In2.lightLayerIndex; +} \ No newline at end of file diff --git a/Assets/Shaders/UI_FS.shshaderb b/Assets/Shaders/UI_FS.shshaderb new file mode 100644 index 00000000..6be9c565 Binary files /dev/null and b/Assets/Shaders/UI_FS.shshaderb differ diff --git a/Assets/Shaders/UI_FS.shshaderb.shmeta b/Assets/Shaders/UI_FS.shshaderb.shmeta new file mode 100644 index 00000000..80bb5dad --- /dev/null +++ b/Assets/Shaders/UI_FS.shshaderb.shmeta @@ -0,0 +1,3 @@ +Name: UI_FS +ID: 35983630 +Type: 2 diff --git a/Assets/Shaders/UI_VS.glsl b/Assets/Shaders/UI_VS.glsl new file mode 100644 index 00000000..c4393b98 --- /dev/null +++ b/Assets/Shaders/UI_VS.glsl @@ -0,0 +1,66 @@ +#version 450 +#extension GL_KHR_vulkan_glsl : enable + +//#include "ShaderDescriptorDefinitions.glsl" + + +layout(location = 0) in vec3 aVertexPos; +layout(location = 1) in vec2 aUV; +layout(location = 2) in vec3 aNormal; +layout(location = 3) in vec3 aTangent; +layout(location = 4) in mat4 worldTransform; +layout(location = 8) in uvec2 integerData; + + +layout(location = 0) out struct +{ + vec4 vertPos; // location 0 + vec2 uv; // location = 1 + vec4 normal; // location = 2 + +} Out; + +// material stuff +layout(location = 3) out struct +{ + int materialIndex; + uint eid; + uint lightLayerIndex; + +} Out2; + +layout(set = 2, binding = 0) uniform CameraData +{ + vec4 position; + mat4 vpMat; + mat4 viewMat; + mat4 perspectiveMat; + mat4 orthoMat; +} cameraData; + +void main() +{ + Out2.materialIndex = gl_InstanceIndex; + Out2.eid = integerData[0]; + Out2.lightLayerIndex = integerData[1]; + + // for transforming gBuffer position and normal data + mat4 modelViewMat = cameraData.viewMat * worldTransform; + + // gBuffer position will be in view space + Out.vertPos = modelViewMat * vec4(aVertexPos, 1.0f); + + // uvs for texturing in fragment shader + Out.uv = aUV; + + mat3 transposeInv = mat3 (transpose(inverse(modelViewMat))); + + // normals are also in view space + Out.normal.rgb = transposeInv * aNormal.rgb; + Out.normal.rgb = normalize (Out.normal.rgb); + + // clip space for rendering + gl_Position = cameraData.orthoMat * worldTransform * vec4 (aVertexPos, 1.0f); + gl_Position.z += 0.1f; // HAX + // gl_Position = vec4 (aVertexPos, 1.0f); +} \ No newline at end of file diff --git a/Assets/Shaders/UI_VS.shshaderb b/Assets/Shaders/UI_VS.shshaderb new file mode 100644 index 00000000..a1f1a8b7 Binary files /dev/null and b/Assets/Shaders/UI_VS.shshaderb differ diff --git a/Assets/Shaders/UI_VS.shshaderb.shmeta b/Assets/Shaders/UI_VS.shshaderb.shmeta new file mode 100644 index 00000000..08fa8435 --- /dev/null +++ b/Assets/Shaders/UI_VS.shshaderb.shmeta @@ -0,0 +1,3 @@ +Name: UI_VS +ID: 46580970 +Type: 2 diff --git a/Dependencies.bat b/Dependencies.bat index 44185ff8..ba411815 100644 --- a/Dependencies.bat +++ b/Dependencies.bat @@ -6,7 +6,7 @@ echo --------------------------------------------------- echo "SHADE DEPENDENCIES (Default - All in 10 Seconds)" echo "A - All" echo "B - VMA" -echo "C - msdf" +echo "C - FontCompiler" echo "D - ModelCompiler" echo "E - spdlog" echo "F - reactphysics3d" @@ -28,7 +28,7 @@ set _e=%ERRORLEVEL% if %_e%==1 goto VMA if %_e%==2 goto VMA -if %_e%==3 goto MSDF +if %_e%==3 goto FontCompiler if %_e%==4 goto ModelCompiler if %_e%==5 goto spdlog if %_e%==6 goto reactphysics3d @@ -47,12 +47,13 @@ if %_e%==16 goto fmod echo -----------------------VMA---------------------------- rmdir "Dependencies/VMA" /S /Q git clone https://github.com/SHADE-DP/VulkanMemoryAllocator.git "Dependencies/VMA" -if %_e%==2 (goto :done) else (goto :MSDF) +if %_e%==2 (goto :done) else (goto :FontCompiler) -:MSDF -echo -----------------------MSDF---------------------------- -rmdir "Dependencies/msdf" /S /Q -git clone --recurse-submodules https://github.com/SHADE-DP/msdf-atlas-gen.git "Dependencies/msdf" +:FontCompiler +echo -----------------------FontCompiler---------------------------- +rmdir "Dependencies/FontCompiler" /S /Q +git clone https://github.com/SHADE-DP/FontCompiler.git "Dependencies/FontCompiler" +git clone --recurse-submodules https://github.com/SHADE-DP/msdf-atlas-gen "Dependencies/FontCompiler/Dependencies/msdf" if %_e%==3 (goto :done) else (goto :ModelCompiler) :ModelCompiler diff --git a/Dependencies.lua b/Dependencies.lua index 2e24222b..fe75c3f4 100644 --- a/Dependencies.lua +++ b/Dependencies.lua @@ -3,8 +3,9 @@ IncludeDir["ModelCompiler"] = "%{wks.location}\\Dependencies\\ModelCompiler" IncludeDir["imgui"] = "%{wks.location}\\Dependencies\\imgui" IncludeDir["imguizmo"] = "%{wks.location}\\Dependencies\\imguizmo" IncludeDir["imnodes"] = "%{wks.location}\\Dependencies\\imnodes" -IncludeDir["msdf_atlas_gen"] = "%{wks.location}\\Dependencies\\msdf" -IncludeDir["msdfgen"] = "%{wks.location}\\Dependencies\\msdf\\msdfgen" +IncludeDir["FontCompiler"] = "%{wks.location}\\Dependencies\\FontCompiler" +IncludeDir["msdf_atlas_gen"] = "%{wks.location}\\Dependencies\\FontCompiler\\Dependencies\\msdf" +IncludeDir["msdfgen"] = "%{wks.location}\\Dependencies\\FontCompiler\\Dependencies\\msdf\\msdfgen" IncludeDir["spdlog"] = "%{wks.location}\\Dependencies\\spdlog" IncludeDir["tracy"] = "%{wks.location}\\Dependencies\\tracy" IncludeDir["VMA"] = "%{wks.location}\\Dependencies\\VMA" diff --git a/SHADE_Application/premake5.lua b/SHADE_Application/premake5.lua index cba0c35e..faa741a6 100644 --- a/SHADE_Application/premake5.lua +++ b/SHADE_Application/premake5.lua @@ -38,7 +38,9 @@ project "SHADE_Application" "%{IncludeDir.spdlog}/include", "%{IncludeDir.tinyddsloader}", "%{IncludeDir.reactphysics3d}\\include", - "%{IncludeDir.yamlcpp}" + "%{IncludeDir.yamlcpp}", + "%{IncludeDir.msdf_atlas_gen}", + "%{IncludeDir.msdfgen}" } externalwarnings "Off" diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index cf5a9277..e80b247b 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -80,6 +80,7 @@ namespace Sandbox SHSystemManager::CreateSystem(); SHSystemManager::CreateSystem(); + std::system("FontCompiler.exe ../../Assets/Fonts/SegoeUI.ttf"); SHSystemManager::CreateSystem(); SHGraphicsSystem* graphicsSystem = static_cast(SHSystemManager::GetSystem()); @@ -140,7 +141,7 @@ namespace Sandbox //SHComponentManager::CreateComponentSparseSet(); SHAssetManager::Load(); - + auto font = SHAssetManager::GetData(176667660); SHSystemManager::RegisterRoutine(); @@ -160,19 +161,24 @@ namespace Sandbox void SBApplication::Update(void) { SHGraphicsSystem* graphicsSystem = SHADE::SHSystemManager::GetSystem(); + +#ifdef SHEDITOR SHEditor* editor = SHADE::SHSystemManager::GetSystem(); +#endif + //TODO: Change true to window is open while (!window.WindowShouldClose()) { SHFrameRateController::UpdateFRC(); SHInputManager::UpdateInput(SHFrameRateController::GetRawDeltaTime()); SHSceneManager::UpdateSceneManager(); + #ifdef SHEDITOR if(editor->editorState == SHEditor::State::PLAY) SHSceneManager::SceneUpdate(0.016f); -#endif SHSystemManager::RunRoutines(editor->editorState != SHEditor::State::PLAY, 0.016f); editor->PollPicking(); +#endif // TODO: Move into an Editor menu static bool drawColliders = false; diff --git a/SHADE_Application/src/Scenes/SBMainScene.cpp b/SHADE_Application/src/Scenes/SBMainScene.cpp index b14f2e6f..9da68729 100644 --- a/SHADE_Application/src/Scenes/SBMainScene.cpp +++ b/SHADE_Application/src/Scenes/SBMainScene.cpp @@ -13,6 +13,7 @@ #include "Physics/Interface/SHRigidBodyComponent.h" #include "Physics/Interface/SHColliderComponent.h" #include "Graphics/MiddleEnd/Lights/SHLightComponent.h" +#include "Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.h" #include "Assets/SHAssetManager.h" #include "Camera/SHCameraComponent.h" @@ -41,6 +42,19 @@ namespace Sandbox void SBMainScene::Init() { sceneName = SHSerialization::DeserializeSceneFromFile(sceneAssetID); + + /*-----------------------------------------------------------------------*/ + /* TESTING CODE */ + /*-----------------------------------------------------------------------*/ + //testText = SHEntityManager::CreateEntity(MAX_EID, "Test Text"); + //auto gfxSystem =SHSystemManager::GetSystem(); + + //auto textComp = SHComponentManager::GetComponent(testText); + + //textComp->SetFont(gfxSystem->GetFontLibrary().GetFonts()[0]); + /*-----------------------------------------------------------------------*/ + /* TESTING CODE */ + /*-----------------------------------------------------------------------*/ } void SBMainScene::Update(float dt) diff --git a/SHADE_Application/src/Scenes/SBMainScene.h b/SHADE_Application/src/Scenes/SBMainScene.h index 7bd10118..18d43e33 100644 --- a/SHADE_Application/src/Scenes/SBMainScene.h +++ b/SHADE_Application/src/Scenes/SBMainScene.h @@ -12,6 +12,8 @@ namespace Sandbox EntityID testObj; std::vector stressTestObjects; + EntityID testText; + public: virtual void Load(); virtual void Init(); diff --git a/SHADE_Engine/premake5.lua b/SHADE_Engine/premake5.lua index 1d6d7bef..88df50d7 100644 --- a/SHADE_Engine/premake5.lua +++ b/SHADE_Engine/premake5.lua @@ -119,6 +119,7 @@ project "SHADE_Engine" { "xcopy /r /y /q \"%{IncludeDir.ModelCompiler}\\bin\\Debug\\assimp-vc142-mtd.dll\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.ModelCompiler}\\bin\\Debug\\ModelCompiler.exe\" \"$(OutDir)\"", + "xcopy /r /y /q \"%{IncludeDir.FontCompiler}\\bin\\Debug\\FontCompiler.exe\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmodL.dll\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmodstudioL.dll\" \"$(OutDir)\"" } @@ -128,6 +129,7 @@ project "SHADE_Engine" { "xcopy /r /y /q \"%{IncludeDir.ModelCompiler}\\bin\\Release\\assimp-vc142-mt.dll\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.ModelCompiler}\\bin\\Release\\ModelCompiler.exe\" \"$(OutDir)\"", + "xcopy /r /y /q \"%{IncludeDir.FontCompiler}\\bin\\Release\\FontCompiler.exe\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmod.dll\" \"$(OutDir)\"", "xcopy /r /y /q \"%{IncludeDir.fmod}\\lib\\fmodstudio.dll\" \"$(OutDir)\"" } diff --git a/SHADE_Engine/src/Assets/Asset Types/SHFontAsset.h b/SHADE_Engine/src/Assets/Asset Types/SHFontAsset.h new file mode 100644 index 00000000..8d2cb67a --- /dev/null +++ b/SHADE_Engine/src/Assets/Asset Types/SHFontAsset.h @@ -0,0 +1,47 @@ +#pragma once + +#include "SHAssetData.h" +#include "SH_API.h" +#include +#include +#include "msdf-atlas-gen/msdf-atlas-gen.h" +#include "math/SHMatrix.h" + +namespace SHADE +{ + struct SH_API SHFontAsset : SHAssetData + { + using GlyphData = SHMatrix; + static constexpr uint32_t NUM_CHANNELS = 3; + static constexpr uint32_t BYTES_PER_CHANNEL = 3; + static constexpr uint8_t BASELINE_LEFT_MATRIX_INDEX_ROW = 3; + static constexpr uint8_t BASELINE_LEFT_MATRIX_INDEX_COL = 2; + static constexpr uint8_t BASELINE_BOTTOM_MATRIX_INDEX_ROW = 3; + static constexpr uint8_t BASELINE_BOTTOM_MATRIX_INDEX_COL = 3; + + /*-----------------------------------------------------------------------*/ + /* MEMBER VARIABLES */ + /*-----------------------------------------------------------------------*/ + //! Name of the shader file (without parent path) + std::string fontName; + + //! Glyphs. Corresponds to the transformation container below. + std::vector glyphs; + + //! Data containing character and uv transformation data and other misc data + std::vector glyphTransformations; + + //! The actual data of the atlas to go into the binary. Was unique_ptr, vector easier to copy. + std::vector bitmapData; + + //! Width of the bitmap + uint32_t bitmapWidth; + + //! Height of the bitmap + uint32_t bitmapHeight; + + //! Font geometry required to get kerning from + msdf_atlas::FontGeometry fontGeometry; + + }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.cpp b/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.cpp new file mode 100644 index 00000000..9bbd3a71 --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.cpp @@ -0,0 +1,75 @@ +#include "SHpch.h" +#include "SHFontLoader.h" +#include "Assets/Asset Types/SHFontAsset.h" +#include "Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.h" +#include "msdf-atlas-gen/msdf-atlas-gen.h" + +#include +#include +#include +#include + +namespace SHADE +{ + SHADE::SHAssetData* SHFontLoader::Load(AssetPath path) + { + auto newFontAsset = new SHFontAsset(); + + std::ifstream file{ path.string(), std::ios::in | std::ios::binary }; + if (!file.is_open()) + { + SHLOG_ERROR("Unable to open compiled font file: {}", path.string()); + return nullptr; + } + + uint32_t numGlyphs = 0; + + // read how many glyphs we have + file.read (reinterpret_cast(&numGlyphs), sizeof (uint32_t)); + + newFontAsset->glyphs.resize(numGlyphs); + newFontAsset->glyphTransformations.resize(numGlyphs); + + for (uint32_t i = 0; i < numGlyphs; ++i) + { + // Read the data for the glyph + file.read(reinterpret_cast(&newFontAsset->glyphs[i]), sizeof(msdf_atlas::unicode_t)); + } + + for (uint32_t i = 0; i < numGlyphs; ++i) + { + // Read the data for the glyph transformations + file.read(reinterpret_cast(&newFontAsset->glyphTransformations[i]), sizeof(SHMatrix)); + } + + // read the width + file.read(reinterpret_cast(&newFontAsset->bitmapWidth), sizeof(SHFontAsset::bitmapWidth)); + + // read the height + file.read(reinterpret_cast(&newFontAsset->bitmapHeight), sizeof(SHFontAsset::bitmapHeight)); + + uint32_t bytesRequired = 0; + file.read(reinterpret_cast(&bytesRequired), sizeof(uint32_t)); + + // Read the bitmap + newFontAsset->bitmapData.resize(bytesRequired); + file.read(reinterpret_cast(newFontAsset->bitmapData.data()), bytesRequired); + + + file.close(); + + return newFontAsset; + } + + void SHFontLoader::Write(SHAssetData const* data, AssetPath path) + { + /* + Stuff to write to binary file : + - Interleaved per character data + - codepoint (actual character) + - 4x4 Matrix data (stores UV transform + extra data) + - Actual atlas texture + */ + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.h b/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.h new file mode 100644 index 00000000..7221d2f2 --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/Loaders/SHFontLoader.h @@ -0,0 +1,24 @@ +/*************************************************************************//** + * \file SHSFontLoader.h + * \author Loh Xiao Qi + * \date 23 10 2022 + * \brief + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ + +#pragma once + +#include "Assets/Libraries/Loaders/SHAssetLoader.h" + +namespace SHADE +{ + struct SHFontLoader : SHAssetLoader + { + + SHAssetData* Load(AssetPath path) override; + void Write(SHAssetData const* data, AssetPath path) override; + }; +} diff --git a/SHADE_Engine/src/Assets/SHAssetMacros.h b/SHADE_Engine/src/Assets/SHAssetMacros.h index 44dfd5c5..5ab1945d 100644 --- a/SHADE_Engine/src/Assets/SHAssetMacros.h +++ b/SHADE_Engine/src/Assets/SHAssetMacros.h @@ -67,8 +67,9 @@ constexpr std::string_view ASSET_ROOT {"../../Assets"}; constexpr std::string_view BUILT_IN_ASSET_ROOT{ "../../Built_In" }; #endif -// COMPILER PATHS +// COMPILER EXE constexpr std::string_view MODEL_COMPILER_EXE{ "ModelCompiler.exe" }; +constexpr std::string_view FONT_COMPILER_EXE{ "FontCompiler.exe" }; // INTERNAL ASSET PATHS constexpr std::string_view SCENE_FOLDER{ "/Scenes/" }; @@ -81,14 +82,14 @@ constexpr std::string_view META_EXTENSION {".shmeta"}; constexpr std::string_view AUDIO_EXTENSION {".ogg"}; constexpr std::string_view AUDIO_WAV_EXTENSION {".wav"}; constexpr std::string_view SHADER_EXTENSION{ ".shshader" }; -constexpr std::string_view SHADER_BUILT_IN_EXTENSION{".shshaderb"}; +constexpr std::string_view SHADER_BUILT_IN_EXTENSION{ ".shshaderb" }; +constexpr std::string_view FONT_EXTENSION{ ".shfont" }; constexpr std::string_view SCRIPT_EXTENSION {".cs"}; constexpr std::string_view SCENE_EXTENSION {".shade"}; constexpr std::string_view PREFAB_EXTENSION {".shprefab"}; constexpr std::string_view MATERIAL_EXTENSION {".shmat"}; constexpr std::string_view TEXTURE_EXTENSION {".shtex"}; constexpr std::string_view MODEL_EXTENSION{ ".shmodel" }; -constexpr std::string_view FONT_EXTENSION{ ".shfont" }; constexpr std::string_view EXTENSIONS[] = { AUDIO_EXTENSION, diff --git a/SHADE_Engine/src/Assets/SHAssetManager.cpp b/SHADE_Engine/src/Assets/SHAssetManager.cpp index 1569a13c..7fcb9b36 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.cpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.cpp @@ -20,6 +20,7 @@ #include "Libraries/Loaders/SHTextureLoader.h" #include "Libraries/Loaders/SHShaderSourceLoader.h" #include "Libraries/Loaders/SHTextBasedLoader.h" +#include "Libraries/Loaders/SHFontLoader.h" //#include "Libraries/Compilers/SHMeshCompiler.h" #include "Libraries/Compilers/SHTextureCompiler.h" @@ -392,7 +393,13 @@ namespace SHADE auto const ext{ path.extension().string() }; if (ext == GLSL_EXTENSION.data()) { - newPath = SHShaderSourceCompiler::LoadAndCompileShader(path).value(); + auto value { SHShaderSourceCompiler::LoadAndCompileShader(path) }; + if (!value.has_value()) + { + SHLOG_ERROR("Shader compile failed: {}", path.string()); + return; + } + newPath = value.value(); } else if (ext == GLTF_EXTENSION.data() || ext == FBX_EXTENSION.data()) { @@ -414,6 +421,16 @@ namespace SHADE } newPath = pathGen.value(); } + else if (ext == TTF_EXTENSION.data()) + { + std::string command = FONT_COMPILER_EXE.data(); + command += " " + path.string(); + std::system(command.c_str()); + + std::string fontPath = path.string().substr(0, path.string().find_last_of('.')); + fontPath += FONT_EXTENSION; + newPath = fontPath; + } else { SHLOG_WARNING("File Type compilation not yet Implemented: {}", path.string()); @@ -466,6 +483,8 @@ namespace SHADE loaders[static_cast(AssetType::PREFAB)] = loaders[static_cast(AssetType::SCENE)]; loaders[static_cast(AssetType::MATERIAL)] = loaders[static_cast(AssetType::SCENE)]; loaders[static_cast(AssetType::MESH)] = nullptr; + loaders[static_cast(AssetType::SCRIPT)] = nullptr; + loaders[static_cast(AssetType::FONT)] = dynamic_cast(new SHFontLoader()); } /**************************************************************************** diff --git a/SHADE_Engine/src/Camera/SHCameraSystem.cpp b/SHADE_Engine/src/Camera/SHCameraSystem.cpp index 78dabc37..489b05a1 100644 --- a/SHADE_Engine/src/Camera/SHCameraSystem.cpp +++ b/SHADE_Engine/src/Camera/SHCameraSystem.cpp @@ -246,7 +246,7 @@ namespace SHADE camera.orthoProjMatrix(2, 3) = -n / (f-n); camera.orthoProjMatrix(3, 3) = 1.0f; - camera.orthoProjMatrix = SHMatrix::OrthographicRH(camera.GetWidth(), camera.GetHeight(), camera.GetNear(), camera.GetFar()); + //camera.orthoProjMatrix = SHMatrix::OrthographicRH(camera.GetWidth(), camera.GetHeight(), camera.GetNear(), camera.GetFar()); //camera.projMatrix.Transpose(); camera.dirtyProj = false; diff --git a/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.hpp b/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.hpp index f111eaeb..50930e4f 100644 --- a/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.hpp +++ b/SHADE_Engine/src/Editor/DragDrop/SHDragDrop.hpp @@ -21,7 +21,7 @@ namespace SHADE template static bool SetPayload(DragDropTag const& type, T* object, ImGuiCond const cond = 0) { - ImGui::SetDragDropPayload(type.data(), static_cast(object), sizeof(T), cond); + hasDragDrop = ImGui::SetDragDropPayload(type.data(), static_cast(object), sizeof(T), cond); hasDragDrop = true; currentDragDropTag = type; diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index 46ffd3bf..ab844b88 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -264,14 +264,19 @@ namespace SHADE SHEditorWidgets::BeginPanel(std::format("{} Constraints", ICON_FA_LOCK).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); SHEditorWidgets::TextLabel("Freeze Position"); + ImGui::PushID("FreezePos"); SHEditorWidgets::CheckBox("X", [component] {return component->GetFreezePositionX(); }, [component](bool const& value) {component->SetFreezePositionX(value); }, "Freeze Position - X"); ImGui::SameLine(); SHEditorWidgets::CheckBox("Y", [component] {return component->GetFreezePositionY(); }, [component](bool const& value) {component->SetFreezePositionY(value); }, "Freeze Position - Y"); ImGui::SameLine(); SHEditorWidgets::CheckBox("Z", [component] {return component->GetFreezePositionZ(); }, [component](bool const& value) {component->SetFreezePositionZ(value); }, "Freeze Position - Z"); + ImGui::PopID(); + SHEditorWidgets::TextLabel("Freeze Rotation"); + ImGui::PushID("FreezeRot"); SHEditorWidgets::CheckBox("X", [component] {return component->GetFreezeRotationX(); }, [component](bool const& value) {component->SetFreezeRotationX(value); }, "Freeze Rotation - X"); ImGui::SameLine(); SHEditorWidgets::CheckBox("Y", [component] {return component->GetFreezeRotationY(); }, [component](bool const& value) {component->SetFreezeRotationY(value); }, "Freeze Rotation - Y"); ImGui::SameLine(); SHEditorWidgets::CheckBox("Z", [component] {return component->GetFreezeRotationZ(); }, [component](bool const& value) {component->SetFreezeRotationZ(value); }, "Freeze Rotation - Z"); + ImGui::PopID(); SHEditorWidgets::EndPanel(); } diff --git a/SHADE_Engine/src/Editor/EditorWindow/MaterialInspector/SHMaterialInspector.cpp b/SHADE_Engine/src/Editor/EditorWindow/MaterialInspector/SHMaterialInspector.cpp index 3ffed8e3..9dbb9542 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/MaterialInspector/SHMaterialInspector.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/MaterialInspector/SHMaterialInspector.cpp @@ -172,11 +172,20 @@ namespace SHADE // Shader bool shaderChanged = false; - const auto* SHADER_INFO = SHAssetManager::GetData(currentMatSpec->fragShader); - const std::string SHADER_NAME = SHADER_INFO ? SHADER_INFO->name : "Unknown Shader"; + const auto* VERT_SHADER_INFO = SHAssetManager::GetData(currentMatSpec->vertexShader); + const std::string VERT_SHADER_NAME = VERT_SHADER_INFO ? VERT_SHADER_INFO->name : "Unknown Shader"; isDirty |= SHEditorWidgets::DragDropReadOnlyField ( - "Fragment Shader", SHADER_NAME.data(), + "Fragment Shader", VERT_SHADER_NAME.data(), + [this]() { return currentMatSpec->vertexShader; }, + [this](const AssetID& id) { currentMatSpec->vertexShader = id; }, + SHDragDrop::DRAG_RESOURCE + ); + const auto* FRAG_SHADER_INFO = SHAssetManager::GetData(currentMatSpec->fragShader); + const std::string FRAG_SHADER_NAME = FRAG_SHADER_INFO ? FRAG_SHADER_INFO->name : "Unknown Shader"; + isDirty |= SHEditorWidgets::DragDropReadOnlyField + ( + "Fragment Shader", FRAG_SHADER_NAME.data(), [this]() { return currentMatSpec->fragShader; }, [this](const AssetID& id) { currentMatSpec->fragShader = id; }, SHDragDrop::DRAG_RESOURCE diff --git a/SHADE_Engine/src/Editor/SHEditor.cpp b/SHADE_Engine/src/Editor/SHEditor.cpp index 7372f227..07caee7a 100644 --- a/SHADE_Engine/src/Editor/SHEditor.cpp +++ b/SHADE_Engine/src/Editor/SHEditor.cpp @@ -516,7 +516,7 @@ namespace SHADE ImGui_ImplVulkan_DestroyFontUploadObjects(); - renderGraph->GetNode("ImGui Node")->GetSubpass("ImGui Draw")->AddExteriorDrawCalls([](Handle& cmd) + renderGraph->GetNode("ImGui Node")->GetSubpass("ImGui Draw")->AddExteriorDrawCalls([](Handle& cmd, uint32_t frameIndex) { cmd->BeginLabeledSegment("ImGui Draw"); ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), cmd->GetVkCommandBuffer()); diff --git a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp index 36108628..8d9c01e6 100644 --- a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp +++ b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.cpp @@ -25,12 +25,9 @@ namespace SHADE { if (cmdBufferHdl && (bufferUsageFlags & vk::BufferUsageFlagBits::eTransferDst)) { - vk::BufferCopy copyRegion - { - .srcOffset = 0, - .dstOffset = 0, - .size = sizeStored, - }; + vk::BufferCopy copyRegion{}; + PrepareBufferCopy(copyRegion); + cmdBufferHdl->GetVkCommandBuffer().copyBuffer(stagingBuffer, vkBuffer, 1, ©Region); } } @@ -54,6 +51,13 @@ namespace SHADE vmaFlushAllocation(vmaAllocator, alloc, srcOffset, dstOffset); } + void SHVkBuffer::PrepareBufferCopy(vk::BufferCopy& bufferCopy) noexcept + { + bufferCopy.srcOffset = 0; + bufferCopy.dstOffset = 0; + bufferCopy.size = sizeStored; + } + vk::Buffer SHVkBuffer::GetVkBuffer(void) const noexcept { return vkBuffer; diff --git a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h index 1119342c..edcc968f 100644 --- a/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h +++ b/SHADE_Engine/src/Graphics/Buffers/SHVkBuffer.h @@ -106,6 +106,7 @@ namespace SHADE void ResizeNoCopy (uint32_t newSize); void ResizeReplace (uint32_t newSize, void* data, uint32_t srcSize); void FlushAllocation (uint32_t srcOffset, uint32_t dstOffset) noexcept; + void PrepareBufferCopy (vk::BufferCopy& bufferCopy) noexcept; /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp index 272a838d..95cf2e91 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.cpp @@ -459,7 +459,7 @@ namespace SHADE return SHVkInstance::GetResourceManager().Create(GetHandle(), &vmaAllocator, w, h, levels, format, usage, create); } - Handle SHVkLogicalDevice::CreateImage(SHImageCreateParams const& imageDetails, unsigned char* data, uint32_t dataSize, std::span inMipOffsets, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags) noexcept + Handle SHVkLogicalDevice::CreateImage(SHImageCreateParams const& imageDetails, unsigned char* data, uint32_t dataSize, std::vector const& inMipOffsets, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags) noexcept { return SHVkInstance::GetResourceManager().Create(GetHandle(), &vmaAllocator, imageDetails, data, dataSize, inMipOffsets, memUsage, allocFlags); } diff --git a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h index 158c20b2..ed09b482 100644 --- a/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h +++ b/SHADE_Engine/src/Graphics/Devices/SHVkLogicalDevice.h @@ -162,7 +162,7 @@ namespace SHADE SHImageCreateParams const& imageDetails, unsigned char* data, uint32_t dataSize, - std::span inMipOffsets, + std::vector const& inMipOffsets, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags ) noexcept; diff --git a/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp b/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp index fa43cd53..53703924 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp +++ b/SHADE_Engine/src/Graphics/Images/SHVkImage.cpp @@ -123,7 +123,7 @@ namespace SHADE SHImageCreateParams const& imageDetails, const unsigned char* data, uint32_t dataSize, - std::span inMipOffsets, + std::vector const& inMipOffsets, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags ) noexcept @@ -311,6 +311,22 @@ namespace SHADE CreateFramebufferImage(); } + void SHVkImage::PrepareImageCopy(std::vector& bufferImageCopy) noexcept + { + for (uint32_t i = 0; i < mipOffsets.size(); ++i) + { + bufferImageCopy[i].bufferOffset = mipOffsets[i]; + bufferImageCopy[i].bufferRowLength = 0; // for padding + bufferImageCopy[i].bufferImageHeight = 0; // for padding + bufferImageCopy[i].imageSubresource.aspectMask = vk::ImageAspectFlagBits::eColor; // TODO: Need to change this to base it off image format. + bufferImageCopy[i].imageSubresource.mipLevel = i; + bufferImageCopy[i].imageSubresource.baseArrayLayer = 0; // TODO: Array textures not supported yet + bufferImageCopy[i].imageSubresource.layerCount = layerCount; + bufferImageCopy[i].imageOffset = vk::Offset3D{ 0,0,0 }; + bufferImageCopy[i].imageExtent = vk::Extent3D{ width >> i, height >> i, 1 }; + } + } + void SHVkImage::LinkWithExteriorImage(vk::Image inVkImage, vk::ImageType type, uint32_t inWidth, uint32_t inHeight, uint32_t inDepth, uint32_t layers, uint8_t levels, vk::Format format, vk::ImageUsageFlags flags) noexcept { vkImage = inVkImage; @@ -405,4 +421,9 @@ namespace SHADE return height; } + uint32_t SHVkImage::GetMipLevels(void) const noexcept + { + return mipLevelCount; + } + } diff --git a/SHADE_Engine/src/Graphics/Images/SHVkImage.h b/SHADE_Engine/src/Graphics/Images/SHVkImage.h index ba459def..98d8d0af 100644 --- a/SHADE_Engine/src/Graphics/Images/SHVkImage.h +++ b/SHADE_Engine/src/Graphics/Images/SHVkImage.h @@ -102,7 +102,7 @@ namespace SHADE vk::Buffer stagingBuffer; //! Mipmap offsets for initializing the vk::BufferImageCopy during transfer to GPU resource - std::span mipOffsets; + std::vector mipOffsets; //! Handle to the device that creates these images Handle device; @@ -125,7 +125,7 @@ namespace SHADE SHImageCreateParams const& imageDetails, const unsigned char* data, uint32_t dataSize, - std::span inMipOffsets, + std::vector const& inMipOffsets, VmaMemoryUsage memUsage, VmaAllocationCreateFlags allocFlags ) noexcept; @@ -142,6 +142,7 @@ namespace SHADE void TransferToDeviceResource (Handle cmdBufferHdl) noexcept; void PrepareImageTransitionInfo (vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::ImageMemoryBarrier& barrier) noexcept; void HandleResizeFramebufferImage(uint32_t newWidth, uint32_t newHeight) noexcept; + void PrepareImageCopy (std::vector& bufferImageCopy) noexcept; /*-----------------------------------------------------------------------*/ /* GETTERS AND SETTERS */ @@ -153,6 +154,7 @@ namespace SHADE vk::Format GetImageFormat (void) const noexcept; uint32_t GetWidth (void) const noexcept; uint32_t GetHeight (void) const noexcept; + uint32_t GetMipLevels (void) const noexcept; }; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp index 2bcad441..9b4b02b0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Batching/SHBatch.cpp @@ -28,6 +28,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h" #include "Graphics/Descriptors/SHVkDescriptorPool.h" #include "Scene/SHSceneManager.h" +#include "UI/SHUIComponent.h" namespace SHADE { @@ -309,7 +310,13 @@ namespace SHADE { if (SHSceneManager::CheckNodeAndComponentsActive(rendId)) { - transformData.emplace_back(transform->GetTRS()); + auto uiComp = SHComponentManager::GetComponent_s(rendId); + if (uiComp) + { + transformData.emplace_back(uiComp->GetMatrix()); + } + else + transformData.emplace_back(transform->GetTRS()); } else { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp index 28333820..d0fbaf2c 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.cpp @@ -97,6 +97,8 @@ namespace SHADE Handle materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE, { materialDataBinding }); SET_VK_OBJ_NAME(logicalDevice, vk::ObjectType::eDescriptorSetLayout, materialDataPerInstanceLayout->GetVkHandle(), "[Descriptor Set Layout] Material Globals"); + + globalDescSetLayouts.push_back(staticGlobalLayout); globalDescSetLayouts.push_back(dynamicGlobalLayout); globalDescSetLayouts.push_back(cameraDataGlobalLayout); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index 60262607..0bfa89a2 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -90,7 +90,7 @@ namespace SHADE auto const& RENDERERS = GFX_SYSTEM->GetDefaultViewport()->GetRenderers(); auto renderGraph = RENDERERS[SHGraphicsConstants::RenderGraphIndices::WORLD]->GetRenderGraph(); auto subPass = renderGraph->GetNode("Debug Draw")->GetSubpass("Debug Draw"); - subPass->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle& cmdBuffer) + subPass->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle& cmdBuffer, uint32_t frameIndex) { // Get Current frame index const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); @@ -106,7 +106,7 @@ namespace SHADE } }); auto subPassWithDepth = renderGraph->GetNode("Debug Draw with Depth")->GetSubpass("Debug Draw with Depth"); - subPassWithDepth->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle& cmdBuffer) + subPassWithDepth->AddExteriorDrawCalls([this, GFX_SYSTEM](Handle& cmdBuffer, uint32_t frameIndex) { // Get Current frame index const uint32_t FRAME_IDX = GFX_SYSTEM->GetCurrentFrameIndex(); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h index 0a67cd9f..e6051841 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsConstants.h @@ -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,13 +77,21 @@ 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. */ /***************************************************************************/ static constexpr uint32_t RENDERGRAPH_NODE_COMPUTE_RESOURCE = 5; + /***************************************************************************/ + /*! + \brief + To store font data. + + */ + /***************************************************************************/ + static constexpr uint32_t FONT_DATA = 4; }; struct DescriptorSetBindings @@ -107,7 +117,7 @@ namespace SHADE /***************************************************************************/ /*! \brief - DescriptorSet binding for combined image sampler data. + DescriptorSet binding for light data. */ /***************************************************************************/ @@ -116,7 +126,7 @@ namespace SHADE /***************************************************************************/ /*! \brief - DescriptorSet binding for lights. + DescriptorSet binding for camera data. */ /***************************************************************************/ @@ -130,6 +140,24 @@ namespace SHADE /***************************************************************************/ static constexpr uint32_t BATCHED_PER_INST_DATA = 0; + /***************************************************************************/ + /*! + \brief + Descriptor set binding for font bitmaps. + + */ + /***************************************************************************/ + static constexpr uint32_t FONT_BITMAP_DATA = 0; + + /***************************************************************************/ + /*! + \brief + Descriptor set binding for font matrix data. + + */ + /***************************************************************************/ + static constexpr uint32_t FONT_MATRIX_DATA = 1; + }; struct VertexBufferBindings @@ -177,6 +205,10 @@ namespace SHADE /***************************************************************************/ static constexpr uint32_t INTEGER_DATA = 5; + static constexpr uint32_t CALCULATED_GLYPH_POSITION = 0; + static constexpr uint32_t GLYPH_INDEX = 1; + + }; /*******************************************************************************/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 5b40a745..3932298c 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -42,6 +42,8 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/SHVkUtil.h" #include "Graphics/RenderGraph/SHRenderGraphNodeCompute.h" #include "../Meshes/SHPrimitiveGenerator.h" +#include "Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.h" +#include "Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h" namespace SHADE { @@ -115,14 +117,25 @@ 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", false); + SHAssetManager::CompileAsset("../../Assets/Shaders/Text_FS.glsl", false); + SHAssetManager::CompileAsset("../../Assets/Shaders/TestCube_VS.glsl", false); + SHAssetManager::CompileAsset("../../Assets/Shaders/UI_VS.glsl", false); + SHAssetManager::CompileAsset("../../Assets/Shaders/UI_FS.glsl", false); + + SHAssetManager::CompileAsset("../../Assets/Models/Quad.gltf", false); + // 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); - static constexpr AssetID VS_DEBUG = 48002439; debugVertShader = SHResourceManager::LoadOrGet(VS_DEBUG); - 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 VS_DEFAULT = 39210065; defaultVertShader = SHResourceManager::LoadOrGet(VS_DEFAULT); + static constexpr AssetID FS_DEFAULT = 46377769; defaultFragShader = SHResourceManager::LoadOrGet(FS_DEFAULT); + static constexpr AssetID VS_DEBUG = 48002439; debugVertShader = SHResourceManager::LoadOrGet(VS_DEBUG); + 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); + static constexpr AssetID SEGOE_UI_FONT = 176667660; testFont = SHResourceManager::LoadOrGet(SEGOE_UI_FONT); } void SHGraphicsSystem::InitSceneRenderGraph(void) noexcept @@ -149,6 +162,8 @@ namespace SHADE worldCamera->SetLookAt(SHVec3(0.0f, 0.0f, 0.0f), SHVec3(0.0f, 0.0f, -2.0f), SHVec3(0.0f, 1.0f, 0.0f)); worldCamera->SetPerspective(90.0f, static_cast(windowDims.first), static_cast(windowDims.second), 0.0f, 100.0f); + worldCameraDirector = cameraSystem->CreateDirector(); + // Create Default Viewport worldViewport = AddViewport(vk::Viewport(0.0f, 0.0f, static_cast(window->GetWindowSize().first), static_cast(window->GetWindowSize().second), 0.0f, 1.0f)); @@ -162,20 +177,21 @@ namespace SHADE } /*-----------------------------------------------------------------------*/ - /* SCENE RENDER GRAPH RESOURCES */ + /* WORLD RENDER GRAPH RESOURCES */ /*-----------------------------------------------------------------------*/ // Initialize world render graph - worldRenderGraph->Init("World Render Graph", device, swapchain); - worldRenderGraph->AddResource("Position", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT, SH_ATT_DESC_TYPE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); - worldRenderGraph->AddResource("Normals", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT, SH_ATT_DESC_TYPE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); + worldRenderGraph->Init("World Render Graph", device, swapchain, &resourceManager); + worldRenderGraph->AddResource("Position", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); + worldRenderGraph->AddResource("Normals", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); //worldRenderGraph->AddResource("Tangents", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT, SH_ATT_DESC_TYPE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32G32B32A32Sfloat); - worldRenderGraph->AddResource("Albedo", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT, SH_ATT_DESC_TYPE_FLAGS::STORAGE }, windowDims.first, windowDims.second); - worldRenderGraph->AddResource("Depth Buffer", { SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL }, windowDims.first, windowDims.second, vk::Format::eD32SfloatS8Uint); - worldRenderGraph->AddResource("Entity ID", { SH_ATT_DESC_TYPE_FLAGS::COLOR }, windowDims.first, windowDims.second, vk::Format::eR32Uint, 1, vk::ImageUsageFlagBits::eTransferSrc); - worldRenderGraph->AddResource("Light Layer Indices", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT, SH_ATT_DESC_TYPE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32Uint, 1, vk::ImageUsageFlagBits::eTransferSrc); - worldRenderGraph->AddResource("Scene", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT, SH_ATT_DESC_TYPE_FLAGS::STORAGE }, windowDims.first, windowDims.second); - worldRenderGraph->AddResource("SSAO", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT, SH_ATT_DESC_TYPE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR8Unorm); - worldRenderGraph->AddResource("SSAO Blur", { SH_ATT_DESC_TYPE_FLAGS::COLOR, SH_ATT_DESC_TYPE_FLAGS::INPUT, SH_ATT_DESC_TYPE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR8Unorm); + worldRenderGraph->AddResource("Albedo", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second); + worldRenderGraph->AddResource("Depth Buffer", { SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH_STENCIL }, windowDims.first, windowDims.second, vk::Format::eD32SfloatS8Uint); + worldRenderGraph->AddResource("Entity ID", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::SHARED }, windowDims.first, windowDims.second, vk::Format::eR32Uint, 1, vk::ImageUsageFlagBits::eTransferSrc); + worldRenderGraph->AddResource("Light Layer Indices", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR32Uint, 1, vk::ImageUsageFlagBits::eTransferSrc); + worldRenderGraph->AddResource("Scene", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE, SH_RENDER_GRAPH_RESOURCE_FLAGS::SHARED }, windowDims.first, windowDims.second); + worldRenderGraph->AddResource("SSAO", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR8Unorm); + worldRenderGraph->AddResource("SSAO Blur", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR, SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT, SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE }, windowDims.first, windowDims.second, vk::Format::eR8Unorm); + /*-----------------------------------------------------------------------*/ /* MAIN NODE */ @@ -205,7 +221,7 @@ namespace SHADE gBufferSubpass->AddColorOutput("Normals"); //gBufferSubpass->AddColorOutput("Tangents"); gBufferSubpass->AddColorOutput("Albedo"); - gBufferSubpass->AddDepthOutput("Depth Buffer", SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL); + gBufferSubpass->AddDepthOutput("Depth Buffer", SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH_STENCIL); /*-----------------------------------------------------------------------*/ /* SSAO PASS AND DATA INIT */ @@ -250,25 +266,63 @@ namespace SHADE /*-----------------------------------------------------------------------*/ gBufferNode->AddNodeCompute("Deferred Composite", deferredCompositeShader, {"Position", "Normals", "Albedo", "Light Layer Indices", "SSAO Blur", "Scene"}); - // Dummy Node - auto dummyNode = worldRenderGraph->AddNode("Dummy Pass", { "Scene" }, { "Debug Draw" }); // no predecessors - auto dummySubpass = dummyNode->AddSubpass("Dummy Subpass"); - dummySubpass->AddInput("Scene"); + { + //// Dummy Node to transition scene render graph resource + //auto dummyNode = worldRenderGraph->AddNode("Dummy Pass", { "Scene" }, { "Debug Draw" }); // no predecessors + //auto dummySubpass = dummyNode->AddSubpass("Dummy Subpass"); + //dummySubpass->AddInput("Scene"); + } /*-----------------------------------------------------------------------*/ - /* GENERATE RENDER GRAPH */ + /* GENERATE WORLD RENDER GRAPH */ /*-----------------------------------------------------------------------*/ // Generate world render graph worldRenderGraph->Generate(); + + /*-----------------------------------------------------------------------*/ + /* SCREEN RENDER GRAPH */ + /*-----------------------------------------------------------------------*/ + // Initialize screen render graph + screenRenderGraph = resourceManager.Create(); + screenRenderGraph->Init("Screen Render Graph", device, swapchain, &resourceManager); + screenRenderGraph->LinkNonOwningResource(worldRenderGraph, "Scene"); + screenRenderGraph->LinkNonOwningResource(worldRenderGraph, "Entity ID"); + screenRenderGraph->AddResource("Present", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT }, windowDims.first, windowDims.second); + + auto screenSpaceNode = screenRenderGraph->AddNode("Screen Space Pass", { "Scene", "Entity ID"}, {}); + auto uiSubpass = screenSpaceNode->AddSubpass("UI"); + uiSubpass->AddColorOutput("Scene"); + uiSubpass->AddColorOutput("Entity ID"); + uiSubpass->AddExteriorDrawCalls([=](Handle& cmdBuffer, uint32_t frameIndex) + { + textRenderingSubSystem->Render(cmdBuffer, frameIndex); + }); + + { + // Dummy Node to transition scene render graph resource + auto dummyNode = screenRenderGraph->AddNode("Dummy Pass", { "Scene" }, { "Screen Space Pass" }); // no predecessors + auto dummySubpass = dummyNode->AddSubpass("Dummy Subpass"); + dummySubpass->AddInput("Scene"); + } + + //screenRenderGraph->AddRenderToSwapchainNode ("Scene", "Present", ) + + screenRenderGraph->Generate(); + /*-----------------------------------------------------------------------*/ /* BIND RENDER GRAPH TO RENDERER */ /*-----------------------------------------------------------------------*/ // Add world renderer to default viewport worldRenderer = worldViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph); worldRenderer->SetCamera(worldCamera); + worldRenderer->SetCameraDirector(worldCameraDirector); + + // Add screen renderer to default viewport + screenRenderer = worldViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, SHGraphicsGlobalData::GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], screenRenderGraph); + screenRenderer->SetCamera(screenCamera); + screenRenderer->SetCameraDirector(worldCameraDirector); - worldRenderer->SetCameraDirector(cameraSystem->CreateDirector()); // Create debug draw pipeline debugDrawPipeline = createDebugDrawPipeline(debugDrawNode->GetRenderpass(), debugDrawSubpass); @@ -315,6 +369,17 @@ namespace SHADE lightingSubSystem = resourceManager.Create(); lightingSubSystem->Init(device, descPool); + + textRenderingSubSystem = resourceManager.Create(); + + // initialize the text renderer + auto uiNode = screenRenderGraph->GetNode("Screen Space Pass"); + textRenderingSubSystem->Init(device, uiNode->GetRenderpass(), uiNode->GetSubpass("UI"), descPool, textVS, textFS, [=](Handle cmdBuffer, uint32_t frameIndex) + { + screenRenderer->BindDescSet(cmdBuffer, frameIndex); + }); + + SHFreetypeInstance::Init(); } void SHGraphicsSystem::InitBuiltInResources(void) @@ -355,8 +420,8 @@ namespace SHADE for (uint32_t i = 0; i < renderContextCmdPools.size(); ++i) renderContextCmdPools[i] = renderContext.GetFrameData(i).cmdPoolHdls[0]; - editorRenderGraph->Init("Editor Render Graph", device, swapchain); - editorRenderGraph->AddResource("Present", { SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT }, windowDims.first, windowDims.second); + editorRenderGraph->Init("Editor Render Graph", device, swapchain, &resourceManager); + editorRenderGraph->AddResource("Present", { SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT }, windowDims.first, windowDims.second); auto imguiNode = editorRenderGraph->AddNode("ImGui Node", { "Present"}, {}); @@ -385,6 +450,7 @@ namespace SHADE void SHGraphicsSystem::Exit(void) { + SHFreetypeInstance::Exit(); } #pragma endregion INIT_EXIT @@ -454,6 +520,8 @@ namespace SHADE #endif } + textRenderingSubSystem->Run(frameIndex); + // For every viewport for (int vpIndex = 0; vpIndex < static_cast(viewports.size()); ++vpIndex) { @@ -513,7 +581,7 @@ namespace SHADE { auto editorSystem = SHSystemManager::GetSystem(); if (editorSystem->editorState != SHEditor::State::PLAY) - worldRenderer->UpdateDataAndBind(currentCmdBuffer, frameIndex, cameraSystem->GetEditorCamera()->GetViewMatrix(), cameraSystem->GetEditorCamera()->GetProjMatrix()); + worldRenderer->UpdateDataAndBind(currentCmdBuffer, frameIndex, cameraSystem->GetEditorCamera()->GetViewMatrix(), cameraSystem->GetEditorCamera()->GetProjMatrix(), cameraSystem->GetEditorCamera()->GetOrthoMatrix()); else renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex); } @@ -761,7 +829,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ Handle SHGraphicsSystem::AddTexture(const SHTextureAsset& texAsset) { - const int MIPS = texAsset.mipOffsets.size(); + const int MIPS = static_cast (texAsset.mipOffsets.size()); auto sampler = samplerCache.GetSampler(device, SHVkSamplerParams { .maxLod = static_cast(MIPS) }); SET_VK_OBJ_NAME(device, vk::ObjectType::eSampler, sampler->GetVkSampler(), "[Sampler] Mips " + std::to_string(MIPS)); return texLibrary.Add(texAsset, sampler); @@ -769,7 +837,7 @@ namespace SHADE SHADE::Handle SHGraphicsSystem::AddTexture(uint32_t pixelCount, const SHTexture::PixelChannel* const pixelData, uint32_t width, uint32_t height, SHTexture::TextureFormat format, std::vector mipOffsets) { - const int MIPS = mipOffsets.size(); + const int MIPS = static_cast (mipOffsets.size()); auto sampler = samplerCache.GetSampler(device, SHVkSamplerParams{ .maxLod = static_cast(MIPS) }); SET_VK_OBJ_NAME(device, vk::ObjectType::eSampler, sampler->GetVkSampler(), "[Sampler] Mips " + std::to_string(MIPS)); return texLibrary.Add(pixelCount, pixelData, width, height, format, mipOffsets, sampler); @@ -798,6 +866,19 @@ namespace SHADE return texLibrary.GetTextureHandle(textureId); } + /*---------------------------------------------------------------------------------*/ + /* Font Registration Functions */ + /*---------------------------------------------------------------------------------*/ + Handle SHGraphicsSystem::AddFont(SHFontAsset const& fontAsset) noexcept + { + return fontLibrary.AddFont(device, resourceManager, fontAsset); + } + + void SHGraphicsSystem::BuildFonts(void) noexcept + { + fontLibrary.BuildFonts(device, graphicsQueue, graphicsCmdPool, descPool, textRenderingSubSystem->GetFontDataDescSetLayout(), resourceManager); + } + #pragma endregion ADD_REMOVE #pragma region ROUTINES @@ -920,6 +1001,7 @@ namespace SHADE #pragma region MISC + void SHGraphicsSystem::PrepareResize(uint32_t newWidth, uint32_t newHeight) noexcept { resizeWidth = newWidth; @@ -946,7 +1028,12 @@ namespace SHADE renderContext.HandleResize(); worldRenderGraph->HandleResize(resizeWidth, resizeHeight); + +#ifdef SHEDITOR editorRenderGraph->HandleResize(windowDims.first, windowDims.second); +#endif + + screenRenderGraph->HandleResize(resizeWidth, resizeHeight); mousePickSystem->HandleResize(); postOffscreenRender->HandleResize(); @@ -986,6 +1073,11 @@ namespace SHADE return worldRenderGraph->GetNode(G_BUFFER_RENDER_GRAPH_NODE_NAME.data()); } + SHADE::SHFontLibrary const& SHGraphicsSystem::GetFontLibrary(void) const noexcept + { + return fontLibrary; + } + Handle SHGraphicsSystem::createDebugDrawPipeline(Handle renderPass, Handle subpass) { auto pipelineLayout = resourceManager.Create diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 2a186041..c201e9a5 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -32,6 +32,8 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/Interface/SHPostOffscreenRenderSystem.h" #include "Graphics/MiddleEnd/Lights/SHLightingSubSystem.h" #include "Graphics/MiddleEnd/PostProcessing/SHSSAO.h" +#include "Camera/SHCameraDirector.h" +#include "Graphics/MiddleEnd/TextRendering/SHFontLibrary.h" namespace SHADE { @@ -54,6 +56,7 @@ namespace SHADE class SHMaterial; class SHMaterialInstance; class SHMousePickSystem; + class SHTextRenderingSubSystem; /*---------------------------------------------------------------------------------*/ /* Type Definitions */ @@ -326,6 +329,22 @@ namespace SHADE */ /***************************************************************************/ Handle GetDefaultTexture() const noexcept { return defaultTexture; } + /***************************************************************************/ + /*! + + \brief + Adds a font into the font library. + + \param fontAsset + Font asset to add. + + \return + Handle to the new font. + + */ + /***************************************************************************/ + Handle AddFont (SHFontAsset const& fontAsset) noexcept; + void BuildFonts (void) noexcept; void PrepareResize(uint32_t newWidth, uint32_t newHeight) noexcept; void HandleResize(void) noexcept; @@ -355,6 +374,7 @@ namespace SHADE Handle GetDebugDrawPipeline(void) const noexcept { return debugDrawPipeline; } Handle GetDebugDrawDepthPipeline(void) const noexcept { return debugDrawDepthPipeline; } uint32_t GetCurrentFrameIndex(void) const noexcept { return renderContext.GetCurrentFrame(); } + SHFontLibrary const& GetFontLibrary (void) const noexcept; /*-----------------------------------------------------------------------------*/ /* Getters */ @@ -391,6 +411,7 @@ namespace SHADE SHResourceHub resourceManager; SHMeshLibrary meshLibrary; SHTextureLibrary texLibrary; + SHFontLibrary fontLibrary; SHSamplerCache samplerCache; SHMaterialInstanceCache materialInstanceCache; @@ -398,18 +419,20 @@ namespace SHADE #ifdef SHEDITOR Handle editorViewport; Handle editorRenderer; - Handle editorRenderGraph; #endif Handle worldViewport; // Whole screen - std::vector> viewports; // Additional viewports + std::vector> viewports; // Additional viewports - // Temp renderers + // Renderers Handle worldRenderer; + Handle screenRenderer; // Temp Cameras Handle worldCamera; Handle screenCamera; + DirectorHandle worldCameraDirector; + // Built-In Shaders Handle defaultVertShader; @@ -419,7 +442,11 @@ namespace SHADE Handle deferredCompositeShader; Handle ssaoShader; Handle ssaoBlurShader; + Handle textVS; + Handle textFS; + // Fonts + Handle testFont; // Built-In Materials Handle defaultMaterial; @@ -434,11 +461,16 @@ namespace SHADE // Render Graphs Handle worldRenderGraph; + Handle screenRenderGraph; +#ifdef SHEDITOR + Handle editorRenderGraph; +#endif // Sub systems Handle mousePickSystem; Handle postOffscreenRender; Handle lightingSubSystem; + Handle textRenderingSubSystem; Handle ssaoStorage; uint32_t resizeWidth = 1; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp index 63d374eb..e47055df 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.cpp @@ -90,17 +90,22 @@ namespace SHADE if (camera && cameraDirector) { //UpdateDataAndBind(cmdBuffer, frameIndex, SHMatrix::Transpose(cameraDirector->GetVPMatrix())); - UpdateDataAndBind(cmdBuffer, frameIndex, cameraDirector->GetViewMatrix(), cameraDirector->GetProjMatrix()); + UpdateDataAndBind(cmdBuffer, frameIndex, cameraDirector->GetViewMatrix(), cameraDirector->GetProjMatrix(), cameraDirector->GetOrthoMatrix()); } } - void SHRenderer::UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix const& viewMatrix, SHMatrix const& projMatrix) noexcept + void SHRenderer::UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix const& viewMatrix, SHMatrix const& projMatrix, SHMatrix const& orthoMatrix) noexcept { - SetViewProjectionMatrix(viewMatrix, projMatrix); + SetViewProjectionMatrix(viewMatrix, projMatrix, orthoMatrix); //cpuCameraData.viewProjectionMatrix = camera->GetViewProjectionMatrix(); cameraBuffer->WriteToMemory(&cpuCameraData, sizeof(SHShaderCameraData), 0, cameraDataAlignedSize * frameIndex); + BindDescSet(cmdBuffer, frameIndex); + } + + void SHRenderer::BindDescSet(Handle cmdBuffer, uint32_t frameIndex) noexcept + { std::array dynamicOffsets{ frameIndex * cameraDataAlignedSize }; cmdBuffer->BindDescriptorSet(cameraDescriptorSet, SH_PIPELINE_TYPE::GRAPHICS, SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, std::span{ dynamicOffsets.data(), 1 }); @@ -111,12 +116,13 @@ namespace SHADE { } - void SHRenderer::SetViewProjectionMatrix(SHMatrix const& viewMatrix, SHMatrix const& projMatrix) noexcept + void SHRenderer::SetViewProjectionMatrix(SHMatrix const& viewMatrix, SHMatrix const& projMatrix, SHMatrix const& orthoMatrix) noexcept { //cpuCameraData.viewProjectionMatrix = camera->GetViewMatrix() * camera->GetProjectionMatrix(); cpuCameraData.viewProjectionMatrix = SHMatrix::Transpose(projMatrix * viewMatrix); cpuCameraData.viewMatrix = SHMatrix::Transpose(viewMatrix); cpuCameraData.projectionMatrix = SHMatrix::Transpose(projMatrix); + cpuCameraData.orthoMatrix = SHMatrix::Transpose (orthoMatrix); } Handle SHRenderer::GetRenderGraph(void) const noexcept diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h index 140cf53b..83291700 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHRenderer.h @@ -48,6 +48,7 @@ namespace SHADE SHMatrix viewProjectionMatrix; SHMatrix viewMatrix; SHMatrix projectionMatrix; + SHMatrix orthoMatrix; }; /*---------------------------------------------------------------------------------*/ @@ -63,6 +64,7 @@ namespace SHADE /***********************************************************************************/ class SHRenderer { + public: /*-----------------------------------------------------------------------------*/ /* Constructor/Destructors */ @@ -81,9 +83,10 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ void Draw(uint32_t frameIndex, Handle descPool) noexcept; void UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex) noexcept; - void UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix const& viewMatrix, SHMatrix const& projMatrix) noexcept; + void UpdateDataAndBind(Handle cmdBuffer, uint32_t frameIndex, SHMatrix const& viewMatrix, SHMatrix const& projMatrix, SHMatrix const& orthoMatrix) noexcept; + void BindDescSet (Handle cmdBuffer, uint32_t frameIndex) noexcept; void UpdateCameraDataToBuffer (void) noexcept; - void SetViewProjectionMatrix (SHMatrix const& viewMatrix, SHMatrix const& projMatrix) noexcept; + void SetViewProjectionMatrix (SHMatrix const& viewMatrix, SHMatrix const& projMatrix, SHMatrix const& orthoMatrix) noexcept; /*-----------------------------------------------------------------------------*/ /* Setters and Getters */ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp index 2949ad43..3d5a5773 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp @@ -423,9 +423,8 @@ namespace SHADE /*! \brief - Loops through every single light component and checks for dirty light - data. If light data is dirty, rewrite to the CPU container. We also want - to bind the descriptor set for the light data. + Loops through every single light component and writes light data to CPU + then GPU. */ /***************************************************************************/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/PostProcessing/SHSSAO.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/PostProcessing/SHSSAO.cpp index 2bf32fd8..db5b08c3 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/PostProcessing/SHSSAO.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/PostProcessing/SHSSAO.cpp @@ -76,7 +76,7 @@ namespace SHADE }; uint32_t mipOffset = 0; - rotationVectorsImage = logicalDevice->CreateImage(imageDetails, reinterpret_cast( rotationVectors.data()), static_cast(sizeof(rotationVectors)), {&mipOffset, 1}, VMA_MEMORY_USAGE_AUTO, {}); + rotationVectorsImage = logicalDevice->CreateImage(imageDetails, reinterpret_cast(rotationVectors.data()), static_cast(sizeof(rotationVectors)), { mipOffset }, VMA_MEMORY_USAGE_AUTO, {}); vk::ImageMemoryBarrier transferBarrier{}; rotationVectorsImage->PrepareImageTransitionInfo(vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, transferBarrier); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFont.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFont.cpp new file mode 100644 index 00000000..3dd54ca5 --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFont.cpp @@ -0,0 +1,160 @@ +#include "SHpch.h" +#include "SHFont.h" +#include "Graphics/Devices/SHVkLogicalDevice.h" +#include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h" +#include "Graphics/Descriptors/SHVkDescriptorSetGroup.h" +#include "Graphics/Buffers/SHVkBuffer.h" +#include "Graphics/Images/SHVkSampler.h" + +namespace SHADE +{ + /***************************************************************************/ + /*! + + \brief + Non-default ctor. Prepares objects in staging buffers with data from + SHFontAsset. + + \param inLogicalDeviceHdl + Logical device required for vulkan object creation. + + \param asset + Font asset to copy data from. + + */ + /***************************************************************************/ + SHFont::SHFont(Handle inLogicalDeviceHdl, SHFontAsset const& asset) noexcept + { + /*-----------------------------------------------------------------------*/ + /* PREPARE GPU DATA */ + /*-----------------------------------------------------------------------*/ + + // assign device for convenient usage + logicalDevice = inLogicalDeviceHdl; + + // Copy the font data + fontAsset = asset; + + SHImageCreateParams imageParams + { + .imageType = vk::ImageType::e2D, + .width = fontAsset.bitmapWidth, + .height = fontAsset.bitmapHeight, + .depth = 1, + .levels = 1, + .arrayLayers = 1, + .imageFormat = vk::Format::eR8G8B8A8Unorm, + //.imageFormat = vk::Format::eR32Sfloat, + //.imageFormat = vk::Format::eR32G32B32Sfloat, + .usageFlags = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eTransferDst, + .createFlags = {} + }; + + uint32_t bytesRequired = asset.bitmapData.size(); + uint32_t mipOffset = 0; + + // Create the image + 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); + + // For indexing + for (uint32_t i = 0; i < fontAsset.glyphs.size(); ++i) + unicodeIndexing.emplace(fontAsset.glyphs[i], i); + + // allocate GPU buffer for matrices + matrixDataBuffer = logicalDevice->CreateBuffer(glyphDataSize, fontAsset.glyphTransformations.data(), glyphDataSize, vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlagBits::eStorageBuffer, VMA_MEMORY_USAGE_AUTO, {}); + + sampler = logicalDevice->CreateSampler(SHVkSamplerParams{}); + + } + + /***************************************************************************/ + /*! + + \brief + Transfers data from staging buffers to GPU. Does not issue any of the + barriers (we want this done outside). + + \param commandBuffer + Command buffer used to + + \return + + */ + /***************************************************************************/ + void SHFont::TransferToGPU(Handle commandBuffer) noexcept + { + /*-----------------------------------------------------------------------*/ + /* COMMANDS TO TRANSFER TO DEVICE MEMORY */ + /*-----------------------------------------------------------------------*/ + // Transfer to device memory + bitmapDataImage->TransferToDeviceResource(commandBuffer); + + // Transfer to GPU buffer + matrixDataBuffer->TransferToDeviceResource(commandBuffer); + } + + void SHFont::DoPostTransfer(Handle descPool, Handle layout) noexcept + { + /*-----------------------------------------------------------------------*/ + /* CREATE IMAGE VIEW */ + /*-----------------------------------------------------------------------*/ + // Create the image view to the device resource + SHImageViewDetails viewDetails + { + .viewType = vk::ImageViewType::e2D, + .format = bitmapDataImage->GetImageFormat(), + .imageAspectFlags = vk::ImageAspectFlagBits::eColor, + .baseMipLevel = 0, + .mipLevelCount = 1, + .baseArrayLayer = 0, + .layerCount = 1, + }; + bitmapDataImageView = bitmapDataImage->CreateImageView(logicalDevice, bitmapDataImage, viewDetails); + + /*-----------------------------------------------------------------------*/ + /* DESCRIPTORS */ + /*-----------------------------------------------------------------------*/ + // allocate desc set for the bitmap and matrix data + descSet = descPool->Allocate({ layout }, { 1 }); + + 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)); + + // Bind image and buffer to desc set. + descSet->UpdateDescriptorSetImages(SHGraphicsConstants::DescriptorSetIndex::FONT_DATA, SHGraphicsConstants::DescriptorSetBindings::FONT_BITMAP_DATA); + descSet->UpdateDescriptorSetBuffer(SHGraphicsConstants::DescriptorSetIndex::FONT_DATA, SHGraphicsConstants::DescriptorSetBindings::FONT_MATRIX_DATA); + + } + + std::unordered_map SHFont::GetUnicodeIndexing(void) const noexcept + { + return unicodeIndexing; + } + + SHFontAsset const& SHFont::GetFontAsset(void) const noexcept + { + return fontAsset; + } + + Handle SHFont::GetImage(void) const noexcept + { + return bitmapDataImage; + } + + Handle SHFont::GetMatrixBuffer(void) const noexcept + { + return matrixDataBuffer; + } + + Handle SHFont::GetDescriptorSet(void) const noexcept + { + return descSet; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFont.h b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFont.h new file mode 100644 index 00000000..1439281a --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFont.h @@ -0,0 +1,72 @@ +#pragma once + +#include "Resource/SHHandle.h" +#include "msdf-atlas-gen/msdf-atlas-gen.h" +#include "Assets/Asset Types/SHFontAsset.h" + +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 + { + private: + //! Device for creation and destruction + Handle logicalDevice; + + //! Font asset contains exactly what we need, so we'll use it + SHFontAsset fontAsset; + + //! Device memory that stores bitmap data + Handle bitmapDataImage; + + //! View to device memory + Handle bitmapDataImageView; + + //! Device memory that stores matrix data + Handle matrixDataBuffer; + + //! Descriptor set required to store the bitmap AND matrix data for the UV and quad transformation + Handle descSet; + + Handle sampler; + + //! Used for getting the correct indices into the matrix data buffer + std::unordered_map unicodeIndexing; + + ////! To transition images for transfer ops + //vk::ImageMemoryBarrier preTransferBarrier; + + ////! To transition images for shader reads + //vk::ImageMemoryBarrier postTransferBarrier; + + public: + /*-----------------------------------------------------------------------*/ + /* PUBLIC MEMBER FUNCTIONS */ + /*-----------------------------------------------------------------------*/ + SHFont (Handle inLogicalDeviceHdl, SHFontAsset const& asset) noexcept; + void TransferToGPU (Handle commandBuffer) noexcept; + void DoPostTransfer (Handle descPool, Handle layout) noexcept; + + + /*-----------------------------------------------------------------------*/ + /* SETTERS AND GETTERS */ + /*-----------------------------------------------------------------------*/ + std::unordered_map GetUnicodeIndexing (void) const noexcept; + SHFontAsset const& GetFontAsset (void) const noexcept; + Handle GetImage (void) const noexcept; + Handle GetMatrixBuffer (void) const noexcept; + Handle GetDescriptorSet (void) const noexcept; + + }; +} diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontAtlasData.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontAtlasData.cpp new file mode 100644 index 00000000..6ea1bceb --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontAtlasData.cpp @@ -0,0 +1,7 @@ +#include "SHpch.h" +#include "SHFontAtlasData.h" + +namespace SHADE +{ + +} diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontAtlasData.h b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontAtlasData.h new file mode 100644 index 00000000..1e6e7470 --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontAtlasData.h @@ -0,0 +1,7 @@ +#pragma once + + +namespace SHADE +{ + +} diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontLibrary.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontLibrary.cpp new file mode 100644 index 00000000..3ae5c2ec --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontLibrary.cpp @@ -0,0 +1,108 @@ +#include "SHpch.h" +#include "SHFontLibrary.h" +#include "Graphics/Images/SHVkImage.h" +#include "Graphics/Commands/SHVkCommandPool.h" +#include "Graphics/Devices/SHVkLogicalDevice.h" +#include "Graphics/Synchronization/SHVkFence.h" +#include "Graphics/Buffers/SHVkBuffer.h" + +namespace SHADE +{ + /***************************************************************************/ + /*! + + \brief + For adding fonts to the library + + \param asset + The asset we want to create an SHFont from. + + */ + /***************************************************************************/ + Handle SHFontLibrary::AddFont(Handle logicalDevice, SHResourceHub& resourceHub, SHFontAsset const& asset) noexcept + { + // Create new font + Handle newFont = resourceHub.Create(logicalDevice, asset); + + // emplace new barriers and prepare them for transfer ops + preTransferBarriers.emplace_back(); + postTransferBarriers.emplace_back(); + 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]); + + unpreparedFonts.emplace_back (newFont); + + return newFont; + } + + /***************************************************************************/ + /*! + + \brief + Transfers staging buffer font memory to the GPU for optimized access. + + \param cmdBuffer + Command buffer to record the transfer operations. + + \param descPool + + \return + + */ + /***************************************************************************/ + void SHFontLibrary::BuildFonts(Handle logicalDevice, Handle queue, Handle cmdPool, Handle descPool, Handle layout, SHResourceHub& resourceHub) noexcept + { + // create fence to wait on after transfer + Handle finishCopyFence = resourceHub.Create(logicalDevice); + finishCopyFence->Reset(); + + // allocate new command buffer + Handle transferCommandBuffer = cmdPool->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY); + + // Begin recording transfer ops + transferCommandBuffer->BeginRecording(); + { + // Transition image to dst + transferCommandBuffer->PipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer, {}, {}, {}, preTransferBarriers); + + // Transfer data from staging to image + for (auto& font : unpreparedFonts) + font->TransferToGPU(transferCommandBuffer); + + // Transition dst to shader read + transferCommandBuffer->PipelineBarrier(vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eFragmentShader, {}, {}, {}, postTransferBarriers); + } + + // End recording for transfer ops + transferCommandBuffer->EndRecording(); + + // Submit command buffer to queue + queue->SubmitCommandBuffer({transferCommandBuffer}, {}, {}, vk::PipelineStageFlagBits::eTransfer, finishCopyFence); + + // wait for copy to finish + finishCopyFence->Wait(true, std::numeric_limits::max()); + + // Prepare image views and desc sets + for (auto& font : unpreparedFonts) + font->DoPostTransfer(descPool, layout); + + // Free the command buffer and fence + transferCommandBuffer.Free(); + resourceHub.Free(finishCopyFence); + + // Once unprepared fonts are now ready for use, push them into container + uint32_t i = static_cast (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(); + } + + std::vector> const& SHFontLibrary::GetFonts(void) const noexcept + { + return fonts; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontLibrary.h b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontLibrary.h new file mode 100644 index 00000000..a3762e6b --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFontLibrary.h @@ -0,0 +1,36 @@ +#pragma once + +#include "SHFont.h" +#include +#include "Graphics/SHVulkanIncludes.h" + +namespace SHADE +{ + class SHVkLogicalDevice; + class SHVkDescriptorPool; + class SHVkCommandPool; + class SHVkCommandBuffer; + class SHVkQueue; + class SHVkDescriptorSetLayout; + + class SH_API SHFontLibrary + { + private: + //! Handles to all the fonts usable in SHTextRendererComponents + std::vector> fonts; + + //! Fonts that have yet to be properly prepared for usage + std::vector> unpreparedFonts; + + //! For transitioning images for transfer operations + std::vector preTransferBarriers; + + //! For transitioning images for shader reads + std::vector postTransferBarriers; + + public: + Handle AddFont (Handle logicalDevice, SHResourceHub& resourceHub, SHFontAsset const& asset) noexcept; + void BuildFonts (Handle logicalDevice, Handle queue, Handle cmdPool, Handle descPool, Handle layout, SHResourceHub& resourceHub) noexcept; + std::vector> const& GetFonts (void) const noexcept; + }; +} diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.cpp new file mode 100644 index 00000000..9e629020 --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.cpp @@ -0,0 +1,27 @@ +#include "SHpch.h" +#include "SHFreetypeInstance.h" +#include "msdf-atlas-gen/msdf-atlas-gen.h" +#include "msdfgen-ext.h" + +namespace SHADE +{ + + msdfgen::FreetypeHandle* SHFreetypeInstance::freetypeHandle = nullptr; + + void SHFreetypeInstance::Init(void) noexcept + { + // initialize freetype + freetypeHandle = msdfgen::initializeFreetype(); + } + + void SHFreetypeInstance::Exit(void) noexcept + { + msdfgen::deinitializeFreetype(freetypeHandle); + } + + msdfgen::FreetypeHandle* SHFreetypeInstance::GetFreetypeHandle(void) noexcept + { + return freetypeHandle; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.h b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.h new file mode 100644 index 00000000..4d1bed9c --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHFreetypeInstance.h @@ -0,0 +1,23 @@ +#pragma once + +namespace msdfgen +{ + class FreetypeHandle; +} + +namespace SHADE +{ + class SHFreetypeInstance + { + private: + //! Only need this to be initialized once + static msdfgen::FreetypeHandle* freetypeHandle; + + public: + static void Init (void) noexcept; + static void Exit (void) noexcept; + + static msdfgen::FreetypeHandle* GetFreetypeHandle(void) noexcept; + + }; +} diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.cpp new file mode 100644 index 00000000..ac1dd73f --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.cpp @@ -0,0 +1,79 @@ +#include "SHpch.h" +#include "SHTextRendererComponent.h" + +namespace SHADE +{ + + void SHTextRendererComponent::MakeDirty(void) noexcept + { + requiresRecompute = true; + } + + void SHTextRendererComponent::Clean(void) noexcept + { + requiresRecompute = false; + } + + /***************************************************************************/ + /*! + + \brief + On create the text has nothing. + + */ + /***************************************************************************/ + void SHTextRendererComponent::OnCreate(void) + { + text = "Text"; + requiresRecompute = true; + + // Default white color. + color = SHColour::WHITE; + } + + void SHTextRendererComponent::OnDestroy(void) + { + + } + + /***************************************************************************/ + /*! + + \brief + Sets the text to be rendered. + + \param newText + + + \return + + */ + /***************************************************************************/ + void SHTextRendererComponent::SetText(std::string_view newText) noexcept + { + text = newText; + MakeDirty(); + } + + void SHTextRendererComponent::SetFont(Handle font) noexcept + { + fontHandle = font; + } + + /***************************************************************************/ + /*! + + \brief + Getter for the text required to render. + + \return + + + */ + /***************************************************************************/ + std::string const& SHTextRendererComponent::GetText(void) const noexcept + { + return text; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.h b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.h new file mode 100644 index 00000000..242253ec --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.h @@ -0,0 +1,61 @@ +#pragma once + +#include +#include +#include "ECS_Base/Components/SHComponent.h" +#include "Math/SHColour.h" +#include "Resource/SHHandle.h" + +namespace SHADE +{ + class SHFont; + class SHVkDescriptorSetGroup; + class SHVkBuffer; + + class SH_API SHTextRendererComponent final : public SHComponent + { + public: + static constexpr uint32_t MAX_CHARACTERS = 500; + + private: + using TextIndexingType = uint32_t; + + private: + //! Text required to be rendered + std::string text; + + //! Color of the text + SHColour color; + + //! Requires to recompute the positions of each glyph/character + bool requiresRecompute; + + //! Handle to the font used to render the text + Handle fontHandle; + + //! We want to index into the buffer with matrices + Handle indexingDataBuffer; + + //! character position data for each letter in the text + Handle charPositionDataBuffer; + + void MakeDirty (void) noexcept; + void Clean (void) noexcept; + + public: + void OnCreate(void) override final; + void OnDestroy(void) override final; + + /*-----------------------------------------------------------------------*/ + /* SETTERS AND GETTERS */ + /*-----------------------------------------------------------------------*/ + void SetText (std::string_view newText) noexcept; + void SetFont (Handle font) noexcept; + + std::string const& GetText (void) const noexcept; + + friend class SHTextRenderingSubSystem; + + }; +} + diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp new file mode 100644 index 00000000..ed7314ba --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.cpp @@ -0,0 +1,235 @@ +#include "SHpch.h" +#include "SHTextRenderingSubSystem.h" +#include "Graphics/MiddleEnd/TextRendering/SHTextRendererComponent.h" +#include "ECS_Base/Managers/SHComponentManager.h" +#include "Math/Vector/SHVec4.h" +#include "Graphics/Devices/SHVkLogicalDevice.h" +#include "Graphics/MiddleEnd/TextRendering/SHFont.h" +#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" +#include "Math/Transform/SHTransformComponent.h" + +namespace SHADE +{ + void SHTextRenderingSubSystem::RecomputePositions(SHTextRendererComponent& textComp) noexcept + { + if (textComp.text.empty() || textComp.fontHandle) + return; + + // Create the buffer + if (!textComp.indexingDataBuffer) + 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); + + // For indexing font transformation in the shader + std::vector indexingData; + + // For placing glyphs correctly + std::vector charPositionData; + + // Baseline relative to entity with this component + SHVec4 baselineOrigin{ 0.0f, 0.0f, 0.0f, 1.0f }; + + // Number of characters in the string + uint32_t numChars = static_cast(textComp.text.size()); + + // Get a ref to the transform indices + auto const& glyphTransformIndices = textComp.fontHandle->GetUnicodeIndexing(); + + // Get a ref to the glyph transformations + auto const& glyphTransforms = textComp.fontHandle->GetFontAsset().glyphTransformations; + + bool dueNextLine = false; + + // for every character + for (uint32_t i = 0; i < numChars; ++i) + { + SHTextRendererComponent::TextIndexingType index = glyphTransformIndices.at(textComp.text[i]); + + // Copy baseline + SHVec4 characterPos = baselineOrigin; + + // Get where to draw the glyph relative to the baseline + characterPos[0] += glyphTransforms[index].m[SHFontAsset::BASELINE_LEFT_MATRIX_INDEX_ROW][SHFontAsset::BASELINE_LEFT_MATRIX_INDEX_COL]; // Inside the matrix but not used in the shader so this value has no positional value + characterPos[1] += glyphTransforms[index].m[SHFontAsset::BASELINE_BOTTOM_MATRIX_INDEX_ROW][SHFontAsset::BASELINE_BOTTOM_MATRIX_INDEX_COL]; // Inside the matrix but not used in the shader so this value has no positional value + + indexingData.push_back(index); + charPositionData.push_back(characterPos); + + // if not the last character + if (i != numChars - 1) + { + // Get the advance and move the baseline + double advance = 0.0; + textComp.fontHandle->GetFontAsset().fontGeometry.getAdvance(advance, textComp.text[i], textComp.text[static_cast(i) + 1]); + baselineOrigin[0] += static_cast(advance); + + //if (baselineOrigin[0] >= textComp.estimatedLineLength) + //{ + // dueNextLine = true; + //} + } + + //if (dueNextLine && textComp.renderedText[i] == ' ') + //{ + // baselineOrigin[0] = 0.0f; + // baselineOrigin[1] -= textComp.lineSpacing; + // dueNextLine = false; + //} + } + + textComp.indexingDataBuffer->WriteToMemory(indexingData.data(), static_cast(indexingData.size()) * sizeof (SHTextRendererComponent::TextIndexingType),0, 0); + textComp.charPositionDataBuffer->WriteToMemory(charPositionData.data(), static_cast(charPositionData.size()) * sizeof (SHVec4), 0, 0); + + indexingData.clear(); + charPositionData.clear(); + + } + + void SHTextRenderingSubSystem::Init(Handle device, Handle compatibleRenderpass, Handle subpass, Handle descPool, Handle textVS, Handle textFS, std::function, uint32_t)> const& bindFunction) noexcept + { + SHComponentManager::CreateComponentSparseSet(); + + cameraDescSetBind = bindFunction; + + 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); + + // vertex input state of the pipeline + 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) + + // 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(static_cast(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 + { + auto& textRendererComps = SHComponentManager::GetDense(); + + for (auto& comp : textRendererComps) + { + // If the component is dirty + if (comp.requiresRecompute) + { + RecomputePositions(comp); + comp.Clean(); + } + } + } + + void SHTextRenderingSubSystem::Render(Handle cmdBuffer, uint32_t frameIndex) noexcept + { + auto& textRendererComps = SHComponentManager::GetDense(); + for (auto& comp : textRendererComps) + { + auto* transform = SHComponentManager::GetComponent(comp.GetEID()); + + Handle fontHandle = comp.fontHandle; + if (fontHandle && transform) + { + // bind the pipeline + cmdBuffer->BindPipeline(pipeline); + + // bind VBO (position and indices) + cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::CALCULATED_GLYPH_POSITION, comp.charPositionDataBuffer, 0); + cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::GLYPH_INDEX, comp.indexingDataBuffer, 0); + + cameraDescSetBind(cmdBuffer, frameIndex); + + // bind descriptors for font (matrices) + cmdBuffer->BindDescriptorSet(fontHandle->GetDescriptorSet(), SH_PIPELINE_TYPE::GRAPHICS, SHGraphicsConstants::DescriptorSetIndex::FONT_DATA, {}); + + cmdBuffer->SetPushConstantVariable("TestPushConstant.worldTransform", transform->GetTRS(), SH_PIPELINE_TYPE::GRAPHICS); + cmdBuffer->SetPushConstantVariable("TestPushConstant.eid", comp.GetEID(), SH_PIPELINE_TYPE::GRAPHICS); + cmdBuffer->SetPushConstantVariable("TestPushConstant.textColor", SHVec3 (1.0f, 1.0f, 1.0f), SH_PIPELINE_TYPE::GRAPHICS); + + cmdBuffer->SubmitPushConstants(SH_PIPELINE_TYPE::GRAPHICS); + + // call draw call + cmdBuffer->DrawArrays(4, comp.text.size(), 0, 0); + //glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, static_cast(textComp.lastRenderedCharacterIndex) + 1); + + } + + } + } + + void SHTextRenderingSubSystem::Exit(void) noexcept + { + + } + + Handle SHTextRenderingSubSystem::GetFontDataDescSetLayout(void) const noexcept + { + return fontDataDescSetLayout; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h new file mode 100644 index 00000000..d569ca91 --- /dev/null +++ b/SHADE_Engine/src/Graphics/MiddleEnd/TextRendering/SHTextRenderingSubSystem.h @@ -0,0 +1,65 @@ +#pragma once + +#include "Resource/SHHandle.h" +#include "Graphics/Pipeline/SHPipelineState.h" +#include "Math/SHMatrix.h" +#include "Math/Vector/SHVec3.h" + +namespace SHADE +{ + class SHVkLogicalDevice; + class SHVkDescriptorPool; + class SHVkDescriptorSetGroup; + class SHVkDescriptorSetLayout; + class SHVkBuffer; + class SHLightComponent; + class SHVkCommandBuffer; + class SHTextRendererComponent; + class SHVkPipeline; + class SHVkPipelineLayout; + class SHVkRenderpass; + class SHSubpass; + class SHVkShaderModule; + + class SHTextRenderingSubSystem + { + private: + struct ShaderPushConstantData + { + SHMatrix worldTransform; + uint32_t eid; + SHVec3 textColor; + }; + + //! Logical device for creation and destruction + Handle logicalDevice; + + //! Pipeline for rendering the text + Handle pipeline; + + //! Pipeline layout for the pipeline + Handle pipelineLayout; + + //! Descriptor set for font data access in shaders + Handle fontDataDescSetLayout; + + //! Super temporary. Global descriptor set needs to be revamped along with + //! entire graphics system. + std::function, uint32_t)> cameraDescSetBind; + + private: + void RecomputePositions(SHTextRendererComponent& textComp) noexcept; + + public: + void Init(Handle device, Handle compatibleRenderpass, Handle subpass, Handle descPool, Handle textVS, Handle textFS, std::function, uint32_t)> const& bindFunction) noexcept; + void Run(uint32_t frameIndex) noexcept; + + void Render (Handle cmdBuffer, uint32_t frameIndex) noexcept; + void Exit(void) noexcept; + + + Handle GetFontDataDescSetLayout (void) const noexcept; + + + }; +} diff --git a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp index 589e66d2..0333aa09 100644 --- a/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp +++ b/SHADE_Engine/src/Graphics/Pipeline/SHVkPipelineLayout.cpp @@ -79,9 +79,9 @@ namespace SHADE startOffset += pcInfo.size; } + stageFlags |= shaderModule->GetShaderStageFlagBits(); } - stageFlags |= shaderModule->GetShaderStageFlagBits(); } // After all the sizes of the push constant blocks have been added, record the size in the interface diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHAttachmentDescriptionType.h b/SHADE_Engine/src/Graphics/RenderGraph/SHAttachmentDescriptionType.h index c4d44ea8..16c07cdf 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHAttachmentDescriptionType.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHAttachmentDescriptionType.h @@ -3,7 +3,7 @@ namespace SHADE { // Used for attachment description creation for renderpass node - enum class SH_ATT_DESC_TYPE_FLAGS + enum class SH_RENDER_GRAPH_RESOURCE_FLAGS { COLOR = 0x01, COLOR_PRESENT = 0x02, @@ -11,7 +11,8 @@ namespace SHADE STENCIL = 0x08, DEPTH_STENCIL = 0x10, INPUT = 0x20, - STORAGE = 0x40 + STORAGE = 0x40, + SHARED = 0x80 // if resource is meant to be used across render graphs, mark this flag }; } diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp index e798bfe7..0580d650 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.cpp @@ -11,6 +11,9 @@ #include "SHAttachmentDescInitParams.h" #include "SHRenderGraphStorage.h" #include "Graphics/RenderGraph/SHRenderGraphNodeCompute.h" +#include "Tools/Utilities/SHUtilities.h" +#include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h" +#include "Graphics/RenderGraph/SHRenderToSwapchainImageSystem.h" namespace SHADE { @@ -49,7 +52,7 @@ namespace SHADE */ /***************************************************************************/ - void SHRenderGraph::AddResource(std::string resourceName, std::initializer_list typeFlags, uint32_t w /*= static_cast(-1)*/, uint32_t h /*= static_cast(-1)*/, vk::Format format/* = vk::Format::eB8G8R8A8Unorm*/, uint8_t levels /*= 1*/, vk::ImageUsageFlagBits usageFlags/* = {}*/, vk::ImageCreateFlagBits createFlags /*= {}*/) + void SHRenderGraph::AddResource(std::string resourceName, std::initializer_list typeFlags, uint32_t w /*= static_cast(-1)*/, uint32_t h /*= static_cast(-1)*/, vk::Format format/* = vk::Format::eB8G8R8A8Unorm*/, uint8_t levels /*= 1*/, vk::ImageUsageFlagBits usageFlags/* = {}*/, vk::ImageCreateFlagBits createFlags /*= {}*/) { // If we set to if (w == static_cast(-1) && h == static_cast(-1)) @@ -59,10 +62,41 @@ namespace SHADE format = renderGraphStorage->swapchain->GetSurfaceFormatKHR().format; } - auto resource = resourceManager->Create(renderGraphStorage, resourceName, typeFlags, format, w, h, levels, usageFlags, createFlags); + auto resource = renderGraphStorage->resourceHub->Create(renderGraphStorage, resourceName, typeFlags, format, w, h, levels, usageFlags, createFlags); renderGraphStorage->graphResources->try_emplace(resourceName, resource); } + void SHRenderGraph::LinkNonOwningResource(Handle resourceOrigin, std::string resourceName) noexcept + { + // resource to link + Handle resourceToLink = resourceOrigin->GetRenderGraphResource(resourceName); + if (!resourceToLink) + { + SHLOG_ERROR("Resource does not exist in render graph, cannot link resource."); + return; + } + + // Get the final layout of the resource from the previous render graph + vk::ImageLayout finalLayout{vk::ImageLayout::eUndefined}; + auto const& nodes = resourceOrigin->GetNodes(); + for (auto& node : nodes) + { + if (vk::ImageLayout layout = resourceToLink->GetInfoTracker()->GetLayout(node, {}); layout != vk::ImageLayout::eUndefined) + finalLayout = layout; + + for (auto& compute : node->nodeComputes) + { + if (vk::ImageLayout layout = resourceToLink->GetInfoTracker()->GetLayout(compute); layout != vk::ImageLayout::eUndefined) + finalLayout = layout; + } + } + + renderGraphStorage->graphResources->try_emplace(resourceName, resourceToLink); + + // add to this pool so that when we generate later, the attachment descriptions will make its initial layout the layout stored here + renderGraphStorage->nonOwningResourceInitialLayouts.emplace(resourceToLink.GetId().Raw, finalLayout); + } + /***************************************************************************/ /*! @@ -88,30 +122,59 @@ namespace SHADE return; } + // We first want to take all resources track their layout as undefined at the start of the node/renderpass + auto const resources = node->GetResources(); + for (auto& resource : resources) + { + resource->GetInfoTracker()->TrackLayout(node, {}, vk::ImageLayout::eUndefined); + } + // attempt to get all final layouts for all resources for (auto& subpass : node->subpasses) { for (auto& color : subpass->colorReferences) { // If final renderpass and attachment is a COLOR_PRESENT resource, make resource transition to present after last subpass - if (i == nodes.size() - 1 && (node->attResources[color.attachment]->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT))) + if (i == nodes.size() - 1 && (node->attResources[color.attachment]->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT))) resourceAttFinalLayouts[color.attachment] = vk::ImageLayout::ePresentSrcKHR; else resourceAttFinalLayouts[color.attachment] = color.layout; + + node->attResources[color.attachment]->infoTracker->TrackLayout(node, subpass, color.layout); } for (auto& depth : subpass->depthReferences) + { resourceAttFinalLayouts[depth.attachment] = depth.layout; + node->attResources[depth.attachment]->infoTracker->TrackLayout(node, subpass, depth.layout); + } for (auto& input : subpass->inputReferences) + { resourceAttFinalLayouts[input.attachment] = input.layout; + node->attResources[input.attachment]->infoTracker->TrackLayout(node, subpass, input.layout); + } } for (uint32_t j = 0; j < node->attachmentDescriptions.size(); ++j) { auto& att = node->attachmentDescriptions[j]; - att.initialLayout = vk::ImageLayout::eUndefined; + auto& resource = node->attResources[j]; + + // If resource is from another render graph, use the final layout it had when it was last used in that graph. This is initialized in LinkNonOwningResource. + // We also want to load the attachment, not "don't care". + if (resource->resourceTypeFlags & SHUtilities::ConvertEnum(SH_RENDER_GRAPH_RESOURCE_FLAGS::SHARED) && + renderGraphStorage->nonOwningResourceInitialLayouts.contains(resource.GetId().Raw)) + { + att.initialLayout = renderGraphStorage->nonOwningResourceInitialLayouts.at (resource.GetId().Raw); + att.loadOp = vk::AttachmentLoadOp::eLoad; + att.stencilLoadOp = vk::AttachmentLoadOp::eLoad; + } + else + att.initialLayout = vk::ImageLayout::eUndefined; + att.finalLayout = resourceAttFinalLayouts[j]; + resource->GetInfoTracker()->TrackLayout(node, {}, att.finalLayout); } ++i; } @@ -146,12 +209,13 @@ namespace SHADE attDesc.stencilLoadOp = vk::AttachmentLoadOp::eLoad; attDesc.stencilStoreOp = vk::AttachmentStoreOp::eStore; - // TODO: Stencil load and store - // When an image is done being used in a renderpass, the image layout will end up being the finalLayout // value of the attachment description. We want this to carry over to the next renderpass; specifically // to have the initialLayout of the attachment description in the next renderpass match the finalLayout in the previous. attDesc.initialLayout = predAttDesc.finalLayout; + + // We also want to track it + predResource->GetInfoTracker()->TrackLayout(node, {}, attDesc.initialLayout); } } } @@ -225,12 +289,12 @@ namespace SHADE for (auto& inputAtt : subpass->inputReferences) { auto resource = node->attResources[inputAtt.attachment]; - if (resource->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::INPUT)) + if (resource->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT)) { - if (resource->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR) || - resource->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT)) + if (resource->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR) || + resource->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT)) colorRead |= (1 << i); - else if (resource->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL)) + else if (resource->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH_STENCIL)) depthRead |= (1 << i); } else @@ -323,6 +387,12 @@ namespace SHADE } } + void SHRenderGraph::ConfigureSubSystems(void) noexcept + { + if (renderToSwapchainImageSystem) + renderToSwapchainImageSystem->ConstructPipelines(renderGraphStorage->logicalDevice); + } + /***************************************************************************/ /*! @@ -354,17 +424,17 @@ namespace SHADE */ /***************************************************************************/ - void SHRenderGraph::Init(std::string graphName, Handle logicalDevice, Handle swapchain) noexcept + void SHRenderGraph::Init(std::string graphName, Handle logicalDevice, Handle swapchain, SHResourceHub* resourceHub) noexcept { - resourceManager = std::make_shared(); + //resourceHub = std::make_shared(); - renderGraphStorage = resourceManager->Create(); - renderGraphStorage->graphResources = resourceManager->Create>>(); + renderGraphStorage = resourceHub->Create(); + renderGraphStorage->graphResources = resourceHub->Create>>(); renderGraphStorage->logicalDevice = logicalDevice; renderGraphStorage->swapchain = swapchain; - renderGraphStorage->resourceManager = resourceManager; + renderGraphStorage->resourceHub = resourceHub; renderGraphStorage->descriptorPool = logicalDevice->CreateDescriptorPools(); name = std::move(graphName); @@ -384,7 +454,6 @@ namespace SHADE SHRenderGraph::SHRenderGraph(void) noexcept : renderGraphStorage{} , nodes{} - , resourceManager{nullptr} { } @@ -392,7 +461,6 @@ namespace SHADE : renderGraphStorage{ rhs.renderGraphStorage } , nodeIndexing{ std::move(rhs.nodeIndexing) } , nodes{ std::move(rhs.nodes) } - , resourceManager{ std::move(rhs.resourceManager) } , name { std::move(rhs.name) } { @@ -406,7 +474,6 @@ namespace SHADE renderGraphStorage = rhs.renderGraphStorage; nodeIndexing = std::move(rhs.nodeIndexing); nodes = std::move(rhs.nodes); - resourceManager = std::move(rhs.resourceManager); name = std::move(rhs.name); return *this; @@ -472,11 +539,38 @@ namespace SHADE } } - auto node = nodes.emplace_back(resourceManager->Create(nodeName, renderGraphStorage, std::move(descInitParams), std::move(predecessors))); + auto node = nodes.emplace_back(renderGraphStorage->resourceHub->Create(nodeName, renderGraphStorage, std::move(descInitParams), std::move(predecessors))); nodeIndexing.emplace(std::move(nodeName), static_cast(nodes.size()) - 1u); return node; } + void SHRenderGraph::AddRenderToSwapchainNode(std::string toSwapchainResource, std::string swapchainResource, std::initializer_list predecessorNodes, std::pair, Handle> shaderModules) noexcept + { + for (auto& node : predecessorNodes) + { + if (!nodeIndexing.contains(node)) + return; + } + + if (renderGraphStorage->graphResources->contains(toSwapchainResource) && renderGraphStorage->graphResources->contains(swapchainResource)) + { + auto newNode = AddNode("Render To Present", { ResourceInstruction (toSwapchainResource.c_str()), ResourceInstruction(swapchainResource.c_str()) }, predecessorNodes); + auto newSubpass = newNode->AddSubpass("Render"); + newSubpass->AddColorOutput(swapchainResource); + newSubpass->AddInput(toSwapchainResource); + + renderToSwapchainImageSystem = renderGraphStorage->resourceHub->Create (newNode, newSubpass, shaderModules); + + newSubpass->AddExteriorDrawCalls([=](Handle& cmdBuffer, uint32_t frameIndex) + { + cmdBuffer->BindPipeline(renderToSwapchainImageSystem->GetPipeline()); + + // draw a quad. + cmdBuffer->DrawIndexed(4, 0, 0); + }); + } + } + /***************************************************************************/ /*! @@ -497,6 +591,7 @@ namespace SHADE ConfigureSubpasses(); ConfigureRenderpasses(); ConfigureFramebuffers(); + ConfigureSubSystems(); } /***************************************************************************/ @@ -539,7 +634,10 @@ namespace SHADE { // resize resources for (auto& [name, resource] : *renderGraphStorage->graphResources) - resource->HandleResize(newWidth, newHeight); + { + if (!renderGraphStorage->nonOwningResourceInitialLayouts.contains (resource.GetId().Raw)) + resource->HandleResize(newWidth, newHeight); + } for (auto& node : nodes) { @@ -556,6 +654,11 @@ namespace SHADE } + std::vector> const& SHRenderGraph::GetNodes(void) const noexcept + { + return nodes; + } + Handle SHRenderGraph::GetRenderGraphResource(std::string const& resourceName) const noexcept { if (renderGraphStorage->graphResources->contains(resourceName)) diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h index 741cc522..0a9ed376 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraph.h @@ -32,6 +32,7 @@ namespace SHADE class SHGraphicsGlobalData; class SHVkDescriptorPool; class SHRenderGraphStorage; + class SHRenderToSwapchainImageSystem; class SH_API SHRenderGraph { @@ -41,7 +42,7 @@ namespace SHADE std::string resourceName; bool dontClearOnLoad; - ResourceInstruction (char const* resourceName, bool dontClearOnLoad = false) noexcept; + ResourceInstruction(char const* resourceName, bool dontClearOnLoad = false) noexcept; }; private: @@ -52,6 +53,7 @@ namespace SHADE void ConfigureAttachmentDescriptions (void) noexcept; void ConfigureSubpasses (void) noexcept; void ConfigureRenderpasses (void) noexcept; + void ConfigureSubSystems (void) noexcept; void ConfigureFramebuffers (void) noexcept; /*-----------------------------------------------------------------------*/ @@ -66,12 +68,12 @@ namespace SHADE //! Render graph nodes std::vector> nodes; - //! Resource library for graph handles - std::shared_ptr resourceManager; - //! Name of the RenderGraph std::string name; + //! For rendering onto the swapchain + Handle renderToSwapchainImageSystem; + public: /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ @@ -84,9 +86,11 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* PUBLIC MEMBER FUNCTIONS */ /*-----------------------------------------------------------------------*/ - void Init (std::string graphName, Handle logicalDevice, Handle swapchain) noexcept; - void AddResource(std::string resourceName, std::initializer_list typeFlags, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint8_t levels = 1, vk::ImageUsageFlagBits usageFlags = {}, vk::ImageCreateFlagBits createFlags = {}); + void Init (std::string graphName, Handle logicalDevice, Handle swapchain, SHResourceHub* resourceHub) noexcept; + void AddResource(std::string resourceName, std::initializer_list typeFlags, uint32_t w = static_cast(-1), uint32_t h = static_cast(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint8_t levels = 1, vk::ImageUsageFlagBits usageFlags = {}, vk::ImageCreateFlagBits createFlags = {}); + void LinkNonOwningResource (Handle resourceOrigin, std::string resourceName) noexcept; Handle AddNode (std::string nodeName, std::initializer_list resourceInstruction, std::initializer_list predecessorNodes) noexcept; + void AddRenderToSwapchainNode (std::string toSwapchainResource, std::string swapchainResource, std::initializer_list predecessorNodes, std::pair, Handle> shaderModules) noexcept; void Generate (void) noexcept; void CheckForNodeComputes (void) noexcept; @@ -97,9 +101,30 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* SETTERS AND GETTERS */ /*-----------------------------------------------------------------------*/ - Handle GetNode (std::string const& nodeName) const noexcept; - Handle GetRenderGraphResource (std::string const& resourceName) const noexcept; + Handle GetNode (std::string const& nodeName) const noexcept; + std::vector> const& GetNodes (void) const noexcept; + Handle GetRenderGraphResource (std::string const& resourceName) const noexcept; }; } #endif + + + +/* Some notes here: + * - The graph wasn't implemented with the intention of it being this versatile. + * - Graphs weren't meant to share resources and hence the generating of the render graph doesn't account for it. + * However, because it was eventually necessary that some resources had to be shared and its contents carried over to + * other graphs, the functionality was implemented through a link function in SHRenderGraph.cpp to facilitate this linkage. + * This should ideally be replaced by an implementation more self-contained, perhaps through a higher level class like a canvas + * that manage the resources instead and can facilitate such linking of resources. Either that, or we allow only 1 render graph, + * but different matrices (SHRenderer) can be used in different nodes. + * - There are also way too many hash maps created for ease of access. This definitely can be cut down. + * - + * + */ + + + + + diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp index 3f6be1d9..0f9379fe 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.cpp @@ -46,7 +46,7 @@ namespace SHADE for (uint32_t j = 0; j < attResources.size(); ++j) { - uint32_t imageViewIndex = (attResources[j]->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT)) ? i : 0; + uint32_t imageViewIndex = (attResources[j]->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT)) ? i : 0; imageViews[j] = attResources[j]->imageViews[imageViewIndex]; // We want the minimum dimensions for the framebuffer because the image attachments referenced cannot have dimensions smaller than the framebuffer's @@ -74,7 +74,7 @@ namespace SHADE for (uint32_t j = 0; j < attResources.size(); ++j) { - uint32_t imageViewIndex = (attResources[j]->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT)) ? i : 0; + uint32_t imageViewIndex = (attResources[j]->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT)) ? i : 0; imageViews[j] = attResources[j]->imageViews[imageViewIndex]; // We want the minimum dimensions for the framebuffer because the image attachments referenced cannot have dimensions smaller than the framebuffer's @@ -160,7 +160,7 @@ namespace SHADE newDesc.format = attResources[i]->resourceFormat; - if (attResources[i]->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT)) + if (attResources[i]->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT)) containsSwapchainImage = true; resourceAttachmentMapping->try_emplace(attResources[i].GetId().Raw, i); @@ -250,7 +250,7 @@ namespace SHADE // Add subpass to container and create mapping for it subpasses.emplace_back ( - graphStorage->resourceManager->Create + graphStorage->resourceHub->Create ( subpassName, graphStorage, GetHandle(), static_cast(subpasses.size()), @@ -259,7 +259,7 @@ namespace SHADE ); subpassIndexing.try_emplace(subpassName, static_cast(subpasses.size()) - 1u); Handle subpass = subpasses.back(); - subpass->Init(*graphStorage->resourceManager); + subpass->Init(*graphStorage->resourceHub); // Register the SuperBatch batcher.RegisterSuperBatch(subpass->GetSuperBatch()); @@ -279,10 +279,18 @@ namespace SHADE nodeComputeResources.push_back(resource); } + // need to use for tracking resources + std::vector> temp (nodeComputeResources); + // Create the subpass compute with the resources - auto nodeCompute = graphStorage->resourceManager->Create(std::move(nodeName), graphStorage, computeShaderModule, std::move(nodeComputeResources), std::move (dynamicBufferBindings), nodeComputes.empty()); + auto nodeCompute = graphStorage->resourceHub->Create(std::move(nodeName), graphStorage, computeShaderModule, std::move(nodeComputeResources), std::move (dynamicBufferBindings), nodeComputes.empty()); nodeComputes.push_back(nodeCompute); + for (auto& resource : temp) + { + resource->GetInfoTracker()->TrackLayout(nodeCompute); + } + return nodeCompute; } @@ -405,4 +413,9 @@ namespace SHADE return {}; } + std::vector> const& SHRenderGraphNode::GetResources(void) const noexcept + { + return attResources; + } + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h index 299578f2..775d64f7 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNode.h @@ -117,6 +117,7 @@ namespace SHADE Handle GetRenderpass(void) const noexcept; Handle GetSubpass(std::string_view subpassName) const noexcept; Handle GetResource (uint32_t resourceIndex) const noexcept; + std::vector> const& GetResources (void) const noexcept; friend class SHRenderGraph; }; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp index f4a103f7..2f8fd968 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp @@ -45,6 +45,7 @@ namespace SHADE // save the resources resources = std::move (subpassComputeResources); + //Get the descriptor set layouts required to allocate. We only want the ones for allocate because //global descriptors are already bound in the main system. auto const& graphResourceLayout = computePipeline->GetPipelineLayout()->GetDescriptorSetLayoutsPipeline()[SHGraphicsConstants::DescriptorSetIndex::RENDERGRAPH_RESOURCE]; @@ -65,7 +66,7 @@ namespace SHADE if (layouts.size() == SHGraphicsConstants::DescriptorSetIndex::RENDERGRAPH_NODE_COMPUTE_RESOURCE + 1) { // create compute resources - computeResource = graphStorage->resourceManager->Create(); + computeResource = graphStorage->resourceHub->Create(); auto computeResourceLayout = layouts[SHGraphicsConstants::DescriptorSetIndex::RENDERGRAPH_NODE_COMPUTE_RESOURCE]; computeResource->descSet = graphStorage->descriptorPool->Allocate({ computeResourceLayout }, { 1 }); #ifdef _DEBUG @@ -119,7 +120,7 @@ namespace SHADE // loop through bindings and write descriptor sets for (auto& binding : pipelineDescSetLayouts->GetBindings()) { - uint32_t imageIndex = (resources[i]->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT)) ? frameIndex : 0; + uint32_t imageIndex = (resources[i]->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT)) ? frameIndex : 0; SHVkDescriptorSetGroup::viewSamplerLayout vsl = std::make_tuple(resources[i]->GetImageView(imageIndex), Handle{}, vk::ImageLayout::eGeneral); graphResourceDescSets[frameIndex]->ModifyWriteDescImage(SHGraphicsConstants::DescriptorSetIndex::RENDERGRAPH_RESOURCE, binding.BindPoint, { &vsl, 1 }); @@ -154,7 +155,7 @@ namespace SHADE .newLayout = vk::ImageLayout::eGeneral, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .image = resource->GetImage((resource->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT)) ? i : 0)->GetVkImage(), + .image = resource->GetImage((resource->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT)) ? i : 0)->GetVkImage(), .subresourceRange = vk::ImageSubresourceRange { .aspectMask = resource->imageAspectFlags, diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.cpp index 4d4099c6..ce7e1622 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.cpp @@ -6,9 +6,41 @@ #include "Graphics/Buffers/SHVkBuffer.h" #include "Graphics/SHVkUtil.h" #include "SHRenderGraphStorage.h" +#include "Graphics/RenderGraph/SHSubpass.h" +#include "Graphics/RenderGraph/SHRenderGraphNode.h" namespace SHADE { + void SHRenderGraphResource::InfoTracker::TrackLayout(Handle node, Handle subpass, vk::ImageLayout layout) noexcept + { + NodeSubpassPair nodeSubpassPair = std::pair(node, subpass); + layoutTracker[std::hash{}(nodeSubpassPair)] = layout; + } + + + void SHRenderGraphResource::InfoTracker::TrackLayout(Handle compute) noexcept + { + computeTracker.emplace (compute.GetId().Raw); + } + + vk::ImageLayout SHRenderGraphResource::InfoTracker::GetLayout(Handle node, Handle subpass) const noexcept + { + NodeSubpassPair nodeSubpassPair = std::pair(node, subpass); + std::size_t hashedValue = std::hash{}(nodeSubpassPair); + + if (layoutTracker.contains(hashedValue)) + return layoutTracker.at(hashedValue); + else + return vk::ImageLayout::eUndefined; + } + + vk::ImageLayout SHRenderGraphResource::InfoTracker::GetLayout(Handle compute) const noexcept + { + if (computeTracker.contains (compute.GetId().Raw)) + return vk::ImageLayout::eGeneral; + else + return vk::ImageLayout::eUndefined; + } /***************************************************************************/ /*! @@ -46,7 +78,7 @@ namespace SHADE */ /***************************************************************************/ - SHRenderGraphResource::SHRenderGraphResource(Handle renderGraphStorage, std::string const& name, std::initializer_list typeFlags, vk::Format format, uint32_t w, uint32_t h, uint8_t levels, vk::ImageUsageFlagBits usageFlags, vk::ImageCreateFlagBits createFlags) noexcept + SHRenderGraphResource::SHRenderGraphResource(Handle renderGraphStorage, std::string const& name, std::initializer_list typeFlags, vk::Format format, uint32_t w, uint32_t h, uint8_t levels, vk::ImageUsageFlagBits usageFlags, vk::ImageCreateFlagBits createFlags) noexcept : graphStorage{renderGraphStorage} , resourceTypeFlags{ } , resourceFormat{ format } @@ -58,9 +90,9 @@ namespace SHADE , resourceName{ name } { // If the resource type is an arbitrary image and not swapchain image - if (typeFlags.size() == 1 && *typeFlags.begin() == SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT) + if (typeFlags.size() == 1 && *typeFlags.begin() == SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT) { - resourceTypeFlags |= static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT); + resourceTypeFlags |= static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT); // Prepare image view details SHImageViewDetails viewDetails @@ -98,30 +130,30 @@ namespace SHADE // Check the resource type and set image usage flags and image aspect flags accordingly switch (type) { - case SH_ATT_DESC_TYPE_FLAGS::COLOR: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR: usage |= vk::ImageUsageFlagBits::eColorAttachment; imageAspectFlags |= vk::ImageAspectFlagBits::eColor; break; - case SH_ATT_DESC_TYPE_FLAGS::DEPTH: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH: usage |= vk::ImageUsageFlagBits::eDepthStencilAttachment; imageAspectFlags |= vk::ImageAspectFlagBits::eDepth; break; - case SH_ATT_DESC_TYPE_FLAGS::STENCIL: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::STENCIL: usage |= vk::ImageUsageFlagBits::eDepthStencilAttachment; imageAspectFlags |= vk::ImageAspectFlagBits::eStencil; break; - case SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH_STENCIL: usage |= vk::ImageUsageFlagBits::eDepthStencilAttachment; imageAspectFlags |= vk::ImageAspectFlagBits::eStencil | vk::ImageAspectFlagBits::eDepth; break; - case SH_ATT_DESC_TYPE_FLAGS::INPUT: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::INPUT: usage |= vk::ImageUsageFlagBits::eInputAttachment; usage |= vk::ImageUsageFlagBits::eSampled; break; - case SH_ATT_DESC_TYPE_FLAGS::STORAGE: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::STORAGE: usage |= vk::ImageUsageFlagBits::eStorage; break; - case SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT: { SHLOG_ERROR ("COLOR_PRESENT cannot be with other resource type flags. "); return; @@ -151,6 +183,8 @@ namespace SHADE imageViews.push_back(imageView); SET_VK_OBJ_NAME(graphStorage->logicalDevice, vk::ObjectType::eImageView, imageView->GetImageView(), "[Image View] " + resourceName); } + + infoTracker = renderGraphStorage->resourceHub->Create(); } /***************************************************************************/ @@ -175,6 +209,7 @@ namespace SHADE , mipLevels{ rhs.mipLevels } , imageAspectFlags{ rhs.imageAspectFlags } , graphStorage{rhs.graphStorage} + , infoTracker {std::move (rhs.infoTracker)} { } @@ -207,6 +242,7 @@ namespace SHADE mipLevels = rhs.mipLevels; imageAspectFlags = rhs.imageAspectFlags; graphStorage = rhs.graphStorage; + infoTracker = std::move(infoTracker); return *this; } @@ -229,7 +265,7 @@ namespace SHADE width = newWidth; height = newHeight; - if ((resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT)) == 0) + if ((resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT)) == 0) { // prepare image view details SHImageViewDetails viewDetails @@ -347,4 +383,9 @@ namespace SHADE return resourceName; } + SHADE::Handle SHRenderGraphResource::GetInfoTracker(void) const noexcept + { + return infoTracker; + } + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.h index e2fc5d8d..7ac2b824 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphResource.h @@ -6,6 +6,7 @@ #include "Resource/SHHandle.h" #include "Graphics/SHVulkanIncludes.h" #include "SH_API.h" +#include namespace SHADE { @@ -16,11 +17,41 @@ namespace SHADE class SHVkCommandBuffer; class SHVkBuffer; class SHRenderGraphStorage; + class SHRenderGraphNode; + class SHSubpass; + class SHRenderGraphNodeCompute; static constexpr uint32_t NON_SWAPCHAIN_RESOURCE_INDEX = 0; class SH_API SHRenderGraphResource { + public: + // For keeping track of resources as it gets passed down the pipeline (between multiple render graphs) + class InfoTracker + { + private: + using NodeSubpassPair = std::pair, Handle>; + + //! key here is the render graph node and subpass name combined, value is the layout of the resource at that node and subpass + std::unordered_map layoutTracker; + + //! if a resource is involved in a compute process, record it here + std::unordered_set computeTracker; + + public: + /*-----------------------------------------------------------------------*/ + /* PUBLIC MEMBER FUNCTIONS */ + /*-----------------------------------------------------------------------*/ + void TrackLayout (Handle node, Handle subpass, vk::ImageLayout layout) noexcept; + void TrackLayout (Handle compute) noexcept; + + /*-----------------------------------------------------------------------*/ + /* SETTERS AND GETTERS */ + /*-----------------------------------------------------------------------*/ + vk::ImageLayout GetLayout(Handle node, Handle subpass) const noexcept; + vk::ImageLayout GetLayout(Handle compute) const noexcept; + }; + private: /*-----------------------------------------------------------------------*/ /* PRIVATE MEMBER VARIABLES */ @@ -62,12 +93,14 @@ namespace SHADE //! usage flags vk::ImageUsageFlags usage = {}; + //! For tracking resource states in stages of the render graphs + Handle infoTracker; public: /*-----------------------------------------------------------------------*/ /* CTORS AND DTORS */ /*-----------------------------------------------------------------------*/ - SHRenderGraphResource(Handle renderGraphStorage, std::string const& name, std::initializer_list typeFlags, vk::Format format, uint32_t w, uint32_t h, uint8_t levels, vk::ImageUsageFlagBits usageFlags, vk::ImageCreateFlagBits createFlags) noexcept; + SHRenderGraphResource(Handle renderGraphStorage, std::string const& name, std::initializer_list typeFlags, vk::Format format, uint32_t w, uint32_t h, uint8_t levels, vk::ImageUsageFlagBits usageFlags, vk::ImageCreateFlagBits createFlags) noexcept; SHRenderGraphResource(SHRenderGraphResource&& rhs) noexcept; SHRenderGraphResource& operator=(SHRenderGraphResource&& rhs) noexcept; ~SHRenderGraphResource(void) noexcept; @@ -89,6 +122,7 @@ namespace SHADE Handle GetImage (uint32_t index = NON_SWAPCHAIN_RESOURCE_INDEX) const noexcept; uint8_t GetMipLevels (void) const noexcept; std::string GetName (void) const noexcept; + Handle GetInfoTracker (void) const noexcept; friend class SHRenderGraphNode; friend class SHRenderGraph; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphStorage.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphStorage.h index 54ef705a..d02d8d39 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphStorage.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphStorage.h @@ -20,7 +20,7 @@ namespace SHADE Handle swapchain; //! Resource manager for creation of objects - std::shared_ptr resourceManager; + SHResourceHub* resourceHub; //! Descriptor pool for the descriptor sets to be created in the subpasses Handle descriptorPool; @@ -28,6 +28,10 @@ namespace SHADE //! For accessing resources anywhere in the graph Handle>> graphResources; + //! HACK: Because there's no interface to specify a dependency between graphs, this will help attachment descriptions + //! use the correct layout, when a resource is shared between graphs + std::unordered_map nonOwningResourceInitialLayouts{}; + //SHRenderGraphStorage(void) noexcept; //SHRenderGraphStorage(SHRenderGraphStorage&& rhs) noexcept; //SHRenderGraphStorage& operator=(SHRenderGraphStorage&& rhs) noexcept; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderToSwapchainImageSystem.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderToSwapchainImageSystem.cpp new file mode 100644 index 00000000..e4f020e4 --- /dev/null +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderToSwapchainImageSystem.cpp @@ -0,0 +1,44 @@ +#include "SHpch.h" +#include "SHRenderToSwapchainImageSystem.h" +#include "Graphics/Devices/SHVkLogicalDevice.h" +#include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h" +#include "Graphics/RenderGraph/SHRenderGraphNode.h" + +namespace SHADE +{ + + SHRenderToSwapchainImageSystem::SHRenderToSwapchainImageSystem(Handle node, Handle subpass, std::pair, Handle> shaders) noexcept + : renderGraphNode {node} + , subpass{subpass} + , shaderModules{shaders} + , pipeline{} + , pipelineLayout{} + { + + } + + void SHRenderToSwapchainImageSystem::ConstructPipelines(Handle logicalDevice) noexcept + { + auto pipelineLayout = logicalDevice->CreatePipelineLayout(SHPipelineLayoutParams + { + .shaderModules = {shaderModules.first, shaderModules.second}, + .globalDescSetLayouts = SHGraphicsGlobalData::GetDescSetLayouts(), + }); + + auto newPipeline = logicalDevice->CreateGraphicsPipeline(pipelineLayout, nullptr, renderGraphNode->GetRenderpass(), subpass); + + SHInputAssemblyState inputAssembly{}; + inputAssembly.topology = vk::PrimitiveTopology::eTriangleFan; + + newPipeline->GetPipelineState().SetInputAssemblyState(inputAssembly); + + newPipeline->ConstructPipeline(); + + } + + Handle SHRenderToSwapchainImageSystem::GetPipeline(void) const noexcept + { + return pipeline; + } + +} \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderToSwapchainImageSystem.h b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderToSwapchainImageSystem.h new file mode 100644 index 00000000..808fd9a0 --- /dev/null +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderToSwapchainImageSystem.h @@ -0,0 +1,43 @@ +#pragma once + +#include "Resource/SHHandle.h" +#include + +namespace SHADE +{ + class SHVkPipeline; + class SHVkPipelineLayout; + class SHVkShaderModule; + class SHRenderGraphNode; + class SHSubpass; + class SHVkLogicalDevice; + class SHSubpass; + + + class SHRenderToSwapchainImageSystem + { + private: + //! Render Graph node to get the renderpass from to initialize the pipeline + Handle renderGraphNode; + + //! Subpass to initialize the pipeline with + Handle subpass; + + //! Shader module required to render the quad on screen + std::pair, Handle> shaderModules; + + //! pipeline required to draw the image + Handle pipeline; + + //! Pipeline layout of the pipeline + Handle pipelineLayout; + + public: + SHRenderToSwapchainImageSystem (Handle node, Handle subpass, std::pair, Handle> shaders) noexcept; + + void ConstructPipelines (Handle logicalDevice) noexcept; + + Handle GetPipeline (void) const noexcept; + + }; +} diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.cpp index dfa5977b..1eae9768 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.cpp @@ -148,18 +148,18 @@ namespace SHADE */ /***************************************************************************/ - void SHSubpass::AddDepthOutput(std::string resourceToReference, SH_ATT_DESC_TYPE_FLAGS attachmentDescriptionType) noexcept + void SHSubpass::AddDepthOutput(std::string resourceToReference, SH_RENDER_GRAPH_RESOURCE_FLAGS attachmentDescriptionType) noexcept { vk::ImageLayout imageLayout; switch (attachmentDescriptionType) { - case SH_ATT_DESC_TYPE_FLAGS::DEPTH: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH: imageLayout = vk::ImageLayout::eDepthAttachmentOptimal; break; - case SH_ATT_DESC_TYPE_FLAGS::STENCIL: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::STENCIL: imageLayout = vk::ImageLayout::eStencilAttachmentOptimal; break; - case SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL: + case SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH_STENCIL: imageLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal; break; default: @@ -211,7 +211,7 @@ namespace SHADE // Draw all the exterior draw calls for (auto& drawCall : exteriorDrawCalls) { - drawCall(commandBuffer); + drawCall(commandBuffer, frameIndex); } commandBuffer->EndLabeledSegment(); } @@ -221,7 +221,7 @@ namespace SHADE UpdateWriteDescriptors(); } - void SHSubpass::AddExteriorDrawCalls(std::function&)> const& newDrawCall) noexcept + void SHSubpass::AddExteriorDrawCalls(std::function&, uint32_t)> const& newDrawCall) noexcept { exteriorDrawCalls.push_back(newDrawCall); } @@ -315,7 +315,7 @@ namespace SHADE auto resource = graphStorage->graphResources->at(inputNames[binding.BindPoint]); // If resource is swapchain image, get the correct image, if not just get 0. - uint32_t viewIndex = (resource->resourceTypeFlags & static_cast(SH_ATT_DESC_TYPE_FLAGS::COLOR_PRESENT)) ? i : 0; + uint32_t viewIndex = (resource->resourceTypeFlags & static_cast(SH_RENDER_GRAPH_RESOURCE_FLAGS::COLOR_PRESENT)) ? i : 0; // layout is GENERAL if image is meant to be used as storage image, if not use SHADER_READ_ONLY_OPTINAL vk::ImageLayout descriptorLayout = (binding.Type == vk::DescriptorType::eStorageImage) ? vk::ImageLayout::eGeneral : vk::ImageLayout::eShaderReadOnlyOptimal; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.h b/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.h index c82ebdd0..c28ff919 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.h +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHSubpass.h @@ -76,7 +76,7 @@ namespace SHADE //! after we draw everything from the batch. Because of this, these draw calls //! are always the last things drawn, so DO NOT USE THIS FUNCTIONALITY FOR ANYTHING //! COMPLEX. - std::vector&)>> exteriorDrawCalls; + std::vector&, uint32_t)>> exteriorDrawCalls; /// For identifying subpasses std::string name; @@ -95,11 +95,11 @@ namespace SHADE // Preparation functions void AddColorOutput(std::string resourceToReference) noexcept; void AddGeneralColorOutput(std::string resourceToReference) noexcept; - void AddDepthOutput(std::string resourceToReference, SH_ATT_DESC_TYPE_FLAGS attachmentDescriptionType = SH_ATT_DESC_TYPE_FLAGS::DEPTH_STENCIL) noexcept; + void AddDepthOutput(std::string resourceToReference, SH_RENDER_GRAPH_RESOURCE_FLAGS attachmentDescriptionType = SH_RENDER_GRAPH_RESOURCE_FLAGS::DEPTH_STENCIL) noexcept; void AddGeneralDepthOutput(std::string resourceToReference) noexcept; void AddInput(std::string resourceToReference) noexcept; void AddGeneralInput (std::string resourceToReference) noexcept; - void AddExteriorDrawCalls(std::function&)> const& newDrawCall) noexcept; + void AddExteriorDrawCalls(std::function&, uint32_t)> const& newDrawCall) noexcept; // Runtime functions void Execute(Handle& commandBuffer, Handle descPool, uint32_t frameIndex) noexcept; diff --git a/SHADE_Engine/src/Resource/SHResourceManager.cpp b/SHADE_Engine/src/Resource/SHResourceManager.cpp index 077835f5..5cb93bb0 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.cpp +++ b/SHADE_Engine/src/Resource/SHResourceManager.cpp @@ -25,7 +25,8 @@ namespace SHADE std::unordered_map> SHResourceManager::typedFreeFuncMap; std::vector SHResourceManager::loadedAssetData; bool SHResourceManager::textureChanged = false; - bool SHResourceManager::meshChanged = false; + bool SHResourceManager::meshChanged = false; + bool SHResourceManager::fontChanged = false; /*-----------------------------------------------------------------------------------*/ /* Function Definitions */ @@ -76,6 +77,11 @@ namespace SHADE gfxSystem->BuildTextures(); textureChanged = false; } + if (fontChanged) + { + gfxSystem->BuildFonts(); + fontChanged = false; + } // Free CPU Resources for (auto assetId : loadedAssetData) diff --git a/SHADE_Engine/src/Resource/SHResourceManager.h b/SHADE_Engine/src/Resource/SHResourceManager.h index dae20f99..6afdf2b0 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.h +++ b/SHADE_Engine/src/Resource/SHResourceManager.h @@ -26,6 +26,7 @@ of DigiPen Institute of Technology is prohibited. #include "Graphics/MiddleEnd/Interface/SHMaterial.h" #include "Graphics/MiddleEnd/Materials/SHMaterialSpec.h" #include "Assets/Asset Types/SHMaterialAsset.h" +#include "Graphics/MiddleEnd/TextRendering/SHFont.h" namespace SHADE { @@ -47,8 +48,8 @@ namespace SHADE template<> struct SHResourceLoader { using AssetType = SHShaderAsset; }; template<> struct SHResourceLoader { using AssetType = SHMaterialAsset; }; template<> struct SHResourceLoader { using AssetType = SHMaterialSpec; }; - - /// + template<> struct SHResourceLoader { using AssetType = SHFontAsset; }; +/// /// Static class responsible for loading and caching runtime resources from their /// serialised Asset IDs. /// @@ -178,6 +179,7 @@ namespace SHADE // Dirty Flags static bool meshChanged; static bool textureChanged; + static bool fontChanged; /*---------------------------------------------------------------------------------*/ /* Helper Functions */ diff --git a/SHADE_Engine/src/Resource/SHResourceManager.hpp b/SHADE_Engine/src/Resource/SHResourceManager.hpp index d0488e66..51ee356a 100644 --- a/SHADE_Engine/src/Resource/SHResourceManager.hpp +++ b/SHADE_Engine/src/Resource/SHResourceManager.hpp @@ -39,6 +39,7 @@ namespace SHADE !std::is_same_v && !std::is_same_v && !std::is_same_v && + !std::is_same_v && !std::is_same_v ) { @@ -337,5 +338,12 @@ namespace SHADE return matHandle; } + else if constexpr (std::is_same_v) + { + loadedAssetData.emplace_back(assetId); + fontChanged = true; + + return gfxSystem->AddFont(assetData); + } } } diff --git a/premake5.lua b/premake5.lua index 2164d649..26f181b5 100644 --- a/premake5.lua +++ b/premake5.lua @@ -25,10 +25,11 @@ workspace "SHADE" include "SHADE_CSharp" group "Dependencies" - include "Dependencies/msdf" + include "Dependencies/FontCompiler/Dependencies/msdf" include "Dependencies/imgui" --include "Dependencies/tracy" include "Dependencies/yamlcpp" include "Dependencies/reactphysics3d" include "Dependencies/ModelCompiler" + include "Dependencies/FontCompiler" group ""