From fbbfbf78906d1654007cdf234548c726293857e1 Mon Sep 17 00:00:00 2001 From: Jupeyy Date: Thu, 6 May 2021 12:55:39 +0200 Subject: [PATCH] don't initialize opengl stuff, if not required --- .../client/backend/opengl/backend_opengl.cpp | 103 ++++++++++-------- .../client/backend/opengl/backend_opengl.h | 6 +- .../client/backend/opengl/backend_opengl3.cpp | 7 +- .../client/backend/opengl/backend_opengl3.h | 2 +- src/engine/client/backend_sdl.cpp | 13 +-- 5 files changed, 73 insertions(+), 58 deletions(-) diff --git a/src/engine/client/backend/opengl/backend_opengl.cpp b/src/engine/client/backend/opengl/backend_opengl.cpp index 2c3b3480a..de552efb5 100644 --- a/src/engine/client/backend/opengl/backend_opengl.cpp +++ b/src/engine/client/backend/opengl/backend_opengl.cpp @@ -310,49 +310,10 @@ GfxOpenGLMessageCallback(GLenum source, } #endif -void CCommandProcessorFragment_OpenGL::InitOpenGL(const SCommand_Init *pCommand) +bool CCommandProcessorFragment_OpenGL::InitOpenGL(const SCommand_Init *pCommand) { m_IsOpenGLES = pCommand->m_RequestedBackend == BACKEND_TYPE_OPENGL_ES; - // set some default settings - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glDisable(GL_CULL_FACE); - glDisable(GL_DEPTH_TEST); - -#ifndef BACKEND_GL_MODERN_API - if(!IsNewApi()) - { - glAlphaFunc(GL_GREATER, 0); - glEnable(GL_ALPHA_TEST); - } -#endif - - glDepthMask(0); - -#ifndef BACKEND_AS_OPENGL_ES - if(g_Config.m_DbgGfx) - { - if(GLEW_KHR_debug || GLEW_ARB_debug_output) - { - // During init, enable debug output - if(GLEW_KHR_debug) - { - glEnable(GL_DEBUG_OUTPUT); - glDebugMessageCallback(GfxOpenGLMessageCallback, 0); - } - else if(GLEW_ARB_debug_output) - { - glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); - glDebugMessageCallbackARB(GfxOpenGLMessageCallback, 0); - } - dbg_msg("gfx", "Enabled OpenGL debug mode"); - } - else - dbg_msg("gfx", "Requested OpenGL debug mode, but the driver does not support the required extension"); - } -#endif - const char *pVendorString = (const char *)glGetString(GL_VENDOR); dbg_msg("opengl", "Vendor string: %s", pVendorString); @@ -583,11 +544,58 @@ void CCommandProcessorFragment_OpenGL::InitOpenGL(const SCommand_Init *pCommand) pCommand->m_pCapabilities->m_NPOTTextures = true; } } + + if(*pCommand->m_pInitError != -2) + { + // set some default settings + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + +#ifndef BACKEND_GL_MODERN_API + if(!IsNewApi()) + { + glAlphaFunc(GL_GREATER, 0); + glEnable(GL_ALPHA_TEST); + } +#endif + + glDepthMask(0); + +#ifndef BACKEND_AS_OPENGL_ES + if(g_Config.m_DbgGfx) + { + if(GLEW_KHR_debug || GLEW_ARB_debug_output) + { + // During init, enable debug output + if(GLEW_KHR_debug) + { + glEnable(GL_DEBUG_OUTPUT); + glDebugMessageCallback(GfxOpenGLMessageCallback, 0); + } + else if(GLEW_ARB_debug_output) + { + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB); + glDebugMessageCallbackARB(GfxOpenGLMessageCallback, 0); + } + dbg_msg("gfx", "Enabled OpenGL debug mode"); + } + else + dbg_msg("gfx", "Requested OpenGL debug mode, but the driver does not support the required extension"); + } +#endif + + return true; + } + else + return false; } -void CCommandProcessorFragment_OpenGL::Cmd_Init(const SCommand_Init *pCommand) +bool CCommandProcessorFragment_OpenGL::Cmd_Init(const SCommand_Init *pCommand) { - InitOpenGL(pCommand); + if(!InitOpenGL(pCommand)) + return false; m_pTextureMemoryUsage = pCommand->m_pTextureMemoryUsage; m_pTextureMemoryUsage->store(0, std::memory_order_relaxed); @@ -613,6 +621,8 @@ void CCommandProcessorFragment_OpenGL::Cmd_Init(const SCommand_Init *pCommand) m_LastBlendMode = CCommandBuffer::BLEND_ALPHA; m_LastClipEnable = false; + + return true; } void CCommandProcessorFragment_OpenGL::Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand) @@ -1584,9 +1594,10 @@ bool CCommandProcessorFragment_OpenGL2::IsTileMapAnalysisSucceeded() return NoError; } -void CCommandProcessorFragment_OpenGL2::Cmd_Init(const SCommand_Init *pCommand) +bool CCommandProcessorFragment_OpenGL2::Cmd_Init(const SCommand_Init *pCommand) { - CCommandProcessorFragment_OpenGL::Cmd_Init(pCommand); + if(!CCommandProcessorFragment_OpenGL::Cmd_Init(pCommand)) + return false; m_OpenGLTextureLodBIAS = g_Config.m_GfxOpenGLTextureLODBIAS; @@ -1747,7 +1758,11 @@ void CCommandProcessorFragment_OpenGL2::Cmd_Init(const SCommand_Init *pCommand) pCommand->m_pCapabilities->m_ContextMajor = 1; pCommand->m_pCapabilities->m_ContextMinor = 5; pCommand->m_pCapabilities->m_ContextPatch = 0; + + return false; } + + return true; } void CCommandProcessorFragment_OpenGL2::Cmd_RenderTex3D(const CCommandBuffer::SCommand_RenderTex3D *pCommand) diff --git a/src/engine/client/backend/opengl/backend_opengl.h b/src/engine/client/backend/opengl/backend_opengl.h index 8a8fb6e41..18ec24cbb 100644 --- a/src/engine/client/backend/opengl/backend_opengl.h +++ b/src/engine/client/backend/opengl/backend_opengl.h @@ -74,7 +74,7 @@ protected: bool IsTexturedState(const CCommandBuffer::SState &State); static bool Texture2DTo3D(void *pImageBuffer, int ImageWidth, int ImageHeight, int ImageColorChannelCount, int SplitCountWidth, int SplitCountHeight, void *pTarget3DImageData, int &Target3DImageWidth, int &Target3DImageHeight); - void InitOpenGL(const SCommand_Init *pCommand); + bool InitOpenGL(const SCommand_Init *pCommand); void SetState(const CCommandBuffer::SState &State, bool Use2DArrayTexture = false); virtual bool IsNewApi() { return false; } @@ -84,7 +84,7 @@ protected: static int TexFormatToImageColorChannelCount(int TexFormat); static void *Resize(int Width, int Height, int NewWidth, int NewHeight, int Format, const unsigned char *pData); - virtual void Cmd_Init(const SCommand_Init *pCommand); + virtual bool Cmd_Init(const SCommand_Init *pCommand); virtual void Cmd_Shutdown(const SCommand_Shutdown *pCommand) {} virtual void Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand); virtual void Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand); @@ -165,7 +165,7 @@ protected: void SetState(const CCommandBuffer::SState &State, CGLSLTWProgram *pProgram, bool Use2DArrayTextures = false); #ifndef BACKEND_GL_MODERN_API - void Cmd_Init(const SCommand_Init *pCommand) override; + bool Cmd_Init(const SCommand_Init *pCommand) override; void Cmd_RenderTex3D(const CCommandBuffer::SCommand_RenderTex3D *pCommand) override; diff --git a/src/engine/client/backend/opengl/backend_opengl3.cpp b/src/engine/client/backend/opengl/backend_opengl3.cpp index a81991789..82e0d8a3c 100644 --- a/src/engine/client/backend/opengl/backend_opengl3.cpp +++ b/src/engine/client/backend/opengl/backend_opengl3.cpp @@ -67,9 +67,10 @@ void CCommandProcessorFragment_OpenGL3_3::InitPrimExProgram(CGLSLPrimitiveExProg pProgram->SetUniformVec2(pProgram->m_LocCenter, 1, Center); } -void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand) +bool CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand) { - InitOpenGL(pCommand); + if(!InitOpenGL(pCommand)) + return false; m_OpenGLTextureLodBIAS = g_Config.m_GfxOpenGLTextureLODBIAS; @@ -483,6 +484,8 @@ void CCommandProcessorFragment_OpenGL3_3::Cmd_Init(const SCommand_Init *pCommand // fix the alignment to allow even 1byte changes, e.g. for alpha components glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + return true; } void CCommandProcessorFragment_OpenGL3_3::Cmd_Shutdown(const SCommand_Shutdown *pCommand) diff --git a/src/engine/client/backend/opengl/backend_opengl3.h b/src/engine/client/backend/opengl/backend_opengl3.h index c58aa6f10..0c62b450d 100644 --- a/src/engine/client/backend/opengl/backend_opengl3.h +++ b/src/engine/client/backend/opengl/backend_opengl3.h @@ -81,7 +81,7 @@ protected: void UploadStreamBufferData(unsigned int PrimitiveType, const void *pVertices, size_t VertSize, unsigned int PrimitiveCount, bool AsTex3D = false); void RenderText(const CCommandBuffer::SState &State, int DrawNum, int TextTextureIndex, int TextOutlineTextureIndex, int TextureSize, const float *pTextColor, const float *pTextOutlineColor); - void Cmd_Init(const SCommand_Init *pCommand) override; + bool Cmd_Init(const SCommand_Init *pCommand) override; void Cmd_Shutdown(const SCommand_Shutdown *pCommand) override; void Cmd_Texture_Update(const CCommandBuffer::SCommand_Texture_Update *pCommand) override; void Cmd_Texture_Destroy(const CCommandBuffer::SCommand_Texture_Destroy *pCommand) override; diff --git a/src/engine/client/backend_sdl.cpp b/src/engine/client/backend_sdl.cpp index e1523ade9..3e3ae2890 100644 --- a/src/engine/client/backend_sdl.cpp +++ b/src/engine/client/backend_sdl.cpp @@ -902,23 +902,20 @@ int CGraphicsBackend_SDL_OpenGL::Init(const char *pName, int *Screen, int *pWidt RunBuffer(&CmdBuffer); WaitForIdle(); CmdBuffer.Reset(); + } - if(InitError == -2) + if(InitError != 0) + { + if(InitError != -2) { + // shutdown the context, as it might have been initialized CCommandProcessorFragment_OpenGLBase::SCommand_Shutdown CmdGL; CmdBuffer.AddCommandUnsafe(CmdGL); RunBuffer(&CmdBuffer); WaitForIdle(); CmdBuffer.Reset(); - - g_Config.m_GfxOpenGLMajor = m_Capabilites.m_ContextMajor; - g_Config.m_GfxOpenGLMinor = m_Capabilites.m_ContextMinor; - g_Config.m_GfxOpenGLPatch = m_Capabilites.m_ContextPatch; } - } - if(InitError != 0) - { CCommandProcessorFragment_SDL::SCommand_Shutdown Cmd; CmdBuffer.AddCommandUnsafe(Cmd); RunBuffer(&CmdBuffer);