Skip to content

Commit

Permalink
Simplified the code in Vulkan writeBuffer function.
Browse files Browse the repository at this point in the history
  • Loading branch information
apanteleev committed Apr 5, 2024
1 parent 970bf53 commit a9e0ca1
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/vulkan/vulkan-buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,28 +419,23 @@ namespace nvrhi::vulkan
return;
}

const size_t commandBufferWriteLimit = 65536;
const size_t vkCmdUpdateBufferLimit = 65536;

if (dataSize <= commandBufferWriteLimit)
// Per Vulkan spec, vkCmdUpdateBuffer requires that the data size is smaller than or equal to 64 kB,
// and that the offset and data size are a multiple of 4. We can't change the offset, but data size
// is rounded up later.
if (dataSize <= vkCmdUpdateBufferLimit && (destOffsetBytes & 3) == 0)
{
if (m_EnableAutomaticBarriers)
{
requireBufferState(buffer, ResourceStates::CopyDest);
}
commitBarriers();

int64_t remaining = dataSize;
const char* base = (const char*)data;
while (remaining > 0)
{
// vulkan allows <= 64kb transfers via VkCmdUpdateBuffer
int64_t thisGulpSize = std::min(remaining, int64_t(commandBufferWriteLimit));
// Round up the write size to a multiple of 4
const size_t sizeToWrite = (dataSize + 3) & ~3ull;

// we bloat the read size here past the incoming buffer since the transfer must be a multiple of 4; the extra garbage should never be used anywhere
thisGulpSize = (thisGulpSize + 3) & ~3ll;
m_CurrentCmdBuf->cmdBuf.updateBuffer(buffer->buffer, destOffsetBytes + dataSize - remaining, thisGulpSize, &base[dataSize - remaining]);
remaining -= thisGulpSize;
}
m_CurrentCmdBuf->cmdBuf.updateBuffer(buffer->buffer, destOffsetBytes, sizeToWrite, data);
}
else
{
Expand Down

0 comments on commit a9e0ca1

Please sign in to comment.