aboutsummaryrefslogtreecommitdiff
path: root/include/RenderBackend
diff options
context:
space:
mode:
Diffstat (limited to 'include/RenderBackend')
-rw-r--r--include/RenderBackend/OpenGL/CommonShader.hpp44
-rw-r--r--include/RenderBackend/OpenGL/CompiledShader.hpp37
-rw-r--r--include/RenderBackend/OpenGL/DeviceFrame.hpp3
-rw-r--r--include/RenderBackend/OpenGL/DeviceMemory.hpp20
-rw-r--r--include/RenderBackend/OpenGL/PixelShader.hpp20
-rw-r--r--include/RenderBackend/OpenGL/Shader.hpp81
-rw-r--r--include/RenderBackend/OpenGL/ShaderProgram.hpp43
-rw-r--r--include/RenderBackend/OpenGL/ShaderVec.hpp142
-rw-r--r--include/RenderBackend/OpenGL/VertexShader.hpp20
9 files changed, 39 insertions, 371 deletions
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 <string>
-
-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 <unordered_map>
-#include <string>
-
-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<std::string, i32> &&_pixelAttributes);
- private:
- u32 shaderId;
- std::unordered_map<std::string, i32> 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<DeviceMemory*> 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 <stdexcept>
-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<f32> &data, StorageType storageType, PrimitiveType primitiveType = PrimitiveType::TRIANGLE);
void copy(const DataView<Triangle2D> &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<void()>;
-
- class PixelShader : public Shader
- {
- DISABLE_COPY(PixelShader)
- friend class ShaderProgram;
- public:
- PixelShader();
-
- void defineMain(PixelShaderMainFunc mainFunc);
- Result<CompiledPixelShader*> 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 <string>
-#include <unordered_map>
-#include <vector>
-#include <stdexcept>
-#include <functional>
+#include "../../utils.hpp"
+#include "../../types.hpp"
+#include <memory>
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;
+ enum class Type {
+ VERTEX,
+ PIXEL
+ };
+
+ ~Shader();
+
+ static Result<std::unique_ptr<Shader>> 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 <vector>
-#include <stdexcept>
+#include <memory>
+
+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<bool> build();
+ static Result<std::unique_ptr<ShaderProgram>> build(const std::vector<Shader*> &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 <string>
-
-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<ShaderVec4()>;
-
- class VertexShader : public Shader
- {
- DISABLE_COPY(VertexShader)
- friend class ShaderProgram;
- public:
- VertexShader();
-
- void defineMain(VertexShaderMainFunc mainFunc);
- Result<CompiledVertexShader*> compile();
- };
-}