aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-10-27 01:30:34 +0200
committerdec05eba <dec05eba@protonmail.com>2021-11-18 15:21:48 +0100
commitfbd2e5d9a802db4fb5e056705ec599ac423e09be (patch)
treeebe71975909a0767b5de045c28fc28d09292bca3 /include
parent24934c7823457767855bd0abb249a55b8a8c077d (diff)
Create Shader class as a base for vertex and pixel shader
Diffstat (limited to 'include')
-rw-r--r--include/RenderBackend/OpenGL/PixelShader.hpp74
-rw-r--r--include/RenderBackend/OpenGL/Shader.hpp79
-rw-r--r--include/RenderBackend/OpenGL/ShaderVec.hpp25
-rw-r--r--include/RenderBackend/OpenGL/VertexShader.hpp71
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;
};
}