From 5267c61c2cb64041ac00ecbe72ce57919a578db5 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Mon, 23 Mar 2026 21:48:46 +0800 Subject: [PATCH] Fix OpenGL integration tests for new SwapChain API - triangle/quad/sphere tests: Update to use new Initialize signature with OpenGLDevice* - triangle/quad/sphere tests: Use GetGLContext() for RenderDoc SetDevice - triangle/quad/sphere tests: Add wglMakeCurrent in render loop - triangle/quad/sphere tests: Update capture timing to frame 25-35 - unit test fixture: Add GetDevice() method - unit test swap_chain: Update to use new Initialize signature --- tests/RHI/OpenGL/integration/quad/main.cpp | 40 +++++++++++------- tests/RHI/OpenGL/integration/sphere/main.cpp | 42 +++++++++++-------- .../RHI/OpenGL/integration/triangle/main.cpp | 40 +++++++++++------- .../OpenGL/unit/fixtures/OpenGLTestFixture.h | 1 + tests/RHI/OpenGL/unit/test_swap_chain.cpp | 6 +-- 5 files changed, 77 insertions(+), 52 deletions(-) diff --git a/tests/RHI/OpenGL/integration/quad/main.cpp b/tests/RHI/OpenGL/integration/quad/main.cpp index 4e19cd2b..f7635b6c 100644 --- a/tests/RHI/OpenGL/integration/quad/main.cpp +++ b/tests/RHI/OpenGL/integration/quad/main.cpp @@ -90,12 +90,18 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine RenderDocCapture::Get().SetCaptureFilePath(".\\quad_frame30"); OpenGLDevice device; - if (!device.InitializeWithExistingWindow(hwnd)) { + RHIDeviceDesc desc = {}; + desc.windowHandle = hwnd; + desc.width = gWidth; + desc.height = gHeight; + desc.appName = L"OpenGL_Quad_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); @@ -104,7 +110,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine Log("[INFO] OpenGL Version: %S", device.GetDeviceInfo().version.c_str()); OpenGLSwapChain swapChain; - swapChain.Initialize(hwnd, gWidth, gHeight); + swapChain.Initialize(&device, hwnd, gWidth, gHeight); OpenGLCommandList commandList; @@ -190,9 +196,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 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; @@ -200,6 +207,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine TranslateMessage(&msg); DispatchMessageW(&msg); } else { + wglMakeCurrent(device.GetPresentationDC(), device.GetGLContext()); + commandList.SetViewport(0, 0, gWidth, gHeight); commandList.Clear(0.0f, 0.0f, 1.0f, 1.0f, 1); @@ -211,24 +220,23 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine commandList.Draw(PrimitiveType::TriangleStrip, 4, 0); - if (frameCount >= targetFrameCount - 1) { - if (RenderDocCapture::Get().BeginCapture("OpenGL_Quad_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_Quad_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; } } } - Log("[INFO] Reached target frame count %d - taking screenshot!", targetFrameCount); + Log("[INFO] Capture complete - taking screenshot!"); OpenGLScreenshot::Capture(device, swapChain, "quad.ppm"); sampler.Shutdown(); diff --git a/tests/RHI/OpenGL/integration/sphere/main.cpp b/tests/RHI/OpenGL/integration/sphere/main.cpp index ee5059c5..ba887667 100644 --- a/tests/RHI/OpenGL/integration/sphere/main.cpp +++ b/tests/RHI/OpenGL/integration/sphere/main.cpp @@ -169,12 +169,18 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine RenderDocCapture::Get().SetCaptureFilePath(".\\sphere_frame30"); OpenGLDevice device; - if (!device.InitializeWithExistingWindow(hwnd)) { + RHIDeviceDesc desc = {}; + desc.windowHandle = hwnd; + desc.width = gWidth; + desc.height = gHeight; + desc.appName = L"OpenGL_Sphere_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); @@ -183,7 +189,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine Log("[INFO] OpenGL Version: %S", device.GetDeviceInfo().version.c_str()); OpenGLSwapChain swapChain; - swapChain.Initialize(hwnd, gWidth, gHeight); + swapChain.Initialize(&device, hwnd, gWidth, gHeight); OpenGLCommandList commandList; @@ -299,10 +305,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine MSG msg = {}; int frameCount = 0; - const int targetFrameCount = 30; + const int captureStartFrame = 25; + const int captureEndFrame = 35; int renderCount = 0; - while (frameCount < targetFrameCount) { + while (frameCount < captureEndFrame) { if (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; @@ -310,6 +317,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine TranslateMessage(&msg); DispatchMessageW(&msg); } else { + wglMakeCurrent(device.GetPresentationDC(), device.GetGLContext()); + renderCount++; GLenum err = glGetError(); if (err != GL_NO_ERROR) Log("[DEBUG] GL error before clear: 0x%x", err); @@ -336,25 +345,24 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine commandList.DrawIndexed(PrimitiveType::Triangles, (uint32_t)indices.size(), 0, 0); - if (frameCount >= targetFrameCount - 1) { - if (RenderDocCapture::Get().BeginCapture("OpenGL_Sphere_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_Sphere_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; } } } - Log("[INFO] Rendered %d frames (target was %d)", renderCount, targetFrameCount); - Log("[INFO] Reached target frame count %d - taking screenshot!", targetFrameCount); + Log("[INFO] Rendered %d frames (capture was %d-%d)", renderCount, captureStartFrame, captureEndFrame); + Log("[INFO] Capture complete - taking screenshot!"); char exePath[MAX_PATH]; GetModuleFileNameA(NULL, exePath, MAX_PATH); diff --git a/tests/RHI/OpenGL/integration/triangle/main.cpp b/tests/RHI/OpenGL/integration/triangle/main.cpp index dbfa323b..7f85ec8f 100644 --- a/tests/RHI/OpenGL/integration/triangle/main.cpp +++ b/tests/RHI/OpenGL/integration/triangle/main.cpp @@ -86,12 +86,18 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine RenderDocCapture::Get().SetCaptureFilePath(".\\triangle_frame30"); OpenGLDevice device; - if (!device.InitializeWithExistingWindow(hwnd)) { + RHIDeviceDesc desc = {}; + desc.windowHandle = hwnd; + desc.width = gWidth; + desc.height = gHeight; + desc.appName = L"OpenGL_Triangle_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); @@ -100,7 +106,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine Log("[INFO] OpenGL Version: %S", device.GetDeviceInfo().version.c_str()); OpenGLSwapChain swapChain; - swapChain.Initialize(hwnd, gWidth, gHeight); + swapChain.Initialize(&device, hwnd, gWidth, gHeight); OpenGLCommandList commandList; @@ -168,9 +174,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 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; @@ -178,6 +185,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine TranslateMessage(&msg); DispatchMessageW(&msg); } else { + wglMakeCurrent(device.GetPresentationDC(), device.GetGLContext()); + commandList.SetViewport(0, 0, gWidth, gHeight); commandList.Clear(0.0f, 0.0f, 1.0f, 1.0f, 1); @@ -185,24 +194,23 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine vertexArray.Bind(); commandList.Draw(PrimitiveType::Triangles, 3, 0); - if (frameCount >= targetFrameCount - 1) { - if (RenderDocCapture::Get().BeginCapture("OpenGL_Triangle_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_Triangle_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; } } } - Log("[INFO] Reached target frame count %d - taking screenshot!", targetFrameCount); + Log("[INFO] Capture complete - taking screenshot!"); OpenGLScreenshot::Capture(device, swapChain, "triangle.ppm"); vertexArray.Shutdown(); diff --git a/tests/RHI/OpenGL/unit/fixtures/OpenGLTestFixture.h b/tests/RHI/OpenGL/unit/fixtures/OpenGLTestFixture.h index bfc7efd7..8c55dc36 100644 --- a/tests/RHI/OpenGL/unit/fixtures/OpenGLTestFixture.h +++ b/tests/RHI/OpenGL/unit/fixtures/OpenGLTestFixture.h @@ -17,6 +17,7 @@ protected: HWND GetWindow() { return m_hwnd; } HDC GetDC() { return m_hdc; } HGLRC GetContext() { return m_hglrc; } + OpenGLDevice* GetDevice() { return m_device; } void MakeContextCurrent(); void DoneContextCurrent(); diff --git a/tests/RHI/OpenGL/unit/test_swap_chain.cpp b/tests/RHI/OpenGL/unit/test_swap_chain.cpp index c1934cd0..d253a928 100644 --- a/tests/RHI/OpenGL/unit/test_swap_chain.cpp +++ b/tests/RHI/OpenGL/unit/test_swap_chain.cpp @@ -7,7 +7,7 @@ TEST_F(OpenGLTestFixture, SwapChain_Initialize_Window) { OpenGLSwapChain swapChain; HWND window = GetWindow(); - bool result = swapChain.Initialize(window, 800, 600); + bool result = swapChain.Initialize(GetDevice(), window, 800, 600); ASSERT_TRUE(result); @@ -17,7 +17,7 @@ TEST_F(OpenGLTestFixture, SwapChain_Initialize_Window) { TEST_F(OpenGLTestFixture, SwapChain_Present) { OpenGLSwapChain swapChain; HWND window = GetWindow(); - swapChain.Initialize(window, 800, 600); + swapChain.Initialize(GetDevice(), window, 800, 600); swapChain.Present(); @@ -30,7 +30,7 @@ TEST_F(OpenGLTestFixture, SwapChain_Present) { TEST_F(OpenGLTestFixture, SwapChain_Resize_ChangesSize) { OpenGLSwapChain swapChain; HWND window = GetWindow(); - swapChain.Initialize(window, 800, 600); + swapChain.Initialize(GetDevice(), window, 800, 600); swapChain.Resize(1024, 768);