Files
XCEngine/tests/RHI/OpenGL/integration/minimal/main.cpp
ssdfasd effc969ad3 Update minimal tests to use improved RenderDocCapture API
- OpenGL test: Use BeginCapture/EndCapture with bool return values
- D3D12 test: Use BeginCapture/EndCapture with bool return values
- Both tests: Use GetCapture() to log capture file info
- Added capture file path and size logging after capture
2026-03-23 18:44:12 +08:00

180 lines
5.9 KiB
C++

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
#include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h"
#include "XCEngine/RHI/OpenGL/OpenGLCommandList.h"
#include "XCEngine/RHI/OpenGL/OpenGLScreenshot.h"
#include "XCEngine/Debug/Logger.h"
#include "XCEngine/Debug/ConsoleLogSink.h"
#include "XCEngine/Debug/RenderDocCapture.h"
#include "XCEngine/Containers/String.h"
#pragma comment(lib, "opengl32.lib")
using namespace XCEngine::RHI;
using namespace XCEngine::Debug;
using namespace XCEngine::Containers;
static const int gWidth = 1280;
static const int gHeight = 720;
void Log(const char* format, ...) {
char buffer[1024];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
Logger::Get().Debug(LogCategory::Rendering, String(buffer));
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, 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;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"XCEngine_OpenGL_Test";
if (!RegisterClassExW(&wc)) {
Log("[ERROR] Failed to register window class");
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",
L"OpenGL Integration Test",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
rect.right - rect.left, rect.bottom - rect.top,
NULL, NULL, hInstance, NULL
);
if (!hwnd) {
Log("[ERROR] Failed to create window");
return -1;
}
// Initialize RenderDoc capture (MUST be before OpenGL init)
// For OpenGL: device=nullptr (will be set via SetDevice), window=hwnd
if (!RenderDocCapture::Get().Initialize(nullptr, hwnd)) {
Log("[WARNING] Failed to initialize RenderDoc, frame capture will not be available");
} else {
RenderDocCapture::Get().SetCaptureFilePath(".\\minimal_frame30");
}
// Initialize OpenGL device with existing window
OpenGLDevice device;
if (!device.InitializeWithExistingWindow(hwnd)) {
Log("[ERROR] Failed to initialize OpenGL device");
return -1;
}
// Set device for RenderDoc (must be called after OpenGL init)
// For OpenGL, device pointer must be HGLRC, not HDC!
RenderDocCapture::Get().SetDevice(device.GetContext());
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);
// Create command list for rendering commands
OpenGLCommandList commandList;
// Main render loop
MSG msg = {};
int frameCount = 0;
const int targetFrameCount = 30;
while (frameCount < targetFrameCount) {
if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
break;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
} else {
if (frameCount == targetFrameCount - 1) {
wglMakeCurrent(device.GetDC(), device.GetContext());
if (RenderDocCapture::Get().BeginCapture("OpenGL_Minimal_Test")) {
Log("[INFO] RenderDoc capture started at frame %d", frameCount + 1);
} else {
Log("[ERROR] Failed to start RenderDoc capture");
}
}
// Set viewport and clear color using encapsulated command list
commandList.SetViewport(0, 0, gWidth, gHeight);
commandList.Clear(1.0f, 0.0f, 0.0f, 1.0f, 1 | 2);
// Present the rendered frame
swapChain.Present(0, 0);
frameCount++;
if (frameCount >= targetFrameCount) {
if (RenderDocCapture::Get().EndCapture()) {
Log("[INFO] RenderDoc capture ended, NumCaptures: %u", RenderDocCapture::Get().GetNumCaptures());
if (RenderDocCapture::Get().GetNumCaptures() > 0) {
RenderDocCaptureInfo info;
if (RenderDocCapture::Get().GetCapture(0, &info)) {
Log("[INFO] Capture file: %s (%u bytes)", info.filename, info.length);
}
}
} else {
Log("[ERROR] Failed to end RenderDoc capture");
}
}
}
}
// 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();
Logger::Get().Shutdown();
Log("[INFO] OpenGL Integration Test Finished");
return 0;
}