From ff4daae11db0ab811cac66e262d289a4107bba4a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 27 Dec 2017 23:48:41 +0100 Subject: Add uniform (shader global variable) --- src/RenderBackend/OpenGL/PixelShader.cpp | 18 ++++++++++++++++++ src/RenderBackend/OpenGL/ShaderProgram.cpp | 21 +++++++++++++++++++++ src/RenderBackend/OpenGL/ShaderVec.cpp | 6 ++++++ 3 files changed, 45 insertions(+) (limited to 'src/RenderBackend/OpenGL') diff --git a/src/RenderBackend/OpenGL/PixelShader.cpp b/src/RenderBackend/OpenGL/PixelShader.cpp index 8aa4d9a..f263513 100644 --- a/src/RenderBackend/OpenGL/PixelShader.cpp +++ b/src/RenderBackend/OpenGL/PixelShader.cpp @@ -49,6 +49,24 @@ namespace amalgine return ShaderOutputVec4(this, attributeIndex); } + // TODO: Generate warning if global variable is defined but not assigned to? + ShaderGlobalVec3 PixelShader::defineGlobalVec3(const std::string &name) + { + if(!isShaderVariableNameValid(name.c_str())) + throw PixelShaderInvalidAttributeName(name); + + if(globalAttributes.find(name) != globalAttributes.end()) + throw PixelShaderAttributeAlreadyDefined(name); + + writeHeader("uniform vec3 "); + writeHeader(name); + writeHeader(";\n"); + + ShaderGlobalVec3 globalVec(name); + globalAttributes[name] = globalVec.getVecObject(); + return ShaderGlobalVec3(globalVec); + } + i32 PixelShader::defineOutputVariable(const string &variableName, const char *typeName) { if(!isShaderVariableNameValid(variableName.c_str())) diff --git a/src/RenderBackend/OpenGL/ShaderProgram.cpp b/src/RenderBackend/OpenGL/ShaderProgram.cpp index 40f5b6c..aceda3b 100644 --- a/src/RenderBackend/OpenGL/ShaderProgram.cpp +++ b/src/RenderBackend/OpenGL/ShaderProgram.cpp @@ -12,6 +12,12 @@ namespace amalgine } + ShaderProgramNonExistingGlobalVariable::ShaderProgramNonExistingGlobalVariable(const char *variableName) : + runtime_error(string("There is no shader global variable with the name '") + variableName + "'") + { + + } + ShaderProgram::ShaderProgram() : built(false) { shaderProgramId = glCreateProgram(); @@ -84,4 +90,19 @@ namespace amalgine throw ShaderProgramUsedBeforeBuilt(); } } + + ShaderProgramGlobalVec3 ShaderProgram::getGlobalVec3(const char *name) + { + if(built) + { + GLint uniformId = glGetUniformLocation(shaderProgramId, name); + if(uniformId == -1) + throw ShaderProgramNonExistingGlobalVariable(name); + return ShaderProgramGlobalVec3(this, uniformId); + } + else + { + throw ShaderProgramUsedBeforeBuilt(); + } + } } diff --git a/src/RenderBackend/OpenGL/ShaderVec.cpp b/src/RenderBackend/OpenGL/ShaderVec.cpp index 3bbe0be..dd3bbe5 100644 --- a/src/RenderBackend/OpenGL/ShaderVec.cpp +++ b/src/RenderBackend/OpenGL/ShaderVec.cpp @@ -20,6 +20,12 @@ namespace amalgine return AttributeType::NONE; } + void ShaderProgramGlobalVec3::set(f32 x, f32 y, f32 z) + { + shaderProgram->use(); + glUniform3f(uniformId, x, y, z); + } + const string& ShaderInputVec2::getName() const { return vertexShader->getInputAttributeName(attributeIndex); -- cgit v1.2.3