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

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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/math_func.hpp"
33#include "../gfx_func.h"
34#include "../debug.h"
35#include "../blitter/factory.hpp"
36#include "../zoom_func.h"
37#include "../core/string_consumer.hpp"
38
39#include "../table/opengl_shader.h"
40#include "../table/sprites.h"
41
42
43#include "../safeguards.h"
44
45
46/* Define function pointers of all OpenGL functions that we load dynamically. */
47
48#define GL(function) static decltype(&function) _ ## function
49
50GL(glGetString);
51GL(glGetIntegerv);
52GL(glGetError);
53GL(glDebugMessageControl);
54GL(glDebugMessageCallback);
55
56GL(glDisable);
57GL(glEnable);
58GL(glViewport);
59GL(glClear);
60GL(glClearColor);
61GL(glBlendFunc);
62GL(glDrawArrays);
63
64GL(glTexImage1D);
65GL(glTexImage2D);
66GL(glTexParameteri);
67GL(glTexSubImage1D);
68GL(glTexSubImage2D);
69GL(glBindTexture);
70GL(glDeleteTextures);
71GL(glGenTextures);
72GL(glPixelStorei);
73
74GL(glActiveTexture);
75
76GL(glGenBuffers);
77GL(glDeleteBuffers);
78GL(glBindBuffer);
79GL(glBufferData);
80GL(glBufferSubData);
81GL(glMapBuffer);
82GL(glUnmapBuffer);
83GL(glClearBufferSubData);
84
85GL(glBufferStorage);
86GL(glMapBufferRange);
87GL(glClientWaitSync);
88GL(glFenceSync);
89GL(glDeleteSync);
90
91GL(glGenVertexArrays);
92GL(glDeleteVertexArrays);
93GL(glBindVertexArray);
94
95GL(glCreateProgram);
96GL(glDeleteProgram);
97GL(glLinkProgram);
98GL(glUseProgram);
99GL(glGetProgramiv);
100GL(glGetProgramInfoLog);
101GL(glCreateShader);
102GL(glDeleteShader);
103GL(glShaderSource);
104GL(glCompileShader);
105GL(glAttachShader);
106GL(glGetShaderiv);
107GL(glGetShaderInfoLog);
108GL(glGetUniformLocation);
109GL(glUniform1i);
110GL(glUniform1f);
111GL(glUniform2f);
112GL(glUniform4f);
113
114GL(glGetAttribLocation);
115GL(glEnableVertexAttribArray);
116GL(glDisableVertexAttribArray);
117GL(glVertexAttribPointer);
118GL(glBindFragDataLocation);
119
120#undef GL
121
122
124 struct Simple2DVertex {
125 float x, y;
126 float u, v;
127};
128
130static const int MAX_CACHED_CURSORS = 48;
131
132/* static */ OpenGLBackend *OpenGLBackend::instance = nullptr;
133
134GetOGLProcAddressProc GetOGLProcAddress;
135
136static std::optional<std::string_view> GlGetString(GLenum name)
137{
138 auto str = reinterpret_cast<const char *>(_glGetString(name));
139 if (str == nullptr) return {};
140 return str;
141}
142
150 bool HasStringInExtensionList(std::string_view string, std::string_view substring)
151{
152 StringConsumer consumer{string};
153 while (consumer.AnyBytesLeft()) {
154 if (substring == consumer.ReadUntil(" ", StringConsumer::SKIP_ALL_SEPARATORS)) return true;
155 }
156
157 return false;
158}
159
165static bool IsOpenGLExtensionSupported(std::string_view extension)
166{
167 static PFNGLGETSTRINGIPROC glGetStringi = nullptr;
168 static bool glGetStringi_loaded = false;
169
170 /* Starting with OpenGL 3.0 the preferred API to get the extensions
171 * has changed. Try to load the required function once. */
172 if (!glGetStringi_loaded) {
173 if (IsOpenGLVersionAtLeast(3, 0)) glGetStringi = (PFNGLGETSTRINGIPROC)GetOGLProcAddress("glGetStringi");
174 glGetStringi_loaded = true;
175 }
176
177 if (glGetStringi != nullptr) {
178 /* New style: Each supported extension can be queried and compared independently. */
179 GLint num_exts;
180 _glGetIntegerv(GL_NUM_EXTENSIONS, &num_exts);
181
182 for (GLint i = 0; i < num_exts; i++) {
183 const char *entry = reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i));
184 if (entry != nullptr && entry == extension) return true;
185 }
186 } else if (auto str = GlGetString(GL_EXTENSIONS); str.has_value()) {
187 /* Old style: A single, space-delimited string for all extensions. */
188 return HasStringInExtensionList(*str, extension);
189 }
190
191 return false;
192}
193
194static uint8_t _gl_major_ver = 0;
195static uint8_t _gl_minor_ver = 0;
196
204 bool IsOpenGLVersionAtLeast(uint8_t major, uint8_t minor)
205{
206 return (_gl_major_ver > major) || (_gl_major_ver == major && _gl_minor_ver >= minor);
207}
208
216template <typename F>
217static bool BindGLProc(F &f, const char *name)
218{
219 f = reinterpret_cast<F>(GetOGLProcAddress(name));
220 return f != nullptr;
221}
222
224static bool BindBasicInfoProcs()
225{
226 if (!BindGLProc(_glGetString, "glGetString")) return false;
227 if (!BindGLProc(_glGetIntegerv, "glGetIntegerv")) return false;
228 if (!BindGLProc(_glGetError, "glGetError")) return false;
229
230 return true;
231}
232
234static bool BindBasicOpenGLProcs()
235{
236 if (!BindGLProc(_glDisable, "glDisable")) return false;
237 if (!BindGLProc(_glEnable, "glEnable")) return false;
238 if (!BindGLProc(_glViewport, "glViewport")) return false;
239 if (!BindGLProc(_glTexImage1D, "glTexImage1D")) return false;
240 if (!BindGLProc(_glTexImage2D, "glTexImage2D")) return false;
241 if (!BindGLProc(_glTexParameteri, "glTexParameteri")) return false;
242 if (!BindGLProc(_glTexSubImage1D, "glTexSubImage1D")) return false;
243 if (!BindGLProc(_glTexSubImage2D, "glTexSubImage2D")) return false;
244 if (!BindGLProc(_glBindTexture, "glBindTexture")) return false;
245 if (!BindGLProc(_glDeleteTextures, "glDeleteTextures")) return false;
246 if (!BindGLProc(_glGenTextures, "glGenTextures")) return false;
247 if (!BindGLProc(_glPixelStorei, "glPixelStorei")) return false;
248 if (!BindGLProc(_glClear, "glClear")) return false;
249 if (!BindGLProc(_glClearColor, "glClearColor")) return false;
250 if (!BindGLProc(_glBlendFunc, "glBlendFunc")) return false;
251 if (!BindGLProc(_glDrawArrays, "glDrawArrays")) return false;
252
253 return true;
254}
255
257static bool BindTextureExtensions()
258{
259 if (IsOpenGLVersionAtLeast(1, 3)) {
260 if (!BindGLProc(_glActiveTexture, "glActiveTexture")) return false;
261 } else {
262 if (!BindGLProc(_glActiveTexture, "glActiveTextureARB")) return false;
263 }
264
265 return true;
266}
267
269static bool BindVBOExtension()
270{
271 if (IsOpenGLVersionAtLeast(1, 5)) {
272 if (!BindGLProc(_glGenBuffers, "glGenBuffers")) return false;
273 if (!BindGLProc(_glDeleteBuffers, "glDeleteBuffers")) return false;
274 if (!BindGLProc(_glBindBuffer, "glBindBuffer")) return false;
275 if (!BindGLProc(_glBufferData, "glBufferData")) return false;
276 if (!BindGLProc(_glBufferSubData, "glBufferSubData")) return false;
277 if (!BindGLProc(_glMapBuffer, "glMapBuffer")) return false;
278 if (!BindGLProc(_glUnmapBuffer, "glUnmapBuffer")) return false;
279 } else {
280 if (!BindGLProc(_glGenBuffers, "glGenBuffersARB")) return false;
281 if (!BindGLProc(_glDeleteBuffers, "glDeleteBuffersARB")) return false;
282 if (!BindGLProc(_glBindBuffer, "glBindBufferARB")) return false;
283 if (!BindGLProc(_glBufferData, "glBufferDataARB")) return false;
284 if (!BindGLProc(_glBufferSubData, "glBufferSubDataARB")) return false;
285 if (!BindGLProc(_glMapBuffer, "glMapBufferARB")) return false;
286 if (!BindGLProc(_glUnmapBuffer, "glUnmapBufferARB")) return false;
287 }
288
289 if (IsOpenGLVersionAtLeast(4, 3) || IsOpenGLExtensionSupported("GL_ARB_clear_buffer_object")) {
290 BindGLProc(_glClearBufferSubData, "glClearBufferSubData");
291 } else {
292 _glClearBufferSubData = nullptr;
293 }
294
295 return true;
296}
297
299static bool BindVBAExtension()
300{
301 /* The APPLE and ARB variants have different semantics (that don't matter for us).
302 * Successfully getting pointers to one variant doesn't mean it is supported for
303 * the current context. Always check the extension strings as well. */
304 if (IsOpenGLVersionAtLeast(3, 0) || IsOpenGLExtensionSupported("GL_ARB_vertex_array_object")) {
305 if (!BindGLProc(_glGenVertexArrays, "glGenVertexArrays")) return false;
306 if (!BindGLProc(_glDeleteVertexArrays, "glDeleteVertexArrays")) return false;
307 if (!BindGLProc(_glBindVertexArray, "glBindVertexArray")) return false;
308 } else if (IsOpenGLExtensionSupported("GL_APPLE_vertex_array_object")) {
309 if (!BindGLProc(_glGenVertexArrays, "glGenVertexArraysAPPLE")) return false;
310 if (!BindGLProc(_glDeleteVertexArrays, "glDeleteVertexArraysAPPLE")) return false;
311 if (!BindGLProc(_glBindVertexArray, "glBindVertexArrayAPPLE")) return false;
312 }
313
314 return true;
315}
316
318static bool BindShaderExtensions()
319{
320 if (IsOpenGLVersionAtLeast(2, 0)) {
321 if (!BindGLProc(_glCreateProgram, "glCreateProgram")) return false;
322 if (!BindGLProc(_glDeleteProgram, "glDeleteProgram")) return false;
323 if (!BindGLProc(_glLinkProgram, "glLinkProgram")) return false;
324 if (!BindGLProc(_glUseProgram, "glUseProgram")) return false;
325 if (!BindGLProc(_glGetProgramiv, "glGetProgramiv")) return false;
326 if (!BindGLProc(_glGetProgramInfoLog, "glGetProgramInfoLog")) return false;
327 if (!BindGLProc(_glCreateShader, "glCreateShader")) return false;
328 if (!BindGLProc(_glDeleteShader, "glDeleteShader")) return false;
329 if (!BindGLProc(_glShaderSource, "glShaderSource")) return false;
330 if (!BindGLProc(_glCompileShader, "glCompileShader")) return false;
331 if (!BindGLProc(_glAttachShader, "glAttachShader")) return false;
332 if (!BindGLProc(_glGetShaderiv, "glGetShaderiv")) return false;
333 if (!BindGLProc(_glGetShaderInfoLog, "glGetShaderInfoLog")) return false;
334 if (!BindGLProc(_glGetUniformLocation, "glGetUniformLocation")) return false;
335 if (!BindGLProc(_glUniform1i, "glUniform1i")) return false;
336 if (!BindGLProc(_glUniform1f, "glUniform1f")) return false;
337 if (!BindGLProc(_glUniform2f, "glUniform2f")) return false;
338 if (!BindGLProc(_glUniform4f, "glUniform4f")) return false;
339
340 if (!BindGLProc(_glGetAttribLocation, "glGetAttribLocation")) return false;
341 if (!BindGLProc(_glEnableVertexAttribArray, "glEnableVertexAttribArray")) return false;
342 if (!BindGLProc(_glDisableVertexAttribArray, "glDisableVertexAttribArray")) return false;
343 if (!BindGLProc(_glVertexAttribPointer, "glVertexAttribPointer")) return false;
344 } else {
345 /* In the ARB extension programs and shaders are in the same object space. */
346 if (!BindGLProc(_glCreateProgram, "glCreateProgramObjectARB")) return false;
347 if (!BindGLProc(_glDeleteProgram, "glDeleteObjectARB")) return false;
348 if (!BindGLProc(_glLinkProgram, "glLinkProgramARB")) return false;
349 if (!BindGLProc(_glUseProgram, "glUseProgramObjectARB")) return false;
350 if (!BindGLProc(_glGetProgramiv, "glGetObjectParameterivARB")) return false;
351 if (!BindGLProc(_glGetProgramInfoLog, "glGetInfoLogARB")) return false;
352 if (!BindGLProc(_glCreateShader, "glCreateShaderObjectARB")) return false;
353 if (!BindGLProc(_glDeleteShader, "glDeleteObjectARB")) return false;
354 if (!BindGLProc(_glShaderSource, "glShaderSourceARB")) return false;
355 if (!BindGLProc(_glCompileShader, "glCompileShaderARB")) return false;
356 if (!BindGLProc(_glAttachShader, "glAttachObjectARB")) return false;
357 if (!BindGLProc(_glGetShaderiv, "glGetObjectParameterivARB")) return false;
358 if (!BindGLProc(_glGetShaderInfoLog, "glGetInfoLogARB")) return false;
359 if (!BindGLProc(_glGetUniformLocation, "glGetUniformLocationARB")) return false;
360 if (!BindGLProc(_glUniform1i, "glUniform1iARB")) return false;
361 if (!BindGLProc(_glUniform1f, "glUniform1fARB")) return false;
362 if (!BindGLProc(_glUniform2f, "glUniform2fARB")) return false;
363 if (!BindGLProc(_glUniform4f, "glUniform4fARB")) return false;
364
365 if (!BindGLProc(_glGetAttribLocation, "glGetAttribLocationARB")) return false;
366 if (!BindGLProc(_glEnableVertexAttribArray, "glEnableVertexAttribArrayARB")) return false;
367 if (!BindGLProc(_glDisableVertexAttribArray, "glDisableVertexAttribArrayARB")) return false;
368 if (!BindGLProc(_glVertexAttribPointer, "glVertexAttribPointerARB")) return false;
369 }
370
371 /* Bind functions only needed when using GLSL 1.50 shaders. */
372 if (IsOpenGLVersionAtLeast(3, 0)) {
373 BindGLProc(_glBindFragDataLocation, "glBindFragDataLocation");
374 } else if (IsOpenGLExtensionSupported("GL_EXT_gpu_shader4")) {
375 BindGLProc(_glBindFragDataLocation, "glBindFragDataLocationEXT");
376 } else {
377 _glBindFragDataLocation = nullptr;
378 }
379
380 return true;
381}
382
384static bool BindPersistentBufferExtensions()
385{
386 /* Optional functions for persistent buffer mapping. */
387 if (IsOpenGLVersionAtLeast(3, 0)) {
388 if (!BindGLProc(_glMapBufferRange, "glMapBufferRange")) return false;
389 }
390 if (IsOpenGLVersionAtLeast(4, 4) || IsOpenGLExtensionSupported("GL_ARB_buffer_storage")) {
391 if (!BindGLProc(_glBufferStorage, "glBufferStorage")) return false;
392 }
393#ifndef NO_GL_BUFFER_SYNC
394 if (IsOpenGLVersionAtLeast(3, 2) || IsOpenGLExtensionSupported("GL_ARB_sync")) {
395 if (!BindGLProc(_glClientWaitSync, "glClientWaitSync")) return false;
396 if (!BindGLProc(_glFenceSync, "glFenceSync")) return false;
397 if (!BindGLProc(_glDeleteSync, "glDeleteSync")) return false;
398 }
399#endif
400
401 return true;
402}
403
405void 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)
406{
407 /* Make severity human readable. */
408 std::string_view severity_str;
409 switch (severity) {
410 case GL_DEBUG_SEVERITY_HIGH: severity_str = "high"; break;
411 case GL_DEBUG_SEVERITY_MEDIUM: severity_str = "medium"; break;
412 case GL_DEBUG_SEVERITY_LOW: severity_str = "low"; break;
413 }
414
415 /* Make type human readable.*/
416 std::string_view type_str = "Other";
417 switch (type) {
418 case GL_DEBUG_TYPE_ERROR: type_str = "Error"; break;
419 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: type_str = "Deprecated"; break;
420 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: type_str = "Undefined behaviour"; break;
421 case GL_DEBUG_TYPE_PERFORMANCE: type_str = "Performance"; break;
422 case GL_DEBUG_TYPE_PORTABILITY: type_str = "Portability"; break;
423 }
424
425 Debug(driver, 6, "OpenGL: {} ({}) - {}", type_str, severity_str, message);
426}
427
429void SetupDebugOutput()
430{
431#ifndef NO_DEBUG_MESSAGES
432 if (_debug_driver_level < 6) return;
433
434 if (IsOpenGLVersionAtLeast(4, 3)) {
435 BindGLProc(_glDebugMessageControl, "glDebugMessageControl");
436 BindGLProc(_glDebugMessageCallback, "glDebugMessageCallback");
437 } else if (IsOpenGLExtensionSupported("GL_ARB_debug_output")) {
438 BindGLProc(_glDebugMessageControl, "glDebugMessageControlARB");
439 BindGLProc(_glDebugMessageCallback, "glDebugMessageCallbackARB");
440 }
441
442 if (_glDebugMessageControl != nullptr && _glDebugMessageCallback != nullptr) {
443 /* Enable debug output. As synchronous debug output costs performance, we only enable it with a high debug level. */
444 _glEnable(GL_DEBUG_OUTPUT);
445 if (_debug_driver_level >= 8) _glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
446
447 _glDebugMessageCallback(&DebugOutputCallback, nullptr);
448 /* Enable all messages on highest debug level.*/
449 _glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, _debug_driver_level >= 9 ? GL_TRUE : GL_FALSE);
450 /* Get debug messages for errors and undefined/deprecated behaviour. */
451 _glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR, GL_DONT_CARE, 0, nullptr, GL_TRUE);
452 _glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR, GL_DONT_CARE, 0, nullptr, GL_TRUE);
453 _glDebugMessageControl(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, GL_DONT_CARE, 0, nullptr, GL_TRUE);
454 }
455#endif
456}
457
464/* static */ std::optional<std::string_view> OpenGLBackend::Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
465{
466 if (OpenGLBackend::instance != nullptr) OpenGLBackend::Destroy();
467
468 GetOGLProcAddress = get_proc;
469
470 OpenGLBackend::instance = new OpenGLBackend();
471 return OpenGLBackend::instance->Init(screen_res);
472}
473
477/* static */ void OpenGLBackend::Destroy()
478{
479 delete OpenGLBackend::instance;
480 OpenGLBackend::instance = nullptr;
481}
482
486OpenGLBackend::OpenGLBackend() : cursor_cache(MAX_CACHED_CURSORS)
487{
488}
489
493OpenGLBackend::~OpenGLBackend()
494{
495 if (_glDeleteProgram != nullptr) {
496 _glDeleteProgram(this->remap_program);
497 _glDeleteProgram(this->vid_program);
498 _glDeleteProgram(this->pal_program);
499 _glDeleteProgram(this->sprite_program);
500 }
501 if (_glDeleteVertexArrays != nullptr) _glDeleteVertexArrays(1, &this->vao_quad);
502 if (_glDeleteBuffers != nullptr) {
503 _glDeleteBuffers(1, &this->vbo_quad);
504 _glDeleteBuffers(1, &this->vid_pbo);
505 _glDeleteBuffers(1, &this->anim_pbo);
506 }
507 if (_glDeleteTextures != nullptr) {
508 this->InternalClearCursorCache();
509 OpenGLSprite::Destroy();
510
511 _glDeleteTextures(1, &this->vid_texture);
512 _glDeleteTextures(1, &this->anim_texture);
513 _glDeleteTextures(1, &this->pal_texture);
514 }
515}
516
517static std::tuple<uint8_t, uint8_t> DecodeVersion(std::string_view ver)
518{
519 StringConsumer consumer{ver};
520 int major = consumer.ReadIntegerBase<uint8_t>(10);
521 if (consumer.ReadIf(".")) return {major, consumer.ReadIntegerBase<uint8_t>(10)};
522 return {major, 0};
523}
524
530std::optional<std::string_view> OpenGLBackend::Init(const Dimension &screen_res)
531{
532 if (!BindBasicInfoProcs()) return "OpenGL not supported";
533
534 /* Always query the supported OpenGL version as the current context might have changed. */
535 auto ver = GlGetString(GL_VERSION);
536 auto vend = GlGetString(GL_VENDOR);
537 auto renderer = GlGetString(GL_RENDERER);
538
539 if (!ver.has_value() || !vend.has_value() || !renderer.has_value()) return "OpenGL not supported";
540
541 Debug(driver, 1, "OpenGL driver: {} - {} ({})", *vend, *renderer, *ver);
542
543#ifndef GL_ALLOW_SOFTWARE_RENDERER
544 /* Don't use MESA software rendering backends as they are slower than
545 * just using a non-OpenGL video driver. */
546 if (renderer->starts_with("llvmpipe") || renderer->starts_with("softpipe")) return "Software renderer detected, not using OpenGL";
547#endif
548
549 std::tie(_gl_major_ver, _gl_minor_ver) = DecodeVersion(*ver);
550
551#ifdef _WIN32
552 /* Old drivers on Windows (especially if made by Intel) seem to be
553 * unstable, so cull the oldest stuff here. */
554 if (!IsOpenGLVersionAtLeast(3, 2)) return "Need at least OpenGL version 3.2 on Windows";
555#endif
556
557 if (!BindBasicOpenGLProcs()) return "Failed to bind basic OpenGL functions.";
558
559 SetupDebugOutput();
560
561 /* OpenGL 1.3 is the absolute minimum. */
562 if (!IsOpenGLVersionAtLeast(1, 3)) return "OpenGL version >= 1.3 required";
563 /* Check for non-power-of-two texture support. */
564 if (!IsOpenGLVersionAtLeast(2, 0) && !IsOpenGLExtensionSupported("GL_ARB_texture_non_power_of_two")) return "Non-power-of-two textures not supported";
565 /* Check for single element texture formats. */
566 if (!IsOpenGLVersionAtLeast(3, 0) && !IsOpenGLExtensionSupported("GL_ARB_texture_rg")) return "Single element texture formats not supported";
567 if (!BindTextureExtensions()) return "Failed to bind texture extension functions";
568 /* Check for vertex buffer objects. */
569 if (!IsOpenGLVersionAtLeast(1, 5) && !IsOpenGLExtensionSupported("ARB_vertex_buffer_object")) return "Vertex buffer objects not supported";
570 if (!BindVBOExtension()) return "Failed to bind VBO extension functions";
571 /* Check for pixel buffer objects. */
572 if (!IsOpenGLVersionAtLeast(2, 1) && !IsOpenGLExtensionSupported("GL_ARB_pixel_buffer_object")) return "Pixel buffer objects not supported";
573 /* Check for vertex array objects. */
574 if (!IsOpenGLVersionAtLeast(3, 0) && (!IsOpenGLExtensionSupported("GL_ARB_vertex_array_object") || !IsOpenGLExtensionSupported("GL_APPLE_vertex_array_object"))) return "Vertex array objects not supported";
575 if (!BindVBAExtension()) return "Failed to bind VBA extension functions";
576 /* Check for shader objects. */
577 if (!IsOpenGLVersionAtLeast(2, 0) && (!IsOpenGLExtensionSupported("GL_ARB_shader_objects") || !IsOpenGLExtensionSupported("GL_ARB_fragment_shader") || !IsOpenGLExtensionSupported("GL_ARB_vertex_shader"))) return "No shader support";
578 if (!BindShaderExtensions()) return "Failed to bind shader extension functions";
579 if (IsOpenGLVersionAtLeast(3, 2) && _glBindFragDataLocation == nullptr) return "OpenGL claims to support version 3.2 but doesn't have glBindFragDataLocation";
580
581 this->persistent_mapping_supported = IsOpenGLVersionAtLeast(3, 0) && (IsOpenGLVersionAtLeast(4, 4) || IsOpenGLExtensionSupported("GL_ARB_buffer_storage"));
582#ifndef NO_GL_BUFFER_SYNC
583 this->persistent_mapping_supported = this->persistent_mapping_supported && (IsOpenGLVersionAtLeast(3, 2) || IsOpenGLExtensionSupported("GL_ARB_sync"));
584#endif
585
586 if (this->persistent_mapping_supported && !BindPersistentBufferExtensions()) {
587 Debug(driver, 1, "OpenGL claims to support persistent buffer mapping but doesn't export all functions, not using persistent mapping.");
588 this->persistent_mapping_supported = false;
589 }
590 if (this->persistent_mapping_supported) Debug(driver, 3, "OpenGL: Using persistent buffer mapping");
591
592 /* Check maximum texture size against screen resolution. */
593 GLint max_tex_size = 0;
594 _glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex_size);
595 if (std::max(screen_res.width, screen_res.height) > (uint)max_tex_size) return "Max supported texture size is too small";
596
597 /* Check available texture units. */
598 GLint max_tex_units = 0;
599 _glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_tex_units);
600 if (max_tex_units < 4) return "Not enough simultaneous textures supported";
601
602 Debug(driver, 2, "OpenGL shading language version: {}, texture units = {}", GlGetString(GL_SHADING_LANGUAGE_VERSION).value_or("Unknown version"), max_tex_units);
603
604 if (!this->InitShaders()) return "Failed to initialize shaders";
605
606 /* Setup video buffer texture. */
607 _glGenTextures(1, &this->vid_texture);
608 _glBindTexture(GL_TEXTURE_2D, this->vid_texture);
609 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
610 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
611 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
612 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
613 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
614 _glBindTexture(GL_TEXTURE_2D, 0);
615 if (_glGetError() != GL_NO_ERROR) return "Can't generate video buffer texture";
616
617 /* Setup video buffer texture. */
618 _glGenTextures(1, &this->anim_texture);
619 _glBindTexture(GL_TEXTURE_2D, this->anim_texture);
620 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
621 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
622 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
623 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
624 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
625 _glBindTexture(GL_TEXTURE_2D, 0);
626 if (_glGetError() != GL_NO_ERROR) return "Can't generate animation buffer texture";
627
628 /* Setup palette texture. */
629 _glGenTextures(1, &this->pal_texture);
630 _glBindTexture(GL_TEXTURE_1D, this->pal_texture);
631 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
632 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
633 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
634 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
635 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
636 _glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA8, 256, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
637 _glBindTexture(GL_TEXTURE_1D, 0);
638 if (_glGetError() != GL_NO_ERROR) return "Can't generate palette lookup texture";
639
640 /* Bind uniforms in rendering shader program. */
641 GLint tex_location = _glGetUniformLocation(this->vid_program, "colour_tex");
642 GLint palette_location = _glGetUniformLocation(this->vid_program, "palette");
643 GLint sprite_location = _glGetUniformLocation(this->vid_program, "sprite");
644 GLint screen_location = _glGetUniformLocation(this->vid_program, "screen");
645 _glUseProgram(this->vid_program);
646 _glUniform1i(tex_location, 0); // Texture unit 0.
647 _glUniform1i(palette_location, 1); // Texture unit 1.
648 /* Values that result in no transform. */
649 _glUniform4f(sprite_location, 0.0f, 0.0f, 1.0f, 1.0f);
650 _glUniform2f(screen_location, 1.0f, 1.0f);
651
652 /* Bind uniforms in palette rendering shader program. */
653 tex_location = _glGetUniformLocation(this->pal_program, "colour_tex");
654 palette_location = _glGetUniformLocation(this->pal_program, "palette");
655 sprite_location = _glGetUniformLocation(this->pal_program, "sprite");
656 screen_location = _glGetUniformLocation(this->pal_program, "screen");
657 _glUseProgram(this->pal_program);
658 _glUniform1i(tex_location, 0); // Texture unit 0.
659 _glUniform1i(palette_location, 1); // Texture unit 1.
660 _glUniform4f(sprite_location, 0.0f, 0.0f, 1.0f, 1.0f);
661 _glUniform2f(screen_location, 1.0f, 1.0f);
662
663 /* Bind uniforms in remap shader program. */
664 tex_location = _glGetUniformLocation(this->remap_program, "colour_tex");
665 palette_location = _glGetUniformLocation(this->remap_program, "palette");
666 GLint remap_location = _glGetUniformLocation(this->remap_program, "remap_tex");
667 this->remap_sprite_loc = _glGetUniformLocation(this->remap_program, "sprite");
668 this->remap_screen_loc = _glGetUniformLocation(this->remap_program, "screen");
669 this->remap_zoom_loc = _glGetUniformLocation(this->remap_program, "zoom");
670 this->remap_rgb_loc = _glGetUniformLocation(this->remap_program, "rgb");
671 _glUseProgram(this->remap_program);
672 _glUniform1i(tex_location, 0); // Texture unit 0.
673 _glUniform1i(palette_location, 1); // Texture unit 1.
674 _glUniform1i(remap_location, 2); // Texture unit 2.
675
676 /* Bind uniforms in sprite shader program. */
677 tex_location = _glGetUniformLocation(this->sprite_program, "colour_tex");
678 palette_location = _glGetUniformLocation(this->sprite_program, "palette");
679 remap_location = _glGetUniformLocation(this->sprite_program, "remap_tex");
680 GLint pal_location = _glGetUniformLocation(this->sprite_program, "pal");
681 this->sprite_sprite_loc = _glGetUniformLocation(this->sprite_program, "sprite");
682 this->sprite_screen_loc = _glGetUniformLocation(this->sprite_program, "screen");
683 this->sprite_zoom_loc = _glGetUniformLocation(this->sprite_program, "zoom");
684 this->sprite_rgb_loc = _glGetUniformLocation(this->sprite_program, "rgb");
685 this->sprite_crash_loc = _glGetUniformLocation(this->sprite_program, "crash");
686 _glUseProgram(this->sprite_program);
687 _glUniform1i(tex_location, 0); // Texture unit 0.
688 _glUniform1i(palette_location, 1); // Texture unit 1.
689 _glUniform1i(remap_location, 2); // Texture unit 2.
690 _glUniform1i(pal_location, 3); // Texture unit 3.
691 (void)_glGetError(); // Clear errors.
692
693 /* Create pixel buffer object as video buffer storage. */
694 _glGenBuffers(1, &this->vid_pbo);
695 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
696 _glGenBuffers(1, &this->anim_pbo);
697 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
698 if (_glGetError() != GL_NO_ERROR) return "Can't allocate pixel buffer for video buffer";
699
700 /* Prime vertex buffer with a full-screen quad and store
701 * the corresponding state in a vertex array object. */
702 static const Simple2DVertex vert_array[] = {
703 /* x y u v */
704 { 1.f, -1.f, 1.f, 1.f },
705 { 1.f, 1.f, 1.f, 0.f },
706 { -1.f, -1.f, 0.f, 1.f },
707 { -1.f, 1.f, 0.f, 0.f },
708 };
709
710 /* Create VAO. */
711 _glGenVertexArrays(1, &this->vao_quad);
712 _glBindVertexArray(this->vao_quad);
713
714 /* Create and fill VBO. */
715 _glGenBuffers(1, &this->vbo_quad);
716 _glBindBuffer(GL_ARRAY_BUFFER, this->vbo_quad);
717 _glBufferData(GL_ARRAY_BUFFER, sizeof(vert_array), vert_array, GL_STATIC_DRAW);
718 if (_glGetError() != GL_NO_ERROR) return "Can't generate VBO for fullscreen quad";
719
720 /* Set vertex state. */
721 GLint loc_position = _glGetAttribLocation(this->vid_program, "position");
722 GLint colour_position = _glGetAttribLocation(this->vid_program, "colour_uv");
723 _glEnableVertexAttribArray(loc_position);
724 _glEnableVertexAttribArray(colour_position);
725 _glVertexAttribPointer(loc_position, 2, GL_FLOAT, GL_FALSE, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, x));
726 _glVertexAttribPointer(colour_position, 2, GL_FLOAT, GL_FALSE, sizeof(Simple2DVertex), (GLvoid *)offsetof(Simple2DVertex, u));
727 _glBindVertexArray(0);
728
729 /* Create resources for sprite rendering. */
730 if (!OpenGLSprite::Create()) return "Failed to create sprite rendering resources";
731
732 this->PrepareContext();
733 (void)_glGetError(); // Clear errors.
734
735 return std::nullopt;
736}
737
738void OpenGLBackend::PrepareContext()
739{
740 _glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
741 _glDisable(GL_DEPTH_TEST);
742 /* Enable alpha blending using the src alpha factor. */
743 _glEnable(GL_BLEND);
744 _glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
745}
746
747std::string OpenGLBackend::GetDriverName()
748{
749 auto renderer = GlGetString(GL_RENDERER);
750 auto version = GlGetString(GL_VERSION);
751 /* Skipping GL_VENDOR as it tends to be "obvious" from the renderer and version data, and just makes the string pointlessly longer */
752 return fmt::format("{}, {}", renderer.value_or("Unknown renderer"), version.value_or("Unknown version"));
753}
754
760static bool VerifyShader(GLuint shader)
761{
762 static ReusableBuffer<char> log_buf;
763
764 GLint result = GL_FALSE;
765 _glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
766
767 /* Output log if there is one. */
768 GLint log_len = 0;
769 _glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
770 if (log_len > 0) {
771 _glGetShaderInfoLog(shader, log_len, nullptr, log_buf.Allocate(log_len));
772 Debug(driver, result != GL_TRUE ? 0 : 2, "{}", log_buf.GetBuffer()); // Always print on failure.
773 }
774
775 return result == GL_TRUE;
776}
777
783static bool VerifyProgram(GLuint program)
784{
785 static ReusableBuffer<char> log_buf;
786
787 GLint result = GL_FALSE;
788 _glGetProgramiv(program, GL_LINK_STATUS, &result);
789
790 /* Output log if there is one. */
791 GLint log_len = 0;
792 _glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
793 if (log_len > 0) {
794 _glGetProgramInfoLog(program, log_len, nullptr, log_buf.Allocate(log_len));
795 Debug(driver, result != GL_TRUE ? 0 : 2, "{}", log_buf.GetBuffer()); // Always print on failure.
796 }
797
798 return result == GL_TRUE;
799}
800
805 bool OpenGLBackend::InitShaders()
806{
807 auto ver = GlGetString(GL_SHADING_LANGUAGE_VERSION);
808 if (!ver.has_value()) return false;
809
810 auto [glsl_major, glsl_minor] = DecodeVersion(*ver);
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
896template <class T>
897static 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 uint8_t b = 0;
948 _glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, line_pixel_count, GL_RED, GL_UNSIGNED_BYTE, &b);
949 } else {
950 ClearPixelBuffer<uint8_t>(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 uint8_t b = 0;
979 _glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, line_pixel_count, GL_RED, GL_UNSIGNED_BYTE, &b);
980 } else {
981 ClearPixelBuffer<uint8_t>(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 (const auto &cs : this->cursor_sprites) {
1078 /* Sprites are cached by PopulateCursorCache(). */
1079 if (this->cursor_cache.Contains(cs.image.sprite)) {
1080 const OpenGLSprite *spr = this->cursor_cache.Get(cs.image.sprite).get();
1081
1082 this->RenderOglSprite(spr, cs.image.pal,
1083 this->cursor_pos.x + cs.pos.x + UnScaleByZoom(spr->x_offs, _gui_zoom),
1084 this->cursor_pos.y + cs.pos.y + UnScaleByZoom(spr->y_offs, _gui_zoom),
1085 _gui_zoom);
1086 }
1087 }
1088}
1089
1090 class OpenGLSpriteAllocator : public SpriteAllocator {
1091public:
1092 OpenGLSpriteLRUCache &lru;
1093 SpriteID sprite;
1094
1095 OpenGLSpriteAllocator(OpenGLSpriteLRUCache &lru, SpriteID sprite) : lru(lru), sprite(sprite) {}
1096protected:
1097 void *AllocatePtr(size_t) override { NOT_REACHED(); }
1098};
1099
1100void OpenGLBackend::PopulateCursorCache()
1101{
1102 if (this->clear_cursor_cache) {
1103 /* We have a pending cursor cache clear to do first. */
1104 this->clear_cursor_cache = false;
1105 this->last_sprite_pal = (PaletteID)-1;
1106
1107 this->InternalClearCursorCache();
1108 }
1109
1110 this->cursor_pos = _cursor.pos;
1111 this->cursor_in_window = _cursor.in_window;
1112
1113 this->cursor_sprites.clear();
1114 for (const auto &sc : _cursor.sprites) {
1115 this->cursor_sprites.emplace_back(sc);
1116
1117 if (!this->cursor_cache.Contains(sc.image.sprite)) {
1118 OpenGLSpriteAllocator allocator(this->cursor_cache, sc.image.sprite);
1119 GetRawSprite(sc.image.sprite, SpriteType::Normal, &allocator, this);
1120 }
1121 }
1122}
1123
1127 void OpenGLBackend::InternalClearCursorCache()
1128{
1129 this->cursor_cache.Clear();
1130}
1131
1135 void OpenGLBackend::ClearCursorCache()
1136{
1137 /* If the game loop is threaded, this function might be called
1138 * from the game thread. As we can call OpenGL functions only
1139 * on the main thread, just set a flag that is handled the next
1140 * time we prepare the cursor cache for drawing. */
1141 this->clear_cursor_cache = true;
1142}
1143
1148 void *OpenGLBackend::GetVideoBuffer()
1149{
1150#ifndef NO_GL_BUFFER_SYNC
1151 if (this->sync_vid_mapping != nullptr) _glClientWaitSync(this->sync_vid_mapping, GL_SYNC_FLUSH_COMMANDS_BIT, 100000000); // 100ms timeout.
1152#endif
1153
1154 if (!this->persistent_mapping_supported) {
1155 assert(this->vid_buffer == nullptr);
1156 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
1157 this->vid_buffer = _glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
1158 } else if (this->vid_buffer == nullptr) {
1159 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
1160 this->vid_buffer = _glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, static_cast<GLsizeiptr>(_screen.pitch) * _screen.height * BlitterFactory::GetCurrentBlitter()->GetScreenDepth() / 8, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
1161 }
1162
1163 return this->vid_buffer;
1164}
1165
1170uint8_t *OpenGLBackend::GetAnimBuffer()
1171{
1172 if (this->anim_pbo == 0) return nullptr;
1173
1174#ifndef NO_GL_BUFFER_SYNC
1175 if (this->sync_anim_mapping != nullptr) _glClientWaitSync(this->sync_anim_mapping, GL_SYNC_FLUSH_COMMANDS_BIT, 100000000); // 100ms timeout.
1176#endif
1177
1178 if (!this->persistent_mapping_supported) {
1179 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
1180 this->anim_buffer = _glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
1181 } else if (this->anim_buffer == nullptr) {
1182 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
1183 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);
1184 }
1185
1186 return (uint8_t *)this->anim_buffer;
1187}
1188
1193 void OpenGLBackend::ReleaseVideoBuffer(const Rect &update_rect)
1194{
1195 assert(this->vid_pbo != 0);
1196
1197 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vid_pbo);
1198 if (!this->persistent_mapping_supported) {
1199 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1200 this->vid_buffer = nullptr;
1201 }
1202
1203#ifndef NO_GL_BUFFER_SYNC
1204 if (this->persistent_mapping_supported) {
1205 _glDeleteSync(this->sync_vid_mapping);
1206 this->sync_vid_mapping = nullptr;
1207 }
1208#endif
1209
1210 /* Update changed rect of the video buffer texture. */
1211 if (!IsEmptyRect(update_rect)) {
1212 _glActiveTexture(GL_TEXTURE0);
1213 _glBindTexture(GL_TEXTURE_2D, this->vid_texture);
1214 _glPixelStorei(GL_UNPACK_ROW_LENGTH, _screen.pitch);
1215 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 8) {
1216 _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));
1217 } else {
1218 _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));
1219 }
1220
1221#ifndef NO_GL_BUFFER_SYNC
1222 if (this->persistent_mapping_supported) this->sync_vid_mapping = _glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1223#endif
1224 }
1225}
1226
1231 void OpenGLBackend::ReleaseAnimBuffer(const Rect &update_rect)
1232{
1233 if (this->anim_pbo == 0) return;
1234
1235 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->anim_pbo);
1236 if (!this->persistent_mapping_supported) {
1237 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1238 this->anim_buffer = nullptr;
1239 }
1240
1241#ifndef NO_GL_BUFFER_SYNC
1242 if (this->persistent_mapping_supported) {
1243 _glDeleteSync(this->sync_anim_mapping);
1244 this->sync_anim_mapping = nullptr;
1245 }
1246#endif
1247
1248 /* Update changed rect of the video buffer texture. */
1249 if (update_rect.left != update_rect.right) {
1250 _glActiveTexture(GL_TEXTURE0);
1251 _glBindTexture(GL_TEXTURE_2D, this->anim_texture);
1252 _glPixelStorei(GL_UNPACK_ROW_LENGTH, _screen.pitch);
1253 _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));
1254
1255#ifndef NO_GL_BUFFER_SYNC
1256 if (this->persistent_mapping_supported) this->sync_anim_mapping = _glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1257#endif
1258 }
1259}
1260
1261/* virtual */ Sprite *OpenGLBackend::Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)
1262{
1263 /* This encoding is only called for mouse cursors. We don't need real sprites but OpenGLSprites to show as cursor. These need to be put in the LRU cache. */
1264 OpenGLSpriteAllocator &gl_allocator = static_cast<OpenGLSpriteAllocator&>(allocator);
1265 gl_allocator.lru.Insert(gl_allocator.sprite, std::make_unique<OpenGLSprite>(sprite_type, sprite));
1266
1267 return nullptr;
1268}
1269
1277 void OpenGLBackend::RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
1278{
1279 /* Set textures. */
1280 bool rgb = gl_sprite->BindTextures();
1281 _glActiveTexture(GL_TEXTURE0 + 1);
1282 _glBindTexture(GL_TEXTURE_1D, this->pal_texture);
1283
1284 /* Set palette remap. */
1285 _glActiveTexture(GL_TEXTURE0 + 3);
1286 if (pal != PAL_NONE) {
1287 _glBindTexture(GL_TEXTURE_1D, OpenGLSprite::pal_tex);
1288 if (pal != this->last_sprite_pal) {
1289 /* Different remap palette in use, update texture. */
1290 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, OpenGLSprite::pal_pbo);
1291 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1292
1293 _glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, 256, GetNonSprite(GB(pal, 0, PALETTE_WIDTH), SpriteType::Recolour) + 1);
1294 _glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RED, GL_UNSIGNED_BYTE, nullptr);
1295
1296 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1297
1298 this->last_sprite_pal = pal;
1299 }
1300 } else {
1301 _glBindTexture(GL_TEXTURE_1D, OpenGLSprite::pal_identity);
1302 }
1303
1304 /* Set up shader program. */
1305 Dimension dim = gl_sprite->GetSize(zoom);
1306 _glUseProgram(this->sprite_program);
1307 _glUniform4f(this->sprite_sprite_loc, (float)x, (float)y, (float)dim.width, (float)dim.height);
1308 _glUniform1f(this->sprite_zoom_loc, (float)zoom);
1309 _glUniform2f(this->sprite_screen_loc, (float)_screen.width, (float)_screen.height);
1310 _glUniform1i(this->sprite_rgb_loc, rgb ? 1 : 0);
1311 _glUniform1i(this->sprite_crash_loc, pal == PALETTE_CRASH ? 1 : 0);
1312
1313 _glBindVertexArray(this->vao_quad);
1314 _glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1315}
1316
1317
1318/* static */ std::array<GLuint, OpenGLSprite::NUM_TEX> OpenGLSprite::dummy_tex{};
1319/* static */ GLuint OpenGLSprite::pal_identity = 0;
1320/* static */ GLuint OpenGLSprite::pal_tex = 0;
1321/* static */ GLuint OpenGLSprite::pal_pbo = 0;
1322
1327/* static */ bool OpenGLSprite::Create()
1328{
1329 _glGenTextures(NUM_TEX, OpenGLSprite::dummy_tex.data());
1330
1331 for (int t = TEX_RGBA; t < NUM_TEX; t++) {
1332 _glBindTexture(GL_TEXTURE_2D, OpenGLSprite::dummy_tex[t]);
1333
1334 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
1335 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1336 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
1337 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1338 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1339 }
1340
1341 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1342 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1343
1344 /* Load dummy RGBA texture. */
1345 const Colour rgb_pixel(0, 0, 0);
1346 _glBindTexture(GL_TEXTURE_2D, OpenGLSprite::dummy_tex[TEX_RGBA]);
1347 _glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &rgb_pixel);
1348
1349 /* Load dummy remap texture. */
1350 const uint pal = 0;
1351 _glBindTexture(GL_TEXTURE_2D, OpenGLSprite::dummy_tex[TEX_REMAP]);
1352 _glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &pal);
1353
1354 /* Create palette remap textures. */
1355 std::array<uint8_t, 256> identity_pal;
1356 std::iota(std::begin(identity_pal), std::end(identity_pal), 0);
1357
1358 /* Permanent texture for identity remap. */
1359 _glGenTextures(1, &OpenGLSprite::pal_identity);
1360 _glBindTexture(GL_TEXTURE_1D, OpenGLSprite::pal_identity);
1361 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1362 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1363 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
1364 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1365 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1366 _glTexImage1D(GL_TEXTURE_1D, 0, GL_R8, 256, 0, GL_RED, GL_UNSIGNED_BYTE, identity_pal.data());
1367
1368 /* Dynamically updated texture for remaps. */
1369 _glGenTextures(1, &OpenGLSprite::pal_tex);
1370 _glBindTexture(GL_TEXTURE_1D, OpenGLSprite::pal_tex);
1371 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1372 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1373 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
1374 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1375 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1376 _glTexImage1D(GL_TEXTURE_1D, 0, GL_R8, 256, 0, GL_RED, GL_UNSIGNED_BYTE, identity_pal.data());
1377
1378 /* Pixel buffer for remap updates. */
1379 _glGenBuffers(1, &OpenGLSprite::pal_pbo);
1380 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, OpenGLSprite::pal_pbo);
1381 _glBufferData(GL_PIXEL_UNPACK_BUFFER, 256, identity_pal.data(), GL_DYNAMIC_DRAW);
1382 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1383
1384 return _glGetError() == GL_NO_ERROR;
1385}
1386
1388/* static */ void OpenGLSprite::Destroy()
1389{
1390 _glDeleteTextures(NUM_TEX, OpenGLSprite::dummy_tex.data());
1391 _glDeleteTextures(1, &OpenGLSprite::pal_identity);
1392 _glDeleteTextures(1, &OpenGLSprite::pal_tex);
1393 if (_glDeleteBuffers != nullptr) _glDeleteBuffers(1, &OpenGLSprite::pal_pbo);
1394}
1395
1400OpenGLSprite::OpenGLSprite(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite)
1401{
1402 const auto &root_sprite = sprite.Root();
1403 this->dim.width = root_sprite.width;
1404 this->dim.height = root_sprite.height;
1405 this->x_offs = root_sprite.x_offs;
1406 this->y_offs = root_sprite.y_offs;
1407
1408 int levels = sprite_type == SpriteType::Font ? 1 : to_underlying(ZoomLevel::End);
1409 assert(levels > 0);
1410 (void)_glGetError();
1411
1412 this->tex = {};
1413 _glActiveTexture(GL_TEXTURE0);
1414 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1415
1416 for (int t = TEX_RGBA; t < NUM_TEX; t++) {
1417 /* Sprite component present? */
1418 if (t == TEX_RGBA && root_sprite.colours == SpriteComponent::Palette) continue;
1419 if (t == TEX_REMAP && !root_sprite.colours.Test(SpriteComponent::Palette)) continue;
1420
1421 /* Allocate texture. */
1422 _glGenTextures(1, &this->tex[t]);
1423 _glBindTexture(GL_TEXTURE_2D, this->tex[t]);
1424
1425 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
1426 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1427 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, levels - 1);
1428 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1429 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1430
1431 /* Set size. */
1432 for (int i = 0, w = this->dim.width, h = this->dim.height; i < levels; i++, w /= 2, h /= 2) {
1433 assert(w * h != 0);
1434 if (t == TEX_REMAP) {
1435 _glTexImage2D(GL_TEXTURE_2D, i, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
1436 } else {
1437 _glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
1438 }
1439 }
1440 }
1441
1442 /* Upload texture data. */
1443 for (ZoomLevel zoom = ZoomLevel::Min; zoom <= (sprite_type == SpriteType::Font ? ZoomLevel::Min : ZoomLevel::Max); ++zoom) {
1444 const auto &src_sprite = sprite[zoom];
1445 this->Update(src_sprite.width, src_sprite.height, to_underlying(zoom), src_sprite.data);
1446 }
1447
1448 assert(_glGetError() == GL_NO_ERROR);
1449}
1450
1451OpenGLSprite::~OpenGLSprite()
1452{
1453 _glDeleteTextures(NUM_TEX, this->tex.data());
1454}
1455
1463 void OpenGLSprite::Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel * data)
1464{
1465 static ReusableBuffer<Colour> buf_rgba;
1466 static ReusableBuffer<uint8_t> buf_pal;
1467
1468 _glActiveTexture(GL_TEXTURE0);
1469 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1470 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1471
1472 if (this->tex[TEX_RGBA] != 0) {
1473 /* Unpack pixel data */
1474 size_t size = static_cast<size_t>(width) * height;
1475 Colour *rgba = buf_rgba.Allocate(size);
1476 for (size_t i = 0; i < size; i++) {
1477 rgba[i].r = data[i].r;
1478 rgba[i].g = data[i].g;
1479 rgba[i].b = data[i].b;
1480 rgba[i].a = data[i].a;
1481 }
1482
1483 _glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA]);
1484 _glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, rgba);
1485 }
1486
1487 if (this->tex[TEX_REMAP] != 0) {
1488 /* Unpack and align pixel data. */
1489 size_t pitch = Align(width, 4);
1490
1491 uint8_t *pal = buf_pal.Allocate(pitch * height);
1492 const SpriteLoader::CommonPixel *row = data;
1493 for (uint y = 0; y < height; y++, pal += pitch, row += width) {
1494 for (uint x = 0; x < width; x++) {
1495 pal[x] = row[x].m;
1496 }
1497 }
1498
1499 _glBindTexture(GL_TEXTURE_2D, this->tex[TEX_REMAP]);
1500 _glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, buf_pal.GetBuffer());
1501 }
1502
1503 assert(_glGetError() == GL_NO_ERROR);
1504}
1505
1511 inline Dimension OpenGLSprite::GetSize(ZoomLevel level) const
1512{
1513 Dimension sd = { (uint)UnScaleByZoomLower(this->dim.width, level), (uint)UnScaleByZoomLower(this->dim.height, level) };
1514 return sd;
1515}
1516
1521 bool OpenGLSprite::BindTextures() const
1522{
1523 _glActiveTexture(GL_TEXTURE0);
1524 _glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA] != 0 ? this->tex[TEX_RGBA] : OpenGLSprite::dummy_tex[TEX_RGBA]);
1525 _glActiveTexture(GL_TEXTURE0 + 2);
1526 _glBindTexture(GL_TEXTURE_2D, this->tex[TEX_REMAP] != 0 ? this->tex[TEX_REMAP] : OpenGLSprite::dummy_tex[TEX_REMAP]);
1527
1528 return this->tex[TEX_RGBA] != 0;
1529}
GB
static constexpr 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
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:136
Blitter::GetScreenDepth
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
LRUCache< SpriteID, std::unique_ptr< OpenGLSprite > >
LRUCache::Insert
void Insert(const Tkey &key, Tdata &&item)
Insert a new data item with a specified key.
Definition lrucache.hpp:57
LRUCache::Clear
void Clear()
Clear the cache.
Definition lrucache.hpp:80
LRUCache::Get
const Tdata & Get(const Tkey &key)
Get an item from the cache.
Definition lrucache.hpp:92
LRUCache::Contains
bool Contains(const Tkey &key)
Test if a key is already contained in the cache.
Definition lrucache.hpp:47
OpenGLBackend
Platform-independent back-end class for OpenGL video drivers.
Definition opengl.h:29
OpenGLBackend::sprite_rgb_loc
GLint sprite_rgb_loc
Uniform location for RGB mode flag.
Definition opengl.h:60
OpenGLBackend::anim_buffer
void * anim_buffer
Pointer to the mapped animation buffer.
Definition opengl.h:46
OpenGLBackend::remap_program
GLuint remap_program
Shader program for blending and rendering a RGBA + remap texture.
Definition opengl.h:50
OpenGLBackend::cursor_in_window
bool cursor_in_window
Cursor inside this window.
Definition opengl.h:68
OpenGLBackend::Paint
void Paint()
Render video buffer to the screen.
Definition opengl.cpp:1039
OpenGLBackend::pal_program
GLuint pal_program
Shader program for rendering a paletted video buffer.
Definition opengl.h:41
OpenGLBackend::cursor_cache
OpenGLSpriteLRUCache cursor_cache
Cache of encoded cursor sprites.
Definition opengl.h:63
OpenGLBackend::cursor_sprites
std::vector< CursorSprite > cursor_sprites
Sprites comprising cursor.
Definition opengl.h:69
OpenGLBackend::remap_screen_loc
GLint remap_screen_loc
Uniform location for screen size.
Definition opengl.h:52
OpenGLBackend::instance
static OpenGLBackend * instance
Singleton instance pointer.
Definition opengl.h:31
OpenGLBackend::GetAnimBuffer
uint8_t * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition opengl.cpp:1170
OpenGLBackend::GetVideoBuffer
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition opengl.cpp:1148
OpenGLBackend::persistent_mapping_supported
bool persistent_mapping_supported
Persistent pixel buffer mapping supported.
Definition opengl.h:33
OpenGLBackend::vid_texture
GLuint vid_texture
Texture handle for the video buffer texture.
Definition opengl.h:39
OpenGLBackend::vao_quad
GLuint vao_quad
Vertex array object storing the rendering state for the fullscreen quad.
Definition opengl.h:42
OpenGLBackend::sprite_zoom_loc
GLint sprite_zoom_loc
Uniform location for sprite zoom.
Definition opengl.h:59
OpenGLBackend::remap_zoom_loc
GLint remap_zoom_loc
Uniform location for sprite zoom.
Definition opengl.h:53
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
OpenGLBackend::OpenGLBackend
OpenGLBackend()
Construct OpenGL back-end class.
Definition opengl.cpp:486
OpenGLBackend::Create
static std::optional< std::string_view > Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
Create and initialize the singleton back-end class.
Definition opengl.cpp:464
OpenGLBackend::vid_buffer
void * vid_buffer
Pointer to the mapped video buffer.
Definition opengl.h:37
OpenGLBackend::UpdatePalette
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition opengl.cpp:1025
OpenGLBackend::anim_texture
GLuint anim_texture
Texture handle for the animation buffer texture.
Definition opengl.h:48
OpenGLBackend::InternalClearCursorCache
void InternalClearCursorCache()
Clear all cached cursor sprites.
Definition opengl.cpp:1127
OpenGLBackend::Encode
Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
Definition opengl.cpp:1261
OpenGLBackend::sprite_program
GLuint sprite_program
Shader program for blending and rendering a sprite to the video buffer.
Definition opengl.h:56
OpenGLBackend::ReleaseAnimBuffer
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
Definition opengl.cpp:1231
OpenGLBackend::vid_pbo
GLuint vid_pbo
Pixel buffer object storing the memory used for the video driver to draw to.
Definition opengl.h:38
OpenGLBackend::sync_anim_mapping
GLsync sync_anim_mapping
Sync object for the persistently mapped animation buffer.
Definition opengl.h:35
OpenGLBackend::~OpenGLBackend
~OpenGLBackend()
Free allocated resources.
Definition opengl.cpp:493
OpenGLBackend::vbo_quad
GLuint vbo_quad
Vertex buffer with a fullscreen quad.
Definition opengl.h:43
OpenGLBackend::clear_cursor_cache
bool clear_cursor_cache
A clear of the cursor cache is pending.
Definition opengl.h:65
OpenGLBackend::remap_rgb_loc
GLint remap_rgb_loc
Uniform location for RGB mode flag.
Definition opengl.h:54
OpenGLBackend::ClearCursorCache
void ClearCursorCache()
Queue a request for cursor cache clear.
Definition opengl.cpp:1135
OpenGLBackend::vid_program
GLuint vid_program
Shader program for rendering a RGBA video buffer.
Definition opengl.h:40
OpenGLBackend::pal_texture
GLuint pal_texture
Palette lookup texture.
Definition opengl.h:44
OpenGLBackend::remap_sprite_loc
GLint remap_sprite_loc
Uniform location for sprite parameters.
Definition opengl.h:51
OpenGLBackend::InitShaders
bool InitShaders()
Create all needed shader programs.
Definition opengl.cpp:805
OpenGLBackend::DrawMouseCursor
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition opengl.cpp:1071
OpenGLBackend::cursor_pos
Point cursor_pos
Cursor position.
Definition opengl.h:67
OpenGLBackend::sprite_crash_loc
GLint sprite_crash_loc
Uniform location for crash remap mode flag.
Definition opengl.h:61
OpenGLBackend::ReleaseVideoBuffer
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition opengl.cpp:1193
OpenGLBackend::sprite_sprite_loc
GLint sprite_sprite_loc
Uniform location for sprite parameters.
Definition opengl.h:57
OpenGLBackend::RenderOglSprite
void RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
Render a sprite to the back buffer.
Definition opengl.cpp:1277
OpenGLBackend::last_sprite_pal
PaletteID last_sprite_pal
Last uploaded remap palette.
Definition opengl.h:64
OpenGLBackend::anim_pbo
GLuint anim_pbo
Pixel buffer object storing the memory used for the animation buffer.
Definition opengl.h:47
OpenGLBackend::sync_vid_mapping
GLsync sync_vid_mapping
Sync object for the persistently mapped video buffer.
Definition opengl.h:34
OpenGLBackend::Init
std::optional< std::string_view > Init(const Dimension &screen_res)
Check for the needed OpenGL functionality and allocate all resources.
Definition opengl.cpp:530
OpenGLBackend::Destroy
static void Destroy()
Free resources and destroy singleton back-end class.
Definition opengl.cpp:477
OpenGLBackend::sprite_screen_loc
GLint sprite_screen_loc
Uniform location for screen size.
Definition opengl.h:58
OpenGLSpriteAllocator
Definition opengl.cpp:1090
OpenGLSpriteAllocator::AllocatePtr
void * AllocatePtr(size_t) override
Allocate memory for a sprite.
Definition opengl.cpp:1097
OpenGLSprite
Class that encapsulates a RGBA texture together with a paletted remap texture.
Definition opengl.h:116
OpenGLSprite::tex
std::array< GLuint, NUM_TEX > tex
The texture objects.
Definition opengl.h:126
OpenGLSprite::BindTextures
bool BindTextures() const
Bind textures for rendering this sprite.
Definition opengl.cpp:1521
OpenGLSprite::OpenGLSprite
OpenGLSprite(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite)
Create an OpenGL sprite with a palette remap part.
Definition opengl.cpp:1400
OpenGLSprite::TEX_RGBA
@ TEX_RGBA
RGBA texture part.
Definition opengl.h:120
OpenGLSprite::TEX_REMAP
@ TEX_REMAP
Remap texture part.
Definition opengl.h:121
OpenGLSprite::GetSize
Dimension GetSize(ZoomLevel level) const
Query the sprite size at a certain zoom level.
Definition opengl.cpp:1511
OpenGLSprite::Create
static bool Create()
Create all common resources for sprite rendering.
Definition opengl.cpp:1327
OpenGLSprite::pal_pbo
static GLuint pal_pbo
Pixel buffer object for remap upload.
Definition opengl.h:134
OpenGLSprite::pal_identity
static GLuint pal_identity
Identity texture mapping.
Definition opengl.h:132
OpenGLSprite::pal_tex
static GLuint pal_tex
Texture for palette remap.
Definition opengl.h:133
OpenGLSprite::y_offs
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition opengl.h:128
OpenGLSprite::dummy_tex
static std::array< GLuint, NUM_TEX > dummy_tex
1x1 dummy textures to substitute for unused sprite components.
Definition opengl.h:1318
OpenGLSprite::x_offs
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition opengl.h:127
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:1463
OpenGLSprite::Destroy
static void Destroy()
Free all common resources for sprite rendering.
Definition opengl.cpp:1388
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:22
ReusableBuffer::GetBuffer
const T * GetBuffer() const
Get the currently allocated buffer.
Definition alloc_type.hpp:58
ReusableBuffer::Allocate
T * Allocate(size_t count)
Get buffer of at least count times T.
Definition alloc_type.hpp:34
SpriteAllocator
Interface for something that can allocate memory for a sprite.
Definition spriteloader.hpp:104
SpriteCollMap
Map zoom level to data.
Definition spriteloader.hpp:34
StringConsumer
Parse data from a string / buffer.
Definition string_consumer.hpp:27
StringConsumer::SKIP_ALL_SEPARATORS
@ SKIP_ALL_SEPARATORS
Read and discard all consecutive separators, do not include any in the result.
Definition string_consumer.hpp:697
StringConsumer::ReadIntegerBase
T ReadIntegerBase(int base, T def=0, bool clamp=false)
Read and parse an integer in number 'base', and advance the reader.
Definition string_consumer.hpp:891
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
to_underlying
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
IsEmptyRect
bool IsEmptyRect(const Rect &r)
Check if a rectangle is empty.
Definition geometry_func.hpp:22
_gui_zoom
ZoomLevel _gui_zoom
GUI Zoom level.
Definition gfx.cpp:61
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
SpriteType
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:357
SpriteType::Recolour
@ Recolour
Recolour sprite.
SpriteType::Font
@ Font
A sprite used for fonts.
SpriteType::Normal
@ Normal
The most basic (normal) sprite.
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
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
opengl.h
OpenGL video driver support.
IsOpenGLVersionAtLeast
bool IsOpenGLVersionAtLeast(uint8_t major, uint8_t minor)
Check if the current OpenGL version is equal or higher than a given one.
Definition opengl.cpp:204
_vertex_shader_sprite
static const char * _vertex_shader_sprite[]
Vertex shader that positions a sprite on screen.
Definition opengl_shader.h:11
_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
_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
_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
_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
_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
_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
_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
_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
_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
GetRawSprite
void * GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator, SpriteEncoder *encoder)
Reads a sprite (from disk or sprite cache).
Definition spritecache.cpp:853
SpriteComponent::Palette
@ Palette
Sprite has palette data.
PALETTE_WIDTH
static constexpr uint8_t PALETTE_WIDTH
number of bits of the sprite containing the recolour palette
Definition sprites.h:1548
PALETTE_CRASH
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition sprites.h:1619
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
CursorVars::pos
Point pos
logical mouse position
Definition gfx_type.h:125
CursorVars::in_window
bool in_window
mouse inside this window, determines drawing logic
Definition gfx_type.h:147
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
Simple2DVertex
A simple 2D vertex with just position and texture.
Definition opengl.cpp:124
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition spriteloader.hpp:48
SpriteLoader::CommonPixel::m
uint8_t m
Remap-channel.
Definition spriteloader.hpp:53
SpriteLoader::CommonPixel::b
uint8_t b
Blue-channel.
Definition spriteloader.hpp:51
SpriteLoader::CommonPixel::r
uint8_t r
Red-channel.
Definition spriteloader.hpp:49
SpriteLoader::CommonPixel::g
uint8_t g
Green-channel.
Definition spriteloader.hpp:50
SpriteLoader::CommonPixel::a
uint8_t a
Alpha-channel.
Definition spriteloader.hpp:52
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16
UnScaleByZoomLower
int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::Min)
Definition zoom_func.h:67
UnScaleByZoom
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:34
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
ZoomLevel::Max
@ Max
Maximum zoom level.
ZoomLevel::Min
@ Min
Minimum zoom level.
ZoomLevel::End
@ End
End for iteration.