diff --git a/src/renderer/vk/device.c b/src/renderer/vk/device.c index c5209fd..5c29369 100644 --- a/src/renderer/vk/device.c +++ b/src/renderer/vk/device.c @@ -8,6 +8,7 @@ #include #include "device.h" +#include "physical_resource_manager.h" #define TARGET_API_VERSION VK_API_VERSION_1_3 @@ -521,58 +522,6 @@ static rt_result CreateDevice(rt_vk_device *dev) { return RT_SUCCESS; } -#if 0 -static rt_result CreateAllocator(rt_vk_device *dev) { -#define SET_FNC(name) fncs.name = name -#define SET_KHR_FNC(name) (fncs).name##KHR = name - VmaVulkanFunctions fncs = {NULL}; - SET_FNC(vkGetInstanceProcAddr); - SET_FNC(vkGetDeviceProcAddr); - SET_FNC(vkGetPhysicalDeviceProperties); - SET_FNC(vkGetPhysicalDeviceMemoryProperties); - SET_FNC(vkAllocateMemory); - SET_FNC(vkFreeMemory); - SET_FNC(vkMapMemory); - SET_FNC(vkUnmapMemory); - SET_FNC(vkFlushMappedMemoryRanges); - SET_FNC(vkInvalidateMappedMemoryRanges); - SET_FNC(vkBindBufferMemory); - SET_FNC(vkBindImageMemory); - SET_FNC(vkGetBufferMemoryRequirements); - SET_FNC(vkGetImageMemoryRequirements); - SET_FNC(vkCreateBuffer); - SET_FNC(vkDestroyBuffer); - SET_FNC(vkCreateImage); - SET_FNC(vkDestroyImage); - SET_FNC(vkCmdCopyBuffer); - SET_KHR_FNC(vkGetBufferMemoryRequirements2); - SET_KHR_FNC(vkGetImageMemoryRequirements2); - SET_KHR_FNC(vkBindBufferMemory2); - SET_KHR_FNC(vkBindImageMemory2); - SET_KHR_FNC(vkGetPhysicalDeviceMemoryProperties2); - SET_FNC(vkGetDeviceBufferMemoryRequirements); - SET_FNC(vkGetDeviceImageMemoryRequirements); -#undef SET_FNC -#undef SET_KHR_FNC - - VmaAllocatorCreateInfo allocator_info = { - .instance = dev->instance, - .physicalDevice = dev->phys_device, - .device = dev->device, - .pAllocationCallbacks = dev->alloc_cb, - .vulkanApiVersion = TARGET_API_VERSION, - .pVulkanFunctions = &fncs, - }; - - return vmaCreateAllocator(&allocator_info, &dev->allocator) == VK_SUCCESS ? RT_SUCCESS - : RT_UNKNOWN_ERROR; -} - -static void DestroyAllocator(void) { - vmaDestroyAllocator(dev->allocator); -} -#endif - static rt_result CreatePerFrameObjects(rt_vk_device *dev) { for (unsigned int i = 0; i < dev->max_frames_in_flight; ++i) { VkSemaphoreCreateInfo semaphore_info = { @@ -669,6 +618,10 @@ rt_create_vk_device_result rtCreateVkDevice(const rt_renderer_window_info *info) goto out; if ((res = CreateSwapchain(&dev)) != RT_SUCCESS) goto out; + rt_create_vk_physical_resource_manager_result phys_res_mgr_res = rtCreateVkPhysicalResourceManager(&dev); + if ((res = phys_res_mgr_res.result) != RT_SUCCESS) + goto out; + dev.phys_res_mgr = phys_res_mgr_res.phys_res_mgr; rt_time_delta initTime = rtTimeBetween(initBegin, rtTimeNow()); rtLog("VK", "Init complete. Took %lf seconds.", initTime); @@ -682,7 +635,7 @@ void rtDestroyVkDevice(rt_vk_device *dev) { vkDeviceWaitIdle(dev->device); DestroySwapchain(dev); DestroyPerFrameObjects(dev); - // DestroyAllocator(); + rtDestroyVkPhysicalResourceManager(&dev->phys_res_mgr); vkDestroyDevice(dev->device, dev->alloc_cb); vkDestroySurfaceKHR(dev->instance, dev->surface, dev->alloc_cb); #ifdef RT_DEBUG @@ -693,6 +646,9 @@ void rtDestroyVkDevice(rt_vk_device *dev) { /* rt_render_device_i functions */ rt_physical_resource_manager_i rtVkDevGetPhysicalResourceManager(void *o) { - rt_physical_resource_manager_i iface = {}; + rt_vk_device *dev = o; + rt_physical_resource_manager_i iface = { + .o = &dev->phys_res_mgr, + }; return iface; } \ No newline at end of file diff --git a/src/renderer/vk/device.h b/src/renderer/vk/device.h index ad95201..4f79700 100644 --- a/src/renderer/vk/device.h +++ b/src/renderer/vk/device.h @@ -6,6 +6,8 @@ #include #include +#include "physical_resource_manager.h" + #ifdef _WIN32 struct HINSTANCE__; struct HWND__; @@ -68,6 +70,8 @@ typedef struct rt_vk_device { VkImageView swapchain_image_views[4]; uint32_t swapchain_image_count; + rt_vk_physical_resource_manager phys_res_mgr; + /* *** Debug utils *** */ #ifdef RT_DEBUG VkDebugUtilsMessengerEXT messenger; diff --git a/src/renderer/vk/meson.build b/src/renderer/vk/meson.build index 147b7a0..5ffcbb9 100644 --- a/src/renderer/vk/meson.build +++ b/src/renderer/vk/meson.build @@ -20,6 +20,7 @@ if get_option('build_vk') 'init.c', 'physical_resource_manager.c', 'swapchain.c', + 'vma_impl.cpp', '../../../contrib/volk/volk.c', '../../../contrib/volk/volk.h', diff --git a/src/renderer/vk/physical_resource_manager.c b/src/renderer/vk/physical_resource_manager.c index 4ed6184..3d0a898 100644 --- a/src/renderer/vk/physical_resource_manager.c +++ b/src/renderer/vk/physical_resource_manager.c @@ -1 +1,66 @@ -#include "physical_resource_manager.h" \ No newline at end of file +#include "physical_resource_manager.h" +#include "device.h" + + +static VmaAllocator CreateAllocator(rt_vk_device *dev) { +#define SET_FNC(name) fncs.name = name +#define SET_KHR_FNC(name) (fncs).name##KHR = name + VmaVulkanFunctions fncs = {NULL}; + SET_FNC(vkGetInstanceProcAddr); + SET_FNC(vkGetDeviceProcAddr); + SET_FNC(vkGetPhysicalDeviceProperties); + SET_FNC(vkGetPhysicalDeviceMemoryProperties); + SET_FNC(vkAllocateMemory); + SET_FNC(vkFreeMemory); + SET_FNC(vkMapMemory); + SET_FNC(vkUnmapMemory); + SET_FNC(vkFlushMappedMemoryRanges); + SET_FNC(vkInvalidateMappedMemoryRanges); + SET_FNC(vkBindBufferMemory); + SET_FNC(vkBindImageMemory); + SET_FNC(vkGetBufferMemoryRequirements); + SET_FNC(vkGetImageMemoryRequirements); + SET_FNC(vkCreateBuffer); + SET_FNC(vkDestroyBuffer); + SET_FNC(vkCreateImage); + SET_FNC(vkDestroyImage); + SET_FNC(vkCmdCopyBuffer); + SET_KHR_FNC(vkGetBufferMemoryRequirements2); + SET_KHR_FNC(vkGetImageMemoryRequirements2); + SET_KHR_FNC(vkBindBufferMemory2); + SET_KHR_FNC(vkBindImageMemory2); + SET_KHR_FNC(vkGetPhysicalDeviceMemoryProperties2); + SET_FNC(vkGetDeviceBufferMemoryRequirements); + SET_FNC(vkGetDeviceImageMemoryRequirements); +#undef SET_FNC +#undef SET_KHR_FNC + + VmaAllocatorCreateInfo allocator_info = { + .instance = dev->instance, + .physicalDevice = dev->phys_device, + .device = dev->device, + .pAllocationCallbacks = dev->alloc_cb, + .vulkanApiVersion = VK_API_VERSION_1_3, + .pVulkanFunctions = &fncs, + }; + + VmaAllocator allocator; + if (vmaCreateAllocator(&allocator_info, &allocator) != VK_SUCCESS) + return NULL; + return allocator; +} + +rt_create_vk_physical_resource_manager_result rtCreateVkPhysicalResourceManager(struct rt_vk_device *dev) { + rt_vk_physical_resource_manager phys_res_mgr; + phys_res_mgr.dev = dev; + phys_res_mgr.allocator = CreateAllocator(dev); + if (!phys_res_mgr.allocator) { + return (rt_create_vk_physical_resource_manager_result){.result = RT_UNKNOWN_ERROR}; + } + + return (rt_create_vk_physical_resource_manager_result){.result = RT_SUCCESS, .phys_res_mgr = phys_res_mgr}; +} + +void rtDestroyVkPhysicalResourceManager(rt_vk_physical_resource_manager *phys_res_mgr) { + vmaDestroyAllocator(phys_res_mgr->allocator); +} \ No newline at end of file diff --git a/src/renderer/vk/physical_resource_manager.h b/src/renderer/vk/physical_resource_manager.h index d4481bf..df0d803 100644 --- a/src/renderer/vk/physical_resource_manager.h +++ b/src/renderer/vk/physical_resource_manager.h @@ -1,10 +1,27 @@ #ifndef RT_VK_PHYSICAL_RESOURCE_MANAGER_H #define RT_VK_PHYSICAL_RESOURCE_MANAGER_H +#include +#include + +#define VMA_STATIC_VULKAN_FUNCTIONS 0 +#define VMA_DYNAMI_VULKAN_FUNCTIONS 0 +#include + struct rt_vk_device; typedef struct { struct rt_vk_device *dev; + VmaAllocator allocator; } rt_vk_physical_resource_manager; +typedef struct { + rt_result result; + rt_vk_physical_resource_manager phys_res_mgr; +} rt_create_vk_physical_resource_manager_result; + +rt_create_vk_physical_resource_manager_result rtCreateVkPhysicalResourceManager(struct rt_vk_device *dev); + +void rtDestroyVkPhysicalResourceManager(rt_vk_physical_resource_manager *phys_res_mgr); + #endif diff --git a/src/renderer/vk/vma_impl.cpp b/src/renderer/vk/vma_impl.cpp new file mode 100644 index 0000000..87fa35e --- /dev/null +++ b/src/renderer/vk/vma_impl.cpp @@ -0,0 +1,21 @@ +#ifdef _MSC_VER +#pragma warning(push, 0) +#elif defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#pragma GCC diagnostic ignored "-Wmissing-braces" +#pragma GCC diagnostic ignored "-Wimplicit-fallthrough" +#pragma GCC diagnostic ignored "-Wconversion" +#pragma GCC diagnostic ignored "-Wunused-variable" +#pragma GCC diagnostic ignored "-Wparentheses" +#endif +#include +#define VMA_STATIC_VULKAN_FUNCTIONS 0 +#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0 +#define VMA_IMPLEMENTATION +#include +#ifdef _MSC_VER +#pragma warning(pop) +#elif defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif