| 1 |
/** |
| 2 |
* Graphics primitives with OpenGL (ES) 2 |
| 3 |
|
| 4 |
* Copyright (C) 2015 Sylvain Beucler |
| 5 |
|
| 6 |
* This file is part of GNU FreeDink |
| 7 |
|
| 8 |
* GNU FreeDink is free software: you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Affero General Public License |
| 10 |
* as published by the Free Software Foundation, either version 3 of |
| 11 |
* the License, or (at your option) any later version. |
| 12 |
|
| 13 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Affero General Public License for more details. |
| 17 |
|
| 18 |
* You should have received a copy of the GNU Affero General Public |
| 19 |
* License along with GNU FreeDink. If not, see |
| 20 |
* <http://www.gnu.org/licenses/>. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "IOGfxDisplayGL2.h" |
| 28 |
|
| 29 |
#include <glm/glm.hpp> |
| 30 |
#include <glm/gtc/matrix_transform.hpp> |
| 31 |
#include <glm/gtc/type_ptr.hpp> |
| 32 |
#include <string.h> |
| 33 |
|
| 34 |
#include "log.h" |
| 35 |
#include "IOGfxGLFuncs.h" |
| 36 |
#include "IOGfxSurfaceGL2.h" |
| 37 |
#include "gfx_palette.h" |
| 38 |
#include "ImageLoader.h" |
| 39 |
|
| 40 |
|
| 41 |
IOGfxDisplayGL2::IOGfxDisplayGL2(int w, int h, bool truecolor, Uint32 flags) |
| 42 |
: IOGfxDisplay(w, h, truecolor, flags | SDL_WINDOW_OPENGL), |
| 43 |
glcontext(NULL), gl(NULL), screen(NULL) { |
| 44 |
palette = -1; |
| 45 |
vboSpriteVertices = -1, vboSpriteTexcoords = -1, vboCroppedSpriteTexcoords = -1; |
| 46 |
blit = NULL; fliprgb = NULL; flippal = NULL; fillRect = NULL; |
| 47 |
} |
| 48 |
|
| 49 |
IOGfxDisplayGL2::~IOGfxDisplayGL2() { |
| 50 |
close(); |
| 51 |
} |
| 52 |
|
| 53 |
bool IOGfxDisplayGL2::open() { |
| 54 |
if (!IOGfxDisplay::open()) return false; |
| 55 |
|
| 56 |
if (!createOpenGLContext()) return false; |
| 57 |
if (!createSpriteVertices()) return false; |
| 58 |
if (!createSpriteTexcoords()) return false; |
| 59 |
if (!createCroppedSpriteTexcoords()) return false; |
| 60 |
if (!createPrograms()) return false; |
| 61 |
if (!createPalette()) return false; |
| 62 |
|
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
void IOGfxDisplayGL2::close() { |
| 67 |
if (gl != NULL) { |
| 68 |
gl->DeleteProgram(blit->program); |
| 69 |
gl->DeleteProgram(fillRect->program); |
| 70 |
gl->DeleteProgram(flippal->program); |
| 71 |
delete blit; |
| 72 |
delete fillRect; |
| 73 |
delete flippal; |
| 74 |
gl->DeleteBuffers(1, &vboSpriteVertices); |
| 75 |
gl->DeleteBuffers(1, &vboSpriteTexcoords); |
| 76 |
gl->DeleteBuffers(1, &vboCroppedSpriteTexcoords); |
| 77 |
gl->Clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); |
| 78 |
delete gl; |
| 79 |
gl = NULL; |
| 80 |
} |
| 81 |
|
| 82 |
if (glcontext != NULL) SDL_GL_DeleteContext(glcontext); |
| 83 |
glcontext = NULL; |
| 84 |
|
| 85 |
IOGfxDisplay::close(); |
| 86 |
} |
| 87 |
|
| 88 |
bool IOGfxDisplayGL2::createOpenGLContext() { |
| 89 |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); |
| 90 |
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
| 91 |
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); |
| 92 |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 93 |
glcontext = SDL_GL_CreateContext(window); |
| 94 |
if (glcontext == NULL) { |
| 95 |
log_error("Could not create OpenGL context: %s", SDL_GetError()); |
| 96 |
return false; |
| 97 |
} |
| 98 |
if (SDL_GL_MakeCurrent(window, glcontext) < 0) { |
| 99 |
SDL_GL_DeleteContext(glcontext); |
| 100 |
log_error("Could not make OpenGL context current: %s", SDL_GetError()); |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
// Let FramerateManager handle frame delay |
| 105 |
// TODO: drop the extra SwapWindow/RenderPresent during speed mode |
| 106 |
// TODO: ideally we should set to 1 to get VSync and avoid tearing, if achievable |
| 107 |
// Commented out for emscripten to avoid "emscripten_set_main_loop_timing: Cannot set timing mode for main loop since a main loop does not exist! Call emscripten_set_main_loop first to set one up." warning |
| 108 |
#ifndef __EMSCRIPTEN__ |
| 109 |
SDL_GL_SetSwapInterval(0); |
| 110 |
#endif |
| 111 |
|
| 112 |
gl = new IOGfxGLFuncs(); |
| 113 |
|
| 114 |
gl->Enable(GL_BLEND); |
| 115 |
//gl->Enable(GL_DEPTH_TEST); |
| 116 |
gl->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 117 |
|
| 118 |
// buffer for glReadPixels, defaults to GL_BACK in double buffered mode |
| 119 |
// This functions is not present in GLES2 |
| 120 |
//gl->ReadBuffer(GL_FRONT); |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
bool IOGfxDisplayGL2::createSpriteVertices() { |
| 126 |
GLfloat spriteVertices[] = { |
| 127 |
0, 0, 0, 1, |
| 128 |
1, 0, 0, 1, |
| 129 |
0, 1, 0, 1, |
| 130 |
1, 1, 0, 1, |
| 131 |
}; |
| 132 |
|
| 133 |
gl->GenBuffers(1, &vboSpriteVertices); |
| 134 |
gl->BindBuffer(GL_ARRAY_BUFFER, vboSpriteVertices); |
| 135 |
gl->BufferData(GL_ARRAY_BUFFER, sizeof(spriteVertices), spriteVertices, GL_STATIC_DRAW); |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
bool IOGfxDisplayGL2::createSpriteTexcoords() { |
| 140 |
GLfloat spriteTexcoords[] = { |
| 141 |
0.0, 0.0, |
| 142 |
1.0, 0.0, |
| 143 |
0.0, 1.0, |
| 144 |
1.0, 1.0, |
| 145 |
}; |
| 146 |
gl->GenBuffers(1, &vboSpriteTexcoords); |
| 147 |
gl->BindBuffer(GL_ARRAY_BUFFER, vboSpriteTexcoords); |
| 148 |
gl->BufferData(GL_ARRAY_BUFFER, sizeof(spriteTexcoords), spriteTexcoords, GL_STATIC_DRAW); |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
bool IOGfxDisplayGL2::createCroppedSpriteTexcoords() { |
| 153 |
gl->GenBuffers(1, &vboCroppedSpriteTexcoords); |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Compile vertex or fragment shader. |
| 159 |
* 'name' is provided for debugging purposes only |
| 160 |
*/ |
| 161 |
GLuint IOGfxDisplayGL2::createShader(const char* name, const char* source, GLenum type) { |
| 162 |
GLuint res = gl->CreateShader(type); |
| 163 |
|
| 164 |
// GLSL version |
| 165 |
std::string version; |
| 166 |
int profile; |
| 167 |
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile); |
| 168 |
if (profile == SDL_GL_CONTEXT_PROFILE_ES) |
| 169 |
version = "#version 100\n"; // OpenGL ES 2.0 |
| 170 |
else |
| 171 |
version = "#version 120\n"; // OpenGL 2.1 |
| 172 |
|
| 173 |
// GLES2 precision specifiers |
| 174 |
std::string precision = |
| 175 |
"#ifdef GL_ES \n" |
| 176 |
"# ifdef GL_FRAGMENT_PRECISION_HIGH \n" |
| 177 |
" precision highp float; \n" |
| 178 |
"# else \n" |
| 179 |
" precision mediump float; \n" |
| 180 |
"# endif \n" |
| 181 |
"#else \n" |
| 182 |
// Ignore unsupported precision specifiers |
| 183 |
"# define lowp \n" |
| 184 |
"# define mediump \n" |
| 185 |
"# define highp \n" |
| 186 |
"#endif \n"; |
| 187 |
|
| 188 |
// Concat instead of giving glShaderSource multiple strings. |
| 189 |
// At least one setup complained that "#version 120" was an incomplete shader |
| 190 |
// (VirtualBox 5.1 + Windows 7 guest) - so not taking chances. |
| 191 |
std::string fullsource = version + precision + source; |
| 192 |
const GLchar* sources[] = { |
| 193 |
fullsource.c_str() |
| 194 |
}; |
| 195 |
gl->ShaderSource(res, 1, sources, NULL); |
| 196 |
|
| 197 |
gl->CompileShader(res); |
| 198 |
GLint compile_ok = GL_FALSE; |
| 199 |
gl->GetShaderiv(res, GL_COMPILE_STATUS, &compile_ok); |
| 200 |
if (compile_ok == GL_FALSE) { |
| 201 |
log_error("when compiling %s %s shader:", name, (type == GL_VERTEX_SHADER) ? "vertex" : "fragment"); |
| 202 |
infoLog(res); |
| 203 |
gl->DeleteShader(res); |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
return res; |
| 208 |
} |
| 209 |
|
| 210 |
void IOGfxDisplayGL2::infoLog(GLuint object) { |
| 211 |
GLint log_length = 0; |
| 212 |
if (gl->IsShader(object)) { |
| 213 |
gl->GetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length); |
| 214 |
} else if (gl->IsProgram(object)) { |
| 215 |
gl->GetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length); |
| 216 |
} else { |
| 217 |
log_error("logShaderInfoLog: Not a shader or a program"); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
char* log = (char*)malloc(log_length); |
| 222 |
|
| 223 |
if (gl->IsShader(object)) |
| 224 |
gl->GetShaderInfoLog(object, log_length, NULL, log); |
| 225 |
else if (gl->IsProgram(object)) |
| 226 |
gl->GetProgramInfoLog(object, log_length, NULL, log); |
| 227 |
|
| 228 |
log_error("%s\n", log); |
| 229 |
free(log); |
| 230 |
} |
| 231 |
|
| 232 |
bool IOGfxDisplayGL2::createPrograms() { |
| 233 |
blit = new IOGfxGLProg(); |
| 234 |
blit->vshader = |
| 235 |
"attribute vec4 v_coord; \n" |
| 236 |
"attribute vec2 v_texcoord; \n" |
| 237 |
"varying vec2 f_texcoord; \n" |
| 238 |
"uniform mat4 mvp; \n" |
| 239 |
" \n" |
| 240 |
"void main(void) { \n" |
| 241 |
" gl_Position = mvp * v_coord; \n" |
| 242 |
" f_texcoord = v_texcoord; \n" |
| 243 |
"} \n"; |
| 244 |
blit->fshader = |
| 245 |
"varying vec2 f_texcoord; \n" |
| 246 |
"uniform sampler2D texture; \n" |
| 247 |
"uniform vec3 colorkey; \n" |
| 248 |
" \n" |
| 249 |
"void main(void) { \n" |
| 250 |
" vec4 f = texture2D(texture, f_texcoord); \n" |
| 251 |
" // don't need float comparison abs(a-b)<.001? \n" |
| 252 |
" if (f.r == colorkey.r \n" |
| 253 |
" && f.g == colorkey.g \n" |
| 254 |
" && f.b == colorkey.b) \n" |
| 255 |
" f.a = 0.0; \n" |
| 256 |
" gl_FragColor = f; \n" |
| 257 |
"} \n"; |
| 258 |
blit->program = createProgram("blit", blit->vshader, blit->fshader); |
| 259 |
if ((blit->attributes["v_coord"] = getAttribLocation(blit->program, "v_coord")) == -1) return false; |
| 260 |
if ((blit->attributes["v_texcoord"] = getAttribLocation(blit->program, "v_texcoord")) == -1) return false; |
| 261 |
if ((blit->uniforms["mvp"] = getUniformLocation(blit->program, "mvp")) == -1) return false; |
| 262 |
if ((blit->uniforms["texture"] = getUniformLocation(blit->program, "texture")) == -1) return false; |
| 263 |
if ((blit->uniforms["colorkey"] = getUniformLocation(blit->program, "colorkey")) == -1) return false; |
| 264 |
|
| 265 |
|
| 266 |
fillRect = new IOGfxGLProg(); |
| 267 |
fillRect->vshader = |
| 268 |
"attribute vec4 v_coord; \n" |
| 269 |
"uniform mat4 mvp; \n" |
| 270 |
" \n" |
| 271 |
"void main(void) { \n" |
| 272 |
" gl_Position = mvp * v_coord; \n" |
| 273 |
"} \n"; |
| 274 |
fillRect->fshader = |
| 275 |
"uniform vec4 color; \n" |
| 276 |
" \n" |
| 277 |
"void main(void) { \n" |
| 278 |
" gl_FragColor = color; \n" |
| 279 |
"} \n"; |
| 280 |
fillRect->program = createProgram("fillRect", fillRect->vshader, fillRect->fshader); |
| 281 |
if ((fillRect->attributes["v_coord"] = getAttribLocation(fillRect->program, "v_coord")) == -1) return false; |
| 282 |
if ((fillRect->uniforms["mvp"] = getUniformLocation(fillRect->program, "mvp")) == -1) return false; |
| 283 |
if ((fillRect->uniforms["color"] = getUniformLocation(fillRect->program, "color")) == -1) return false; |
| 284 |
|
| 285 |
|
| 286 |
fliprgb = new IOGfxGLProg(); |
| 287 |
fliprgb->vshader = |
| 288 |
"attribute vec4 v_coord; \n" |
| 289 |
"attribute vec2 v_texcoord; \n" |
| 290 |
"varying vec2 f_texcoord; \n" |
| 291 |
"uniform mat4 mvp; \n" |
| 292 |
" \n" |
| 293 |
"void main(void) { \n" |
| 294 |
" gl_Position = mvp * v_coord; \n" |
| 295 |
" f_texcoord = v_texcoord; \n" |
| 296 |
"} \n"; |
| 297 |
fliprgb->fshader = |
| 298 |
"varying vec2 f_texcoord; \n" |
| 299 |
"uniform sampler2D texture; \n" |
| 300 |
"uniform float brightness; \n" |
| 301 |
" \n" |
| 302 |
"void main(void) { \n" |
| 303 |
" vec4 f = texture2D(texture, f_texcoord); \n" |
| 304 |
" if (f == vec4(1.0,1.0,1.0, 1.0)) \n" |
| 305 |
" gl_FragColor = f; \n" |
| 306 |
" else \n" |
| 307 |
" gl_FragColor = vec4(f.rgb * brightness, 1.0);\n" |
| 308 |
"} \n"; |
| 309 |
fliprgb->program = createProgram("pflip", fliprgb->vshader, fliprgb->fshader); |
| 310 |
if ((fliprgb->attributes["v_coord"] = getAttribLocation(fliprgb->program, "v_coord")) == -1) return false; |
| 311 |
if ((fliprgb->attributes["v_texcoord"] = getAttribLocation(fliprgb->program, "v_texcoord")) == -1) return false; |
| 312 |
if ((fliprgb->uniforms["mvp"] = getUniformLocation(fliprgb->program, "mvp")) == -1) return false; |
| 313 |
if ((fliprgb->uniforms["texture"] = getUniformLocation(fliprgb->program, "texture")) == -1) return false; |
| 314 |
if ((fliprgb->uniforms["brightness"] = getUniformLocation(fliprgb->program, "brightness")) == -1) return false; |
| 315 |
|
| 316 |
|
| 317 |
/* Palette emulation */ |
| 318 |
flippal = new IOGfxGLProg(); |
| 319 |
flippal->vshader = |
| 320 |
"attribute vec4 v_coord; \n" |
| 321 |
"attribute vec2 v_texcoord; \n" |
| 322 |
"varying vec2 f_texcoord; \n" |
| 323 |
"uniform mat4 mvp; \n" |
| 324 |
" \n" |
| 325 |
"void main(void) { \n" |
| 326 |
" gl_Position = mvp * v_coord; \n" |
| 327 |
" f_texcoord = v_texcoord; \n" |
| 328 |
"} \n"; |
| 329 |
flippal->fshader = |
| 330 |
"varying vec2 f_texcoord; \n" |
| 331 |
"uniform sampler2D texture; \n" |
| 332 |
"uniform sampler2D palette; \n" |
| 333 |
" \n" |
| 334 |
"void main(void) { \n" |
| 335 |
" vec4 index = texture2D(texture, f_texcoord); \n" |
| 336 |
" gl_FragColor = texture2D(palette, vec2(index.r, 0.5)); \n" |
| 337 |
"} \n"; |
| 338 |
flippal->program = createProgram("i2rgb", flippal->vshader, flippal->fshader); |
| 339 |
if ((flippal->attributes["v_coord"] = getAttribLocation(flippal->program, "v_coord")) == -1) return false; |
| 340 |
if ((flippal->attributes["v_texcoord"] = getAttribLocation(flippal->program, "v_texcoord")) == -1) return false; |
| 341 |
if ((flippal->uniforms["mvp"] = getUniformLocation(flippal->program, "mvp")) == -1) return false; |
| 342 |
if ((flippal->uniforms["texture"] = getUniformLocation(flippal->program, "texture")) == -1) return false; |
| 343 |
if ((flippal->uniforms["palette"] = getUniformLocation(flippal->program, "palette")) == -1) return false; |
| 344 |
|
| 345 |
|
| 346 |
if (blit->program == 0 || fliprgb->program == 0 || fillRect->program == 0 || flippal->program == 0) |
| 347 |
return false; |
| 348 |
return true; |
| 349 |
} |
| 350 |
|
| 351 |
GLuint IOGfxDisplayGL2::createProgram(const char* name, const char* vertexShaderSource, const char* fragmentShaderSource) { |
| 352 |
GLuint vs, fs; |
| 353 |
if ((vs = createShader(name, vertexShaderSource, GL_VERTEX_SHADER)) == 0) return false; |
| 354 |
if ((fs = createShader(name, fragmentShaderSource, GL_FRAGMENT_SHADER)) == 0) return false; |
| 355 |
|
| 356 |
GLuint program = gl->CreateProgram(); |
| 357 |
if (program == 0) { |
| 358 |
log_error("Could not allocate program"); |
| 359 |
return 0; |
| 360 |
} |
| 361 |
gl->AttachShader(program, vs); |
| 362 |
gl->AttachShader(program, fs); |
| 363 |
|
| 364 |
GLint link_ok = GL_FALSE; |
| 365 |
gl->LinkProgram(program); |
| 366 |
gl->GetProgramiv(program, GL_LINK_STATUS, &link_ok); |
| 367 |
if (!link_ok) { |
| 368 |
infoLog(program); |
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
return program; |
| 373 |
} |
| 374 |
|
| 375 |
GLint IOGfxDisplayGL2::getAttribLocation(GLuint program, const char* name) { |
| 376 |
GLint attribute = gl->GetAttribLocation(program, name); |
| 377 |
if (attribute == -1) |
| 378 |
log_error("Could not locate attribute %s", name); |
| 379 |
return attribute; |
| 380 |
} |
| 381 |
GLint IOGfxDisplayGL2::getUniformLocation(GLuint program, const char* name) { |
| 382 |
GLint uniform = gl->GetUniformLocation(program, name); |
| 383 |
if (uniform == -1) |
| 384 |
log_error("Could not locate uniform %s", name); |
| 385 |
return uniform; |
| 386 |
} |
| 387 |
|
| 388 |
bool IOGfxDisplayGL2::createPalette() { |
| 389 |
gl->GenTextures(1, &palette); |
| 390 |
gl->BindTexture(GL_TEXTURE_2D, palette); |
| 391 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 392 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 393 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 394 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 395 |
|
| 396 |
updatePalette(); |
| 397 |
return true; // I hereby thank OpenGL error reporting.. |
| 398 |
} |
| 399 |
|
| 400 |
void IOGfxDisplayGL2::updatePalette() { |
| 401 |
SDL_Color pal_phys[256]; |
| 402 |
gfx_palette_get_phys(pal_phys); |
| 403 |
gl->BindTexture(GL_TEXTURE_2D, palette); |
| 404 |
gl->TexImage2D(GL_TEXTURE_2D, // target |
| 405 |
0, // level, 0 = base, no minimap, |
| 406 |
GL_RGBA, // internalformat |
| 407 |
256, // width |
| 408 |
1, // height |
| 409 |
0, // border, always 0 in OpenGL ES |
| 410 |
GL_RGBA, // format |
| 411 |
GL_UNSIGNED_BYTE, // type |
| 412 |
pal_phys); |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
void IOGfxDisplayGL2::logOpenGLInfo() { |
| 417 |
int major, minor, profile; |
| 418 |
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major); |
| 419 |
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor); |
| 420 |
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile); |
| 421 |
const char* profile_str = ""; |
| 422 |
if (profile & SDL_GL_CONTEXT_PROFILE_CORE) |
| 423 |
profile_str = "CORE"; |
| 424 |
if (profile & SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) |
| 425 |
profile_str = "COMPATIBILITY"; |
| 426 |
if (profile & SDL_GL_CONTEXT_PROFILE_ES) |
| 427 |
profile_str = "ES"; |
| 428 |
log_info("OpenGL %d.%d %s", major, minor, profile_str); |
| 429 |
|
| 430 |
int doublebuffer; |
| 431 |
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuffer); |
| 432 |
log_info("Double buffered: %d", doublebuffer); |
| 433 |
#ifndef __EMSCRIPTEN__ |
| 434 |
// Error: WebGL warning: getParameter: parameter: invalid enum value READ_BUFFER |
| 435 |
int read_buffer; |
| 436 |
gl->GetIntegerv(GL_READ_BUFFER, &read_buffer); |
| 437 |
if (read_buffer == GL_FRONT) |
| 438 |
log_info("Read buffer: GL_FRONT"); |
| 439 |
else if (read_buffer == GL_BACK) |
| 440 |
log_info("Read buffer: GL_BACK"); |
| 441 |
else |
| 442 |
log_info("Read buffer: 0x%04X", read_buffer); |
| 443 |
#endif |
| 444 |
} |
| 445 |
|
| 446 |
void IOGfxDisplayGL2::logDisplayInfo() { |
| 447 |
log_info("FreeDink graphics mode: IOGfxDisplayGL2"); |
| 448 |
IOGfxDisplay::logDisplayInfo(); |
| 449 |
logOpenGLInfo(); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
void IOGfxDisplayGL2::clear() { |
| 454 |
gl->BindFramebuffer(GL_FRAMEBUFFER, 0); |
| 455 |
gl->ClearColor(0,0,0,1); |
| 456 |
gl->Clear(GL_COLOR_BUFFER_BIT); |
| 457 |
} |
| 458 |
|
| 459 |
SDL_Surface* IOGfxDisplayGL2::screenshot(SDL_Rect* rect) { |
| 460 |
gl->BindFramebuffer(GL_FRAMEBUFFER, 0); |
| 461 |
|
| 462 |
// assume 4-bytes alignment |
| 463 |
SDL_Surface* image = SDL_CreateRGBSurface(0, |
| 464 |
rect->w, rect->h, 32, |
| 465 |
#if SDL_BYTEORDER == SDL_BIG_ENDIAN |
| 466 |
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff |
| 467 |
#else |
| 468 |
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 |
| 469 |
#endif |
| 470 |
); |
| 471 |
unsigned char* pixels = (unsigned char*)image->pixels; |
| 472 |
gl->ReadPixels(rect->x, h-rect->h-rect->y, rect->w, rect->h, |
| 473 |
GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 474 |
|
| 475 |
// Flip vertically |
| 476 |
unsigned int length = rect->w*4; |
| 477 |
unsigned char* src = pixels + (rect->h-1)*length; |
| 478 |
unsigned char* tmp = new unsigned char[length]; |
| 479 |
unsigned char* dst = pixels; |
| 480 |
unsigned int rows = rect->h / 2; |
| 481 |
while (rows--) { |
| 482 |
memcpy(tmp, dst, length); |
| 483 |
memcpy(dst, src, length); |
| 484 |
memcpy(src, tmp, length); |
| 485 |
dst += length; |
| 486 |
src -= length; |
| 487 |
} |
| 488 |
delete[] tmp; |
| 489 |
|
| 490 |
return image; |
| 491 |
} |
| 492 |
|
| 493 |
void IOGfxDisplayGL2::setVertexAttrib(IOGfxGLProg* prog, GLuint attribLocation, GLuint vbo, GLint size) { |
| 494 |
gl->EnableVertexAttribArray(attribLocation); |
| 495 |
// Describe our vertices array to OpenGL (it can't guess its format automatically) |
| 496 |
gl->BindBuffer(GL_ARRAY_BUFFER, vbo); |
| 497 |
gl->VertexAttribPointer(attribLocation, // attribute |
| 498 |
size, // number of elements per vertex, e.g. (x,y,z,t) |
| 499 |
GL_FLOAT, // the type of each element |
| 500 |
GL_FALSE, // take our values as-is |
| 501 |
0, // no extra data between each position |
| 502 |
0 // offset of first element |
| 503 |
); |
| 504 |
} |
| 505 |
|
| 506 |
void IOGfxDisplayGL2::flip(IOGfxSurface* backbuffer, SDL_Rect* dstrect, |
| 507 |
bool interpolation, bool hwflip) { |
| 508 |
if (backbuffer == NULL) |
| 509 |
SDL_SetError("IOGfxDisplayGL2::flip: passed a NULL surface"); |
| 510 |
IOGfxSurfaceGL2* surf = dynamic_cast<IOGfxSurfaceGL2*>(backbuffer); |
| 511 |
GLuint texture = surf->texture; |
| 512 |
|
| 513 |
IOGfxGLProg* prog = truecolor ? fliprgb : flippal; |
| 514 |
gl->UseProgram(prog->program); |
| 515 |
setVertexAttrib(prog, prog->attributes["v_coord"], vboSpriteVertices, 4); |
| 516 |
setVertexAttrib(prog, prog->attributes["v_texcoord"], vboSpriteTexcoords, 2); |
| 517 |
|
| 518 |
gl->ActiveTexture(GL_TEXTURE0); |
| 519 |
gl->Uniform1i(prog->uniforms["texture"], /*GL_TEXTURE*/0); |
| 520 |
gl->BindTexture(GL_TEXTURE_2D, texture); |
| 521 |
// GL_LINEAR's slight blur is closer to 90's CRT monitors |
| 522 |
// GL_NEAREST is clean-pixellated and harder on the eyes |
| 523 |
if (truecolor && interpolation) { |
| 524 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 525 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 526 |
} else { |
| 527 |
// but GL_LINEAR messes palette emulation |
| 528 |
// TODO: implement 2-step flip: 1) i2rgb 2) interpolation |
| 529 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 530 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 531 |
} |
| 532 |
if (!truecolor) { |
| 533 |
gl->ActiveTexture(GL_TEXTURE1); |
| 534 |
gl->Uniform1i(prog->uniforms["palette"], /*GL_TEXTURE*/1); |
| 535 |
gl->BindTexture(GL_TEXTURE_2D, palette); |
| 536 |
updatePalette(); |
| 537 |
} |
| 538 |
|
| 539 |
if (truecolor) |
| 540 |
gl->Uniform1f(prog->uniforms["brightness"], brightness/256.0); |
| 541 |
|
| 542 |
// Y-inversed projection for top-left origin and top-bottom textures |
| 543 |
// Beware that rotation is reversed too |
| 544 |
glm::mat4 projection = glm::ortho(0.0f, 1.0f*w, 1.0f*h, 0.0f); |
| 545 |
glm::mat4 m_transform = glm::translate(glm::mat4(1.0f), glm::vec3(dstrect->x,dstrect->y, 0)) |
| 546 |
* glm::scale(glm::mat4(1.0f), glm::vec3(dstrect->w, dstrect->h, 0)); |
| 547 |
glm::mat4 mvp = projection * m_transform; // * view * model * anim; |
| 548 |
gl->UniformMatrix4fv(prog->uniforms["mvp"], 1, GL_FALSE, glm::value_ptr(mvp)); |
| 549 |
|
| 550 |
/* Push each element in buffer_vertices to the vertex shader */ |
| 551 |
gl->BindFramebuffer(GL_FRAMEBUFFER, 0); |
| 552 |
gl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 553 |
|
| 554 |
// gl->DisableVertexAttribArray(prog->attributes["v_coord"]); |
| 555 |
// gl->DisableVertexAttribArray(blit->attributes["v_texcoord"]); |
| 556 |
|
| 557 |
if (hwflip) |
| 558 |
SDL_GL_SwapWindow(window); |
| 559 |
} |
| 560 |
|
| 561 |
|
| 562 |
void IOGfxDisplayGL2::onSizeChange(int w, int h) { |
| 563 |
this->w = w; |
| 564 |
this->h = h; |
| 565 |
gl->Viewport(0, 0, w, h); |
| 566 |
} |
| 567 |
|
| 568 |
IOGfxSurface* IOGfxDisplayGL2::upload(SDL_Surface* surf) { |
| 569 |
GLuint texture = -1; |
| 570 |
gl->GenTextures(1, &texture); |
| 571 |
gl->BindTexture(GL_TEXTURE_2D, texture); |
| 572 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 573 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 574 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 575 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 576 |
log_trace("surf->w=%d", surf->w); |
| 577 |
log_trace("surf->h=%d", surf->h); |
| 578 |
log_trace("surf->format->bits=%d", surf->format->BitsPerPixel); |
| 579 |
|
| 580 |
// Save transparency color |
| 581 |
SDL_Color colorkey = {0,0,0, 0}; |
| 582 |
Uint32 key; |
| 583 |
if (SDL_GetColorKey(surf, &key) == -1) { |
| 584 |
colorkey.a = SDL_ALPHA_OPAQUE; // no colorkey |
| 585 |
} else { |
| 586 |
if (truecolor) |
| 587 |
SDL_GetRGBA(key, surf->format, &colorkey.r, &colorkey.g, &colorkey.b, &colorkey.a); |
| 588 |
else |
| 589 |
colorkey.r = colorkey.g = colorkey.b = key; |
| 590 |
colorkey.a = SDL_ALPHA_TRANSPARENT; |
| 591 |
} |
| 592 |
|
| 593 |
if (truecolor) { |
| 594 |
// Dink images get alpha disabled, so use 24-bit for simplicity |
| 595 |
if (surf->format->BitsPerPixel != 24 || surf->format->format != SDL_PIXELFORMAT_RGB888) { |
| 596 |
// don't black out transparent pixels, keep original color |
| 597 |
SDL_SetColorKey(surf, SDL_FALSE, 0); |
| 598 |
SDL_PixelFormat fmt; |
| 599 |
fmt.BitsPerPixel = 24; |
| 600 |
#if SDL_BYTEORDER == SDL_BIG_ENDIAN |
| 601 |
fmt.Rmask = 0xFF000000; |
| 602 |
fmt.Gmask = 0x00FF0000; |
| 603 |
fmt.Bmask = 0x0000FF00; |
| 604 |
fmt.Amask = 0x00000000; |
| 605 |
#else |
| 606 |
fmt.Rmask = 0x000000FF; |
| 607 |
fmt.Gmask = 0x0000FF00; |
| 608 |
fmt.Bmask = 0x00FF0000; |
| 609 |
fmt.Amask = 0x00000000; |
| 610 |
#endif |
| 611 |
SDL_Surface* surf24 = SDL_CreateRGBSurface(0, surf->w, surf->h, fmt.BitsPerPixel, |
| 612 |
fmt.Rmask, fmt.Gmask, fmt.Bmask, fmt.Amask); |
| 613 |
SDL_BlitSurface(surf, NULL, surf24, NULL); |
| 614 |
|
| 615 |
SDL_FreeSurface(surf); |
| 616 |
surf = surf24; |
| 617 |
} |
| 618 |
gl->TexImage2D(GL_TEXTURE_2D, // target |
| 619 |
0, // level, 0 = base, no minimap, |
| 620 |
GL_RGB, // internalformat |
| 621 |
surf->w, // width |
| 622 |
surf->h, // height |
| 623 |
0, // border, always 0 in OpenGL ES |
| 624 |
GL_RGB, // format |
| 625 |
GL_UNSIGNED_BYTE, // type |
| 626 |
surf->pixels); |
| 627 |
} else { |
| 628 |
if (surf->format->BitsPerPixel != 8) { |
| 629 |
log_error("IOGfxDisplayGL2::upload: format is not indexed"); |
| 630 |
SDL_FreeSurface(surf); |
| 631 |
return NULL; |
| 632 |
} |
| 633 |
gl->TexImage2D(GL_TEXTURE_2D, // target |
| 634 |
0, // level, 0 = base, no minimap, |
| 635 |
GL_LUMINANCE, // internalformat |
| 636 |
surf->w, // width |
| 637 |
surf->h, // height |
| 638 |
0, // border, always 0 in OpenGL ES |
| 639 |
GL_LUMINANCE, // format |
| 640 |
GL_UNSIGNED_BYTE, // type |
| 641 |
surf->pixels); |
| 642 |
} |
| 643 |
|
| 644 |
int w = surf->w; |
| 645 |
int h = surf->h; |
| 646 |
|
| 647 |
SDL_FreeSurface(surf); |
| 648 |
return new IOGfxSurfaceGL2(this, texture, w, h, colorkey); |
| 649 |
} |
| 650 |
|
| 651 |
IOGfxSurface* IOGfxDisplayGL2::allocBuffer(int surfW, int surfH) { |
| 652 |
GLuint texture = -1; |
| 653 |
gl->GenTextures(1, &texture); |
| 654 |
gl->BindTexture(GL_TEXTURE_2D, texture); |
| 655 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 656 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 657 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 658 |
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 659 |
|
| 660 |
// No color key |
| 661 |
SDL_Color colorkey = {0,0,0, SDL_ALPHA_OPAQUE}; |
| 662 |
|
| 663 |
// Blank texture |
| 664 |
// RGB even in indexed mode because some setup (Android) don't allow GL_LUMINANCE as framebuffer |
| 665 |
unsigned char* pixels = new unsigned char[(surfW+(4-surfW%4))*4 * surfH]; |
| 666 |
gl->TexImage2D(GL_TEXTURE_2D, // target |
| 667 |
0, // level, 0 = base, no minimap, |
| 668 |
GL_RGB, // internalformat |
| 669 |
surfW, // width |
| 670 |
surfH, // height |
| 671 |
0, // border, always 0 in OpenGL ES |
| 672 |
GL_RGB, // format |
| 673 |
GL_UNSIGNED_BYTE, // type |
| 674 |
pixels); |
| 675 |
delete[] pixels; |
| 676 |
|
| 677 |
return new IOGfxSurfaceGL2(this, texture, surfW, surfH, colorkey); |
| 678 |
} |