diff --git a/CMakeLists.txt b/CMakeLists.txt index afce8dc..ec0f042 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,6 +124,9 @@ set(VOLK_HEADERS_ONLY ON) add_subdirectory(dependencies/volk) target_link_libraries(${PROJECT_NAME} PRIVATE volk_headers) +# Vulkan Memory Allocator +target_include_directories(${PROJECT_NAME} PRIVATE dependencies/VulkanMemoryAllocator/include) + # SDL2: find_package(SDL2) if (SDL2_FOUND) diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 81442ac..a7be894 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -10,7 +10,11 @@ #include "log.hpp" #define VOLK_IMPLEMENTATION -#include "volk.h" +#include + +#define VMA_STATIC_VULKAN_FUNCTIONS 0 +#define VMA_IMPLEMENTATION +#include #include @@ -80,9 +84,9 @@ namespace engine { m_debugMessenger = std::make_unique(instance); // owns the instance auto surface = std::make_shared(window, instance); // owns the instance - auto device = std::make_shared(surface); // owns the surface + m_device = std::make_unique(surface); // owns the surface - m_swapchain = std::make_unique(device); // owns the device + m_swapchain = std::make_unique(m_device.get()); // owns the device } @@ -596,6 +600,8 @@ namespace engine { res = vkAllocateCommandBuffers(m_handle, &gfxCmdBufInfo, &m_gfxCommandBuffer); assert(res == VK_SUCCESS); + m_allocator = std::make_unique(this); + } Device(const Device&) = delete; Device& operator=(const Device&) = delete; @@ -617,6 +623,11 @@ namespace engine { return m_physicalDevice; } + VkInstance getInstance() const + { + return m_surface->getInstance(); + } + struct SwapchainSupportDetails { VkSurfaceCapabilitiesKHR caps{}; std::vector formats{}; @@ -672,6 +683,49 @@ namespace engine { throw std::runtime_error("Unable to find compute queue"); }*/ + class GPUAllocator { + public: + GPUAllocator(const Device* device) + { + VmaVulkanFunctions functions{ + .vkGetInstanceProcAddr = vkGetInstanceProcAddr, + .vkGetDeviceProcAddr = vkGetDeviceProcAddr, + }; + + VmaAllocatorCreateInfo createInfo{ + .flags = 0, + .physicalDevice = device->getPhysicalDevice(), + .device = device->getHandle(), + .preferredLargeHeapBlockSize = 0, + .pAllocationCallbacks = nullptr, + .pDeviceMemoryCallbacks = nullptr, + .pHeapSizeLimit = nullptr, + .pVulkanFunctions = &functions, + .instance = device->getInstance(), + .vulkanApiVersion = VK_API_VERSION_1_3, + .pTypeExternalMemoryHandleTypes = nullptr + }; + + VkResult res = vmaCreateAllocator(&createInfo, &m_handle); + assert(res == VK_SUCCESS); + + } + GPUAllocator(const GPUAllocator&) = delete; + GPUAllocator& operator=(const GPUAllocator&) = delete; + ~GPUAllocator() + { + + } + + VmaAllocator getHandle() const + { + return m_handle; + } + + private: + VmaAllocator m_handle = nullptr; + }; + private: std::shared_ptr m_surface; @@ -686,12 +740,14 @@ namespace engine { VkCommandPool m_gfxCommandPool = VK_NULL_HANDLE; VkCommandBuffer m_gfxCommandBuffer = VK_NULL_HANDLE; + std::unique_ptr m_allocator; + }; class Swapchain { public: - Swapchain(std::shared_ptr device) : m_device(device) + Swapchain(Device* device) : m_device(device) { VkResult res; @@ -817,7 +873,7 @@ namespace engine { // create depth buffer - m_depthBuffer = std::make_unique(m_currentExtent, m_device.get()); + m_depthBuffer = std::make_unique(m_currentExtent, m_device); } Swapchain(const Swapchain&) = delete; @@ -833,7 +889,7 @@ namespace engine { } private: - std::shared_ptr m_device; + Device* m_device; VkSwapchainKHR m_handle = VK_NULL_HANDLE; @@ -975,6 +1031,9 @@ namespace engine { }; std::unique_ptr m_debugMessenger; // uses instance + + std::unique_ptr m_device; + std::unique_ptr m_swapchain; };