Skip to content

Commit

Permalink
#5912: Reduce amount of duplicated code. Add some comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Mar 5, 2022
1 parent 15a6c9c commit 34100c9
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions radiantcore/rendersystem/backend/WindingRenderer.h
Expand Up @@ -61,6 +61,11 @@ class WindingRenderer final :
using VertexBuffer = CompactWindingVertexBuffer<ArbitraryMeshVertex, WindingIndexerT>;
static constexpr typename VertexBuffer::Slot InvalidVertexBufferSlot = std::numeric_limits<typename VertexBuffer::Slot>::max();
static constexpr IGeometryStore::Slot InvalidStorageHandle = std::numeric_limits<IGeometryStore::Slot>::max();

IGeometryStore& _geometryStore;
Shader* _owningShader;

// A Bucket holds all windings of a certain size (3,4,5...)
struct Bucket
{
Bucket(std::size_t size) :
Expand All @@ -83,16 +88,13 @@ class WindingRenderer final :
std::pair<typename VertexBuffer::Slot, typename VertexBuffer::Slot> modifiedSlotRange;
};

IGeometryStore& _geometryStore;
Shader* _owningShader;

// Maintain one bucket per winding size, allocated on demand
std::vector<Bucket> _buckets;

using BucketIndex = std::uint16_t;
static constexpr BucketIndex InvalidBucketIndex = std::numeric_limits<BucketIndex>::max();

// Stores the indices to a winding slot into a bucket, client code receives an index to a SlotMapping
// Stores the offset of a winding slot within a bucket, client code receives an index to a SlotMapping
struct SlotMapping
{
BucketIndex bucketIndex = InvalidBucketIndex;
Expand All @@ -106,6 +108,8 @@ class WindingRenderer final :

std::size_t _windingCount;

// Represents a group of windings associated to a single entity
// A winding is identified by its slot mapping index as used by the parent WindingRenderer
class WindingGroup :
public IRenderableObject
{
Expand Down Expand Up @@ -248,6 +252,7 @@ class WindingRenderer final :
}
};

// Internal helper to groups windings (slots) by entities
class EntityWindings
{
private:
Expand Down Expand Up @@ -311,21 +316,18 @@ class WindingRenderer final :
{
if (RenderingTraits<WindingIndexerT>::SupportsEntitySurfaces())
{
_entitySurfaces.reset(new EntityWindings(*this));
_entitySurfaces = std::make_unique<EntityWindings>(*this);
}
}

~WindingRenderer()
{
_entitySurfaces.reset();

// Release all storage allocations
for (auto& bucket : _buckets)
{
if (bucket.storageHandle != InvalidStorageHandle)
{
_geometryStore.deallocateSlot(bucket.storageHandle);
bucket.storageHandle = InvalidStorageHandle;
bucket.storageCapacity = 0;
}
deallocateStorage(bucket);
}
}

Expand Down Expand Up @@ -504,12 +506,7 @@ class WindingRenderer final :
if (numberOfStoredWindings == 0)
{
// Empty, deallocate the storage
if (bucket.storageHandle != InvalidStorageHandle)
{
_geometryStore.deallocateSlot(bucket.storageHandle);
bucket.storageHandle = InvalidStorageHandle;
bucket.storageCapacity = 0;
}
deallocateStorage(bucket);

bucket.modifiedSlotRange.first = InvalidVertexBufferSlot;
bucket.modifiedSlotRange.second = 0;
Expand All @@ -535,12 +532,7 @@ class WindingRenderer final :
{
// (Re-)allocate a chunk that is large enough
// Release the old one first
if (bucket.storageHandle != InvalidStorageHandle)
{
_geometryStore.deallocateSlot(bucket.storageHandle);
bucket.storageHandle = InvalidStorageHandle;
bucket.storageCapacity = 0;
}
deallocateStorage(bucket);

bucket.storageHandle = _geometryStore.allocateSlot(vertices.size(), indices.size());
bucket.storageCapacity = numberOfStoredWindings;
Expand Down Expand Up @@ -579,6 +571,15 @@ class WindingRenderer final :
bucket.modifiedSlotRange.second = 0;
}

void deallocateStorage(Bucket& bucket)
{
if (bucket.storageHandle == InvalidStorageHandle) return;

_geometryStore.deallocateSlot(bucket.storageHandle);
bucket.storageHandle = InvalidStorageHandle;
bucket.storageCapacity = 0;
}

void commitDeletions(BucketIndex bucketIndex)
{
auto& bucket = _buckets[bucketIndex];
Expand Down

0 comments on commit 34100c9

Please sign in to comment.