Fixing staging buffers not getting deleted

remove vgcore
This commit is contained in:
2023-11-06 15:08:27 +01:00
parent d35f7acddc
commit 4c567b60f3
69 changed files with 505 additions and 304 deletions
+21 -54
View File
@@ -18,22 +18,12 @@ SubAllocation::~SubAllocation()
owner->markFree(this);
}
constexpr VkDeviceMemory SubAllocation::getHandle() const
VkDeviceMemory SubAllocation::getHandle() const
{
return owner->getHandle();
}
constexpr VkDeviceSize SubAllocation::getSize() const
{
return size;
}
constexpr VkDeviceSize SubAllocation::getOffset() const
{
return alignedOffset;
}
constexpr bool SubAllocation::isReadable() const
bool SubAllocation::isReadable() const
{
return owner->isReadable();
}
@@ -203,30 +193,6 @@ void Allocation::markFree(PSubAllocation allocation)
}
}
constexpr VkDeviceMemory Allocation::getHandle() const
{
return allocatedMemory;
}
constexpr void* Allocation::getMappedPointer()
{
if (!canMap)
{
return nullptr;
}
if (!isMapped)
{
vkMapMemory(device, allocatedMemory, 0, bytesAllocated, 0, &mappedPointer);
isMapped = true;
}
return mappedPointer;
}
constexpr bool Allocation::isReadable() const
{
return readable;
}
void Allocation::flushMemory()
{
VkMappedMemoryRange range = {
@@ -260,6 +226,7 @@ Allocator::Allocator(PGraphics graphics)
VkMemoryHeap memoryHeap = memProperties.memoryHeaps[i];
HeapInfo heapInfo;
heapInfo.maxSize = memoryHeap.size;
std::cout << "Creating heap " << i << " with properties " << memoryHeap.flags << " size " << memoryHeap.size << std::endl;
heaps.add(std::move(heapInfo));
}
}
@@ -293,6 +260,7 @@ OSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2
{
OAllocation newAllocation = new Allocation(graphics, this, requirements.size, memoryTypeIndex, properties, dedicatedInfo);
heaps[heapIndex].inUse += newAllocation->bytesAllocated;
std::cout << "Heap " << heapIndex << " +" <<newAllocation->bytesAllocated << ": " << (float)heaps[heapIndex].inUse / heaps[heapIndex].maxSize << "%" << std::endl;
heaps[heapIndex].allocations.add(std::move(newAllocation));
return heaps[heapIndex].allocations.back()->getSuballocation(requirements.size, requirements.alignment);
}
@@ -312,7 +280,7 @@ OSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2
// no suitable allocations found, allocate new block
OAllocation newAllocation = new Allocation(graphics, this, (requirements.size > MemoryBlockSize) ? requirements.size : (VkDeviceSize)MemoryBlockSize, memoryTypeIndex, properties, nullptr);
heaps[heapIndex].inUse += newAllocation->bytesAllocated;
heaps[heapIndex].allocations.add(std::move(newAllocation));
std::cout << "Heap " << heapIndex << " +" <<newAllocation->bytesAllocated << ": " << (float)heaps[heapIndex].inUse / heaps[heapIndex].maxSize << "%" << std::endl; heaps[heapIndex].allocations.add(std::move(newAllocation));
return heaps[heapIndex].allocations.back()->getSuballocation(requirements.size, requirements.alignment);
}
@@ -321,12 +289,13 @@ void Allocator::free(Allocation *allocation)
std::scoped_lock lck(lock);
for (auto& heap : heaps)
{
for (uint32 i = 0; i < heap.allocations.size(); ++i)
for (uint32 heapIndex = 0; heapIndex < heap.allocations.size(); ++heapIndex)
{
if (heap.allocations[i] == allocation)
if (heap.allocations[heapIndex] == allocation)
{
heap.inUse -= allocation->bytesAllocated;
heap.allocations.removeAt(i, false);
std::cout << "Heap " << heapIndex << " -" <<allocation->bytesAllocated << ":" << (float)heaps[heapIndex].inUse / heaps[heapIndex].maxSize << "%" << std::endl;
heap.allocations.removeAt(heapIndex, false);
return;
}
}
@@ -374,31 +343,21 @@ void StagingBuffer::invalidateMemory()
allocation->invalidateMemory();
}
constexpr VkDeviceMemory StagingBuffer::getMemoryHandle() const
VkDeviceMemory StagingBuffer::getMemoryHandle() const
{
return allocation->getHandle();
}
constexpr VkDeviceSize StagingBuffer::getOffset() const
VkDeviceSize StagingBuffer::getOffset() const
{
return allocation->getOffset();
}
constexpr uint64 StagingBuffer::getSize() const
uint64 StagingBuffer::getSize() const
{
return allocation->getSize();
}
constexpr bool StagingBuffer::isReadable() const
{
return readable;
}
constexpr VkBufferUsageFlags StagingBuffer::getUsage() const
{
return usage;
}
StagingManager::StagingManager(PGraphics graphics, PAllocator allocator)
: graphics(graphics), allocator(allocator)
{
@@ -422,8 +381,9 @@ OStagingBuffer StagingManager::allocateStagingBuffer(uint64 size, VkBufferUsageF
{
//std::cout << "Reusing staging buffer" << std::endl;
activeBuffers.add(freeBuffer);
OStagingBuffer owner = std::move(freeBuffer);
freeBuffers.remove(it, false);
return std::move(freeBuffer);
return owner;
}
}
//std::cout << "Creating new stagingbuffer" << std::endl;
@@ -463,6 +423,8 @@ OStagingBuffer StagingManager::allocateStagingBuffer(uint64 size, VkBufferUsageF
);
vkBindBufferMemory(graphics->getDevice(), buffer, stagingBuffer->getMemoryHandle(), stagingBuffer->getOffset());
std::cout << "Creating new stagingbuffer size " << stagingBuffer->getSize() << std::endl;
activeBuffers.add(stagingBuffer);
return stagingBuffer;
@@ -471,6 +433,11 @@ OStagingBuffer StagingManager::allocateStagingBuffer(uint64 size, VkBufferUsageF
void StagingManager::releaseStagingBuffer(OStagingBuffer buffer)
{
std::scoped_lock l(lock);
if(activeBuffers.find(buffer) == activeBuffers.end())
{
return;
}
activeBuffers.remove(buffer);
std::cout << "Releasing stagingbuffer size " << buffer->getSize() << std::endl;
freeBuffers.add(std::move(buffer));
}
+51 -12
View File
@@ -17,10 +17,20 @@ class SubAllocation
public:
SubAllocation(PAllocation owner, VkDeviceSize allocatedOffset, VkDeviceSize size, VkDeviceSize alignedOffset, VkDeviceSize allocatedSize);
~SubAllocation();
constexpr VkDeviceMemory getHandle() const;
constexpr VkDeviceSize getSize() const;
constexpr VkDeviceSize getOffset() const;
constexpr bool isReadable() const;
VkDeviceMemory getHandle() const;
constexpr VkDeviceSize getSize() const
{
return size;
}
constexpr VkDeviceSize getOffset() const
{
return alignedOffset;
}
bool isReadable() const;
void *getMappedPointer();
void flushMemory();
void invalidateMemory();
@@ -43,9 +53,30 @@ public:
~Allocation();
OSubAllocation getSuballocation(VkDeviceSize size, VkDeviceSize alignment);
void markFree(PSubAllocation alloc);
constexpr VkDeviceMemory getHandle() const;
constexpr void* getMappedPointer();
constexpr bool isReadable() const;
constexpr VkDeviceMemory getHandle() const
{
return allocatedMemory;
}
constexpr void* getMappedPointer()
{
if (!canMap)
{
return nullptr;
}
if (!isMapped)
{
vkMapMemory(device, allocatedMemory, 0, bytesAllocated, 0, &mappedPointer);
isMapped = true;
}
return mappedPointer;
}
constexpr bool isReadable() const
{
return readable;
}
void flushMemory();
void invalidateMemory();
@@ -134,11 +165,19 @@ public:
{
return buffer;
}
constexpr VkDeviceMemory getMemoryHandle() const;
constexpr VkDeviceSize getOffset() const;
constexpr uint64 getSize() const;
constexpr bool isReadable() const;
constexpr VkBufferUsageFlags getUsage() const;
VkDeviceMemory getMemoryHandle() const;
VkDeviceSize getOffset() const;
uint64 getSize() const;
constexpr bool isReadable() const
{
return readable;
}
constexpr VkBufferUsageFlags getUsage() const
{
return usage;
}
private:
OSubAllocation allocation;
VkBuffer buffer;
+9 -2
View File
@@ -291,6 +291,7 @@ UniformBuffer::UniformBuffer(PGraphics graphics, const UniformBufferCreateInfo &
UniformBuffer::~UniformBuffer()
{
graphics->getStagingManager()->releaseStagingBuffer(std::move(dedicatedStagingBuffer));
}
bool UniformBuffer::updateContents(const DataSource &sourceData)
@@ -363,7 +364,12 @@ VkAccessFlags UniformBuffer::getDestAccessMask()
ShaderBuffer::ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo &sourceData)
: Gfx::ShaderBuffer(graphics->getFamilyMapping(), sourceData.stride, sourceData.sourceData.size / sourceData.stride, sourceData.sourceData)
, Vulkan::Buffer(graphics, sourceData.sourceData.size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, currentOwner, sourceData.dynamic)
, dedicatedStagingBuffer(nullptr)
{
if(sourceData.dynamic)
{
dedicatedStagingBuffer = graphics->getStagingManager()->allocateStagingBuffer(sourceData.sourceData.size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
}
if (sourceData.sourceData.data != nullptr)
{
void *data = lock();
@@ -374,6 +380,7 @@ ShaderBuffer::ShaderBuffer(PGraphics graphics, const ShaderBufferCreateInfo &sou
ShaderBuffer::~ShaderBuffer()
{
graphics->getStagingManager()->releaseStagingBuffer(std::move(dedicatedStagingBuffer));
}
bool ShaderBuffer::updateContents(const DataSource &sourceData)
@@ -422,13 +429,13 @@ void ShaderBuffer::requestOwnershipTransfer(Gfx::QueueType newOwner)
void ShaderBuffer::executeOwnershipBarrier(Gfx::QueueType newOwner)
{
Vulkan::ShaderBuffer::executeOwnershipBarrier(newOwner);
Vulkan::Buffer::executeOwnershipBarrier(newOwner);
}
void ShaderBuffer::executePipelineBarrier(VkAccessFlags srcAccess, VkPipelineStageFlags srcStage,
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage)
{
Vulkan::ShaderBuffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
Vulkan::Buffer::executePipelineBarrier(srcAccess, srcStage, dstAccess, dstStage);
}
VkAccessFlags ShaderBuffer::getSourceAccessMask()
+1 -2
View File
@@ -34,7 +34,7 @@ protected:
struct BufferAllocation
{
VkBuffer buffer;
PSubAllocation allocation;
OSubAllocation allocation;
};
PGraphics graphics;
uint32 currentBuffer;
@@ -140,6 +140,5 @@ protected:
VkAccessFlags dstAccess, VkPipelineStageFlags dstStage);
};
DEFINE_REF(IndexBuffer)
}
}
@@ -172,6 +172,8 @@ VkDeviceCreateInfo init::DeviceCreateInfo(VkDeviceQueueCreateInfo *queueInfos, u
createInfo.enabledLayerCount = layerCount;
createInfo.ppEnabledLayerNames = layers;
#else
(void)layers;
(void)layerCount;
createInfo.enabledLayerCount = 0;
layerCount = 0;
layers = nullptr;
+1 -1
View File
@@ -217,7 +217,7 @@ void TextureHandle::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Ar
auto prevlayout = layout;
changeLayout(Gfx::SE_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
PCmdBuffer cmdBuffer = graphics->getQueueCommands(currentOwner)->getCommands();
Gfx::FormatCompatibilityInfo formatInfo = Gfx::getFormatInfo(format);
//Gfx::FormatCompatibilityInfo formatInfo = Gfx::getFormatInfo(format);
VkBufferImageCopy region = {
.bufferOffset = 0,
.bufferRowLength = 0,