Adding delayed destruction for resources

This commit is contained in:
2023-11-12 16:11:27 +01:00
parent c1e4891456
commit b3c9af384b
10 changed files with 92 additions and 23 deletions
+5 -5
View File
@@ -101,7 +101,7 @@ OSubAllocation Allocation::getSuballocation(VkDeviceSize requestedSize, VkDevice
activeAllocations.add(alloc); activeAllocations.add(alloc);
freeRanges.erase(lower); freeRanges.erase(lower);
bytesUsed += allocatedSize; bytesUsed += allocatedSize;
return std::move(alloc); return alloc;
} }
else if (allocatedSize < size) else if (allocatedSize < size)
{ {
@@ -112,7 +112,7 @@ OSubAllocation Allocation::getSuballocation(VkDeviceSize requestedSize, VkDevice
freeRanges.erase(lower); freeRanges.erase(lower);
freeRanges[newLower] = newSize; freeRanges[newLower] = newSize;
bytesUsed += allocatedSize; bytesUsed += allocatedSize;
return std::move(subAlloc); return subAlloc;
} }
} }
return nullptr; return nullptr;
@@ -212,7 +212,7 @@ OSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2
{ {
OAllocation newAllocation = new Allocation(graphics, this, requirements.size, memoryTypeIndex, properties, dedicatedInfo); OAllocation newAllocation = new Allocation(graphics, this, requirements.size, memoryTypeIndex, properties, dedicatedInfo);
heaps[heapIndex].inUse += newAllocation->bytesAllocated; heaps[heapIndex].inUse += newAllocation->bytesAllocated;
std::cout << "Heap " << heapIndex << " +" <<newAllocation->bytesAllocated << ": " << (float)heaps[heapIndex].inUse / heaps[heapIndex].maxSize << "%" << std::endl; std::cout << "Heap " << heapIndex << ": " << (float)heaps[heapIndex].inUse / heaps[heapIndex].maxSize << "%" << std::endl;
heaps[heapIndex].allocations.add(std::move(newAllocation)); heaps[heapIndex].allocations.add(std::move(newAllocation));
return heaps[heapIndex].allocations.back()->getSuballocation(requirements.size, requirements.alignment); return heaps[heapIndex].allocations.back()->getSuballocation(requirements.size, requirements.alignment);
} }
@@ -232,7 +232,7 @@ OSubAllocation Allocator::allocate(const VkMemoryRequirements2 &memRequirements2
// no suitable allocations found, allocate new block // no suitable allocations found, allocate new block
OAllocation newAllocation = new Allocation(graphics, this, (requirements.size > MemoryBlockSize) ? requirements.size : (VkDeviceSize)MemoryBlockSize, memoryTypeIndex, properties, nullptr); OAllocation newAllocation = new Allocation(graphics, this, (requirements.size > MemoryBlockSize) ? requirements.size : (VkDeviceSize)MemoryBlockSize, memoryTypeIndex, properties, nullptr);
heaps[heapIndex].inUse += newAllocation->bytesAllocated; 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)); std::cout << "Heap " << heapIndex << ": " << (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); return heaps[heapIndex].allocations.back()->getSuballocation(requirements.size, requirements.alignment);
} }
@@ -245,7 +245,7 @@ void Allocator::free(PAllocation allocation)
if (heaps[heapIndex].allocations[alloc] == allocation) if (heaps[heapIndex].allocations[alloc] == allocation)
{ {
heaps[heapIndex].inUse -= allocation->bytesAllocated; heaps[heapIndex].inUse -= allocation->bytesAllocated;
std::cout << "Heap " << heapIndex << " -" <<allocation->bytesAllocated << ": " << (float)heaps[heapIndex].inUse / heaps[heapIndex].maxSize << "%" << std::endl; std::cout << "Heap " << heapIndex << ": " << (float)heaps[heapIndex].inUse / heaps[heapIndex].maxSize << "%" << std::endl;
heaps[heapIndex].allocations.removeAt(alloc, false); heaps[heapIndex].allocations.removeAt(alloc, false);
return; return;
} }
+1 -1
View File
@@ -39,8 +39,8 @@ private:
PAllocation owner; PAllocation owner;
VkDeviceSize requestedSize; VkDeviceSize requestedSize;
VkDeviceSize allocatedOffset; VkDeviceSize allocatedOffset;
VkDeviceSize alignedOffset;
VkDeviceSize allocatedSize; VkDeviceSize allocatedSize;
VkDeviceSize alignedOffset;
friend class Allocation; friend class Allocation;
friend class Allocator; friend class Allocator;
}; };
+4
View File
@@ -61,6 +61,10 @@ Buffer::Buffer(PGraphics graphics, uint64 size, VkBufferUsageFlags usage, Gfx::Q
Buffer::~Buffer() Buffer::~Buffer()
{ {
for(uint32 i = 0; i < numBuffers; ++i)
{
graphics->getDestructionManager()->queueBuffer(graphics->getQueueCommands(owner)->getCommands(), buffers[i].buffer);
}
} }
VkDeviceSize Buffer::getOffset() const VkDeviceSize Buffer::getOffset() const
@@ -156,6 +156,7 @@ void CmdBuffer::refreshFence()
descriptor->unbind(); descriptor->unbind();
} }
boundDescriptors.clear(); boundDescriptors.clear();
graphics->getDestructionManager()->notifyCmdComplete(this);
state = State::ReadyBegin; state = State::ReadyBegin;
} }
} }
+14 -6
View File
@@ -49,6 +49,7 @@ void Graphics::init(GraphicsInitializer initInfo)
allocator = new Allocator(this); allocator = new Allocator(this);
stagingManager = new StagingManager(this, allocator); stagingManager = new StagingManager(this, allocator);
pipelineCache = new PipelineCache(this, "pipeline.cache"); pipelineCache = new PipelineCache(this, "pipeline.cache");
destructionManager = new DestructionManager(this);
} }
Gfx::OWindow Graphics::createWindow(const WindowCreateInfo &createInfo) Gfx::OWindow Graphics::createWindow(const WindowCreateInfo &createInfo)
@@ -164,31 +165,31 @@ Gfx::OVertexShader Graphics::createVertexShader(const ShaderCreateInfo& createIn
{ {
OVertexShader shader = new VertexShader(this); OVertexShader shader = new VertexShader(this);
shader->create(createInfo); shader->create(createInfo);
return std::move(shader); return shader;
} }
Gfx::OFragmentShader Graphics::createFragmentShader(const ShaderCreateInfo& createInfo) Gfx::OFragmentShader Graphics::createFragmentShader(const ShaderCreateInfo& createInfo)
{ {
OFragmentShader shader = new FragmentShader(this); OFragmentShader shader = new FragmentShader(this);
shader->create(createInfo); shader->create(createInfo);
return std::move(shader); return shader;
} }
Gfx::OComputeShader Graphics::createComputeShader(const ShaderCreateInfo& createInfo) Gfx::OComputeShader Graphics::createComputeShader(const ShaderCreateInfo& createInfo)
{ {
OComputeShader shader = new ComputeShader(this); OComputeShader shader = new ComputeShader(this);
shader->create(createInfo); shader->create(createInfo);
return std::move(shader); return shader;
} }
Gfx::OTaskShader Graphics::createTaskShader(const ShaderCreateInfo& createInfo) Gfx::OTaskShader Graphics::createTaskShader(const ShaderCreateInfo& createInfo)
{ {
OTaskShader shader = new TaskShader(this); OTaskShader shader = new TaskShader(this);
shader->create(createInfo); shader->create(createInfo);
return std::move(shader); return shader;
} }
Gfx::OMeshShader Graphics::createMeshShader(const ShaderCreateInfo& createInfo) Gfx::OMeshShader Graphics::createMeshShader(const ShaderCreateInfo& createInfo)
{ {
OMeshShader shader = new MeshShader(this); OMeshShader shader = new MeshShader(this);
shader->create(createInfo); shader->create(createInfo);
return std::move(shader); return shader;
} }
Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo createInfo) Gfx::PGraphicsPipeline Graphics::createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo createInfo)
@@ -227,12 +228,14 @@ Gfx::OSamplerState Graphics::createSamplerState(const SamplerCreateInfo& createI
vkInfo.mipmapMode = cast(createInfo.mipmapMode); vkInfo.mipmapMode = cast(createInfo.mipmapMode);
vkInfo.unnormalizedCoordinates = createInfo.unnormalizedCoordinates; vkInfo.unnormalizedCoordinates = createInfo.unnormalizedCoordinates;
VK_CHECK(vkCreateSampler(handle, &vkInfo, nullptr, &sampler->sampler)); VK_CHECK(vkCreateSampler(handle, &vkInfo, nullptr, &sampler->sampler));
return std::move(sampler); return sampler;
} }
Gfx::ODescriptorLayout Graphics::createDescriptorLayout(const std::string& name) Gfx::ODescriptorLayout Graphics::createDescriptorLayout(const std::string& name)
{ {
return new DescriptorLayout(this, name); return new DescriptorLayout(this, name);
} }
Gfx::OPipelineLayout Graphics::createPipelineLayout(Gfx::PPipelineLayout baseLayout) Gfx::OPipelineLayout Graphics::createPipelineLayout(Gfx::PPipelineLayout baseLayout)
{ {
return new PipelineLayout(this, baseLayout); return new PipelineLayout(this, baseLayout);
@@ -367,6 +370,11 @@ PStagingManager Graphics::getStagingManager()
return stagingManager; return stagingManager;
} }
PDestructionManager Graphics::getDestructionManager()
{
return destructionManager;
}
Array<const char *> Graphics::getRequiredExtensions() Array<const char *> Graphics::getRequiredExtensions()
{ {
Array<const char *> extensions; Array<const char *> extensions;
+3
View File
@@ -8,6 +8,7 @@ namespace Vulkan
{ {
DECLARE_REF(Allocator) DECLARE_REF(Allocator)
DECLARE_REF(StagingManager) DECLARE_REF(StagingManager)
DECLARE_REF(DestructionManager)
DECLARE_REF(CommandBufferManager) DECLARE_REF(CommandBufferManager)
DECLARE_REF(Queue) DECLARE_REF(Queue)
DECLARE_REF(RenderPass) DECLARE_REF(RenderPass)
@@ -34,6 +35,7 @@ public:
PAllocator getAllocator(); PAllocator getAllocator();
PStagingManager getStagingManager(); PStagingManager getStagingManager();
PDestructionManager getDestructionManager();
// Inherited via Graphics // Inherited via Graphics
virtual void init(GraphicsInitializer initializer) override; virtual void init(GraphicsInitializer initializer) override;
@@ -107,6 +109,7 @@ protected:
Map<uint32, OFramebuffer> allocatedFramebuffers; Map<uint32, OFramebuffer> allocatedFramebuffers;
OAllocator allocator; OAllocator allocator;
OStagingManager stagingManager; OStagingManager stagingManager;
ODestructionManager destructionManager;
friend class Window; friend class Window;
}; };
+43
View File
@@ -80,6 +80,49 @@ void Fence::wait(uint32 timeout)
} }
} }
DestructionManager::DestructionManager(PGraphics graphics)
: graphics(graphics)
{
}
DestructionManager::~DestructionManager()
{
}
void DestructionManager::queueBuffer(PCmdBuffer cmd, VkBuffer buffer)
{
buffers[cmd].add(buffer);
}
void DestructionManager::queueImage(PCmdBuffer cmd, VkImage image)
{
images[cmd].add(image);
}
void DestructionManager::queueImageView(PCmdBuffer cmd, VkImageView image)
{
views[cmd].add(image);
}
void DestructionManager::notifyCmdComplete(PCmdBuffer cmdbuffer)
{
for(auto buf : buffers[cmdbuffer])
{
vkDestroyBuffer(graphics->getDevice(), buf, nullptr);
}
for(auto view : views[cmdbuffer])
{
vkDestroyImageView(graphics->getDevice(), view, nullptr);
}
for(auto img : images[cmdbuffer])
{
vkDestroyImage(graphics->getDevice(), img, nullptr);
}
buffers[cmdbuffer].clear();
images[cmdbuffer].clear();
views[cmdbuffer].clear();
}
VertexDeclaration::VertexDeclaration(const Array<Gfx::VertexElement>& elementList) VertexDeclaration::VertexDeclaration(const Array<Gfx::VertexElement>& elementList)
: elementList(elementList) : elementList(elementList)
{ {
+18 -4
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <functional> #include <functional>
#include "Containers/List.h"
#include "Graphics/Resources.h" #include "Graphics/Resources.h"
#include "Allocator.h" #include "Allocator.h"
@@ -42,10 +43,6 @@ public:
return fence; return fence;
} }
void wait(uint32 timeout); void wait(uint32 timeout);
/*Event& operator co_await()
{
return signaled;
}*/
bool operator<(const Fence &other) const bool operator<(const Fence &other) const
{ {
return fence < other.fence; return fence < other.fence;
@@ -58,6 +55,23 @@ private:
}; };
DEFINE_REF(Fence) DEFINE_REF(Fence)
class DestructionManager
{
public:
DestructionManager(PGraphics graphics);
~DestructionManager();
void queueBuffer(PCmdBuffer cmd, VkBuffer buffer);
void queueImage(PCmdBuffer cmd, VkImage image);
void queueImageView(PCmdBuffer cmd, VkImageView view);
void notifyCmdComplete(PCmdBuffer cmdbuffer);
private:
PGraphics graphics;
Map<PCmdBuffer, List<VkBuffer>> buffers;
Map<PCmdBuffer, List<VkImage>> images;
Map<PCmdBuffer, List<VkImageView>> views;
};
DEFINE_REF(DestructionManager)
class VertexDeclaration : public Gfx::VertexDeclaration class VertexDeclaration : public Gfx::VertexDeclaration
{ {
public: public:
+1
View File
@@ -3,6 +3,7 @@
#include "slang.h" #include "slang.h"
#include "slang-com-ptr.h" #include "slang-com-ptr.h"
#include "stdlib.h" #include "stdlib.h"
#include <format>
using namespace Seele; using namespace Seele;
using namespace Seele::Vulkan; using namespace Seele::Vulkan;
+2 -7
View File
@@ -159,13 +159,8 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType,
TextureHandle::~TextureHandle() TextureHandle::~TextureHandle()
{ {
//auto cmdBuffer = graphics->getQueueCommands(currentOwner)->getCommands(); graphics->getDestructionManager()->queueImage(graphics->getQueueCommands(currentOwner)->getCommands(), image);
//VkDevice device = graphics->getDevice(); graphics->getDestructionManager()->queueImageView(graphics->getQueueCommands(currentOwner)->getCommands(), defaultView);
//VkImageView view = defaultView;
//VkImage img = image;
//vkDestroyImageView(device, view, nullptr);
//vkDestroyImage(device, img, nullptr);
//co_return;
} }
TextureHandle* TextureBase::cast(Gfx::PTexture texture) TextureHandle* TextureBase::cast(Gfx::PTexture texture)