minimizing memory allocations in CmdCreateBLAS

This commit is contained in:
myT 2024-02-07 00:40:20 +01:00
parent a76dba5cfb
commit fedfff191e

View file

@ -752,6 +752,8 @@ namespace RHI
HBuffer raytracingScratchBuffer;
HBuffer raytracingInstanceBuffer;
uint32_t beginFrameCounter;
D3D12_RAYTRACING_GEOMETRY_DESC* rtGeoDescs;
uint32_t rtGeoDescCount;
// immediate-mode barrier API
TextureBarrier textureBarriers[64];
@ -3687,6 +3689,8 @@ namespace RHI
CloseHandle(rhi.frameLatencyWaitableObject);
}
free(rhi.rtGeoDescs);
rhi.upload.Release();
rhi.readback.Release();
rhi.mainFence.Release();
@ -5521,17 +5525,22 @@ namespace RHI
const D3D12_GPU_VIRTUAL_ADDRESS baseVertexAddress = rhi.buffers.Get(rhiDesc.vertexBuffer).gpuAddress;
const D3D12_GPU_VIRTUAL_ADDRESS baseIndexAddress = rhi.buffers.Get(rhiDesc.indexBuffer).gpuAddress;
D3D12_RAYTRACING_GEOMETRY_DESC* const geos =
(D3D12_RAYTRACING_GEOMETRY_DESC*)calloc(rhiDesc.meshCount, sizeof(D3D12_RAYTRACING_GEOMETRY_DESC));
if(geos == NULL)
if(rhiDesc.meshCount > rhi.rtGeoDescCount)
{
ri.Error(ERR_FATAL, "Failed to allocate %d D3D12_RAYTRACING_GEOMETRY_DESC instances\n", (int)rhiDesc.meshCount);
const uint32_t meshCount = max(rhiDesc.meshCount, 2 * rhi.rtGeoDescCount);
const size_t byteCount = meshCount * sizeof(D3D12_RAYTRACING_GEOMETRY_DESC);
rhi.rtGeoDescs = (D3D12_RAYTRACING_GEOMETRY_DESC*)realloc(rhi.rtGeoDescs, byteCount);
if(rhi.rtGeoDescs == NULL)
{
ri.Error(ERR_FATAL, "Failed to allocate %d D3D12_RAYTRACING_GEOMETRY_DESC instances\n", (int)meshCount);
}
rhi.rtGeoDescCount = rhiDesc.meshCount;
}
for(uint32_t i = 0; i < rhiDesc.meshCount; ++i)
{
const BLASMeshDesc& mesh = rhiDesc.meshes[i];
D3D12_RAYTRACING_GEOMETRY_DESC& geoDesc = geos[i];
D3D12_RAYTRACING_GEOMETRY_DESC& geoDesc = rhi.rtGeoDescs[i];
geoDesc.Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
geoDesc.Flags = mesh.isFullyOpaque ?
D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE :
@ -5551,7 +5560,7 @@ namespace RHI
inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_NONE;
inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
inputs.NumDescs = rhiDesc.meshCount;
inputs.pGeometryDescs = geos;
inputs.pGeometryDescs = rhi.rtGeoDescs;
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO info = {};
rhi.device->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &info);
@ -5574,8 +5583,6 @@ namespace RHI
rtasDesc.Inputs = inputs;
rhi.commandList->BuildRaytracingAccelerationStructure(&rtasDesc, 0, NULL);
free(geos);
CmdBeginBarrier();
CmdBufferBarrier(*blasBuffer, ResourceStates::UnorderedAccessBit);
CmdEndBarrier();