From b3038ef713920a1b5840aa970ffe82a35284fab8 Mon Sep 17 00:00:00 2001 From: Tleety Date: Fri, 26 Feb 2016 16:33:55 +0100 Subject: [PATCH 1/2] Includes are now supported in shader with the key #GLSL --- include/Engine/Rendering/ShaderProgram.h | 2 + resources/Shaders/ForwardPlus.frag.glsl | 3 ++ resources/Shaders/Util/CommonUniforms.glsl | 1 + src/Engine/Rendering/ShaderProgram.cpp | 47 +++++++++++++++++----- 4 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 resources/Shaders/Util/CommonUniforms.glsl diff --git a/include/Engine/Rendering/ShaderProgram.h b/include/Engine/Rendering/ShaderProgram.h index ad01c029..af90ff78 100644 --- a/include/Engine/Rendering/ShaderProgram.h +++ b/include/Engine/Rendering/ShaderProgram.h @@ -21,6 +21,8 @@ public: std::string GetFileName() const; GLuint GetHandle() const; bool IsCompiled() const; + static std::string ReadFile(std::string fileName); +private: protected: GLenum m_ShaderType; std::string m_FileName; diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 6fbc9c27..fc5e7009 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -2,6 +2,8 @@ #define MIN_AMBIENT_LIGHT 0.3 +#GLSL "Shaders/Util/CommonUniforms.glsl" + uniform mat4 M; uniform mat4 V; uniform mat4 P; @@ -180,6 +182,7 @@ void main() color_result += FillColor; } sceneColor = vec4(color_result.xyz, clamp(color_result.a, 0, 1)); + //sceneColor = CommonUniforms.testColour; //sceneColor = vec4(reflectionColor.xyz, 1); color_result += glowTexel*GlowIntensity; diff --git a/resources/Shaders/Util/CommonUniforms.glsl b/resources/Shaders/Util/CommonUniforms.glsl new file mode 100644 index 00000000..046693f2 --- /dev/null +++ b/resources/Shaders/Util/CommonUniforms.glsl @@ -0,0 +1 @@ +#define testtest1234; \ No newline at end of file diff --git a/src/Engine/Rendering/ShaderProgram.cpp b/src/Engine/Rendering/ShaderProgram.cpp index ae536bc0..9d441f13 100644 --- a/src/Engine/Rendering/ShaderProgram.cpp +++ b/src/Engine/Rendering/ShaderProgram.cpp @@ -4,22 +4,29 @@ GLuint Shader::CompileShader(GLenum shaderType, std::string fileName) { LOG_INFO("Compiling shader \"%s\"", fileName.c_str()); - std::string shaderFile; - std::ifstream in(fileName, std::ios::in); - if (!in) { - LOG_ERROR("Error: Failed to open shader file \"%s\"", fileName.c_str()); - return 0; - } - in.seekg(0, std::ios::end); - shaderFile.resize((int)in.tellg()); - in.seekg(0, std::ios::beg); - in.read(&shaderFile[0], shaderFile.size()); - in.close(); + std::string shaderFile = ReadFile(fileName); GLuint shader = glCreateShader(shaderType); if (GLERROR("glCreateShader")) return 0; + std::size_t startPos = 0; + std::size_t SEofNewFile[2]; + while((startPos = shaderFile.find("#GLSL", startPos)) != std::string::npos) + { + SEofNewFile[0] = shaderFile.find('"', startPos+5)+1; + SEofNewFile[1] = shaderFile.find('"', SEofNewFile[0]); + if (SEofNewFile[0] == std::string::npos || SEofNewFile[1] == std::string::npos) + { + return 0; + } + + std::string replacementFileName = shaderFile.substr(SEofNewFile[0], SEofNewFile[1] - SEofNewFile[0]); + std::string replacementString = ReadFile(replacementFileName); + shaderFile.replace(startPos, SEofNewFile[1]+2 - startPos, replacementString + "\n"); + startPos += replacementString.length(); //This might not be wanted. + } + const GLchar* shaderFiles = shaderFile.c_str(); const GLint length = static_cast(shaderFile.length()); glShaderSource(shader, 1, &shaderFiles, &length); @@ -46,6 +53,24 @@ GLuint Shader::CompileShader(GLenum shaderType, std::string fileName) return shader; } +std::string Shader::ReadFile(std::string fileName) +{ + std::string shaderFile; + std::ifstream in(fileName, std::ios::in); + if (!in) { + LOG_ERROR("Error: Failed to open shader file \"%s\"", fileName.c_str()); + return 0; + } + + in.seekg(0, std::ios::end); + shaderFile.resize((int)in.tellg()); + in.seekg(0, std::ios::beg); + in.read(&shaderFile[0], shaderFile.size()); + in.close(); + return shaderFile; +} + + Shader::Shader(GLenum shaderType, std::string fileName) : m_ShaderType(shaderType), m_FileName(fileName) { m_ShaderHandle = 0; From b1a59e0ed2f9f9393c37891ba96a4c8586eec39c Mon Sep 17 00:00:00 2001 From: Tleety Date: Fri, 26 Feb 2016 17:46:18 +0100 Subject: [PATCH 2/2] Includes now use the key #include. Fixed a bug where it would crash if the file is not found. Fixed a bug where null terminators where included from the included file, causing it to stop reading after the first included file. --- resources/Shaders/ForwardPlus.frag.glsl | 9 +-------- resources/Shaders/Util/CommonUniforms.glsl | 7 ++++++- src/Engine/Rendering/ShaderProgram.cpp | 13 ++++++++----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index fc5e7009..1516e2a4 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -2,8 +2,6 @@ #define MIN_AMBIENT_LIGHT 0.3 -#GLSL "Shaders/Util/CommonUniforms.glsl" - uniform mat4 M; uniform mat4 V; uniform mat4 P; @@ -118,12 +116,7 @@ LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensi return result; } -vec4 CalcNormalMappedValue(vec3 normal, vec3 tangent, vec3 bitangent, vec2 textureCoordinate, sampler2D normalMap) -{ - mat3 TBN = mat3(tangent, bitangent, normal); - vec3 NormalMap = texture(normalMap, textureCoordinate).xyz * 2.0 - vec3(1.0); - return vec4(TBN * normalize(NormalMap), 0.0); -} +#include "Shaders/Util/CommonUniforms.glsl" void main() { diff --git a/resources/Shaders/Util/CommonUniforms.glsl b/resources/Shaders/Util/CommonUniforms.glsl index 046693f2..de6700ae 100644 --- a/resources/Shaders/Util/CommonUniforms.glsl +++ b/resources/Shaders/Util/CommonUniforms.glsl @@ -1 +1,6 @@ -#define testtest1234; \ No newline at end of file +vec4 CalcNormalMappedValue(vec3 normal, vec3 tangent, vec3 bitangent, vec2 textureCoordinate, sampler2D normalMap) +{ + mat3 TBN = mat3(tangent, bitangent, normal); + vec3 NormalMap = texture(normalMap, textureCoordinate).xyz * 2.0 - vec3(1.0); + return vec4(TBN * normalize(NormalMap), 0.0); +} \ No newline at end of file diff --git a/src/Engine/Rendering/ShaderProgram.cpp b/src/Engine/Rendering/ShaderProgram.cpp index 9d441f13..d2a45888 100644 --- a/src/Engine/Rendering/ShaderProgram.cpp +++ b/src/Engine/Rendering/ShaderProgram.cpp @@ -12,17 +12,20 @@ GLuint Shader::CompileShader(GLenum shaderType, std::string fileName) std::size_t startPos = 0; std::size_t SEofNewFile[2]; - while((startPos = shaderFile.find("#GLSL", startPos)) != std::string::npos) + std::string key = "#include"; + while((startPos = shaderFile.find(key, startPos)) != std::string::npos) { - SEofNewFile[0] = shaderFile.find('"', startPos+5)+1; + SEofNewFile[0] = shaderFile.find('"', startPos+key.length())+1; SEofNewFile[1] = shaderFile.find('"', SEofNewFile[0]); if (SEofNewFile[0] == std::string::npos || SEofNewFile[1] == std::string::npos) - { return 0; - } std::string replacementFileName = shaderFile.substr(SEofNewFile[0], SEofNewFile[1] - SEofNewFile[0]); std::string replacementString = ReadFile(replacementFileName); + size_t firstof = replacementString.find_first_of((char)0); + replacementString.erase(firstof, replacementString.size() - firstof); + if (replacementString.length() <= 0) + return 0; shaderFile.replace(startPos, SEofNewFile[1]+2 - startPos, replacementString + "\n"); startPos += replacementString.length(); //This might not be wanted. } @@ -59,7 +62,7 @@ std::string Shader::ReadFile(std::string fileName) std::ifstream in(fileName, std::ios::in); if (!in) { LOG_ERROR("Error: Failed to open shader file \"%s\"", fileName.c_str()); - return 0; + return ""; } in.seekg(0, std::ios::end);