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