From e7d74e6b0f656ce962dc6ceae99c47d726e9d629 Mon Sep 17 00:00:00 2001 From: Anish Bhobe Date: Thu, 12 Jun 2025 22:29:20 +0200 Subject: [PATCH] Added VMA. --- Blaze.vcxproj | 1 + Blaze.vcxproj.filters | 3 +++ MiscData.cpp | 1 - RenderDevice.cpp | 42 ++++++++++++++++++++++++++++++++++-------- RenderDevice.h | 8 +++++++- VmaImpl.cpp | 7 +++++++ vcpkg.json | 1 + 7 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 VmaImpl.cpp diff --git a/Blaze.vcxproj b/Blaze.vcxproj index 352a156..f13e2be 100644 --- a/Blaze.vcxproj +++ b/Blaze.vcxproj @@ -154,6 +154,7 @@ + diff --git a/Blaze.vcxproj.filters b/Blaze.vcxproj.filters index a7a61f6..5ffccc0 100644 --- a/Blaze.vcxproj.filters +++ b/Blaze.vcxproj.filters @@ -36,6 +36,9 @@ Source Files + + Source Files + diff --git a/MiscData.cpp b/MiscData.cpp index 4ac2060..11fd91d 100644 --- a/MiscData.cpp +++ b/MiscData.cpp @@ -278,7 +278,6 @@ void MiscData::init(RenderDevice const& renderDevice) .imageMemoryBarrierCount = 1, .pImageMemoryBarriers = &renderToPresentBarrier, }; - } void MiscData::cleanup(RenderDevice const& renderDevice) diff --git a/RenderDevice.cpp b/RenderDevice.cpp index e004812..583e35b 100644 --- a/RenderDevice.cpp +++ b/RenderDevice.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include "Frame.h" @@ -60,7 +61,8 @@ RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo con VkPhysicalDevice physicalDeviceInUse = nullptr; VkDevice device = nullptr; - uint32_t directQueueFamilyIndex = -1; + VmaAllocator gpuAllocator = nullptr; + std::optional directQueueFamilyIndex; VkQueue directQueue = nullptr; // Create Device and Queue { @@ -152,13 +154,14 @@ RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo con } ASSERT(physicalDeviceInUse); + ASSERT(directQueueFamilyIndex.has_value()); float priority = 1.0f; VkDeviceQueueCreateInfo queueCreateInfo = { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .pNext = nullptr, .flags = 0, - .queueFamilyIndex = directQueueFamilyIndex, + .queueFamilyIndex = directQueueFamilyIndex.value(), .queueCount = 1, .pQueuePriorities = &priority, }; @@ -193,7 +196,29 @@ RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo con }; VK_CHECK(vkCreateDevice(physicalDeviceInUse, &deviceCreateInfo, nullptr, &device)); - vkGetDeviceQueue(device, directQueueFamilyIndex, 0, &directQueue); + volkLoadDevice(device); + + VmaAllocatorCreateInfo allocatorCreateInfo = { + .flags = 0, + .physicalDevice = physicalDeviceInUse, + .device = device, + .preferredLargeHeapBlockSize = 0, + .pAllocationCallbacks = nullptr, + .pDeviceMemoryCallbacks = nullptr, + .pHeapSizeLimit = nullptr, + .pVulkanFunctions = nullptr, + .instance = instance, + .vulkanApiVersion = VK_API_VERSION_1_3, + .pTypeExternalMemoryHandleTypes = nullptr, + }; + + VmaVulkanFunctions vkFunctions; + VK_CHECK(vmaImportVulkanFunctionsFromVolk(&allocatorCreateInfo, &vkFunctions)); + allocatorCreateInfo.pVulkanFunctions = &vkFunctions; + + VK_CHECK(vmaCreateAllocator(&allocatorCreateInfo, &gpuAllocator)); + + vkGetDeviceQueue(device, directQueueFamilyIndex.value(), 0, &directQueue); mem->restoreState(tempAllocStart); } @@ -340,13 +365,13 @@ RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo con Frame* frames = reinterpret_cast(mem->allocate(sizeof(Frame) * swapchainImageCount)); for (uint32_t i = 0; i != swapchainImageCount; ++i) { - new (frames + i) Frame(device, directQueueFamilyIndex); + new (frames + i) Frame(device, directQueueFamilyIndex.value()); } Byte* allocation = mem->allocate(sizeof(RenderDevice), alignof(RenderDevice)); return new (allocation) RenderDevice{ instance, surface, physicalDeviceInUse, - device, directQueue, directQueueFamilyIndex, + device, gpuAllocator, directQueue, directQueueFamilyIndex.value(), swapchainFormat, swapchainExtent, swapchain, swapchainImages, swapchainViews, frames, swapchainImageCount, }; } @@ -393,13 +418,14 @@ uint32_t RenderDevice::getNumFrames() const } RenderDevice::RenderDevice(VkInstance const instance, VkSurfaceKHR const surface, VkPhysicalDevice const physicalDeviceInUse, - VkDevice const device, VkQueue const directQueue, uint32_t const directQueueFamilyIndex, VkFormat const swapchainFormat, - VkExtent2D const swapchainExtent, VkSwapchainKHR const swapchain, VkImage* swapchainImages, VkImageView* swapchainViews, - Frame* frames, uint32_t const swapchainImageCount) + VkDevice const device, VmaAllocator gpuAllocator, VkQueue const directQueue, uint32_t const directQueueFamilyIndex, + VkFormat const swapchainFormat, VkExtent2D const swapchainExtent, VkSwapchainKHR const swapchain, VkImage* swapchainImages, + VkImageView* swapchainViews, Frame* frames, uint32_t const swapchainImageCount) : instance{ instance } , surface{ surface } , physicalDeviceInUse{ physicalDeviceInUse } , device{ device } + , gpuAllocator{ gpuAllocator } , directQueue{ directQueue } , directQueueFamilyIndex{ directQueueFamilyIndex } , swapchainFormat{ swapchainFormat } diff --git a/RenderDevice.h b/RenderDevice.h index fef80bf..b49bf5d 100644 --- a/RenderDevice.h +++ b/RenderDevice.h @@ -2,6 +2,10 @@ #include +#define VMA_STATIC_VULKAN_FUNCTIONS 0 +#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0 +#include + #include #include @@ -24,10 +28,11 @@ struct RenderDevice VkInstance instance; VkSurfaceKHR surface; VkPhysicalDevice physicalDeviceInUse; + VkDevice device; + VmaAllocator gpuAllocator; VkQueue directQueue; uint32_t directQueueFamilyIndex; - // TODO: Pack? VkFormat swapchainFormat; VkExtent2D swapchainExtent; @@ -48,6 +53,7 @@ struct RenderDevice VkSurfaceKHR surface, VkPhysicalDevice physicalDeviceInUse, VkDevice device, + VmaAllocator gpuAllocator, VkQueue directQueue, uint32_t directQueueFamilyIndex, // TODO: Pack? diff --git a/VmaImpl.cpp b/VmaImpl.cpp new file mode 100644 index 0000000..7cf3063 --- /dev/null +++ b/VmaImpl.cpp @@ -0,0 +1,7 @@ +#include + +#pragma warning(push, 1) +#pragma warning(disable : 5045) +#define VMA_IMPLEMENTATION +#include +#pragma warning(pop) \ No newline at end of file diff --git a/vcpkg.json b/vcpkg.json index b008f48..d73339a 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -2,6 +2,7 @@ "dependencies": [ "volk", "shader-slang", + "vulkan-memory-allocator", { "name": "sdl3", "features": [ "vulkan" ]