fix: simplify RenderDocCapture usage with TriggerCapture

- Remove BeginCapture/EndCapture complexity for manual frame capture
- Use TriggerCapture for single frame capture (simpler API)
- Move TriggerCapture before Present to ensure frame is captured
- Add SetForegroundWindow/SetFocus before TriggerCapture
- Remove default capture path in Initialize to allow SetCaptureFilePath to work
This commit is contained in:
2026-03-23 19:40:50 +08:00
parent 067c82c3a9
commit 79422cfddd
3 changed files with 28 additions and 65 deletions

View File

@@ -29,7 +29,6 @@ bool RenderDocCapture::Initialize(void* device, void* window) {
m_api->SetCaptureOptionU32(2, 1); m_api->SetCaptureOptionU32(2, 1);
m_api->SetCaptureOptionU32(8, 1); m_api->SetCaptureOptionU32(8, 1);
m_api->SetCaptureOptionU32(9, 1); m_api->SetCaptureOptionU32(9, 1);
m_api->SetCaptureFilePathTemplate(".\\captures");
m_initialized = true; m_initialized = true;
Logger::Get().Info(LogCategory::General, "RenderDocCapture initialized successfully"); Logger::Get().Info(LogCategory::General, "RenderDocCapture initialized successfully");
@@ -176,6 +175,10 @@ void RenderDocCapture::TriggerCapture() {
if (!m_isLoaded || !m_api) { if (!m_isLoaded || !m_api) {
return; return;
} }
if (m_window) {
SetForegroundWindow((HWND)m_window);
SetFocus((HWND)m_window);
}
m_api->TriggerCapture(); m_api->TriggerCapture();
} }

View File

@@ -229,12 +229,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
return -1; return -1;
} }
// Initialize RenderDoc capture (MUST be before D3D12 init) RenderDocCapture::Get().Initialize(nullptr, gHWND);
if (!RenderDocCapture::Get().Initialize(nullptr, gHWND)) { RenderDocCapture::Get().SetCaptureFilePath(".\\minimal_frame30");
Log("[WARNING] Failed to initialize RenderDoc, frame capture will not be available");
} else {
RenderDocCapture::Get().SetCaptureFilePath(".\\minimal_frame30");
}
// Initialize D3D12 // Initialize D3D12
if (!InitD3D12()) { if (!InitD3D12()) {
@@ -277,19 +273,20 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
frameCount++; frameCount++;
EndRender();
// Execute
ExecuteCommandList();
if (frameCount >= targetFrameCount) {
RenderDocCapture::Get().TriggerCapture();
Log("[INFO] RenderDoc capture triggered");
}
// Present
gSwapChain.Present(0, 0);
if (frameCount >= targetFrameCount) { 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");
}
ExecuteCommandList();
WaitForGPU(); WaitForGPU();
Log("[INFO] GPU idle, taking screenshot..."); Log("[INFO] GPU idle, taking screenshot...");
bool screenshotResult = D3D12Screenshot::Capture( bool screenshotResult = D3D12Screenshot::Capture(
@@ -306,22 +303,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
Log("[INFO] RenderDoc capture completed"); Log("[INFO] RenderDoc capture completed");
break; break;
} }
if (frameCount == targetFrameCount - 1) {
if (RenderDocCapture::Get().BeginCapture("D3D12_Minimal_Test")) {
Log("[INFO] RenderDoc capture started at frame %d", frameCount + 1);
} else {
Log("[ERROR] Failed to start RenderDoc capture");
}
}
EndRender();
// Execute
ExecuteCommandList();
// Present
gSwapChain.Present(0, 0);
} }
} }

View File

@@ -86,13 +86,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
return -1; return -1;
} }
// Initialize RenderDoc capture (MUST be before OpenGL init) RenderDocCapture::Get().Initialize(nullptr, hwnd);
// For OpenGL: device=nullptr (will be set via SetDevice), window=hwnd RenderDocCapture::Get().SetCaptureFilePath(".\\minimal_frame30");
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 // Initialize OpenGL device with existing window
OpenGLDevice device; OpenGLDevice device;
@@ -101,8 +96,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
return -1; 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()); RenderDocCapture::Get().SetDevice(device.GetContext());
ShowWindow(hwnd, nShowCmd); ShowWindow(hwnd, nShowCmd);
@@ -132,35 +125,21 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessageW(&msg); DispatchMessageW(&msg);
} else { } else {
if (frameCount == targetFrameCount - 1) { wglMakeCurrent(device.GetDC(), device.GetContext());
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.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);
// Present the rendered frame if (frameCount >= targetFrameCount - 1) {
RenderDocCapture::Get().TriggerCapture();
Log("[INFO] RenderDoc capture triggered");
}
swapChain.Present(0, 0); swapChain.Present(0, 0);
frameCount++; frameCount++;
if (frameCount >= targetFrameCount) { if (frameCount >= targetFrameCount) {
if (RenderDocCapture::Get().EndCapture()) { break;
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");
}
} }
} }
} }