aboutsummaryrefslogtreecommitdiff
path: root/source/Shader.hpp
blob: 8af52175040896613aae1834570125b459836ad1 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#pragma once

#include "EditorAttachment.hpp"
#include "GraphicsTags.hpp"
#include "RcPtr.hpp"

#include <absl/container/flat_hash_map.h>
#include <glad/glad.h>
#include <filesystem>
#include <memory>
#include <string_view>
#include <vector>

// TODO move to variable after pattern matching is in the language

struct ShaderVariable {
	enum Kind {
		KD_Math,
		KD_Sampler,
		KD_UniformBlock,
	};

	Kind kind;

protected:
	ShaderVariable(Kind kind)
		: kind{ kind } {}
};

struct ShaderMathVariable : public ShaderVariable {
	std::string name;
	GLuint location;
	GLenum scalarType;
	int arrayLength;
	int width;
	int height;

	ShaderMathVariable()
		: ShaderVariable(KD_Math) {}
};

struct ShaderSamplerVariable : public ShaderVariable {
	std::string name;
	GLuint location;
	GLenum type;
	int arrayLength;

	ShaderSamplerVariable()
		: ShaderVariable(KD_Sampler) {}
};

struct ShaderUniformBlockVariable : public ShaderVariable {
	std::string name;
	/// Possible values: KD_Math
	std::vector<std::unique_ptr<ShaderVariable>> items;
	GLuint index;

	ShaderUniformBlockVariable()
		: ShaderVariable(KD_UniformBlock) {}
};

struct ShaderInfo {
	enum ThingKind {
		TKD_Input,
		TKD_Output,
		TKD_Uniform,
		TKD_UniformBlock,
	};

	struct ThingId {
		ThingKind kind;
		int index;
	};

	struct InputOutputThing {
		ShaderMathVariable variable;
		Tags::VertexElementSemantic semantic = Tags::VES_Generic;
	};

	absl::flat_hash_map<std::string, ThingId> things;
	std::vector<InputOutputThing> inputs;
	std::vector<InputOutputThing> outputs;
	/// Possible values: KD_Math, KD_Sampler
	std::vector<std::unique_ptr<ShaderVariable>> uniforms;
	std::vector<ShaderUniformBlockVariable> uniformBlocks;

	bool SaveToFile(const std::filesystem::path& filePath) const;
	bool LoadFromFile(const std::filesystem::path& filePath);
};

class Shader : public RefCounted {
private:
	std::string mName;
	std::unique_ptr<ShaderInfo> mInfo;
	std::unique_ptr<EditorAttachment> mEditorAttachment;
	GLuint mHandle = 0;

public:
	Shader(std::string name = "");
	~Shader();
	Shader(const Shader&) = delete;
	Shader& operator=(const Shader&) = delete;
	Shader(Shader&&) = default;
	Shader& operator=(Shader&&) = default;

	enum ErrorCode {
		Success,
		/// Generated when Init*() functions are called on an already initialized Shader object.
		ShaderAlreadyCreated,
		/// Generated when the one-source-file text contains invalid or duplicate shader variants.
		InvalidShaderVariant,
		FileIOFailed,
		CompilationFailed,
		LinkingFailed,
	};

	struct ShaderSources {
		std::string_view vertex;
		std::string_view geometry;
		std::string_view tessControl;
		std::string_view tessEval;
		std::string_view fragment;
	};

	/// Create shader by compiling each shader source file separately and then combining them together
	/// into a Shader object.
	ErrorCode InitFromSources(const ShaderSources& sources);

	/// The given text will be split into different shader sections according to #type directives,
	/// and combined to form a Shader object.
	/// For OpenGL, this process involves compililng each section separately and then linking them
	/// together.
	///
	/// There are a directive for each shader type
	/// - `#type vertex`: Vertex shader
	/// - `#type geometry`: Geometry shader
	/// - `#type tessellation_control`: Tessellation control shader
	/// - `#type tessellation_evaluation`: Tessellation evaluation shader
	/// - `#type fragment`: Fragment shader
	ErrorCode InitFromSource(std::string_view source);

	EditorAttachment* GetEditorAttachment() const { return mEditorAttachment.get(); }
	void SetEditorAttachment(EditorAttachment* attachment) { mEditorAttachment.reset(attachment); }

	bool CreateEmptyInfoIfAbsent();
	bool GatherInfoIfAbsent();
	ShaderInfo* GetInfo() const;
	/// If not empty, this name must not duplicate with any other shader object in the process.
	const std::string& GetName() const;
	GLuint GetProgram() const;

	bool IsValid() const;
};

class ShaderManager {
public:
	static inline ShaderManager* instance = nullptr;

private:
	absl::flat_hash_map<std::string_view, RcPtr<Shader>> mShaders;

public:
	void DiscoverShaders();

	const auto& GetShaders() const { return mShaders; }
	Shader* FindShader(std::string_view name);
};