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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* 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.
*
*/
#include "backends/graphics/opengl/shader.h"
#if !USE_FORCED_GLES
#include "common/textconsole.h"
#include "common/util.h"
namespace Common {
DECLARE_SINGLETON(OpenGL::ShaderManager);
}
namespace OpenGL {
namespace {
#pragma mark - Builtin Shader Sources -
const char *const g_defaultVertexShader =
"attribute vec4 position;\n"
"attribute vec2 texCoordIn;\n"
"attribute vec4 blendColorIn;\n"
"\n"
"uniform mat4 projection;\n"
"\n"
"varying vec2 texCoord;\n"
"varying vec4 blendColor;\n"
"\n"
"void main(void) {\n"
"\ttexCoord = texCoordIn;\n"
"\tblendColor = blendColorIn;\n"
"\tgl_Position = projection * position;\n"
"}\n";
const char *const g_defaultFragmentShader =
"varying vec2 texCoord;\n"
"varying vec4 blendColor;\n"
"\n"
"uniform sampler2D shaderTexture;\n"
"\n"
"void main(void) {\n"
"\tgl_FragColor = blendColor * texture2D(shaderTexture, texCoord);\n"
"}\n";
const char *const g_lookUpFragmentShader =
"varying vec2 texCoord;\n"
"varying vec4 blendColor;\n"
"\n"
"uniform sampler2D shaderTexture;\n"
"uniform sampler2D palette;\n"
"\n"
"const float adjustFactor = 255.0 / 256.0 + 1.0 / (2.0 * 256.0);"
"\n"
"void main(void) {\n"
"\tvec4 index = texture2D(shaderTexture, texCoord);\n"
"\tgl_FragColor = blendColor * texture2D(palette, vec2(index.a * adjustFactor, 0.0));\n"
"}\n";
// Taken from: https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_03#OpenGL_ES_2_portability
const char *const g_precisionDefines =
"#ifdef GL_ES\n"
"\t#if defined(GL_FRAGMENT_PRECISION_HIGH) && GL_FRAGMENT_PRECISION_HIGH == 1\n"
"\t\tprecision highp float;\n"
"\t#else\n"
"\t\tprecision mediump float;\n"
"\t#endif\n"
"#else\n"
"\t#define highp\n"
"\t#define mediump\n"
"\t#define lowp\n"
"#endif\n";
} // End of anonymous namespace
#pragma mark - Uniform Values -
void ShaderUniformInteger::set(GLint location) const {
GL_CALL(glUniform1i(location, _value));
}
void ShaderUniformFloat::set(GLint location) const {
GL_CALL(glUniform1f(location, _value));
}
void ShaderUniformMatrix44::set(GLint location) const {
GL_CALL(glUniformMatrix4fv(location, 1, GL_FALSE, _matrix));
}
#pragma mark - Shader Implementation -
Shader::Shader(const Common::String &vertex, const Common::String &fragment)
: _vertex(vertex), _fragment(fragment), _isActive(false), _program(0), _uniforms() {
recreate();
}
Shader::~Shader() {
// According to extension specification glDeleteObjectARB silently ignores
// 0. However, with nVidia drivers this can cause GL_INVALID_VALUE, thus
// we do not call it with 0 as parameter to avoid warnings.
if (_program) {
GL_CALL_SAFE(glDeleteProgram, (_program));
}
}
void Shader::destroy() {
// According to extension specification glDeleteObjectARB silently ignores
// 0. However, with nVidia drivers this can cause GL_INVALID_VALUE, thus
// we do not call it with 0 as parameter to avoid warnings.
if (_program) {
GL_CALL(glDeleteProgram(_program));
_program = 0;
}
}
bool Shader::recreate() {
// Make sure any old programs are destroyed properly.
destroy();
GLshader vertexShader = compileShader(_vertex.c_str(), GL_VERTEX_SHADER);
if (!vertexShader) {
return false;
}
GLshader fragmentShader = compileShader(_fragment.c_str(), GL_FRAGMENT_SHADER);
if (!fragmentShader) {
GL_CALL(glDeleteShader(vertexShader));
return false;
}
GL_ASSIGN(_program, glCreateProgram());
if (!_program) {
GL_CALL(glDeleteShader(vertexShader));
GL_CALL(glDeleteShader(fragmentShader));
return false;
}
GL_CALL(glAttachShader(_program, vertexShader));
GL_CALL(glAttachShader(_program, fragmentShader));
GL_CALL(glLinkProgram(_program));
GL_CALL(glDetachShader(_program, fragmentShader));
GL_CALL(glDeleteShader(fragmentShader));
GL_CALL(glDetachShader(_program, vertexShader));
GL_CALL(glDeleteShader(vertexShader));
GLint result;
GL_CALL(glGetProgramiv(_program, GL_LINK_STATUS, &result));
if (result == GL_FALSE) {
GLint logSize;
GL_CALL(glGetProgramiv(_program, GL_INFO_LOG_LENGTH, &logSize));
GLchar *log = new GLchar[logSize];
GL_CALL(glGetProgramInfoLog(_program, logSize, nullptr, log));
warning("Could not link shader: \"%s\"", log);
delete[] log;
destroy();
return false;
}
// Set program object in case shader is active during recreation.
if (_isActive) {
GL_CALL(glUseProgram(_program));
}
for (UniformMap::iterator i = _uniforms.begin(), end = _uniforms.end(); i != end; ++i) {
i->_value.location = getUniformLocation(i->_key.c_str());
i->_value.altered = true;
if (_isActive) {
i->_value.set();
}
}
return true;
}
void Shader::activate() {
// Activate program.
GL_CALL(glUseProgram(_program));
// Reset changed uniform values.
for (UniformMap::iterator i = _uniforms.begin(), end = _uniforms.end(); i != end; ++i) {
i->_value.set();
}
_isActive = true;
}
void Shader::deactivate() {
_isActive = false;
}
GLint Shader::getAttributeLocation(const char *name) const {
GLint result = -1;
GL_ASSIGN(result, glGetAttribLocation(_program, name));
return result;
}
GLint Shader::getUniformLocation(const char *name) const {
GLint result = -1;
GL_ASSIGN(result, glGetUniformLocation(_program, name));
return result;
}
bool Shader::setUniform(const Common::String &name, ShaderUniformValue *value) {
UniformMap::iterator uniformIter = _uniforms.find(name);
Uniform *uniform;
if (uniformIter == _uniforms.end()) {
const GLint location = getUniformLocation(name.c_str());
if (location == -1) {
delete value;
return false;
}
uniform = &_uniforms[name];
uniform->location = location;
} else {
uniform = &uniformIter->_value;
}
uniform->value = Common::SharedPtr<ShaderUniformValue>(value);
uniform->altered = true;
if (_isActive) {
uniform->set();
}
return true;
}
GLshader Shader::compileShader(const char *source, GLenum shaderType) {
const GLchar *versionSource = g_context.type == kContextGLES2 ? "#version 100\n" : "#version 120\n";
GLshader handle;
GL_ASSIGN(handle, glCreateShader(shaderType));
if (!handle) {
return 0;
}
const char *const shaderSources[] = {
versionSource,
g_precisionDefines,
source
};
GL_CALL(glShaderSource(handle, ARRAYSIZE(shaderSources), shaderSources, nullptr));
GL_CALL(glCompileShader(handle));
GLint result;
GL_CALL(glGetShaderiv(handle, GL_COMPILE_STATUS, &result));
if (result == GL_FALSE) {
GLint logSize;
GL_CALL(glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logSize));
GLchar *log = new GLchar[logSize];
GL_CALL(glGetShaderInfoLog(handle, logSize, nullptr, log));
warning("Could not compile shader \"%s\": \"%s\"", source, log);
delete[] log;
GL_CALL(glDeleteShader(handle));
return 0;
}
return handle;
}
ShaderManager::ShaderManager() : _initializeShaders(true) {
for (int i = 0; i < ARRAYSIZE(_builtIn); ++i) {
_builtIn[i] = nullptr;
}
}
ShaderManager::~ShaderManager() {
for (int i = 0; i < ARRAYSIZE(_builtIn); ++i) {
delete _builtIn[i];
}
}
void ShaderManager::notifyDestroy() {
for (int i = 0; i < ARRAYSIZE(_builtIn); ++i) {
_builtIn[i]->destroy();
}
}
void ShaderManager::notifyCreate() {
if (_initializeShaders) {
_initializeShaders = false;
_builtIn[kDefault] = new Shader(g_defaultVertexShader, g_defaultFragmentShader);
_builtIn[kCLUT8LookUp] = new Shader(g_defaultVertexShader, g_lookUpFragmentShader);
_builtIn[kCLUT8LookUp]->setUniform1I("palette", 1);
for (uint i = 0; i < kMaxUsages; ++i) {
_builtIn[i]->setUniform1I("shaderTexture", 0);
}
} else {
for (int i = 0; i < ARRAYSIZE(_builtIn); ++i) {
_builtIn[i]->recreate();
}
}
}
Shader *ShaderManager::query(ShaderUsage shader) const {
if (shader == kMaxUsages) {
warning("OpenGL: ShaderManager::query used with kMaxUsages");
return nullptr;
}
return _builtIn[shader];
}
} // End of namespace OpenGL
#endif // !USE_FORCED_GLES
|