修复 NanoVDB 加载后渲染黑屏问题
问题原因: 1. RunNanoVDBTest 在 LoadNanoVDB 之后重复调用了 EndCommandList(), 而 LoadNanoVDB 内部已经完整处理了命令列表生命周期 2. 命令列表状态混乱导致后续渲染失效 修复内容: 1. 移除 RunNanoVDBTest 中重复的 EndCommandList/WaitForCompletion 调用 2. 在 main.cpp 添加控制台输出重定向,方便调试 3. 添加 run.bat 启动脚本
This commit is contained in:
36
main.cpp
36
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;
|
||||
|
||||
Reference in New Issue
Block a user