OpenTTD Source: src/video/opengl.cpp Source File

OpenTTD Source 12.0-beta1

opengl.cpp
1 /* $Id$ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD 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, version 2.
6 * OpenTTD 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.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "../stdafx.h"
13
14 /* Define to disable buffer syncing. Will increase max fast forward FPS but produces artifacts. Mainly useful for performance testing. */
15 // #define NO_GL_BUFFER_SYNC
16 /* Define to allow software rendering backends. */
17 // #define GL_ALLOW_SOFTWARE_RENDERER
18
19 #if defined(_WIN32)
20 # include <windows.h>
21 #endif
22
23 #define GL_GLEXT_PROTOTYPES
24 #if defined(__APPLE__)
25 # define GL_SILENCE_DEPRECATION
26 # include <OpenGL/gl3.h>
27 #else
28 # include <GL/gl.h>
29 #endif
30 #include "../3rdparty/opengl/glext.h"
31
32 #include "opengl.h"
33 #include "../core/geometry_func.hpp"
34 #include "../core/mem_func.hpp"
35 #include "../core/math_func.hpp"
36 #include "../core/mem_func.hpp"
37 #include "../gfx_func.h"
38 #include "../debug.h"
39 #include "../blitter/factory.hpp"
40 #include "../zoom_func.h"
41 #include <array>
42 #include <numeric>
43
44 #include "../table/opengl_shader.h"
45 #include "../table/sprites.h"
46
47
48 #include "../safeguards.h"
49
50
51 /* Define function pointers of all OpenGL functions that we load dynamically. */
52
53 #define GL(function) static decltype(&function) _ ## function
54
55 GL(glGetString);
56 GL(glGetIntegerv);
57 GL(glGetError);
58 GL(glDebugMessageControl);
59 GL(glDebugMessageCallback);
60
61 GL(glDisable);
62 GL(glEnable);
63 GL(glViewport);
64 GL(glClear);
65 GL(glClearColor);
66 GL(glBlendFunc);
67 GL(glDrawArrays);
68
69 GL(glTexImage1D);
70 GL(glTexImage2D);
71 GL(glTexParameteri);
72 GL(glTexSubImage1D);
73 GL(glTexSubImage2D);
74 GL(glBindTexture);
75 GL(glDeleteTextures);
76 GL(glGenTextures);
77 GL(glPixelStorei);
78
79 GL(glActiveTexture);
80
81 GL(glGenBuffers);
82 GL(glDeleteBuffers);
83 GL(glBindBuffer);
84 GL(glBufferData);
85 GL(glBufferSubData);
86 GL(glMapBuffer);
87 GL(glUnmapBuffer);
88 GL(glClearBufferSubData);
89
90 GL(glBufferStorage);
91 GL(glMapBufferRange);
92 GL(glClientWaitSync);
93 GL(glFenceSync);
94 GL(glDeleteSync);
95
96 GL(glGenVertexArrays);
97 GL(glDeleteVertexArrays);
98 GL(glBindVertexArray);
99
100 GL(glCreateProgram);
101 GL(glDeleteProgram);
102 GL(glLinkProgram);
103 GL(glUseProgram);
104 GL(glGetProgramiv);
105 GL(glGetProgramInfoLog);
106 GL(glCreateShader);
107 GL(glDeleteShader);
108 GL(glShaderSource);
109 GL(glCompileShader);
110 GL(glAttachShader);
111 GL(glGetShaderiv);
112 GL(glGetShaderInfoLog);
113 GL(glGetUniformLocation);
114 GL(glUniform1i);
115 GL(glUniform1f);
116 GL(glUniform2f);
117 GL(glUniform4f);
118
119 GL(glGetAttribLocation);
120 GL(glEnableVertexAttribArray);
121 GL(glDisableVertexAttribArray);
122 GL(glVertexAttribPointer);
123 GL(glBindFragDataLocation);
124
125 #undef GL
126
127
129 struct Simple2DVertex {
130 float x, y;
131 float u, v;
132 };
133
135 static const int MAX_CACHED_CURSORS = 48;
136
137 /* static */ OpenGLBackend *OpenGLBackend::instance = nullptr;
138
139 GetOGLProcAddressProc GetOGLProcAddress;
140
148 const char *FindStringInExtensionList(const char *string, const char *substring)
149 {
150 while (true) {
151 /* Is the extension string present at all? */
152 const char *pos = strstr(string, substring);
153 if (pos == nullptr) break;
154
155 /* Is this a real match, i.e. are the chars before and after the matched string
156 * indeed spaces (or the start or end of the string, respectively)? */
157 const char *end = pos + strlen(substring);
158 if ((pos == string || pos[-1] == ' ') && (*end == ' ' || *end == '\0')) return pos;
159
160 /* False hit, try again for the remaining string. */
161 string = end;
162 }
163
164 return nullptr;
165 }
166
172 static bool IsOpenGLExtensionSupported(const char *extension)
173 {
174 static PFNGLGETSTRINGIPROC glGetStringi = nullptr;
175 static bool glGetStringi_loaded = false;
176
177 /* Starting with OpenGL 3.0 the preferred API to get the extensions
178 * has changed. Try to load the required function once. */
179 if (!glGetStringi_loaded) {
180 if (IsOpenGLVersionAtLeast(3, 0)) glGetStringi = (PFNGLGETSTRINGIPROC)GetOGLProcAddress("glGetStringi");
181 glGetStringi_loaded = true;
182 }
183
184 if (glGetStringi != nullptr) {
185 /* New style: Each supported extension can be queried and compared independently. */
186 GLint num_exts;
187 _glGetIntegerv(GL_NUM_EXTENSIONS, &num_exts);
188
189 for (GLint i = 0; i < num_exts; i++) {
190 const char *entry = (const char *)glGetStringi(GL_EXTENSIONS, i);
191 if (strcmp(entry, extension) == 0) return true;
192 }
193 } else {
194 /* Old style: A single, space-delimited string for all extensions. */
195 return FindStringInExtensionList((const char *)_glGetString(GL_EXTENSIONS), extension) != nullptr;
196 }
197
198 return false;
199 }
200
201 static byte _gl_major_ver = 0;
202 static byte _gl_minor_ver = 0;
203
211 bool IsOpenGLVersionAtLeast(byte major, byte minor)
212 {
213 return (_gl_major_ver > major) || (_gl_major_ver == major && _gl_minor_ver >= minor);
214 }
215
223 template <typename F>
224 static bool BindGLProc(F &f, const char *name)
225 {
226 f = reinterpret_cast<F>(GetOGLProcAddress(name));
227 return f != nullptr;
228 }
229
231 static bool BindBasicInfoProcs()
232 {
233 if (!BindGLProc(_glGetString, "glGetString")) return false;
234 if (!BindGLProc(_glGetIntegerv, "glGetIntegerv")) return false;
235 if (!BindGLProc(_glGetError, "glGetError")) return false;
236
237 return true;
238 }
239
241 static bool BindBasicOpenGLProcs()
242 {
243 if (!BindGLProc(_glDisable, "glDisable")) return false;
244 if (!BindGLProc(_glEnable, "glEnable")) return false;
245 if (!BindGLProc(_glViewport, "glViewport")) return false;
246 if (!BindGLProc(_glTexImage1D, "glTexImage1D")) return false;
247 if (!BindGLProc(_glTexImage2D, "glTexImage2D")) return false;
248 if (!BindGLProc(_glTexParameteri, "glTexParameteri")) return false;
249 if (!BindGLProc(_glTexSubImage1D, "glTexSubImage1D")) return false;
250 if (!BindGLProc(_glTexSubImage2D, "glTexSubImage2D")) return false;
251 if (!BindGLProc(_glBindTexture, "glBindTexture")) return false;
252 if (!BindGLProc(_glDeleteTextures, "glDeleteTextures")) return false;
253 if (!BindGLProc(_glGenTextures, "glGenTextures")) return false;
254 if (!BindGLProc(_glPixelStorei, "glPixelStorei")) return false;
255 if (!BindGLProc(_glClear, "glClear")) return false;
256 if (!BindGLProc(_glClearColor, "glClearColor")) return false;
257 if (!BindGLProc(_glBlendFunc, "glBlendFunc")) return false;
258 if (!BindGLProc(_glDrawArrays, "glDrawArrays")) return false;
259
260 return true;
261 }
262
264 static bool BindTextureExtensions()
265 {
266 if (IsOpenGLVersionAtLeast(1, 3)) {
267 if (!BindGLProc(_glActiveTexture, "glActiveTexture")) return false;
268 } else {
269 if (!BindGLProc(_glActiveTexture, "glActiveTextureARB")) return false;
270 }
271
272 return true;
273 }
274
276 static bool BindVBOExtension()
277 {
278 if (IsOpenGLVersionAtLeast(1, 5)) {
279 if (!BindGLProc(_glGenBuffers, "glGenBuffers")) return false;
280 if (!BindGLProc(_glDeleteBuffers, "glDeleteBuffers")) return false;
281 if (!BindGLProc(_glBindBuffer, "glBindBuffer")) return false;
282 if (!BindGLProc(_glBufferData, "glBufferData")) return false;
283 if (!BindGLProc(_glBufferSubData, "glBufferSubData")) return false;
284 if (!BindGLProc(_glMapBuffer, "glMapBuffer")) return false;
285 if (!BindGLProc(_glUnmapBuffer, "glUnmapBuffer")) return false;
286 } else {
287 if (!BindGLProc(_glGenBuffers, "glGenBuffersARB")) return false;
288 if (!BindGLProc(_glDeleteBuffers, "glDeleteBuffersARB")) return false;
289 if (!BindGLProc(_glBindBuffer, "glBindBufferARB")) return false;
290 if (!BindGLProc(_glBufferData, "glBufferDataARB")) return false;
291 if (!BindGLProc(_glBufferSubData, "glBufferSubDataARB")) return false;
292 if (!BindGLProc(_glMapBuffer, "glMapBufferARB")) return false;
293 if (!BindGLProc(_glUnmapBuffer, "glUnmapBufferARB")) return false;
294 }
295
296 if (IsOpenGLVersionAtLeast(4, 3) || IsOpenGLExtensionSupported("GL_ARB_clear_buffer_object")) {
297 BindGLProc(_glClearBufferSubData, "glClearBufferSubData");
298 } else {
299 _glClearBufferSubData = nullptr;
300 }
301
302 return true;
303 }
304
306 static bool BindVBAExtension()
307 {
308 /* The APPLE and ARB variants have different semantics (that don't matter for us).
309 * Successfully getting pointers to one variant doesn't mean it is supported for
310 * the current context. Always check the extension strings as well. */
311 if (IsOpenGLVersionAtLeast(3, 0) || IsOpenGLExtensionSupported("GL_ARB_vertex_array_object")) {
312 if (!BindGLProc(_glGenVertexArrays, "glGenVertexArrays")) return false;
313 if (!BindGLProc(_glDeleteVertexArrays, "glDeleteVertexArrays")) return false;
314 if (!BindGLProc(_glBindVertexArray, "glBindVertexArray")) return false;
315 } else if (IsOpenGLExtensionSupported("GL_APPLE_vertex_array_object")) {
316 if (!BindGLProc(_glGenVertexArrays, "glGenVertexArraysAPPLE")) return false;
317 if (!BindGLProc(_glDeleteVertexArrays, "glDeleteVertexArraysAPPLE")) return false;
318 if (!BindGLProc(_glBindVertexArray, "glBindVertexArrayAPPLE")) return false;
319 }
320
321 return true;
322 }
323
325 static bool BindShaderExtensions()
326 {
327 if (IsOpenGLVersionAtLeast(2, 0)) {
328 if (!BindGLProc(_glCreateProgram, "glCreateProgram")) return false;
329 if (!BindGLProc(_glDeleteProgram, "glDeleteProgram")) return false;
330 if (!BindGLProc(_glLinkProgram, "glLinkProgram")) return false;
331 if (!BindGLProc(_glUseProgram, "glUseProgram")) return false;
332 if (!BindGLProc(_glGetProgramiv, "glGetProgramiv")) return false;
333 if (!BindGLProc(_glGetProgramInfoLog, "glGetProgramInfoLog")) return false;
334 if (!BindGLProc(_glCreateShader, "glCreateShader")) return false;
335 if (!BindGLProc(_glDeleteShader, "glDeleteShader")) return false;
336 if (!BindGLProc(_glShaderSource, "glShaderSource")) return false;
337 if (!BindGLProc(_glCompileShader, "glCompileShader")) return false;
338 if (!BindGLProc(_glAttachShader, "glAttachShader")) return false;
339 if (!BindGLProc(_glGetShaderiv, "glGetShaderiv")) return false;
340 if (!BindGLProc(_glGetShaderInfoLog, "glGetShaderInfoLog")) return false;
341 if (!BindGLProc(_glGetUniformLocation, "glGetUniformLocation")) return false;
342 if (!BindGLProc(_glUniform1i, "glUniform1i")) return false;
343 if (!BindGLProc(_glUniform1f, "glUniform1f")) return false;
344 if (!BindGLProc(_glUniform2f, "glUniform2f")) return false;
345 if (!BindGLProc(_glUniform4f, "glUniform4f")) return false;
346
347 if (!BindGLProc(_glGetAttribLocation, "glGetAttribLocation")) return false;
348 if (!BindGLProc(_glEnableVertexAttribArray, "glEnableVertexAttribArray")) return false;
349 if (!BindGLProc(_glDisableVertexAttribArray, "glDisableVertexAttribArray")) return false;
350 if (!BindGLProc(_glVertexAttribPointer, "glVertexAttribPointer")) return false;
351 } else {
352 /* In the ARB extension programs and shaders are in the same object space. */
353 if (!BindGLProc(_glCreateProgram, "glCreateProgramObjectARB")) return false;
354 if (!BindGLProc(_glDeleteProgram, "glDeleteObjectARB")) return false;
355 if (!BindGLProc(_glLinkProgram, "glLinkProgramARB")) return false;
356 if (!BindGLProc(_glUseProgram, "glUseProgramObjectARB")) return false;
357 if (!BindGLProc(_glGetProgramiv, "glGetObjectParameterivARB")) return false;
358 if (!BindGLProc(_glGetProgramInfoLog, "glGetInfoLogARB")) return false;
359 if (!BindGLProc(_glCreateShader, "glCreateShaderObjectARB")) return false;
360 if (!BindGLProc(_glDeleteShader, "glDeleteObjectARB")) return false;
361 if (!BindGLProc(_glShaderSource, "glShaderSourceARB")) return false;
362 if (!BindGLProc(_glCompileShader, "glCompileShaderARB")) return false;
363 if (!BindGLProc(_glAttachShader, "glAttachObjectARB")) return false;
364 if (!BindGLProc(_glGetShaderiv, "glGetObjectParameterivARB")) return false;
365 if (!BindGLProc(_glGetShaderInfoLog, "glGetInfoLogARB")) return false;
366 if (!BindGLProc(_glGetUniformLocation, "glGetUniformLocationARB")) return false;
367 if (!BindGLProc(_glUniform1i, "glUniform1iARB")) return false;
368 if (!BindGLProc(_glUniform1f, "glUniform1fARB")) return false;
369 if (!BindGLProc(_glUniform2f, "glUniform2fARB")) return false;
370 if (!BindGLProc(_glUniform4f, "glUniform4fARB")) return false;
371
372 if (!BindGLProc(_glGetAttribLocation, "glGetAttribLocationARB")) return false;
373 if (!BindGLProc(_glEnableVertexAttribArray, "glEnableVertexAttribArrayARB")) return false;
374 if (!BindGLProc(_glDisableVertexAttribArray, "glDisableVertexAttribArrayARB")) return false;
375 if (!BindGLProc(_glVertexAttribPointer, "glVertexAttribPointerARB")) return false;
376 }
377
378 /* Bind functions only needed when using GLSL 1.50 shaders. */
379 if (IsOpenGLVersionAtLeast(3, 0)) {
380 BindGLProc(_glBindFragDataLocation, "glBindFragDataLocation");
381 } else if (IsOpenGLExtensionSupported("GL_EXT_gpu_shader4")) {
382 BindGLProc(_glBindFragDataLocation, "glBindFragDataLocationEXT");
383 } else {
384 _glBindFragDataLocation = nullptr;
385 }
386
387 return true;
388 }
389
391 static bool BindPersistentBufferExtensions()
392 {
393 /* Optional functions for persistent buffer mapping. */
394 if (IsOpenGLVersionAtLeast(3, 0)) {
395 if (!BindGLProc(_glMapBufferRange, "glMapBufferRange")) return false;
396 }
397 if (IsOpenGLVersionAtLeast(4, 4) || IsOpenGLExtensionSupported("GL_ARB_buffer_storage")) {
398 if (!BindGLProc(_glBufferStorage, "glBufferStorage")) return false;
399 }
400 #ifndef NO_GL_BUFFER_SYNC
401 if (IsOpenGLVersionAtLeast(3, 2) || IsOpenGLExtensionSupported("GL_ARB_sync")) {
402 if (!BindGLProc(_glClientWaitSync, "glClientWaitSync")) return false;
403 if (!BindGLProc(_glFenceSync, "glFenceSync")) return false;
404 if (!BindGLProc(_glDeleteSync, "glDeleteSync")) return false;
405 }
406 #endif
407
408 return true;
409 }
410
412 void APIENTRY DebugOutputCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
413 {
414 /* Make severity human readable. */
415 const char *severity_str = "";
416 switch (severity) {
417 case GL_DEBUG_SEVERITY_HIGH: severity_str = "high"; break;
418 case GL_DEBUG_SEVERITY_MEDIUM: severity_str = "medium"; break;
419 case GL_DEBUG_SEVERITY_LOW: severity_str = "low"; break;
420 }
421
422 /* Make type human readable.*/
423 const char *type_str = "Other";
424 switch (type) {
425 case GL_DEBUG_TYPE_ERROR: type_str = "Error"; break;
426 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: type_str = "Deprecated"; break;
427 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: type_str = "Undefined behaviour"; break;
428 case GL_DEBUG_TYPE_PERFORMANCE: type_str = "Performance"; break;
429 case GL_DEBUG_TYPE_PORTABILITY: type_str = "Portability"; break;
430 }
431
432 Debug(driver, 6, "OpenGL: {} ({}) - {}", type_str, severity_str, message);
433 }
434
436 void SetupDebugOutput()
437 {
438 #ifndef NO_DEBUG_MESSAGES
439 if (_debug_driver_level < 6) return;
440
441 if (IsOpenGLVersionAtLeast(4, 3)) {
442 BindGLProc(_glDebugMessageControl, "glDebugMessageControl");
443 BindGLProc(_glDebugMessageCallback, "glDebugMessageCallback");
444 } else if (IsOpenGLExtensionSupported("GL_ARB_debug_output")) {
445 BindGLProc(_glDebugMessageControl, "glDebugMessageControlARB");
446 BindGLProc(_glDebugMessageCallback, "glDebugMessageCallbackARB");
447 }
448
449 if (_glDebugMessageControl != nullptr && _glDebugMessageCallback != nullptr) {
450 /* Enable debug output. As synchronous debug output costs performance, we only enable it with a high debug level. */
451 _glEnable(GL_DEBUG_OUTPUT);
452 if (_debug_driver_level >= 8) _glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
453
454 _glDebugMessageCallback(&DebugOutputCallback, nullptr);
455 /* Enable all messages on highest debug level.*/
456 _glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, _debug_driver_level >= 9 ? GL_TRUE : GL_FALSE);
457 /* Get debug messages for errors and undefined/deprecated behaviour. */
458 _glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR, GL_DONT_CARE, 0, nullptr, GL_TRUE);
459 _glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR, GL_DONT_CARE, 0, nullptr, GL_TRUE);
460 _glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, GL_DONT_CARE, 0, nullptr, GL_TRUE);
461 }
462 #endif
463 }
464
471 /* static */ const char *OpenGLBackend::Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
472 {
473 if (OpenGLBackend::instance != nullptr) OpenGLBackend::Destroy();
474
475 GetOGLProcAddress = get_proc;
476
477 OpenGLBackend::instance = new OpenGLBackend();
478 return OpenGLBackend::instance->Init(screen_res);
479 }
480
484 /* static */ void OpenGLBackend::Destroy()
485 {
486 delete OpenGLBackend::instance;
487 OpenGLBackend::instance = nullptr;
488 }
489
493 OpenGLBackend::OpenGLBackend() : cursor_cache(MAX_CACHED_CURSORS)
494 {
495 }
496
500 OpenGLBackend::~OpenGLBackend()
501 {
502 if (_glDeleteProgram != nullptr) {
503 _glDeleteProgram(this->remap_program);
504 _glDeleteProgram(this->vid_program);
505 _glDeleteProgram(this->pal_program);
506 _glDeleteProgram(this->sprite_program);
507 }
508 if (_glDeleteVertexArrays != nullptr) _glDeleteVertexArrays(1, &this->vao_quad);
509 if (_glDeleteBuffers != nullptr) {
510 _glDeleteBuffers(1, &this->vbo_quad);
511 _glDeleteBuffers(1, &this->vid_pbo);
512 _glDeleteBuffers(1, &this->anim_pbo);
513 }
514 if (_glDeleteTextures != nullptr) {
515 this->InternalClearCursorCache();
516 OpenGLSprite::Destroy();
517
518 _glDeleteTextures(1, &this->vid_texture);
519 _glDeleteTextures(1, &this->anim_texture);
520 _glDeleteTextures(1, &this->pal_texture);
521 }
522 }
523
529 const char *OpenGLBackend::Init(const Dimension &screen_res)
530 {
531 if (!BindBasicInfoProcs()) return "OpenGL not supported";
532
533 /* Always query the supported OpenGL version as the current context might have changed. */
534 const char *ver = (const char *)_glGetString(GL_VERSION);
535 const char *vend = (const char *)_glGetString(GL_VENDOR);
536 const char *renderer = (const char *)_glGetString(GL_RENDERER);
537
538 if (ver == nullptr || vend == nullptr || renderer == nullptr) return "OpenGL not supported";
539
540 Debug(driver, 1, "OpenGL driver: {} - {} ({})", vend, renderer, ver);
541
542 #ifndef GL_ALLOW_SOFTWARE_RENDERER
543 /* Don't use MESA software rendering backends as they are slower than
544 * just using a non-OpenGL video driver. */
545 if (strncmp(renderer, "llvmpipe", 8) == 0 || strncmp(renderer, "softpipe", 8) == 0) return "Software renderer detected, not using OpenGL";
546 #endif
547
548 const char *minor = strchr(ver, '.');
549 _gl_major_ver = atoi(ver);
550 _gl_minor_ver = minor != nullptr ? atoi(minor + 1) : 0;
551
552 #ifdef _WIN32
553 /* Old drivers on Windows (especially if made by Intel) seem to be
554 * unstable, so cull the oldest stuff here. */
555 if (!IsOpenGLVersionAtLeast(3, 2)) return "Need at least OpenGL version 3.2 on Windows";
556 #endif
557
558 if (!BindBasicOpenGLProcs()) return "Failed to bind basic OpenGL functions.";
559
560 SetupDebugOutput();
561
562 /* OpenGL 1.3 is the absolute minimum. */
563 if (!IsOpenGLVersionAtLeast(1, 3)) return "OpenGL version >= 1.3 required";
564 /* Check for non-power-of-two texture support. */
565 if (!IsOpenGLVersionAtLeast(2, 0) && !IsOpenGLExtensionSupported("GL_ARB_texture_non_power_of_two")) return "Non-power-of-two textures not supported";
566 /* Check for single element texture formats. */
567 if (!IsOpenGLVersionAtLeast(3, 0) && !IsOpenGLExtensionSupported("GL_ARB_texture_rg")) return "Single element texture formats not supported";
568 if (!BindTextureExtensions()) return "Failed to bind texture extension functions";
569 /* Check for vertex buffer objects. */
570 if (!IsOpenGLVersionAtLeast(1, 5) && !IsOpenGLExtensionSupported("ARB_vertex_buffer_object")) return "Vertex buffer objects not supported";
571 if (!BindVBOExtension()) return "Failed to bind VBO extension functions";
572 /* Check for pixel buffer objects. */
573 if (!IsOpenGLVersionAtLeast(2, 1) && !IsOpenGLExtensionSupported("GL_ARB_pixel_buffer_object")) return "Pixel buffer objects not supported";
574 /* Check for vertex array objects. */
575 if (!IsOpenGLVersionAtLeast(3, 0) && (!IsOpenGLExtensionSupported("GL_ARB_vertex_array_object") || !IsOpenGLExtensionSupported("GL_APPLE_vertex_array_object"))) return "Vertex array objects not supported";
576 if (!BindVBAExtension()) return "Failed to bind VBA extension functions";
577 /* Check for shader objects. */
578 if (!IsOpenGLVersionAtLeast(2, 0) && (!IsOpenGLExtensionSupported("GL_ARB_shader_objects") || !IsOpenGLExtensionSupported("GL_ARB_fragment_shader") || !IsOpenGLExtensionSupported("GL_ARB_vertex_shader"))) return "No shader support";
579 if (!BindShaderExtensions()) return "Failed to bind shader extension functions";
580 if (IsOpenGLVersionAtLeast(3, 2) && _glBindFragDataLocation == nullptr) return "OpenGL claims to support version 3.2 but doesn't have glBindFragDataLocation";
581
582 this->persistent_mapping_supported = IsOpenGLVersionAtLeast(3, 0) && (IsOpenGLVersionAtLeast(4, 4) || IsOpenGLExtensionSupported("GL_ARB_buffer_storage"));
583 #ifndef NO_GL_BUFFER_SYNC
584 this->persistent_mapping_supported = this->persistent_mapping_supported && (IsOpenGLVersionAtLeast(3, 2) || IsOpenGLExtensionSupported("GL_ARB_sync"));
585 #endif
586
587 if (this->persistent_mapping_supported && !BindPersistentBufferExtensions()) {
588 Debug(driver, 1, "OpenGL claims to support persistent buffer mapping but doesn't export all functions, not using persistent mapping.");
589 this->persistent_mapping_supported = false;
590 }
591 if (this->persistent_mapping_supported) Debug(driver, 3, "OpenGL: Using persistent buffer mapping");
592
593 /* Check maximum texture size against screen resolution. */
594 GLint max_tex_size = 0;
595 _glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex_size);
596 if (std::max(screen_res.width, screen_res.height) > (uint)max_tex_size) return "Max supported texture size is too small";
597
598 /* Check available texture units. */
599 GLint max_tex_units = 0;
600 _glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_tex_units);
601 if (max_tex_units < 4) return "Not enough simultaneous textures supported";
602
603 Debug(driver, 2, "OpenGL shading language version: {}, texture units = {}", (const char *)_glGetString(GL_SHADING_LANGUAGE_VERSION), (int)max_tex_units);
604
605 if (!this->InitShaders()) return "Failed to initialize shaders";
606
607 /* Setup video buffer texture. */
608 _glGenTextures(1, &this->vid_texture);
609 _glBindTexture(GL_TEXTURE_2D, this->vid_texture);
610 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
611 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
612 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
613 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
614 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
615 _glBindTexture(GL_TEXTURE_2D, 0);
616 if (_glGetError() != GL_NO_ERROR) return "Can't generate video buffer texture";
617
618 /* Setup video buffer texture. */
619 _glGenTextures(1, &this->anim_texture);
620 _glBindTexture(GL_TEXTURE_2D, this->anim_texture);
621 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
622 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
623 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
624 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
625 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
626 _glBindTexture(GL_TEXTURE_2D, 0);
627 if (_glGetError() != GL_NO_ERROR) return "Can't generate animation buffer texture";
628
629 /* Setup palette texture. */
630 _glGenTextures(1, &this->pal_texture);
631 _glBindTexture(GL_TEXTURE_1D, this->pal_texture);
632 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
633 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
634 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
635 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
636 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
637 _glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 256, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
638 _glBindTexture(GL_TEXTURE_1D, 0);
639 if (_glGetError() != GL_NO_ERROR) return "Can't generate palette lookup texture";
640
641 /* Bind uniforms in rendering shader program. */
642 GLint tex_location = _glGetUniformLocation(this->vid_program, "colour_tex");
643 GLint palette_location = _glGetUniformLocation(this->vid_program, "palette");
644 GLint sprite_location = _glGetUniformLocation(this->vid_program, "sprite");
645 GLint screen_location = _glGetUniformLocation(this->vid_program, "screen");
646 _glUseProgram(this->vid_program);
647 _glUniform1i(tex_location, 0); // Texture unit 0.
648 _glUniform1i(palette_location, 1); // Texture unit 1.
649 /* Values that result in no transform. */
650 _glUniform4f(sprite_location, 0.0f, 0.0f, 1.0f, 1.0f);
651 _glUniform2f(screen_location, 1.0f, 1.0f);
652
653 /* Bind uniforms in palette rendering shader program. */
654 tex_location = _glGetUniformLocation(this->pal_program, "colour_tex");
655 palette_location = _glGetUniformLocation(this->pal_program, "palette");
656 sprite_location = _glGetUniformLocation(this->pal_program, "sprite");
657 screen_location = _glGetUniformLocation(this->pal_program, "screen");
658 _glUseProgram(this->pal_program);
659 _glUniform1i(tex_location, 0); // Texture unit 0.
660 _glUniform1i(palette_location, 1); // Texture unit 1.
661 _glUniform4f(sprite_location, 0.0f, 0.0f, 1.0f, 1.0f);
662 _glUniform2f(screen_location, 1.0f, 1.0f);
663
664 /* Bind uniforms in remap shader program. */
665 tex_location = _glGetUniformLocation(this->remap_program, "colour_tex");
666 palette_location = _glGetUniformLocation(this->remap_program, "palette");
667 GLint remap_location = _glGetUniformLocation(this->remap_program, "remap_tex");
668 this->remap_sprite_loc = _glGetUniformLocation(this->remap_program, "sprite");
669 this->remap_screen_loc = _glGetUniformLocation(this->remap_program, "screen");
670 this->remap_zoom_loc = _glGetUniformLocation(this->remap_program, "zoom");
671 this->remap_rgb_loc = _glGetUniformLocation(this->remap_program, "rgb");
672 _glUseProgram(this->remap_program);
673 _glUniform1i(tex_location, 0); // Texture unit 0.
674 _glUniform1i(palette_location, 1); // Texture unit 1.
675 _glUniform1i(remap_location, 2); // Texture unit 2.
676
677 /* Bind uniforms in sprite shader program. */
678 tex_location = _glGetUniformLocation(this->sprite_program, "colour_tex");
679 palette_location = _glGetUniformLocation(this->sprite_program, "palette");
680 remap_location = _glGetUniformLocation(this->sprite_program, "remap_tex");
681 GLint pal_location = _glGetUniformLocation(this->sprite_program, "pal");
682 this->sprite_sprite_loc = _glGetUniformLocation(this->sprite_program, "sprite");
683 this->sprite_screen_loc = _glGetUniformLocation(this->sprite_program, "screen");
684 this->sprite_zoom_loc = _glGetUniformLocation(this->sprite_program, "zoom");
685 this->sprite_rgb_loc = _glGetUniformLocation(this->sprite_program, "rgb");
686 this->sprite_crash_loc = _glGetUniformLocation(this->sprite_program, "crash");
687 _glUseProgram(this->sprite_program);
688 _glUniform1i(tex_location, 0); // Texture unit 0.
689 _glUniform1i(palette_location, 1); // Texture unit 1.
690 _glUniform1i(remap_location, 2); // Texture unit 2.
691 _glUniform1i(pal_location, 3); // Texture unit 3.
692 (void)_glGetError(); // Clear errors.
693
694 /* Create pixel buffer object as video buffer storage. */
695 _glGenBuffers(1, &this->vid_pbo);
696 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
697 _glGenBuffers(1, &this->anim_pbo);
698 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
699 if (_glGetError() != GL_NO_ERROR) return "Can't allocate pixel buffer for video buffer";
700
701 /* Prime vertex buffer with a full-screen quad and store
702 * the corresponding state in a vertex array object. */
703 static const Simple2DVertex vert_array[] = {
704 // x y u v
705 { 1.f, -1.f, 1.f, 1.f },
706 { 1.f, 1.f, 1.f, 0.f },
707 { -1.f, -1.f, 0.f, 1.f },
708 { -1.f, 1.f, 0.f, 0.f },
709 };
710
711 /* Create VAO. */
712 _glGenVertexArrays(1, &this->vao_quad);
713 _glBindVertexArray(this->vao_quad);
714
715 /* Create and fill VBO. */
716 _glGenBuffers(1, &this->vbo_quad);
717 _glBindBuffer(GL_ARRAY_BUFFER, this->vbo_quad);
718 _glBufferData(GL_ARRAY_BUFFER, sizeof(vert_array), vert_array, GL_STATIC_DRAW);
719 if (_glGetError() != GL_NO_ERROR) return "Can't generate VBO for fullscreen quad";
720
721 /* Set vertex state. */
722 GLint loc_position = _glGetAttribLocation(this->vid_program, "position");
723 GLint colour_position = _glGetAttribLocation(this->vid_program, "colour_uv");
724 _glEnableVertexAttribArray(loc_position);
725 _glEnableVertexAttribArray(colour_position);
726 _glVertexAttribPointer(loc_position, 2, GL_FLOAT, GL_FALSE, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, x));
727 _glVertexAttribPointer(colour_position, 2, GL_FLOAT, GL_FALSE, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, u));
728 _glBindVertexArray(0);
729
730 /* Create resources for sprite rendering. */
731 if (!OpenGLSprite::Create()) return "Failed to create sprite rendering resources";
732
733 this->PrepareContext();
734 (void)_glGetError(); // Clear errors.
735
736 return nullptr;
737 }
738
739 void OpenGLBackend::PrepareContext()
740 {
741 _glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
742 _glDisable(GL_DEPTH_TEST);
743 /* Enable alpha blending using the src alpha factor. */
744 _glEnable(GL_BLEND);
745 _glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
746 }
747
753 static bool VerifyShader(GLuint shader)
754 {
755 static ReusableBuffer<char> log_buf;
756
757 GLint result = GL_FALSE;
758 _glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
759
760 /* Output log if there is one. */
761 GLint log_len = 0;
762 _glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
763 if (log_len > 0) {
764 _glGetShaderInfoLog(shader, log_len, nullptr, log_buf.Allocate(log_len));
765 Debug(driver, result != GL_TRUE ? 0 : 2, "{}", log_buf.GetBuffer()); // Always print on failure.
766 }
767
768 return result == GL_TRUE;
769 }
770
776 static bool VerifyProgram(GLuint program)
777 {
778 static ReusableBuffer<char> log_buf;
779
780 GLint result = GL_FALSE;
781 _glGetProgramiv(program, GL_LINK_STATUS, &result);
782
783 /* Output log if there is one. */
784 GLint log_len = 0;
785 _glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
786 if (log_len > 0) {
787 _glGetProgramInfoLog(program, log_len, nullptr, log_buf.Allocate(log_len));
788 Debug(driver, result != GL_TRUE ? 0 : 2, "{}", log_buf.GetBuffer()); // Always print on failure.
789 }
790
791 return result == GL_TRUE;
792 }
793
798 bool OpenGLBackend::InitShaders()
799 {
800 const char *ver = (const char *)_glGetString(GL_SHADING_LANGUAGE_VERSION);
801 if (ver == nullptr) return false;
802
803 int glsl_major = ver[0] - '0';
804 int glsl_minor = ver[2] - '0';
805
806 bool glsl_150 = (IsOpenGLVersionAtLeast(3, 2) || glsl_major > 1 || (glsl_major == 1 && glsl_minor >= 5)) && _glBindFragDataLocation != nullptr;
807
808 /* Create vertex shader. */
809 GLuint vert_shader = _glCreateShader(GL_VERTEX_SHADER);
810 _glShaderSource(vert_shader, glsl_150 ? lengthof(_vertex_shader_sprite_150) : lengthof(_vertex_shader_sprite), glsl_150 ? _vertex_shader_sprite_150 : _vertex_shader_sprite, nullptr);
811 _glCompileShader(vert_shader);
812 if (!VerifyShader(vert_shader)) return false;
813
814 /* Create fragment shader for plain RGBA. */
815 GLuint frag_shader_rgb = _glCreateShader(GL_FRAGMENT_SHADER);
816 _glShaderSource(frag_shader_rgb, glsl_150 ? lengthof(_frag_shader_direct_150) : lengthof(_frag_shader_direct), glsl_150 ? _frag_shader_direct_150 : _frag_shader_direct, nullptr);
817 _glCompileShader(frag_shader_rgb);
818 if (!VerifyShader(frag_shader_rgb)) return false;
819
820 /* Create fragment shader for paletted only. */
821 GLuint frag_shader_pal = _glCreateShader(GL_FRAGMENT_SHADER);
822 _glShaderSource(frag_shader_pal, glsl_150 ? lengthof(_frag_shader_palette_150) : lengthof(_frag_shader_palette), glsl_150 ? _frag_shader_palette_150 : _frag_shader_palette, nullptr);
823 _glCompileShader(frag_shader_pal);
824 if (!VerifyShader(frag_shader_pal)) return false;
825
826 /* Sprite remap fragment shader. */
827 GLuint remap_shader = _glCreateShader(GL_FRAGMENT_SHADER);
828 _glShaderSource(remap_shader, glsl_150 ? lengthof(_frag_shader_rgb_mask_blend_150) : lengthof(_frag_shader_rgb_mask_blend), glsl_150 ? _frag_shader_rgb_mask_blend_150 : _frag_shader_rgb_mask_blend, nullptr);
829 _glCompileShader(remap_shader);
830 if (!VerifyShader(remap_shader)) return false;
831
832 /* Sprite fragment shader. */
833 GLuint sprite_shader = _glCreateShader(GL_FRAGMENT_SHADER);
834 _glShaderSource(sprite_shader, glsl_150 ? lengthof(_frag_shader_sprite_blend_150) : lengthof(_frag_shader_sprite_blend), glsl_150 ? _frag_shader_sprite_blend_150 : _frag_shader_sprite_blend, nullptr);
835 _glCompileShader(sprite_shader);
836 if (!VerifyShader(sprite_shader)) return false;
837
838 /* Link shaders to program. */
839 this->vid_program = _glCreateProgram();
840 _glAttachShader(this->vid_program, vert_shader);
841 _glAttachShader(this->vid_program, frag_shader_rgb);
842
843 this->pal_program = _glCreateProgram();
844 _glAttachShader(this->pal_program, vert_shader);
845 _glAttachShader(this->pal_program, frag_shader_pal);
846
847 this->remap_program = _glCreateProgram();
848 _glAttachShader(this->remap_program, vert_shader);
849 _glAttachShader(this->remap_program, remap_shader);
850
851 this->sprite_program = _glCreateProgram();
852 _glAttachShader(this->sprite_program, vert_shader);
853 _glAttachShader(this->sprite_program, sprite_shader);
854
855 if (glsl_150) {
856 /* Bind fragment shader outputs. */
857 _glBindFragDataLocation(this->vid_program, 0, "colour");
858 _glBindFragDataLocation(this->pal_program, 0, "colour");
859 _glBindFragDataLocation(this->remap_program, 0, "colour");
860 _glBindFragDataLocation(this->sprite_program, 0, "colour");
861 }
862
863 _glLinkProgram(this->vid_program);
864 if (!VerifyProgram(this->vid_program)) return false;
865
866 _glLinkProgram(this->pal_program);
867 if (!VerifyProgram(this->pal_program)) return false;
868
869 _glLinkProgram(this->remap_program);
870 if (!VerifyProgram(this->remap_program)) return false;
871
872 _glLinkProgram(this->sprite_program);
873 if (!VerifyProgram(this->sprite_program)) return false;
874
875 _glDeleteShader(vert_shader);
876 _glDeleteShader(frag_shader_rgb);
877 _glDeleteShader(frag_shader_pal);
878 _glDeleteShader(remap_shader);
879 _glDeleteShader(sprite_shader);
880
881 return true;
882 }
883
890 template <class T>
891 static void ClearPixelBuffer(size_t len, T data)
892 {
893 T *buf = reinterpret_cast<T *>(_glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE));
894 for (size_t i = 0; i < len; i++) {
895 *buf++ = data;
896 }
897 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
898 }
899
907 bool OpenGLBackend::Resize(int w, int h, bool force)
908 {
909 if (!force && _screen.width == w && _screen.height == h) return false;
910
911 int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
912 int pitch = Align(w, 4);
913
914 _glViewport(0, 0, w, h);
915
916 _glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
917
918 this->vid_buffer = nullptr;
919 if (this->persistent_mapping_supported) {
920 _glDeleteBuffers(1, &this->vid_pbo);
921 _glGenBuffers(1, &this->vid_pbo);
922 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
923 _glBufferStorage(GL_PIXEL_UNPACK_BUFFER, pitch * h * bpp / 8, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
924 } else {
925 /* Re-allocate video buffer texture and backing store. */
926 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
927 _glBufferData(GL_PIXEL_UNPACK_BUFFER, pitch * h * bpp / 8, nullptr, GL_DYNAMIC_DRAW);
928 }
929
930 if (bpp == 32) {
931 /* Initialize backing store alpha to opaque for 32bpp modes. */
932 Colour black(0, 0, 0);
933 if (_glClearBufferSubData != nullptr) {
934 _glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_RGBA8, 0, pitch * h * bpp / 8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &black.data);
935 } else {
936 ClearPixelBuffer<uint32>(pitch * h, black.data);
937 }
938 } else if (bpp == 8) {
939 if (_glClearBufferSubData != nullptr) {
940 byte b = 0;
941 _glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, pitch * h, GL_RED, GL_UNSIGNED_BYTE, &b);
942 } else {
943 ClearPixelBuffer<byte>(pitch * h, 0);
944 }
945 }
946
947 _glActiveTexture(GL_TEXTURE0);
948 _glBindTexture(GL_TEXTURE_2D, this->vid_texture);
949 switch (bpp) {
950 case 8:
951 _glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
952 break;
953
954 default:
955 _glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
956 break;
957 }
958 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
959
960 /* Does this blitter need a separate animation buffer? */
961 if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
962 this->anim_buffer = nullptr;
963 if (this->persistent_mapping_supported) {
964 _glDeleteBuffers(1, &this->anim_pbo);
965 _glGenBuffers(1, &this->anim_pbo);
966 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
967 _glBufferStorage(GL_PIXEL_UNPACK_BUFFER, pitch * h, nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT | GL_CLIENT_STORAGE_BIT);
968 } else {
969 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
970 _glBufferData(GL_PIXEL_UNPACK_BUFFER, pitch * h, nullptr, GL_DYNAMIC_DRAW);
971 }
972
973 /* Initialize buffer as 0 == no remap. */
974 if (_glClearBufferSubData != nullptr) {
975 byte b = 0;
976 _glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, pitch * h, GL_RED, GL_UNSIGNED_BYTE, &b);
977 } else {
978 ClearPixelBuffer<byte>(pitch * h, 0);
979 }
980
981 _glBindTexture(GL_TEXTURE_2D, this->anim_texture);
982 _glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
983 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
984 } else {
985 if (this->anim_buffer != nullptr) {
986 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
987 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
988 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
989 this->anim_buffer = nullptr;
990 }
991
992 /* Allocate dummy texture that always reads as 0 == no remap. */
993 uint dummy = 0;
994 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
995 _glBindTexture(GL_TEXTURE_2D, this->anim_texture);
996 _glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &dummy);
997 }
998
999 _glBindTexture(GL_TEXTURE_2D, 0);
1000
1001 /* Set new viewport. */
1002 _screen.height = h;
1003 _screen.width = w;
1004 _screen.pitch = pitch;
1005 _screen.dst_ptr = nullptr;
1006
1007 /* Update screen size in remap shader program. */
1008 _glUseProgram(this->remap_program);
1009 _glUniform2f(this->remap_screen_loc, (float)_screen.width, (float)_screen.height);
1010
1011 _glClear(GL_COLOR_BUFFER_BIT);
1012
1013 return true;
1014 }
1015
1022 void OpenGLBackend::UpdatePalette(const Colour *pal, uint first, uint length)
1023 {
1024 assert(first + length <= 256);
1025
1026 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1027 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1028 _glActiveTexture(GL_TEXTURE1);
1029 _glBindTexture(GL_TEXTURE_1D, this->pal_texture);
1030 _glTexSubImage1D(GL_TEXTURE_1D, 0, first, length, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pal + first);
1031 }
1032
1036 void OpenGLBackend::Paint()
1037 {
1038 _glClear(GL_COLOR_BUFFER_BIT);
1039
1040 _glDisable(GL_BLEND);
1041
1042 /* Blit video buffer to screen. */
1043 _glActiveTexture(GL_TEXTURE0);
1044 _glBindTexture(GL_TEXTURE_2D, this->vid_texture);
1045 _glActiveTexture(GL_TEXTURE1);
1046 _glBindTexture(GL_TEXTURE_1D, this->pal_texture);
1047 /* Is the blitter relying on a separate animation buffer? */
1048 if (BlitterFactory::GetCurrentBlitter()->NeedsAnimationBuffer()) {
1049 _glActiveTexture(GL_TEXTURE2);
1050 _glBindTexture(GL_TEXTURE_2D, this->anim_texture);
1051 _glUseProgram(this->remap_program);
1052 _glUniform4f(this->remap_sprite_loc, 0.0f, 0.0f, 1.0f, 1.0f);
1053 _glUniform2f(this->remap_screen_loc, 1.0f, 1.0f);
1054 _glUniform1f(this->remap_zoom_loc, 0);
1055 _glUniform1i(this->remap_rgb_loc, 1);
1056 } else {
1057 _glUseProgram(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 8 ? this->pal_program : this->vid_program);
1058 }
1059 _glBindVertexArray(this->vao_quad);
1060 _glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1061
1062 _glEnable(GL_BLEND);
1063 }
1064
1068 void OpenGLBackend::DrawMouseCursor()
1069 {
1070 if (!this->cursor_in_window) return;
1071
1072 /* Draw cursor on screen */
1073 _cur_dpi = &_screen;
1074 for (uint i = 0; i < this->cursor_sprite_count; ++i) {
1075 SpriteID sprite = this->cursor_sprite_seq[i].sprite;
1076
1077 /* Sprites are cached by PopulateCursorCache(). */
1078 if (this->cursor_cache.Contains(sprite)) {
1079 Sprite *spr = this->cursor_cache.Get(sprite);
1080
1081 this->RenderOglSprite((OpenGLSprite *)spr->data, this->cursor_sprite_seq[i].pal,
1082 this->cursor_pos.x + this->cursor_sprite_pos[i].x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI),
1083 this->cursor_pos.y + this->cursor_sprite_pos[i].y + UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI),
1084 ZOOM_LVL_GUI);
1085 }
1086 }
1087 }
1088
1089 void OpenGLBackend::PopulateCursorCache()
1090 {
1091 static_assert(lengthof(_cursor.sprite_seq) == lengthof(this->cursor_sprite_seq));
1092 static_assert(lengthof(_cursor.sprite_pos) == lengthof(this->cursor_sprite_pos));
1093
1094 if (this->clear_cursor_cache) {
1095 /* We have a pending cursor cache clear to do first. */
1096 this->clear_cursor_cache = false;
1097 this->last_sprite_pal = (PaletteID)-1;
1098
1099 this->InternalClearCursorCache();
1100 }
1101
1102 this->cursor_pos = _cursor.pos;
1103 this->cursor_sprite_count = _cursor.sprite_count;
1104 this->cursor_in_window = _cursor.in_window;
1105
1106 for (uint i = 0; i < _cursor.sprite_count; ++i) {
1107 this->cursor_sprite_seq[i] = _cursor.sprite_seq[i];
1108 this->cursor_sprite_pos[i] = _cursor.sprite_pos[i];
1109 SpriteID sprite = _cursor.sprite_seq[i].sprite;
1110
1111 if (!this->cursor_cache.Contains(sprite)) {
1112 Sprite *old = this->cursor_cache.Insert(sprite, (Sprite *)GetRawSprite(sprite, ST_NORMAL, &SimpleSpriteAlloc, this));
1113 if (old != nullptr) {
1114 OpenGLSprite *sprite = (OpenGLSprite *)old->data;
1115 sprite->~OpenGLSprite();
1116 free(old);
1117 }
1118 }
1119 }
1120 }
1121
1125 void OpenGLBackend::InternalClearCursorCache()
1126 {
1127 Sprite *sp;
1128 while ((sp = this->cursor_cache.Pop()) != nullptr) {
1129 OpenGLSprite *sprite = (OpenGLSprite *)sp->data;
1130 sprite->~OpenGLSprite();
1131 free(sp);
1132 }
1133 }
1134
1138 void OpenGLBackend::ClearCursorCache()
1139 {
1140 /* If the game loop is threaded, this function might be called
1141 * from the game thread. As we can call OpenGL functions only
1142 * on the main thread, just set a flag that is handled the next
1143 * time we prepare the cursor cache for drawing. */
1144 this->clear_cursor_cache = true;
1145 }
1146
1151 void *OpenGLBackend::GetVideoBuffer()
1152 {
1153 #ifndef NO_GL_BUFFER_SYNC
1154 if (this->sync_vid_mapping != nullptr) _glClientWaitSync(this->sync_vid_mapping, GL_SYNC_FLUSH_COMMANDS_BIT, 100000000); // 100ms timeout.
1155 #endif
1156
1157 if (!this->persistent_mapping_supported) {
1158 assert(this->vid_buffer == nullptr);
1159 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
1160 this->vid_buffer = _glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
1161 } else if (this->vid_buffer == nullptr) {
1162 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
1163 this->vid_buffer = _glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, _screen.pitch * _screen.height * BlitterFactory::GetCurrentBlitter()->GetScreenDepth() / 8, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
1164 }
1165
1166 return this->vid_buffer;
1167 }
1168
1173 uint8 *OpenGLBackend::GetAnimBuffer()
1174 {
1175 if (this->anim_pbo == 0) return nullptr;
1176
1177 #ifndef NO_GL_BUFFER_SYNC
1178 if (this->sync_anim_mapping != nullptr) _glClientWaitSync(this->sync_anim_mapping, GL_SYNC_FLUSH_COMMANDS_BIT, 100000000); // 100ms timeout.
1179 #endif
1180
1181 if (!this->persistent_mapping_supported) {
1182 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
1183 this->anim_buffer = _glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
1184 } else if (this->anim_buffer == nullptr) {
1185 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
1186 this->anim_buffer = _glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, _screen.pitch * _screen.height, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
1187 }
1188
1189 return (uint8 *)this->anim_buffer;
1190 }
1191
1196 void OpenGLBackend::ReleaseVideoBuffer(const Rect &update_rect)
1197 {
1198 assert(this->vid_pbo != 0);
1199
1200 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
1201 if (!this->persistent_mapping_supported) {
1202 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1203 this->vid_buffer = nullptr;
1204 }
1205
1206 #ifndef NO_GL_BUFFER_SYNC
1207 if (this->persistent_mapping_supported) {
1208 _glDeleteSync(this->sync_vid_mapping);
1209 this->sync_vid_mapping = nullptr;
1210 }
1211 #endif
1212
1213 /* Update changed rect of the video buffer texture. */
1214 if (!IsEmptyRect(update_rect)) {
1215 _glActiveTexture(GL_TEXTURE0);
1216 _glBindTexture(GL_TEXTURE_2D, this->vid_texture);
1217 _glPixelStorei(GL_UNPACK_ROW_LENGTH, _screen.pitch);
1218 switch (BlitterFactory::GetCurrentBlitter()->GetScreenDepth()) {
1219 case 8:
1220 _glTexSubImage2D(GL_TEXTURE_2D, 0, update_rect.left, update_rect.top, update_rect.right - update_rect.left, update_rect.bottom - update_rect.top, GL_RED, GL_UNSIGNED_BYTE, (GLvoid *)(size_t)(update_rect.top * _screen.pitch + update_rect.left));
1221 break;
1222
1223 default:
1224 _glTexSubImage2D(GL_TEXTURE_2D, 0, update_rect.left, update_rect.top, update_rect.right - update_rect.left, update_rect.bottom - update_rect.top, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, (GLvoid *)(size_t)(update_rect.top * _screen.pitch * 4 + update_rect.left * 4));
1225 break;
1226 }
1227
1228 #ifndef NO_GL_BUFFER_SYNC
1229 if (this->persistent_mapping_supported) this->sync_vid_mapping = _glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1230 #endif
1231 }
1232 }
1233
1238 void OpenGLBackend::ReleaseAnimBuffer(const Rect &update_rect)
1239 {
1240 if (this->anim_pbo == 0) return;
1241
1242 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
1243 if (!this->persistent_mapping_supported) {
1244 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1245 this->anim_buffer = nullptr;
1246 }
1247
1248 #ifndef NO_GL_BUFFER_SYNC
1249 if (this->persistent_mapping_supported) {
1250 _glDeleteSync(this->sync_anim_mapping);
1251 this->sync_anim_mapping = nullptr;
1252 }
1253 #endif
1254
1255 /* Update changed rect of the video buffer texture. */
1256 if (update_rect.left != update_rect.right) {
1257 _glActiveTexture(GL_TEXTURE0);
1258 _glBindTexture(GL_TEXTURE_2D, this->anim_texture);
1259 _glPixelStorei(GL_UNPACK_ROW_LENGTH, _screen.pitch);
1260 _glTexSubImage2D(GL_TEXTURE_2D, 0, update_rect.left, update_rect.top, update_rect.right - update_rect.left, update_rect.bottom - update_rect.top, GL_RED, GL_UNSIGNED_BYTE, (GLvoid *)(size_t)(update_rect.top * _screen.pitch + update_rect.left));
1261
1262 #ifndef NO_GL_BUFFER_SYNC
1263 if (this->persistent_mapping_supported) this->sync_anim_mapping = _glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1264 #endif
1265 }
1266 }
1267
1268 /* virtual */ Sprite *OpenGLBackend::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
1269 {
1270 /* Allocate and construct sprite data. */
1271 Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sizeof(OpenGLSprite));
1272
1273 OpenGLSprite *gl_sprite = (OpenGLSprite *)dest_sprite->data;
1274 new (gl_sprite) OpenGLSprite(sprite->width, sprite->height, sprite->type == ST_FONT ? 1 : ZOOM_LVL_COUNT, sprite->colours);
1275
1276 /* Upload texture data. */
1277 for (int i = 0; i < (sprite->type == ST_FONT ? 1 : ZOOM_LVL_COUNT); i++) {
1278 gl_sprite->Update(sprite[i].width, sprite[i].height, i, sprite[i].data);
1279 }
1280
1281 dest_sprite->height = sprite->height;
1282 dest_sprite->width = sprite->width;
1283 dest_sprite->x_offs = sprite->x_offs;
1284 dest_sprite->y_offs = sprite->y_offs;
1285
1286 return dest_sprite;
1287 }
1288
1296 void OpenGLBackend::RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
1297 {
1298 /* Set textures. */
1299 bool rgb = gl_sprite->BindTextures();
1300 _glActiveTexture(GL_TEXTURE0 + 1);
1301 _glBindTexture(GL_TEXTURE_1D, this->pal_texture);
1302
1303 /* Set palette remap. */
1304 _glActiveTexture(GL_TEXTURE0 + 3);
1305 if (pal != PAL_NONE) {
1306 _glBindTexture(GL_TEXTURE_1D, OpenGLSprite::pal_tex);
1307 if (pal != this->last_sprite_pal) {
1308 /* Different remap palette in use, update texture. */
1309 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, OpenGLSprite::pal_pbo);
1310 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1311
1312 _glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, 256, GetNonSprite(GB(pal, 0, PALETTE_WIDTH), ST_RECOLOUR) + 1);
1313 _glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RED, GL_UNSIGNED_BYTE, nullptr);
1314
1315 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1316
1317 this->last_sprite_pal = pal;
1318 }
1319 } else {
1320 _glBindTexture(GL_TEXTURE_1D, OpenGLSprite::pal_identity);
1321 }
1322
1323 /* Set up shader program. */
1324 Dimension dim = gl_sprite->GetSize(zoom);
1325 _glUseProgram(this->sprite_program);
1326 _glUniform4f(this->sprite_sprite_loc, (float)x, (float)y, (float)dim.width, (float)dim.height);
1327 _glUniform1f(this->sprite_zoom_loc, (float)(zoom - ZOOM_LVL_BEGIN));
1328 _glUniform2f(this->sprite_screen_loc, (float)_screen.width, (float)_screen.height);
1329 _glUniform1i(this->sprite_rgb_loc, rgb ? 1 : 0);
1330 _glUniform1i(this->sprite_crash_loc, pal == PALETTE_CRASH ? 1 : 0);
1331
1332 _glBindVertexArray(this->vao_quad);
1333 _glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1334 }
1335
1336
1337 /* static */ GLuint OpenGLSprite::dummy_tex[] = { 0, 0 };
1338 /* static */ GLuint OpenGLSprite::pal_identity = 0;
1339 /* static */ GLuint OpenGLSprite::pal_tex = 0;
1340 /* static */ GLuint OpenGLSprite::pal_pbo = 0;
1341
1346 /* static */ bool OpenGLSprite::Create()
1347 {
1348 _glGenTextures(NUM_TEX, OpenGLSprite::dummy_tex);
1349
1350 for (int t = TEX_RGBA; t < NUM_TEX; t++) {
1351 _glBindTexture(GL_TEXTURE_2D, OpenGLSprite::dummy_tex[t]);
1352
1353 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
1354 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1355 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
1356 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1357 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1358 }
1359
1360 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1361 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1362
1363 /* Load dummy RGBA texture. */
1364 const Colour rgb_pixel(0, 0, 0);
1365 _glBindTexture(GL_TEXTURE_2D, OpenGLSprite::dummy_tex[TEX_RGBA]);
1366 _glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &rgb_pixel);
1367
1368 /* Load dummy remap texture. */
1369 const uint pal = 0;
1370 _glBindTexture(GL_TEXTURE_2D, OpenGLSprite::dummy_tex[TEX_REMAP]);
1371 _glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &pal);
1372
1373 /* Create palette remap textures. */
1374 std::array<uint8, 256> identity_pal;
1375 std::iota(std::begin(identity_pal), std::end(identity_pal), 0);
1376
1377 /* Permanent texture for identity remap. */
1378 _glGenTextures(1, &OpenGLSprite::pal_identity);
1379 _glBindTexture(GL_TEXTURE_1D, OpenGLSprite::pal_identity);
1380 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1381 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1382 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
1383 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1384 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1385 _glTexImage1D(GL_TEXTURE_1D, 0, GL_R8, 256, 0, GL_RED, GL_UNSIGNED_BYTE, identity_pal.data());
1386
1387 /* Dynamically updated texture for remaps. */
1388 _glGenTextures(1, &OpenGLSprite::pal_tex);
1389 _glBindTexture(GL_TEXTURE_1D, OpenGLSprite::pal_tex);
1390 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1391 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1392 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
1393 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1394 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1395 _glTexImage1D(GL_TEXTURE_1D, 0, GL_R8, 256, 0, GL_RED, GL_UNSIGNED_BYTE, identity_pal.data());
1396
1397 /* Pixel buffer for remap updates. */
1398 _glGenBuffers(1, &OpenGLSprite::pal_pbo);
1399 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, OpenGLSprite::pal_pbo);
1400 _glBufferData(GL_PIXEL_UNPACK_BUFFER, 256, identity_pal.data(), GL_DYNAMIC_DRAW);
1401 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1402
1403 return _glGetError() == GL_NO_ERROR;
1404 }
1405
1407 /* static */ void OpenGLSprite::Destroy()
1408 {
1409 _glDeleteTextures(NUM_TEX, OpenGLSprite::dummy_tex);
1410 _glDeleteTextures(1, &OpenGLSprite::pal_identity);
1411 _glDeleteTextures(1, &OpenGLSprite::pal_tex);
1412 if (_glDeleteBuffers != nullptr) _glDeleteBuffers(1, &OpenGLSprite::pal_pbo);
1413 }
1414
1422 OpenGLSprite::OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components)
1423 {
1424 assert(levels > 0);
1425 (void)_glGetError();
1426
1427 this->dim.width = width;
1428 this->dim.height = height;
1429
1430 MemSetT(this->tex, 0, NUM_TEX);
1431 _glActiveTexture(GL_TEXTURE0);
1432 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1433
1434 for (int t = TEX_RGBA; t < NUM_TEX; t++) {
1435 /* Sprite component present? */
1436 if (t == TEX_RGBA && components == SCC_PAL) continue;
1437 if (t == TEX_REMAP && (components & SCC_PAL) != SCC_PAL) continue;
1438
1439 /* Allocate texture. */
1440 _glGenTextures(1, &this->tex[t]);
1441 _glBindTexture(GL_TEXTURE_2D, this->tex[t]);
1442
1443 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
1444 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1445 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, levels - 1);
1446 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1447 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1448
1449 /* Set size. */
1450 for (uint i = 0, w = width, h = height; i < levels; i++, w /= 2, h /= 2) {
1451 assert(w * h != 0);
1452 if (t == TEX_REMAP) {
1453 _glTexImage2D(GL_TEXTURE_2D, i, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
1454 } else {
1455 _glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
1456 }
1457 }
1458 }
1459
1460 assert(_glGetError() == GL_NO_ERROR);
1461 }
1462
1463 OpenGLSprite::~OpenGLSprite()
1464 {
1465 _glDeleteTextures(NUM_TEX, this->tex);
1466 }
1467
1475 void OpenGLSprite::Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel * data)
1476 {
1477 static ReusableBuffer<Colour> buf_rgba;
1478 static ReusableBuffer<uint8> buf_pal;
1479
1480 _glActiveTexture(GL_TEXTURE0);
1481 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1482 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1483
1484 if (this->tex[TEX_RGBA] != 0) {
1485 /* Unpack pixel data */
1486 Colour *rgba = buf_rgba.Allocate(width * height);
1487 for (size_t i = 0; i < width * height; i++) {
1488 rgba[i].r = data[i].r;
1489 rgba[i].g = data[i].g;
1490 rgba[i].b = data[i].b;
1491 rgba[i].a = data[i].a;
1492 }
1493
1494 _glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA]);
1495 _glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, rgba);
1496 }
1497
1498 if (this->tex[TEX_REMAP] != 0) {
1499 /* Unpack and align pixel data. */
1500 int pitch = Align(width, 4);
1501
1502 uint8 *pal = buf_pal.Allocate(pitch * height);
1503 const SpriteLoader::CommonPixel *row = data;
1504 for (uint y = 0; y < height; y++, pal += pitch, row += width) {
1505 for (uint x = 0; x < width; x++) {
1506 pal[x] = row[x].m;
1507 }
1508 }
1509
1510 _glBindTexture(GL_TEXTURE_2D, this->tex[TEX_REMAP]);
1511 _glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, buf_pal.GetBuffer());
1512 }
1513
1514 assert(_glGetError() == GL_NO_ERROR);
1515 }
1516
1522 inline Dimension OpenGLSprite::GetSize(ZoomLevel level) const
1523 {
1524 Dimension sd = { (uint)UnScaleByZoomLower(this->dim.width, level), (uint)UnScaleByZoomLower(this->dim.height, level) };
1525 return sd;
1526 }
1527
1532 bool OpenGLSprite::BindTextures()
1533 {
1534 _glActiveTexture(GL_TEXTURE0);
1535 _glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA] != 0 ? this->tex[TEX_RGBA] : OpenGLSprite::dummy_tex[TEX_RGBA]);
1536 _glActiveTexture(GL_TEXTURE0 + 2);
1537 _glBindTexture(GL_TEXTURE_2D, this->tex[TEX_REMAP] != 0 ? this->tex[TEX_REMAP] : OpenGLSprite::dummy_tex[TEX_REMAP]);
1538
1539 return this->tex[TEX_RGBA] != 0;
1540 }
SpriteLoader::CommonPixel::m
uint8 m
Remap-channel.
Definition: spriteloader.hpp:39
OpenGLBackend::cursor_pos
Point cursor_pos
Cursor position.
Definition: opengl.h:68
SCC_PAL
@ SCC_PAL
Sprite has palette data.
Definition: spriteloader.hpp:25
OpenGLBackend::sync_vid_mapping
GLsync sync_vid_mapping
Sync object for the persistently mapped video buffer.
Definition: opengl.h:35
_frag_shader_palette
static const char * _frag_shader_palette[]
Fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
Definition: opengl_shader.h:64
SpriteLoader::CommonPixel::r
uint8 r
Red-channel.
Definition: spriteloader.hpp:35
LRUCache::Get
Tdata * Get(const Tkey key)
Get an item from the cache.
Definition: lrucache.hpp:106
OpenGLBackend::RenderOglSprite
void RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
Render a sprite to the back buffer.
Definition: opengl.cpp:1296
OpenGLSprite::pal_tex
static GLuint pal_tex
Texture for palette remap.
Definition: opengl.h:132
Colour::data
uint32 data
Conversion of the channel information to a 32 bit number.
Definition: gfx_type.h:164
OpenGLBackend::pal_program
GLuint pal_program
Shader program for rendering a paletted video buffer.
Definition: opengl.h:42
OpenGLSprite::TEX_RGBA
@ TEX_RGBA
RGBA texture part.
Definition: opengl.h:121
UnScaleByZoomLower
static int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL)
Definition: zoom_func.h:56
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
ST_FONT
@ ST_FONT
A sprite used for fonts.
Definition: gfx_type.h:304
ReusableBuffer
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
Definition: alloc_type.hpp:24
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
_frag_shader_sprite_blend_150
static const char * _frag_shader_sprite_blend_150[]
GLSL 1.50 fragment shader that performs a palette lookup to read the colour from a sprite texture.
Definition: opengl_shader.h:179
OpenGLSprite::GetSize
Dimension GetSize(ZoomLevel level) const
Query the sprite size at a certain zoom level.
Definition: opengl.cpp:1522
CursorVars::sprite_count
uint sprite_count
number of sprites to draw
Definition: gfx_type.h:130
LRUCache::Pop
Tdata * Pop()
Pop the least recently used item.
Definition: lrucache.hpp:90
OpenGLSprite::pal_identity
static GLuint pal_identity
Identity texture mapping.
Definition: opengl.h:131
_frag_shader_direct_150
static const char * _frag_shader_direct_150[]
GLSL 1.50 fragment shader that reads the fragment colour from a 32bpp texture.
Definition: opengl_shader.h:53
_vertex_shader_sprite
static const char * _vertex_shader_sprite[]
Vertex shader that positions a sprite on screen.
Definition: opengl_shader.h:13
OpenGLSprite::Destroy
static void Destroy()
Free all common resources for sprite rendering.
Definition: opengl.cpp:1407
Sprite::data
byte data[]
Sprite data.
Definition: spritecache.h:22
OpenGLSprite::tex
GLuint tex[NUM_TEX]
The texture objects.
Definition: opengl.h:127
OpenGLSprite::OpenGLSprite
OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components)
Create an OpenGL sprite with a palette remap part.
Definition: opengl.cpp:1422
OpenGLSprite::TEX_REMAP
@ TEX_REMAP
Remap texture part.
Definition: opengl.h:122
Blitter::GetScreenDepth
virtual uint8 GetScreenDepth()=0
Get the screen depth this blitter works for.
PalSpriteID::sprite
SpriteID sprite
The 'real' sprite.
Definition: gfx_type.h:23
ZOOM_LVL_COUNT
@ ZOOM_LVL_COUNT
Number of zoom levels.
Definition: zoom_type.h:32
Sprite::height
uint16 height
Height of the sprite.
Definition: spritecache.h:18
Sprite::x_offs
int16 x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:20
OpenGLBackend::vid_buffer
void * vid_buffer
Pointer to the mapped video buffer.
Definition: opengl.h:38
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
ReusableBuffer::Allocate
T * Allocate(size_t count)
Get buffer of at least count times T.
Definition: alloc_type.hpp:42
ST_NORMAL
@ ST_NORMAL
The most basic (normal) sprite.
Definition: gfx_type.h:302
OpenGLSprite::pal_pbo
static GLuint pal_pbo
Pixel buffer object for remap upload.
Definition: opengl.h:133
OpenGLBackend::~OpenGLBackend
~OpenGLBackend()
Free allocated resources.
Definition: opengl.cpp:500
OpenGLBackend::sprite_zoom_loc
GLint sprite_zoom_loc
Uniform location for sprite zoom;.
Definition: opengl.h:60
CursorVars::sprite_pos
Point sprite_pos[16]
relative position of individual sprites
Definition: gfx_type.h:129
ZOOM_LVL_BEGIN
@ ZOOM_LVL_BEGIN
Begin for iteration.
Definition: zoom_type.h:23
OpenGLBackend::anim_pbo
GLuint anim_pbo
Pixel buffer object storing the memory used for the animation buffer.
Definition: opengl.h:48
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
OpenGLBackend
Platform-independent back-end class for OpenGL video drivers.
Definition: opengl.h:30
OpenGLBackend::vid_texture
GLuint vid_texture
Texture handle for the video buffer texture.
Definition: opengl.h:40
OpenGLBackend::remap_rgb_loc
GLint remap_rgb_loc
Uniform location for RGB mode flag;.
Definition: opengl.h:55
_frag_shader_palette_150
static const char * _frag_shader_palette_150[]
GLSL 1.50 fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
Definition: opengl_shader.h:76
OpenGLBackend::DrawMouseCursor
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition: opengl.cpp:1068
OpenGLBackend::last_sprite_pal
PaletteID last_sprite_pal
Last uploaded remap palette.
Definition: opengl.h:65
Align
static T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:35
Simple2DVertex
A simple 2D vertex with just position and texture.
Definition: opengl.cpp:129
PALETTE_WIDTH
@ PALETTE_WIDTH
number of bits of the sprite containing the recolour palette
Definition: sprites.h:1524
SpriteLoader::Sprite::type
SpriteType type
The sprite type.
Definition: spriteloader.hpp:53
OpenGLBackend::anim_buffer
void * anim_buffer
Pointer to the mapped animation buffer.
Definition: opengl.h:47
OpenGLBackend::ReleaseVideoBuffer
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition: opengl.cpp:1196
OpenGLBackend::vbo_quad
GLuint vbo_quad
Vertex buffer with a fullscreen quad.
Definition: opengl.h:44
OpenGLSprite::Create
static bool Create()
Create all common resources for sprite rendering.
Definition: opengl.cpp:1346
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition: spriteloader.hpp:34
LRUCache::Contains
bool Contains(const Tkey key)
Test if a key is already contained in the cache.
Definition: lrucache.hpp:49
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:141
OpenGLBackend::GetVideoBuffer
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition: opengl.cpp:1151
OpenGLBackend::instance
static OpenGLBackend * instance
Singleton instance pointer.
Definition: opengl.h:32
Sprite::width
uint16 width
Width of the sprite.
Definition: spritecache.h:19
SpriteLoader::Sprite::x_offs
int16 x_offs
The x-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:51
OpenGLBackend::cursor_in_window
bool cursor_in_window
Cursor inside this window.
Definition: opengl.h:69
OpenGLBackend::anim_texture
GLuint anim_texture
Texture handle for the animation buffer texture.
Definition: opengl.h:49
OpenGLBackend::sprite_screen_loc
GLint sprite_screen_loc
Uniform location for screen size;.
Definition: opengl.h:59
OpenGLBackend::Create
static const char * Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
Create and initialize the singleton back-end class.
Definition: opengl.cpp:471
_vertex_shader_sprite_150
static const char * _vertex_shader_sprite_150[]
GLSL 1.50 vertex shader that positions a sprite on screen.
Definition: opengl_shader.h:28
OpenGLBackend::sync_anim_mapping
GLsync sync_anim_mapping
Sync object for the persistently mapped animation buffer.
Definition: opengl.h:36
OpenGLBackend::Destroy
static void Destroy()
Free resources and destroy singleton back-end class.
Definition: opengl.cpp:484
UnScaleByZoom
static int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_NORMAL) When shifting right,...
Definition: zoom_func.h:34
OpenGLBackend::persistent_mapping_supported
bool persistent_mapping_supported
Persistent pixel buffer mapping supported.
Definition: opengl.h:34
OpenGLBackend::cursor_cache
LRUCache< SpriteID, Sprite > cursor_cache
Cache of encoded cursor sprites.
Definition: opengl.h:64
LRUCache::Insert
Tdata * Insert(const Tkey key, Tdata *item)
Insert a new data item with a specified key.
Definition: lrucache.hpp:60
SpriteLoader::Sprite::colours
SpriteColourComponent colours
The colour components of the sprite with useful information.
Definition: spriteloader.hpp:54
OpenGLBackend::cursor_sprite_seq
PalSpriteID cursor_sprite_seq[16]
Current image of cursor.
Definition: opengl.h:70
_frag_shader_rgb_mask_blend_150
static const char * _frag_shader_rgb_mask_blend_150[]
GLSL 1.50 fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
Definition: opengl_shader.h:128
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:163
OpenGLBackend::vao_quad
GLuint vao_quad
Vertex array object storing the rendering state for the fullscreen quad.
Definition: opengl.h:43
OpenGLSprite::dummy_tex
static GLuint dummy_tex[NUM_TEX]
1x1 dummy textures to substitute for unused sprite components.
Definition: opengl.h:129
OpenGLBackend::ClearCursorCache
void ClearCursorCache()
Queue a request for cursor cache clear.
Definition: opengl.cpp:1138
SpriteLoader::CommonPixel::b
uint8 b
Blue-channel.
Definition: spriteloader.hpp:37
SpriteLoader::Sprite::width
uint16 width
Width of the sprite.
Definition: spriteloader.hpp:50
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition: sprites.h:1600
OpenGLSprite::Update
void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data)
Update a single mip-map level with new pixel data.
Definition: opengl.cpp:1475
IsEmptyRect
static bool IsEmptyRect(const Rect &r)
Check if a rectangle is empty.
Definition: geometry_func.hpp:22
_frag_shader_sprite_blend
static const char * _frag_shader_sprite_blend[]
Fragment shader that performs a palette lookup to read the colour from a sprite texture.
Definition: opengl_shader.h:151
OpenGLBackend::pal_texture
GLuint pal_texture
Palette lookup texture.
Definition: opengl.h:45
Colour::a
uint8 a
colour channels in LE order
Definition: gfx_type.h:171
FindStringInExtensionList
const char * FindStringInExtensionList(const char *string, const char *substring)
Find a substring in a string made of space delimited elements.
Definition: opengl.cpp:148
GetRawSprite
void * GetRawSprite(SpriteID sprite, SpriteType type, AllocatorProc *allocator, SpriteEncoder *encoder)
Reads a sprite (from disk or sprite cache).
Definition: spritecache.cpp:905
ReusableBuffer::GetBuffer
const T * GetBuffer() const
Get the currently allocated buffer.
Definition: alloc_type.hpp:75
OpenGLBackend::cursor_sprite_count
uint cursor_sprite_count
Number of cursor sprites to draw.
Definition: opengl.h:72
PaletteID
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
OpenGLBackend::InternalClearCursorCache
void InternalClearCursorCache()
Clear all cached cursor sprites.
Definition: opengl.cpp:1125
OpenGLBackend::sprite_sprite_loc
GLint sprite_sprite_loc
Uniform location for sprite parameters.
Definition: opengl.h:58
Sprite::y_offs
int16 y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:21
opengl.h
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition: spriteloader.hpp:48
OpenGLSprite::BindTextures
bool BindTextures()
Bind textures for rendering this sprite.
Definition: opengl.cpp:1532
OpenGLBackend::Paint
void Paint()
Render video buffer to the screen.
Definition: opengl.cpp:1036
OpenGLBackend::remap_sprite_loc
GLint remap_sprite_loc
Uniform location for sprite parameters.
Definition: opengl.h:52
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
CursorVars::sprite_seq
PalSpriteID sprite_seq[16]
current image of cursor
Definition: gfx_type.h:128
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:378
OpenGLBackend::UpdatePalette
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition: opengl.cpp:1022
OpenGLBackend::sprite_program
GLuint sprite_program
Shader program for blending and rendering a sprite to the video buffer.
Definition: opengl.h:57
OpenGLBackend::sprite_crash_loc
GLint sprite_crash_loc
Uniform location for crash remap mode flag;.
Definition: opengl.h:62
MemSetT
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
OpenGLSprite
Class that encapsulates a RGBA texture together with a paletted remap texture.
Definition: opengl.h:117
SpriteLoader::CommonPixel::g
uint8 g
Green-channel.
Definition: spriteloader.hpp:36
SpriteLoader::CommonPixel::a
uint8 a
Alpha-channel.
Definition: spriteloader.hpp:38
OpenGLBackend::clear_cursor_cache
bool clear_cursor_cache
A clear of the cursor cache is pending.
Definition: opengl.h:66
OpenGLBackend::OpenGLBackend
OpenGLBackend()
Construct OpenGL back-end class.
Definition: opengl.cpp:493
_frag_shader_rgb_mask_blend
static const char * _frag_shader_rgb_mask_blend[]
Fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
Definition: opengl_shader.h:104
ST_RECOLOUR
@ ST_RECOLOUR
Recolour sprite.
Definition: gfx_type.h:305
SpriteLoader::Sprite::y_offs
int16 y_offs
The y-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:52
SpriteLoader::Sprite::height
uint16 height
Height of the sprite.
Definition: spriteloader.hpp:49
OpenGLBackend::sprite_rgb_loc
GLint sprite_rgb_loc
Uniform location for RGB mode flag;.
Definition: opengl.h:61
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
_frag_shader_direct
static const char * _frag_shader_direct[]
Fragment shader that reads the fragment colour from a 32bpp texture.
Definition: opengl_shader.h:43
SimpleSpriteAlloc
void * SimpleSpriteAlloc(size_t size)
Sprite allocator simply using malloc.
Definition: spritecache.cpp:846
OpenGLBackend::remap_zoom_loc
GLint remap_zoom_loc
Uniform location for sprite zoom;.
Definition: opengl.h:54
OpenGLBackend::remap_screen_loc
GLint remap_screen_loc
Uniform location for screen size;.
Definition: opengl.h:53
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
CursorVars::pos
Point pos
logical mouse position
Definition: gfx_type.h:117
OpenGLBackend::vid_pbo
GLuint vid_pbo
Pixel buffer object storing the memory used for the video driver to draw to.
Definition: opengl.h:39
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
CursorVars::in_window
bool in_window
mouse inside this window, determines drawing logic
Definition: gfx_type.h:141
OpenGLBackend::ReleaseAnimBuffer
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
Definition: opengl.cpp:1238
OpenGLBackend::remap_program
GLuint remap_program
Shader program for blending and rendering a RGBA + remap texture.
Definition: opengl.h:51
OpenGLBackend::Init
const char * Init(const Dimension &screen_res)
Check for the needed OpenGL functionality and allocate all resources.
Definition: opengl.cpp:529
OpenGLBackend::vid_program
GLuint vid_program
Shader program for rendering a RGBA video buffer.
Definition: opengl.h:41
OpenGLBackend::Resize
bool Resize(int w, int h, bool force=false)
Change the size of the drawing window and allocate matching resources.
Definition: opengl.cpp:907
OpenGLBackend::GetAnimBuffer
uint8 * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition: opengl.cpp:1173
OpenGLBackend::Encode
Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override
Convert a sprite from the loader to our own format.
Definition: opengl.cpp:1268
IsOpenGLVersionAtLeast
bool IsOpenGLVersionAtLeast(byte major, byte minor)
Check if the current OpenGL version is equal or higher than a given one.
Definition: opengl.cpp:211
OpenGLBackend::cursor_sprite_pos
Point cursor_sprite_pos[16]
Relative position of individual cursor sprites.
Definition: opengl.h:71
OpenGLBackend::InitShaders
bool InitShaders()
Create all needed shader programs.
Definition: opengl.cpp:798
SpriteColourComponent
SpriteColourComponent
The different colour components a sprite can have.
Definition: spriteloader.hpp:22