graphics: refactoring #2

This commit is contained in:
nmzik
2026-07-21 07:20:55 +02:00
parent b51d4322cc
commit 0a5f78c727
137 changed files with 10230 additions and 10893 deletions
+70 -77
View File
@@ -48,34 +48,32 @@ void UntrackAllocationImpl(const VulkanMemory& memory) {
} // namespace
void VulkanTrackAllocation(const VulkanMemory* memory) {
EXIT_IF(memory == nullptr);
TrackAllocationImpl(*memory);
void VulkanTrackAllocation(const VulkanMemory& memory) {
TrackAllocationImpl(memory);
}
void VulkanUntrackAllocation(const VulkanMemory* memory) {
EXIT_IF(memory == nullptr);
UntrackAllocationImpl(*memory);
void VulkanUntrackAllocation(const VulkanMemory& memory) {
UntrackAllocationImpl(memory);
}
bool VulkanCreateAllocator(GraphicContext* ctx) {
bool GraphicContext::CreateAllocator() {
KYTY_PROFILER_FUNCTION();
EXIT_IF(ctx == nullptr || ctx->instance == nullptr || ctx->physical_device == nullptr ||
ctx->device == nullptr || ctx->allocator != nullptr);
EXIT_IF(instance == nullptr || physical_device == nullptr || device == nullptr ||
allocator != nullptr);
VmaVulkanFunctions functions {};
functions.vkGetInstanceProcAddr = VULKAN_HPP_DEFAULT_DISPATCHER.vkGetInstanceProcAddr;
functions.vkGetDeviceProcAddr = VULKAN_HPP_DEFAULT_DISPATCHER.vkGetDeviceProcAddr;
VmaAllocatorCreateInfo info {};
info.instance = ctx->instance;
info.physicalDevice = ctx->physical_device;
info.device = ctx->device;
info.instance = instance;
info.physicalDevice = physical_device;
info.device = device;
info.pVulkanFunctions = &functions;
info.vulkanApiVersion = VULKAN_TARGET_API_VERSION;
info.flags = ctx->memory_budget_ext_enabled ? VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT : 0;
info.flags = memory_budget_ext_enabled ? VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT : 0;
const auto result = static_cast<vk::Result>(vmaCreateAllocator(&info, &ctx->allocator));
const auto result = static_cast<vk::Result>(vmaCreateAllocator(&info, &allocator));
if (result != vk::Result::eSuccess) {
LOGF("vmaCreateAllocator failed: %s\n", VulkanToString(result).c_str());
return false;
@@ -83,12 +81,12 @@ bool VulkanCreateAllocator(GraphicContext* ctx) {
return true;
}
void VulkanDestroyAllocator(GraphicContext* ctx) {
if (ctx == nullptr || ctx->allocator == nullptr) {
void GraphicContext::DestroyAllocator() {
if (allocator == nullptr) {
return;
}
vmaDestroyAllocator(ctx->allocator);
ctx->allocator = nullptr;
vmaDestroyAllocator(allocator);
allocator = nullptr;
}
uint64_t VulkanNextMemoryUniqueId() {
@@ -96,14 +94,14 @@ uint64_t VulkanNextMemoryUniqueId() {
return ++sequence;
}
void VulkanLogMemoryBudget(GraphicContext* ctx) {
if (ctx == nullptr || ctx->allocator == nullptr || ctx->physical_device == nullptr) {
void GraphicContext::LogMemoryBudget() const {
if (allocator == nullptr || physical_device == nullptr) {
return;
}
const auto& properties = ctx->GetPhysicalDeviceMemoryProperties();
const auto& properties = GetPhysicalDeviceMemoryProperties();
VmaBudget budgets[VK_MAX_MEMORY_HEAPS] {};
vmaGetHeapBudgets(ctx->allocator, budgets);
vmaGetHeapBudgets(allocator, budgets);
for (uint32_t i = 0; i < properties.memoryHeapCount; i++) {
LOGF("VMA heap %u: usage=%" PRIu64 ", budget=%" PRIu64 ", allocation=%" PRIu64
", blocks=%" PRIu64 "\n",
@@ -113,115 +111,110 @@ void VulkanLogMemoryBudget(GraphicContext* ctx) {
}
}
void VulkanCreateBuffer(GraphicContext* ctx, uint64_t size, VulkanBuffer* buffer) {
void GraphicContext::CreateBuffer(uint64_t size, VulkanBuffer& buffer) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(ctx == nullptr || ctx->allocator == nullptr || buffer == nullptr ||
buffer->buffer != nullptr || buffer->memory.allocation != nullptr || size == 0);
EXIT_IF(allocator == nullptr || buffer.buffer != nullptr ||
buffer.memory.allocation != nullptr || size == 0);
vk::BufferCreateInfo buffer_info {};
buffer_info.sType = vk::StructureType::eBufferCreateInfo;
buffer_info.size = size;
buffer_info.usage = buffer->usage;
buffer_info.usage = buffer.usage;
buffer_info.sharingMode = vk::SharingMode::eExclusive;
VmaAllocationCreateInfo alloc_info {};
alloc_info.requiredFlags =
static_cast<vk::MemoryPropertyFlags::MaskType>(buffer->memory.property);
static_cast<vk::MemoryPropertyFlags::MaskType>(buffer.memory.property);
alloc_info.preferredFlags =
static_cast<vk::MemoryPropertyFlags::MaskType>(buffer->memory.preferred_property);
static_cast<vk::MemoryPropertyFlags::MaskType>(buffer.memory.preferred_property);
vk::Buffer::CType native_buffer = VK_NULL_HANDLE;
const auto result = static_cast<vk::Result>(vmaCreateBuffer(
ctx->allocator, static_cast<const vk::BufferCreateInfo::NativeType*>(buffer_info),
&alloc_info, &native_buffer, &buffer->memory.allocation, &buffer->memory.allocation_info));
buffer->buffer = native_buffer;
allocator, static_cast<const vk::BufferCreateInfo::NativeType*>(buffer_info), &alloc_info,
&native_buffer, &buffer.memory.allocation, &buffer.memory.allocation_info));
buffer.buffer = native_buffer;
if (result != vk::Result::eSuccess) {
VulkanLogMemoryBudget(ctx);
LogMemoryBudget();
}
EXIT_NOT_IMPLEMENTED(result != vk::Result::eSuccess);
ctx->device.getBufferMemoryRequirements(buffer->buffer, &buffer->memory.requirements);
buffer->memory.type = buffer->memory.allocation_info.memoryType;
buffer->memory.memory = buffer->memory.allocation_info.deviceMemory;
buffer->memory.offset = buffer->memory.allocation_info.offset;
buffer->memory.unique_id = VulkanNextMemoryUniqueId();
buffer->buffer_size = size;
VulkanTrackAllocation(&buffer->memory);
device.getBufferMemoryRequirements(buffer.buffer, &buffer.memory.requirements);
buffer.memory.type = buffer.memory.allocation_info.memoryType;
buffer.memory.memory = buffer.memory.allocation_info.deviceMemory;
buffer.memory.offset = buffer.memory.allocation_info.offset;
buffer.memory.unique_id = VulkanNextMemoryUniqueId();
buffer.buffer_size = size;
VulkanTrackAllocation(buffer.memory);
}
void VulkanDeleteBuffer(GraphicContext* ctx, VulkanBuffer* buffer) {
void GraphicContext::DeleteBuffer(VulkanBuffer& buffer) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(ctx == nullptr || ctx->allocator == nullptr || buffer == nullptr ||
buffer->buffer == nullptr || buffer->memory.allocation == nullptr);
EXIT_IF(allocator == nullptr || buffer.buffer == nullptr ||
buffer.memory.allocation == nullptr);
VulkanUntrackAllocation(&buffer->memory);
vmaDestroyBuffer(ctx->allocator, buffer->buffer, buffer->memory.allocation);
buffer->buffer = nullptr;
buffer->memory.memory = nullptr;
buffer->memory.allocation = nullptr;
buffer->memory.allocation_info = {};
buffer->memory.offset = 0;
VulkanUntrackAllocation(buffer.memory);
vmaDestroyBuffer(allocator, buffer.buffer, buffer.memory.allocation);
buffer.buffer = nullptr;
buffer.memory.memory = nullptr;
buffer.memory.allocation = nullptr;
buffer.memory.allocation_info = {};
buffer.memory.offset = 0;
}
bool VulkanCreateImage(GraphicContext* ctx, const vk::ImageCreateInfo& image_info,
VulkanImage* image) {
bool GraphicContext::CreateImage(const vk::ImageCreateInfo& image_info, VulkanImage& image) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(ctx == nullptr || ctx->allocator == nullptr || image == nullptr ||
image->image != nullptr || image->memory.allocation != nullptr);
EXIT_IF(allocator == nullptr || image.image != nullptr || image.memory.allocation != nullptr);
auto& memory = image->memory;
auto& memory = image.memory;
VmaAllocationCreateInfo alloc_info {};
alloc_info.requiredFlags = static_cast<vk::MemoryPropertyFlags::MaskType>(memory.property);
alloc_info.preferredFlags =
static_cast<vk::MemoryPropertyFlags::MaskType>(memory.preferred_property);
vk::Image::CType native_image = VK_NULL_HANDLE;
const auto result = static_cast<vk::Result>(vmaCreateImage(
ctx->allocator, static_cast<const vk::ImageCreateInfo::NativeType*>(image_info),
&alloc_info, &native_image, &memory.allocation, &memory.allocation_info));
image->image = native_image;
const auto result = static_cast<vk::Result>(
vmaCreateImage(allocator, static_cast<const vk::ImageCreateInfo::NativeType*>(image_info),
&alloc_info, &native_image, &memory.allocation, &memory.allocation_info));
image.image = native_image;
if (result != vk::Result::eSuccess) {
VulkanLogMemoryBudget(ctx);
LogMemoryBudget();
return false;
}
ctx->device.getImageMemoryRequirements(image->image, &memory.requirements);
device.getImageMemoryRequirements(image.image, &memory.requirements);
memory.type = memory.allocation_info.memoryType;
memory.memory = memory.allocation_info.deviceMemory;
memory.offset = memory.allocation_info.offset;
memory.unique_id = VulkanNextMemoryUniqueId();
VulkanTrackAllocation(&memory);
VulkanTrackAllocation(memory);
return true;
}
void VulkanDeleteImage(GraphicContext* ctx, VulkanImage* image) {
void GraphicContext::DeleteImage(VulkanImage& image) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(ctx == nullptr || ctx->allocator == nullptr || image == nullptr ||
image->image == nullptr || image->memory.allocation == nullptr);
EXIT_IF(allocator == nullptr || image.image == nullptr || image.memory.allocation == nullptr);
auto& memory = image->memory;
VulkanUntrackAllocation(&memory);
vmaDestroyImage(ctx->allocator, image->image, memory.allocation);
image->image = nullptr;
auto& memory = image.memory;
VulkanUntrackAllocation(memory);
vmaDestroyImage(allocator, image.image, memory.allocation);
image.image = nullptr;
memory.memory = nullptr;
memory.allocation = nullptr;
memory.allocation_info = {};
memory.offset = 0;
}
void VulkanMapMemory(GraphicContext* ctx, VulkanMemory* memory, void** data) {
void GraphicContext::MapMemory(VulkanMemory& memory, void*& data) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(ctx == nullptr || memory == nullptr || data == nullptr || ctx->allocator == nullptr ||
memory->allocation == nullptr);
EXIT_NOT_IMPLEMENTED(static_cast<vk::Result>(vmaMapMemory(ctx->allocator, memory->allocation,
data)) != vk::Result::eSuccess);
EXIT_IF(allocator == nullptr || memory.allocation == nullptr);
EXIT_NOT_IMPLEMENTED(static_cast<vk::Result>(vmaMapMemory(allocator, memory.allocation,
&data)) != vk::Result::eSuccess);
}
void VulkanUnmapMemory(GraphicContext* ctx, VulkanMemory* memory) {
void GraphicContext::UnmapMemory(VulkanMemory& memory) {
KYTY_PROFILER_FUNCTION();
EXIT_IF(ctx == nullptr || memory == nullptr || ctx->allocator == nullptr ||
memory->allocation == nullptr);
vmaUnmapMemory(ctx->allocator, memory->allocation);
EXIT_IF(allocator == nullptr || memory.allocation == nullptr);
vmaUnmapMemory(allocator, memory.allocation);
}
} // namespace Libs::Graphics