Skip to content

Commit 89ec473

Browse files
committed
Allocation failure handling
1 parent 70e123c commit 89ec473

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

include/nbl/asset/utils/CCPUMeshPackerV1.h

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
#include <nbl/asset/utils/IMeshPacker.h>
1010
#include <nbl/core/math/intutil.h>
1111

12-
//AFTER SAFE SHRINK FIX TODO LIST:
13-
//1. extendend tests
14-
//2. test `getPackerCreationParamsFromMeshBufferRange`
15-
1612
namespace nbl
1713
{
1814
namespace asset
@@ -103,7 +99,6 @@ class CCPUMeshPackerV1 final : public IMeshPacker<ICPUMeshBuffer, MDIStructType>
10399

104100
inline PackerDataStore& getPackerDataStore() { return m_output; };
105101

106-
//TODO: update comment
107102
// returns number of distinct mesh packers needed to pack the meshes and a sorted list of meshes by the meshpacker ID they should be packed into, as well as the parameters for the packers
108103
// `packerParamsOut` should be big enough to fit `std::distance(begin,end)` entries, the return value will tell you how many were actually written
109104
template<typename Iterator>
@@ -191,21 +186,16 @@ CCPUMeshPackerV1<MDIStructType>::CCPUMeshPackerV1(const SVertexInputParams& preD
191186
);
192187
}
193188

194-
// TODO: why cant this implementation of `alloc` be common to both CPU and CPU?
195189
template <typename MDIStructType>
196190
//`Iterator` may be only an Iterator or pointer to pointer
191+
//allocation should be happening even if processed mesh buffer doesn't have attribute that was declared in pre defined `SVertexInputParams`, if mesh buffer has any attributes that are not declared in pre defined `SVertexInputParams` then these should be always ignored
192+
/*
193+
Requirements for input mesh buffers:
194+
- attributes bound to the same binding must have identical format
195+
*/
197196
template <typename MeshBufferIterator>
198197
typename CCPUMeshPackerV1<MDIStructType>::ReservedAllocationMeshBuffers CCPUMeshPackerV1<MDIStructType>::alloc(const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd)
199198
{
200-
/*
201-
Requirements for input mesh buffers:
202-
- attributes bound to the same binding must have identical format
203-
*/
204-
205-
//TODO:
206-
//allocation should be happening even if processed mesh buffer doesn't have attribute that was declared in pre defined `SVertexInputParams`, if mesh buffer has any attributes that are not declared in pre defined `SVertexInputParams` then these should be always ignored
207-
//include it in the ducumentation? YES
208-
209199
//validation
210200
for (auto it = mbBegin; it != mbEnd; it++)
211201
{

include/nbl/asset/utils/IMeshPacker.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class IMeshPackerBase : public virtual core::IReferenceCounted
7676
size_t MDIDataBuffMinAllocCnt = 32ull;
7777
};
7878

79-
// TODO: make a function which initializes allocators as a copy (incl resized copy)
8079
void initializeCommonAllocators(const AllocationParamsCommon& allocParams)
8180
{
8281
if (allocParams.indexBuffSupportedCnt)

include/nbl/asset/utils/IMeshPackerV2.h

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,40 @@ class IMeshPackerV2 : public IMeshPacker<MeshBufferType,MDIStructType>, public I
591591
template <typename MeshBufferIterator>
592592
bool alloc(ReservedAllocationMeshBuffers* rambOut, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd);
593593

594-
//TODO: test (free only part of the scene)
594+
template <typename MeshBufferIterator>
595+
void freeAllocatedAddressesOnAllocFail(ReservedAllocationMeshBuffers* rambOut, const MeshBufferIterator mbBegin, const MeshBufferIterator mbEnd)
596+
{
597+
size_t i = 0ull;
598+
for (auto it = mbBegin; it != mbEnd; it++)
599+
{
600+
ReservedAllocationMeshBuffers& ramb = *(rambOut + i);
601+
602+
if (ramb.indexAllocationOffset == INVALID_ADDRESS)
603+
return;
604+
605+
m_idxBuffAlctr.free_addr(ramb.indexAllocationOffset, ramb.indexAllocationReservedCnt);
606+
607+
const auto& mbVtxInputParams = (*it)->getPipeline()->getVertexInputParams();
608+
for (uint16_t attrBit = 0x0001, location = 0; location < SVertexInputParams::MAX_ATTR_BUF_BINDING_COUNT; attrBit <<= 1, location++)
609+
{
610+
if (!(attrBit & mbVtxInputParams.enabledAttribFlags))
611+
continue;
612+
613+
if (ramb.attribAllocParams[location].offset == INVALID_ADDRESS)
614+
return;
615+
616+
m_vtxBuffAlctr.free_addr(ramb.attribAllocParams[location].offset, ramb.attribAllocParams[location].size);
617+
}
618+
619+
if (ramb.mdiAllocationOffset == INVALID_ADDRESS)
620+
return;
621+
622+
m_MDIDataAlctr.free_addr(ramb.mdiAllocationOffset, ramb.mdiAllocationReservedCnt);
623+
624+
i++;
625+
}
626+
}
627+
595628
void free(const ReservedAllocationMeshBuffers* rambIn, uint32_t meshBuffersToFreeCnt)
596629
{
597630
for (uint32_t i = 0u; i < meshBuffersToFreeCnt; i++)
@@ -657,7 +690,10 @@ bool IMeshPackerV2<BufferType,DescriptorSetType,MeshBufferType,MDIStructType>::a
657690
//allocate indices
658691
ramb.indexAllocationOffset = m_idxBuffAlctr.alloc_addr(idxCnt, 1u);
659692
if (ramb.indexAllocationOffset == INVALID_ADDRESS)
693+
{
694+
freeAllocatedAddressesOnAllocFail(rambOut, mbBegin, mbEnd);
660695
return false;
696+
}
661697
ramb.indexAllocationReservedCnt = idxCnt;
662698

663699
//allocate vertices
@@ -681,12 +717,24 @@ bool IMeshPackerV2<BufferType,DescriptorSetType,MeshBufferType,MDIStructType>::a
681717
const uint32_t allocByteSize = maxVtxCnt * attribSize;
682718
ramb.attribAllocParams[location].offset = m_vtxBuffAlctr.alloc_addr(allocByteSize, attribSize);
683719
ramb.attribAllocParams[location].size = allocByteSize;
720+
721+
if(ramb.attribAllocParams[location].offset == INVALID_ADDRESS)
722+
{
723+
freeAllocatedAddressesOnAllocFail(rambOut, mbBegin, mbEnd);
724+
return false;
725+
}
684726
}
685727
else if (inputRate == EVIR_PER_INSTANCE)
686728
{
687729
const uint32_t allocByteSize = insCnt * attribSize;
688730
ramb.attribAllocParams[location].offset = m_vtxBuffAlctr.alloc_addr(allocByteSize, attribSize);
689731
ramb.attribAllocParams[location].size = allocByteSize;
732+
733+
if (ramb.attribAllocParams[location].offset == INVALID_ADDRESS)
734+
{
735+
freeAllocatedAddressesOnAllocFail(rambOut, mbBegin, mbEnd);
736+
return false;
737+
}
690738
}
691739

692740
if (ramb.attribAllocParams[location].offset == INVALID_ADDRESS)
@@ -701,7 +749,10 @@ bool IMeshPackerV2<BufferType,DescriptorSetType,MeshBufferType,MDIStructType>::a
701749

702750
ramb.mdiAllocationOffset = m_MDIDataAlctr.alloc_addr(possibleMDIStructsNeededCnt, 1u);
703751
if (ramb.mdiAllocationOffset == INVALID_ADDRESS)
752+
{
753+
freeAllocatedAddressesOnAllocFail(rambOut, mbBegin, mbEnd);
704754
return false;
755+
}
705756
ramb.mdiAllocationReservedCnt = possibleMDIStructsNeededCnt;
706757

707758
i++;

0 commit comments

Comments
 (0)