From fbd2e5d9a802db4fb5e056705ec599ac423e09be Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 27 Oct 2019 01:30:34 +0200 Subject: Create Shader class as a base for vertex and pixel shader --- include/RenderBackend/OpenGL/PixelShader.hpp | 74 ++----------------------- include/RenderBackend/OpenGL/Shader.hpp | 79 +++++++++++++++++++++++++++ include/RenderBackend/OpenGL/ShaderVec.hpp | 25 ++++----- include/RenderBackend/OpenGL/VertexShader.hpp | 71 ++---------------------- 4 files changed, 102 insertions(+), 147 deletions(-) create mode 100644 include/RenderBackend/OpenGL/Shader.hpp (limited to 'include') diff --git a/include/RenderBackend/OpenGL/PixelShader.hpp b/include/RenderBackend/OpenGL/PixelShader.hpp index 6794fb1..ebe5a12 100644 --- a/include/RenderBackend/OpenGL/PixelShader.hpp +++ b/include/RenderBackend/OpenGL/PixelShader.hpp @@ -1,82 +1,20 @@ #pragma once -#include "../../DataView.hpp" -#include "../../utils.hpp" -#include "../../Result.hpp" -#include "CommonShader.hpp" -#include "ShaderVec.hpp" -#include -#include -#include -#include -#include +#include "Shader.hpp" -namespace amalgine -{ - class PixelShaderTooManyAttributes : public std::runtime_error - { - public: - PixelShaderTooManyAttributes(i32 maxPixelAttributes); - }; - - class PixelShaderAttributeAlreadyDefined : public std::runtime_error - { - public: - PixelShaderAttributeAlreadyDefined(const std::string &attributeName); - }; - - class PixelShaderInvalidAttributeName : public std::runtime_error - { - public: - PixelShaderInvalidAttributeName(const std::string &attributeName); - }; - - class PixelShaderFunctionAlreadyDefined : public std::runtime_error - { - public: - PixelShaderFunctionAlreadyDefined(const std::string &funcName); - }; - - using PixelShaderMainFunc = std::function; - - /* - * Using the same PixelShader instance in multiple threads to define data is not thread safe. - * All get*** functions are thread safe. - */ +namespace amalgine { class CompiledPixelShader; + + using PixelShaderMainFunc = std::function; - class PixelShader + class PixelShader : public Shader { DISABLE_COPY(PixelShader) friend class ShaderProgram; public: PixelShader(); - - 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); Result compile(); - private: - void writeHeader(const std::string &code); - void writeBody(const std::string &code); - std::string build() const; - - /* - * 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; - std::unordered_map globalAttributes; - bool mainFuncDefined; }; } diff --git a/include/RenderBackend/OpenGL/Shader.hpp b/include/RenderBackend/OpenGL/Shader.hpp new file mode 100644 index 0000000..7aecc4e --- /dev/null +++ b/include/RenderBackend/OpenGL/Shader.hpp @@ -0,0 +1,79 @@ +#pragma once + +#include "../../DataView.hpp" +#include "../../utils.hpp" +#include "../../Result.hpp" +#include "CommonShader.hpp" +#include "ShaderVec.hpp" +#include +#include +#include +#include +#include + +namespace amalgine { + class ShaderTooManyAttributes : public std::runtime_error + { + public: + ShaderTooManyAttributes(i32 maxAttributes); + }; + + class ShaderAttributeAlreadyDefined : public std::runtime_error + { + public: + ShaderAttributeAlreadyDefined(const std::string &attributeName); + }; + + class ShaderInvalidAttributeName : public std::runtime_error + { + public: + ShaderInvalidAttributeName(const std::string &attributeName); + }; + + class ShaderFunctionAlreadyDefined : public std::runtime_error + { + public: + ShaderFunctionAlreadyDefined(const std::string &funcName); + }; + + class Shader + { + DISABLE_COPY(Shader) + friend class ShaderProgram; + public: + Shader(); + + const std::string& getOutputAttributeName(i32 attributeIndex); + const std::string& getInputAttributeName(i32 attributeIndex) const; + AttributeType getInputAttributeType(i32 attributeIndex) const; + ShaderOutputVec4 defineOutputVec4(const std::string &name); + ShaderGlobalVec3 defineGlobalVec3(const std::string &name); + ShaderInputVec2 defineInputVec2(const std::string &name); + i32 defineInputVariable(const std::string &variableName, const char *typeName); + + void assign(const ShaderOutputVec4 &lhsVariable, const ShaderVec4 &rhsVariable); + protected: + void writeHeader(const std::string &code); + void writeBody(const std::string &code); + std::string build() const; + + /* + * Throws ShaderTooManyAttributes if too many attributes are defined for the platform. + * Throws ShaderAttributeAlreadyDefined if a attribute with the same name has already been defined. + */ + i32 defineOutputVariable(const std::string &variableName, const char *typeName); + protected: + std::string header; + std::string body; + int inputLocationCounter; + int outputLocationCounter; + // TOOD: Verify if this is correct. This same variable is used for both output and input variables + i32 maxAttribs; // Could make this static + std::unordered_map inputAttributes; + std::vector inputAttributeNames; + std::unordered_map outputAttributes; + std::vector outputAttributeNames; + std::unordered_map uniforms; + bool mainFuncDefined; + }; +} diff --git a/include/RenderBackend/OpenGL/ShaderVec.hpp b/include/RenderBackend/OpenGL/ShaderVec.hpp index 7c14af1..4353dd7 100644 --- a/include/RenderBackend/OpenGL/ShaderVec.hpp +++ b/include/RenderBackend/OpenGL/ShaderVec.hpp @@ -6,8 +6,7 @@ namespace amalgine { - class VertexShader; - class PixelShader; + class Shader; class ShaderProgram; enum class AttributeType @@ -22,8 +21,7 @@ namespace amalgine class ShaderGlobalVec { - friend class VertexShader; - friend class PixelShader; + friend class Shader; friend class ShaderGlobalVec3; public: ShaderGlobalVec() : attributeType(AttributeType::NONE) {} @@ -38,8 +36,7 @@ namespace amalgine class ShaderGlobalVec3 { - friend class VertexShader; - friend class PixelShader; + friend class Shader; public: const std::string& getName() const { return globalVec.getName(); } AttributeType getAttributeType() const { return globalVec.getAttributeType(); } @@ -64,19 +61,19 @@ namespace amalgine class ShaderInputVec2 { - friend class VertexShader; + friend class Shader; public: const std::string& getName() const; void setData(const DeviceMemory &data); private: - ShaderInputVec2(VertexShader *_vertexShader, i32 _attributeIndex) : - vertexShader(_vertexShader), + ShaderInputVec2(Shader *_shader, i32 _attributeIndex) : + shader(_shader), attributeIndex(_attributeIndex) { } private: - VertexShader *vertexShader; + Shader *shader; i32 attributeIndex; }; @@ -126,20 +123,20 @@ namespace amalgine class ShaderOutputVec4 { - friend class PixelShader; + friend class Shader; public: const std::string& getName() const; void operator=(const ShaderVec4 &shaderVec4); private: - ShaderOutputVec4(PixelShader *_pixelShader, i32 _attributeIndex) : - pixelShader(_pixelShader), + ShaderOutputVec4(Shader *_shader, i32 _attributeIndex) : + shader(_shader), attributeIndex(_attributeIndex) { } private: - PixelShader *pixelShader; + Shader *shader; i32 attributeIndex; }; } diff --git a/include/RenderBackend/OpenGL/VertexShader.hpp b/include/RenderBackend/OpenGL/VertexShader.hpp index aaf8b24..6fdd6a4 100644 --- a/include/RenderBackend/OpenGL/VertexShader.hpp +++ b/include/RenderBackend/OpenGL/VertexShader.hpp @@ -1,79 +1,20 @@ #pragma once -#include "../../DataView.hpp" -#include "../../utils.hpp" -#include "../../Result.hpp" -#include "CommonShader.hpp" -#include "ShaderVec.hpp" -#include -#include -#include -#include -#include +#include "Shader.hpp" -namespace amalgine -{ - class VertexShaderTooManyAttributes : public std::runtime_error - { - public: - VertexShaderTooManyAttributes(i32 maxVertexAttributes); - }; - - class VertexShaderAttributeAlreadyDefined : public std::runtime_error - { - public: - VertexShaderAttributeAlreadyDefined(const std::string &attributeName); - }; - - class VertexShaderInvalidAttributeName : public std::runtime_error - { - public: - VertexShaderInvalidAttributeName(const std::string &attributeName); - }; - - class VertexShaderFunctionAlreadyDefined : public std::runtime_error - { - public: - VertexShaderFunctionAlreadyDefined(const std::string &funcName); - }; - - using VertexShaderMainFunc = std::function; - - /* - * Using the same VertexShader instance in multiple threads to define data is not thread safe. - * All get*** functions are thread safe. - */ +namespace amalgine { class CompiledVertexShader; + + using VertexShaderMainFunc = std::function; - class VertexShader + class VertexShader : public Shader { DISABLE_COPY(VertexShader) friend class ShaderProgram; public: VertexShader(); - - const std::string& getInputAttributeName(i32 attributeIndex) const; - AttributeType getInputAttributeType(i32 attributeIndex) const; - ShaderInputVec2 defineInputVec2(const std::string &name); + void defineMain(VertexShaderMainFunc mainFunc); Result compile(); - private: - void writeHeader(const std::string &code); - void writeBody(const std::string &code); - std::string build() const; - - /* - * Throws VertexShaderTooManyInputAttributes if too many vertex attributes are defined for the platform. - * Throws VertexShaderAttributeAlreadyDefined if a vertex attribute with the same name has already been defined. - */ - i32 defineInputVariable(const std::string &variableName, const char *typeName); - private: - std::string header; - std::string body; - int locationCounter; - i32 maxVertexAttribs; // Could make this static - std::unordered_map vertexAttributes; - std::vector vertexAttributeNames; - bool mainFuncDefined; }; } -- cgit v1.2.3