Files
Triforce-Source/gpu/vk/vk_vma.cpp
ruckus7382sfgsgdf 34b3683441 Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:32:48 -06:00

348 lines
19 KiB
C++

// Vulkan Memory Allocator (v3.3.0, from Eglantine's CPM cache: nx2/build.py adds its include path): images and
// device buffers are sub-allocated from large blocks (NVIDIA allows ~4096 allocations, and the imported pools
// already take about 2400 of them). Its Vulkan functions come from vkGetInstanceProcAddr / vkGetDeviceProcAddr.
#define VMA_IMPLEMENTATION
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1
#include "vk_internal.h"
#include <vk_mem_alloc.h>
#include <algorithm>
#include <atomic>
#include <cstdlib>
#include <vector>
namespace mkwvk {
VmaAllocator vma;
bool vma_init(PFN_vkGetInstanceProcAddr gipa) {
VmaVulkanFunctions f{};
f.vkGetInstanceProcAddr = gipa;
f.vkGetDeviceProcAddr = vkGetDeviceProcAddr;
VmaAllocatorCreateInfo ci{};
ci.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT |
(dev.memory_budget ? VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT : 0);
ci.physicalDevice = dev.physical;
ci.device = dev.device;
ci.instance = dev.instance;
ci.vulkanApiVersion = VK_API_VERSION_1_3;
ci.pVulkanFunctions = &f;
ci.preferredLargeHeapBlockSize = 64ull << 20; // (64 MB blocks: Eglantine found 256 MB ones wasted VRAM)
return vmaCreateAllocator(&ci, &vma) == VK_SUCCESS;
}
// Images come from a pool of 128 MB blocks: on their own, VMA gave most of them dedicated memory (the driver prefers it
// for render targets, and anything over half a default block), a vkAllocateMemory and a kernel call for each, and
// the dynamic resolution makes a new set of render targets every few frames. Images too large for a block, or of a
// memory type the pool's is not, are allocated as before.
VmaPool image_pool = VK_NULL_HANDLE;
bool image_pool_tried = false;
// (RX_VK_IMAGE_BLOCK_MB=<n>, 16 and up: the block's size. VMA keeps one block empty for the next image, 128 MB of VRAM that a 64 MB block
// would halve, at twice the memory objects: the driver allows ~4096, the imported pools take ~2400)
const uint64_t kImageBlock = (getenv("RX_VK_IMAGE_BLOCK_MB") ? (uint64_t)std::max(16, atoi(getenv("RX_VK_IMAGE_BLOCK_MB"))) : 128u) << 20;
// (hookshot) and the render targets' images in blocks of their own (texture_get's for a target: OoT's transient ones, made and let go
// all the time). In the same blocks as the textures that stay, each one gone left a hole beside a texture that kept its block: the
// pool's 47-54 blocks (6-6.9 GB) held 1.9-3.6 GB of images in Kokiri Forest, VRAM sat at 93-98% of what the driver allows, and an
// image made there then took 30-70 ms (RTX 5070). RX_VK_TARGET_BLOCKS=0: in the textures' blocks, as before
VmaPool target_image_pool = VK_NULL_HANDLE;
bool target_pool_tried = false;
VmaPool images_pool();
VmaPool target_images_pool() {
if (target_pool_tried) return target_image_pool;
target_pool_tried = true;
static const bool off = getenv("RX_VK_TARGET_BLOCKS") && !atoi(getenv("RX_VK_TARGET_BLOCKS"));
if (off || !images_pool()) return VK_NULL_HANDLE;
VkImageCreateInfo rep{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; // (the image pool's representative image: the same memory type)
rep.imageType = VK_IMAGE_TYPE_2D;
rep.format = VK_FORMAT_R8G8B8A8_UNORM;
rep.extent = {256, 256, 1};
rep.mipLevels = rep.arrayLayers = 1;
rep.samples = VK_SAMPLE_COUNT_1_BIT;
rep.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
VmaAllocationCreateInfo ai{};
ai.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
ai.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
uint32_t type = 0;
if (vmaFindMemoryTypeIndexForImageInfo(vma, &rep, &ai, &type) != VK_SUCCESS) return VK_NULL_HANDLE;
VmaPoolCreateInfo pi{};
pi.memoryTypeIndex = type;
pi.blockSize = kImageBlock;
if (vmaCreatePool(vma, &pi, &target_image_pool) != VK_SUCCESS) target_image_pool = VK_NULL_HANDLE;
logf("vk: render targets' images from blocks of their own (%llu MB, memory type %u)%s", (unsigned long long)(kImageBlock >> 20),
type, target_image_pool ? "" : ": not made");
return target_image_pool;
}
VmaPool images_pool() {
if (image_pool_tried) return image_pool;
image_pool_tried = true;
VkImageCreateInfo rep{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; // (a representative image: the pool's memory type)
rep.imageType = VK_IMAGE_TYPE_2D;
rep.format = VK_FORMAT_R8G8B8A8_UNORM;
rep.extent = {256, 256, 1};
rep.mipLevels = rep.arrayLayers = 1;
rep.samples = VK_SAMPLE_COUNT_1_BIT;
rep.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
VmaAllocationCreateInfo ai{};
ai.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
ai.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
uint32_t type = 0;
if (vmaFindMemoryTypeIndexForImageInfo(vma, &rep, &ai, &type) != VK_SUCCESS) return VK_NULL_HANDLE;
VmaPoolCreateInfo pi{};
pi.memoryTypeIndex = type;
pi.blockSize = kImageBlock;
if (vmaCreatePool(vma, &pi, &image_pool) != VK_SUCCESS) image_pool = VK_NULL_HANDLE;
logf("vk: images from a pool of %llu MB blocks (memory type %u)%s", (unsigned long long)(kImageBlock >> 20), type,
image_pool ? "" : ": not made");
return image_pool;
}
// (hookshot) The sampled textures' own pool, when they are compacted (RX_VK_COMPACT=1): VMA's defragmentation asks to move any
// allocation of the pool it works on, and only vk_texture.cpp's images can be moved (it knows what they hold and who uses
// them) or kept from being freed while a pass is open. The render targets' images stay in their blocks above; DLSS's, the
// frame interpolation's and the scratch ones in the images' pool
VmaPool texture_pool = VK_NULL_HANDLE;
bool texture_pool_tried = false;
VmaPool textures_pool() {
if (texture_pool_tried) return texture_pool;
texture_pool_tried = true;
VkImageCreateInfo rep{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; // (the image pool's representative image: the same memory type)
rep.imageType = VK_IMAGE_TYPE_2D;
rep.format = VK_FORMAT_R8G8B8A8_UNORM;
rep.extent = {256, 256, 1};
rep.mipLevels = rep.arrayLayers = 1;
rep.samples = VK_SAMPLE_COUNT_1_BIT;
rep.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
VmaAllocationCreateInfo ai{};
ai.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
ai.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
uint32_t type = 0;
if (vmaFindMemoryTypeIndexForImageInfo(vma, &rep, &ai, &type) != VK_SUCCESS) return VK_NULL_HANDLE;
VmaPoolCreateInfo pi{};
pi.memoryTypeIndex = type;
pi.blockSize = kImageBlock;
if (vmaCreatePool(vma, &pi, &texture_pool) != VK_SUCCESS) texture_pool = VK_NULL_HANDLE;
logf("vk: textures from a pool of their own, %llu MB blocks (RX_VK_COMPACT)%s", (unsigned long long)(kImageBlock >> 20),
texture_pool ? "" : ": not made");
return texture_pool;
}
bool textures_pool_usage(uint64_t* block_bytes, uint64_t* free_bytes) {
if (!texture_pool) return false;
VmaStatistics st{};
vmaGetPoolStatistics(vma, texture_pool, &st);
*block_bytes = st.blockBytes, *free_bytes = st.blockBytes - std::min(st.blockBytes, st.allocationBytes);
return true;
}
// ---- the textures' pool compacted: VMA's defragmentation context and its open pass ----
namespace {
VmaDefragmentationContext compact_ctx = VK_NULL_HANDLE;
VmaDefragmentationPassMoveInfo compact_pass{};
bool compact_in_pass = false;
bool compact_over(uint64_t* freed_bytes, uint32_t* freed_blocks) {
VmaDefragmentationStats st{};
vmaEndDefragmentation(vma, compact_ctx, &st);
compact_ctx = VK_NULL_HANDLE;
if (freed_bytes) *freed_bytes = st.bytesFreed;
if (freed_blocks) *freed_blocks = st.deviceMemoryBlocksFreed;
return false;
}
}
bool image_compact_begin(uint64_t max_bytes_a_pass, uint32_t max_moves_a_pass) {
if (compact_ctx || !texture_pool) return false;
VmaDefragmentationInfo di{};
di.pool = texture_pool;
di.maxBytesPerPass = max_bytes_a_pass;
di.maxAllocationsPerPass = max_moves_a_pass;
return vmaBeginDefragmentation(vma, &di, &compact_ctx) == VK_SUCCESS;
}
bool image_compact_active() { return compact_ctx != VK_NULL_HANDLE; }
bool image_compact_pass(std::vector<ImageMove>& moves, uint64_t* freed_bytes, uint32_t* freed_blocks) {
moves.clear();
if (!compact_ctx || compact_in_pass) return false;
const VkResult r = vmaBeginDefragmentationPass(vma, compact_ctx, &compact_pass);
if (r != VK_INCOMPLETE) return compact_over(freed_bytes, freed_blocks); // (VK_SUCCESS: nothing left to move; or an error)
compact_in_pass = true;
for (uint32_t i = 0; i < compact_pass.moveCount; i++) moves.push_back({compact_pass.pMoves[i].srcAllocation});
return true;
}
bool image_compact_bind(size_t i, VkImage image) {
return compact_in_pass && i < compact_pass.moveCount &&
vmaBindImageMemory(vma, compact_pass.pMoves[i].dstTmpAllocation, image) == VK_SUCCESS;
}
void image_compact_ignore(size_t i) {
if (compact_in_pass && i < compact_pass.moveCount)
compact_pass.pMoves[i].operation = VMA_DEFRAGMENTATION_MOVE_OPERATION_IGNORE;
}
bool image_compact_pass_end(uint64_t* freed_bytes, uint32_t* freed_blocks) {
if (!compact_ctx || !compact_in_pass) return false;
compact_in_pass = false;
if (vmaEndDefragmentationPass(vma, compact_ctx, &compact_pass) == VK_INCOMPLETE) return true;
return compact_over(freed_bytes, freed_blocks);
}
bool create_image(const VkImageCreateInfo& ci, VkImage& image, VmaAllocation& allocation, bool dedicated, bool target, bool textures) {
VmaAllocationCreateInfo ai{};
ai.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
// (in VRAM or not at all: an image VMA puts in system memory when VRAM is full is read over PCIe every frame,
// and takes the RAM the game and everything else on the machine need)
ai.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
if (dev.memory_budget) ai.flags = VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT;
if (dedicated) { // (a memory object of its own, which freeing gives back whole: a pool's block is kept by VMA when it empties)
VmaAllocationCreateInfo dai = ai;
dai.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
if (vmaCreateImage(vma, &ci, &dai, &image, &allocation, nullptr) == VK_SUCCESS) return true;
}
static const bool no_pool = getenv("RX_VK_NO_IMAGE_POOL") != nullptr;
if (VmaPool pool = no_pool ? VK_NULL_HANDLE
: textures && textures_pool() ? textures_pool()
: target && target_images_pool() ? target_images_pool() : images_pool()) {
VmaAllocationCreateInfo pai = ai;
pai.pool = pool;
pai.usage = VMA_MEMORY_USAGE_UNKNOWN;
if (vmaCreateImage(vma, &ci, &pai, &image, &allocation, nullptr) == VK_SUCCESS) return true;
}
if (vmaCreateImage(vma, &ci, &ai, &image, &allocation, nullptr) == VK_SUCCESS) return true;
// (hookshot) VRAM is full (a 4 GB card at a high resolution, #96): an image left unmade is a black screen, so it goes
// to system memory instead, slow but drawn
if (getenv("RX_VK_IMAGES_VRAM_ONLY")) return false;
VmaAllocationCreateInfo sai{};
sai.usage = VMA_MEMORY_USAGE_AUTO;
if (vmaCreateImage(vma, &ci, &sai, &image, &allocation, nullptr) != VK_SUCCESS) return false;
static std::atomic<int> told{0};
if (told++ < 4) logf("vk: VRAM full: a %ux%u image (format %d) in system memory", ci.extent.width, ci.extent.height, (int)ci.format);
return true;
}
void destroy_image(VkImage image, VmaAllocation allocation) {
vmaDestroyImage(vma, image, allocation);
}
uint64_t allocation_size(VmaAllocation allocation) {
VmaAllocationInfo info{};
if (allocation) vmaGetAllocationInfo(vma, allocation, &info);
return info.size;
}
bool create_buffer(uint64_t size, VkBufferUsageFlags usage, bool host_visible, VkBuffer& buffer,
VmaAllocation& allocation, void** mapped, bool dedicated) {
VkBufferCreateInfo bi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
bi.size = size;
bi.usage = usage;
VmaAllocationCreateInfo ai{};
ai.usage = VMA_MEMORY_USAGE_AUTO;
if (host_visible) ai.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
// (a buffer with VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT gets VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT from VMA:
// vma_init sets VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT)
static const bool blocks = getenv("RX_VK_BLOCK_BUFFERS") != nullptr;
if (dedicated && !blocks) ai.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
VmaAllocationInfo info{};
if (vmaCreateBuffer(vma, &bi, &ai, &buffer, &allocation, &info) != VK_SUCCESS) return false;
if (mapped) *mapped = info.pMappedData;
return true;
}
/// A buffer in cached host memory, mapped: written on one thread, read back on the CPU too (the captures: the game's
/// threads copy pages into it, the replay reads texture pages out of it and the GPU copies the rest from it)
bool create_buffer_cached(uint64_t size, VkBufferUsageFlags usage, VkBuffer& buffer, VmaAllocation& allocation,
void** mapped, bool dedicated) {
VkBufferCreateInfo bi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
bi.size = size;
bi.usage = usage;
VmaAllocationCreateInfo ai{};
ai.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
ai.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
if (dedicated) ai.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; // (its own memory object: no part of a 64 MB block left unused)
VmaAllocationInfo info{};
if (vmaCreateBuffer(vma, &bi, &ai, &buffer, &allocation, &info) != VK_SUCCESS) return false;
if (mapped) *mapped = info.pMappedData;
return true;
}
void flush_mapped(VmaAllocation allocation, uint64_t size) {
if (size) vmaFlushAllocation(vma, allocation, 0, size);
}
void flush_mapped_range(VmaAllocation allocation, uint64_t offset, uint64_t size) {
if (size) vmaFlushAllocation(vma, allocation, offset, size);
}
void invalidate_mapped(VmaAllocation allocation, uint64_t size) {
if (size) vmaInvalidateAllocation(vma, allocation, 0, size);
}
void destroy_buffer(VkBuffer buffer, VmaAllocation allocation) {
vmaDestroyBuffer(vma, buffer, allocation);
}
bool create_device_buffer(uint64_t size, VkBufferUsageFlags usage, VkBuffer& buffer, VmaAllocation& allocation, bool dedicated) {
VkBufferCreateInfo bi{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
bi.size = size;
bi.usage = usage;
VmaAllocationCreateInfo ai{};
ai.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
ai.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
if (dev.memory_budget) ai.flags = VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT;
if (dedicated) ai.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
return vmaCreateBuffer(vma, &bi, &ai, &buffer, &allocation, nullptr) == VK_SUCCESS;
}
/// (every heap: what VMA has in blocks and allocations there, what the process uses of it, its budget; one line)
/// The image pools: their blocks, the images in them, and the free ranges between them (every 600 presents, with the heaps)
void vma_log_image_blocks() {
for (VmaPool pool : {image_pool, target_image_pool, texture_pool}) {
if (!pool) continue;
VmaDetailedStatistics d{};
vmaCalculatePoolStatistics(vma, pool, &d);
logf("vk: image blocks%s: %u blocks %llu MB, %u images %llu MB; %u free ranges, the largest %llu MB",
pool == target_image_pool ? " (render targets)" : pool == texture_pool ? " (textures, compacted)" : "", d.statistics.blockCount,
(unsigned long long)(d.statistics.blockBytes >> 20), d.statistics.allocationCount,
(unsigned long long)(d.statistics.allocationBytes >> 20), d.unusedRangeCount,
(unsigned long long)(d.unusedRangeSizeMax >> 20));
}
}
void vma_log_heaps() {
VmaBudget b[VK_MAX_MEMORY_HEAPS]{};
vmaGetHeapBudgets(vma, b);
char line[1024];
int n = snprintf(line, sizeof line, "vk: memory heaps:");
for (uint32_t h = 0; h < dev.memory.memoryHeapCount && n < 900; h++)
n += snprintf(line + n, sizeof line - n, " heap %u%s: %u blocks %llu MB, %u allocations %llu MB; used %llu of %llu MB;", h,
(dev.memory.memoryHeaps[h].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) ? " (device)" : " (system)",
b[h].statistics.blockCount, (unsigned long long)(b[h].statistics.blockBytes >> 20),
b[h].statistics.allocationCount, (unsigned long long)(b[h].statistics.allocationBytes >> 20),
(unsigned long long)(b[h].usage >> 20), (unsigned long long)(b[h].budget >> 20));
logf("%s", line);
}
bool vram_usage(uint64_t* used, uint64_t* budget, uint64_t* block_free) {
VmaBudget b[VK_MAX_MEMORY_HEAPS]{};
vmaGetHeapBudgets(vma, b);
uint64_t u = 0, t = 0, f = 0;
for (uint32_t h = 0; h < dev.memory.memoryHeapCount; h++)
if (dev.memory.memoryHeaps[h].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) {
u += b[h].usage, t += b[h].budget;
// (the free space in VMA's blocks: the driver counts it as the process's, new allocations take it first)
f += b[h].statistics.blockBytes - std::min(b[h].statistics.blockBytes, b[h].statistics.allocationBytes);
}
// (a test: RX_VK_TEST_ALLOWED_MB=<n> reports what the driver allows as at most n MB, so a 16 GB card plays the eviction and
// keep-rendered paths an 8 GB card takes when its VRAM fills (the rainbow textures of issue 993 were never seen here otherwise))
static const uint64_t test_allowed = getenv("RX_VK_TEST_ALLOWED_MB") ? (uint64_t)atoi(getenv("RX_VK_TEST_ALLOWED_MB")) << 20 : 0;
if (test_allowed && t > test_allowed) t = test_allowed;
*used = u, *budget = t;
if (block_free) *block_free = f;
return t != 0;
}
/// The system-memory heaps the GPU reaches: what the process uses there (the imported guest memory too: pinned by the
/// driver) and what the driver allows it, asked now (a new frame index makes VMA ask the driver again); false when unknown
namespace {
std::atomic<uint32_t> budget_frame{0}; // (VMA's frame index: a new one makes it ask the driver for the budgets again)
}
bool vram_usage_now(uint64_t* used, uint64_t* budget) {
vmaSetCurrentFrameIndex(vma, ++budget_frame);
return vram_usage(used, budget) && dev.memory_budget;
}
bool system_heap_usage(uint64_t* used, uint64_t* budget) {
vmaSetCurrentFrameIndex(vma, ++budget_frame);
VmaBudget b[VK_MAX_MEMORY_HEAPS]{};
vmaGetHeapBudgets(vma, b);
uint64_t u = 0, t = 0;
for (uint32_t h = 0; h < dev.memory.memoryHeapCount; h++)
if (!(dev.memory.memoryHeaps[h].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)) u += b[h].usage, t += b[h].budget;
*used = u, *budget = t;
return dev.memory_budget && t != 0;
}
} // namespace mkwvk