Skip to content

Commit

Permalink
#5924: Increase number of frame buffers to 3, fix transaction log app…
Browse files Browse the repository at this point in the history
…lication
  • Loading branch information
codereader committed Mar 19, 2022
1 parent b8fb5d6 commit 7fc8f82
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
9 changes: 7 additions & 2 deletions libs/render/ContinuousBuffer.h
Expand Up @@ -224,8 +224,13 @@ class ContinuousBuffer
void applyTransactions(const std::vector<detail::BufferTransaction>& transactions, const ContinuousBuffer<ElementType>& other,
const std::function<std::uint32_t(IGeometryStore::Slot)>& getHandle)
{
// Ensure the buffer is the same size
_buffer.resize(other._buffer.size());
// Ensure the buffer is at least the same size
auto otherSize = other._buffer.size();

if (otherSize > _buffer.size())
{
_buffer.resize(otherSize);
}

for (const auto& transaction : transactions)
{
Expand Down
43 changes: 24 additions & 19 deletions libs/render/GeometryStore.h
Expand Up @@ -22,10 +22,7 @@ class GeometryStore :
IndexRemap = 1,
};

static constexpr auto NumFrameBuffers = 2;

// Keep track of modified slots as long as a single buffer is in use
std::vector<detail::BufferTransaction> _transactionLog;
static constexpr auto NumFrameBuffers = 3;

// Represents the storage for a single frame
struct FrameBuffer
Expand All @@ -38,10 +35,13 @@ class GeometryStore :
IBufferObject::Ptr vertexBufferObject;
IBufferObject::Ptr indexBufferObject;

void applyTransactions(const std::vector<detail::BufferTransaction>& transactions, const FrameBuffer& other)
// Keep track of modified slots as long as this buffer is in use
std::vector<detail::BufferTransaction> transactionLog;

void applyTransactions(const FrameBuffer& other)
{
vertices.applyTransactions(transactions, other.vertices, GetVertexSlot);
indices.applyTransactions(transactions, other.indices, GetIndexSlot);
vertices.applyTransactions(other.transactionLog, other.vertices, GetVertexSlot);
indices.applyTransactions(other.transactionLog, other.indices, GetIndexSlot);
}

void syncToBufferObjects()
Expand Down Expand Up @@ -75,9 +75,6 @@ class GeometryStore :
// Marks the beginning of a frame, switches to the next writing buffers
void onFrameStart()
{
// Switch to the next frame
auto& previous = getCurrentBuffer();

_currentBuffer = (_currentBuffer + 1) % NumFrameBuffers;
auto& current = getCurrentBuffer();

Expand All @@ -88,9 +85,17 @@ class GeometryStore :
current.syncObject.reset();
}

// Replay any modifications to the new buffer
current.applyTransactions(_transactionLog, previous);
_transactionLog.clear();
// Replay any modifications of all other buffers onto this one,
// in the order they are switched through
for (auto bufferIndex = (_currentBuffer + 1) % NumFrameBuffers;
bufferIndex != _currentBuffer;
bufferIndex = (bufferIndex + 1) % NumFrameBuffers)
{
current.applyTransactions(_frameBuffers[bufferIndex]);
}

// This buffer is in sync now, we can clear its log
current.transactionLog.clear();
}

std::pair<IBufferObject::Ptr, IBufferObject::Ptr> getBufferObjects() override
Expand Down Expand Up @@ -124,7 +129,7 @@ class GeometryStore :

auto slot = GetSlot(SlotType::Regular, vertexSlot, indexSlot);

_transactionLog.emplace_back(detail::BufferTransaction{
current.transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Allocate
});

Expand All @@ -148,7 +153,7 @@ class GeometryStore :
// In an IndexRemap slot, the vertex slot ID refers to the one containing the vertices
auto slot = GetSlot(SlotType::IndexRemap, GetVertexSlot(slotContainingVertexData), indexSlot);

_transactionLog.emplace_back(detail::BufferTransaction{
current.transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Allocate
});

Expand All @@ -173,7 +178,7 @@ class GeometryStore :
assert(!indices.empty());
current.indices.setData(GetIndexSlot(slot), indices);

_transactionLog.emplace_back(detail::BufferTransaction{
current.transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Update
});
}
Expand All @@ -196,7 +201,7 @@ class GeometryStore :
assert(!indices.empty());
current.indices.setSubData(GetIndexSlot(slot), indexOffset, indices);

_transactionLog.emplace_back(detail::BufferTransaction{
current.transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Update
});
}
Expand All @@ -216,7 +221,7 @@ class GeometryStore :

current.indices.resizeData(GetIndexSlot(slot), indexSize);

_transactionLog.emplace_back(detail::BufferTransaction{
current.transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Update
});
}
Expand All @@ -234,7 +239,7 @@ class GeometryStore :

current.indices.deallocate(GetIndexSlot(slot));

_transactionLog.emplace_back(detail::BufferTransaction{
current.transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Deallocate
});
}
Expand Down

0 comments on commit 7fc8f82

Please sign in to comment.