diff --git a/src/Layers/xrRenderGL/glHW.cpp b/src/Layers/xrRenderGL/glHW.cpp index a69cb1e18de..b734ec5a634 100644 --- a/src/Layers/xrRenderGL/glHW.cpp +++ b/src/Layers/xrRenderGL/glHW.cpp @@ -85,29 +85,48 @@ void CHW::CreateDevice(SDL_Window* hWnd) // Apply the pixel format to the device context SDL_SetWindowDisplayMode(m_window, &mode); + Caps.fTarget = D3DFMT_A8R8G8B8; + Caps.fDepth = D3DFMT_D24S8; + // Create the context m_context = SDL_GL_CreateContext(m_window); if (m_context == nullptr) { - Log("! Could not create drawing context:", SDL_GetError()); + Log("! OpenGL: could not create drawing context:", SDL_GetError()); return; } if (MakeContextCurrent(IRender::PrimaryContext) != 0) { - Log("! Could not make context current:", SDL_GetError()); + Log("! OpenGL: could not make context current:", SDL_GetError()); return; } - UpdateVSync(); - -#ifdef DEBUG - if (glDebugMessageCallback) + int version; { - CHK_GL(glEnable(GL_DEBUG_OUTPUT)); - CHK_GL(glDebugMessageCallback((GLDEBUGPROC)OnDebugCallback, nullptr)); + ZoneScopedN("gladLoadGL"); + version = gladLoadGL(reinterpret_cast(SDL_GL_GetProcAddress)); } + if (version == 0) + { + Log("! OpenGL: could not initialize GLAD."); + if (const auto err = SDL_GetError()) + Log("SDL Error:", err); + return; + } + + if (ThisInstanceIsGlobal()) + { + UpdateVSync(); + +#ifdef DEBUG + if (glDebugMessageCallback) + { + CHK_GL(glEnable(GL_DEBUG_OUTPUT)); + CHK_GL(glDebugMessageCallback((GLDEBUGPROC)OnDebugCallback, nullptr)); + } #endif // DEBUG + } int iMaxVTFUnits, iMaxCTIUnits; glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &iMaxVTFUnits); @@ -124,16 +143,18 @@ void CHW::CreateDevice(SDL_Window* hWnd) ComputeShadersSupported = false; // XXX: Implement compute shaders support - Caps.fTarget = D3DFMT_A8R8G8B8; - Caps.fDepth = D3DFMT_D24S8; - // Create render target and depth-stencil views here UpdateViews(); } void CHW::DestroyDevice() { - SDL_GL_MakeCurrent(nullptr, nullptr); + CHK_GL(glDeleteFramebuffers(1, &pFB)); + pFB = 0; + + const auto context = SDL_GL_GetCurrentContext(); + if (context == m_context) + SDL_GL_MakeCurrent(nullptr, nullptr); SDL_GL_DeleteContext(m_context); m_context = nullptr; @@ -147,7 +168,9 @@ void CHW::Reset() ZoneScoped; CHK_GL(glDeleteFramebuffers(1, &pFB)); + pFB = 0; UpdateViews(); + UpdateVSync(); } diff --git a/src/Layers/xrRenderPC_GL/r2_test_hw.cpp b/src/Layers/xrRenderPC_GL/r2_test_hw.cpp index 3ba5b683490..d5f3bcc025c 100644 --- a/src/Layers/xrRenderPC_GL/r2_test_hw.cpp +++ b/src/Layers/xrRenderPC_GL/r2_test_hw.cpp @@ -3,38 +3,31 @@ class sdl_window_test_helper { SDL_Window* m_window{}; - SDL_GLContext m_context{}; + CHW m_hw; public: sdl_window_test_helper() { - ZoneScoped; u32 flags{}; - HW.SetPrimaryAttributes(flags); + m_hw.SetPrimaryAttributes(flags); m_window = SDL_CreateWindow("TestOpenGLWindow", 0, 0, 1, 1, SDL_WINDOW_HIDDEN | flags); if (!m_window) { - Log("~ Cannot create helper window for OpenGL:", SDL_GetError()); - return; - } - - m_context = SDL_GL_CreateContext(m_window); - if (!m_context) - { - Log("~ Cannot create OpenGL context:", SDL_GetError()); + Log("~ Cannot create helper window for OpenGL test:", SDL_GetError()); return; } + m_hw.CreateDevice(m_window); } [[nodiscard]] bool successful() const { - return m_window && m_context; + return m_window && m_hw.m_context && m_hw.pFB; } ~sdl_window_test_helper() { - SDL_GL_DeleteContext(m_context); + m_hw.DestroyDevice(); SDL_DestroyWindow(m_window); } }; @@ -45,21 +38,8 @@ BOOL xrRender_test_hw() // Check if minimal required OpenGL features are available const sdl_window_test_helper windowTest; - if (!windowTest.successful()) - return FALSE; - - int version; - { - ZoneScopedN("gladLoadGL"); - version = gladLoadGL((GLADloadfunc) SDL_GL_GetProcAddress); - } - if (version == 0) - { - Log("~ Could not initialize GLAD."); - if (auto err = SDL_GetError()) - Log("SDL Error:", err); - return FALSE; - } + if (windowTest.successful()) + return TRUE; - return TRUE; + return FALSE; }