diff options
Diffstat (limited to 'src/RenderBackend/OpenGL/PixelShader.cpp')
-rw-r--r-- | src/RenderBackend/OpenGL/PixelShader.cpp | 67 |
1 files changed, 66 insertions, 1 deletions
diff --git a/src/RenderBackend/OpenGL/PixelShader.cpp b/src/RenderBackend/OpenGL/PixelShader.cpp index 2a7c913..2af68e5 100644 --- a/src/RenderBackend/OpenGL/PixelShader.cpp +++ b/src/RenderBackend/OpenGL/PixelShader.cpp @@ -1,4 +1,7 @@ #include "../../../include/RenderBackend/OpenGL/PixelShader.hpp" +#include "../../../include/RenderBackend/OpenGL/opengl.hpp" +#include "../../../include/RenderBackend/OpenGL/CompiledShader.hpp" +#include <cassert> using namespace std; @@ -28,11 +31,51 @@ namespace amalgine } - PixelShader::PixelShader() : mainFuncDefined(false) + PixelShader::PixelShader() : locationCounter(0), mainFuncDefined(false) { + glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxPixelAttribs); writeHeader("#version 330 core\n\n"); } + const string& PixelShader::getOutputAttributeName(i32 attributeIndex) + { + assert(attributeIndex < pixelAttributeNames.size()); + return pixelAttributeNames[attributeIndex].name; + } + + ShaderOutputVec4 PixelShader::defineOutputVec4(const std::string &name) + { + i32 attributeIndex = defineOutputVariable(name, "vec4"); + return ShaderOutputVec4(this, attributeIndex); + } + + i32 PixelShader::defineOutputVariable(const string &variableName, const char *typeName) + { + if(!isShaderVariableNameValid(variableName.c_str())) + throw PixelShaderInvalidAttributeName(variableName); + + if(locationCounter + 1 > maxPixelAttribs) + throw PixelShaderTooManyAttributes(maxPixelAttribs); + + if(pixelAttributes.find(variableName) != pixelAttributes.end()) + throw PixelShaderAttributeAlreadyDefined(variableName); + + i32 attributeIndex = locationCounter; + pixelAttributes[variableName] = locationCounter; + writeHeader("out "); + writeHeader(typeName); + writeHeader(" "); + writeHeader(variableName); + writeHeader(";\n"); + + ShaderAttribute shaderAttribute; + shaderAttribute.name = variableName; + shaderAttribute.typeName = typeName; + pixelAttributeNames.push_back(shaderAttribute); + ++locationCounter; + return attributeIndex; + } + void PixelShader::defineMain(PixelShaderMainFunc mainFunc) { if(mainFuncDefined) @@ -71,4 +114,26 @@ namespace amalgine { body += code; } + + Result<CompiledPixelShader*> PixelShader::compile() + { + GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER); + string verterShaderSource = build(); + const char *verterShaderSourcePtr = verterShaderSource.c_str(); + printf("Vertex shader:\n%s\n", verterShaderSourcePtr); + glShaderSource(fragmentShaderId, 1, &verterShaderSourcePtr, NULL); + glCompileShader(fragmentShaderId); + + GLint status; + glGetShaderiv(fragmentShaderId, GL_COMPILE_STATUS, &status); + string compileLog = getShaderCompileLog((u32)fragmentShaderId); + if(status == GL_TRUE) + { + if(!compileLog.empty()) + printf("Pixel shader compile log:\n%s", compileLog.c_str()); + return Result<CompiledPixelShader*>::Ok(new CompiledPixelShader(fragmentShaderId)); + } + else + return Result<CompiledPixelShader*>::Err(compileLog); + } } |