Skip to content

Commit 3ca6219

Browse files
author
devsh
committed
erase unsuccessfully converted (deps included) root GPU objects ASAP from CAssetConverter::SReserveResult to free up device memory
1 parent b3b2987 commit 3ca6219

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/nbl/video/utilities/CAssetConverter.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,11 +3596,27 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
35963596
return const_cast<core::blake3_hash_t*>(&found->second.value);
35973597
};
35983598
// wipe gpu item in staging cache (this may drop it as well if it was made for only a root asset == no users)
3599-
auto markFailureInStaging = [logger](const char* message, auto* gpuObj, core::blake3_hash_t* hash)->void
3599+
core::unordered_map<const IBackendObject*,uint32_t> outputReverseMap;
3600+
core::for_each_in_tuple(reservations.m_gpuObjects,[&outputReverseMap](const auto& gpuObjects)->void
3601+
{
3602+
uint32_t i = 0;
3603+
for (const auto& gpuObj : gpuObjects)
3604+
outputReverseMap[gpuObj.value.get()] = i++;
3605+
}
3606+
);
3607+
auto markFailureInStaging = [&reservations,&outputReverseMap,logger]<Asset AssetType>(const char* message, const asset_traits<AssetType>::video_t* gpuObj, core::blake3_hash_t* hash)->void
36003608
{
36013609
logger.log("%s failed for \"%s\"",system::ILogger::ELL_ERROR,message,gpuObj->getObjectDebugName());
36023610
// change the content hash on the reverse map to a NoContentHash
36033611
*hash = CHashCache::NoContentHash;
3612+
// also drop the smart pointer from the output array so failures release memory quickly
3613+
const auto foundIx = outputReverseMap.find(gpuObj);
3614+
if (foundIx!=outputReverseMap.end())
3615+
{
3616+
auto& resultOutput = std::get<SReserveResult::vector_t<AssetType>>(reservations.m_gpuObjects);
3617+
resultOutput[foundIx->second].value = nullptr;
3618+
outputReverseMap.erase(foundIx);
3619+
}
36043620
};
36053621

36063622
//
@@ -3683,7 +3699,7 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
36833699
item.canonical = nullptr;
36843700
if (!success)
36853701
{
3686-
markFailureInStaging("Data Upload",buffer,pFoundHash);
3702+
markFailureInStaging.operator()<ICPUBuffer>("Data Upload",buffer,pFoundHash);
36873703
continue;
36883704
}
36893705
submitsNeeded |= IQueue::FAMILY_FLAGS::TRANSFER_BIT;
@@ -3883,7 +3899,7 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
38833899
}
38843900
if (!quickWriteDescriptor(SrcMipBinding,srcIx,std::move(srcView)))
38853901
{
3886-
markFailureInStaging("Source Mip Level Descriptor Write",image,pFoundHash);
3902+
markFailureInStaging.operator()<ICPUImage>("Source Mip Level Descriptor Write",image,pFoundHash);
38873903
continue;
38883904
}
38893905
}
@@ -4191,7 +4207,7 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
41914207
// failed in the for-loop
41924208
if (lvl != creationParams.mipLevels)
41934209
{
4194-
markFailureInStaging("Compute Mip Mapping",image,pFoundHash);
4210+
markFailureInStaging.operator()<ICPUImage>("Compute Mip Mapping",image,pFoundHash);
41954211
continue;
41964212
}
41974213
}
@@ -4200,7 +4216,7 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
42004216
{
42014217
if (!pipelineBarrier(xferCmdBuf,{.memBarriers={},.bufBarriers={},.imgBarriers=transferBarriers},"Final Pipeline Barrier recording to Transfer Command Buffer failed"))
42024218
{
4203-
markFailureInStaging("Image Data Upload Pipeline Barrier",image,pFoundHash);
4219+
markFailureInStaging.operator()<ICPUImage>("Image Data Upload Pipeline Barrier",image,pFoundHash);
42044220
continue;
42054221
}
42064222
submitsNeeded |= IQueue::FAMILY_FLAGS::TRANSFER_BIT;
@@ -4210,7 +4226,7 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
42104226
dsAlloc->multi_deallocate(SrcMipBinding,1,&srcIx,params.compute->getFutureScratchSemaphore());
42114227
if (!pipelineBarrier(computeCmdBuf,{.memBarriers={},.bufBarriers={},.imgBarriers=computeBarriers},"Final Pipeline Barrier recording to Compute Command Buffer failed"))
42124228
{
4213-
markFailureInStaging("Compute Mip Mapping Pipeline Barrier",image,pFoundHash);
4229+
markFailureInStaging.operator()<ICPUImage>("Compute Mip Mapping Pipeline Barrier",image,pFoundHash);
42144230
continue;
42154231
}
42164232
}
@@ -4287,7 +4303,7 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
42874303
//
42884304
if (!device->buildAccelerationStructure(dOp.get(),info,range))
42894305
{
4290-
markFailureInStaging("BLAS Build Command Recording",gpuObj,pFoundHash);
4306+
markFailureInStaging.operator()<ICPUBottomLevelAccelerationStructure>("BLAS Build Command Recording",gpuObj,pFoundHash);
42914307
continue;
42924308
}
42934309
}
@@ -4353,7 +4369,7 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
43534369
for (const auto& info : buildInfos)
43544370
{
43554371
const auto pFoundHash = findInStaging.operator()<ICPUTopLevelAccelerationStructure>(info.dstAS);
4356-
markFailureInStaging("TLAS Build Command Recording",info.dstAS,pFoundHash); // TODO: make messages configurable message
4372+
markFailureInStaging.operator()<ICPUTopLevelAccelerationStructure>("TLAS Build Command Recording",info.dstAS,pFoundHash); // TODO: make messages configurable message
43574373
}
43584374
buildInfos.clear();
43594375
rangeInfos.clear();
@@ -4383,7 +4399,7 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
43834399
// problem with finding the dependents (BLASes)
43844400
if (instanceDataSize==0)
43854401
{
4386-
markFailureInStaging("Finding Dependant GPU BLASes for TLAS build",as,pFoundHash);
4402+
markFailureInStaging.operator()<ICPUTopLevelAccelerationStructure>("Finding Dependant GPU BLASes for TLAS build",as,pFoundHash);
43874403
continue;
43884404
}
43894405
// allocate scratch and build inputs
@@ -4485,7 +4501,7 @@ ISemaphore::future_t<IQueue::RESULT> CAssetConverter::convert_impl(SReserveResul
44854501
xferCmdBuf = params.transfer->getCommandBufferForRecording();
44864502
if (!success)
44874503
{
4488-
markFailureInStaging("Uploading Instance Data for TLAS build failed",as,pFoundHash);
4504+
markFailureInStaging.operator()<ICPUTopLevelAccelerationStructure>("Uploading Instance Data for TLAS build failed",as,pFoundHash);
44894505
continue;
44904506
}
44914507
}

0 commit comments

Comments
 (0)