aboutsummaryrefslogtreecommitdiff
path: root/source/Material.hpp
blob: f1cd7dd15329dfad34d32742461bf8fec7cc1ae1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once

#include "Ires.hpp"
#include "RcPtr.hpp"
#include "Shader.hpp"
#include "Texture.hpp"

#include <glad/glad.h>
#include <robin_hood.h>
#include <cstddef>
#include <cstdint>
#include <glm/glm.hpp>
#include <memory>
#include <span>
#include <string_view>
#include <vector>

// Forward declarations
class Material;
class IresMaterial;

class Material : public RefCounted {
	friend class IresMaterial;

public:
	// NOTE: specialize between scalar vs matrix vs vector to save memory

	enum UniformType : uint16_t {
		UT_Scalar,
		UT_Vector,
		UT_Matrix,
	};

	struct UniformIndex {
		UniformType type;
		uint16_t index;
	};

	struct ScalarUniform {
		union {
			float floatValue;
			int32_t intValue;
			uint32_t uintValue;
		};
		GLenum actualType;
		/* Transient */ int infoUniformIndex;
		/* Transient */ GLint location;
	};

	struct VectorUniform {
		float value[4];
		int actualLength;
		/* Transient */ int infoUniformIndex;
		/* Transient */ GLint location;
	};

	struct MatrixUniform {
		float value[16];
		int actualWidth;
		int actualHeight;
		/* Transient */ int infoUniformIndex;
		/* Transient */ GLint location;
	};

	struct TextureUniform {
		RcPtr<Texture> value;
		/* Transient */ int infoUniformIndex;
		/* Transient */ GLint location;
	};

	IresMaterial* mIres = nullptr;
	RcPtr<Shader> mShader;
	std::vector<ScalarUniform> mBoundScalars;
	std::vector<VectorUniform> mBoundVectors;
	std::vector<MatrixUniform> mBoundMatrices;
	std::vector<TextureUniform> mBoundTextures;

public:
	Material();

	void SetFloat(const char* name, float value);
	void SetInt(const char* name, int32_t value);
	void SetUInt(const char* name, uint32_t value);

	/// Instanciated for length == 1, 2, 3, 4
	template <int length>
	void SetVector(const char* name, const glm::vec<length, float>& vec);

	/// Instanciated for sizes (2,2) (3,3) (4,4) (2,3) (3,2) (2,4) (4,2) (3,4) (4,3)
	template <int width, int height>
	void SetMatrix(const char* name, const glm::mat<width, height, float>& mat);

	void SetTexture(const char* name, Texture* texture);

	std::span<const VectorUniform> GetVectors() const;
	std::span<const MatrixUniform> GetMatrices() const;
	std::span<const TextureUniform> GetTextures() const;
	Shader* GetShader() const;
	void SetShader(Shader* shader);

	IresMaterial* GetIres() const { return mIres; }

	bool IsValid() const;

	void UseUniforms() const;
};

// Initialized in main()
inline RcPtr<Material> gDefaultMaterial;

class IresMaterial : public IresObject {
private:
	RcPtr<Material> mInstance;

public:
	IresMaterial();

	Material* GetInstance() const;
	void InvalidateInstance();

	void ShowEditor(IEditor& editor) override;

	void Write(IresWritingContext& ctx, rapidjson::Value& value, rapidjson::Document& root) const override;
	void Read(IresLoadingContext& ctx, const rapidjson::Value& value) override;
};