From bfd21732b35a3856b1f72c826816c2857710fcb3 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 21 Dec 2017 22:52:12 +0100 Subject: Added shader program --- include/RenderBackend/OpenGL/PixelShader.hpp | 14 +------- include/RenderBackend/OpenGL/ShaderProgram.hpp | 34 ++++++++++++++++++ include/RenderBackend/OpenGL/ShaderVec.hpp | 8 ++--- include/RenderBackend/OpenGL/VertexShader.hpp | 2 +- include/Result.hpp | 48 ++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 include/RenderBackend/OpenGL/ShaderProgram.hpp create mode 100644 include/Result.hpp (limited to 'include') diff --git a/include/RenderBackend/OpenGL/PixelShader.hpp b/include/RenderBackend/OpenGL/PixelShader.hpp index 3d33155..b869a8f 100644 --- a/include/RenderBackend/OpenGL/PixelShader.hpp +++ b/include/RenderBackend/OpenGL/PixelShader.hpp @@ -45,27 +45,15 @@ namespace amalgine public: PixelShader(); - const std::string& getOutputAttributeName(i32 attributeIndex); - ShaderOutputVec4 defineOutputVec4(const std::string &name); void defineMain(PixelShaderMainFunc mainFunc); void assign(const ShaderOutputVec4 &lhsVariable, const ShaderVec4 &rhsVariable); - std::string build(); + std::string build() const; private: void writeHeader(const std::string &code); void writeBody(const std::string &code); - - /* - * Throws PixelShaderTooManyAttributes if too many pixel attributes are defined for the platform. - * Throws PixelShaderAttributeAlreadyDefined if a pixel attribute with the same name has already been defined. - */ - i32 defineOutputVariable(const std::string &variableName, const char *typeName); private: std::string header; std::string body; - int locationCounter; - i32 maxPixelAttribs; // Could make this static - std::unordered_map pixelAttributes; - std::vector pixelAttributeNames; bool mainFuncDefined; }; } diff --git a/include/RenderBackend/OpenGL/ShaderProgram.hpp b/include/RenderBackend/OpenGL/ShaderProgram.hpp new file mode 100644 index 0000000..efeae5b --- /dev/null +++ b/include/RenderBackend/OpenGL/ShaderProgram.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "VertexShader.hpp" +#include "PixelShader.hpp" +#include "../../Result.hpp" +#include "../../types.hpp" +#include + +namespace amalgine +{ + class ShaderProgram + { + public: + ShaderProgram(); + + const std::string& getOutputAttributeName(i32 attributeIndex); + ShaderOutputVec4 defineOutputVec4(const std::string &name); + //Result addVertexShader(const VertexShader &vertexShader); + //Result addPixelShader(const PixelShader &pixelShader); + //void build(); + private: + /* + * Throws PixelShaderTooManyAttributes if too many pixel attributes are defined for the platform. + * Throws PixelShaderAttributeAlreadyDefined if a pixel attribute with the same name has already been defined. + */ + i32 defineOutputVariable(const std::string &variableName, const char *typeName); + private: + int locationCounter; + i32 maxPixelAttribs; // Could make this static + std::unordered_map pixelAttributes; + std::vector pixelAttributeNames; + std::vector shaderIds; + }; +} diff --git a/include/RenderBackend/OpenGL/ShaderVec.hpp b/include/RenderBackend/OpenGL/ShaderVec.hpp index 10e628c..0c0a88a 100644 --- a/include/RenderBackend/OpenGL/ShaderVec.hpp +++ b/include/RenderBackend/OpenGL/ShaderVec.hpp @@ -6,7 +6,7 @@ namespace amalgine { class VertexShader; - class PixelShader; + class ShaderProgram; class ShaderInputVec2 { @@ -62,20 +62,20 @@ namespace amalgine class ShaderOutputVec4 { - friend class PixelShader; + friend class ShaderProgram; public: const std::string& getName() const; void operator=(const ShaderVec4 &shaderVec4); private: - ShaderOutputVec4(PixelShader *_pixelShader, i32 _attributeIndex) : + ShaderOutputVec4(ShaderProgram *_pixelShader, i32 _attributeIndex) : pixelShader(_pixelShader), attributeIndex(_attributeIndex) { } private: - PixelShader *pixelShader; + ShaderProgram *shaderProgram; i32 attributeIndex; }; } diff --git a/include/RenderBackend/OpenGL/VertexShader.hpp b/include/RenderBackend/OpenGL/VertexShader.hpp index ec8be22..3239be2 100644 --- a/include/RenderBackend/OpenGL/VertexShader.hpp +++ b/include/RenderBackend/OpenGL/VertexShader.hpp @@ -48,7 +48,7 @@ namespace amalgine const std::string& getInputAttributeName(i32 attributeIndex); ShaderInputVec2 defineInputVec2(const std::string &name); void defineMain(VertexShaderMainFunc mainFunc); - std::string build(); + std::string build() const; private: void writeHeader(const std::string &code); void writeBody(const std::string &code); diff --git a/include/Result.hpp b/include/Result.hpp new file mode 100644 index 0000000..316e45a --- /dev/null +++ b/include/Result.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include + +namespace amalgine +{ + template + class Result + { + public: + static Result Ok(const T &data) + { + Result result; + result.data = data; + result.errorCode = 0; + return result; + } + + static Result Err(const std::string &errorMsg, int errorCode = -1) + { + Result result; + result.errorMsg = errorMsg; + result.errorCode = errorCode; + return result; + } + + template + static Result Err(const Result &otherResult) + { + Result result; + result.errorMsg = otherResult.errorMsg; + result.errorCode = otherResult.errorCode; + return result; + } + + bool isOk() const { return errorCode == 0; } + bool isErr() const { return !isOk(); } + operator bool() const { return isOk(); } + + const std::string& getErrorMsg() const { return errorMsg; } + private: + Result(){} + private: + T data; + int errorCode; + std::string errorMsg; + }; +} -- cgit v1.2.3