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

OpenTTD Source 14.0-beta3

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