Skip to content

Commit

Permalink
sync triple buffering
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongfq committed Nov 24, 2020
1 parent 3c423ec commit 65a887b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class BufferMTL : public Buffer
void beginFrame();

private:
void updateIndex();
void inflightBuffer(std::size_t offset, std::size_t size);

id<MTLBuffer> _mtlBuffer = nil;
NSMutableArray* _dynamicDataBuffers = nil;
Expand Down
16 changes: 13 additions & 3 deletions frameworks/cocos2d-x/cocos/renderer/backend/metal/BufferMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ of this software and associated documentation files (the "Software"), to deal
void BufferMTL::updateData(void* data, std::size_t size)
{
assert(size <= _size);
updateIndex();
inflightBuffer(0, size);
memcpy((uint8_t*)_mtlBuffer.contents, data, size);
}

void BufferMTL::updateSubData(void* data, std::size_t offset, std::size_t size)
{
assert(offset + size <= _size);
updateIndex();
inflightBuffer(offset, size);
memcpy((uint8_t*)_mtlBuffer.contents + offset, data, size);
}

Expand All @@ -92,12 +92,22 @@ of this software and associated documentation files (the "Software"), to deal
_indexUpdated = false;
}

void BufferMTL::updateIndex()
void BufferMTL::inflightBuffer(std::size_t offset, std::size_t size)
{
if (BufferUsage::DYNAMIC == _usage && !_indexUpdated)
{
_currentFrameIndex = (_currentFrameIndex + 1) % MAX_INFLIGHT_BUFFER;
id<MTLBuffer> prevFrameBuffer = _mtlBuffer;
_mtlBuffer = _dynamicDataBuffers[_currentFrameIndex];
if(offset)
{
memcpy((uint8_t*)_mtlBuffer.contents, prevFrameBuffer.contents, offset);
}
offset += size;
if(offset < _size)
{
memcpy((uint8_t*)_mtlBuffer.contents + offset, (uint8_t*)prevFrameBuffer.contents + offset, _size - offset);
}
_indexUpdated = true;
}
}
Expand Down

0 comments on commit 65a887b

Please sign in to comment.