1#include <base/detect.h>
2
3#ifndef CONF_BACKEND_OPENGL_ES
4#include <GL/glew.h>
5#endif
6
7#include <base/log.h>
8#include <base/math.h>
9#include <base/tl/threading.h>
10
11#include <engine/shared/config.h>
12#include <engine/shared/localization.h>
13
14#include <SDL.h>
15#include <SDL_messagebox.h>
16#include <SDL_vulkan.h>
17
18#if defined(CONF_VIDEORECORDER)
19#include <engine/shared/video.h>
20#endif
21
22#include "backend_sdl.h"
23
24#if defined(CONF_HEADLESS_CLIENT)
25#include "backend/null/backend_null.h"
26#endif
27
28#if !defined(CONF_BACKEND_OPENGL_ES)
29#include "backend/opengl/backend_opengl3.h"
30#endif
31
32#if defined(CONF_BACKEND_OPENGL_ES3) || defined(CONF_BACKEND_OPENGL_ES)
33#include "backend/opengles/backend_opengles3.h"
34#endif
35
36#if defined(CONF_BACKEND_VULKAN)
37#include "backend/vulkan/backend_vulkan.h"
38#endif
39
40#include "graphics_threaded.h"
41
42#include <engine/graphics.h>
43
44#include <algorithm>
45#include <cstdlib>
46
47class IStorage;
48
49// ------------ CGraphicsBackend_Threaded
50
51// Run everything single threaded when compiling for Emscripten, as context binding does not work outside of the main thread with SDL2.
52// TODO SDL3: Check if SDL3 supports threaded graphics and PROXY_TO_PTHREAD, OFFSCREENCANVAS_SUPPORT and OFFSCREEN_FRAMEBUFFER correctly.
53#if !defined(CONF_PLATFORM_EMSCRIPTEN)
54void CGraphicsBackend_Threaded::ThreadFunc(void *pUser)
55{
56 auto *pSelf = (CGraphicsBackend_Threaded *)pUser;
57 std::unique_lock<std::mutex> Lock(pSelf->m_BufferSwapMutex);
58 // notify, that the thread started
59 pSelf->m_Started = true;
60 pSelf->m_BufferSwapCond.notify_all();
61 while(!pSelf->m_Shutdown)
62 {
63 pSelf->m_BufferSwapCond.wait(lock&: Lock, p: [&pSelf] { return pSelf->m_pBuffer != nullptr || pSelf->m_Shutdown; });
64 if(pSelf->m_pBuffer)
65 {
66#ifdef CONF_PLATFORM_MACOS
67 CAutoreleasePool AutoreleasePool;
68#endif
69 pSelf->m_pProcessor->RunBuffer(pBuffer: pSelf->m_pBuffer);
70
71 pSelf->m_pBuffer = nullptr;
72 pSelf->m_BufferInProcess.store(i: false, m: std::memory_order_relaxed);
73 pSelf->m_BufferSwapCond.notify_all();
74
75#if defined(CONF_VIDEORECORDER)
76 if(IVideo::Current())
77 IVideo::Current()->NextVideoFrameThread();
78#endif
79 }
80 }
81}
82#endif
83
84CGraphicsBackend_Threaded::CGraphicsBackend_Threaded(TTranslateFunc &&TranslateFunc) :
85 m_TranslateFunc(std::move(TranslateFunc))
86{
87 m_pProcessor = nullptr;
88 m_Shutdown = true;
89#if !defined(CONF_PLATFORM_EMSCRIPTEN)
90 m_pBuffer = nullptr;
91 m_BufferInProcess.store(i: false, m: std::memory_order_relaxed);
92#endif
93}
94
95void CGraphicsBackend_Threaded::StartProcessor(ICommandProcessor *pProcessor)
96{
97 dbg_assert(m_Shutdown, "Processor was already not shut down.");
98 m_Shutdown = false;
99 m_pProcessor = pProcessor;
100#if !defined(CONF_PLATFORM_EMSCRIPTEN)
101 std::unique_lock<std::mutex> Lock(m_BufferSwapMutex);
102 m_pThread = thread_init(threadfunc: ThreadFunc, user: this, name: "Graphics thread");
103 // wait for the thread to start
104 m_BufferSwapCond.wait(lock&: Lock, p: [this]() -> bool { return m_Started; });
105#endif
106}
107
108void CGraphicsBackend_Threaded::StopProcessor()
109{
110 dbg_assert(!m_Shutdown, "Processor was already shut down.");
111 m_Shutdown = true;
112#if defined(CONF_PLATFORM_EMSCRIPTEN)
113 m_Warning = m_pProcessor->GetWarning();
114#else
115 {
116 std::unique_lock<std::mutex> Lock(m_BufferSwapMutex);
117 m_Warning = m_pProcessor->GetWarning();
118 m_BufferSwapCond.notify_all();
119 }
120 thread_wait(thread: m_pThread);
121#endif
122}
123
124void CGraphicsBackend_Threaded::RunBuffer(CCommandBuffer *pBuffer)
125{
126 SGfxErrorContainer Error;
127#if defined(CONF_PLATFORM_EMSCRIPTEN)
128 Error = m_pProcessor->GetError();
129 if(Error.m_ErrorType == GFX_ERROR_TYPE_NONE)
130 {
131 RunBufferSingleThreadedUnsafe(pBuffer);
132#if defined(CONF_VIDEORECORDER)
133 if(IVideo::Current())
134 IVideo::Current()->NextVideoFrameThread();
135#endif
136 }
137#else
138 WaitForIdle();
139 {
140 std::unique_lock<std::mutex> Lock(m_BufferSwapMutex);
141 Error = m_pProcessor->GetError();
142 if(Error.m_ErrorType == GFX_ERROR_TYPE_NONE)
143 {
144 m_pBuffer = pBuffer;
145 m_BufferInProcess.store(i: true, m: std::memory_order_relaxed);
146 m_BufferSwapCond.notify_all();
147 }
148 }
149#endif
150
151 // Process error after lock is released to prevent deadlock
152 if(Error.m_ErrorType != GFX_ERROR_TYPE_NONE)
153 {
154 ProcessError(Error);
155 }
156}
157
158void CGraphicsBackend_Threaded::RunBufferSingleThreadedUnsafe(CCommandBuffer *pBuffer)
159{
160 m_pProcessor->RunBuffer(pBuffer);
161}
162
163bool CGraphicsBackend_Threaded::IsIdle() const
164{
165#if defined(CONF_PLATFORM_EMSCRIPTEN)
166 return true;
167#else
168 return !m_BufferInProcess.load(m: std::memory_order_relaxed);
169#endif
170}
171
172void CGraphicsBackend_Threaded::WaitForIdle()
173{
174#if !defined(CONF_PLATFORM_EMSCRIPTEN)
175 std::unique_lock<std::mutex> Lock(m_BufferSwapMutex);
176 m_BufferSwapCond.wait(lock&: Lock, p: [this]() { return m_pBuffer == nullptr; });
177#endif
178}
179
180void CGraphicsBackend_Threaded::ProcessError(const SGfxErrorContainer &Error)
181{
182 std::string VerboseStr = "Graphics Assertion:";
183 for(const auto &ErrStr : Error.m_vErrors)
184 {
185 VerboseStr.append(s: "\n");
186 if(ErrStr.m_RequiresTranslation)
187 VerboseStr.append(s: m_TranslateFunc(ErrStr.m_Err.c_str(), ""));
188 else
189 VerboseStr.append(str: ErrStr.m_Err);
190 }
191 dbg_assert_failed("%s", VerboseStr.c_str());
192}
193
194bool CGraphicsBackend_Threaded::GetWarning(std::vector<std::string> &WarningStrings)
195{
196 if(m_Warning.m_WarningType != GFX_WARNING_TYPE_NONE)
197 {
198 m_Warning.m_WarningType = GFX_WARNING_TYPE_NONE;
199 WarningStrings = m_Warning.m_vWarnings;
200 return true;
201 }
202 return false;
203}
204
205// ------------ CCommandProcessorFragment_General
206
207void CCommandProcessorFragment_General::Cmd_Signal(const CCommandBuffer::SCommand_Signal *pCommand)
208{
209 pCommand->m_pSemaphore->Signal();
210}
211
212bool CCommandProcessorFragment_General::RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
213{
214 switch(pBaseCommand->m_Cmd)
215 {
216 case CCommandBuffer::CMD_SIGNAL: Cmd_Signal(pCommand: static_cast<const CCommandBuffer::SCommand_Signal *>(pBaseCommand)); break;
217 default: return false;
218 }
219
220 return true;
221}
222
223// ------------ CCommandProcessorFragment_SDL
224void CCommandProcessorFragment_SDL::Cmd_Init(const SCommand_Init *pCommand)
225{
226 m_GLContext = pCommand->m_GLContext;
227 m_pWindow = pCommand->m_pWindow;
228 if(m_GLContext)
229 SDL_GL_MakeCurrent(window: m_pWindow, context: m_GLContext);
230}
231
232void CCommandProcessorFragment_SDL::Cmd_Shutdown(const SCommand_Shutdown *pCommand)
233{
234 if(m_GLContext)
235 SDL_GL_MakeCurrent(window: nullptr, context: nullptr);
236}
237
238void CCommandProcessorFragment_SDL::Cmd_Swap(const CCommandBuffer::SCommand_Swap *pCommand)
239{
240 if(m_GLContext)
241 SDL_GL_SwapWindow(window: m_pWindow);
242}
243
244void CCommandProcessorFragment_SDL::Cmd_VSync(const CCommandBuffer::SCommand_VSync *pCommand)
245{
246 if(m_GLContext)
247 {
248#if defined(CONF_PLATFORM_EMSCRIPTEN)
249 // SDL_GL_SetSwapInterval is not supported with Emscripten as this is only a wrapper for the
250 // emscripten_set_main_loop_timing function which does not work because we do not use the
251 // emscripten_set_main_loop function before.
252 *pCommand->m_pRetOk = !pCommand->m_VSync;
253#else
254 *pCommand->m_pRetOk = SDL_GL_SetSwapInterval(interval: pCommand->m_VSync) == 0;
255#endif
256 }
257}
258
259void CCommandProcessorFragment_SDL::Cmd_WindowCreateNtf(const CCommandBuffer::SCommand_WindowCreateNtf *pCommand)
260{
261 m_pWindow = SDL_GetWindowFromID(id: pCommand->m_WindowId);
262 // Android destroys windows when they are not visible, so we get the new one and work with that
263 // The graphic context does not need to be recreated, just unbound see @see SCommand_WindowDestroyNtf
264#ifdef CONF_PLATFORM_ANDROID
265 if(m_GLContext)
266 SDL_GL_MakeCurrent(m_pWindow, m_GLContext);
267#endif
268}
269
270void CCommandProcessorFragment_SDL::Cmd_WindowDestroyNtf(const CCommandBuffer::SCommand_WindowDestroyNtf *pCommand)
271{
272 // Unbind the graphic context from the window, so it does not get destroyed
273#ifdef CONF_PLATFORM_ANDROID
274 if(m_GLContext)
275 SDL_GL_MakeCurrent(NULL, NULL);
276#endif
277}
278
279CCommandProcessorFragment_SDL::CCommandProcessorFragment_SDL() = default;
280
281bool CCommandProcessorFragment_SDL::RunCommand(const CCommandBuffer::SCommand *pBaseCommand)
282{
283 switch(pBaseCommand->m_Cmd)
284 {
285 case CCommandBuffer::CMD_WINDOW_CREATE_NTF: Cmd_WindowCreateNtf(pCommand: static_cast<const CCommandBuffer::SCommand_WindowCreateNtf *>(pBaseCommand)); break;
286 case CCommandBuffer::CMD_WINDOW_DESTROY_NTF: Cmd_WindowDestroyNtf(pCommand: static_cast<const CCommandBuffer::SCommand_WindowDestroyNtf *>(pBaseCommand)); break;
287 case CCommandBuffer::CMD_SWAP: Cmd_Swap(pCommand: static_cast<const CCommandBuffer::SCommand_Swap *>(pBaseCommand)); break;
288 case CCommandBuffer::CMD_VSYNC: Cmd_VSync(pCommand: static_cast<const CCommandBuffer::SCommand_VSync *>(pBaseCommand)); break;
289 case CCommandBuffer::CMD_MULTISAMPLING: break;
290 case CMD_INIT: Cmd_Init(pCommand: static_cast<const SCommand_Init *>(pBaseCommand)); break;
291 case CMD_SHUTDOWN: Cmd_Shutdown(pCommand: static_cast<const SCommand_Shutdown *>(pBaseCommand)); break;
292 case CCommandProcessorFragment_GLBase::CMD_PRE_INIT: break;
293 case CCommandProcessorFragment_GLBase::CMD_POST_SHUTDOWN: break;
294 default: return false;
295 }
296
297 return true;
298}
299
300// ------------ CCommandProcessor_SDL_GL
301
302void CCommandProcessor_SDL_GL::HandleError()
303{
304 switch(m_Error.m_ErrorType)
305 {
306 case GFX_ERROR_TYPE_INIT:
307 m_Error.m_vErrors.emplace_back(args: SGfxErrorContainer::SError{.m_RequiresTranslation: true, .m_Err: Localizable(pStr: "Failed during initialization. Try to change gfx_backend to OpenGL or Vulkan in settings_ddnet.cfg in the config directory and try again.", pContext: "Graphics error")});
308 break;
309 case GFX_ERROR_TYPE_OUT_OF_MEMORY_IMAGE:
310 [[fallthrough]];
311 case GFX_ERROR_TYPE_OUT_OF_MEMORY_BUFFER:
312 [[fallthrough]];
313 case GFX_ERROR_TYPE_OUT_OF_MEMORY_STAGING:
314 m_Error.m_vErrors.emplace_back(args: SGfxErrorContainer::SError{.m_RequiresTranslation: true, .m_Err: Localizable(pStr: "Out of VRAM. Try setting 'cl_skins_loaded_max' to a lower value or remove custom assets (skins, entities, etc.), especially those with high resolution.", pContext: "Graphics error")});
315 break;
316 case GFX_ERROR_TYPE_RENDER_RECORDING:
317 m_Error.m_vErrors.emplace_back(args: SGfxErrorContainer::SError{.m_RequiresTranslation: true, .m_Err: Localizable(pStr: "An error during command recording occurred. Try to update your GPU drivers.", pContext: "Graphics error")});
318 break;
319 case GFX_ERROR_TYPE_RENDER_CMD_FAILED:
320 m_Error.m_vErrors.emplace_back(args: SGfxErrorContainer::SError{.m_RequiresTranslation: true, .m_Err: Localizable(pStr: "A render command failed. Try to update your GPU drivers.", pContext: "Graphics error")});
321 break;
322 case GFX_ERROR_TYPE_RENDER_SUBMIT_FAILED:
323 m_Error.m_vErrors.emplace_back(args: SGfxErrorContainer::SError{.m_RequiresTranslation: true, .m_Err: Localizable(pStr: "Submitting the render commands failed. Try to update your GPU drivers.", pContext: "Graphics error")});
324 break;
325 case GFX_ERROR_TYPE_SWAP_FAILED:
326 m_Error.m_vErrors.emplace_back(args: SGfxErrorContainer::SError{.m_RequiresTranslation: true, .m_Err: Localizable(pStr: "Failed to swap framebuffers. Try to update your GPU drivers.", pContext: "Graphics error")});
327 break;
328 case GFX_ERROR_TYPE_UNKNOWN:
329 [[fallthrough]];
330 default:
331 m_Error.m_vErrors.emplace_back(args: SGfxErrorContainer::SError{.m_RequiresTranslation: true, .m_Err: Localizable(pStr: "Unknown error. Try to change gfx_backend to OpenGL or Vulkan in settings_ddnet.cfg in the config directory and try again.", pContext: "Graphics error")});
332 break;
333 }
334}
335
336void CCommandProcessor_SDL_GL::HandleWarning()
337{
338 switch(m_Warning.m_WarningType)
339 {
340 case GFX_WARNING_TYPE_INIT_FAILED:
341 m_Warning.m_vWarnings.emplace_back(args: Localizable(pStr: "Could not initialize the given graphics backend, reverting to the default backend now.", pContext: "Graphics error"));
342 break;
343 case GFX_WARNING_TYPE_INIT_FAILED_MISSING_INTEGRATED_GPU_DRIVER:
344 m_Warning.m_vWarnings.emplace_back(args: Localizable(pStr: "Could not initialize the given graphics backend, this is probably because you didn't install the driver of the integrated graphics card.", pContext: "Graphics error"));
345 break;
346 case GFX_WARNING_MISSING_EXTENSION:
347 // ignore this warning for now
348 return;
349 case GFX_WARNING_LOW_ON_MEMORY:
350 // ignore this warning for now
351 return;
352 case GFX_WARNING_TYPE_INIT_FAILED_NO_DEVICE_WITH_REQUIRED_VERSION:
353 {
354 // Ignore this warning for now completely.
355 // A console message was already printed by the backend
356 m_Warning.m_WarningType = GFX_WARNING_TYPE_NONE;
357 m_Warning.m_vWarnings.clear();
358 return;
359 }
360 default:
361 dbg_assert_failed("Unhandled graphics warning type %d", (int)m_Warning.m_WarningType);
362 }
363}
364
365void CCommandProcessor_SDL_GL::RunBuffer(CCommandBuffer *pBuffer)
366{
367 m_pGLBackend->StartCommands(CommandCount: pBuffer->m_CommandCount, EstimatedRenderCallCount: pBuffer->m_RenderCallCount);
368
369 for(const CCommandBuffer::SCommand *pCommand = pBuffer->Head(); pCommand; pCommand = pCommand->m_pNext)
370 {
371 auto Res = m_pGLBackend->RunCommand(pBaseCommand: pCommand);
372 if(Res == ERunCommandReturnTypes::RUN_COMMAND_COMMAND_HANDLED)
373 {
374 continue;
375 }
376 else if(Res == ERunCommandReturnTypes::RUN_COMMAND_COMMAND_ERROR)
377 {
378 m_Error = m_pGLBackend->GetError();
379 HandleError();
380 return;
381 }
382 else if(Res == ERunCommandReturnTypes::RUN_COMMAND_COMMAND_WARNING)
383 {
384 m_Warning = m_pGLBackend->GetWarning();
385 HandleWarning();
386 return;
387 }
388
389 if(m_SDL.RunCommand(pBaseCommand: pCommand))
390 continue;
391
392 if(m_General.RunCommand(pBaseCommand: pCommand))
393 continue;
394
395 dbg_assert_failed("Unknown graphics command %d", pCommand->m_Cmd);
396 }
397
398 m_pGLBackend->EndCommands();
399}
400
401CCommandProcessor_SDL_GL::CCommandProcessor_SDL_GL(EBackendType BackendType, int GLMajor, int GLMinor, int GLPatch)
402{
403 m_BackendType = BackendType;
404
405#if defined(CONF_HEADLESS_CLIENT)
406 m_pGLBackend = new CCommandProcessorFragment_Null();
407#else
408 if(BackendType == BACKEND_TYPE_OPENGL_ES)
409 {
410#if defined(CONF_BACKEND_OPENGL_ES) || defined(CONF_BACKEND_OPENGL_ES3)
411 if(GLMajor < 3)
412 {
413 m_pGLBackend = new CCommandProcessorFragment_OpenGLES();
414 }
415 else
416 {
417 m_pGLBackend = new CCommandProcessorFragment_OpenGLES3();
418 }
419#endif
420 }
421 else if(BackendType == BACKEND_TYPE_OPENGL)
422 {
423#if !defined(CONF_BACKEND_OPENGL_ES)
424 if(GLMajor < 2)
425 {
426 m_pGLBackend = new CCommandProcessorFragment_OpenGL();
427 }
428 if(GLMajor == 2)
429 {
430 m_pGLBackend = new CCommandProcessorFragment_OpenGL2();
431 }
432 if(GLMajor == 3 && GLMinor == 0)
433 {
434 m_pGLBackend = new CCommandProcessorFragment_OpenGL3();
435 }
436 else if((GLMajor == 3 && GLMinor == 3) || GLMajor >= 4)
437 {
438 m_pGLBackend = new CCommandProcessorFragment_OpenGL3_3();
439 }
440#endif
441 }
442 else if(BackendType == BACKEND_TYPE_VULKAN)
443 {
444#if defined(CONF_BACKEND_VULKAN)
445 m_pGLBackend = CreateVulkanCommandProcessorFragment();
446#endif
447 }
448#endif
449}
450
451CCommandProcessor_SDL_GL::~CCommandProcessor_SDL_GL()
452{
453 delete m_pGLBackend;
454}
455
456const SGfxErrorContainer &CCommandProcessor_SDL_GL::GetError() const
457{
458 return m_Error;
459}
460
461void CCommandProcessor_SDL_GL::ErroneousCleanup()
462{
463 m_pGLBackend->ErroneousCleanup();
464}
465
466const SGfxWarningContainer &CCommandProcessor_SDL_GL::GetWarning() const
467{
468 return m_Warning;
469}
470
471// ------------ CGraphicsBackend_SDL_GL
472
473#if !defined(CONF_HEADLESS_CLIENT)
474static bool BackendInitGlew(EBackendType BackendType, int &GlewMajor, int &GlewMinor, int &GlewPatch)
475{
476 if(BackendType == BACKEND_TYPE_OPENGL)
477 {
478#if !defined(CONF_BACKEND_OPENGL_ES)
479 // Support graphic cards that are pretty old (and Linux)
480 glewExperimental = GL_TRUE;
481#ifdef CONF_GLEW_HAS_CONTEXT_INIT
482 const GLenum InitResult = glewContextInit();
483 if(InitResult != GLEW_OK)
484 {
485 log_error("gfx", "Unable to init glew (glewContextInit): %s", glewGetErrorString(InitResult));
486 return false;
487 }
488#else
489 const GLenum InitResult = glewInit();
490 if(InitResult != GLEW_OK)
491 {
492 // With wayland the glewInit function is allowed to fail with GLEW_ERROR_NO_GLX_DISPLAY,
493 // as it will already have initialized the context with glewContextInit internally.
494 const char *pVideoDriver = SDL_GetCurrentVideoDriver();
495 if(pVideoDriver == nullptr || str_comp(a: pVideoDriver, b: "wayland") != 0 || InitResult != GLEW_ERROR_NO_GLX_DISPLAY)
496 {
497 log_error("gfx", "Unable to init glew (glewInit): %s", glewGetErrorString(InitResult));
498 return false;
499 }
500 }
501#endif
502
503#ifdef GLEW_VERSION_4_6
504 if(GLEW_VERSION_4_6)
505 {
506 GlewMajor = 4;
507 GlewMinor = 6;
508 GlewPatch = 0;
509 return true;
510 }
511#endif
512#ifdef GLEW_VERSION_4_5
513 if(GLEW_VERSION_4_5)
514 {
515 GlewMajor = 4;
516 GlewMinor = 5;
517 GlewPatch = 0;
518 return true;
519 }
520#endif
521// Don't allow GL 3.3, if the driver doesn't support at least OpenGL 4.5
522#ifndef CONF_FAMILY_WINDOWS
523 if(GLEW_VERSION_4_4)
524 {
525 GlewMajor = 4;
526 GlewMinor = 4;
527 GlewPatch = 0;
528 return true;
529 }
530 if(GLEW_VERSION_4_3)
531 {
532 GlewMajor = 4;
533 GlewMinor = 3;
534 GlewPatch = 0;
535 return true;
536 }
537 if(GLEW_VERSION_4_2)
538 {
539 GlewMajor = 4;
540 GlewMinor = 2;
541 GlewPatch = 0;
542 return true;
543 }
544 if(GLEW_VERSION_4_1)
545 {
546 GlewMajor = 4;
547 GlewMinor = 1;
548 GlewPatch = 0;
549 return true;
550 }
551 if(GLEW_VERSION_4_0)
552 {
553 GlewMajor = 4;
554 GlewMinor = 0;
555 GlewPatch = 0;
556 return true;
557 }
558 if(GLEW_VERSION_3_3)
559 {
560 GlewMajor = 3;
561 GlewMinor = 3;
562 GlewPatch = 0;
563 return true;
564 }
565#endif
566 if(GLEW_VERSION_3_0)
567 {
568 GlewMajor = 3;
569 GlewMinor = 0;
570 GlewPatch = 0;
571 return true;
572 }
573 if(GLEW_VERSION_2_1)
574 {
575 GlewMajor = 2;
576 GlewMinor = 1;
577 GlewPatch = 0;
578 return true;
579 }
580 if(GLEW_VERSION_2_0)
581 {
582 GlewMajor = 2;
583 GlewMinor = 0;
584 GlewPatch = 0;
585 return true;
586 }
587 if(GLEW_VERSION_1_5)
588 {
589 GlewMajor = 1;
590 GlewMinor = 5;
591 GlewPatch = 0;
592 return true;
593 }
594 if(GLEW_VERSION_1_4)
595 {
596 GlewMajor = 1;
597 GlewMinor = 4;
598 GlewPatch = 0;
599 return true;
600 }
601 if(GLEW_VERSION_1_3)
602 {
603 GlewMajor = 1;
604 GlewMinor = 3;
605 GlewPatch = 0;
606 return true;
607 }
608 if(GLEW_VERSION_1_2_1)
609 {
610 GlewMajor = 1;
611 GlewMinor = 2;
612 GlewPatch = 1;
613 return true;
614 }
615 if(GLEW_VERSION_1_2)
616 {
617 GlewMajor = 1;
618 GlewMinor = 2;
619 GlewPatch = 0;
620 return true;
621 }
622 if(GLEW_VERSION_1_1)
623 {
624 GlewMajor = 1;
625 GlewMinor = 1;
626 GlewPatch = 0;
627 return true;
628 }
629#endif
630 }
631 else if(BackendType == BACKEND_TYPE_OPENGL_ES)
632 {
633 // just assume the version we need
634 GlewMajor = 3;
635 GlewMinor = 0;
636 GlewPatch = 0;
637 return true;
638 }
639 else
640 {
641 dbg_assert_failed("Invalid backend type for glew: %d", (int)BackendType);
642 }
643
644 return false;
645}
646
647static int IsVersionSupportedGlew(EBackendType BackendType, int VersionMajor, int VersionMinor, int VersionPatch, int GlewMajor, int GlewMinor, int GlewPatch)
648{
649 if(BackendType == BACKEND_TYPE_OPENGL)
650 {
651 if(VersionMajor >= 4 && GlewMajor < 4)
652 {
653 return -1;
654 }
655 else if(VersionMajor >= 3 && GlewMajor < 3)
656 {
657 return -1;
658 }
659 else if(VersionMajor == 3 && GlewMajor == 3)
660 {
661 if(VersionMinor >= 3 && GlewMinor < 3)
662 {
663 return -1;
664 }
665 if(VersionMinor >= 2 && GlewMinor < 2)
666 {
667 return -1;
668 }
669 if(VersionMinor >= 1 && GlewMinor < 1)
670 {
671 return -1;
672 }
673 if(VersionMinor >= 0 && GlewMinor < 0)
674 {
675 return -1;
676 }
677 }
678 else if(VersionMajor >= 2 && GlewMajor < 2)
679 {
680 return -1;
681 }
682 else if(VersionMajor == 2 && GlewMajor == 2)
683 {
684 if(VersionMinor >= 1 && GlewMinor < 1)
685 {
686 return -1;
687 }
688 if(VersionMinor >= 0 && GlewMinor < 0)
689 {
690 return -1;
691 }
692 }
693 else if(VersionMajor >= 1 && GlewMajor < 1)
694 {
695 return -1;
696 }
697 else if(VersionMajor == 1 && GlewMajor == 1)
698 {
699 if(VersionMinor >= 5 && GlewMinor < 5)
700 {
701 return -1;
702 }
703 if(VersionMinor >= 4 && GlewMinor < 4)
704 {
705 return -1;
706 }
707 if(VersionMinor >= 3 && GlewMinor < 3)
708 {
709 return -1;
710 }
711 if(VersionMinor >= 2 && GlewMinor < 2)
712 {
713 return -1;
714 }
715 else if(VersionMinor == 2 && GlewMinor == 2)
716 {
717 if(VersionPatch >= 1 && GlewPatch < 1)
718 {
719 return -1;
720 }
721 if(VersionPatch >= 0 && GlewPatch < 0)
722 {
723 return -1;
724 }
725 }
726 if(VersionMinor >= 1 && GlewMinor < 1)
727 {
728 return -1;
729 }
730 if(VersionMinor >= 0 && GlewMinor < 0)
731 {
732 return -1;
733 }
734 }
735 }
736 return 0;
737}
738#endif // !CONF_HEADLESS_CLIENT
739
740EBackendType CGraphicsBackend_SDL_GL::DetectBackend()
741{
742 EBackendType RetBackendType = BACKEND_TYPE_OPENGL;
743#if defined(CONF_BACKEND_VULKAN)
744 const char *pEnvDriver = SDL_getenv(name: "DDNET_DRIVER");
745 if(pEnvDriver && str_comp_nocase(a: pEnvDriver, b: "GLES") == 0)
746 RetBackendType = BACKEND_TYPE_OPENGL_ES;
747 else if(pEnvDriver && str_comp_nocase(a: pEnvDriver, b: "Vulkan") == 0)
748 RetBackendType = BACKEND_TYPE_VULKAN;
749 else if(pEnvDriver && str_comp_nocase(a: pEnvDriver, b: "OpenGL") == 0)
750 RetBackendType = BACKEND_TYPE_OPENGL;
751 else if(pEnvDriver == nullptr)
752 {
753 // load the config backend
754 const char *pConfBackend = g_Config.m_GfxBackend;
755 if(str_comp_nocase(a: pConfBackend, b: "GLES") == 0)
756 RetBackendType = BACKEND_TYPE_OPENGL_ES;
757 else if(str_comp_nocase(a: pConfBackend, b: "Vulkan") == 0)
758 RetBackendType = BACKEND_TYPE_VULKAN;
759 else if(str_comp_nocase(a: pConfBackend, b: "OpenGL") == 0)
760 RetBackendType = BACKEND_TYPE_OPENGL;
761 }
762#else
763 RetBackendType = BACKEND_TYPE_OPENGL;
764#endif
765#if !defined(CONF_BACKEND_OPENGL_ES) && !defined(CONF_BACKEND_OPENGL_ES3)
766 if(RetBackendType == BACKEND_TYPE_OPENGL_ES)
767 RetBackendType = BACKEND_TYPE_OPENGL;
768#elif defined(CONF_BACKEND_OPENGL_ES)
769 if(RetBackendType == BACKEND_TYPE_OPENGL)
770 RetBackendType = BACKEND_TYPE_OPENGL_ES;
771#endif
772 return RetBackendType;
773}
774
775void CGraphicsBackend_SDL_GL::ClampDriverVersion(EBackendType BackendType)
776{
777 if(BackendType == BACKEND_TYPE_OPENGL)
778 {
779 // clamp the versions to existing versions(only for OpenGL major <= 3)
780 if(g_Config.m_GfxGLMajor == 1)
781 {
782 g_Config.m_GfxGLMinor = std::clamp(val: g_Config.m_GfxGLMinor, lo: 1, hi: 5);
783 if(g_Config.m_GfxGLMinor == 2)
784 g_Config.m_GfxGLPatch = std::clamp(val: g_Config.m_GfxGLPatch, lo: 0, hi: 1);
785 else
786 g_Config.m_GfxGLPatch = 0;
787 }
788 else if(g_Config.m_GfxGLMajor == 2)
789 {
790 g_Config.m_GfxGLMinor = std::clamp(val: g_Config.m_GfxGLMinor, lo: 0, hi: 1);
791 g_Config.m_GfxGLPatch = 0;
792 }
793 else if(g_Config.m_GfxGLMajor == 3)
794 {
795 g_Config.m_GfxGLMinor = std::clamp(val: g_Config.m_GfxGLMinor, lo: 0, hi: 3);
796 if(g_Config.m_GfxGLMinor < 3)
797 g_Config.m_GfxGLMinor = 0;
798 g_Config.m_GfxGLPatch = 0;
799 }
800 }
801 else if(BackendType == BACKEND_TYPE_OPENGL_ES)
802 {
803#if !defined(CONF_BACKEND_OPENGL_ES3)
804 // Make sure GLES is set to 1.0 (which is equivalent to OpenGL 1.3), if its not set to >= 3.0(which is equivalent to OpenGL 3.3)
805 if(g_Config.m_GfxGLMajor < 3)
806 {
807 g_Config.m_GfxGLMajor = 1;
808 g_Config.m_GfxGLMinor = 0;
809 g_Config.m_GfxGLPatch = 0;
810
811 // GLES also doesn't know GL_QUAD
812 g_Config.m_GfxQuadAsTriangle = 1;
813 }
814#else
815 g_Config.m_GfxGLMajor = 3;
816 g_Config.m_GfxGLMinor = 0;
817 g_Config.m_GfxGLPatch = 0;
818#endif
819 }
820 else if(BackendType == BACKEND_TYPE_VULKAN)
821 {
822#if defined(CONF_BACKEND_VULKAN)
823 g_Config.m_GfxGLMajor = gs_BackendVulkanMajor;
824 g_Config.m_GfxGLMinor = gs_BackendVulkanMinor;
825 g_Config.m_GfxGLPatch = 0;
826#endif
827 }
828}
829
830static Uint32 MessageBoxTypeToSdlFlags(IGraphics::EMessageBoxType Type)
831{
832 switch(Type)
833 {
834 case IGraphics::EMessageBoxType::ERROR:
835 return SDL_MESSAGEBOX_ERROR;
836 case IGraphics::EMessageBoxType::WARNING:
837 return SDL_MESSAGEBOX_WARNING;
838 case IGraphics::EMessageBoxType::INFO:
839 return SDL_MESSAGEBOX_INFORMATION;
840 default:
841 dbg_assert_failed("Type invalid");
842 }
843}
844
845static std::optional<int> ShowMessageBoxImpl(const IGraphics::CMessageBox &MessageBox, SDL_Window *pWindow)
846{
847 dbg_assert(!MessageBox.m_vButtons.empty(), "At least one button is required");
848
849 std::vector<SDL_MessageBoxButtonData> vButtonData;
850 vButtonData.reserve(n: MessageBox.m_vButtons.size());
851 for(const auto &Button : MessageBox.m_vButtons)
852 {
853 SDL_MessageBoxButtonData ButtonData{};
854 ButtonData.buttonid = vButtonData.size();
855 ButtonData.flags = (Button.m_Confirm ? SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT : 0) | (Button.m_Cancel ? SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT : 0);
856 ButtonData.text = Button.m_pLabel;
857 vButtonData.emplace_back(args&: ButtonData);
858 }
859#if defined(CONF_FAMILY_WINDOWS)
860 // TODO SDL3: The order of buttons is not defined by default, but the flags returned by MessageBoxTypeToSdlFlags do not work together
861 // with SDL_MESSAGEBOX_BUTTONS_LEFT_TO_RIGHT with SDL2 on various platforms. Windows appears to be the only platform that
862 // lays out buttons from right to left by default, so we reverse the order manually.
863 std::reverse(vButtonData.begin(), vButtonData.end());
864#endif
865 SDL_MessageBoxData MessageBoxData{};
866 MessageBoxData.title = MessageBox.m_pTitle;
867 MessageBoxData.message = MessageBox.m_pMessage;
868 MessageBoxData.flags = MessageBoxTypeToSdlFlags(Type: MessageBox.m_Type);
869 MessageBoxData.numbuttons = vButtonData.size();
870 MessageBoxData.buttons = vButtonData.data();
871 MessageBoxData.window = pWindow;
872 int ButtonId = -1;
873 if(SDL_ShowMessageBox(messageboxdata: &MessageBoxData, buttonid: &ButtonId) != 0)
874 {
875 return std::nullopt;
876 }
877 return ButtonId;
878}
879
880std::optional<int> ShowMessageBoxWithoutGraphics(const IGraphics::CMessageBox &MessageBox)
881{
882 return ShowMessageBoxImpl(MessageBox, pWindow: nullptr);
883}
884
885std::optional<int> CGraphicsBackend_SDL_GL::ShowMessageBox(const IGraphics::CMessageBox &MessageBox)
886{
887 if(m_pProcessor != nullptr)
888 {
889 m_pProcessor->ErroneousCleanup();
890 }
891 // TODO: Remove this workaround when https://github.com/libsdl-org/SDL/issues/3750 is
892 // fixed and pass the window to SDL_ShowSimpleMessageBox to make the popup modal instead
893 // of destroying the window before opening the popup.
894 if(m_pWindow != nullptr)
895 {
896 SDL_DestroyWindow(window: m_pWindow);
897 m_pWindow = nullptr;
898 }
899 return ShowMessageBoxImpl(MessageBox, pWindow: m_pWindow);
900}
901
902bool CGraphicsBackend_SDL_GL::IsModernAPI(EBackendType BackendType)
903{
904 if(BackendType == BACKEND_TYPE_OPENGL)
905 return (g_Config.m_GfxGLMajor == 3 && g_Config.m_GfxGLMinor == 3) || g_Config.m_GfxGLMajor >= 4;
906 else if(BackendType == BACKEND_TYPE_OPENGL_ES)
907 return g_Config.m_GfxGLMajor >= 3;
908 else if(BackendType == BACKEND_TYPE_VULKAN)
909 return true;
910
911 return false;
912}
913
914bool CGraphicsBackend_SDL_GL::GetDriverVersion(EGraphicsDriverAgeType DriverAgeType, int &Major, int &Minor, int &Patch, const char *&pName, EBackendType BackendType)
915{
916 if(BackendType == BACKEND_TYPE_AUTO)
917 BackendType = m_BackendType;
918 if(BackendType == BACKEND_TYPE_OPENGL)
919 {
920 pName = "OpenGL";
921#ifndef CONF_BACKEND_OPENGL_ES
922 if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_LEGACY)
923 {
924 Major = 1;
925 Minor = 4;
926 Patch = 0;
927 return true;
928 }
929 else if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_DEFAULT)
930 {
931 Major = 3;
932 Minor = 0;
933 Patch = 0;
934 return true;
935 }
936 else if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_MODERN)
937 {
938 Major = 3;
939 Minor = 3;
940 Patch = 0;
941 return true;
942 }
943#endif
944 }
945 else if(BackendType == BACKEND_TYPE_OPENGL_ES)
946 {
947 pName = "GLES";
948#ifdef CONF_BACKEND_OPENGL_ES
949 if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_LEGACY)
950 {
951 Major = 1;
952 Minor = 0;
953 Patch = 0;
954 return true;
955 }
956 else if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_DEFAULT)
957 {
958 Major = 3;
959 Minor = 0;
960 Patch = 0;
961 // there isn't really a default one
962 return false;
963 }
964#endif
965#ifdef CONF_BACKEND_OPENGL_ES3
966 if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_MODERN)
967 {
968 Major = 3;
969 Minor = 0;
970 Patch = 0;
971 return true;
972 }
973#endif
974 }
975 else if(BackendType == BACKEND_TYPE_VULKAN)
976 {
977 pName = "Vulkan";
978#ifdef CONF_BACKEND_VULKAN
979 if(DriverAgeType == GRAPHICS_DRIVER_AGE_TYPE_DEFAULT)
980 {
981 Major = gs_BackendVulkanMajor;
982 Minor = gs_BackendVulkanMinor;
983 Patch = 0;
984 return true;
985 }
986#else
987 return false;
988#endif
989 }
990 return false;
991}
992
993const char *CGraphicsBackend_SDL_GL::GetScreenName(int Screen) const
994{
995 const char *pName = SDL_GetDisplayName(displayIndex: Screen);
996 return pName == nullptr ? "unknown/error" : pName;
997}
998
999static void DisplayToVideoMode(CVideoMode *pVMode, SDL_DisplayMode *pMode, float HiDPIScale, int RefreshRate)
1000{
1001 pVMode->m_CanvasWidth = pMode->w * HiDPIScale;
1002 pVMode->m_CanvasHeight = pMode->h * HiDPIScale;
1003 pVMode->m_WindowWidth = pMode->w;
1004 pVMode->m_WindowHeight = pMode->h;
1005 pVMode->m_RefreshRate = RefreshRate;
1006 pVMode->m_Red = SDL_BITSPERPIXEL(pMode->format);
1007 pVMode->m_Green = SDL_BITSPERPIXEL(pMode->format);
1008 pVMode->m_Blue = SDL_BITSPERPIXEL(pMode->format);
1009 pVMode->m_Format = pMode->format;
1010}
1011
1012void CGraphicsBackend_SDL_GL::GetVideoModes(CVideoMode *pModes, int MaxModes, int *pNumModes, float HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int ScreenId)
1013{
1014 SDL_DisplayMode DesktopMode;
1015 int MaxModesAvailable = SDL_GetNumDisplayModes(displayIndex: ScreenId);
1016
1017 // Only collect fullscreen modes when requested, that makes sure in windowed mode no refresh rates are shown that aren't supported without
1018 // fullscreen anyway(except fullscreen desktop)
1019 bool IsFullscreenDesktop = m_pWindow != nullptr && (((SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) || g_Config.m_GfxFullscreen == 3);
1020 bool CollectFullscreenModes = m_pWindow == nullptr || ((SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN) != 0 && !IsFullscreenDesktop);
1021
1022 if(SDL_GetDesktopDisplayMode(displayIndex: ScreenId, mode: &DesktopMode) < 0)
1023 {
1024 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", ScreenId, SDL_GetError());
1025 }
1026
1027 constexpr int ModeCount = 256;
1028 SDL_DisplayMode aModes[ModeCount];
1029 int NumModes = 0;
1030 for(int i = 0; i < MaxModesAvailable && NumModes < ModeCount; i++)
1031 {
1032 SDL_DisplayMode Mode;
1033 if(SDL_GetDisplayMode(displayIndex: ScreenId, modeIndex: i, mode: &Mode) < 0)
1034 {
1035 log_error("gfx", "Unable to get display mode %d of screen %d: %s", i, ScreenId, SDL_GetError());
1036 continue;
1037 }
1038
1039 aModes[NumModes] = Mode;
1040 ++NumModes;
1041 }
1042
1043 int NumModesInserted = 0;
1044 auto &&ModeInsert = [&](SDL_DisplayMode &Mode) {
1045 if(NumModesInserted < MaxModes)
1046 {
1047 // if last mode was equal, ignore this one --- in fullscreen this can really only happen if the screen
1048 // supports different color modes
1049 // in non fullscreen these are the modes that show different refresh rate, but are basically the same
1050 if(NumModesInserted > 0 && pModes[NumModesInserted - 1].m_WindowWidth == Mode.w && pModes[NumModesInserted - 1].m_WindowHeight == Mode.h && (pModes[NumModesInserted - 1].m_RefreshRate == Mode.refresh_rate || (Mode.refresh_rate != DesktopMode.refresh_rate && !CollectFullscreenModes)))
1051 return;
1052
1053 DisplayToVideoMode(pVMode: &pModes[NumModesInserted], pMode: &Mode, HiDPIScale, RefreshRate: !CollectFullscreenModes ? DesktopMode.refresh_rate : Mode.refresh_rate);
1054 NumModesInserted++;
1055 }
1056 };
1057
1058 for(int i = 0; i < NumModes; i++)
1059 {
1060 SDL_DisplayMode &Mode = aModes[i];
1061
1062 if(Mode.w > MaxWindowWidth || Mode.h > MaxWindowHeight)
1063 continue;
1064
1065 ModeInsert(Mode);
1066
1067 if(IsFullscreenDesktop)
1068 break;
1069
1070 if(NumModesInserted >= MaxModes)
1071 break;
1072 }
1073 *pNumModes = NumModesInserted;
1074}
1075
1076void CGraphicsBackend_SDL_GL::GetCurrentVideoMode(CVideoMode &CurMode, float HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int ScreenId)
1077{
1078 SDL_DisplayMode DpMode;
1079 // if "real" fullscreen, obtain the video mode for that
1080 if((SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN)
1081 {
1082 if(SDL_GetCurrentDisplayMode(displayIndex: ScreenId, mode: &DpMode))
1083 {
1084 log_error("gfx", "Unable to get current display mode of screen %d: %s", ScreenId, SDL_GetError());
1085 }
1086 }
1087 else
1088 {
1089 if(SDL_GetDesktopDisplayMode(displayIndex: ScreenId, mode: &DpMode) < 0)
1090 {
1091 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", ScreenId, SDL_GetError());
1092 }
1093 else
1094 {
1095 int Width = 0;
1096 int Height = 0;
1097 if(m_BackendType != EBackendType::BACKEND_TYPE_VULKAN)
1098 SDL_GL_GetDrawableSize(window: m_pWindow, w: &Width, h: &Height);
1099 else
1100 SDL_Vulkan_GetDrawableSize(window: m_pWindow, w: &Width, h: &Height);
1101 // SDL video modes are in screen space which are logical pixels
1102 DpMode.w = Width / HiDPIScale;
1103 DpMode.h = Height / HiDPIScale;
1104 }
1105 }
1106 DisplayToVideoMode(pVMode: &CurMode, pMode: &DpMode, HiDPIScale, RefreshRate: DpMode.refresh_rate);
1107}
1108
1109CGraphicsBackend_SDL_GL::CGraphicsBackend_SDL_GL(TTranslateFunc &&TranslateFunc) :
1110 CGraphicsBackend_Threaded(std::move(TranslateFunc))
1111{
1112 m_aErrorString[0] = '\0';
1113}
1114
1115int CGraphicsBackend_SDL_GL::Init(const char *pName, int *pScreen, int *pWidth, int *pHeight, int *pRefreshRate, int *pFsaaSamples, int Flags, int *pDesktopWidth, int *pDesktopHeight, int *pCurrentWidth, int *pCurrentHeight, IStorage *pStorage)
1116{
1117#if defined(CONF_HEADLESS_CLIENT)
1118 m_BackendType = BACKEND_TYPE_OPENGL;
1119 g_Config.m_GfxGLMajor = 0;
1120 g_Config.m_GfxGLMinor = 0;
1121 g_Config.m_GfxGLPatch = 0;
1122 int InitError = 0;
1123 int GlewMajor = 0;
1124 int GlewMinor = 0;
1125 int GlewPatch = 0;
1126 *pScreen = 0;
1127 *pWidth = *pDesktopWidth = *pCurrentWidth = 800;
1128 *pHeight = *pDesktopHeight = *pCurrentHeight = 600;
1129 *pRefreshRate = 60;
1130 *pFsaaSamples = 0;
1131 log_info("gfx", "Created headless context");
1132#else
1133 // print sdl version
1134 {
1135 SDL_version Compiled;
1136 SDL_version Linked;
1137
1138 SDL_VERSION(&Compiled);
1139 SDL_GetVersion(ver: &Linked);
1140 log_info("sdl", "SDL version %d.%d.%d (compiled = %d.%d.%d)",
1141 Linked.major, Linked.minor, Linked.patch,
1142 Compiled.major, Compiled.minor, Compiled.patch);
1143
1144#if CONF_PLATFORM_LINUX && SDL_VERSION_ATLEAST(2, 0, 22)
1145 // needed to workaround SDL from forcing exclusively X11 if linking against the GLX flavour of GLEW instead of the EGL one
1146 // w/o this on Wayland systems (no XWayland support) SDL's Video subsystem will fail to load (starting from SDL2.30+)
1147 if(Linked.major == 2 && Linked.minor >= 30)
1148 SDL_SetHint(SDL_HINT_VIDEODRIVER, value: "x11,wayland");
1149#endif
1150 }
1151
1152 if(!SDL_WasInit(SDL_INIT_VIDEO))
1153 {
1154 if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
1155 {
1156 log_error("gfx", "Unable to initialize SDL video: %s", SDL_GetError());
1157 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_INIT_FAILED;
1158 }
1159 }
1160
1161 EBackendType OldBackendType = m_BackendType;
1162 m_BackendType = DetectBackend();
1163 // little fallback for Vulkan
1164 if(OldBackendType != BACKEND_TYPE_AUTO &&
1165 m_BackendType == BACKEND_TYPE_VULKAN)
1166 {
1167 // try default opengl settings
1168 str_copy(dst&: g_Config.m_GfxBackend, src: "OpenGL");
1169 g_Config.m_GfxGLMajor = 3;
1170 g_Config.m_GfxGLMinor = 0;
1171 g_Config.m_GfxGLPatch = 0;
1172 // do another analysis round too, just in case
1173 g_Config.m_Gfx3DTextureAnalysisRan = 0;
1174 g_Config.m_GfxDriverIsBlocked = 0;
1175 m_BackendType = DetectBackend();
1176 }
1177
1178 ClampDriverVersion(BackendType: m_BackendType);
1179
1180 const bool UseModernGL = IsModernAPI(BackendType: m_BackendType);
1181 const bool IsOpenGLFamilyBackend = m_BackendType == BACKEND_TYPE_OPENGL || m_BackendType == BACKEND_TYPE_OPENGL_ES;
1182
1183 if(IsOpenGLFamilyBackend)
1184 {
1185 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_MAJOR_VERSION, value: g_Config.m_GfxGLMajor);
1186 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_MINOR_VERSION, value: g_Config.m_GfxGLMinor);
1187 }
1188
1189 const char *pBackendName;
1190 switch(m_BackendType)
1191 {
1192 case BACKEND_TYPE_OPENGL:
1193 pBackendName = "OpenGL";
1194 break;
1195 case BACKEND_TYPE_OPENGL_ES:
1196 pBackendName = "OpenGL ES";
1197 break;
1198 case BACKEND_TYPE_VULKAN:
1199 pBackendName = "Vulkan";
1200 break;
1201 default:
1202 dbg_assert_failed("Invalid m_BackendType: %d", m_BackendType);
1203 }
1204 log_info("gfx", "Created %s %d.%d context", pBackendName, g_Config.m_GfxGLMajor, g_Config.m_GfxGLMinor);
1205
1206 if(m_BackendType == BACKEND_TYPE_OPENGL)
1207 {
1208 if(g_Config.m_GfxGLMajor == 3 && g_Config.m_GfxGLMinor == 0)
1209 {
1210 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_PROFILE_MASK, value: SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
1211 }
1212 else if(UseModernGL)
1213 {
1214 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_PROFILE_MASK, value: SDL_GL_CONTEXT_PROFILE_CORE);
1215 }
1216 }
1217 else if(m_BackendType == BACKEND_TYPE_OPENGL_ES)
1218 {
1219 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_PROFILE_MASK, value: SDL_GL_CONTEXT_PROFILE_ES);
1220 }
1221
1222 if(IsOpenGLFamilyBackend)
1223 {
1224 *pFsaaSamples = std::clamp(val: *pFsaaSamples, lo: 0, hi: 8);
1225 }
1226
1227 // set screen
1228 m_NumScreens = SDL_GetNumVideoDisplays();
1229 if(m_NumScreens > 0)
1230 {
1231 SDL_Rect ScreenPos;
1232 *pScreen = std::clamp(val: *pScreen, lo: 0, hi: m_NumScreens - 1);
1233 if(SDL_GetDisplayBounds(displayIndex: *pScreen, rect: &ScreenPos) != 0)
1234 {
1235 log_error("gfx", "Unable to get display bounds of screen %d: %s", *pScreen, SDL_GetError());
1236 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_INFO_REQUEST_FAILED;
1237 }
1238 }
1239 else
1240 {
1241 log_error("gfx", "Unable to get number of screens: %s", SDL_GetError());
1242 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_REQUEST_FAILED;
1243 }
1244
1245 // store desktop resolution for settings reset button
1246 SDL_DisplayMode DisplayMode;
1247 if(SDL_GetDesktopDisplayMode(displayIndex: *pScreen, mode: &DisplayMode))
1248 {
1249 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", *pScreen, SDL_GetError());
1250 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_RESOLUTION_REQUEST_FAILED;
1251 }
1252
1253 bool IsDesktopChanged = *pDesktopWidth == 0 || *pDesktopHeight == 0 || *pDesktopWidth != DisplayMode.w || *pDesktopHeight != DisplayMode.h;
1254
1255 *pDesktopWidth = DisplayMode.w;
1256 *pDesktopHeight = DisplayMode.h;
1257
1258 // fetch supported video modes
1259 bool SupportedResolution = false;
1260
1261 CVideoMode aModes[256];
1262 int ModesCount = 0;
1263 int IndexOfResolution = -1;
1264 GetVideoModes(pModes: aModes, MaxModes: std::size(aModes), pNumModes: &ModesCount, HiDPIScale: 1, MaxWindowWidth: *pDesktopWidth, MaxWindowHeight: *pDesktopHeight, ScreenId: *pScreen);
1265
1266 for(int i = 0; i < ModesCount; i++)
1267 {
1268 if(*pWidth == aModes[i].m_WindowWidth && *pHeight == aModes[i].m_WindowHeight && (*pRefreshRate == aModes[i].m_RefreshRate || *pRefreshRate == 0))
1269 {
1270 SupportedResolution = true;
1271 IndexOfResolution = i;
1272 break;
1273 }
1274 }
1275
1276 // set flags
1277 int SdlFlags = SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_ALLOW_HIGHDPI;
1278 SdlFlags |= (IsOpenGLFamilyBackend) ? SDL_WINDOW_OPENGL : SDL_WINDOW_VULKAN;
1279 if(Flags & IGraphicsBackend::INITFLAG_RESIZABLE)
1280 SdlFlags |= SDL_WINDOW_RESIZABLE;
1281 if(Flags & IGraphicsBackend::INITFLAG_BORDERLESS)
1282 SdlFlags |= SDL_WINDOW_BORDERLESS;
1283 if(Flags & IGraphicsBackend::INITFLAG_FULLSCREEN)
1284 SdlFlags |= SDL_WINDOW_FULLSCREEN;
1285 else if(Flags & (IGraphicsBackend::INITFLAG_DESKTOP_FULLSCREEN))
1286 SdlFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
1287
1288 bool IsFullscreen = (SdlFlags & SDL_WINDOW_FULLSCREEN) != 0 || g_Config.m_GfxFullscreen == 3;
1289 // use desktop resolution as default resolution, clamp resolution if users's display is smaller than we remembered
1290 // if the user starts in fullscreen, and the resolution was not found use the desktop one
1291 if((IsFullscreen && !SupportedResolution) || *pWidth == 0 || *pHeight == 0 || (IsDesktopChanged && (!SupportedResolution || !IsFullscreen) && (*pWidth > *pDesktopWidth || *pHeight > *pDesktopHeight)))
1292 {
1293 *pWidth = *pDesktopWidth;
1294 *pHeight = *pDesktopHeight;
1295 *pRefreshRate = DisplayMode.refresh_rate;
1296 }
1297
1298 // if in fullscreen and refresh rate wasn't set yet, just use the one from the found list
1299 if(*pRefreshRate == 0 && SupportedResolution)
1300 {
1301 *pRefreshRate = aModes[IndexOfResolution].m_RefreshRate;
1302 }
1303 else if(*pRefreshRate == 0)
1304 {
1305 *pRefreshRate = DisplayMode.refresh_rate;
1306 }
1307
1308 // set gl attributes
1309 if(IsOpenGLFamilyBackend)
1310 {
1311 SDL_GL_SetAttribute(attr: SDL_GL_DOUBLEBUFFER, value: 1);
1312 if(*pFsaaSamples)
1313 {
1314 SDL_GL_SetAttribute(attr: SDL_GL_MULTISAMPLEBUFFERS, value: 1);
1315 SDL_GL_SetAttribute(attr: SDL_GL_MULTISAMPLESAMPLES, value: *pFsaaSamples);
1316 }
1317 else
1318 {
1319 SDL_GL_SetAttribute(attr: SDL_GL_MULTISAMPLEBUFFERS, value: 0);
1320 SDL_GL_SetAttribute(attr: SDL_GL_MULTISAMPLESAMPLES, value: 0);
1321 }
1322 }
1323
1324 m_pWindow = SDL_CreateWindow(
1325 title: pName,
1326 SDL_WINDOWPOS_CENTERED_DISPLAY(*pScreen),
1327 SDL_WINDOWPOS_CENTERED_DISPLAY(*pScreen),
1328 w: *pWidth,
1329 h: *pHeight,
1330 flags: SdlFlags);
1331
1332 // set caption
1333 if(m_pWindow == nullptr)
1334 {
1335 log_error("gfx", "Unable to create window: %s", SDL_GetError());
1336 if(m_BackendType == BACKEND_TYPE_VULKAN)
1337 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_CONTEXT_FAILED;
1338 else
1339 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_WINDOW_CREATE_FAILED;
1340 }
1341
1342 int GlewMajor = 0;
1343 int GlewMinor = 0;
1344 int GlewPatch = 0;
1345
1346 if(IsOpenGLFamilyBackend)
1347 {
1348 m_GLContext = SDL_GL_CreateContext(window: m_pWindow);
1349
1350 if(m_GLContext == nullptr)
1351 {
1352 log_error("gfx", "Unable to create graphics context: %s", SDL_GetError());
1353 SDL_DestroyWindow(window: m_pWindow);
1354 m_pWindow = nullptr;
1355 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_CONTEXT_FAILED;
1356 }
1357
1358 if(!BackendInitGlew(BackendType: m_BackendType, GlewMajor, GlewMinor, GlewPatch))
1359 {
1360 SDL_GL_DeleteContext(context: m_GLContext);
1361 SDL_DestroyWindow(window: m_pWindow);
1362 m_pWindow = nullptr;
1363 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GLEW_INIT_FAILED;
1364 }
1365 }
1366
1367 int InitError = IsVersionSupportedGlew(BackendType: m_BackendType, VersionMajor: g_Config.m_GfxGLMajor, VersionMinor: g_Config.m_GfxGLMinor, VersionPatch: g_Config.m_GfxGLPatch, GlewMajor, GlewMinor, GlewPatch);
1368
1369 // SDL_GL_GetDrawableSize reports HiDPI resolution even with SDL_WINDOW_ALLOW_HIGHDPI not set, which is wrong
1370 if(SdlFlags & SDL_WINDOW_ALLOW_HIGHDPI)
1371 {
1372 if(IsOpenGLFamilyBackend)
1373 SDL_GL_GetDrawableSize(window: m_pWindow, w: pCurrentWidth, h: pCurrentHeight);
1374 else
1375 SDL_Vulkan_GetDrawableSize(window: m_pWindow, w: pCurrentWidth, h: pCurrentHeight);
1376 }
1377 else
1378 SDL_GetWindowSize(window: m_pWindow, w: pCurrentWidth, h: pCurrentHeight);
1379 SDL_GetWindowSize(window: m_pWindow, w: pWidth, h: pHeight);
1380
1381 if(IsOpenGLFamilyBackend)
1382 {
1383#if !defined(CONF_PLATFORM_EMSCRIPTEN)
1384 // SDL_GL_SetSwapInterval is not supported with Emscripten as this is only a wrapper for the
1385 // emscripten_set_main_loop_timing function which does not work because we do not use the
1386 // emscripten_set_main_loop function before.
1387 SDL_GL_SetSwapInterval(interval: Flags & IGraphicsBackend::INITFLAG_VSYNC ? 1 : 0);
1388#endif
1389 SDL_GL_MakeCurrent(window: nullptr, context: nullptr);
1390 }
1391
1392 if(InitError != 0)
1393 {
1394 if(m_GLContext)
1395 SDL_GL_DeleteContext(context: m_GLContext);
1396 SDL_DestroyWindow(window: m_pWindow);
1397 m_pWindow = nullptr;
1398
1399 // try setting to glew supported version
1400 g_Config.m_GfxGLMajor = GlewMajor;
1401 g_Config.m_GfxGLMinor = GlewMinor;
1402 g_Config.m_GfxGLPatch = GlewPatch;
1403
1404 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_VERSION_FAILED;
1405 }
1406#endif // !CONF_HEADLESS_CLIENT
1407
1408 // start the command processor
1409 dbg_assert(m_pProcessor == nullptr, "Processor was not cleaned up properly.");
1410 m_pProcessor = new CCommandProcessor_SDL_GL(m_BackendType, g_Config.m_GfxGLMajor, g_Config.m_GfxGLMinor, g_Config.m_GfxGLPatch);
1411 StartProcessor(pProcessor: m_pProcessor);
1412
1413 // issue init commands for OpenGL and SDL
1414 CCommandBuffer CmdBuffer(1024, 512);
1415 CCommandProcessorFragment_GLBase::SCommand_PreInit CmdPre;
1416 CmdPre.m_pWindow = m_pWindow;
1417 CmdPre.m_Width = *pCurrentWidth;
1418 CmdPre.m_Height = *pCurrentHeight;
1419 CmdPre.m_pVendorString = m_aVendorString;
1420 CmdPre.m_pVersionString = m_aVersionString;
1421 CmdPre.m_pRendererString = m_aRendererString;
1422 CmdPre.m_pGpuList = &m_GpuList;
1423 CmdBuffer.AddCommandUnsafe(Command: CmdPre);
1424 RunBufferSingleThreadedUnsafe(pBuffer: &CmdBuffer);
1425 CmdBuffer.Reset();
1426
1427 // run sdl first to have the context in the thread
1428 CCommandProcessorFragment_SDL::SCommand_Init CmdSDL;
1429 CmdSDL.m_pWindow = m_pWindow;
1430 CmdSDL.m_GLContext = m_GLContext;
1431 CmdBuffer.AddCommandUnsafe(Command: CmdSDL);
1432 RunBuffer(pBuffer: &CmdBuffer);
1433 WaitForIdle();
1434 CmdBuffer.Reset();
1435
1436 const char *pErrorStr = nullptr;
1437 if(InitError == 0)
1438 {
1439 CCommandProcessorFragment_GLBase::SCommand_Init CmdGL;
1440 CmdGL.m_pWindow = m_pWindow;
1441 CmdGL.m_Width = *pCurrentWidth;
1442 CmdGL.m_Height = *pCurrentHeight;
1443 CmdGL.m_pTextureMemoryUsage = &m_TextureMemoryUsage;
1444 CmdGL.m_pBufferMemoryUsage = &m_BufferMemoryUsage;
1445 CmdGL.m_pStreamMemoryUsage = &m_StreamMemoryUsage;
1446 CmdGL.m_pStagingMemoryUsage = &m_StagingMemoryUsage;
1447 CmdGL.m_pGpuList = &m_GpuList;
1448 CmdGL.m_pReadPresentedImageDataFunc = &m_ReadPresentedImageDataFunc;
1449 CmdGL.m_pStorage = pStorage;
1450 CmdGL.m_pCapabilities = &m_Capabilities;
1451 CmdGL.m_pInitError = &InitError;
1452 CmdGL.m_RequestedMajor = g_Config.m_GfxGLMajor;
1453 CmdGL.m_RequestedMinor = g_Config.m_GfxGLMinor;
1454 CmdGL.m_RequestedPatch = g_Config.m_GfxGLPatch;
1455 CmdGL.m_GlewMajor = GlewMajor;
1456 CmdGL.m_GlewMinor = GlewMinor;
1457 CmdGL.m_GlewPatch = GlewPatch;
1458 CmdGL.m_pErrStringPtr = &pErrorStr;
1459 CmdGL.m_pVendorString = m_aVendorString;
1460 CmdGL.m_pVersionString = m_aVersionString;
1461 CmdGL.m_pRendererString = m_aRendererString;
1462 CmdGL.m_RequestedBackend = m_BackendType;
1463 CmdBuffer.AddCommandUnsafe(Command: CmdGL);
1464
1465 RunBuffer(pBuffer: &CmdBuffer);
1466 WaitForIdle();
1467 CmdBuffer.Reset();
1468 }
1469
1470 if(InitError != 0)
1471 {
1472 if(InitError != -2)
1473 {
1474 // shutdown the context, as it might have been initialized
1475 CCommandProcessorFragment_GLBase::SCommand_Shutdown CmdGL;
1476 CmdBuffer.AddCommandUnsafe(Command: CmdGL);
1477 RunBuffer(pBuffer: &CmdBuffer);
1478 WaitForIdle();
1479 CmdBuffer.Reset();
1480 }
1481
1482 CCommandProcessorFragment_SDL::SCommand_Shutdown Cmd;
1483 CmdBuffer.AddCommandUnsafe(Command: Cmd);
1484 RunBuffer(pBuffer: &CmdBuffer);
1485 WaitForIdle();
1486 CmdBuffer.Reset();
1487
1488 CCommandProcessorFragment_GLBase::SCommand_PostShutdown CmdPost;
1489 CmdBuffer.AddCommandUnsafe(Command: CmdPost);
1490 RunBufferSingleThreadedUnsafe(pBuffer: &CmdBuffer);
1491 CmdBuffer.Reset();
1492
1493 // stop and delete the processor
1494 StopProcessor();
1495 delete m_pProcessor;
1496 m_pProcessor = nullptr;
1497
1498 if(m_GLContext)
1499 SDL_GL_DeleteContext(context: m_GLContext);
1500 SDL_DestroyWindow(window: m_pWindow);
1501 m_pWindow = nullptr;
1502
1503 // try setting to version string's supported version
1504 if(InitError == -2)
1505 {
1506 g_Config.m_GfxGLMajor = m_Capabilities.m_ContextMajor;
1507 g_Config.m_GfxGLMinor = m_Capabilities.m_ContextMinor;
1508 g_Config.m_GfxGLPatch = m_Capabilities.m_ContextPatch;
1509 }
1510
1511 if(pErrorStr != nullptr)
1512 {
1513 str_copy(dst&: m_aErrorString, src: pErrorStr);
1514 }
1515
1516 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_VERSION_FAILED;
1517 }
1518
1519 {
1520 CCommandBuffer::SCommand_Update_Viewport CmdSDL2;
1521 CmdSDL2.m_X = 0;
1522 CmdSDL2.m_Y = 0;
1523 CmdSDL2.m_Width = *pCurrentWidth;
1524 CmdSDL2.m_Height = *pCurrentHeight;
1525 CmdSDL2.m_ByResize = true;
1526 CmdBuffer.AddCommandUnsafe(Command: CmdSDL2);
1527 RunBuffer(pBuffer: &CmdBuffer);
1528 WaitForIdle();
1529 CmdBuffer.Reset();
1530 }
1531
1532 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_NONE;
1533}
1534
1535int CGraphicsBackend_SDL_GL::Shutdown()
1536{
1537 if(m_pProcessor != nullptr)
1538 {
1539 // issue a shutdown command
1540 CCommandBuffer CmdBuffer(1024, 512);
1541 CCommandProcessorFragment_GLBase::SCommand_Shutdown CmdGL;
1542 CmdBuffer.AddCommandUnsafe(Command: CmdGL);
1543 RunBuffer(pBuffer: &CmdBuffer);
1544 WaitForIdle();
1545 CmdBuffer.Reset();
1546
1547 CCommandProcessorFragment_SDL::SCommand_Shutdown Cmd;
1548 CmdBuffer.AddCommandUnsafe(Command: Cmd);
1549 RunBuffer(pBuffer: &CmdBuffer);
1550 WaitForIdle();
1551 CmdBuffer.Reset();
1552
1553 CCommandProcessorFragment_GLBase::SCommand_PostShutdown CmdPost;
1554 CmdBuffer.AddCommandUnsafe(Command: CmdPost);
1555 RunBufferSingleThreadedUnsafe(pBuffer: &CmdBuffer);
1556 CmdBuffer.Reset();
1557
1558 // stop and delete the processor
1559 StopProcessor();
1560 delete m_pProcessor;
1561 m_pProcessor = nullptr;
1562 }
1563
1564 if(m_GLContext != nullptr)
1565 SDL_GL_DeleteContext(context: m_GLContext);
1566 SDL_DestroyWindow(window: m_pWindow);
1567 m_pWindow = nullptr;
1568
1569 SDL_QuitSubSystem(SDL_INIT_VIDEO);
1570 return 0;
1571}
1572
1573uint64_t CGraphicsBackend_SDL_GL::TextureMemoryUsage() const
1574{
1575 return m_TextureMemoryUsage;
1576}
1577
1578uint64_t CGraphicsBackend_SDL_GL::BufferMemoryUsage() const
1579{
1580 return m_BufferMemoryUsage;
1581}
1582
1583uint64_t CGraphicsBackend_SDL_GL::StreamedMemoryUsage() const
1584{
1585 return m_StreamMemoryUsage;
1586}
1587
1588uint64_t CGraphicsBackend_SDL_GL::StagingMemoryUsage() const
1589{
1590 return m_StagingMemoryUsage;
1591}
1592
1593const TTwGraphicsGpuList &CGraphicsBackend_SDL_GL::GetGpus() const
1594{
1595 return m_GpuList;
1596}
1597
1598void CGraphicsBackend_SDL_GL::Minimize()
1599{
1600 SDL_MinimizeWindow(window: m_pWindow);
1601}
1602
1603void CGraphicsBackend_SDL_GL::SetWindowParams(int FullscreenMode, bool IsBorderless)
1604{
1605 // The flags have to be kept consistent with flags set in the CGraphics_Threaded::IssueInit function!
1606
1607 if(FullscreenMode > 0)
1608 {
1609 bool IsDesktopFullscreen = FullscreenMode == 2;
1610#ifndef CONF_FAMILY_WINDOWS
1611 // Windowed fullscreen is only available on Windows, use desktop fullscreen on other platforms
1612 IsDesktopFullscreen |= FullscreenMode == 3;
1613#endif
1614 if(FullscreenMode == 1)
1615 {
1616#if defined(CONF_PLATFORM_MACOS) || defined(CONF_PLATFORM_HAIKU)
1617 // Todo SDL: remove this when fixed (game freezes when losing focus in fullscreen)
1618 SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
1619#else
1620 SDL_SetWindowFullscreen(window: m_pWindow, flags: SDL_WINDOW_FULLSCREEN);
1621#endif
1622 SDL_SetWindowResizable(window: m_pWindow, resizable: SDL_FALSE);
1623 }
1624 else if(IsDesktopFullscreen)
1625 {
1626 SDL_SetWindowFullscreen(window: m_pWindow, flags: SDL_WINDOW_FULLSCREEN_DESKTOP);
1627 SDL_SetWindowResizable(window: m_pWindow, resizable: SDL_FALSE);
1628 }
1629 else // Windowed fullscreen
1630 {
1631 SDL_SetWindowFullscreen(window: m_pWindow, flags: 0);
1632 SDL_SetWindowBordered(window: m_pWindow, bordered: SDL_TRUE);
1633 SDL_SetWindowResizable(window: m_pWindow, resizable: SDL_FALSE);
1634 SDL_DisplayMode DpMode;
1635 if(SDL_GetDesktopDisplayMode(displayIndex: g_Config.m_GfxScreen, mode: &DpMode) < 0)
1636 {
1637 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", g_Config.m_GfxScreen, SDL_GetError());
1638 }
1639 else
1640 {
1641 ResizeWindow(w: DpMode.w, h: DpMode.h, RefreshRate: DpMode.refresh_rate);
1642 SDL_SetWindowPosition(window: m_pWindow, SDL_WINDOWPOS_CENTERED_DISPLAY(g_Config.m_GfxScreen), SDL_WINDOWPOS_CENTERED_DISPLAY(g_Config.m_GfxScreen));
1643 }
1644 }
1645 }
1646 else // Windowed
1647 {
1648 SDL_SetWindowFullscreen(window: m_pWindow, flags: 0);
1649 SDL_SetWindowBordered(window: m_pWindow, bordered: SDL_bool(!IsBorderless));
1650 SDL_SetWindowResizable(window: m_pWindow, resizable: SDL_TRUE);
1651 }
1652}
1653
1654bool CGraphicsBackend_SDL_GL::SetWindowScreen(int Index, bool MoveToCenter)
1655{
1656 if(Index < 0 || Index >= m_NumScreens)
1657 {
1658 log_error("gfx", "Invalid screen number: %d (min: 0, max: %d)", Index, m_NumScreens);
1659 return false;
1660 }
1661
1662 SDL_Rect ScreenPos;
1663 if(SDL_GetDisplayBounds(displayIndex: Index, rect: &ScreenPos) != 0)
1664 {
1665 log_error("gfx", "Unable to get bounds of screen %d: %s", Index, SDL_GetError());
1666 return false;
1667 }
1668
1669 if(MoveToCenter)
1670 {
1671 SDL_SetWindowPosition(window: m_pWindow,
1672 SDL_WINDOWPOS_CENTERED_DISPLAY(Index),
1673 SDL_WINDOWPOS_CENTERED_DISPLAY(Index));
1674 }
1675 else
1676 {
1677 SDL_SetWindowPosition(window: m_pWindow,
1678 SDL_WINDOWPOS_UNDEFINED_DISPLAY(Index),
1679 SDL_WINDOWPOS_UNDEFINED_DISPLAY(Index));
1680 }
1681
1682 return UpdateDisplayMode(Index);
1683}
1684
1685bool CGraphicsBackend_SDL_GL::UpdateDisplayMode(int Index)
1686{
1687 SDL_DisplayMode DisplayMode;
1688 if(SDL_GetDesktopDisplayMode(displayIndex: Index, mode: &DisplayMode) < 0)
1689 {
1690 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", Index, SDL_GetError());
1691 return false;
1692 }
1693
1694 g_Config.m_GfxScreen = Index;
1695 g_Config.m_GfxDesktopWidth = DisplayMode.w;
1696 g_Config.m_GfxDesktopHeight = DisplayMode.h;
1697 return true;
1698}
1699
1700int CGraphicsBackend_SDL_GL::GetWindowScreen()
1701{
1702 return SDL_GetWindowDisplayIndex(window: m_pWindow);
1703}
1704
1705int CGraphicsBackend_SDL_GL::WindowActive()
1706{
1707 return m_pWindow && SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_INPUT_FOCUS;
1708}
1709
1710int CGraphicsBackend_SDL_GL::WindowOpen()
1711{
1712 return m_pWindow && SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_SHOWN;
1713}
1714
1715void CGraphicsBackend_SDL_GL::SetWindowGrab(bool Grab)
1716{
1717 SDL_SetWindowGrab(window: m_pWindow, grabbed: Grab ? SDL_TRUE : SDL_FALSE);
1718}
1719
1720bool CGraphicsBackend_SDL_GL::ResizeWindow(int w, int h, int RefreshRate)
1721{
1722 // don't call resize events when the window is at fullscreen desktop
1723 if(!m_pWindow || (SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)
1724 return false;
1725
1726 // if the window is at fullscreen use SDL_SetWindowDisplayMode instead, suggested by SDL
1727 if(SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN)
1728 {
1729#ifdef CONF_FAMILY_WINDOWS
1730 // in windows make the window windowed mode first, this prevents strange window glitches (other games probably do something similar)
1731 SetWindowParams(0, true);
1732#endif
1733 SDL_DisplayMode SetMode = {};
1734 SDL_DisplayMode ClosestMode = {};
1735 SetMode.format = 0;
1736 SetMode.w = w;
1737 SetMode.h = h;
1738 SetMode.refresh_rate = RefreshRate;
1739 SDL_SetWindowDisplayMode(window: m_pWindow, mode: SDL_GetClosestDisplayMode(displayIndex: g_Config.m_GfxScreen, mode: &SetMode, closest: &ClosestMode));
1740#ifdef CONF_FAMILY_WINDOWS
1741 // now change it back to fullscreen, this will restore the above set state, bcs SDL saves fullscreen modes apart from other video modes (as of SDL 2.0.16)
1742 // see implementation of SDL_SetWindowDisplayMode
1743 SetWindowParams(1, false);
1744#endif
1745 return true;
1746 }
1747 else
1748 {
1749 SDL_SetWindowSize(window: m_pWindow, w, h);
1750 if(SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_MAXIMIZED)
1751 // remove maximize flag
1752 SDL_RestoreWindow(window: m_pWindow);
1753 }
1754
1755 return false;
1756}
1757
1758void CGraphicsBackend_SDL_GL::GetViewportSize(int &w, int &h)
1759{
1760 if(m_BackendType != EBackendType::BACKEND_TYPE_VULKAN)
1761 SDL_GL_GetDrawableSize(window: m_pWindow, w: &w, h: &h);
1762 else
1763 SDL_Vulkan_GetDrawableSize(window: m_pWindow, w: &w, h: &h);
1764}
1765
1766void CGraphicsBackend_SDL_GL::NotifyWindow()
1767{
1768 // Minimum version 2.0.16, after version 2.0.22 the naming is changed to 2.24.0 etc.
1769#if SDL_MAJOR_VERSION > 2 || (SDL_MAJOR_VERSION == 2 && SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL >= 16) || (SDL_MAJOR_VERSION == 2 && SDL_MINOR_VERSION > 0)
1770 if(SDL_FlashWindow(window: m_pWindow, operation: SDL_FlashOperation::SDL_FLASH_UNTIL_FOCUSED) != 0)
1771 {
1772 // fails if SDL hasn't implemented it
1773 return;
1774 }
1775#endif
1776}
1777
1778bool CGraphicsBackend_SDL_GL::IsScreenKeyboardShown()
1779{
1780 return SDL_IsScreenKeyboardShown(window: m_pWindow);
1781}
1782
1783void CGraphicsBackend_SDL_GL::WindowDestroyNtf(uint32_t WindowId)
1784{
1785}
1786
1787void CGraphicsBackend_SDL_GL::WindowCreateNtf(uint32_t WindowId)
1788{
1789 m_pWindow = SDL_GetWindowFromID(id: WindowId);
1790}
1791
1792TGLBackendReadPresentedImageData &CGraphicsBackend_SDL_GL::GetReadPresentedImageDataFuncUnsafe()
1793{
1794 return m_ReadPresentedImageDataFunc;
1795}
1796
1797IGraphicsBackend *CreateGraphicsBackend(TTranslateFunc &&TranslateFunc) { return new CGraphicsBackend_SDL_GL(std::move(TranslateFunc)); }
1798