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