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) --- include/RenderBackend/OpenGL/PixelShader.hpp | 2 + include/RenderBackend/OpenGL/ShaderProgram.hpp | 8 ++++ include/RenderBackend/OpenGL/ShaderVec.hpp | 56 +++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/RenderBackend/OpenGL/PixelShader.hpp b/include/RenderBackend/OpenGL/PixelShader.hpp index e025c6f..6794fb1 100644 --- a/include/RenderBackend/OpenGL/PixelShader.hpp +++ b/include/RenderBackend/OpenGL/PixelShader.hpp @@ -54,6 +54,7 @@ namespace amalgine const std::string& getOutputAttributeName(i32 attributeIndex); ShaderOutputVec4 defineOutputVec4(const std::string &name); + ShaderGlobalVec3 defineGlobalVec3(const std::string &name); void defineMain(PixelShaderMainFunc mainFunc); void assign(const ShaderOutputVec4 &lhsVariable, const ShaderVec4 &rhsVariable); @@ -75,6 +76,7 @@ namespace amalgine i32 maxPixelAttribs; // Could make this static std::unordered_map pixelAttributes; std::vector pixelAttributeNames; + std::unordered_map globalAttributes; bool mainFuncDefined; }; } diff --git a/include/RenderBackend/OpenGL/ShaderProgram.hpp b/include/RenderBackend/OpenGL/ShaderProgram.hpp index 2b1c5b4..c8740f8 100644 --- a/include/RenderBackend/OpenGL/ShaderProgram.hpp +++ b/include/RenderBackend/OpenGL/ShaderProgram.hpp @@ -3,6 +3,7 @@ #include "../../Result.hpp" #include "../../types.hpp" #include "../../utils.hpp" +#include "ShaderVec.hpp" #include #include @@ -19,6 +20,12 @@ namespace amalgine ShaderProgramUsedBeforeBuilt(); }; + class ShaderProgramNonExistingGlobalVariable : public std::runtime_error + { + public: + ShaderProgramNonExistingGlobalVariable(const char *variableName); + }; + class ShaderProgram { DISABLE_COPY(ShaderProgram) @@ -31,6 +38,7 @@ namespace amalgine Result build(); void use(); + ShaderProgramGlobalVec3 getGlobalVec3(const char *name); private: u32 shaderProgramId; bool built; diff --git a/include/RenderBackend/OpenGL/ShaderVec.hpp b/include/RenderBackend/OpenGL/ShaderVec.hpp index a5163ba..7c14af1 100644 --- a/include/RenderBackend/OpenGL/ShaderVec.hpp +++ b/include/RenderBackend/OpenGL/ShaderVec.hpp @@ -8,15 +8,60 @@ namespace amalgine { class VertexShader; class PixelShader; + class ShaderProgram; enum class AttributeType { NONE, - VEC2 + VEC2, + VEC3, + VEC4 }; AttributeType getAttributeTypeByName(const char *attributeName); + class ShaderGlobalVec + { + friend class VertexShader; + friend class PixelShader; + friend class ShaderGlobalVec3; + public: + ShaderGlobalVec() : attributeType(AttributeType::NONE) {} + const std::string& getName() const { return name; } + AttributeType getAttributeType() const { return attributeType; } + protected: + ShaderGlobalVec(const std::string &_name, AttributeType _attributeType) : name(_name), attributeType(_attributeType) {} + private: + std::string name; + AttributeType attributeType; + }; + + class ShaderGlobalVec3 + { + friend class VertexShader; + friend class PixelShader; + public: + const std::string& getName() const { return globalVec.getName(); } + AttributeType getAttributeType() const { return globalVec.getAttributeType(); } + ShaderGlobalVec getVecObject() const { return globalVec; } + private: + ShaderGlobalVec3(const std::string &_name) : globalVec(_name, AttributeType::VEC3) {} + private: + ShaderGlobalVec globalVec; + }; + + class ShaderProgramGlobalVec3 + { + friend class ShaderProgram; + public: + void set(f32 x = 0.0f, f32 y = 0.0f, f32 z = 0.0f); + private: + ShaderProgramGlobalVec3(ShaderProgram *_shaderProgram, i32 _uniformId) : shaderProgram(_shaderProgram), uniformId(_uniformId){} + private: + ShaderProgram *shaderProgram; + i32 uniformId; + }; + class ShaderInputVec2 { friend class VertexShader; @@ -62,6 +107,15 @@ namespace amalgine result += ")"; } + ShaderVec4(const ShaderGlobalVec3 &vec3, f32 w = 0.0f) + { + result = "vec4("; + result += vec3.getName(); + result += ", "; + result += std::to_string(w); + result += ")"; + } + const std::string& getOutput() const { return result; -- cgit v1.2.3