Reducing memory usage

This commit is contained in:
Dynamitos
2024-08-07 21:19:33 +02:00
parent 819b541ff2
commit 64a26bfd57
37 changed files with 216 additions and 44 deletions
+12 -1
View File
@@ -5,12 +5,17 @@
#include <vma/vk_mem_alloc.h>
#include <vulkan/vulkan_core.h>
uint64 bufferSize = 0;
uint64 getBufferSize()
{ return bufferSize; }
using namespace Seele;
using namespace Seele::Vulkan;
BufferAllocation::BufferAllocation(PGraphics graphics, const std::string& name, VkBufferCreateInfo bufferInfo,
VmaAllocationCreateInfo allocInfo, Gfx::QueueType owner, uint64 alignment)
: CommandBoundResource(graphics), size(bufferInfo.size), name(name), owner(owner) {
: CommandBoundResource(graphics, name), size(bufferInfo.size), owner(owner) {
VK_CHECK(vmaCreateBufferWithAlignment(graphics->getAllocator(), &bufferInfo, &allocInfo, alignment, &buffer, &allocation, &info));
vmaGetAllocationMemoryProperties(graphics->getAllocator(), allocation, &properties);
VkDebugUtilsObjectNameInfoEXT nameInfo = {
@@ -20,6 +25,9 @@ BufferAllocation::BufferAllocation(PGraphics graphics, const std::string& name,
.objectHandle = (uint64)buffer,
.pObjectName = name.c_str(),
};
if (!(properties & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) {
bufferSize += size;
}
assert(!name.empty());
vkSetDebugUtilsObjectNameEXT(graphics->getDevice(), &nameInfo);
if (bufferInfo.usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
@@ -34,6 +42,9 @@ BufferAllocation::BufferAllocation(PGraphics graphics, const std::string& name,
BufferAllocation::~BufferAllocation() {
if (buffer != VK_NULL_HANDLE) {
if (!(properties & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) {
bufferSize -= size;
}
vmaDestroyBuffer(graphics->getAllocator(), buffer, allocation);
}
}
+3 -1
View File
@@ -4,10 +4,13 @@
#include "Graphics/Enums.h"
#include "Resources.h"
uint64 getBufferSize();
namespace Seele {
namespace Vulkan {
DECLARE_REF(Command)
DECLARE_REF(Fence)
class BufferAllocation : public CommandBoundResource {
public:
BufferAllocation(PGraphics graphics, const std::string& name, VkBufferCreateInfo bufferInfo, VmaAllocationCreateInfo allocInfo,
@@ -24,7 +27,6 @@ class BufferAllocation : public CommandBoundResource {
VmaAllocationInfo info = VmaAllocationInfo();
VkMemoryPropertyFlags properties = 0;
uint64 size = 0;
std::string name;
VkDeviceAddress deviceAddress;
Gfx::QueueType owner;
};
+1
View File
@@ -464,6 +464,7 @@ CommandPool::CommandPool(PGraphics graphics, PQueue queue) : graphics(graphics),
}
CommandPool::~CommandPool() {
submitCommands();
vkDeviceWaitIdle(graphics->getDevice());
for (auto& cmd : allocatedBuffers) {
cmd->checkFence();
+2 -2
View File
@@ -57,7 +57,7 @@ void DescriptorLayout::create() {
}
DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout)
: CommandBoundResource(graphics), graphics(graphics), layout(layout) {
: CommandBoundResource(graphics, layout->getName()), graphics(graphics), layout(layout) {
for (uint32 i = 0; i < cachedHandles.size(); ++i) {
cachedHandles[i] = nullptr;
}
@@ -159,7 +159,7 @@ void DescriptorPool::reset() {
}
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner)
: Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics), setHandle(VK_NULL_HANDLE), graphics(graphics), owner(owner){
: Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics, owner->getLayout()->getName()), setHandle(VK_NULL_HANDLE), graphics(graphics), owner(owner){
boundResources.resize(owner->getLayout()->getBindings().size());
for (uint32 i = 0; i < boundResources.size(); ++i)
{
+2 -1
View File
@@ -165,7 +165,8 @@ void Graphics::beginRenderPass(Gfx::PRenderPass renderPass) {
void Graphics::endRenderPass() { getGraphicsCommands()->getCommands()->endRenderPass(); }
void Graphics::waitDeviceIdle() {
void Graphics::waitDeviceIdle() {
getGraphicsCommands()->submitCommands();
vkDeviceWaitIdle(handle);
getGraphicsCommands()->refreshCommands();
}
+1 -1
View File
@@ -92,7 +92,7 @@ void DestructionManager::notifyCommandComplete() {
}
}
SamplerHandle::SamplerHandle(PGraphics graphics, VkSamplerCreateInfo createInfo) : CommandBoundResource(graphics) {
SamplerHandle::SamplerHandle(PGraphics graphics, VkSamplerCreateInfo createInfo) : CommandBoundResource(graphics, "Sampler") {
vkCreateSampler(graphics->getDevice(), &createInfo, nullptr, &sampler);
}
+2 -1
View File
@@ -61,7 +61,7 @@ DEFINE_REF(DestructionManager)
class CommandBoundResource {
public:
CommandBoundResource(PGraphics graphics) : graphics(graphics) {}
CommandBoundResource(PGraphics graphics, const std::string& name) : graphics(graphics), name(name) {}
virtual ~CommandBoundResource() {
if (isCurrentlyBound())
abort();
@@ -72,6 +72,7 @@ class CommandBoundResource {
protected:
PGraphics graphics;
std::string name;
uint64 bindCount = 0;
};
DEFINE_REF(CommandBoundResource)
-1
View File
@@ -6,7 +6,6 @@
#include "stdlib.h"
#include <fmt/core.h>
using namespace Seele;
using namespace Seele::Vulkan;
+3 -2
View File
@@ -5,6 +5,7 @@
#include "Graphics/Initializer.h"
#include <math.h>
#include <vulkan/vulkan_core.h>
#include <fmt/format.h>
using namespace Seele;
using namespace Seele::Vulkan;
@@ -28,7 +29,7 @@ VkImageAspectFlags getAspectFromFormat(Gfx::SeFormat format) {
}
TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const TextureCreateInfo& createInfo, VkImage existingImage)
: CommandBoundResource(graphics), image(existingImage), width(createInfo.width), height(createInfo.height), depth(createInfo.depth),
: CommandBoundResource(graphics, createInfo.name), image(existingImage), width(createInfo.width), height(createInfo.height), depth(createInfo.depth),
arrayCount(createInfo.elements), layerCount(createInfo.layers), mipLevels(createInfo.mipLevels), samples(createInfo.samples),
format(createInfo.format), usage(createInfo.usage), layout(Gfx::SE_IMAGE_LAYOUT_UNDEFINED),
aspect(getAspectFromFormat(createInfo.format)), ownsImage(false), owner(createInfo.sourceData.owner) {
@@ -102,7 +103,7 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
};
OBufferAllocation stagingAlloc = new BufferAllocation(graphics, createInfo.name, stagingInfo, alloc, Gfx::QueueType::TRANSFER);
OBufferAllocation stagingAlloc = new BufferAllocation(graphics, fmt::format("{}Staging", createInfo.name), stagingInfo, alloc, Gfx::QueueType::TRANSFER);
vmaMapMemory(graphics->getAllocator(), stagingAlloc->allocation, &data);
std::memcpy(data, sourceData.data, sourceData.size);
vmaUnmapMemory(graphics->getAllocator(), stagingAlloc->allocation);