aboutsummaryrefslogtreecommitdiff
path: root/backends/graphics/opengl/shader.h
blob: ec1e516d149ef5672fd65459f51a429adfc0eac0 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef BACKENDS_GRAPHICS_OPENGL_SHADER_H
#define BACKENDS_GRAPHICS_OPENGL_SHADER_H

#include "backends/graphics/opengl/opengl-sys.h"

#if !USE_FORCED_GLES

#include "common/singleton.h"
#include "common/hash-str.h"
#include "common/ptr.h"

namespace OpenGL {

/**
 * A generic uniform value interface for a shader program.
 */
class ShaderUniformValue {
public:
	virtual ~ShaderUniformValue() {}

	/**
	 * Setup the the value to the given location.
	 *
	 * @param location Location of the uniform.
	 */
	virtual void set(GLint location) const = 0;
};

/**
 * Integer value for a shader uniform.
 */
class ShaderUniformInteger : public ShaderUniformValue {
public:
	ShaderUniformInteger(GLint value) : _value(value) {}

	virtual void set(GLint location) const override;

private:
	const GLint _value;
};

/**
 * Float value for a shader uniform.
 */
class ShaderUniformFloat : public ShaderUniformValue {
public:
	ShaderUniformFloat(GLfloat value) : _value(value) {}

	virtual void set(GLint location) const override;

private:
	const GLfloat _value;
};

/**
 * 4x4 Matrix value for a shader uniform.
 */
class ShaderUniformMatrix44 : public ShaderUniformValue {
public:
	ShaderUniformMatrix44(const GLfloat *mat44) {
		memcpy(_matrix, mat44, sizeof(_matrix));
	}

	virtual void set(GLint location) const override;

private:
	GLfloat _matrix[4*4];
};

class Shader {
public:
	Shader(const Common::String &vertex, const Common::String &fragment);
	~Shader();

	/**
	 * Destroy the shader program.
	 *
	 * This keeps the vertex and fragment shader sources around and thus
	 * allows for recreating the shader on context recreation. It also keeps
	 * the uniform state around.
	 */
	void destroy();

	/**
	 * Recreate shader program.
	 *
	 * @return true on success, false on failure.
	 */
	bool recreate();

	/**
	 * Make shader active.
	 */
	void activate();

	/**
	 * Make shader inactive.
	 */
	void deactivate();

	/**
	 * Return location for attribute with given name.
	 *
	 * @param name Name of the attribute to look up in the shader.
	 * @return The loctaion of -1 if attribute was not found.
	 */
	GLint getAttributeLocation(const char *name) const;
	GLint getAttributeLocation(const Common::String &name) const {
		return getAttributeLocation(name.c_str());
	}

	/**
	 * Return location for uniform with given name.
	 *
	 * @param name Name of the uniform to look up in the shader.
	 * @return The location or -1 if uniform was not found.
	 */
	GLint getUniformLocation(const char *name) const;
	GLint getUniformLocation(const Common::String &name) const {
		return getUniformLocation(name.c_str());
	}

	/**
	 * Bind value to uniform.
	 *
	 * @param name  The name of the uniform to be set.
	 * @param value The value to be set.
	 * @return 'false' on error (i.e. uniform unknown or otherwise),
	 *         'true' otherwise.
	 */
	bool setUniform(const Common::String &name, ShaderUniformValue *value);

	/**
	 * Bind integer value to uniform.
	 *
	 * @param name  The name of the uniform to be set.
	 * @param value The value to be set.
	 * @return 'false' on error (i.e. uniform unknown or otherwise),
	 *         'true' otherwise.
	 */
	bool setUniform1I(const Common::String &name, GLint value) {
		return setUniform(name, new ShaderUniformInteger(value));
	}
protected:
	/**
	 * Vertex shader sources.
	 */
	const Common::String _vertex;

	/**
	 * Fragment shader sources.
	 */
	const Common::String _fragment;

	/**
	 * Whether the shader is active or not.
	 */
	bool _isActive;

	/**
	 * Shader program handle.
	 */
	GLprogram _program;

	/**
	 * A uniform descriptor.
	 *
	 * This stores the state of a shader uniform. The state is made up of the
	 * uniform location, whether the state was altered since last set, and the
	 * value of the uniform.
	 */
	struct Uniform {
		Uniform() : location(-1), altered(false), value() {}
		Uniform(GLint loc, ShaderUniformValue *val)
		    : location(loc), altered(true), value(val) {}

		/**
		 * Write uniform value into currently active shader.
		 */
		void set() {
			if (altered && value) {
				value->set(location);
				altered = false;
			}
		}

		/**
		 * The location of the uniform or -1 in case it does not exist.
		 */
		GLint location;

		/**
		 * Whether the uniform state was aletered since last 'set'.
		 */
		bool altered;

		/**
		 * The value of the uniform.
		 */
		Common::SharedPtr<ShaderUniformValue> value;
	};

	typedef Common::HashMap<Common::String, Uniform> UniformMap;

	/**
	 * Map from uniform name to associated uniform description.
	 */
	UniformMap _uniforms;

	/**
	 * Compile a vertex or fragment shader.
	 *
	 * @param source     Sources to the shader.
	 * @param shaderType Type of shader to compile (GL_FRAGMENT_SHADER_ARB or
	 *                   GL_VERTEX_SHADER_ARB)
	 * @return The shader object or 0 on failure.
	 */
	static GLshader compileShader(const char *source, GLenum shaderType);
};

class ShaderManager : public Common::Singleton<ShaderManager> {
public:
	enum ShaderUsage {
		/** Default shader implementing the GL fixed-function pipeline. */
		kDefault = 0,

		/** CLUT8 look up shader. */
		kCLUT8LookUp,

		/** Number of built-in shaders. Should not be used for query. */
		kMaxUsages
	};

	/**
	 * Notify shader manager about context destruction.
	 */
	void notifyDestroy();

	/**
	 * Notify shader manager about context creation.
	 */
	void notifyCreate();

	/**
	 * Query a built-in shader.
	 */
	Shader *query(ShaderUsage shader) const;

private:
	friend class Common::Singleton<SingletonBaseType>;
	ShaderManager();
	~ShaderManager();

	bool _initializeShaders;

	Shader *_builtIn[kMaxUsages];
};

} // End of namespace OpenGL

/** Shortcut for accessing the font manager. */
#define ShaderMan (OpenGL::ShaderManager::instance())

#endif // !USE_FORCED_GLES

#endif