Skip to content

Commit 1e20efc

Browse files
author
devsh
committed
outline the TLAS compaction a bit more
1 parent 3433067 commit 1e20efc

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

src/nbl/video/IGPUCommandBuffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ bool IGPUCommandBuffer::writeAccelerationStructureProperties(const std::span<con
13401340
return false;
13411341
}
13421342

1343-
for (auto& as : pAccelerationStructures)
1343+
for (const auto* as : pAccelerationStructures)
13441344
{
13451345
if (!isCompatibleDevicewise(as))
13461346
{
@@ -1357,7 +1357,7 @@ bool IGPUCommandBuffer::writeAccelerationStructureProperties(const std::span<con
13571357
}
13581358

13591359
auto oit = cmd->getVariableCountResources();
1360-
for (auto& as : pAccelerationStructures)
1360+
for (const auto* as : pAccelerationStructures)
13611361
*(oit++) = core::smart_refctd_ptr<const core::IReferenceCounted>(as);
13621362
m_noCommands = false;
13631363
return writeAccelerationStructureProperties_impl(pAccelerationStructures, queryType, queryPool, firstQuery);

src/nbl/video/utilities/CAssetConverter.cpp

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4232,32 +4232,87 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
42324232
};
42334233
if (blasCount==0 || pipelineBarrier(computeCmdBuf,{.memBarriers={&readBLASInTLASBuildBarrier,1}},"Failed to sync BLAS with TLAS build!"))
42344234
{
4235-
core::vector<IGPUTopLevelAccelerationStructure*> compactions;
4235+
core::vector<const IGPUAccelerationStructure*> compactions;
42364236
compactions.reserve(tlasCount);
42374237
// build
42384238
for (const auto& tlasToBuild : tlasesToBuild)
42394239
{
4240-
// allocate scratch
4241-
// check dependents
4242-
// stream build infos
4243-
// record builds
4244-
// record compaction queries
4240+
// allocate scratch
4241+
// check dependents
4242+
// stream build infos
4243+
// record builds
42454244
}
42464245
computeCmdBuf->cmdbuf->endDebugMarker();
42474246
// no longer need this info
42484247
compactedBLASMap.clear();
42494248
// compact
42504249
computeCmdBuf->cmdbuf->beginDebugMarker("Asset Converter Compact TLASes");
4251-
// compact needs to wait for Build
4252-
if (!compactions.empty() && pipelineBarrier(computeCmdBuf,{.memBarriers={&readASInASCompactBarrier,1}},"Failed to sync Acceleration Structure builds with compactions!"))
4250+
// compact needs to wait for Build then record queries
4251+
if (!compactions.empty() &&
4252+
pipelineBarrier(computeCmdBuf,{.memBarriers={&readASInASCompactBarrier,1}},"Failed to sync Acceleration Structure builds with compactions!") &&
4253+
computeCmdBuf->cmdbuf->writeAccelerationStructureProperties(compactions,IQueryPool::TYPE::ACCELERATION_STRUCTURE_COMPACTED_SIZE,queryPool.get(),0)
4254+
)
42534255
{
4254-
// drain compute
4256+
// drain compute
42554257
// get queries
4256-
for (auto* tlas : compactions)
4258+
core::vector<size_t> sizes(compactions.size());
4259+
if (device->getQueryPoolResults(
4260+
queryPool.get(),0,compactions.size(),sizes.data(),sizeof(size_t),
4261+
bitflag(IQueryPool::RESULTS_FLAGS::WAIT_BIT)|IQueryPool::RESULTS_FLAGS::_64_BIT
4262+
))
42574263
{
4258-
// recreate Acceleration Structure
4259-
// record compaction
4260-
// insert into compaction map
4264+
auto logFail = [](const char* msg)->void
4265+
{
4266+
//TODO
4267+
};
4268+
auto itSize = sizes.data();
4269+
// create buffers
4270+
// recreate and compact Acceleration Structures
4271+
for (auto* pas : compactions)
4272+
{
4273+
const size_t compactedSize = *(itSize++);
4274+
const auto* as = static_cast<const IGPUTopLevelAccelerationStructure*>(pas);
4275+
// recreate Acceleration Structure
4276+
smart_refctd_ptr<IGPUBuffer> buff;
4277+
{
4278+
const auto* oldBuffer = as->getCreationParams().bufferRange.buffer.get();
4279+
//
4280+
IGPUBuffer::SCreationParams creationParams = { {.size=compactedSize,.usage=IGPUBuffer::E_USAGE_FLAGS::EUF_ACCELERATION_STRUCTURE_STORAGE_BIT},{} };
4281+
creationParams.queueFamilyIndexCount = oldBuffer->getCachedCreationParams().queueFamilyIndexCount;
4282+
//TODO creationParams.queueFamilyIndices = oldBuffer->;
4283+
buff = device->createBuffer(std::move(creationParams));
4284+
if (!buff)
4285+
{
4286+
logFail("create Buffer backing the Compacted Acceleration Structure");
4287+
continue;
4288+
}
4289+
// allocate new memory
4290+
auto bufReqs = buff->getMemoryReqs();
4291+
bufReqs.memoryTypeBits &= device->getPhysicalDevice()->getDeviceLocalMemoryTypeBits();
4292+
if (!params.compactedASAllocator->allocate(bufReqs,nullptr,IDeviceMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT).isValid())
4293+
{
4294+
logFail("allocate and bind memory to the Buffer backing the Compacted Acceleration Structure");
4295+
continue;
4296+
}
4297+
}
4298+
IGPUTopLevelAccelerationStructure::SCreationParams creationParams = {pas->getCreationParams()};
4299+
creationParams.bufferRange = {.offset=0,.size=compactedSize,.buffer=buff};
4300+
creationParams.maxInstanceCount = as->getMaxInstanceCount();
4301+
auto compactedAS = device->createTopLevelAccelerationStructure(std::move(creationParams));
4302+
if (!compactedAS)
4303+
{
4304+
logFail("create the Compacted Acceleration Structure");
4305+
continue;
4306+
}
4307+
// record compaction
4308+
if (!computeCmdBuf->cmdbuf->copyAccelerationStructure({.src=as,.dst=compactedAS.get(),.mode=IGPUAccelerationStructure::COPY_MODE::COMPACT}))
4309+
{
4310+
logFail("record Acceleration Structure compaction");
4311+
continue;
4312+
}
4313+
// insert into compaction map
4314+
compactedTLASMap[as] = std::move(compactedAS);
4315+
}
42614316
}
42624317
}
42634318
}

0 commit comments

Comments
 (0)