18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

Merge pull request #582 from MaSzyna-EU07/feat/vulkan-memory-breakdown

feat(vulkan): the statistics show the device memory by its uses
This commit is contained in:
2026-12-01 23:33:43 +00:00
committed by GitHub
5 changed files with 200 additions and 17 deletions
+90 -7
View File
@@ -13,8 +13,10 @@ http://mozilla.org/MPL/2.0/.
#include "rendering/vulkan14/vulkan14instance.h"
#include "rendering/vulkan14/vulkan14device.h"
#include <algorithm>
#include <cinttypes>
#include <cstdio>
#include <functional>
namespace vk14
{
@@ -287,6 +289,29 @@ VkDeviceAddress image_address(VmaAllocator const Allocator, VkDevice const Devic
} // namespace address_registry
// the uses of the memory
std::string_view MemoryUseOf(std::string_view const Name, std::string_view const Group)
{
if (false == Group.empty())
{
return Group;
}
auto const end{Name.find_last_not_of("0123456789 ")};
return (end == std::string_view::npos ? std::string_view{"unnamed"} : Name.substr(0, end + 1));
}
void MemoryCount::Release() noexcept
{
if (tally != nullptr)
{
tally->bytes -= bytes;
--tally->count;
}
tally = nullptr;
bytes = 0;
}
// allocator
allocator::~allocator()
@@ -337,6 +362,11 @@ bool allocator::create(instance const &Instance, device const &Device, std::stri
{
m_deviceheaps[i] = ((memory.memoryHeaps[i].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0);
}
deviceTypes.assign(memory.memoryTypeCount, false);
for (std::uint32_t i = 0; i < memory.memoryTypeCount; ++i)
{
deviceTypes[i] = ((memory.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) != 0);
}
return true;
}
@@ -370,22 +400,70 @@ VkSharingMode allocator::sharing(bool const Shared, std::uint32_t &Count, std::u
void allocator::budget(VkDeviceSize &Usage, VkDeviceSize &Budget) const
{
Usage = 0;
Budget = 0;
auto const summed{Heaps()};
Usage = summed.usage;
Budget = summed.budget;
}
MemoryHeaps allocator::Heaps() const
{
MemoryHeaps result;
if (m_allocator == VK_NULL_HANDLE)
{
return;
return result;
}
std::vector<VmaBudget> budgets(m_heapcount);
vmaGetHeapBudgets(m_allocator, budgets.data());
for (std::uint32_t i = 0; i < m_heapcount; ++i)
{
if (m_deviceheaps[i])
if (false == m_deviceheaps[i])
{
Usage += budgets[i].usage;
Budget += budgets[i].budget;
continue;
}
result.usage += budgets[i].usage;
result.budget += budgets[i].budget;
result.blocks += budgets[i].statistics.blockBytes;
result.allocations += budgets[i].statistics.allocationBytes;
}
return result;
}
std::vector<MemoryUse> allocator::Uses() const
{
std::vector<MemoryUse> result;
{
std::scoped_lock lock(tallyMutex);
result.reserve(tallies.size());
for (auto const &tally : tallies)
{
if (auto const count{tally.count.load()}; count > 0)
{
result.emplace_back(tally.group, tally.bytes.load(), count);
}
}
}
std::ranges::sort(result, std::ranges::greater{}, &MemoryUse::bytes);
return result;
}
MemoryCount allocator::Count(std::string_view const Group, std::uint32_t const Memorytype, VkDeviceSize const Bytes) const
{
if ((Memorytype >= deviceTypes.size()) || (false == deviceTypes[Memorytype]))
{
return {};
}
std::string group{Group};
std::scoped_lock lock(tallyMutex);
auto lookup{tallyGroups.find(group)};
if (lookup == tallyGroups.end())
{
auto &made{tallies.emplace_back(group)};
lookup = tallyGroups.emplace(std::move(group), &made).first;
}
auto *tally{lookup->second};
tally->bytes += Bytes;
++tally->count;
return {tally, Bytes};
}
// buffer
@@ -406,6 +484,7 @@ buffer &buffer::operator=(buffer &&Other) noexcept
std::swap(m_size, Other.m_size);
std::swap(m_mapped, Other.m_mapped);
std::swap(m_address, Other.m_address);
std::swap(counted, Other.counted);
}
return *this;
}
@@ -464,6 +543,7 @@ bool buffer::create(allocator const &Allocator, VkDeviceSize Size, VkBufferUsage
m_allocator = Allocator.handle();
m_size = Size;
m_mapped = static_cast<std::uint8_t *>(allocated.pMappedData);
counted = Allocator.Count(MemoryUseOf(Name, {}), allocated.memoryType, allocated.size);
name_object(Allocator.device_handle(), VK_OBJECT_TYPE_BUFFER, handle_value(m_buffer), Name);
return true;
}
@@ -478,6 +558,7 @@ void buffer::destroy()
}
vmaDestroyBuffer(m_allocator, m_buffer, m_allocation);
}
counted.Release();
m_buffer = VK_NULL_HANDLE;
m_allocation = VK_NULL_HANDLE;
m_size = 0;
@@ -542,7 +623,7 @@ VkImageAspectFlags image::aspect() const
return VK_IMAGE_ASPECT_COLOR_BIT;
}
bool image::create(allocator const &Allocator, image_desc const &Desc, std::string const &Name)
bool image::create(allocator const &Allocator, image_desc const &Desc, std::string const &Name, std::string_view const Group)
{
destroy();
m_desc = Desc;
@@ -573,6 +654,7 @@ bool image::create(allocator const &Allocator, image_desc const &Desc, std::stri
m_allocator = Allocator.handle();
m_device = Allocator.device_handle();
m_bytes = allocated.size;
counted = Allocator.Count(MemoryUseOf(Name, Group), allocated.memoryType, allocated.size);
if (address_registry::enabled())
{
// an image in a block of the allocator (the memory has device addresses there) is placed exactly; one in memory of its own isn't
@@ -616,6 +698,7 @@ void image::destroy()
}
vmaDestroyImage(m_allocator, m_image, m_allocation);
}
counted.Release();
m_view = VK_NULL_HANDLE;
m_image = VK_NULL_HANDLE;
m_allocation = VK_NULL_HANDLE;
+61 -2
View File
@@ -15,9 +15,14 @@ http://mozilla.org/MPL/2.0/.
#include "rendering/vulkan14/vulkan14common.h"
#include <atomic>
#include <deque>
#include <mutex>
#include <functional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include <vk_mem_alloc.h>
namespace vk14
@@ -26,6 +31,45 @@ namespace vk14
class instance;
class device;
// the device local memory the buffers and the images of one use hold: those made with the same name (without the number at its
// end) or in the same group. the allocator keeps these, the buffers and the images count themselves to them
struct MemoryTally
{
explicit MemoryTally(std::string_view Group) : group{Group} {}
std::string const group;
std::atomic<VkDeviceSize> bytes{0};
std::atomic<std::uint32_t> count{0};
};
// what a buffer or an image counted to the tally of its use, given back when it goes
struct MemoryCount
{
MemoryTally *tally{nullptr}; // null where the memory isn't device local
VkDeviceSize bytes{0};
void Release() noexcept;
};
// a use of the device local memory as allocator::Uses() gives it
struct MemoryUse
{
std::string group;
VkDeviceSize bytes{0};
std::uint32_t count{0};
};
// the heaps which are local to the device, summed up, bytes
struct MemoryHeaps
{
VkDeviceSize usage{0}; // used by the process, as the driver tells it (the allocator's blocks, the swapchain, the driver's own)
VkDeviceSize budget{0}; // what the driver gives the process
VkDeviceSize blocks{0}; // the blocks of memory the allocator holds
VkDeviceSize allocations{0}; // what its buffers and images take of these
};
// the use a buffer or an image named Name counts to: Group, or where none is given, Name without the number at its end ("shadow
// cascade 2" counts to "shadow cascade")
std::string_view MemoryUseOf(std::string_view Name, std::string_view Group);
// the allocator of the device memory
class allocator
{
@@ -48,6 +92,12 @@ class allocator
}
// device memory used and the budget of the heaps which are local to the device, bytes
void budget(VkDeviceSize &Usage, VkDeviceSize &Budget) const;
// the same with what the allocator holds of the memory
MemoryHeaps Heaps() const;
// the device local memory held by the buffers and the images by their uses, the largest first
std::vector<MemoryUse> Uses() const;
// counts an allocation of Bytes in memory of the type Memorytype to the use Group, where that memory is device local. any thread
MemoryCount Count(std::string_view Group, std::uint32_t Memorytype, VkDeviceSize Bytes) const;
// the buffers and images made shared from now on are used by the queues of both families without transfers of their ownership
// (VK_SHARING_MODE_CONCURRENT, the async compute of M6); nothing changes when the families are the same
void share_between(std::uint32_t First, std::uint32_t Second);
@@ -60,6 +110,12 @@ class allocator
std::array<std::uint32_t, 2> m_shared{VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED};
std::uint32_t m_heapcount{0};
std::vector<bool> m_deviceheaps;
std::vector<bool> deviceTypes; // the memory types which are device local
// the tallies of the uses, in a deque where they stay in place as it grows (the buffers and the images point at them). counting
// doesn't change what the allocator does, so it's done by the const allocator the buffers and the images are made with
mutable std::mutex tallyMutex;
mutable std::deque<MemoryTally> tallies;
mutable std::unordered_map<std::string, MemoryTally *> tallyGroups;
};
// what the memory of the gpu holds by its device addresses, for the report of a lost device (device::fault_report()): the buffers, all
@@ -127,6 +183,7 @@ class buffer
VkDeviceSize m_size{0};
std::uint8_t *m_mapped{nullptr};
mutable VkDeviceAddress m_address{0}; // asked for the first time it's needed
MemoryCount counted;
};
// the state of an image the barriers start from: its layout and the last stage and access which used it
@@ -162,8 +219,9 @@ class image
image &operator=(image const &) = delete;
~image();
// a device local image with a view of all its levels and layers
bool create(allocator const &Allocator, image_desc const &Desc, std::string const &Name = {});
// a device local image with a view of all its levels and layers. Group: the use its memory counts to, where its name doesn't tell
// it (the textures, named after their files)
bool create(allocator const &Allocator, image_desc const &Desc, std::string const &Name = {}, std::string_view Group = {});
void destroy();
VkImage handle() const
@@ -198,6 +256,7 @@ class image
VmaAllocation m_allocation{VK_NULL_HANDLE};
VkDeviceSize m_bytes{0};
image_desc m_desc;
MemoryCount counted;
};
// a barrier of an image from its state to the given one, the state of the image updated. nothing when the image already is
+33 -1
View File
@@ -19,6 +19,9 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/utilities.h"
#include "imgui/imgui.h"
#include <format>
#include <ranges>
namespace
{
@@ -81,6 +84,35 @@ std::size_t render_threads()
return (cores > 3 ? cores - 2 : 1);
}
// the device local memory by its uses: those which hold the most of it, then the free space in the blocks of the allocator and what the
// process holds outside of it (the swapchain, the driver's own, dlss)
std::string memoryUsesInfo(vk14::allocator const &Memory)
{
constexpr std::size_t listed{10};
auto const heaps{Memory.Heaps()};
auto const uses{Memory.Uses()};
std::string text{"device memory by use:\n"};
for (auto const &use : uses | std::views::take(listed))
{
text += std::format(" {}: {} in {}\n", use.group, vk14::memory_size_string(use.bytes), use.count);
}
VkDeviceSize otherBytes{0};
std::uint32_t otherCount{0};
for (auto const &use : uses | std::views::drop(listed))
{
otherBytes += use.bytes;
otherCount += use.count;
}
if (otherCount > 0)
{
text += std::format(" the others: {} in {}\n", vk14::memory_size_string(otherBytes), otherCount);
}
auto const freeInBlocks{heaps.blocks > heaps.allocations ? heaps.blocks - heaps.allocations : 0};
auto const outside{heaps.usage > heaps.blocks ? heaps.usage - heaps.blocks : 0};
text += std::format(" free in the blocks of the allocator: {}\n outside the allocator (swapchain, driver, dlss): {}\n", vk14::memory_size_string(freeInBlocks), vk14::memory_size_string(outside));
return text;
}
} // namespace
bool vulkan14_renderer::context::create_frames(std::uint32_t const Count, std::string &Reason)
@@ -1273,7 +1305,7 @@ void vulkan14_renderer::update_info()
(ctx.render_threaded() ? ", on the render thread; waited for it: " + to_string(ctx.render_wait, 2) + " ms\n" : "\n") + "gpu frame parts:\n" + ctx.passes.info();
VkDeviceSize usage{0}, budget{0};
ctx.memory.budget(usage, budget);
m_infostats = "device memory: " + vk14::memory_size_string(usage) + " of " + vk14::memory_size_string(budget) + (ctx.device.memory_budget_enabled() ? "" : " (no budget from the driver)") + "\n" + "images: " + std::to_string(ctx.images.size()) +
m_infostats = "device memory: " + vk14::memory_size_string(usage) + " of " + vk14::memory_size_string(budget) + (ctx.device.memory_budget_enabled() ? "" : " (no budget from the driver)") + "\n" + memoryUsesInfo(ctx.memory) + "images: " + std::to_string(ctx.images.size()) +
", uploaded " + vk14::memory_size_string(ctx.uploads.uploaded()) + ", waiting for the gpu to be deleted: " + std::to_string(ctx.deletions.size()) + "\n" + (ctx.textures ? ctx.textures->info() + "\n" : std::string{}) + ctx.shaders.info() +
"\n" + ctx.pipelines.info() + "\n" + background_info() + "frame ring: " + vk14::memory_size_string(ctx.ring.used()) + " of " + vk14::memory_size_string(ctx.ring.capacity()) + ", of the draws: " +
vk14::memory_size_string(ctx.scene.draws.ring.used()) + " of " + vk14::memory_size_string(ctx.scene.draws.ring.capacity()) + ", of the instances: " +
+7 -2
View File
@@ -13,12 +13,17 @@ http://mozilla.org/MPL/2.0/.
#include "rendering/vulkan14/vulkan14device.h"
#include "utilities/Logs.h"
#include <string_view>
namespace vk14
{
namespace
{
// the use the memory of the maps of the terrain counts to: they are named after the textures they're made for
constexpr std::string_view terrainMemoryUse{"terrain maps"};
std::uint32_t mip_count(std::uint32_t const Size)
{
return static_cast<std::uint32_t>(std::floor(std::log2(std::max(Size, 1u)))) + 1;
@@ -73,7 +78,7 @@ std::shared_ptr<image> terrain_materials::make_image(VkFormat const Format, std:
desc.mips = Mips;
desc.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
auto result{std::make_shared<image>()};
if (false == result->create(*m_resources->memory, desc, Name))
if (false == result->create(*m_resources->memory, desc, Name, terrainMemoryUse))
{
ErrorLog(std::string("heightmap terrain: no gpu image for ") + Name);
return nullptr;
@@ -127,7 +132,7 @@ void terrain_materials::gpu_storage(texture_array const &Array, int const Layers
desc.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
auto &target{texture(Array.handle)};
auto map{std::make_shared<image>()};
if (false == map->create(*m_resources->memory, desc, target.name))
if (false == map->create(*m_resources->memory, desc, target.name, terrainMemoryUse))
{
ErrorLog("heightmap terrain: no gpu image for the layers of \"" + target.name + "\"");
return;
+9 -5
View File
@@ -23,6 +23,7 @@ http://mozilla.org/MPL/2.0/.
#include <memory>
#include <optional>
#include <span>
#include <string_view>
namespace vk14
{
@@ -30,6 +31,9 @@ namespace vk14
namespace
{
// the use the memory of the images of the textures counts to: they are named after their files
constexpr std::string_view textureMemoryUse{"textures"};
// the data a texture is made of, for the alpha test of the shadow maps
struct alpha_source
{
@@ -112,7 +116,7 @@ struct job_ticket
// the host copy
bool createTextureImage(image &Target, allocator const &Memory, uploader &Uploads, image_desc &Desc, std::string const &Name, bool &HostCopy)
{
if (Target.create(Memory, Desc, Name))
if (Target.create(Memory, Desc, Name, textureMemoryUse))
{
return true;
}
@@ -121,7 +125,7 @@ bool createTextureImage(image &Target, allocator const &Memory, uploader &Upload
return false;
}
Desc.usage &= ~VkImageUsageFlags{VK_IMAGE_USAGE_HOST_TRANSFER_BIT};
if (false == Target.create(Memory, Desc, Name))
if (false == Target.create(Memory, Desc, Name, textureMemoryUse))
{
return false;
}
@@ -343,7 +347,7 @@ bool uploader::upload_buffer(std::shared_ptr<buffer> const &Target, void const *
upload.parts.emplace_back(0, Size);
}
upload.staging = std::make_shared<buffer>();
if ((false == upload.staging->create(*m_allocator, Size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, memory_kind::upload)) || (upload.staging->mapped() == nullptr))
if ((false == upload.staging->create(*m_allocator, Size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, memory_kind::upload, "upload staging")) || (upload.staging->mapped() == nullptr))
{
log_error("no staging buffer of " + memory_size_string(Size) + " for an upload");
return false;
@@ -380,7 +384,7 @@ bool uploader::prepare(std::shared_ptr<image> const &Image, void const *Data, Vk
Upload.levels = Levels;
Upload.mipmaps = Mipmaps;
Upload.staging = std::make_shared<buffer>();
if ((false == Upload.staging->create(*m_allocator, Size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, memory_kind::upload)) || (Upload.staging->mapped() == nullptr))
if ((false == Upload.staging->create(*m_allocator, Size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, memory_kind::upload, "upload staging")) || (Upload.staging->mapped() == nullptr))
{
log_error("no staging buffer of " + memory_size_string(Size) + " for an upload");
Upload = prepared{};
@@ -1285,7 +1289,7 @@ void vulkan14_texture::upload_rgba(std::uint8_t const *Data, std::size_t Width,
desc.mips = (generate ? static_cast<std::uint32_t>(std::floor(std::log2(std::max(width, height)))) + 1 : 1);
desc.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | (generate ? VK_IMAGE_USAGE_TRANSFER_SRC_BIT : 0);
target = std::make_shared<vk14::image>();
if (false == target->create(*m_resources.memory, desc, name))
if (false == target->create(*m_resources.memory, desc, name, textureMemoryUse))
{
return;
}