1 | /** |
2 | * Copyright (c) 2006-2023 LOVE Development Team |
3 | * |
4 | * This software is provided 'as-is', without any express or implied |
5 | * warranty. In no event will the authors be held liable for any damages |
6 | * arising from the use of this software. |
7 | * |
8 | * Permission is granted to anyone to use this software for any purpose, |
9 | * including commercial applications, and to alter it and redistribute it |
10 | * freely, subject to the following restrictions: |
11 | * |
12 | * 1. The origin of this software must not be misrepresented; you must not |
13 | * claim that you wrote the original software. If you use this software |
14 | * in a product, an acknowledgment in the product documentation would be |
15 | * appreciated but is not required. |
16 | * 2. Altered source versions must be plainly marked as such, and must not be |
17 | * misrepresented as being the original software. |
18 | * 3. This notice may not be removed or altered from any source distribution. |
19 | **/ |
20 | |
21 | #pragma once |
22 | |
23 | // LOVE |
24 | #include "common/Object.h" |
25 | #include "common/StringMap.h" |
26 | #include "Texture.h" |
27 | #include "ShaderStage.h" |
28 | #include "Resource.h" |
29 | |
30 | // STL |
31 | #include <string> |
32 | #include <map> |
33 | #include <vector> |
34 | #include <stddef.h> |
35 | |
36 | namespace glslang |
37 | { |
38 | class TShader; |
39 | } |
40 | |
41 | namespace love |
42 | { |
43 | namespace graphics |
44 | { |
45 | |
46 | class Graphics; |
47 | |
48 | // A GLSL shader |
49 | class Shader : public Object, public Resource |
50 | { |
51 | public: |
52 | |
53 | static love::Type type; |
54 | |
55 | enum Language |
56 | { |
57 | LANGUAGE_GLSL1, |
58 | LANGUAGE_ESSL1, |
59 | LANGUAGE_GLSL3, |
60 | LANGUAGE_ESSL3, |
61 | LANGUAGE_MAX_ENUM |
62 | }; |
63 | |
64 | // Built-in uniform variables. |
65 | enum BuiltinUniform |
66 | { |
67 | BUILTIN_TEXTURE_MAIN, |
68 | BUILTIN_TEXTURE_VIDEO_Y, |
69 | BUILTIN_TEXTURE_VIDEO_CB, |
70 | BUILTIN_TEXTURE_VIDEO_CR, |
71 | BUILTIN_MATRIX_VIEW_FROM_LOCAL, |
72 | BUILTIN_MATRIX_CLIP_FROM_VIEW, |
73 | BUILTIN_MATRIX_CLIP_FROM_LOCAL, |
74 | BUILTIN_MATRIX_VIEW_NORMAL_FROM_LOCAL, |
75 | BUILTIN_POINT_SIZE, |
76 | BUILTIN_SCREEN_SIZE, |
77 | BUILTIN_MAX_ENUM |
78 | }; |
79 | |
80 | // Types of potential uniform variables used in love's shaders. |
81 | enum UniformType |
82 | { |
83 | UNIFORM_FLOAT, |
84 | UNIFORM_MATRIX, |
85 | UNIFORM_INT, |
86 | UNIFORM_UINT, |
87 | UNIFORM_BOOL, |
88 | UNIFORM_SAMPLER, |
89 | UNIFORM_UNKNOWN, |
90 | UNIFORM_MAX_ENUM |
91 | }; |
92 | |
93 | enum StandardShader |
94 | { |
95 | STANDARD_DEFAULT, |
96 | STANDARD_VIDEO, |
97 | STANDARD_ARRAY, |
98 | STANDARD_MAX_ENUM |
99 | }; |
100 | |
101 | struct MatrixSize |
102 | { |
103 | short columns; |
104 | short rows; |
105 | }; |
106 | |
107 | struct UniformInfo |
108 | { |
109 | int location; |
110 | int count; |
111 | |
112 | union |
113 | { |
114 | int components; |
115 | MatrixSize matrix; |
116 | }; |
117 | |
118 | UniformType baseType; |
119 | TextureType textureType; |
120 | bool isDepthSampler; |
121 | std::string name; |
122 | |
123 | union |
124 | { |
125 | void *data; |
126 | float *floats; |
127 | int *ints; |
128 | unsigned int *uints; |
129 | }; |
130 | |
131 | size_t dataSize; |
132 | |
133 | Texture **textures; |
134 | }; |
135 | |
136 | // Pointer to currently active Shader. |
137 | static Shader *current; |
138 | |
139 | // Pointer to the default Shader. |
140 | static Shader *standardShaders[STANDARD_MAX_ENUM]; |
141 | |
142 | Shader(ShaderStage *vertex, ShaderStage *pixel); |
143 | virtual ~Shader(); |
144 | |
145 | /** |
146 | * Binds this Shader's program to be used when rendering. |
147 | **/ |
148 | virtual void attach() = 0; |
149 | |
150 | /** |
151 | * Attach a default shader. |
152 | **/ |
153 | static void attachDefault(StandardShader defaultType); |
154 | |
155 | /** |
156 | * Gets whether any of the default shaders are currently active. |
157 | **/ |
158 | static bool isDefaultActive(); |
159 | |
160 | /** |
161 | * Returns any warnings this Shader may have generated. |
162 | **/ |
163 | virtual std::string getWarnings() const = 0; |
164 | |
165 | virtual int getVertexAttributeIndex(const std::string &name) = 0; |
166 | |
167 | virtual const UniformInfo *getUniformInfo(const std::string &name) const = 0; |
168 | virtual const UniformInfo *getUniformInfo(BuiltinUniform builtin) const = 0; |
169 | |
170 | virtual void updateUniform(const UniformInfo *info, int count) = 0; |
171 | |
172 | virtual void sendTextures(const UniformInfo *info, Texture **textures, int count) = 0; |
173 | |
174 | /** |
175 | * Gets whether a uniform with the specified name exists and is actively |
176 | * used in the shader. |
177 | **/ |
178 | virtual bool hasUniform(const std::string &name) const = 0; |
179 | |
180 | /** |
181 | * Sets the textures used when rendering a video. For internal use only. |
182 | **/ |
183 | virtual void setVideoTextures(Texture *ytexture, Texture *cbtexture, Texture *crtexture) = 0; |
184 | |
185 | TextureType getMainTextureType() const; |
186 | void checkMainTextureType(TextureType textype, bool isDepthSampler) const; |
187 | void checkMainTexture(Texture *texture) const; |
188 | |
189 | static bool validate(ShaderStage *vertex, ShaderStage *pixel, std::string &err); |
190 | |
191 | static bool initialize(); |
192 | static void deinitialize(); |
193 | |
194 | static bool getConstant(const char *in, Language &out); |
195 | static bool getConstant(Language in, const char *&out); |
196 | |
197 | static bool getConstant(const char *in, BuiltinUniform &out); |
198 | static bool getConstant(BuiltinUniform in, const char *&out); |
199 | |
200 | protected: |
201 | |
202 | StrongRef<ShaderStage> stages[ShaderStage::STAGE_MAX_ENUM]; |
203 | |
204 | private: |
205 | |
206 | static StringMap<Language, LANGUAGE_MAX_ENUM>::Entry languageEntries[]; |
207 | static StringMap<Language, LANGUAGE_MAX_ENUM> languages; |
208 | |
209 | // Names for the built-in uniform variables. |
210 | static StringMap<BuiltinUniform, BUILTIN_MAX_ENUM>::Entry builtinNameEntries[]; |
211 | static StringMap<BuiltinUniform, BUILTIN_MAX_ENUM> builtinNames; |
212 | |
213 | }; // Shader |
214 | |
215 | } // graphics |
216 | } // love |
217 | |