aboutsummaryrefslogtreecommitdiff
path: root/source/30-game/Shader.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/30-game/Shader.hpp')
-rw-r--r--source/30-game/Shader.hpp73
1 files changed, 40 insertions, 33 deletions
diff --git a/source/30-game/Shader.hpp b/source/30-game/Shader.hpp
index 707e6cc..cb980cd 100644
--- a/source/30-game/Shader.hpp
+++ b/source/30-game/Shader.hpp
@@ -7,8 +7,10 @@
#include <glad/glad.h>
#include <robin_hood.h>
+#include <json_dto/pub.hpp>
#include <memory>
#include <string_view>
+#include <variant>
#include <vector>
// TODO move to variable after pattern matching is in the language
@@ -17,44 +19,44 @@
class Shader;
class IresShader;
-struct ShaderVariable {
- enum Kind {
- KD_Math,
- KD_Sampler,
- };
-
+struct ShaderMathVariable {
std::string name;
- Kind kind;
GLuint location;
Tags::VertexElementSemantic semantic = Tags::VES_Generic;
-
- virtual void ShowInfo() const = 0;
-
-protected:
- ShaderVariable(Kind kind)
- : kind{ kind } {}
-};
-
-struct ShaderMathVariable : public ShaderVariable {
GLenum scalarType;
- int arrayLength;
int width;
int height;
+ int arrayLength = 1;
- ShaderMathVariable()
- : ShaderVariable(KD_Math) {}
+ void ShowInfo() const;
- virtual void ShowInfo() const override;
+ template <typename TJsonIo>
+ void json_io(TJsonIo& io) {
+ io& json_dto::mandatory("Name", name);
+ io& json_dto::mandatory("Semantic", static_cast<int>(semantic));
+ io& json_dto::mandatory("ScalarType", scalarType);
+ io& json_dto::mandatory("Width", width);
+ io& json_dto::mandatory("Height", height);
+ io& json_dto::optional("ArrayLength", arrayLength, 1);
+ }
};
-struct ShaderSamplerVariable : public ShaderVariable {
- GLenum type;
- int arrayLength;
+struct ShaderSamplerVariable {
+ std::string name;
+ GLuint location;
+ Tags::VertexElementSemantic semantic = Tags::VES_Generic;
+ GLenum samplerType;
+ int arrayLength = 1;
- ShaderSamplerVariable()
- : ShaderVariable(KD_Sampler) {}
+ void ShowInfo() const;
- virtual void ShowInfo() const override;
+ template <typename TJsonIo>
+ void json_io(TJsonIo& io) {
+ io& json_dto::mandatory("Name", name);
+ io& json_dto::mandatory("Semantic", static_cast<int>(semantic));
+ io& json_dto::mandatory("SamplerType", samplerType);
+ io& json_dto::optional("ArrayLength", arrayLength, 1);
+ }
};
struct ShaderThingId {
@@ -62,24 +64,29 @@ struct ShaderThingId {
KD_Input,
KD_Output,
KD_Uniform,
- KD_Invalid,
};
- Kind kind = KD_Invalid;
- int index = 0;
-
- bool IsValid() const;
+ Kind kind;
+ int index;
};
struct ShaderInfo {
robin_hood::unordered_map<std::string, ShaderThingId, StringHash, StringEqual> things;
std::vector<ShaderMathVariable> inputs;
std::vector<ShaderMathVariable> outputs;
- std::vector<std::unique_ptr<ShaderVariable>> uniforms;
+ std::vector<std::variant<ShaderMathVariable, ShaderSamplerVariable>> uniforms;
+ // Find the first variable with the matching semantic
GLuint FindInputLocation(Tags::VertexElementSemantic semantic);
GLuint FindOutputLocation(Tags::VertexElementSemantic semantic);
- ShaderVariable* FindVariable(const ShaderThingId& thing);
+
+ template <typename TJsonIo>
+ void json_io(TJsonIo& io) {
+ io& json_dto::mandatory("Inputs", inputs);
+ io& json_dto::mandatory("Outputs", outputs);
+ // TODO make json_dto support std::variant
+// io& json_dto::mandatory("Uniforms", uniforms);
+ }
};
class Shader : public RefCounted {