From 79422cfddd9ced069ea7b5da023dd945d9140c85 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Mon, 23 Mar 2026 19:40:50 +0800 Subject: [PATCH] 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 --- engine/src/Debug/RenderDocCapture.cpp | 5 +- tests/RHI/D3D12/integration/minimal/main.cpp | 49 ++++++------------- tests/RHI/OpenGL/integration/minimal/main.cpp | 39 ++++----------- 3 files changed, 28 insertions(+), 65 deletions(-) diff --git a/engine/src/Debug/RenderDocCapture.cpp b/engine/src/Debug/RenderDocCapture.cpp index 62b23903..21110720 100644 --- a/engine/src/Debug/RenderDocCapture.cpp +++ b/engine/src/Debug/RenderDocCapture.cpp @@ -29,7 +29,6 @@ bool RenderDocCapture::Initialize(void* device, void* window) { m_api->SetCaptureOptionU32(2, 1); m_api->SetCaptureOptionU32(8, 1); m_api->SetCaptureOptionU32(9, 1); - m_api->SetCaptureFilePathTemplate(".\\captures"); m_initialized = true; Logger::Get().Info(LogCategory::General, "RenderDocCapture initialized successfully"); @@ -176,6 +175,10 @@ void RenderDocCapture::TriggerCapture() { if (!m_isLoaded || !m_api) { return; } + if (m_window) { + SetForegroundWindow((HWND)m_window); + SetFocus((HWND)m_window); + } m_api->TriggerCapture(); } diff --git a/tests/RHI/D3D12/integration/minimal/main.cpp b/tests/RHI/D3D12/integration/minimal/main.cpp index 4e7c8aa7..2c68ceaa 100644 --- a/tests/RHI/D3D12/integration/minimal/main.cpp +++ b/tests/RHI/D3D12/integration/minimal/main.cpp @@ -229,12 +229,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine return -1; } - // Initialize RenderDoc capture (MUST be before D3D12 init) - if (!RenderDocCapture::Get().Initialize(nullptr, gHWND)) { - Log("[WARNING] Failed to initialize RenderDoc, frame capture will not be available"); - } else { - RenderDocCapture::Get().SetCaptureFilePath(".\\minimal_frame30"); - } + RenderDocCapture::Get().Initialize(nullptr, gHWND); + RenderDocCapture::Get().SetCaptureFilePath(".\\minimal_frame30"); // Initialize D3D12 if (!InitD3D12()) { @@ -277,19 +273,20 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine frameCount++; + EndRender(); + + // Execute + ExecuteCommandList(); + + if (frameCount >= targetFrameCount) { + RenderDocCapture::Get().TriggerCapture(); + Log("[INFO] RenderDoc capture triggered"); + } + + // Present + gSwapChain.Present(0, 0); + 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(); Log("[INFO] GPU idle, taking screenshot..."); bool screenshotResult = D3D12Screenshot::Capture( @@ -306,22 +303,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine Log("[INFO] RenderDoc capture completed"); 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); } } diff --git a/tests/RHI/OpenGL/integration/minimal/main.cpp b/tests/RHI/OpenGL/integration/minimal/main.cpp index bf61cae3..27a900ca 100644 --- a/tests/RHI/OpenGL/integration/minimal/main.cpp +++ b/tests/RHI/OpenGL/integration/minimal/main.cpp @@ -86,13 +86,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 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"); - } + RenderDocCapture::Get().Initialize(nullptr, hwnd); + RenderDocCapture::Get().SetCaptureFilePath(".\\minimal_frame30"); // Initialize OpenGL device with existing window OpenGLDevice device; @@ -101,8 +96,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 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); @@ -132,35 +125,21 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 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"); - } - } + wglMakeCurrent(device.GetDC(), device.GetContext()); - // 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 + if (frameCount >= targetFrameCount - 1) { + RenderDocCapture::Get().TriggerCapture(); + Log("[INFO] RenderDoc capture triggered"); + } + 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"); - } + break; } } }