实现 NanoVDB 边界框可视化

修改内容:
1. NanoVDBLoader 添加边界框信息读取 (worldBBox[6])
2. BattleFireDirect 添加 Volume 渲染相关函数:
   - InitVolumeRootSignature(): 体积渲染的 root signature
   - CreateVolumePSO(): 线框模式 PSO
3. main.cpp:
   - 打印 NanoVDB 边界框信息
   - 创建立方体网格表示边界框
   - 渲染绿色线框立方体
4. 修复 CreateShaderFromFile 支持 #include
5. 修复 volume.hlsl 使用 32 位地址模式

验证结果:
- 成功显示 NanoVDB 边界框线框
- 着色器能正确读取 NanoVDB 数据
This commit is contained in:
2026-03-11 20:32:54 +08:00
parent bc58abc98f
commit af3b61a45a
6 changed files with 184 additions and 3 deletions

View File

@@ -74,6 +74,37 @@ ID3D12RootSignature* InitRootSignature() {
return d3d12RootSignature;
}
ID3D12RootSignature* InitVolumeRootSignature() {
D3D12_ROOT_PARAMETER rootParameters[2];
rootParameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[0].Descriptor.RegisterSpace = 0;
rootParameters[0].Descriptor.ShaderRegister = 1;
rootParameters[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
rootParameters[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[1].Descriptor.RegisterSpace = 0;
rootParameters[1].Descriptor.ShaderRegister = 1;
D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc = {};
rootSignatureDesc.NumParameters = _countof(rootParameters);
rootSignatureDesc.pParameters = rootParameters;
rootSignatureDesc.NumStaticSamplers = 0;
rootSignatureDesc.pStaticSamplers = nullptr;
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
ID3DBlob* signature;
HRESULT hResult = D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, nullptr);
ID3D12RootSignature* d3d12RootSignature;
gD3D12Device->CreateRootSignature(
0, signature->GetBufferPointer(), signature->GetBufferSize(),
IID_PPV_ARGS(&d3d12RootSignature));
return d3d12RootSignature;
}
void CreateShaderFromFile(
LPCTSTR inShaderFilePath,
const char* inMainFunctionName,
@@ -81,12 +112,16 @@ void CreateShaderFromFile(
D3D12_SHADER_BYTECODE* inShader) {
ID3DBlob* shaderBuffer = nullptr;
ID3DBlob* errorBuffer = nullptr;
HRESULT hResult = D3DCompileFromFile(inShaderFilePath, nullptr, nullptr,
HRESULT hResult = D3DCompileFromFile(inShaderFilePath, nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE,
inMainFunctionName, inTarget, D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION,
0, &shaderBuffer, &errorBuffer);
if (FAILED(hResult)) {
char szLog[1024] = {0};
strcpy(szLog, (char*)errorBuffer->GetBufferPointer());
if (errorBuffer) {
strcpy(szLog, (char*)errorBuffer->GetBufferPointer());
} else {
strcpy(szLog, "Unknown error");
}
printf("CreateShaderFromFile error : [%s][%s]:[%s]\n", inMainFunctionName, inTarget, szLog);
errorBuffer->Release();
return;
@@ -183,6 +218,69 @@ ID3D12PipelineState* CreatePSO(ID3D12RootSignature* inID3D12RootSignature,
return d3d12PSO;
}
ID3D12PipelineState* CreateVolumePSO(ID3D12RootSignature* inID3D12RootSignature,
D3D12_SHADER_BYTECODE inVertexShader, D3D12_SHADER_BYTECODE inPixelShader) {
D3D12_INPUT_ELEMENT_DESC vertexDataElementDesc[] = {
{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,0}
};
D3D12_INPUT_LAYOUT_DESC vertexDataLayoutDesc = {};
vertexDataLayoutDesc.NumElements = 1;
vertexDataLayoutDesc.pInputElementDescs = vertexDataElementDesc;
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.pRootSignature = inID3D12RootSignature;
psoDesc.VS = inVertexShader;
psoDesc.PS = inPixelShader;
psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
psoDesc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
psoDesc.SampleDesc.Count = 1;
psoDesc.SampleDesc.Quality = 0;
psoDesc.SampleMask = 0xffffffff;
psoDesc.InputLayout = vertexDataLayoutDesc;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE;
psoDesc.RasterizerState.FillMode = D3D12_FILL_MODE_WIREFRAME;
psoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
psoDesc.RasterizerState.FrontCounterClockwise = FALSE;
psoDesc.RasterizerState.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
psoDesc.RasterizerState.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
psoDesc.RasterizerState.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
psoDesc.RasterizerState.DepthClipEnable = TRUE;
psoDesc.RasterizerState.MultisampleEnable = FALSE;
psoDesc.RasterizerState.AntialiasedLineEnable = FALSE;
psoDesc.RasterizerState.ForcedSampleCount = 0;
psoDesc.RasterizerState.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
psoDesc.DepthStencilState.DepthEnable = TRUE;
psoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
psoDesc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
psoDesc.DepthStencilState.StencilEnable = FALSE;
psoDesc.BlendState.AlphaToCoverageEnable = FALSE;
psoDesc.BlendState.IndependentBlendEnable = FALSE;
for (int i = 0; i < D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) {
psoDesc.BlendState.RenderTarget[i].BlendEnable = FALSE;
psoDesc.BlendState.RenderTarget[i].LogicOpEnable = FALSE;
psoDesc.BlendState.RenderTarget[i].SrcBlend = D3D12_BLEND_ONE;
psoDesc.BlendState.RenderTarget[i].DestBlend = D3D12_BLEND_ZERO;
psoDesc.BlendState.RenderTarget[i].BlendOp = D3D12_BLEND_OP_ADD;
psoDesc.BlendState.RenderTarget[i].SrcBlendAlpha = D3D12_BLEND_ONE;
psoDesc.BlendState.RenderTarget[i].DestBlendAlpha = D3D12_BLEND_ZERO;
psoDesc.BlendState.RenderTarget[i].BlendOpAlpha = D3D12_BLEND_OP_ADD;
psoDesc.BlendState.RenderTarget[i].LogicOp = D3D12_LOGIC_OP_NOOP;
psoDesc.BlendState.RenderTarget[i].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
}
psoDesc.NumRenderTargets = 1;
ID3D12PipelineState* d3d12PSO = nullptr;
HRESULT hResult = gD3D12Device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&d3d12PSO));
if (FAILED(hResult)) {
printf("CreateVolumePSO failed: 0x%08X\n", hResult);
return nullptr;
}
return d3d12PSO;
}
bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
HRESULT hResult;
UINT dxgiFactoryFlags = 0;