From d1a9530b0f1ec6d153e548dedde18ee2da0842c6 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Wed, 11 Mar 2026 20:01:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20NanoVDB=20=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=90=8E=E6=B8=B2=E6=9F=93=E9=BB=91=E5=B1=8F=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题原因: 1. RunNanoVDBTest 在 LoadNanoVDB 之后重复调用了 EndCommandList(), 而 LoadNanoVDB 内部已经完整处理了命令列表生命周期 2. 命令列表状态混乱导致后续渲染失效 修复内容: 1. 移除 RunNanoVDBTest 中重复的 EndCommandList/WaitForCompletion 调用 2. 在 main.cpp 添加控制台输出重定向,方便调试 3. 添加 run.bat 启动脚本 --- main.cpp | 36 +++++++++++++++++++++++++++++++----- run.bat | 4 ++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 run.bat diff --git a/main.cpp b/main.cpp index cca83c17..655987a6 100644 --- a/main.cpp +++ b/main.cpp @@ -40,9 +40,6 @@ void RunNanoVDBTest() { printf("[NanoVDB Test] Done.\n"); FreeNanoVDB(vdbData); - - EndCommandList(); - WaitForCompletionOfCommandList(); } LRESULT CALLBACK WindowProc(HWND inHWND, UINT inMSG, WPARAM inWParam, LPARAM inLParam) { @@ -54,6 +51,11 @@ LRESULT CALLBACK WindowProc(HWND inHWND, UINT inMSG, WPARAM inWParam, LPARAM inL return DefWindowProc(inHWND, inMSG, inWParam, inLParam); } int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int inShowCmd) { + AttachConsole(ATTACH_PARENT_PROCESS); + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); + printf("XCVolumeRenderer started\n"); + printf("Initializing D3D12...\n"); //register WNDCLASSEX wndClassEx; wndClassEx.cbSize = sizeof(WNDCLASSEX); @@ -99,22 +101,33 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi } //show InitD3D12(hwnd, 1280, 720); + printf("D3D12 initialized\n"); ID3D12GraphicsCommandList* commandList = GetCommandList(); ID3D12CommandAllocator* commandAllocator = GetCommandAllocator(); - StaticMeshComponent staticMeshComponent; - staticMeshComponent.InitFromFile(commandList, "Res/Model/Sphere.lhsm"); bool runTest = true; // set to true to run NanoVDB test if (runTest) { RunNanoVDBTest(); } + printf("After NanoVDB test\n"); + + StaticMeshComponent staticMeshComponent; + staticMeshComponent.InitFromFile(commandList, "Res/Model/Sphere.lhsm"); + printf("Mesh loaded, vertex count: %d\n", staticMeshComponent.mVertexCount); ID3D12RootSignature* rootSignature = InitRootSignature(); + printf("Root signature created\n"); D3D12_SHADER_BYTECODE vs,gs,ps; CreateShaderFromFile(L"Res/Shader/gs.hlsl", "MainVS", "vs_5_1", &vs); CreateShaderFromFile(L"Res/Shader/gs.hlsl", "MainGS", "gs_5_1", &gs); CreateShaderFromFile(L"Res/Shader/gs.hlsl", "MainPS", "ps_5_1", &ps); ID3D12PipelineState*pso=CreatePSO(rootSignature, vs, ps, gs); + printf("PSO created: %p\n", pso); + + ID3D12Device* device = GetD3DDevice(); + if (device->GetDeviceRemovedReason() != S_OK) { + printf("DEVICE REMOVED! Reason: 0x%08X\n", device->GetDeviceRemovedReason()); + } ID3D12Resource* cb = CreateConstantBufferObject(65536);//1024x64(4x4) DirectX::XMMATRIX projectionMatrix=DirectX::XMMatrixPerspectiveFovLH( @@ -154,8 +167,10 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi int imageWidth, imageHeight,imageChannel; //stbi_set_flip_vertically_on_load(true); stbi_uc* pixels = stbi_load("Res/Image/earth_d.jpg", &imageWidth, &imageHeight, &imageChannel, 4); + printf("Texture loaded: %dx%d, pixels=%p\n", imageWidth, imageHeight, pixels); ID3D12Resource* texture = CreateTexture2D(commandList, pixels, imageWidth * imageHeight * imageChannel, imageWidth, imageHeight,DXGI_FORMAT_R8G8B8A8_UNORM); + printf("Texture created: %p\n", texture); delete[]pixels; ID3D12Device* d3dDevice = GetD3DDevice(); @@ -180,13 +195,16 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi EndCommandList(); WaitForCompletionOfCommandList(); + printf("All resources uploaded to GPU\n"); ShowWindow(hwnd, inShowCmd); UpdateWindow(hwnd); + printf("Window shown, entering render loop\n"); float color[] = {0.5f,0.5f,0.5f,1.0f}; MSG msg; DWORD last_time = timeGetTime(); DWORD appStartTime = last_time; + int frameCount = 0; while (true){ ZeroMemory(&msg, sizeof(MSG)); if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { @@ -217,10 +235,18 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi commandList->SetGraphicsRootDescriptorTable(2, srvHeap->GetGPUDescriptorHandleForHeapStart()); commandList->SetGraphicsRootShaderResourceView(3, sb->GetGPUVirtualAddress()); commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + if (frameCount == 1) { + printf("Drawing mesh with %d vertices, VBO=%p, subMeshes=%zu\n", + staticMeshComponent.mVertexCount, staticMeshComponent.mVBO, staticMeshComponent.mSubMeshes.size()); + } staticMeshComponent.Render(commandList); EndRenderToSwapChain(commandList); EndCommandList(); SwapD3D12Buffers(); + frameCount++; + if (frameCount <= 3) { + printf("Frame %d rendered\n", frameCount); + } } } return 0; diff --git a/run.bat b/run.bat new file mode 100644 index 00000000..071dc800 --- /dev/null +++ b/run.bat @@ -0,0 +1,4 @@ +@echo off +cd /d "%~dp0" +Release\XCVolumeRenderer.exe +pause \ No newline at end of file