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(nullptr, nullptr);
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}
1018
1019void CGraphicsBackend_SDL_GL::GetVideoModes(CVideoMode *pModes, int MaxModes, int *pNumModes, float HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int ScreenId)
1020{
1021 SDL_DisplayMode DesktopMode;
1022 int MaxModesAvailable = SDL_GetNumDisplayModes(displayIndex: ScreenId);
1023
1024 // Only collect fullscreen modes when requested, that makes sure in windowed mode no refresh rates are shown that aren't supported without
1025 // fullscreen anyway(except fullscreen desktop)
1026 bool IsFullscreenDesktop = m_pWindow != nullptr && (((SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) || g_Config.m_GfxFullscreen == 3);
1027 bool CollectFullscreenModes = m_pWindow == nullptr || ((SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN) != 0 && !IsFullscreenDesktop);
1028
1029 if(SDL_GetDesktopDisplayMode(displayIndex: ScreenId, mode: &DesktopMode) < 0)
1030 {
1031 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", ScreenId, SDL_GetError());
1032 }
1033
1034 constexpr int ModeCount = 256;
1035 SDL_DisplayMode aModes[ModeCount];
1036 int NumModes = 0;
1037 for(int i = 0; i < MaxModesAvailable && NumModes < ModeCount; i++)
1038 {
1039 SDL_DisplayMode Mode;
1040 if(SDL_GetDisplayMode(displayIndex: ScreenId, modeIndex: i, mode: &Mode) < 0)
1041 {
1042 log_error("gfx", "Unable to get display mode %d of screen %d: %s", i, ScreenId, SDL_GetError());
1043 continue;
1044 }
1045
1046 aModes[NumModes] = Mode;
1047 ++NumModes;
1048 }
1049
1050 int NumModesInserted = 0;
1051 auto &&ModeInsert = [&](SDL_DisplayMode &Mode) {
1052 if(NumModesInserted < MaxModes)
1053 {
1054 // if last mode was equal, ignore this one --- in fullscreen this can really only happen if the screen
1055 // supports different color modes
1056 // in non fullscreen these are the modes that show different refresh rate, but are basically the same
1057 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)))
1058 return;
1059
1060 DisplayToVideoMode(pVMode: &pModes[NumModesInserted], pMode: &Mode, HiDPIScale, RefreshRate: !CollectFullscreenModes ? DesktopMode.refresh_rate : Mode.refresh_rate);
1061 NumModesInserted++;
1062 }
1063 };
1064
1065 for(int i = 0; i < NumModes; i++)
1066 {
1067 SDL_DisplayMode &Mode = aModes[i];
1068
1069 if(Mode.w > MaxWindowWidth || Mode.h > MaxWindowHeight)
1070 continue;
1071
1072 ModeInsert(Mode);
1073
1074 if(IsFullscreenDesktop)
1075 break;
1076
1077 if(NumModesInserted >= MaxModes)
1078 break;
1079 }
1080 *pNumModes = NumModesInserted;
1081}
1082
1083void CGraphicsBackend_SDL_GL::GetCurrentVideoMode(CVideoMode &CurMode, float HiDPIScale, int MaxWindowWidth, int MaxWindowHeight, int ScreenId)
1084{
1085 SDL_DisplayMode DpMode;
1086 // if "real" fullscreen, obtain the video mode for that
1087 if((SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN)
1088 {
1089 if(SDL_GetCurrentDisplayMode(displayIndex: ScreenId, mode: &DpMode))
1090 {
1091 log_error("gfx", "Unable to get current display mode of screen %d: %s", ScreenId, SDL_GetError());
1092 }
1093 }
1094 else
1095 {
1096 if(SDL_GetDesktopDisplayMode(displayIndex: ScreenId, mode: &DpMode) < 0)
1097 {
1098 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", ScreenId, SDL_GetError());
1099 }
1100 else
1101 {
1102 int Width = 0;
1103 int Height = 0;
1104 if(m_BackendType != EBackendType::BACKEND_TYPE_VULKAN)
1105 SDL_GL_GetDrawableSize(window: m_pWindow, w: &Width, h: &Height);
1106 else
1107 SDL_Vulkan_GetDrawableSize(window: m_pWindow, w: &Width, h: &Height);
1108 // SDL video modes are in screen space which are logical pixels
1109 DpMode.w = Width / HiDPIScale;
1110 DpMode.h = Height / HiDPIScale;
1111 }
1112 }
1113 DisplayToVideoMode(pVMode: &CurMode, pMode: &DpMode, HiDPIScale, RefreshRate: DpMode.refresh_rate);
1114}
1115
1116CGraphicsBackend_SDL_GL::CGraphicsBackend_SDL_GL(TTranslateFunc &&TranslateFunc) :
1117 CGraphicsBackend_Threaded(std::move(TranslateFunc))
1118{
1119 m_aErrorString[0] = '\0';
1120}
1121
1122int 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)
1123{
1124#if defined(CONF_HEADLESS_CLIENT)
1125 m_BackendType = BACKEND_TYPE_OPENGL;
1126 g_Config.m_GfxGLMajor = 0;
1127 g_Config.m_GfxGLMinor = 0;
1128 g_Config.m_GfxGLPatch = 0;
1129 int InitError = 0;
1130 int GlewMajor = 0;
1131 int GlewMinor = 0;
1132 int GlewPatch = 0;
1133 *pScreen = 0;
1134 *pWidth = *pDesktopWidth = *pCurrentWidth = 800;
1135 *pHeight = *pDesktopHeight = *pCurrentHeight = 600;
1136 *pRefreshRate = 60;
1137 *pFsaaSamples = 0;
1138 log_info("gfx", "Created headless context");
1139#else
1140 // print sdl version
1141 {
1142 SDL_version Compiled;
1143 SDL_version Linked;
1144
1145 SDL_VERSION(&Compiled);
1146 SDL_GetVersion(ver: &Linked);
1147 log_info("sdl", "SDL version %d.%d.%d (compiled = %d.%d.%d)",
1148 Linked.major, Linked.minor, Linked.patch,
1149 Compiled.major, Compiled.minor, Compiled.patch);
1150
1151#if CONF_PLATFORM_LINUX && SDL_VERSION_ATLEAST(2, 0, 22)
1152 // needed to workaround SDL from forcing exclusively X11 if linking against the GLX flavour of GLEW instead of the EGL one
1153 // w/o this on Wayland systems (no XWayland support) SDL's Video subsystem will fail to load (starting from SDL2.30+)
1154 if(Linked.major == 2 && Linked.minor >= 30)
1155 SDL_SetHint(SDL_HINT_VIDEODRIVER, value: "x11,wayland");
1156#endif
1157 }
1158
1159 if(!SDL_WasInit(SDL_INIT_VIDEO))
1160 {
1161 if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
1162 {
1163 log_error("gfx", "Unable to initialize SDL video: %s", SDL_GetError());
1164 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_INIT_FAILED;
1165 }
1166 }
1167
1168 EBackendType OldBackendType = m_BackendType;
1169 m_BackendType = DetectBackend();
1170 // little fallback for Vulkan
1171 if(OldBackendType != BACKEND_TYPE_AUTO &&
1172 m_BackendType == BACKEND_TYPE_VULKAN)
1173 {
1174 // try default opengl settings
1175 str_copy(dst&: g_Config.m_GfxBackend, src: "OpenGL");
1176 g_Config.m_GfxGLMajor = 3;
1177 g_Config.m_GfxGLMinor = 0;
1178 g_Config.m_GfxGLPatch = 0;
1179 // do another analysis round too, just in case
1180 g_Config.m_Gfx3DTextureAnalysisRan = 0;
1181 g_Config.m_GfxDriverIsBlocked = 0;
1182 m_BackendType = DetectBackend();
1183 }
1184
1185 ClampDriverVersion(BackendType: m_BackendType);
1186
1187 const bool UseModernGL = IsModernAPI(BackendType: m_BackendType);
1188 const bool IsOpenGLFamilyBackend = m_BackendType == BACKEND_TYPE_OPENGL || m_BackendType == BACKEND_TYPE_OPENGL_ES;
1189
1190 if(IsOpenGLFamilyBackend)
1191 {
1192 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_MAJOR_VERSION, value: g_Config.m_GfxGLMajor);
1193 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_MINOR_VERSION, value: g_Config.m_GfxGLMinor);
1194 }
1195
1196 const char *pBackendName;
1197 switch(m_BackendType)
1198 {
1199 case BACKEND_TYPE_OPENGL:
1200 pBackendName = "OpenGL";
1201 break;
1202 case BACKEND_TYPE_OPENGL_ES:
1203 pBackendName = "OpenGL ES";
1204 break;
1205 case BACKEND_TYPE_VULKAN:
1206 pBackendName = "Vulkan";
1207 break;
1208 default:
1209 dbg_assert_failed("Invalid m_BackendType: %d", m_BackendType);
1210 }
1211 log_info("gfx", "Created %s %d.%d context", pBackendName, g_Config.m_GfxGLMajor, g_Config.m_GfxGLMinor);
1212
1213 if(m_BackendType == BACKEND_TYPE_OPENGL)
1214 {
1215 if(g_Config.m_GfxGLMajor == 3 && g_Config.m_GfxGLMinor == 0)
1216 {
1217 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_PROFILE_MASK, value: SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
1218 }
1219 else if(UseModernGL)
1220 {
1221 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_PROFILE_MASK, value: SDL_GL_CONTEXT_PROFILE_CORE);
1222 }
1223 }
1224 else if(m_BackendType == BACKEND_TYPE_OPENGL_ES)
1225 {
1226 SDL_GL_SetAttribute(attr: SDL_GL_CONTEXT_PROFILE_MASK, value: SDL_GL_CONTEXT_PROFILE_ES);
1227 }
1228
1229 if(IsOpenGLFamilyBackend)
1230 {
1231 *pFsaaSamples = std::clamp(val: *pFsaaSamples, lo: 0, hi: 8);
1232 }
1233
1234 // set screen
1235 m_NumScreens = SDL_GetNumVideoDisplays();
1236 if(m_NumScreens > 0)
1237 {
1238 SDL_Rect ScreenPos;
1239 *pScreen = std::clamp(val: *pScreen, lo: 0, hi: m_NumScreens - 1);
1240 if(SDL_GetDisplayBounds(displayIndex: *pScreen, rect: &ScreenPos) != 0)
1241 {
1242 log_error("gfx", "Unable to get display bounds of screen %d: %s", *pScreen, SDL_GetError());
1243 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_INFO_REQUEST_FAILED;
1244 }
1245 }
1246 else
1247 {
1248 log_error("gfx", "Unable to get number of screens: %s", SDL_GetError());
1249 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_REQUEST_FAILED;
1250 }
1251
1252 // store desktop resolution for settings reset button
1253 SDL_DisplayMode DisplayMode;
1254 if(SDL_GetDesktopDisplayMode(displayIndex: *pScreen, mode: &DisplayMode))
1255 {
1256 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", *pScreen, SDL_GetError());
1257 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_SCREEN_RESOLUTION_REQUEST_FAILED;
1258 }
1259
1260 bool IsDesktopChanged = *pDesktopWidth == 0 || *pDesktopHeight == 0 || *pDesktopWidth != DisplayMode.w || *pDesktopHeight != DisplayMode.h;
1261
1262 *pDesktopWidth = DisplayMode.w;
1263 *pDesktopHeight = DisplayMode.h;
1264
1265 // fetch supported video modes
1266 bool SupportedResolution = false;
1267
1268 CVideoMode aModes[256];
1269 int ModesCount = 0;
1270 int IndexOfResolution = -1;
1271 GetVideoModes(pModes: aModes, MaxModes: std::size(aModes), pNumModes: &ModesCount, HiDPIScale: 1, MaxWindowWidth: *pDesktopWidth, MaxWindowHeight: *pDesktopHeight, ScreenId: *pScreen);
1272
1273 for(int i = 0; i < ModesCount; i++)
1274 {
1275 if(*pWidth == aModes[i].m_WindowWidth && *pHeight == aModes[i].m_WindowHeight && (*pRefreshRate == aModes[i].m_RefreshRate || *pRefreshRate == 0))
1276 {
1277 SupportedResolution = true;
1278 IndexOfResolution = i;
1279 break;
1280 }
1281 }
1282
1283 // set flags
1284 int SdlFlags = SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_ALLOW_HIGHDPI;
1285 SdlFlags |= (IsOpenGLFamilyBackend) ? SDL_WINDOW_OPENGL : SDL_WINDOW_VULKAN;
1286 if(Flags & IGraphicsBackend::INITFLAG_RESIZABLE)
1287 SdlFlags |= SDL_WINDOW_RESIZABLE;
1288 if(Flags & IGraphicsBackend::INITFLAG_BORDERLESS)
1289 SdlFlags |= SDL_WINDOW_BORDERLESS;
1290 if(Flags & IGraphicsBackend::INITFLAG_FULLSCREEN)
1291 SdlFlags |= SDL_WINDOW_FULLSCREEN;
1292 else if(Flags & (IGraphicsBackend::INITFLAG_DESKTOP_FULLSCREEN))
1293 SdlFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
1294
1295 bool IsFullscreen = (SdlFlags & SDL_WINDOW_FULLSCREEN) != 0 || g_Config.m_GfxFullscreen == 3;
1296 // use desktop resolution as default resolution, clamp resolution if users's display is smaller than we remembered
1297 // if the user starts in fullscreen, and the resolution was not found use the desktop one
1298 if((IsFullscreen && !SupportedResolution) || *pWidth == 0 || *pHeight == 0 || (IsDesktopChanged && (!SupportedResolution || !IsFullscreen) && (*pWidth > *pDesktopWidth || *pHeight > *pDesktopHeight)))
1299 {
1300 *pWidth = *pDesktopWidth;
1301 *pHeight = *pDesktopHeight;
1302 *pRefreshRate = DisplayMode.refresh_rate;
1303 }
1304
1305 // if in fullscreen and refresh rate wasn't set yet, just use the one from the found list
1306 if(*pRefreshRate == 0 && SupportedResolution)
1307 {
1308 *pRefreshRate = aModes[IndexOfResolution].m_RefreshRate;
1309 }
1310 else if(*pRefreshRate == 0)
1311 {
1312 *pRefreshRate = DisplayMode.refresh_rate;
1313 }
1314
1315 // set gl attributes
1316 if(IsOpenGLFamilyBackend)
1317 {
1318 SDL_GL_SetAttribute(attr: SDL_GL_DOUBLEBUFFER, value: 1);
1319 if(*pFsaaSamples)
1320 {
1321 SDL_GL_SetAttribute(attr: SDL_GL_MULTISAMPLEBUFFERS, value: 1);
1322 SDL_GL_SetAttribute(attr: SDL_GL_MULTISAMPLESAMPLES, value: *pFsaaSamples);
1323 }
1324 else
1325 {
1326 SDL_GL_SetAttribute(attr: SDL_GL_MULTISAMPLEBUFFERS, value: 0);
1327 SDL_GL_SetAttribute(attr: SDL_GL_MULTISAMPLESAMPLES, value: 0);
1328 }
1329 }
1330
1331 m_pWindow = SDL_CreateWindow(
1332 title: pName,
1333 SDL_WINDOWPOS_CENTERED_DISPLAY(*pScreen),
1334 SDL_WINDOWPOS_CENTERED_DISPLAY(*pScreen),
1335 w: *pWidth,
1336 h: *pHeight,
1337 flags: SdlFlags);
1338
1339 // set caption
1340 if(m_pWindow == nullptr)
1341 {
1342 log_error("gfx", "Unable to create window: %s", SDL_GetError());
1343 if(m_BackendType == BACKEND_TYPE_VULKAN)
1344 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_CONTEXT_FAILED;
1345 else
1346 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_SDL_WINDOW_CREATE_FAILED;
1347 }
1348
1349 int GlewMajor = 0;
1350 int GlewMinor = 0;
1351 int GlewPatch = 0;
1352
1353 if(IsOpenGLFamilyBackend)
1354 {
1355 m_GLContext = SDL_GL_CreateContext(window: m_pWindow);
1356
1357 if(m_GLContext == nullptr)
1358 {
1359 log_error("gfx", "Unable to create graphics context: %s", SDL_GetError());
1360 SDL_DestroyWindow(window: m_pWindow);
1361 m_pWindow = nullptr;
1362 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_CONTEXT_FAILED;
1363 }
1364
1365 if(!BackendInitGlew(BackendType: m_BackendType, GlewMajor, GlewMinor, GlewPatch))
1366 {
1367 SDL_GL_DeleteContext(context: m_GLContext);
1368 SDL_DestroyWindow(window: m_pWindow);
1369 m_pWindow = nullptr;
1370 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GLEW_INIT_FAILED;
1371 }
1372 }
1373
1374 int InitError = IsVersionSupportedGlew(BackendType: m_BackendType, VersionMajor: g_Config.m_GfxGLMajor, VersionMinor: g_Config.m_GfxGLMinor, VersionPatch: g_Config.m_GfxGLPatch, GlewMajor, GlewMinor, GlewPatch);
1375
1376 // SDL_GL_GetDrawableSize reports HiDPI resolution even with SDL_WINDOW_ALLOW_HIGHDPI not set, which is wrong
1377 if(SdlFlags & SDL_WINDOW_ALLOW_HIGHDPI)
1378 {
1379 if(IsOpenGLFamilyBackend)
1380 SDL_GL_GetDrawableSize(window: m_pWindow, w: pCurrentWidth, h: pCurrentHeight);
1381 else
1382 SDL_Vulkan_GetDrawableSize(window: m_pWindow, w: pCurrentWidth, h: pCurrentHeight);
1383 }
1384 else
1385 SDL_GetWindowSize(window: m_pWindow, w: pCurrentWidth, h: pCurrentHeight);
1386 SDL_GetWindowSize(window: m_pWindow, w: pWidth, h: pHeight);
1387
1388 if(IsOpenGLFamilyBackend)
1389 {
1390#if !defined(CONF_PLATFORM_EMSCRIPTEN)
1391 // SDL_GL_SetSwapInterval is not supported with Emscripten as this is only a wrapper for the
1392 // emscripten_set_main_loop_timing function which does not work because we do not use the
1393 // emscripten_set_main_loop function before.
1394 SDL_GL_SetSwapInterval(interval: Flags & IGraphicsBackend::INITFLAG_VSYNC ? 1 : 0);
1395#endif
1396 SDL_GL_MakeCurrent(window: nullptr, context: nullptr);
1397 }
1398
1399 if(InitError != 0)
1400 {
1401 if(m_GLContext)
1402 SDL_GL_DeleteContext(context: m_GLContext);
1403 SDL_DestroyWindow(window: m_pWindow);
1404 m_pWindow = nullptr;
1405
1406 // try setting to glew supported version
1407 g_Config.m_GfxGLMajor = GlewMajor;
1408 g_Config.m_GfxGLMinor = GlewMinor;
1409 g_Config.m_GfxGLPatch = GlewPatch;
1410
1411 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_VERSION_FAILED;
1412 }
1413#endif // !CONF_HEADLESS_CLIENT
1414
1415 // start the command processor
1416 dbg_assert(m_pProcessor == nullptr, "Processor was not cleaned up properly.");
1417 m_pProcessor = new CCommandProcessor_SDL_GL(m_BackendType, g_Config.m_GfxGLMajor, g_Config.m_GfxGLMinor, g_Config.m_GfxGLPatch);
1418 StartProcessor(pProcessor: m_pProcessor);
1419
1420 // issue init commands for OpenGL and SDL
1421 CCommandBuffer CmdBuffer(1024, 512);
1422 CCommandProcessorFragment_GLBase::SCommand_PreInit CmdPre;
1423 CmdPre.m_pWindow = m_pWindow;
1424 CmdPre.m_Width = *pCurrentWidth;
1425 CmdPre.m_Height = *pCurrentHeight;
1426 CmdPre.m_pVendorString = m_aVendorString;
1427 CmdPre.m_pVersionString = m_aVersionString;
1428 CmdPre.m_pRendererString = m_aRendererString;
1429 CmdPre.m_pGpuList = &m_GpuList;
1430 CmdBuffer.AddCommandUnsafe(Command: CmdPre);
1431 RunBufferSingleThreadedUnsafe(pBuffer: &CmdBuffer);
1432 CmdBuffer.Reset();
1433
1434 // run sdl first to have the context in the thread
1435 CCommandProcessorFragment_SDL::SCommand_Init CmdSDL;
1436 CmdSDL.m_pWindow = m_pWindow;
1437 CmdSDL.m_GLContext = m_GLContext;
1438 CmdBuffer.AddCommandUnsafe(Command: CmdSDL);
1439 RunBuffer(pBuffer: &CmdBuffer);
1440 WaitForIdle();
1441 CmdBuffer.Reset();
1442
1443 const char *pErrorStr = nullptr;
1444 if(InitError == 0)
1445 {
1446 CCommandProcessorFragment_GLBase::SCommand_Init CmdGL;
1447 CmdGL.m_pWindow = m_pWindow;
1448 CmdGL.m_Width = *pCurrentWidth;
1449 CmdGL.m_Height = *pCurrentHeight;
1450 CmdGL.m_pTextureMemoryUsage = &m_TextureMemoryUsage;
1451 CmdGL.m_pBufferMemoryUsage = &m_BufferMemoryUsage;
1452 CmdGL.m_pStreamMemoryUsage = &m_StreamMemoryUsage;
1453 CmdGL.m_pStagingMemoryUsage = &m_StagingMemoryUsage;
1454 CmdGL.m_pGpuList = &m_GpuList;
1455 CmdGL.m_pReadPresentedImageDataFunc = &m_ReadPresentedImageDataFunc;
1456 CmdGL.m_pStorage = pStorage;
1457 CmdGL.m_pCapabilities = &m_Capabilities;
1458 CmdGL.m_pInitError = &InitError;
1459 CmdGL.m_RequestedMajor = g_Config.m_GfxGLMajor;
1460 CmdGL.m_RequestedMinor = g_Config.m_GfxGLMinor;
1461 CmdGL.m_RequestedPatch = g_Config.m_GfxGLPatch;
1462 CmdGL.m_GlewMajor = GlewMajor;
1463 CmdGL.m_GlewMinor = GlewMinor;
1464 CmdGL.m_GlewPatch = GlewPatch;
1465 CmdGL.m_pErrStringPtr = &pErrorStr;
1466 CmdGL.m_pVendorString = m_aVendorString;
1467 CmdGL.m_pVersionString = m_aVersionString;
1468 CmdGL.m_pRendererString = m_aRendererString;
1469 CmdGL.m_RequestedBackend = m_BackendType;
1470 CmdBuffer.AddCommandUnsafe(Command: CmdGL);
1471
1472 RunBuffer(pBuffer: &CmdBuffer);
1473 WaitForIdle();
1474 CmdBuffer.Reset();
1475 }
1476
1477 if(InitError != 0)
1478 {
1479 if(InitError != -2)
1480 {
1481 // shutdown the context, as it might have been initialized
1482 CCommandProcessorFragment_GLBase::SCommand_Shutdown CmdGL;
1483 CmdBuffer.AddCommandUnsafe(Command: CmdGL);
1484 RunBuffer(pBuffer: &CmdBuffer);
1485 WaitForIdle();
1486 CmdBuffer.Reset();
1487 }
1488
1489 CCommandProcessorFragment_SDL::SCommand_Shutdown Cmd;
1490 CmdBuffer.AddCommandUnsafe(Command: Cmd);
1491 RunBuffer(pBuffer: &CmdBuffer);
1492 WaitForIdle();
1493 CmdBuffer.Reset();
1494
1495 CCommandProcessorFragment_GLBase::SCommand_PostShutdown CmdPost;
1496 CmdBuffer.AddCommandUnsafe(Command: CmdPost);
1497 RunBufferSingleThreadedUnsafe(pBuffer: &CmdBuffer);
1498 CmdBuffer.Reset();
1499
1500 // stop and delete the processor
1501 StopProcessor();
1502 delete m_pProcessor;
1503 m_pProcessor = nullptr;
1504
1505 if(m_GLContext)
1506 SDL_GL_DeleteContext(context: m_GLContext);
1507 SDL_DestroyWindow(window: m_pWindow);
1508 m_pWindow = nullptr;
1509
1510 // try setting to version string's supported version
1511 if(InitError == -2)
1512 {
1513 g_Config.m_GfxGLMajor = m_Capabilities.m_ContextMajor;
1514 g_Config.m_GfxGLMinor = m_Capabilities.m_ContextMinor;
1515 g_Config.m_GfxGLPatch = m_Capabilities.m_ContextPatch;
1516 }
1517
1518 if(pErrorStr != nullptr)
1519 {
1520 str_copy(dst&: m_aErrorString, src: pErrorStr);
1521 }
1522
1523 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_VERSION_FAILED;
1524 }
1525
1526 {
1527 CCommandBuffer::SCommand_Update_Viewport CmdSDL2;
1528 CmdSDL2.m_X = 0;
1529 CmdSDL2.m_Y = 0;
1530 CmdSDL2.m_Width = *pCurrentWidth;
1531 CmdSDL2.m_Height = *pCurrentHeight;
1532 CmdSDL2.m_ByResize = true;
1533 CmdBuffer.AddCommandUnsafe(Command: CmdSDL2);
1534 RunBuffer(pBuffer: &CmdBuffer);
1535 WaitForIdle();
1536 CmdBuffer.Reset();
1537 }
1538
1539 return EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_NONE;
1540}
1541
1542int CGraphicsBackend_SDL_GL::Shutdown()
1543{
1544 if(m_pProcessor != nullptr)
1545 {
1546 // issue a shutdown command
1547 CCommandBuffer CmdBuffer(1024, 512);
1548 CCommandProcessorFragment_GLBase::SCommand_Shutdown CmdGL;
1549 CmdBuffer.AddCommandUnsafe(Command: CmdGL);
1550 RunBuffer(pBuffer: &CmdBuffer);
1551 WaitForIdle();
1552 CmdBuffer.Reset();
1553
1554 CCommandProcessorFragment_SDL::SCommand_Shutdown Cmd;
1555 CmdBuffer.AddCommandUnsafe(Command: Cmd);
1556 RunBuffer(pBuffer: &CmdBuffer);
1557 WaitForIdle();
1558 CmdBuffer.Reset();
1559
1560 CCommandProcessorFragment_GLBase::SCommand_PostShutdown CmdPost;
1561 CmdBuffer.AddCommandUnsafe(Command: CmdPost);
1562 RunBufferSingleThreadedUnsafe(pBuffer: &CmdBuffer);
1563 CmdBuffer.Reset();
1564
1565 // stop and delete the processor
1566 StopProcessor();
1567 delete m_pProcessor;
1568 m_pProcessor = nullptr;
1569 }
1570
1571 if(m_GLContext != nullptr)
1572 SDL_GL_DeleteContext(context: m_GLContext);
1573 SDL_DestroyWindow(window: m_pWindow);
1574 m_pWindow = nullptr;
1575
1576 SDL_QuitSubSystem(SDL_INIT_VIDEO);
1577 return 0;
1578}
1579
1580uint64_t CGraphicsBackend_SDL_GL::TextureMemoryUsage() const
1581{
1582 return m_TextureMemoryUsage;
1583}
1584
1585uint64_t CGraphicsBackend_SDL_GL::BufferMemoryUsage() const
1586{
1587 return m_BufferMemoryUsage;
1588}
1589
1590uint64_t CGraphicsBackend_SDL_GL::StreamedMemoryUsage() const
1591{
1592 return m_StreamMemoryUsage;
1593}
1594
1595uint64_t CGraphicsBackend_SDL_GL::StagingMemoryUsage() const
1596{
1597 return m_StagingMemoryUsage;
1598}
1599
1600const TTwGraphicsGpuList &CGraphicsBackend_SDL_GL::GetGpus() const
1601{
1602 return m_GpuList;
1603}
1604
1605void CGraphicsBackend_SDL_GL::Minimize()
1606{
1607 SDL_MinimizeWindow(window: m_pWindow);
1608}
1609
1610void CGraphicsBackend_SDL_GL::SetWindowParams(int FullscreenMode, bool IsBorderless)
1611{
1612 // The flags have to be kept consistent with flags set in the CGraphics_Threaded::IssueInit function!
1613
1614 if(FullscreenMode > 0)
1615 {
1616 bool IsDesktopFullscreen = FullscreenMode == 2;
1617#ifndef CONF_FAMILY_WINDOWS
1618 // Windowed fullscreen is only available on Windows, use desktop fullscreen on other platforms
1619 IsDesktopFullscreen |= FullscreenMode == 3;
1620#endif
1621 if(FullscreenMode == 1)
1622 {
1623#if defined(CONF_PLATFORM_MACOS) || defined(CONF_PLATFORM_HAIKU)
1624 // Todo SDL: remove this when fixed (game freezes when losing focus in fullscreen)
1625 SDL_SetWindowFullscreen(m_pWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
1626#else
1627 SDL_SetWindowFullscreen(window: m_pWindow, flags: SDL_WINDOW_FULLSCREEN);
1628#endif
1629 SDL_SetWindowResizable(window: m_pWindow, resizable: SDL_FALSE);
1630 }
1631 else if(IsDesktopFullscreen)
1632 {
1633 SDL_SetWindowFullscreen(window: m_pWindow, flags: SDL_WINDOW_FULLSCREEN_DESKTOP);
1634 SDL_SetWindowResizable(window: m_pWindow, resizable: SDL_FALSE);
1635 }
1636 else // Windowed fullscreen
1637 {
1638 SDL_SetWindowFullscreen(window: m_pWindow, flags: 0);
1639 SDL_SetWindowBordered(window: m_pWindow, bordered: SDL_TRUE);
1640 SDL_SetWindowResizable(window: m_pWindow, resizable: SDL_FALSE);
1641 SDL_DisplayMode DpMode;
1642 if(SDL_GetDesktopDisplayMode(displayIndex: g_Config.m_GfxScreen, mode: &DpMode) < 0)
1643 {
1644 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", g_Config.m_GfxScreen, SDL_GetError());
1645 }
1646 else
1647 {
1648 ResizeWindow(w: DpMode.w, h: DpMode.h, RefreshRate: DpMode.refresh_rate);
1649 SDL_SetWindowPosition(window: m_pWindow, SDL_WINDOWPOS_CENTERED_DISPLAY(g_Config.m_GfxScreen), SDL_WINDOWPOS_CENTERED_DISPLAY(g_Config.m_GfxScreen));
1650 }
1651 }
1652 }
1653 else // Windowed
1654 {
1655 SDL_SetWindowFullscreen(window: m_pWindow, flags: 0);
1656 SDL_SetWindowBordered(window: m_pWindow, bordered: SDL_bool(!IsBorderless));
1657 SDL_SetWindowResizable(window: m_pWindow, resizable: SDL_TRUE);
1658 }
1659}
1660
1661bool CGraphicsBackend_SDL_GL::SetWindowScreen(int Index, bool MoveToCenter, ivec2 *pDesktopSize)
1662{
1663 if(Index < 0 || Index >= m_NumScreens)
1664 {
1665 log_error("gfx", "Invalid screen number: %d (min: 0, max: %d)", Index, m_NumScreens);
1666 return false;
1667 }
1668
1669 SDL_Rect ScreenPos;
1670 if(SDL_GetDisplayBounds(displayIndex: Index, rect: &ScreenPos) != 0)
1671 {
1672 log_error("gfx", "Unable to get bounds of screen %d: %s", Index, SDL_GetError());
1673 return false;
1674 }
1675
1676 if(MoveToCenter)
1677 {
1678 SDL_SetWindowPosition(window: m_pWindow,
1679 SDL_WINDOWPOS_CENTERED_DISPLAY(Index),
1680 SDL_WINDOWPOS_CENTERED_DISPLAY(Index));
1681 }
1682 else
1683 {
1684 SDL_SetWindowPosition(window: m_pWindow,
1685 SDL_WINDOWPOS_UNDEFINED_DISPLAY(Index),
1686 SDL_WINDOWPOS_UNDEFINED_DISPLAY(Index));
1687 }
1688
1689 return UpdateDisplayMode(Index, pDesktopSize);
1690}
1691
1692bool CGraphicsBackend_SDL_GL::UpdateDisplayMode(int Index, ivec2 *pDesktopSize)
1693{
1694 SDL_DisplayMode DisplayMode;
1695 if(SDL_GetDesktopDisplayMode(displayIndex: Index, mode: &DisplayMode) < 0)
1696 {
1697 log_error("gfx", "Unable to get desktop display mode of screen %d: %s", Index, SDL_GetError());
1698 return false;
1699 }
1700
1701 g_Config.m_GfxScreen = Index;
1702 pDesktopSize->x = DisplayMode.w;
1703 pDesktopSize->y = DisplayMode.h;
1704 return true;
1705}
1706
1707int CGraphicsBackend_SDL_GL::GetWindowScreen()
1708{
1709 return SDL_GetWindowDisplayIndex(window: m_pWindow);
1710}
1711
1712int CGraphicsBackend_SDL_GL::WindowActive()
1713{
1714 return m_pWindow && SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_INPUT_FOCUS;
1715}
1716
1717int CGraphicsBackend_SDL_GL::WindowOpen()
1718{
1719 return m_pWindow && SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_SHOWN;
1720}
1721
1722void CGraphicsBackend_SDL_GL::SetWindowGrab(bool Grab)
1723{
1724 // Works around https://github.com/libsdl-org/sdl2-compat/issues/578.
1725 if(!m_pWindow)
1726 return;
1727
1728 SDL_SetWindowGrab(window: m_pWindow, grabbed: Grab ? SDL_TRUE : SDL_FALSE);
1729}
1730
1731bool CGraphicsBackend_SDL_GL::ResizeWindow(int w, int h, int RefreshRate)
1732{
1733 // don't call resize events when the window is at fullscreen desktop
1734 if(!m_pWindow || (SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)
1735 return false;
1736
1737 // if the window is at fullscreen use SDL_SetWindowDisplayMode instead, suggested by SDL
1738 if(SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_FULLSCREEN)
1739 {
1740#ifdef CONF_FAMILY_WINDOWS
1741 // in windows make the window windowed mode first, this prevents strange window glitches (other games probably do something similar)
1742 SetWindowParams(0, true);
1743#endif
1744 SDL_DisplayMode SetMode = {};
1745 SDL_DisplayMode ClosestMode = {};
1746 SetMode.format = 0;
1747 SetMode.w = w;
1748 SetMode.h = h;
1749 SetMode.refresh_rate = RefreshRate;
1750 SDL_SetWindowDisplayMode(window: m_pWindow, mode: SDL_GetClosestDisplayMode(displayIndex: g_Config.m_GfxScreen, mode: &SetMode, closest: &ClosestMode));
1751#ifdef CONF_FAMILY_WINDOWS
1752 // 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)
1753 // see implementation of SDL_SetWindowDisplayMode
1754 SetWindowParams(1, false);
1755#endif
1756 return true;
1757 }
1758 else
1759 {
1760 SDL_SetWindowSize(window: m_pWindow, w, h);
1761 if(SDL_GetWindowFlags(window: m_pWindow) & SDL_WINDOW_MAXIMIZED)
1762 // remove maximize flag
1763 SDL_RestoreWindow(window: m_pWindow);
1764 }
1765
1766 return false;
1767}
1768
1769void CGraphicsBackend_SDL_GL::GetViewportSize(int &w, int &h)
1770{
1771 if(m_BackendType != EBackendType::BACKEND_TYPE_VULKAN)
1772 SDL_GL_GetDrawableSize(window: m_pWindow, w: &w, h: &h);
1773 else
1774 SDL_Vulkan_GetDrawableSize(window: m_pWindow, w: &w, h: &h);
1775}
1776
1777void CGraphicsBackend_SDL_GL::NotifyWindow()
1778{
1779 // Minimum version 2.0.16, after version 2.0.22 the naming is changed to 2.24.0 etc.
1780#if SDL_MAJOR_VERSION > 2 || (SDL_MAJOR_VERSION == 2 && SDL_MINOR_VERSION == 0 && SDL_PATCHLEVEL >= 16) || (SDL_MAJOR_VERSION == 2 && SDL_MINOR_VERSION > 0)
1781 if(SDL_FlashWindow(window: m_pWindow, operation: SDL_FlashOperation::SDL_FLASH_UNTIL_FOCUSED) != 0)
1782 {
1783 // fails if SDL hasn't implemented it
1784 return;
1785 }
1786#endif
1787}
1788
1789bool CGraphicsBackend_SDL_GL::IsScreenKeyboardShown()
1790{
1791 return SDL_IsScreenKeyboardShown(window: m_pWindow);
1792}
1793
1794void CGraphicsBackend_SDL_GL::WindowDestroyNtf(uint32_t WindowId)
1795{
1796}
1797
1798void CGraphicsBackend_SDL_GL::WindowCreateNtf(uint32_t WindowId)
1799{
1800 m_pWindow = SDL_GetWindowFromID(id: WindowId);
1801}
1802
1803TGLBackendReadPresentedImageData &CGraphicsBackend_SDL_GL::GetReadPresentedImageDataFuncUnsafe()
1804{
1805 return m_ReadPresentedImageDataFunc;
1806}
1807
1808IGraphicsBackend *CreateGraphicsBackend(TTranslateFunc &&TranslateFunc) { return new CGraphicsBackend_SDL_GL(std::move(TranslateFunc)); }
1809