From 033a3b1dd1846f6106da2b8fda19b54446e74a9d Mon Sep 17 00:00:00 2001 From: Light Date: Sat, 10 Jul 2021 19:59:27 +0430 Subject: [PATCH] Shader files: hlsl & glsl - Moved #define LT_ENGINE_RESOURCES_*_SHADER_* definitions to files * Note: This was done so for a gcc compilation error caused by R"()" string - Removed test print fps in 'Application::GameLoop()' --- Engine/premake5.lua | 11 ++- Engine/res/Shaders/Quad/Quad_PS.glsl | 10 +++ Engine/res/Shaders/Quad/Quad_PS.hlsl | 4 + Engine/res/Shaders/Quad/Quad_VS.glsl | 17 +++++ Engine/res/Shaders/Quad/Quad_VS.hlsl | 14 ++++ Engine/res/Shaders/QuadShader.h | 63 --------------- Engine/res/Shaders/Texture/Texture_PS.glsl | 12 +++ Engine/res/Shaders/Texture/Texture_PS.hlsl | 7 ++ Engine/res/Shaders/Texture/Texture_VS.glsl | 17 +++++ Engine/res/Shaders/Texture/Texture_VS.hlsl | 19 +++++ Engine/res/Shaders/TextureShader.h | 76 ------------------- Engine/src/Engine/Core/Application.cpp | 11 --- .../RendererPrograms/QuadRendererProgram.cpp | 5 +- .../TextureRendererProgram.cpp | 4 +- Engine/src/Engine/Utility/ResourceManager.cpp | 25 ++---- 15 files changed, 116 insertions(+), 179 deletions(-) create mode 100644 Engine/res/Shaders/Quad/Quad_PS.glsl create mode 100644 Engine/res/Shaders/Quad/Quad_PS.hlsl create mode 100644 Engine/res/Shaders/Quad/Quad_VS.glsl create mode 100644 Engine/res/Shaders/Quad/Quad_VS.hlsl delete mode 100644 Engine/res/Shaders/QuadShader.h create mode 100644 Engine/res/Shaders/Texture/Texture_PS.glsl create mode 100644 Engine/res/Shaders/Texture/Texture_PS.hlsl create mode 100644 Engine/res/Shaders/Texture/Texture_VS.glsl create mode 100644 Engine/res/Shaders/Texture/Texture_VS.hlsl delete mode 100644 Engine/res/Shaders/TextureShader.h diff --git a/Engine/premake5.lua b/Engine/premake5.lua index 4bf5ffc..57ca657 100644 --- a/Engine/premake5.lua +++ b/Engine/premake5.lua @@ -37,8 +37,9 @@ project "Engine" includedirs { -- engine - "%{prj.location}/src" , - "%{prj.location}/src/Engine" , + "%{prj.location}" , + "%{prj.location}/src" , + "%{prj.location}/src/Engine" , "%{prj.location}/src/Platform/GraphicsAPI" , "%{prj.location}/src/Platform/OS" , @@ -108,4 +109,8 @@ project "Engine" -- distribution filter "configurations:Distribution" defines "LIGHT_DIST" - optimize "on" \ No newline at end of file + optimize "on" + + filter { "files:**.hlsl" } + flags "ExcludeFromBuild" + shadermodel "4.0" \ No newline at end of file diff --git a/Engine/res/Shaders/Quad/Quad_PS.glsl b/Engine/res/Shaders/Quad/Quad_PS.glsl new file mode 100644 index 0000000..4e278f2 --- /dev/null +++ b/Engine/res/Shaders/Quad/Quad_PS.glsl @@ -0,0 +1,10 @@ +#version 440 core + +in vec4 vso_FragmentColor; + +out vec4 fso_FragmentColor; + +void main() +{ + fso_FragmentColor = vso_FragmentColor; +} \ No newline at end of file diff --git a/Engine/res/Shaders/Quad/Quad_PS.hlsl b/Engine/res/Shaders/Quad/Quad_PS.hlsl new file mode 100644 index 0000000..5ffca78 --- /dev/null +++ b/Engine/res/Shaders/Quad/Quad_PS.hlsl @@ -0,0 +1,4 @@ +float4 main(float4 Color : COLOR) : SV_Target +{ + return Color; +} \ No newline at end of file diff --git a/Engine/res/Shaders/Quad/Quad_VS.glsl b/Engine/res/Shaders/Quad/Quad_VS.glsl new file mode 100644 index 0000000..86915bd --- /dev/null +++ b/Engine/res/Shaders/Quad/Quad_VS.glsl @@ -0,0 +1,17 @@ +#version 440 core + +layout(location = 0) in vec3 a_Position; +layout(location = 1) in vec4 a_Color; + +layout(std140, binding = 0) uniform ub_ViewProjection +{ + mat4 viewProjection; +}; + +out vec4 vso_FragmentColor; + +void main() +{ + gl_Position = viewProjection * vec4(a_Position, 1.0); + vso_FragmentColor = a_Color; +} \ No newline at end of file diff --git a/Engine/res/Shaders/Quad/Quad_VS.hlsl b/Engine/res/Shaders/Quad/Quad_VS.hlsl new file mode 100644 index 0000000..e84e339 --- /dev/null +++ b/Engine/res/Shaders/Quad/Quad_VS.hlsl @@ -0,0 +1,14 @@ +struct VertexOut +{ + float4 Color : COLOR; + float4 Position : SV_Position; +}; + +VertexOut main(float3 InPosition : POSITION, float4 InColor : COLOR) +{ + VertexOut vso; + vso.Position = float4(InPosition.x, InPosition.y, InPosition.z, 1.0); + vso.Color = InColor; + + return vso; +} \ No newline at end of file diff --git a/Engine/res/Shaders/QuadShader.h b/Engine/res/Shaders/QuadShader.h deleted file mode 100644 index 7e60105..0000000 --- a/Engine/res/Shaders/QuadShader.h +++ /dev/null @@ -1,63 +0,0 @@ -#define LT_ENGINE_RESOURCES_QUAD_SHADER_VS \ -R"( -+GLSL -#version 440 core - -layout(location = 0) in vec3 a_Position; -layout(location = 1) in vec4 a_Color; - -layout(std140, binding = 0) uniform ub_ViewProjection -{ - mat4 viewProjection; -}; - -out vec4 vso_FragmentColor; - -void main() -{ - gl_Position = viewProjection * vec4(a_Position, 1.0); - vso_FragmentColor = a_Color; -} --GLSL - - -+HLSL -struct VertexOut -{ - float4 Color : COLOR; - float4 Position : SV_Position; -}; - -VertexOut main(float3 InPosition : POSITION, float4 InColor : COLOR) -{ - VertexOut vso; - vso.Position = float4(InPosition.x, InPosition.y, InPosition.z, 1.0); - vso.Color = InColor; - - return vso; -} --HLSL)" - -#define LT_ENGINE_RESOURCES_QUAD_SHADER_PS \ -R"( -+GLSL -#version 440 core - -in vec4 vso_FragmentColor; - -out vec4 fso_FragmentColor; - -void main() -{ - fso_FragmentColor = vso_FragmentColor; -} --GLSL - - -+HLSL -float4 main(float4 Color : COLOR) : SV_Target -{ - return Color; -} --HLSL -)" \ No newline at end of file diff --git a/Engine/res/Shaders/Texture/Texture_PS.glsl b/Engine/res/Shaders/Texture/Texture_PS.glsl new file mode 100644 index 0000000..19ba23e --- /dev/null +++ b/Engine/res/Shaders/Texture/Texture_PS.glsl @@ -0,0 +1,12 @@ +#version 440 core + +in vec2 vso_TexCoord; + +uniform sampler2D u_Texture; + +out vec4 fso_FragmentColor; + +void main() +{ + fso_FragmentColor = texture(u_Texture, vso_TexCoord); +} \ No newline at end of file diff --git a/Engine/res/Shaders/Texture/Texture_PS.hlsl b/Engine/res/Shaders/Texture/Texture_PS.hlsl new file mode 100644 index 0000000..af0f401 --- /dev/null +++ b/Engine/res/Shaders/Texture/Texture_PS.hlsl @@ -0,0 +1,7 @@ +sampler samplerState : register(s0); +Texture2D myTexture : register(t0); + +float4 main(float2 InTexChoord : TEXCOORD) : SV_Target +{ + return myTexture.Sample(samplerState, InTexChoord); +} \ No newline at end of file diff --git a/Engine/res/Shaders/Texture/Texture_VS.glsl b/Engine/res/Shaders/Texture/Texture_VS.glsl new file mode 100644 index 0000000..b6b0800 --- /dev/null +++ b/Engine/res/Shaders/Texture/Texture_VS.glsl @@ -0,0 +1,17 @@ +#version 440 core + +layout(location = 0) in vec3 a_Position; +layout(location = 1) in vec2 a_TexCoord; + +layout(std140, binding = 0) uniform ub_ViewProjection +{ + mat4 u_ViewProjection; +}; + +out vec2 vso_TexCoord; + +void main() +{ + gl_Position = u_ViewProjection * vec4(a_Position, 1.0); + vso_TexCoord = a_TexCoord; +} \ No newline at end of file diff --git a/Engine/res/Shaders/Texture/Texture_VS.hlsl b/Engine/res/Shaders/Texture/Texture_VS.hlsl new file mode 100644 index 0000000..b572bfc --- /dev/null +++ b/Engine/res/Shaders/Texture/Texture_VS.hlsl @@ -0,0 +1,19 @@ +struct VertexOut +{ + float2 TexChoord : TEXCOORD; + float4 Position : SV_Position; +}; + +cbuffer cb_ViewProjection : register(b0) +{ + row_major matrix viewProjection; +} + +VertexOut main(float3 InPosition : POSITION, float2 InTexChoord : TEXCOORD) +{ + VertexOut vso; + vso.Position = mul(float4(InPosition, 1.0), viewProjection); + vso.TexChoord = InTexChoord; + + return vso; +} \ No newline at end of file diff --git a/Engine/res/Shaders/TextureShader.h b/Engine/res/Shaders/TextureShader.h deleted file mode 100644 index 2745343..0000000 --- a/Engine/res/Shaders/TextureShader.h +++ /dev/null @@ -1,76 +0,0 @@ -#define LT_ENGINE_RESOURCES_TEXTURE_SHADER_VS \ -R"( -+GLSL -#version 440 core - -layout(location = 0) in vec3 a_Position; -layout(location = 1) in vec2 a_TexCoord; - -layout(std140, binding = 0) uniform ub_ViewProjection -{ - mat4 u_ViewProjection; -}; - -out vec2 vso_TexCoord; - -void main() -{ - gl_Position = u_ViewProjection * vec4(a_Position, 1.0); - vso_TexCoord = a_TexCoord; -} --GLSL - - -+HLSL -struct VertexOut -{ - float2 TexChoord : TEXCHOORD; - float4 Position : SV_Position; -}; - -cbuffer cb_ViewProjection : register(b0) -{ - row_major matrix viewProjection; -} - - -VertexOut main(float3 InPosition : POSITION, float2 InTexChoord : TEXCOORD) -{ - VertexOut vso; - vso.Position = mul(float4(InPosition, 1.0), viewProjection); - vso.TexChoord = InTexChoord; - - return vso; -} --HLSL -)" - -#define LT_ENGINE_RESOURCES_TEXTURE_SHADER_PS \ -R"( -+GLSL -#version 440 core - -in vec2 vso_TexCoord; - -uniform sampler2D u_Texture; - -out vec4 fso_FragmentColor; - -void main() -{ - fso_FragmentColor = texture(u_Texture, vso_TexCoord); -} --GLSL - - -+HLSL -sampler samplerState : register(s0); -Texture2D myTexture : register(t0); - -float4 main(float2 InTexChoord : TEXCHOORD) : SV_Target -{ - return myTexture.Sample(samplerState, InTexChoord); -} - --HLSL -)" \ No newline at end of file diff --git a/Engine/src/Engine/Core/Application.cpp b/Engine/src/Engine/Core/Application.cpp index 5546dbc..b063dfe 100644 --- a/Engine/src/Engine/Core/Application.cpp +++ b/Engine/src/Engine/Core/Application.cpp @@ -43,8 +43,6 @@ namespace Light { m_Window->SetVisibility(true); DeltaTimer deltaTimer; - Timer timer; - int frames = 0; //** GAMELOOP **// while (!m_Window->IsClosed()) @@ -52,15 +50,6 @@ namespace Light { // update layers m_LayerStack.OnUpdate(deltaTimer.GetDeltaTime()); - frames++; - if (timer.GetElapsedTime() > 1.0f) - { - LT_ENGINE_INFO(frames); - - frames = 0; - timer.Reset(); - } - // render layers m_Window->GetGfxContext()->GetRenderer()->BeginFrame(); m_LayerStack.OnRender(); diff --git a/Engine/src/Engine/Graphics/RendererPrograms/QuadRendererProgram.cpp b/Engine/src/Engine/Graphics/RendererPrograms/QuadRendererProgram.cpp index ff45447..2b58a28 100644 --- a/Engine/src/Engine/Graphics/RendererPrograms/QuadRendererProgram.cpp +++ b/Engine/src/Engine/Graphics/RendererPrograms/QuadRendererProgram.cpp @@ -7,8 +7,6 @@ #include "Graphics/Buffers.h" #include "Graphics/VertexLayout.h" -#include "../res/Shaders/QuadShader.h" - #include "Utility/ResourceManager.h" namespace Light { @@ -16,7 +14,7 @@ namespace Light { QuadRendererProgram::QuadRendererProgram(unsigned int maxVertices, std::shared_ptr sharedContext) : m_MaxVertices(maxVertices) { - ResourceManager::CreateShader("LT_ENGINE_RESOURCES_QUAD_SHADER", LT_ENGINE_RESOURCES_QUAD_SHADER_VS, LT_ENGINE_RESOURCES_QUAD_SHADER_PS); + ResourceManager::LoadShader("LT_ENGINE_RESOURCES_QUAD_SHADER", "../Engine/res/Shaders/Quad/Quad_VS", "../Engine//res/Shaders/Quad/Quad_PS"); m_Shader = ResourceManager::GetShader("LT_ENGINE_RESOURCES_QUAD_SHADER"); m_VertexBuffer = std::shared_ptr(VertexBuffer::Create(nullptr, sizeof(QuadVertexData), maxVertices, sharedContext)); @@ -63,4 +61,5 @@ namespace Light { m_VertexBuffer->Bind(); m_IndexBuffer->Bind(); } + } \ No newline at end of file diff --git a/Engine/src/Engine/Graphics/RendererPrograms/TextureRendererProgram.cpp b/Engine/src/Engine/Graphics/RendererPrograms/TextureRendererProgram.cpp index 3abd820..46f06e9 100644 --- a/Engine/src/Engine/Graphics/RendererPrograms/TextureRendererProgram.cpp +++ b/Engine/src/Engine/Graphics/RendererPrograms/TextureRendererProgram.cpp @@ -7,8 +7,6 @@ #include "Graphics/Buffers.h" #include "Graphics/VertexLayout.h" -#include "../res/Shaders/TextureShader.h" - #include "Utility/ResourceManager.h" namespace Light { @@ -16,7 +14,7 @@ namespace Light { TextureRendererProgram::TextureRendererProgram(unsigned int maxVertices, std::shared_ptr sharedContext) : m_MaxVertices(maxVertices) { - ResourceManager::CreateShader("LT_ENGINE_RESOURCES_TEXTURE_SHADER", LT_ENGINE_RESOURCES_TEXTURE_SHADER_VS, LT_ENGINE_RESOURCES_TEXTURE_SHADER_PS); + ResourceManager::LoadShader("LT_ENGINE_RESOURCES_TEXTURE_SHADER", "../Engine/res/Shaders/Texture/Texture_VS", "../Engine/res/Shaders/Texture/Texture_PS"); m_Shader = ResourceManager::GetShader("LT_ENGINE_RESOURCES_TEXTURE_SHADER"); m_VertexBuffer = std::shared_ptr(VertexBuffer::Create(nullptr, sizeof(TextureVertexData), maxVertices, sharedContext)); diff --git a/Engine/src/Engine/Utility/ResourceManager.cpp b/Engine/src/Engine/Utility/ResourceManager.cpp index a8da573..d5ac346 100644 --- a/Engine/src/Engine/Utility/ResourceManager.cpp +++ b/Engine/src/Engine/Utility/ResourceManager.cpp @@ -55,21 +55,14 @@ namespace Light { LT_ENGINE_ASSERT(!vertexPath.empty(), "ResourceManager::LoadShader: 'vertexPath' is empty"); LT_ENGINE_ASSERT(!pixelPath.empty(), "ResourceManager::LoadShader: 'pixelPath' is empty"); + std::string vPath = vertexPath + (GraphicsContext::GetGraphicsAPI() == GraphicsAPI::OpenGL ? ".glsl" : ".hlsl"); + std::string pPath = pixelPath + (GraphicsContext::GetGraphicsAPI() == GraphicsAPI::OpenGL ? ".glsl" : ".hlsl"); + // initialize - std::ifstream vsStream(vertexPath), psStream(pixelPath); + std::ifstream vsStream(vPath), psStream(pPath); std::stringstream vsSS, psSS; // pss pss pss pss :D - std::string vertexSource, pixelSource; std::string line; - // delim - std::string delim = GraphicsContext::GetGraphicsAPI() == GraphicsAPI::OpenGL ? "GLSL" : - GraphicsContext::GetGraphicsAPI() == GraphicsAPI::DirectX ? "HLSL" : NULL; - - // check - LT_ENGINE_ASSERT(!delim.empty(), "ResourceManager::LoadShader: invalid/unsupported 'GraphicsAPI': {}", GraphicsContext::GetGraphicsAPI()); - LT_ENGINE_ASSERT(vsStream.is_open(), "ResourceManager::LoadShader: invalid 'vertexPath': {}", vertexPath); - LT_ENGINE_ASSERT(psStream.is_open(), "ResourceManager::LoadShader: invalid 'pixelPath': {}", pixelPath); - // read while (std::getline(vsStream, line)) vsSS << line << '\n'; @@ -77,16 +70,8 @@ namespace Light { while (std::getline(psStream, line)) psSS << line << '\n'; - // save to string - vertexSource = vsSS.str(); - pixelSource = psSS.str(); - - // extract source - ResourceManager::ExtractShaderSource(vertexSource, delim); - ResourceManager::ExtractShaderSource(pixelSource, delim); - // create shader - m_Shaders[name] = std::shared_ptr(Shader::Create(vertexSource, pixelSource, m_SharedContext)); + m_Shaders[name] = std::shared_ptr(Shader::Create(vsSS.str(), psSS.str(), m_SharedContext)); } void ResourceManager::LoadTextureImpl(const std::string& name, const std::string& path, unsigned int desiredComponents /* = 4u */)