Skip to content

Commit

Permalink
Refactored OpenGL support testing (#1761)
Browse files Browse the repository at this point in the history
This is an attempt to fix segfaults on some systems.
The OpenGL test helper class (sdl_window_test_helper) now uses internal
CHW instance just like DX11 (and DX9) renderer.
GLAD is to be initialized twice for sanity: once during testing and once
during renderer initialization (if OpenGL was selected)

CHW now correctly deletes created framebuffer on destroy.
  • Loading branch information
Xottab-DUTY committed Jan 12, 2025
1 parent 99e06cb commit 6df6613
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
47 changes: 35 additions & 12 deletions src/Layers/xrRenderGL/glHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GLADloadfunc>(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);
Expand All @@ -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;
Expand All @@ -147,7 +168,9 @@ void CHW::Reset()
ZoneScoped;

CHK_GL(glDeleteFramebuffers(1, &pFB));
pFB = 0;
UpdateViews();

UpdateVSync();
}

Expand Down
38 changes: 9 additions & 29 deletions src/Layers/xrRenderPC_GL/r2_test_hw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand All @@ -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;
}

0 comments on commit 6df6613

Please sign in to comment.