Refactor OpenGL SwapChain HDC management

- OpenGLSwapChain now gets HDC from OpenGLDevice instead of creating its own
- Renamed OpenGLDevice::GetContext() to GetGLContext() for clarity
- Renamed OpenGLDevice::GetDC() to GetPresentationDC() for clarity
- OpenGLDevice now owns the HDC/HGLRC lifecycle
- OpenGLSwapChain::Initialize() now takes OpenGLDevice* parameter
- OpenGLSwapChain::Present() uses device's HDC for SwapBuffers
- Updated minimal test to use new API and capture from frame 25-35
- RenderDoc SetDevice now uses GetGLContext() for proper OpenGL hook
This commit is contained in:
2026-03-23 21:43:32 +08:00
parent 0fa4f2e3a8
commit 003d6ed630
5 changed files with 108 additions and 147 deletions

View File

@@ -1,11 +1,11 @@
#pragma once #pragma once
#include <string> #include <string>
#include <Windows.h>
#include "../RHIDevice.h" #include "../RHIDevice.h"
#include "../RHICapabilities.h" #include "../RHICapabilities.h"
struct HWND__;
struct HDC__; struct HDC__;
struct HGLRC__; struct HGLRC__;
@@ -13,9 +13,10 @@ namespace XCEngine {
namespace RHI { namespace RHI {
using HWND = HWND__*; using HWND = HWND__*;
using HDC = HDC__*;
using HGLRC = HGLRC__*; using HGLRC = HGLRC__*;
class OpenGLSwapChain;
class OpenGLDevice : public RHIDevice { class OpenGLDevice : public RHIDevice {
public: public:
OpenGLDevice(); OpenGLDevice();
@@ -24,12 +25,11 @@ public:
bool Initialize(const RHIDeviceDesc& desc) override; bool Initialize(const RHIDeviceDesc& desc) override;
void Shutdown() override; void Shutdown() override;
bool CreateRenderWindow(int width, int height, const char* title, bool enableDebug = false);
bool InitializeWithExistingWindow(HWND hwnd); bool InitializeWithExistingWindow(HWND hwnd);
bool CreateRenderWindow(int width, int height, const char* title, bool enableDebug = false);
HWND GetWindow() const { return m_hwnd; } HWND GetWindow() const { return m_hwnd; }
HDC GetDC() const { return m_hdc; } HDC GetPresentationDC() const { return m_hdc; }
HGLRC GetContext() const { return m_hglrc; } HGLRC GetGLContext() const { return m_hglrc; }
const RHIDeviceInfo& GetDeviceInfoImpl() const { return m_deviceInfo; } const RHIDeviceInfo& GetDeviceInfoImpl() const { return m_deviceInfo; }
void SwapBuffers(); void SwapBuffers();
@@ -54,14 +54,16 @@ public:
void* GetNativeHandle() const; void* GetNativeHandle() const;
private: private:
HWND m_hwnd; friend class OpenGLSwapChain;
HDC m_hdc;
HGLRC m_hglrc; HWND m_hwnd = nullptr;
HDC m_hdc = nullptr;
HGLRC m_hglrc = nullptr;
RHIDeviceInfo m_deviceInfo; RHIDeviceInfo m_deviceInfo;
RHICapabilities m_capabilities; RHICapabilities m_capabilities;
bool m_initialized; bool m_initialized = false;
bool m_ownsWindow; bool m_ownsWindow = false;
bool m_shouldClose; bool m_shouldClose = false;
}; };
} // namespace RHI } // namespace RHI

View File

@@ -4,6 +4,7 @@
#include "../RHISwapChain.h" #include "../RHISwapChain.h"
#include "OpenGLTexture.h" #include "OpenGLTexture.h"
#include "OpenGLDevice.h"
struct HWND__; struct HWND__;
struct HDC__; struct HDC__;
@@ -11,9 +12,6 @@ struct HDC__;
namespace XCEngine { namespace XCEngine {
namespace RHI { namespace RHI {
using HWND = HWND__*;
using HDC = HDC__*;
enum class PresentMode { enum class PresentMode {
Immediate, Immediate,
VSync, VSync,
@@ -33,8 +31,7 @@ public:
OpenGLSwapChain(); OpenGLSwapChain();
~OpenGLSwapChain() override; ~OpenGLSwapChain() override;
bool Initialize(HWND window, bool vsync = true); bool Initialize(OpenGLDevice* device, HWND window, int width, int height);
bool Initialize(HWND window, int width, int height, PresentMode mode = PresentMode::VSync);
void Shutdown() override; void Shutdown() override;
void Present(uint32_t syncInterval = 1, uint32_t flags = 0) override; void Present(uint32_t syncInterval = 1, uint32_t flags = 0) override;
@@ -52,7 +49,7 @@ public:
int GetFramebufferHeight() const { return m_framebufferHeight; } int GetFramebufferHeight() const { return m_framebufferHeight; }
HWND GetWindow() const { return m_hwnd; } HWND GetWindow() const { return m_hwnd; }
HDC GetDC() const { return m_hdc; } HDC GetDC() const { return m_device ? m_device->GetPresentationDC() : nullptr; }
bool ShouldClose() const override; bool ShouldClose() const override;
void SetShouldClose(bool shouldClose) override; void SetShouldClose(bool shouldClose) override;
@@ -63,17 +60,17 @@ public:
void* GetNativeHandle() override; void* GetNativeHandle() override;
private: private:
HWND m_hwnd; OpenGLDevice* m_device = nullptr;
HDC m_hdc; HWND m_hwnd = nullptr;
int m_width; int m_width = 0;
int m_height; int m_height = 0;
int m_framebufferWidth; int m_framebufferWidth = 0;
int m_framebufferHeight; int m_framebufferHeight = 0;
bool m_vsync; bool m_vsync = true;
bool m_shouldClose; bool m_shouldClose = false;
bool m_fullscreen; bool m_fullscreen = false;
PresentMode m_presentMode; PresentMode m_presentMode = PresentMode::VSync;
OpenGLTexture* m_backBufferTexture; OpenGLTexture* m_backBufferTexture = nullptr;
}; };
} // namespace RHI } // namespace RHI

View File

@@ -67,52 +67,6 @@ bool OpenGLDevice::Initialize(const RHIDeviceDesc& desc) {
return CreateRenderWindow(desc.width, desc.height, titleStr.c_str(), desc.enableDebugLayer); return CreateRenderWindow(desc.width, desc.height, titleStr.c_str(), desc.enableDebugLayer);
} }
bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title, bool enableDebug) {
if (m_initialized) {
return true;
}
if (!s_windowClassRegistered) {
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = DefWindowProcW;
wc.hInstance = GetModuleHandleW(nullptr);
wc.lpszClassName = kWindowClassName;
if (!RegisterClassExW(&wc)) {
return false;
}
s_windowClassRegistered = true;
}
std::wstring titleW(title ? std::wstring(title, title + strlen(title)) : L"XCEngine");
HWND hwnd = CreateWindowExW(
0,
kWindowClassName,
titleW.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
width, height,
nullptr,
nullptr,
GetModuleHandleW(nullptr),
nullptr
);
if (!hwnd) {
return false;
}
m_hwnd = hwnd;
m_ownsWindow = true;
ShowWindow(m_hwnd, SW_SHOWNORMAL);
UpdateWindow(m_hwnd);
return InitializeWithExistingWindow(m_hwnd);
}
bool OpenGLDevice::InitializeWithExistingWindow(HWND hwnd) { bool OpenGLDevice::InitializeWithExistingWindow(HWND hwnd) {
if (m_initialized) { if (m_initialized) {
return true; return true;
@@ -280,6 +234,52 @@ bool OpenGLDevice::InitializeWithExistingWindow(HWND hwnd) {
return true; return true;
} }
bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title, bool enableDebug) {
if (m_initialized) {
return true;
}
if (!s_windowClassRegistered) {
WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = DefWindowProcW;
wc.hInstance = GetModuleHandleW(nullptr);
wc.lpszClassName = kWindowClassName;
if (!RegisterClassExW(&wc)) {
return false;
}
s_windowClassRegistered = true;
}
std::wstring titleW(title ? std::wstring(title, title + strlen(title)) : L"XCEngine");
HWND hwnd = CreateWindowExW(
0,
kWindowClassName,
titleW.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
width, height,
nullptr,
nullptr,
GetModuleHandleW(nullptr),
nullptr
);
if (!hwnd) {
return false;
}
m_hwnd = hwnd;
m_ownsWindow = true;
ShowWindow(m_hwnd, SW_SHOWNORMAL);
UpdateWindow(m_hwnd);
return InitializeWithExistingWindow(m_hwnd);
}
void OpenGLDevice::Shutdown() { void OpenGLDevice::Shutdown() {
if (m_hglrc) { if (m_hglrc) {
wglMakeCurrent(nullptr, nullptr); wglMakeCurrent(nullptr, nullptr);
@@ -396,7 +396,7 @@ RHITexture* OpenGLDevice::CreateTexture(const TextureDesc& desc) {
RHISwapChain* OpenGLDevice::CreateSwapChain(const SwapChainDesc& desc) { RHISwapChain* OpenGLDevice::CreateSwapChain(const SwapChainDesc& desc) {
auto* swapChain = new OpenGLSwapChain(); auto* swapChain = new OpenGLSwapChain();
if (m_hwnd) { if (m_hwnd) {
swapChain->Initialize(m_hwnd, desc.width, desc.height); swapChain->Initialize(this, m_hwnd, desc.width, desc.height);
} }
return swapChain; return swapChain;
} }

View File

@@ -13,8 +13,8 @@ namespace XCEngine {
namespace RHI { namespace RHI {
OpenGLSwapChain::OpenGLSwapChain() OpenGLSwapChain::OpenGLSwapChain()
: m_hwnd(nullptr) : m_device(nullptr)
, m_hdc(nullptr) , m_hwnd(nullptr)
, m_width(0) , m_width(0)
, m_height(0) , m_height(0)
, m_framebufferWidth(0) , m_framebufferWidth(0)
@@ -30,42 +30,13 @@ OpenGLSwapChain::~OpenGLSwapChain() {
Shutdown(); Shutdown();
} }
bool OpenGLSwapChain::Initialize(HWND window, bool vsync) { bool OpenGLSwapChain::Initialize(OpenGLDevice* device, HWND window, int width, int height) {
m_device = device;
m_hwnd = window; m_hwnd = window;
m_hdc = ::GetDC(m_hwnd);
m_vsync = vsync;
m_presentMode = vsync ? PresentMode::VSync : PresentMode::Immediate;
m_shouldClose = false;
if (!wglSwapIntervalEXT) {
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
}
if (wglSwapIntervalEXT) {
wglSwapIntervalEXT(vsync ? 1 : 0);
}
RECT rect;
::GetClientRect(m_hwnd, &rect);
m_width = rect.right - rect.left;
m_height = rect.bottom - rect.top;
m_framebufferWidth = m_width;
m_framebufferHeight = m_height;
if (!m_backBufferTexture) {
m_backBufferTexture = new OpenGLTexture();
m_backBufferTexture->Initialize(OpenGLTextureType::Texture2D, m_width, m_height, 1, 1, OpenGLFormat::RGBA8, nullptr);
}
return true;
}
bool OpenGLSwapChain::Initialize(HWND window, int width, int height, PresentMode mode) {
m_hwnd = window;
m_hdc = ::GetDC(m_hwnd);
m_width = width; m_width = width;
m_height = height; m_height = height;
m_presentMode = mode; m_presentMode = PresentMode::VSync;
m_vsync = (mode == PresentMode::VSync || mode == PresentMode::Fifo); m_vsync = true;
m_shouldClose = false; m_shouldClose = false;
if (!wglSwapIntervalEXT) { if (!wglSwapIntervalEXT) {
@@ -95,16 +66,13 @@ void OpenGLSwapChain::Shutdown() {
delete m_backBufferTexture; delete m_backBufferTexture;
m_backBufferTexture = nullptr; m_backBufferTexture = nullptr;
} }
if (m_hdc && m_hwnd) {
::ReleaseDC(m_hwnd, m_hdc);
m_hdc = nullptr;
}
m_hwnd = nullptr; m_hwnd = nullptr;
m_device = nullptr;
} }
void OpenGLSwapChain::SwapBuffers() { void OpenGLSwapChain::SwapBuffers() {
if (m_hdc) { if (m_device) {
::SwapBuffers(m_hdc); ::SwapBuffers(m_device->GetPresentationDC());
} }
} }
@@ -144,8 +112,8 @@ void OpenGLSwapChain::PollEvents() {
} }
void OpenGLSwapChain::Present(uint32_t syncInterval, uint32_t flags) { void OpenGLSwapChain::Present(uint32_t syncInterval, uint32_t flags) {
if (m_hdc) { if (m_device) {
::SwapBuffers(m_hdc); ::SwapBuffers(m_device->GetPresentationDC());
} }
} }

View File

@@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include <windows.h> #include <windows.h>
#include <GL/gl.h>
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h" #include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
#include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h" #include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h"
@@ -42,18 +43,15 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
} }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
// Set RenderDoc environment variables for global capture
_putenv_s("RENDERDOC_CAPTUREINTERACTIVE", "0"); _putenv_s("RENDERDOC_CAPTUREINTERACTIVE", "0");
_putenv_s("RENDERDOC_CAPTUREFRAMESTART", "0"); _putenv_s("RENDERDOC_CAPTUREFRAMESTART", "0");
// Initialize logger
Logger::Get().Initialize(); Logger::Get().Initialize();
Logger::Get().AddSink(std::make_unique<ConsoleLogSink>()); Logger::Get().AddSink(std::make_unique<ConsoleLogSink>());
Logger::Get().SetMinimumLevel(LogLevel::Debug); Logger::Get().SetMinimumLevel(LogLevel::Debug);
Log("[INFO] OpenGL Integration Test Starting"); Log("[INFO] OpenGL Integration Test Starting");
// Register window class
WNDCLASSEXW wc = {}; WNDCLASSEXW wc = {};
wc.cbSize = sizeof(WNDCLASSEXW); wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = CS_HREDRAW | CS_VREDRAW; wc.style = CS_HREDRAW | CS_VREDRAW;
@@ -66,11 +64,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
return -1; return -1;
} }
// Calculate full window size from client area size
RECT rect = { 0, 0, gWidth, gHeight }; RECT rect = { 0, 0, gWidth, gHeight };
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE); AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
// Create window
HWND hwnd = CreateWindowExW( HWND hwnd = CreateWindowExW(
0, 0,
L"XCEngine_OpenGL_Test", L"XCEngine_OpenGL_Test",
@@ -89,35 +85,37 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
RenderDocCapture::Get().Initialize(nullptr, hwnd); RenderDocCapture::Get().Initialize(nullptr, hwnd);
RenderDocCapture::Get().SetCaptureFilePath(".\\minimal_frame30"); RenderDocCapture::Get().SetCaptureFilePath(".\\minimal_frame30");
// Initialize OpenGL device with existing window
OpenGLDevice device; OpenGLDevice device;
if (!device.InitializeWithExistingWindow(hwnd)) { RHIDeviceDesc desc = {};
desc.windowHandle = hwnd;
desc.width = gWidth;
desc.height = gHeight;
desc.appName = L"OpenGL_Minimal_Test";
desc.enableDebugLayer = true;
if (!device.Initialize(desc)) {
Log("[ERROR] Failed to initialize OpenGL device"); Log("[ERROR] Failed to initialize OpenGL device");
return -1; return -1;
} }
RenderDocCapture::Get().SetDevice(device.GetContext()); RenderDocCapture::Get().SetDevice(device.GetGLContext());
ShowWindow(hwnd, nShowCmd); ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd); UpdateWindow(hwnd);
// Log OpenGL device info
Log("[INFO] OpenGL Device: %S", device.GetDeviceInfo().renderer.c_str()); Log("[INFO] OpenGL Device: %S", device.GetDeviceInfo().renderer.c_str());
Log("[INFO] OpenGL Version: %S", device.GetDeviceInfo().version.c_str()); Log("[INFO] OpenGL Version: %S", device.GetDeviceInfo().version.c_str());
// Create swap chain for rendering
OpenGLSwapChain swapChain; OpenGLSwapChain swapChain;
swapChain.Initialize(hwnd, gWidth, gHeight); swapChain.Initialize(&device, hwnd, gWidth, gHeight);
// Create command list for rendering commands
OpenGLCommandList commandList; OpenGLCommandList commandList;
// Main render loop
MSG msg = {}; MSG msg = {};
int frameCount = 0; int frameCount = 0;
const int targetFrameCount = 30; const int captureStartFrame = 25;
const int captureEndFrame = 35;
while (frameCount < targetFrameCount) { while (frameCount < captureEndFrame) {
if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) { if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) { if (msg.message == WM_QUIT) {
break; break;
@@ -125,34 +123,30 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessageW(&msg); DispatchMessageW(&msg);
} else { } else {
wglMakeCurrent(device.GetDC(), device.GetContext()); wglMakeCurrent(device.GetPresentationDC(), device.GetGLContext());
commandList.SetViewport(0, 0, gWidth, gHeight); commandList.SetViewport(0, 0, gWidth, gHeight);
commandList.Clear(1.0f, 0.0f, 0.0f, 1.0f, 1 | 2); commandList.Clear(1.0f, 0.0f, 0.0f, 1.0f, 1 | 2);
if (frameCount >= targetFrameCount - 1) {
if (RenderDocCapture::Get().BeginCapture("OpenGL_Minimal_Test")) {
Log("[INFO] RenderDoc capture started");
}
}
swapChain.Present(0, 0); swapChain.Present(0, 0);
frameCount++; frameCount++;
if (frameCount >= targetFrameCount) { if (frameCount == captureStartFrame) {
if (RenderDocCapture::Get().EndCapture()) { RenderDocCapture::Get().BeginCapture("OpenGL_Minimal_Test");
Log("[INFO] RenderDoc capture ended"); Log("[INFO] RenderDoc capture started at frame %d", frameCount);
} }
if (frameCount == captureEndFrame) {
RenderDocCapture::Get().EndCapture();
Log("[INFO] RenderDoc capture ended at frame %d", frameCount);
break; break;
} }
} }
} }
// Take screenshot after target frame count is reached
Log("[INFO] Taking screenshot!"); Log("[INFO] Taking screenshot!");
OpenGLScreenshot::Capture(device, swapChain, "minimal.ppm"); OpenGLScreenshot::Capture(device, swapChain, "minimal.ppm");
// Shutdown in reverse order of initialization
RenderDocCapture::Get().Shutdown(); RenderDocCapture::Get().Shutdown();
swapChain.Shutdown(); swapChain.Shutdown();
device.Shutdown(); device.Shutdown();