vulkan: fill buffer with initial data if supplied

This commit is contained in:
niki
2022-09-21 14:03:06 +02:00
parent 26ec23f4ba
commit 503b17a5fc
2 changed files with 14 additions and 2 deletions
+10 -2
View File
@@ -59,6 +59,7 @@ Buffer::Buffer(love::graphics::Graphics *gfx, const Settings &settings, const st
, usageFlags(settings.usageFlags) , usageFlags(settings.usageFlags)
, vgfx(dynamic_cast<Graphics*>(gfx)) , vgfx(dynamic_cast<Graphics*>(gfx))
, zeroInitialize(settings.zeroInitialize) , zeroInitialize(settings.zeroInitialize)
, initialData(data)
{ {
loadVolatile(); loadVolatile();
} }
@@ -86,6 +87,9 @@ bool Buffer::loadVolatile()
if (zeroInitialize) if (zeroInitialize)
vkCmdFillBuffer(vgfx->getCommandBufferForDataTransfer(), buffer, 0, VK_WHOLE_SIZE, 0); vkCmdFillBuffer(vgfx->getCommandBufferForDataTransfer(), buffer, 0, VK_WHOLE_SIZE, 0);
if (initialData)
fill(0, size, initialData);
if (usageFlags & BUFFERUSAGEFLAG_TEXEL) if (usageFlags & BUFFERUSAGEFLAG_TEXEL)
{ {
VkBufferViewCreateInfo bufferViewInfo{}; VkBufferViewCreateInfo bufferViewInfo{};
@@ -156,6 +160,8 @@ void *Buffer::map(MapType map, size_t offset, size_t size)
if (vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &stagingBuffer, &stagingAllocation, &stagingAllocInfo) != VK_SUCCESS) if (vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &stagingBuffer, &stagingAllocation, &stagingAllocInfo) != VK_SUCCESS)
throw love::Exception("failed to create staging buffer"); throw love::Exception("failed to create staging buffer");
mappedRange = Range(offset, size);
return stagingAllocInfo.pMappedData; return stagingAllocInfo.pMappedData;
} }
} }
@@ -188,7 +194,8 @@ bool Buffer::fill(size_t offset, size_t size, const void *data)
memcpy(fillAllocInfo.pMappedData, data, size); memcpy(fillAllocInfo.pMappedData, data, size);
VkBufferCopy bufferCopy{}; VkBufferCopy bufferCopy{};
bufferCopy.srcOffset = offset; bufferCopy.srcOffset = 0;
bufferCopy.dstOffset = offset;
bufferCopy.size = size; bufferCopy.size = size;
vkCmdCopyBuffer(vgfx->getCommandBufferForDataTransfer(), fillBuffer, buffer, 1, &bufferCopy); vkCmdCopyBuffer(vgfx->getCommandBufferForDataTransfer(), fillBuffer, buffer, 1, &bufferCopy);
@@ -205,7 +212,8 @@ void Buffer::unmap(size_t usedoffset, size_t usedsize)
if (dataUsage != BUFFERDATAUSAGE_READBACK) if (dataUsage != BUFFERDATAUSAGE_READBACK)
{ {
VkBufferCopy bufferCopy{}; VkBufferCopy bufferCopy{};
bufferCopy.srcOffset = usedoffset; bufferCopy.srcOffset = usedoffset - mappedRange.getOffset();
bufferCopy.dstOffset = usedoffset;
bufferCopy.size = usedsize; bufferCopy.size = usedsize;
vkCmdCopyBuffer(vgfx->getCommandBufferForDataTransfer(), stagingBuffer, buffer, 1, &bufferCopy); vkCmdCopyBuffer(vgfx->getCommandBufferForDataTransfer(), stagingBuffer, buffer, 1, &bufferCopy);
+4
View File
@@ -20,6 +20,8 @@
#pragma once #pragma once
#include "common/Range.h"
#include "graphics/Buffer.h" #include "graphics/Buffer.h"
#include "graphics/Volatile.h" #include "graphics/Volatile.h"
@@ -55,6 +57,7 @@ public:
private: private:
bool zeroInitialize; bool zeroInitialize;
const void *initialData;
VkBuffer buffer = VK_NULL_HANDLE; VkBuffer buffer = VK_NULL_HANDLE;
VkBuffer stagingBuffer = VK_NULL_HANDLE; VkBuffer stagingBuffer = VK_NULL_HANDLE;
VkBufferView bufferView = VK_NULL_HANDLE; VkBufferView bufferView = VK_NULL_HANDLE;
@@ -65,6 +68,7 @@ private:
VmaAllocationInfo allocInfo; VmaAllocationInfo allocInfo;
VmaAllocationInfo stagingAllocInfo; VmaAllocationInfo stagingAllocInfo;
BufferUsageFlags usageFlags; BufferUsageFlags usageFlags;
Range mappedRange;
}; };
} // vulkan } // vulkan