diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/RenderBackend/OpenGL/PixelShader.hpp | 74 | ||||
-rw-r--r-- | include/RenderBackend/OpenGL/Shader.hpp | 79 | ||||
-rw-r--r-- | include/RenderBackend/OpenGL/ShaderVec.hpp | 25 | ||||
-rw-r--r-- | include/RenderBackend/OpenGL/VertexShader.hpp | 71 |
4 files changed, 102 insertions, 147 deletions
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 <string> -#include <unordered_map> -#include <vector> -#include <stdexcept> -#include <functional> +#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<void()>; - - /* - * 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<void()>; - 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<CompiledPixelShader*> 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<std::string, i32> pixelAttributes; - std::vector<ShaderAttribute> pixelAttributeNames; - std::unordered_map<std::string, ShaderGlobalVec> 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 <string> +#include <unordered_map> +#include <vector> +#include <stdexcept> +#include <functional> + +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<std::string, i32> inputAttributes; + std::vector<ShaderAttribute> inputAttributeNames; + std::unordered_map<std::string, i32> outputAttributes; + std::vector<ShaderAttribute> outputAttributeNames; + std::unordered_map<std::string, ShaderGlobalVec> 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 <string> -#include <unordered_map> -#include <vector> -#include <stdexcept> -#include <functional> +#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<ShaderVec4()>; - - /* - * 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<ShaderVec4()>; - 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<CompiledVertexShader*> 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<std::string, i32> vertexAttributes; - std::vector<ShaderAttribute> vertexAttributeNames; - bool mainFuncDefined; }; } |