Added VMA.

This commit is contained in:
Anish Bhobe 2025-06-12 22:29:20 +02:00
parent 35347d28e0
commit e7d74e6b0f
7 changed files with 53 additions and 10 deletions

View File

@ -154,6 +154,7 @@
<ClCompile Include="GlobalMemory.cpp" /> <ClCompile Include="GlobalMemory.cpp" />
<ClCompile Include="MiscData.cpp" /> <ClCompile Include="MiscData.cpp" />
<ClCompile Include="RenderDevice.cpp" /> <ClCompile Include="RenderDevice.cpp" />
<ClCompile Include="VmaImpl.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include=".gitignore" /> <None Include=".gitignore" />

View File

@ -36,6 +36,9 @@
<ClCompile Include="GlobalMemory.cpp"> <ClCompile Include="GlobalMemory.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="VmaImpl.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="vcpkg.json"> <None Include="vcpkg.json">

View File

@ -278,7 +278,6 @@ void MiscData::init(RenderDevice const& renderDevice)
.imageMemoryBarrierCount = 1, .imageMemoryBarrierCount = 1,
.pImageMemoryBarriers = &renderToPresentBarrier, .pImageMemoryBarriers = &renderToPresentBarrier,
}; };
} }
void MiscData::cleanup(RenderDevice const& renderDevice) void MiscData::cleanup(RenderDevice const& renderDevice)

View File

@ -5,6 +5,7 @@
#include <SDL3/SDL_log.h> #include <SDL3/SDL_log.h>
#include <array> #include <array>
#include <optional>
#include <span> #include <span>
#include "Frame.h" #include "Frame.h"
@ -60,7 +61,8 @@ RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo con
VkPhysicalDevice physicalDeviceInUse = nullptr; VkPhysicalDevice physicalDeviceInUse = nullptr;
VkDevice device = nullptr; VkDevice device = nullptr;
uint32_t directQueueFamilyIndex = -1; VmaAllocator gpuAllocator = nullptr;
std::optional<uint32_t> directQueueFamilyIndex;
VkQueue directQueue = nullptr; VkQueue directQueue = nullptr;
// Create Device and Queue // Create Device and Queue
{ {
@ -152,13 +154,14 @@ RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo con
} }
ASSERT(physicalDeviceInUse); ASSERT(physicalDeviceInUse);
ASSERT(directQueueFamilyIndex.has_value());
float priority = 1.0f; float priority = 1.0f;
VkDeviceQueueCreateInfo queueCreateInfo = { VkDeviceQueueCreateInfo queueCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.pNext = nullptr, .pNext = nullptr,
.flags = 0, .flags = 0,
.queueFamilyIndex = directQueueFamilyIndex, .queueFamilyIndex = directQueueFamilyIndex.value(),
.queueCount = 1, .queueCount = 1,
.pQueuePriorities = &priority, .pQueuePriorities = &priority,
}; };
@ -193,7 +196,29 @@ RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo con
}; };
VK_CHECK(vkCreateDevice(physicalDeviceInUse, &deviceCreateInfo, nullptr, &device)); 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); mem->restoreState(tempAllocStart);
} }
@ -340,13 +365,13 @@ RenderDevice* CreateRenderDevice(GlobalMemory* mem, RenderDevice::CreateInfo con
Frame* frames = reinterpret_cast<Frame*>(mem->allocate(sizeof(Frame) * swapchainImageCount)); Frame* frames = reinterpret_cast<Frame*>(mem->allocate(sizeof(Frame) * swapchainImageCount));
for (uint32_t i = 0; i != swapchainImageCount; ++i) 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)); Byte* allocation = mem->allocate(sizeof(RenderDevice), alignof(RenderDevice));
return new (allocation) RenderDevice{ return new (allocation) RenderDevice{
instance, surface, physicalDeviceInUse, instance, surface, physicalDeviceInUse,
device, directQueue, directQueueFamilyIndex, device, gpuAllocator, directQueue, directQueueFamilyIndex.value(),
swapchainFormat, swapchainExtent, swapchain, swapchainImages, swapchainViews, frames, swapchainImageCount, 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, RenderDevice::RenderDevice(VkInstance const instance, VkSurfaceKHR const surface, VkPhysicalDevice const physicalDeviceInUse,
VkDevice const device, VkQueue const directQueue, uint32_t const directQueueFamilyIndex, VkFormat const swapchainFormat, VkDevice const device, VmaAllocator gpuAllocator, VkQueue const directQueue, uint32_t const directQueueFamilyIndex,
VkExtent2D const swapchainExtent, VkSwapchainKHR const swapchain, VkImage* swapchainImages, VkImageView* swapchainViews, VkFormat const swapchainFormat, VkExtent2D const swapchainExtent, VkSwapchainKHR const swapchain, VkImage* swapchainImages,
Frame* frames, uint32_t const swapchainImageCount) VkImageView* swapchainViews, Frame* frames, uint32_t const swapchainImageCount)
: instance{ instance } : instance{ instance }
, surface{ surface } , surface{ surface }
, physicalDeviceInUse{ physicalDeviceInUse } , physicalDeviceInUse{ physicalDeviceInUse }
, device{ device } , device{ device }
, gpuAllocator{ gpuAllocator }
, directQueue{ directQueue } , directQueue{ directQueue }
, directQueueFamilyIndex{ directQueueFamilyIndex } , directQueueFamilyIndex{ directQueueFamilyIndex }
, swapchainFormat{ swapchainFormat } , swapchainFormat{ swapchainFormat }

View File

@ -2,6 +2,10 @@
#include <volk.h> #include <volk.h>
#define VMA_STATIC_VULKAN_FUNCTIONS 0
#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0
#include <vma/vk_mem_alloc.h>
#include <SDL3/SDL_video.h> #include <SDL3/SDL_video.h>
#include <SDL3/SDL_vulkan.h> #include <SDL3/SDL_vulkan.h>
@ -24,10 +28,11 @@ struct RenderDevice
VkInstance instance; VkInstance instance;
VkSurfaceKHR surface; VkSurfaceKHR surface;
VkPhysicalDevice physicalDeviceInUse; VkPhysicalDevice physicalDeviceInUse;
VkDevice device; VkDevice device;
VmaAllocator gpuAllocator;
VkQueue directQueue; VkQueue directQueue;
uint32_t directQueueFamilyIndex; uint32_t directQueueFamilyIndex;
// TODO: Pack?
VkFormat swapchainFormat; VkFormat swapchainFormat;
VkExtent2D swapchainExtent; VkExtent2D swapchainExtent;
@ -48,6 +53,7 @@ struct RenderDevice
VkSurfaceKHR surface, VkSurfaceKHR surface,
VkPhysicalDevice physicalDeviceInUse, VkPhysicalDevice physicalDeviceInUse,
VkDevice device, VkDevice device,
VmaAllocator gpuAllocator,
VkQueue directQueue, VkQueue directQueue,
uint32_t directQueueFamilyIndex, uint32_t directQueueFamilyIndex,
// TODO: Pack? // TODO: Pack?

7
VmaImpl.cpp Normal file
View File

@ -0,0 +1,7 @@
#include <volk.h>
#pragma warning(push, 1)
#pragma warning(disable : 5045)
#define VMA_IMPLEMENTATION
#include <vma/vk_mem_alloc.h>
#pragma warning(pop)

View File

@ -2,6 +2,7 @@
"dependencies": [ "dependencies": [
"volk", "volk",
"shader-slang", "shader-slang",
"vulkan-memory-allocator",
{ {
"name": "sdl3", "name": "sdl3",
"features": [ "vulkan" ] "features": [ "vulkan" ]