From 23a37b2cdd8ffde8bb85a4159888bf3a7ec35966 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 4 Nov 2019 00:50:45 +0100 Subject: Use external shaders instead of generating shader code from c++ code... --- include/RenderBackend/OpenGL/CommonShader.hpp | 44 -------- include/RenderBackend/OpenGL/CompiledShader.hpp | 37 ------ include/RenderBackend/OpenGL/DeviceFrame.hpp | 3 +- include/RenderBackend/OpenGL/DeviceMemory.hpp | 20 ++-- include/RenderBackend/OpenGL/PixelShader.hpp | 20 ---- include/RenderBackend/OpenGL/Shader.hpp | 81 +++----------- include/RenderBackend/OpenGL/ShaderProgram.hpp | 43 +++---- include/RenderBackend/OpenGL/ShaderVec.hpp | 142 ------------------------ include/RenderBackend/OpenGL/VertexShader.hpp | 20 ---- include/Result.hpp | 4 +- include/Vec.hpp | 14 +++ include/types.hpp | 5 +- 12 files changed, 57 insertions(+), 376 deletions(-) delete mode 100644 include/RenderBackend/OpenGL/CommonShader.hpp delete mode 100644 include/RenderBackend/OpenGL/CompiledShader.hpp delete mode 100644 include/RenderBackend/OpenGL/PixelShader.hpp delete mode 100644 include/RenderBackend/OpenGL/ShaderVec.hpp delete mode 100644 include/RenderBackend/OpenGL/VertexShader.hpp create mode 100644 include/Vec.hpp (limited to 'include') diff --git a/include/RenderBackend/OpenGL/CommonShader.hpp b/include/RenderBackend/OpenGL/CommonShader.hpp deleted file mode 100644 index 80e63a9..0000000 --- a/include/RenderBackend/OpenGL/CommonShader.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#pragma once - -#include "../../types.hpp" -#include - -namespace amalgine -{ - struct ShaderAttribute - { - std::string name; - const char *typeName; - }; - - static bool isAlpha(char c) - { - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); - } - - static bool isDigit(char c) - { - return c >= '0' && c <= '9'; - } - - static bool isShaderVariableNameValid(const char *variableName) - { - const char *p = &variableName[0]; - if(isAlpha(*p) || *p == '_') - { - ++p; - while(true) - { - char c = *p; - if(c == '\0') - return true; - else if(isAlpha(c) || isDigit(c) || c == '_') - ++p; - } - } - - return false; - } - - std::string getShaderCompileLog(u32 shaderId); -} diff --git a/include/RenderBackend/OpenGL/CompiledShader.hpp b/include/RenderBackend/OpenGL/CompiledShader.hpp deleted file mode 100644 index 8d2ee84..0000000 --- a/include/RenderBackend/OpenGL/CompiledShader.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include "../../types.hpp" -#include -#include - -namespace amalgine -{ - class CompiledVertexShader - { - friend class VertexShader; - public: - ~CompiledVertexShader(); - u32 getShaderId() const; - private: - CompiledVertexShader(u32 _shaderId); - private: - u32 shaderId; - }; - - class CompiledPixelShader - { - friend class PixelShader; - public: - ~CompiledPixelShader(); - u32 getShaderId() const; - const auto& getPixelAttributes() const - { - return pixelAttributes; - } - private: - CompiledPixelShader(u32 _shaderId, std::unordered_map &&_pixelAttributes); - private: - u32 shaderId; - std::unordered_map pixelAttributes; - }; -} diff --git a/include/RenderBackend/OpenGL/DeviceFrame.hpp b/include/RenderBackend/OpenGL/DeviceFrame.hpp index 12e578a..f10af4c 100644 --- a/include/RenderBackend/OpenGL/DeviceFrame.hpp +++ b/include/RenderBackend/OpenGL/DeviceFrame.hpp @@ -7,6 +7,7 @@ namespace amalgine { class DeviceMemory; + class ShaderProgram; class DeviceFrame { @@ -16,7 +17,7 @@ namespace amalgine ~DeviceFrame(); DeviceMemory* alloc(); - void draw(); + void draw(ShaderProgram *shader); private: u32 vertexArrayObjectId; std::vector buffers; diff --git a/include/RenderBackend/OpenGL/DeviceMemory.hpp b/include/RenderBackend/OpenGL/DeviceMemory.hpp index c37acfb..c9c8b23 100644 --- a/include/RenderBackend/OpenGL/DeviceMemory.hpp +++ b/include/RenderBackend/OpenGL/DeviceMemory.hpp @@ -3,21 +3,19 @@ #include "../../DataView.hpp" #include "../../utils.hpp" #include "../../Triangle2D.hpp" -#include -namespace amalgine -{ - class DeviceMemoryEmpty : public std::runtime_error - { - public: - DeviceMemoryEmpty(const std::string &errMsg) : std::runtime_error(errMsg) {} +namespace amalgine { + enum class DeviceMemoryType { + NONE, + VEC2, + VEC3 }; - + class DeviceMemory { DISABLE_COPY(DeviceMemory) friend class DeviceFrame; - friend class ShaderInputVec2; + friend class ShaderProgram; public: enum class StorageType { @@ -40,13 +38,15 @@ namespace amalgine //void copy(const DataView &data, StorageType storageType, PrimitiveType primitiveType = PrimitiveType::TRIANGLE); void copy(const DataView &triangles, StorageType storageType); void draw(); + + DeviceMemoryType get_type() const { return type; } private: DeviceMemory(); - void operator delete(void *data); void use() const; private: u32 vertexBufferObjectId; u32 primitiveType; u32 numVertices; + DeviceMemoryType type; }; } diff --git a/include/RenderBackend/OpenGL/PixelShader.hpp b/include/RenderBackend/OpenGL/PixelShader.hpp deleted file mode 100644 index ebe5a12..0000000 --- a/include/RenderBackend/OpenGL/PixelShader.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include "Shader.hpp" - -namespace amalgine { - class CompiledPixelShader; - - using PixelShaderMainFunc = std::function; - - class PixelShader : public Shader - { - DISABLE_COPY(PixelShader) - friend class ShaderProgram; - public: - PixelShader(); - - void defineMain(PixelShaderMainFunc mainFunc); - Result compile(); - }; -} diff --git a/include/RenderBackend/OpenGL/Shader.hpp b/include/RenderBackend/OpenGL/Shader.hpp index 7aecc4e..0f802ec 100644 --- a/include/RenderBackend/OpenGL/Shader.hpp +++ b/include/RenderBackend/OpenGL/Shader.hpp @@ -1,79 +1,26 @@ #pragma once -#include "../../DataView.hpp" -#include "../../utils.hpp" #include "../../Result.hpp" -#include "CommonShader.hpp" -#include "ShaderVec.hpp" -#include -#include -#include -#include -#include +#include "../../utils.hpp" +#include "../../types.hpp" +#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; + enum class Type { + VERTEX, + PIXEL + }; + + ~Shader(); + + static Result> compile(Type type, const char *str, int size); + const u32 id; + private: + Shader(u32 shader_id); }; } diff --git a/include/RenderBackend/OpenGL/ShaderProgram.hpp b/include/RenderBackend/OpenGL/ShaderProgram.hpp index c8740f8..ca96d0c 100644 --- a/include/RenderBackend/OpenGL/ShaderProgram.hpp +++ b/include/RenderBackend/OpenGL/ShaderProgram.hpp @@ -3,44 +3,27 @@ #include "../../Result.hpp" #include "../../types.hpp" #include "../../utils.hpp" -#include "ShaderVec.hpp" +#include "../../Vec.hpp" +#include "DeviceMemory.hpp" #include -#include +#include + +namespace amalgine { + class Shader; -namespace amalgine -{ - class CompiledVertexShader; - class CompiledPixelShader; - - class ShaderProgramUsedBeforeBuilt : public std::runtime_error - { - public: - // TODO: Add name to ShaderProgram so we know which shader has issue when - // an exception is thrown? - ShaderProgramUsedBeforeBuilt(); - }; - - class ShaderProgramNonExistingGlobalVariable : public std::runtime_error - { - public: - ShaderProgramNonExistingGlobalVariable(const char *variableName); - }; - class ShaderProgram { DISABLE_COPY(ShaderProgram) public: - ShaderProgram(); ~ShaderProgram(); - - bool setVertexShader(CompiledVertexShader *vertexShader); - bool setPixelShader(CompiledPixelShader *pixelShader); - - Result build(); + static Result> build(const std::vector &shaders); + int set_uniform(const char *name, const vec3f &value); + int set_vertex_input(const char *name, const DeviceMemory &data); + void use(); - ShaderProgramGlobalVec3 getGlobalVec3(const char *name); private: - u32 shaderProgramId; - bool built; + ShaderProgram(u32 shader_program_id); + private: + u32 program_id; }; } diff --git a/include/RenderBackend/OpenGL/ShaderVec.hpp b/include/RenderBackend/OpenGL/ShaderVec.hpp deleted file mode 100644 index 4353dd7..0000000 --- a/include/RenderBackend/OpenGL/ShaderVec.hpp +++ /dev/null @@ -1,142 +0,0 @@ -#pragma once - -#include "../../types.hpp" -#include "../../RenderBackend/OpenGL/DeviceMemory.hpp" -#include - -namespace amalgine -{ - class Shader; - class ShaderProgram; - - enum class AttributeType - { - NONE, - VEC2, - VEC3, - VEC4 - }; - - AttributeType getAttributeTypeByName(const char *attributeName); - - class ShaderGlobalVec - { - friend class Shader; - 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 Shader; - 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 Shader; - public: - const std::string& getName() const; - void setData(const DeviceMemory &data); - private: - ShaderInputVec2(Shader *_shader, i32 _attributeIndex) : - shader(_shader), - attributeIndex(_attributeIndex) - { - - } - private: - Shader *shader; - i32 attributeIndex; - }; - - class ShaderVec4 - { - public: - ShaderVec4(f32 x = 0.0f, f32 y = 0.0f, f32 z = 0.0f, f32 w = 0.0f) - { - result = "vec4("; - result += std::to_string(x); - result += ", "; - result += std::to_string(y); - result += ", "; - result += std::to_string(z); - result += ", "; - result += std::to_string(w); - result += ")"; - } - - ShaderVec4(const ShaderInputVec2 &vec2, f32 z = 0.0f, f32 w = 0.0f) - { - result = "vec4("; - result += vec2.getName(); - result += ", "; - result += std::to_string(z); - result += ", "; - result += std::to_string(w); - 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; - } - private: - std::string result; - }; - - class ShaderOutputVec4 - { - friend class Shader; - public: - const std::string& getName() const; - - void operator=(const ShaderVec4 &shaderVec4); - private: - ShaderOutputVec4(Shader *_shader, i32 _attributeIndex) : - shader(_shader), - attributeIndex(_attributeIndex) - { - - } - private: - Shader *shader; - i32 attributeIndex; - }; -} diff --git a/include/RenderBackend/OpenGL/VertexShader.hpp b/include/RenderBackend/OpenGL/VertexShader.hpp deleted file mode 100644 index 6fdd6a4..0000000 --- a/include/RenderBackend/OpenGL/VertexShader.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include "Shader.hpp" - -namespace amalgine { - class CompiledVertexShader; - - using VertexShaderMainFunc = std::function; - - class VertexShader : public Shader - { - DISABLE_COPY(VertexShader) - friend class ShaderProgram; - public: - VertexShader(); - - void defineMain(VertexShaderMainFunc mainFunc); - Result compile(); - }; -} diff --git a/include/Result.hpp b/include/Result.hpp index 749d381..50a0d0c 100644 --- a/include/Result.hpp +++ b/include/Result.hpp @@ -9,10 +9,10 @@ namespace amalgine class Result { public: - static Result Ok(const T &data) + static Result Ok(T data) { Result result; - result.data = data; + result.data = std::move(data); result.errorCode = 0; return result; } diff --git a/include/Vec.hpp b/include/Vec.hpp new file mode 100644 index 0000000..339a98b --- /dev/null +++ b/include/Vec.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "types.hpp" + +namespace amalgine { + struct vec3f { + f32 x; + f32 y; + f32 z; + + vec3f() : x(0.0f), y(0.0f) {} + vec3f(f32 x, f32 y, f32 z) : x(x), y(y), z(z) {} + }; +} \ No newline at end of file diff --git a/include/types.hpp b/include/types.hpp index 4f95730..924dd67 100644 --- a/include/types.hpp +++ b/include/types.hpp @@ -1,9 +1,8 @@ #pragma once -#include +#include -namespace amalgine -{ +namespace amalgine { typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; -- cgit v1.2.3