rhi: vulkan: use the vmaCopy* utility functions

- This simplifies the code from our point of view: the (invalidate-)map-
memcpy-unmap(-flush) dance is done by the VMA.
- In the read case, this adds an invalidate before the read.
- The flush/invalidate errors are handled and now print an error.

Change-Id: Ib29b9446796938f7b77c18976858a0ccf55c448a
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
This commit is contained in:
Aurélien Brooke 2025-05-06 18:46:59 +02:00
parent 30f5ad0329
commit 5d679be9ff

View File

@ -4082,16 +4082,13 @@ void QRhiVulkan::enqueueResourceUpdates(QVkCommandBuffer *cbD, QRhiResourceUpdat
} }
} }
void *p = nullptr; VkResult err = vmaCopyMemoryToAllocation(toVmaAllocator(allocator), u.data.constData(),
VmaAllocation a = toVmaAllocation(bufD->stagingAllocations[currentFrameSlot]); toVmaAllocation(bufD->stagingAllocations[currentFrameSlot]),
VkResult err = vmaMapMemory(toVmaAllocator(allocator), a, &p); u.offset, u.data.size());
if (err != VK_SUCCESS) { if (err != VK_SUCCESS) {
qWarning("Failed to map buffer: %d", err); qWarning("Failed to copy memory to buffer: %d", err);
continue; continue;
} }
memcpy(static_cast<uchar *>(p) + u.offset, u.data.constData(), u.data.size());
vmaFlushAllocation(toVmaAllocator(allocator), a, u.offset, u.data.size());
vmaUnmapMemory(toVmaAllocator(allocator), a);
trackedBufferBarrier(cbD, bufD, 0, trackedBufferBarrier(cbD, bufD, 0,
VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
@ -4130,13 +4127,13 @@ void QRhiVulkan::enqueueResourceUpdates(QVkCommandBuffer *cbD, QRhiResourceUpdat
QVkBuffer *bufD = QRHI_RES(QVkBuffer, u.buf); QVkBuffer *bufD = QRHI_RES(QVkBuffer, u.buf);
if (bufD->m_type == QRhiBuffer::Dynamic) { if (bufD->m_type == QRhiBuffer::Dynamic) {
executeBufferHostWritesForSlot(bufD, currentFrameSlot); executeBufferHostWritesForSlot(bufD, currentFrameSlot);
void *p = nullptr; u.result->data.resizeForOverwrite(u.readSize);
VmaAllocation a = toVmaAllocation(bufD->allocations[currentFrameSlot]); VkResult err = vmaCopyAllocationToMemory(toVmaAllocator(allocator),
VkResult err = vmaMapMemory(toVmaAllocator(allocator), a, &p); toVmaAllocation(bufD->allocations[currentFrameSlot]),
if (err == VK_SUCCESS) { u.offset, u.result->data.data(), u.readSize);
u.result->data.resize(u.readSize); if (err != VK_SUCCESS) {
memcpy(u.result->data.data(), reinterpret_cast<char *>(p) + u.offset, u.readSize); qWarning("Failed to copy memory from buffer: %d", err);
vmaUnmapMemory(toVmaAllocator(allocator), a); u.result->data.clear();
} }
if (u.result->completed) if (u.result->completed)
u.result->completed(); u.result->completed();
@ -4660,18 +4657,16 @@ void QRhiVulkan::finishActiveReadbacks(bool forced)
if (forced || currentFrameSlot == readback.activeFrameSlot || readback.activeFrameSlot < 0) { if (forced || currentFrameSlot == readback.activeFrameSlot || readback.activeFrameSlot < 0) {
readback.result->format = readback.format; readback.result->format = readback.format;
readback.result->pixelSize = readback.rect.size(); readback.result->pixelSize = readback.rect.size();
VmaAllocation a = toVmaAllocation(readback.stagingAlloc); readback.result->data.resizeForOverwrite(readback.byteSize);
void *p = nullptr; VkResult err = vmaCopyAllocationToMemory(toVmaAllocator(allocator),
VkResult err = vmaMapMemory(toVmaAllocator(allocator), a, &p); toVmaAllocation(readback.stagingAlloc),
if (err == VK_SUCCESS && p) { 0, readback.result->data.data(), readback.byteSize);
readback.result->data.resize(int(readback.byteSize)); if (err != VK_SUCCESS) {
memcpy(readback.result->data.data(), p, readback.byteSize); qWarning("Failed to copy texture readback buffer of size %u: %d", readback.byteSize, err);
vmaUnmapMemory(toVmaAllocator(allocator), a); readback.result->data.clear();
} else {
qWarning("Failed to map texture readback buffer of size %u: %d", readback.byteSize, err);
} }
vmaDestroyBuffer(toVmaAllocator(allocator), readback.stagingBuf, a); vmaDestroyBuffer(toVmaAllocator(allocator), readback.stagingBuf, toVmaAllocation(readback.stagingAlloc));
if (readback.result->completed) if (readback.result->completed)
completedCallbacks.append(readback.result->completed); completedCallbacks.append(readback.result->completed);
@ -4683,18 +4678,16 @@ void QRhiVulkan::finishActiveReadbacks(bool forced)
for (int i = activeBufferReadbacks.size() - 1; i >= 0; --i) { for (int i = activeBufferReadbacks.size() - 1; i >= 0; --i) {
const QRhiVulkan::BufferReadback &readback(activeBufferReadbacks[i]); const QRhiVulkan::BufferReadback &readback(activeBufferReadbacks[i]);
if (forced || currentFrameSlot == readback.activeFrameSlot || readback.activeFrameSlot < 0) { if (forced || currentFrameSlot == readback.activeFrameSlot || readback.activeFrameSlot < 0) {
VmaAllocation a = toVmaAllocation(readback.stagingAlloc); readback.result->data.resizeForOverwrite(readback.byteSize);
void *p = nullptr; VkResult err = vmaCopyAllocationToMemory(toVmaAllocator(allocator),
VkResult err = vmaMapMemory(toVmaAllocator(allocator), a, &p); toVmaAllocation(readback.stagingAlloc),
if (err == VK_SUCCESS && p) { 0, readback.result->data.data(), readback.byteSize);
readback.result->data.resize(readback.byteSize); if (err != VK_SUCCESS) {
memcpy(readback.result->data.data(), p, readback.byteSize); qWarning("Failed to copy buffer readback buffer of size %d: %d", readback.byteSize, err);
vmaUnmapMemory(toVmaAllocator(allocator), a); readback.result->data.clear();
} else {
qWarning("Failed to map buffer readback buffer of size %d: %d", readback.byteSize, err);
} }
vmaDestroyBuffer(toVmaAllocator(allocator), readback.stagingBuf, a); vmaDestroyBuffer(toVmaAllocator(allocator), readback.stagingBuf, toVmaAllocation(readback.stagingAlloc));
if (readback.result->completed) if (readback.result->completed)
completedCallbacks.append(readback.result->completed); completedCallbacks.append(readback.result->completed);