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

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