diff options
author | dec05eba <dec05eba@protonmail.com> | 2017-12-21 22:52:12 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-18 15:21:48 +0100 |
commit | bfd21732b35a3856b1f72c826816c2857710fcb3 (patch) | |
tree | 4a3f19551c744db3e339a64ac1afa2ebe1ec015f /src | |
parent | ca92d8c90f7103db6d7cae4cef49b278d804b474 (diff) |
Added shader program
Diffstat (limited to 'src')
-rw-r--r-- | src/RenderBackend/OpenGL/PixelShader.cpp | 44 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/ShaderProgram.cpp | 103 | ||||
-rw-r--r-- | src/RenderBackend/OpenGL/VertexShader.cpp | 3 | ||||
-rw-r--r-- | src/main.cpp | 5 |
4 files changed, 110 insertions, 45 deletions
diff --git a/src/RenderBackend/OpenGL/PixelShader.cpp b/src/RenderBackend/OpenGL/PixelShader.cpp index cbdc842..2a7c913 100644 --- a/src/RenderBackend/OpenGL/PixelShader.cpp +++ b/src/RenderBackend/OpenGL/PixelShader.cpp @@ -1,7 +1,4 @@ #include "../../../include/RenderBackend/OpenGL/PixelShader.hpp" -#include "../../../include/RenderBackend/OpenGL/CommonShader.hpp" -#include "../../../include/RenderBackend/OpenGL/opengl.hpp" -#include <cassert> using namespace std; @@ -31,24 +28,11 @@ namespace amalgine } - PixelShader::PixelShader() : locationCounter(0), mainFuncDefined(false) + PixelShader::PixelShader() : 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]; - } - - ShaderOutputVec4 PixelShader::defineOutputVec4(const std::string &name) - { - i32 attributeIndex = defineOutputVariable(name, "vec4"); - return ShaderOutputVec4(this, attributeIndex); - } - void PixelShader::defineMain(PixelShaderMainFunc mainFunc) { if(mainFuncDefined) @@ -68,31 +52,7 @@ namespace amalgine writeBody(";\n"); } - 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; - pixelAttributeNames.push_back(variableName); - - ++locationCounter; - writeHeader("out "); - writeHeader(typeName); - writeHeader(" "); - writeHeader(variableName); - writeHeader(";\n"); - return attributeIndex; - } - - string PixelShader::build() + string PixelShader::build() const { std::string result; result.reserve(header.size() + 2 + body.size()); diff --git a/src/RenderBackend/OpenGL/ShaderProgram.cpp b/src/RenderBackend/OpenGL/ShaderProgram.cpp new file mode 100644 index 0000000..892f45e --- /dev/null +++ b/src/RenderBackend/OpenGL/ShaderProgram.cpp @@ -0,0 +1,103 @@ +#include "../../../include/RenderBackend/OpenGL/ShaderProgram.hpp" +#include "../../../include/RenderBackend/OpenGL/CommonShader.hpp" +#include "../../../include/RenderBackend/OpenGL/opengl.hpp" + +using namespace std; + +namespace amalgine +{ + string getShaderCompileLog(GLuint shaderId) + { + string result; + GLint shaderLogLength; + glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &shaderLogLength); + if(shaderLogLength > 0) + { + result.resize(shaderLogLength); + glGetShaderInfoLog(shaderId, shaderLogLength, NULL, &result[0]); + } + return result; + } + + ShaderProgram::ShaderProgram() : locationCounter(0) + { + glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxPixelAttribs); + } + + const string& ShaderProgram::getOutputAttributeName(i32 attributeIndex) + { + assert(attributeIndex < pixelAttributeNames.size()); + return pixelAttributeNames[attributeIndex]; + } + + ShaderOutputVec4 ShaderProgram::defineOutputVec4(const std::string &name) + { + i32 attributeIndex = defineOutputVariable(name, "vec4"); + return ShaderOutputVec4(this, attributeIndex); + } + + i32 ShaderProgram::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; + pixelAttributeNames.push_back(variableName); + ++locationCounter; + return attributeIndex; + } + + /* + Result<string> ShaderProgram::addVertexShader(const VertexShader &vertexShader) + { + GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER); + string verterShaderSource = vertexShader.build(); + const char *verterShaderSourcePtr = verterShaderSource.c_str(); + glShaderSource(vertexShaderId, 1, &verterShaderSourcePtr, NULL); + glCompileShader(vertexShaderId); + shaderIds.push_back(vertexShaderId); + + GLint status; + glGetShaderiv(vertexShaderId, GL_COMPILE_STATUS, &status); + string compileLog = getShaderCompileLog(vertexShaderId); + if(status == GL_TRUE) + return Result<string>::Ok(compileLog); + else + return Result<string>::Err(compileLog); + } + + Result<string> ShaderProgram::addPixelShader(const PixelShader &pixelShader) + { + GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER); + string fragmentShaderSource = pixelShader.build(); + const char *fragmentShaderSourcePtr = fragmentShaderSource.c_str(); + glShaderSource(fragmentShaderId, 1, &fragmentShaderSourcePtr, NULL); + glCompileShader(fragmentShaderId); + shaderIds.push_back(fragmentShaderId); + + GLint status; + glGetShaderiv(fragmentShaderId, GL_COMPILE_STATUS, &status); + string compileLog = getShaderCompileLog(fragmentShaderId); + if(status == GL_TRUE) + return Result<string>::Ok(compileLog); + else + return Result<string>::Err(compileLog); + } + + void ShaderProgram::build() + { + GLuint shaderProgramId = glCreateProgram(); + for(u32 shaderId : shaderIds) + { + glAttachShader(shaderProgramId, shaderId); + } + } + */ +} diff --git a/src/RenderBackend/OpenGL/VertexShader.cpp b/src/RenderBackend/OpenGL/VertexShader.cpp index b869624..04bf863 100644 --- a/src/RenderBackend/OpenGL/VertexShader.cpp +++ b/src/RenderBackend/OpenGL/VertexShader.cpp @@ -1,7 +1,6 @@ #include "../../../include/RenderBackend/OpenGL/VertexShader.hpp" #include "../../../include/RenderBackend/OpenGL/CommonShader.hpp" #include "../../../include/RenderBackend/OpenGL/opengl.hpp" -#include <cassert> using namespace std; @@ -88,7 +87,7 @@ namespace amalgine return attributeIndex; } - string VertexShader::build() + string VertexShader::build() const { std::string result; result.reserve(header.size() + 2 + body.size()); diff --git a/src/main.cpp b/src/main.cpp index fe48ddf..03222f1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include <glm/glm.hpp> #include "../include/RenderBackend/OpenGL/VertexShader.hpp" #include "../include/RenderBackend/OpenGL/PixelShader.hpp" +#include "../include/RenderBackend/OpenGL/ShaderProgram.hpp" #include "../include/RenderBackend/OpenGL/DeviceMemory.hpp" using namespace amalgine; @@ -54,6 +55,8 @@ int main() DeviceMemory triangle; triangle.copyStatic(vertices); + ShaderProgram shaderProgram; + VertexShader vertexShader; ShaderInputVec2 inputPosition = vertexShader.defineInputVec2("position"); @@ -63,7 +66,7 @@ int main() }); PixelShader pixelShader; - ShaderOutputVec4 outColor = pixelShader.defineOutputVec4("outColor"); + ShaderOutputVec4 outColor = shaderProgram.defineOutputVec4("outColor"); pixelShader.defineMain([&outColor]() { |