21#define GL_GLEXT_PROTOTYPES
23# define GL_SILENCE_DEPRECATION
24# include <OpenGL/gl3.h>
28#include "../3rdparty/opengl/glext.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"
37#include "../blitter/factory.hpp"
38#include "../zoom_func.h"
40#include "../table/opengl_shader.h"
41#include "../table/sprites.h"
44#include "../safeguards.h"
49#define GL(function) static decltype(&function) _ ## function
54GL(glDebugMessageControl);
55GL(glDebugMessageCallback);
84GL(glClearBufferSubData);
93GL(glDeleteVertexArrays);
101GL(glGetProgramInfoLog);
108GL(glGetShaderInfoLog);
109GL(glGetUniformLocation);
115GL(glGetAttribLocation);
116GL(glEnableVertexAttribArray);
117GL(glDisableVertexAttribArray);
118GL(glVertexAttribPointer);
119GL(glBindFragDataLocation);
131static const int MAX_CACHED_CURSORS = 48;
135GetOGLProcAddressProc GetOGLProcAddress;
144const char *FindStringInExtensionList(
const char *
string,
const char *substring)
148 const char *pos = strstr(
string, substring);
149 if (pos ==
nullptr)
break;
153 const char *end = pos + strlen(substring);
154 if ((pos ==
string || pos[-1] ==
' ') && (*end ==
' ' || *end ==
'\0'))
return pos;
168static bool IsOpenGLExtensionSupported(
const char *extension)
170 static PFNGLGETSTRINGIPROC glGetStringi =
nullptr;
171 static bool glGetStringi_loaded =
false;
175 if (!glGetStringi_loaded) {
176 if (IsOpenGLVersionAtLeast(3, 0)) glGetStringi = (PFNGLGETSTRINGIPROC)GetOGLProcAddress(
"glGetStringi");
177 glGetStringi_loaded =
true;
180 if (glGetStringi !=
nullptr) {
183 _glGetIntegerv(GL_NUM_EXTENSIONS, &num_exts);
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;
191 return FindStringInExtensionList((
const char *)_glGetString(GL_EXTENSIONS), extension) !=
nullptr;
197static uint8_t _gl_major_ver = 0;
198static uint8_t _gl_minor_ver = 0;
207bool IsOpenGLVersionAtLeast(uint8_t major, uint8_t minor)
209 return (_gl_major_ver > major) || (_gl_major_ver == major && _gl_minor_ver >= minor);
220static bool BindGLProc(F &f,
const char *name)
222 f =
reinterpret_cast<F
>(GetOGLProcAddress(name));
227static bool BindBasicInfoProcs()
229 if (!BindGLProc(_glGetString,
"glGetString"))
return false;
230 if (!BindGLProc(_glGetIntegerv,
"glGetIntegerv"))
return false;
231 if (!BindGLProc(_glGetError,
"glGetError"))
return false;
237static bool BindBasicOpenGLProcs()
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;
260static bool BindTextureExtensions()
262 if (IsOpenGLVersionAtLeast(1, 3)) {
263 if (!BindGLProc(_glActiveTexture,
"glActiveTexture"))
return false;
265 if (!BindGLProc(_glActiveTexture,
"glActiveTextureARB"))
return false;
272static bool BindVBOExtension()
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;
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;
292 if (IsOpenGLVersionAtLeast(4, 3) || IsOpenGLExtensionSupported(
"GL_ARB_clear_buffer_object")) {
293 BindGLProc(_glClearBufferSubData,
"glClearBufferSubData");
295 _glClearBufferSubData =
nullptr;
302static bool BindVBAExtension()
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;
321static bool BindShaderExtensions()
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;
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;
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;
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;
375 if (IsOpenGLVersionAtLeast(3, 0)) {
376 BindGLProc(_glBindFragDataLocation,
"glBindFragDataLocation");
377 }
else if (IsOpenGLExtensionSupported(
"GL_EXT_gpu_shader4")) {
378 BindGLProc(_glBindFragDataLocation,
"glBindFragDataLocationEXT");
380 _glBindFragDataLocation =
nullptr;
387static bool BindPersistentBufferExtensions()
390 if (IsOpenGLVersionAtLeast(3, 0)) {
391 if (!BindGLProc(_glMapBufferRange,
"glMapBufferRange"))
return false;
393 if (IsOpenGLVersionAtLeast(4, 4) || IsOpenGLExtensionSupported(
"GL_ARB_buffer_storage")) {
394 if (!BindGLProc(_glBufferStorage,
"glBufferStorage"))
return false;
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;
408void 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)
411 const char *severity_str =
"";
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;
419 const char *type_str =
"Other";
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;
428 Debug(driver, 6,
"OpenGL: {} ({}) - {}", type_str, severity_str, message);
432void SetupDebugOutput()
434#ifndef NO_DEBUG_MESSAGES
435 if (_debug_driver_level < 6)
return;
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");
445 if (_glDebugMessageControl !=
nullptr && _glDebugMessageCallback !=
nullptr) {
447 _glEnable(GL_DEBUG_OUTPUT);
448 if (_debug_driver_level >= 8) _glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
450 _glDebugMessageCallback(&DebugOutputCallback,
nullptr);
452 _glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0,
nullptr, _debug_driver_level >= 9 ? GL_TRUE : GL_FALSE);
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);
471 GetOGLProcAddress = get_proc;
498 if (_glDeleteProgram !=
nullptr) {
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);
510 if (_glDeleteTextures !=
nullptr) {
527 if (!BindBasicInfoProcs())
return "OpenGL not supported";
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);
534 if (ver ==
nullptr || vend ==
nullptr || renderer ==
nullptr)
return "OpenGL not supported";
536 Debug(driver, 1,
"OpenGL driver: {} - {} ({})", vend, renderer, ver);
538#ifndef GL_ALLOW_SOFTWARE_RENDERER
541 if (strncmp(renderer,
"llvmpipe", 8) == 0 || strncmp(renderer,
"softpipe", 8) == 0)
return "Software renderer detected, not using OpenGL";
544 const char *minor = strchr(ver,
'.');
545 _gl_major_ver = atoi(ver);
546 _gl_minor_ver = minor !=
nullptr ? atoi(minor + 1) : 0;
554 if (!BindBasicOpenGLProcs())
return "Failed to bind basic OpenGL functions.";
561 if (!
IsOpenGLVersionAtLeast(2, 0) && !IsOpenGLExtensionSupported(
"GL_ARB_texture_non_power_of_two"))
return "Non-power-of-two textures not supported";
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";
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";
569 if (!
IsOpenGLVersionAtLeast(2, 1) && !IsOpenGLExtensionSupported(
"GL_ARB_pixel_buffer_object"))
return "Pixel buffer objects not supported";
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";
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";
579#ifndef NO_GL_BUFFER_SYNC
584 Debug(driver, 1,
"OpenGL claims to support persistent buffer mapping but doesn't export all functions, not using persistent mapping.");
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";
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";
599 Debug(driver, 2,
"OpenGL shading language version: {}, texture units = {}", (
const char *)_glGetString(GL_SHADING_LANGUAGE_VERSION), (
int)max_tex_units);
601 if (!this->
InitShaders())
return "Failed to initialize shaders";
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";
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";
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";
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");
643 _glUniform1i(tex_location, 0);
644 _glUniform1i(palette_location, 1);
646 _glUniform4f(sprite_location, 0.0f, 0.0f, 1.0f, 1.0f);
647 _glUniform2f(screen_location, 1.0f, 1.0f);
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");
655 _glUniform1i(tex_location, 0);
656 _glUniform1i(palette_location, 1);
657 _glUniform4f(sprite_location, 0.0f, 0.0f, 1.0f, 1.0f);
658 _glUniform2f(screen_location, 1.0f, 1.0f);
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");
669 _glUniform1i(tex_location, 0);
670 _glUniform1i(palette_location, 1);
671 _glUniform1i(remap_location, 2);
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");
684 _glUniform1i(tex_location, 0);
685 _glUniform1i(palette_location, 1);
686 _glUniform1i(remap_location, 2);
687 _glUniform1i(pal_location, 3);
691 _glGenBuffers(1, &this->
vid_pbo);
692 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->
vid_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";
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 },
708 _glGenVertexArrays(1, &this->
vao_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";
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);
724 _glBindVertexArray(0);
729 this->PrepareContext();
735void OpenGLBackend::PrepareContext()
737 _glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
738 _glDisable(GL_DEPTH_TEST);
741 _glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
744std::string OpenGLBackend::GetDriverName()
748 res +=
reinterpret_cast<const char *
>(_glGetString(GL_RENDERER));
750 res +=
reinterpret_cast<const char *
>(_glGetString(GL_VERSION));
759static bool VerifyShader(GLuint shader)
763 GLint result = GL_FALSE;
764 _glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
768 _glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
770 _glGetShaderInfoLog(shader, log_len,
nullptr, log_buf.
Allocate(log_len));
771 Debug(driver, result != GL_TRUE ? 0 : 2,
"{}", log_buf.GetBuffer());
774 return result == GL_TRUE;
782static bool VerifyProgram(GLuint program)
786 GLint result = GL_FALSE;
787 _glGetProgramiv(program, GL_LINK_STATUS, &result);
791 _glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
793 _glGetProgramInfoLog(program, log_len,
nullptr, log_buf.
Allocate(log_len));
794 Debug(driver, result != GL_TRUE ? 0 : 2,
"{}", log_buf.GetBuffer());
797 return result == GL_TRUE;
806 const char *ver = (
const char *)_glGetString(GL_SHADING_LANGUAGE_VERSION);
807 if (ver ==
nullptr)
return false;
809 int glsl_major = ver[0] -
'0';
810 int glsl_minor = ver[2] -
'0';
812 bool glsl_150 = (
IsOpenGLVersionAtLeast(3, 2) || glsl_major > 1 || (glsl_major == 1 && glsl_minor >= 5)) && _glBindFragDataLocation !=
nullptr;
815 GLuint vert_shader = _glCreateShader(GL_VERTEX_SHADER);
817 _glCompileShader(vert_shader);
818 if (!VerifyShader(vert_shader))
return false;
821 GLuint frag_shader_rgb = _glCreateShader(GL_FRAGMENT_SHADER);
823 _glCompileShader(frag_shader_rgb);
824 if (!VerifyShader(frag_shader_rgb))
return false;
827 GLuint frag_shader_pal = _glCreateShader(GL_FRAGMENT_SHADER);
829 _glCompileShader(frag_shader_pal);
830 if (!VerifyShader(frag_shader_pal))
return false;
833 GLuint remap_shader = _glCreateShader(GL_FRAGMENT_SHADER);
835 _glCompileShader(remap_shader);
836 if (!VerifyShader(remap_shader))
return false;
839 GLuint sprite_shader = _glCreateShader(GL_FRAGMENT_SHADER);
841 _glCompileShader(sprite_shader);
842 if (!VerifyShader(sprite_shader))
return false;
847 _glAttachShader(this->
vid_program, frag_shader_rgb);
851 _glAttachShader(this->
pal_program, frag_shader_pal);
863 _glBindFragDataLocation(this->
vid_program, 0,
"colour");
864 _glBindFragDataLocation(this->
pal_program, 0,
"colour");
870 if (!VerifyProgram(this->
vid_program))
return false;
873 if (!VerifyProgram(this->
pal_program))
return false;
881 _glDeleteShader(vert_shader);
882 _glDeleteShader(frag_shader_rgb);
883 _glDeleteShader(frag_shader_pal);
884 _glDeleteShader(remap_shader);
885 _glDeleteShader(sprite_shader);
897static void ClearPixelBuffer(
size_t len, T data)
899 T *buf =
reinterpret_cast<T *
>(_glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE));
900 for (
size_t i = 0; i < len; i++) {
903 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
915 if (!force && _screen.width == w && _screen.height == h)
return false;
918 int pitch =
Align(w, 4);
919 size_t line_pixel_count =
static_cast<size_t>(pitch) * h;
921 _glViewport(0, 0, w, h);
923 _glPixelStorei(GL_UNPACK_ROW_LENGTH, pitch);
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);
933 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->
vid_pbo);
934 _glBufferData(GL_PIXEL_UNPACK_BUFFER, line_pixel_count * bpp / 8,
nullptr, GL_DYNAMIC_DRAW);
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);
943 ClearPixelBuffer<uint32_t>(line_pixel_count, black.
data);
945 }
else if (bpp == 8) {
946 if (_glClearBufferSubData !=
nullptr) {
948 _glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, line_pixel_count, GL_RED, GL_UNSIGNED_BYTE, &b);
950 ClearPixelBuffer<uint8_t>(line_pixel_count, 0);
954 _glActiveTexture(GL_TEXTURE0);
957 _glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE,
nullptr);
959 _glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
nullptr);
961 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
967 _glDeleteBuffers(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);
972 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->
anim_pbo);
973 _glBufferData(GL_PIXEL_UNPACK_BUFFER, line_pixel_count,
nullptr, GL_DYNAMIC_DRAW);
977 if (_glClearBufferSubData !=
nullptr) {
979 _glClearBufferSubData(GL_PIXEL_UNPACK_BUFFER, GL_R8, 0, line_pixel_count, GL_RED, GL_UNSIGNED_BYTE, &b);
981 ClearPixelBuffer<uint8_t>(line_pixel_count, 0);
985 _glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE,
nullptr);
986 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
989 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->
anim_pbo);
990 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
991 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
997 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
999 _glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &dummy);
1002 _glBindTexture(GL_TEXTURE_2D, 0);
1007 _screen.pitch = pitch;
1008 _screen.dst_ptr =
nullptr;
1012 _glUniform2f(this->
remap_screen_loc, (
float)_screen.width, (
float)_screen.height);
1014 _glClear(GL_COLOR_BUFFER_BIT);
1027 assert(first + length <= 256);
1029 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1030 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1031 _glActiveTexture(GL_TEXTURE1);
1033 _glTexSubImage1D(GL_TEXTURE_1D, 0, first, length, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, pal + first);
1041 _glClear(GL_COLOR_BUFFER_BIT);
1043 _glDisable(GL_BLEND);
1046 _glActiveTexture(GL_TEXTURE0);
1048 _glActiveTexture(GL_TEXTURE1);
1052 _glActiveTexture(GL_TEXTURE2);
1062 _glBindVertexArray(this->
vao_quad);
1063 _glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1065 _glEnable(GL_BLEND);
1076 _cur_dpi = &_screen;
1090void OpenGLBackend::PopulateCursorCache()
1104 for (
const auto &sc : _cursor.sprites) {
1110 if (old !=
nullptr) {
1112 gl_sprite->~OpenGLSprite();
1127 sprite->~OpenGLSprite();
1150#ifndef NO_GL_BUFFER_SYNC
1156 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->
vid_pbo);
1157 this->
vid_buffer = _glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
1159 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->
vid_pbo);
1172 if (this->
anim_pbo == 0)
return nullptr;
1174#ifndef NO_GL_BUFFER_SYNC
1179 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->
anim_pbo);
1180 this->
anim_buffer = _glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE);
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);
1197 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->
vid_pbo);
1199 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1203#ifndef NO_GL_BUFFER_SYNC
1212 _glActiveTexture(GL_TEXTURE0);
1214 _glPixelStorei(GL_UNPACK_ROW_LENGTH, _screen.pitch);
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));
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));
1221#ifndef NO_GL_BUFFER_SYNC
1235 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->
anim_pbo);
1237 _glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1241#ifndef NO_GL_BUFFER_SYNC
1249 if (update_rect.left != update_rect.right) {
1250 _glActiveTexture(GL_TEXTURE0);
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));
1255#ifndef NO_GL_BUFFER_SYNC
1271 gl_sprite->
Update(sprite[i].width, sprite[i].height, i, sprite[i].data);
1293 _glActiveTexture(GL_TEXTURE0 + 1);
1297 _glActiveTexture(GL_TEXTURE0 + 3);
1298 if (pal != PAL_NONE) {
1303 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1306 _glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RED, GL_UNSIGNED_BYTE,
nullptr);
1308 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1319 _glUniform4f(this->
sprite_sprite_loc, (
float)x, (
float)y, (
float)dim.width, (
float)dim.height);
1321 _glUniform2f(this->
sprite_screen_loc, (
float)_screen.width, (
float)_screen.height);
1325 _glBindVertexArray(this->
vao_quad);
1326 _glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
1343 for (
int t =
TEX_RGBA; t < NUM_TEX; t++) {
1346 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
1347 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1348 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
1349 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1350 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1353 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1354 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1357 const Colour rgb_pixel(0, 0, 0);
1359 _glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1, 1, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &rgb_pixel);
1364 _glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 1, 1, 0, GL_RED, GL_UNSIGNED_BYTE, &pal);
1367 std::array<uint8_t, 256> identity_pal;
1368 std::iota(std::begin(identity_pal), std::end(identity_pal), 0);
1373 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1374 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1375 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
1376 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1377 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1378 _glTexImage1D(GL_TEXTURE_1D, 0, GL_R8, 256, 0, GL_RED, GL_UNSIGNED_BYTE, identity_pal.data());
1383 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1384 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1385 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);
1386 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1387 _glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1388 _glTexImage1D(GL_TEXTURE_1D, 0, GL_R8, 256, 0, GL_RED, GL_UNSIGNED_BYTE, identity_pal.data());
1393 _glBufferData(GL_PIXEL_UNPACK_BUFFER, 256, identity_pal.data(), GL_DYNAMIC_DRAW);
1394 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1396 return _glGetError() == GL_NO_ERROR;
1418 (void)_glGetError();
1420 this->dim.width = width;
1421 this->dim.height = height;
1424 _glActiveTexture(GL_TEXTURE0);
1425 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1427 for (
int t =
TEX_RGBA; t < NUM_TEX; t++) {
1433 _glGenTextures(1, &this->
tex[t]);
1434 _glBindTexture(GL_TEXTURE_2D, this->
tex[t]);
1436 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
1437 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1438 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, levels - 1);
1439 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1440 _glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1443 for (uint i = 0, w = width, h = height; i < levels; i++, w /= 2, h /= 2) {
1446 _glTexImage2D(GL_TEXTURE_2D, i, GL_R8, w, h, 0, GL_RED, GL_UNSIGNED_BYTE,
nullptr);
1448 _glTexImage2D(GL_TEXTURE_2D, i, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
nullptr);
1453 assert(_glGetError() == GL_NO_ERROR);
1456OpenGLSprite::~OpenGLSprite()
1458 _glDeleteTextures(NUM_TEX, this->
tex);
1473 _glActiveTexture(GL_TEXTURE0);
1474 _glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
1475 _glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1479 size_t size =
static_cast<size_t>(width) * height;
1481 for (
size_t i = 0; i < size; i++) {
1482 rgba[i].r = data[i].
r;
1483 rgba[i].g = data[i].
g;
1484 rgba[i].
b = data[i].
b;
1485 rgba[i].a = data[i].
a;
1488 _glBindTexture(GL_TEXTURE_2D, this->
tex[
TEX_RGBA]);
1489 _glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, rgba);
1494 size_t pitch =
Align(width, 4);
1496 uint8_t *pal = buf_pal.
Allocate(pitch * height);
1498 for (uint y = 0; y < height; y++, pal += pitch, row += width) {
1499 for (uint x = 0; x < width; x++) {
1505 _glTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, buf_pal.
GetBuffer());
1508 assert(_glGetError() == GL_NO_ERROR);
1528 _glActiveTexture(GL_TEXTURE0);
1530 _glActiveTexture(GL_TEXTURE0 + 2);
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
Tdata * Get(const Tkey key)
Get an item from the cache.
bool Contains(const Tkey key)
Test if a key is already contained in the cache.
Tdata * Pop()
Pop the least recently used item.
Tdata * Insert(const Tkey key, Tdata *item)
Insert a new data item with a specified key.
Platform-independent back-end class for OpenGL video drivers.
GLint sprite_rgb_loc
Uniform location for RGB mode flag.
void * anim_buffer
Pointer to the mapped animation buffer.
GLuint remap_program
Shader program for blending and rendering a RGBA + remap texture.
bool cursor_in_window
Cursor inside this window.
void Paint()
Render video buffer to the screen.
GLuint pal_program
Shader program for rendering a paletted video buffer.
void RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
Render a sprite to the back buffer.
std::vector< CursorSprite > cursor_sprites
Sprites comprising cursor.
GLint remap_screen_loc
Uniform location for screen size.
static OpenGLBackend * instance
Singleton instance pointer.
uint8_t * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
bool persistent_mapping_supported
Persistent pixel buffer mapping supported.
GLuint vid_texture
Texture handle for the video buffer texture.
GLuint vao_quad
Vertex array object storing the rendering state for the fullscreen quad.
GLint sprite_zoom_loc
Uniform location for sprite zoom.
GLint remap_zoom_loc
Uniform location for sprite zoom.
bool Resize(int w, int h, bool force=false)
Change the size of the drawing window and allocate matching resources.
OpenGLBackend()
Construct OpenGL back-end class.
static std::optional< std::string_view > Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
Create and initialize the singleton back-end class.
void * vid_buffer
Pointer to the mapped video buffer.
Sprite * Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
GLuint anim_texture
Texture handle for the animation buffer texture.
void InternalClearCursorCache()
Clear all cached cursor sprites.
GLuint sprite_program
Shader program for blending and rendering a sprite to the video buffer.
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
GLuint vid_pbo
Pixel buffer object storing the memory used for the video driver to draw to.
GLsync sync_anim_mapping
Sync object for the persistently mapped animation buffer.
~OpenGLBackend()
Free allocated resources.
GLuint vbo_quad
Vertex buffer with a fullscreen quad.
bool clear_cursor_cache
A clear of the cursor cache is pending.
GLint remap_rgb_loc
Uniform location for RGB mode flag.
void ClearCursorCache()
Queue a request for cursor cache clear.
GLuint vid_program
Shader program for rendering a RGBA video buffer.
GLuint pal_texture
Palette lookup texture.
GLint remap_sprite_loc
Uniform location for sprite parameters.
bool InitShaders()
Create all needed shader programs.
void DrawMouseCursor()
Draw mouse cursor on screen.
Point cursor_pos
Cursor position.
GLint sprite_crash_loc
Uniform location for crash remap mode flag.
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
GLint sprite_sprite_loc
Uniform location for sprite parameters.
PaletteID last_sprite_pal
Last uploaded remap palette.
GLuint anim_pbo
Pixel buffer object storing the memory used for the animation buffer.
LRUCache< SpriteID, Sprite > cursor_cache
Cache of encoded cursor sprites.
GLsync sync_vid_mapping
Sync object for the persistently mapped video buffer.
std::optional< std::string_view > Init(const Dimension &screen_res)
Check for the needed OpenGL functionality and allocate all resources.
static void Destroy()
Free resources and destroy singleton back-end class.
GLint sprite_screen_loc
Uniform location for screen size.
Class that encapsulates a RGBA texture together with a paletted remap texture.
OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components)
Create an OpenGL sprite with a palette remap part.
static GLuint dummy_tex[NUM_TEX]
1x1 dummy textures to substitute for unused sprite components.
bool BindTextures()
Bind textures for rendering this sprite.
Dimension GetSize(ZoomLevel level) const
Query the sprite size at a certain zoom level.
static bool Create()
Create all common resources for sprite rendering.
static GLuint pal_pbo
Pixel buffer object for remap upload.
GLuint tex[NUM_TEX]
The texture objects.
static GLuint pal_identity
Identity texture mapping.
static GLuint pal_tex
Texture for palette remap.
void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data)
Update a single mip-map level with new pixel data.
@ TEX_RGBA
RGBA texture part.
@ TEX_REMAP
Remap texture part.
static void Destroy()
Free all common resources for sprite rendering.
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
const T * GetBuffer() const
Get the currently allocated buffer.
T * Allocate(size_t count)
Get buffer of at least count times T.
SpriteAllocate that uses malloc to allocate memory.
Interface for something that can allocate memory for a sprite.
T * Allocate(size_t size)
Allocate memory for a sprite.
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
bool IsEmptyRect(const Rect &r)
Check if a rectangle is empty.
@ Recolour
Recolour sprite.
@ Font
A sprite used for fonts.
@ Normal
The most basic (normal) sprite.
uint32_t PaletteID
The number of the palette.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
void MemSetT(T *ptr, uint8_t value, size_t num=1)
Type-safe version of memset().
OpenGL video driver support.
bool IsOpenGLVersionAtLeast(uint8_t major, uint8_t minor)
Check if the current OpenGL version is equal or higher than a given one.
static const char * _vertex_shader_sprite[]
Vertex shader that positions a sprite on screen.
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.
static const char * _frag_shader_direct[]
Fragment shader that reads the fragment colour from a 32bpp texture.
static const char * _frag_shader_rgb_mask_blend[]
Fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
static const char * _frag_shader_palette[]
Fragment shader that performs a palette lookup to read the colour from an 8bpp texture.
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.
static const char * _frag_shader_direct_150[]
GLSL 1.50 fragment shader that reads the fragment colour from a 32bpp texture.
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.
static const char * _frag_shader_sprite_blend[]
Fragment shader that performs a palette lookup to read the colour from a sprite texture.
static const char * _vertex_shader_sprite_150[]
GLSL 1.50 vertex shader that positions a sprite on screen.
void * GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator, SpriteEncoder *encoder)
Reads a sprite (from disk or sprite cache).
SpriteColourComponent
The different colour components a sprite can have.
@ SCC_PAL
Sprite has palette data.
static constexpr uint8_t PALETTE_WIDTH
number of bits of the sprite containing the recolour palette
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
#define lengthof(array)
Return the length of an fixed size array.
Point pos
logical mouse position
bool in_window
mouse inside this window, determines drawing logic
Dimensions (a width and height) of a rectangle in 2D.
Specification of a rectangle with absolute coordinates of all edges.
A simple 2D vertex with just position and texture.
Definition of a common pixel in OpenTTD's realm.
Data structure describing a sprite.
uint16_t width
Width of the sprite.
uint16_t height
Height of the sprite.
int16_t y_offs
Number of pixels to shift the sprite downwards.
uint8_t data[]
Sprite data.
int16_t x_offs
Number of pixels to shift the sprite to the right.
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
uint32_t data
Conversion of the channel information to a 32 bit number.
uint8_t b
colour channels in BE order
int UnScaleByZoomLower(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN)
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN) When shifting right,...
ZoomLevel
All zoom levels we know.
@ ZOOM_LVL_END
End for iteration.
@ ZOOM_LVL_MIN
Minimum zoom level.