diff --git a/CMakeLists.txt b/CMakeLists.txt index b849cf8..b68b976 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,5 +10,17 @@ find_package(Vulkan REQUIRED) find_package(glfw3 REQUIRED) find_package(glm REQUIRED) -target_link_libraries(VulkanSimulation glfw Vulkan::Vulkan) +include(FetchContent) +FetchContent_Declare(vma + GIT_REPOSITORY https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator.git + GIT_TAG v3.1.0 + GIT_PROGRESS ON + FIND_PACKAGE_ARGS 3.1.0) +FetchContent_MakeAvailable(vma) + +target_link_libraries(VulkanSimulation glm::glm glfw Vulkan::Vulkan GPUOpen::VulkanMemoryAllocator) target_include_directories(VulkanSimulation PRIVATE include) +target_compile_definitions(VulkanSimulation PRIVATE + GLM_FORCE_RADIANS + GLM_FORCE_DEPTH_ZERO_TO_ONE +) diff --git a/include/glm.h b/include/glm.h deleted file mode 100644 index 2626495..0000000 --- a/include/glm.h +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -#define GLM_FORCE_RADIANS -#define GLM_FORCE_DEPTH_ZERO_TO_ONE -#include -#include \ No newline at end of file diff --git a/include/vulkan/application.hpp b/include/vulkan/application.hpp index 2a10195..30c3fa5 100644 --- a/include/vulkan/application.hpp +++ b/include/vulkan/application.hpp @@ -46,12 +46,9 @@ private: Buffer* vertexBuffer = nullptr; Buffer* indexBuffer = nullptr; Buffer* uniformBuffer = nullptr; - CommandPool* commandPool = nullptr; ComputePipeline* computePipeline = nullptr; - void* uniformBufferMapped = nullptr; - void updateUniformBuffer(); void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex); diff --git a/include/vulkan/buffer.hpp b/include/vulkan/buffer.hpp index 2c1551a..bcd7da4 100644 --- a/include/vulkan/buffer.hpp +++ b/include/vulkan/buffer.hpp @@ -3,27 +3,22 @@ #include #include #include "vertex.hpp" - -const std::vector indices = { - 0, 1, 2, 0, 3, 1, - 4, 5, 6, 4, 7, 5 -}; +#include "vk_mem_alloc.h" class Instance; class CommandPool; class Buffer { public: - explicit Buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties); + explicit Buffer(VkDeviceSize size, VkBufferUsageFlags bufferUsage, + VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags); + explicit Buffer(VkDeviceSize size, void* data, VkDeviceSize dataSize, VkBufferUsageFlags bufferUsage, + VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags); ~Buffer(); VkBuffer handle = VK_NULL_HANDLE; - VkDeviceMemory memory = VK_NULL_HANDLE; + VmaAllocation allocation = VK_NULL_HANDLE; + VmaAllocationInfo allocationInfo {}; VkDeviceSize size; - - static Buffer* createStagedVertexBuffer(); - static Buffer* createStagedIndexBuffer(); - - void copyTo(Buffer* dst, CommandPool* commandPool); - private: + void copyTo(Buffer* dst); }; \ No newline at end of file diff --git a/include/vulkan/image.hpp b/include/vulkan/image.hpp index faa1a6d..25d6db4 100644 --- a/include/vulkan/image.hpp +++ b/include/vulkan/image.hpp @@ -1,13 +1,14 @@ #pragma once #include +#include "vk_mem_alloc.h" class Instance; class Image { public: explicit Image(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, - VkImageUsageFlags usage, VkMemoryPropertyFlags properties); + VkImageUsageFlags usage); ~Image(); VkImageView createView(VkFormat format, VkImageAspectFlags aspectFlags); @@ -15,5 +16,6 @@ public: VkImage handle = VK_NULL_HANDLE; VkImageView view = VK_NULL_HANDLE; private: - VkDeviceMemory memory = VK_NULL_HANDLE; + VmaAllocation allocation = VK_NULL_HANDLE; + VmaAllocationInfo allocationInfo {}; }; \ No newline at end of file diff --git a/include/vulkan/instance.hpp b/include/vulkan/instance.hpp index 9e16bb2..cdd0c53 100644 --- a/include/vulkan/instance.hpp +++ b/include/vulkan/instance.hpp @@ -4,6 +4,9 @@ #include #include #include +#include "vk_mem_alloc.h" + +class CommandPool; class Instance { public: @@ -14,10 +17,13 @@ public: VkSurfaceKHR surface = VK_NULL_HANDLE; VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkDevice device = VK_NULL_HANDLE; + VmaAllocator allocator = VK_NULL_HANDLE; VkQueue graphicsQueue = VK_NULL_HANDLE; VkQueue presentQueue = VK_NULL_HANDLE; VkQueue computeQueue = VK_NULL_HANDLE; + CommandPool* commandPool = nullptr; + struct QueueFamilyIndices { std::optional graphicsFamily; std::optional computeFamily; @@ -49,8 +55,9 @@ private: void createSurface(); void pickPhysicalDevice(); void createLogicalDevice(); + void createAllocator(); - bool isDeviceSuitable(VkPhysicalDevice physicalDevice); + bool isDeviceSuitable(VkPhysicalDevice potentialPhysicalDevice); static bool checkDeviceExtensionSupport(VkPhysicalDevice device); diff --git a/include/vulkan/pipeline.hpp b/include/vulkan/pipeline.hpp index 1324064..16ba0ac 100644 --- a/include/vulkan/pipeline.hpp +++ b/include/vulkan/pipeline.hpp @@ -1,6 +1,6 @@ #pragma once -#include "../glm.h" +#include #include class Instance; diff --git a/include/vulkan/vertex.hpp b/include/vulkan/vertex.hpp index fc60cd0..78e4548 100644 --- a/include/vulkan/vertex.hpp +++ b/include/vulkan/vertex.hpp @@ -1,6 +1,6 @@ #pragma once -#include "../glm.h" +#include #include #include diff --git a/src/vulkan/application.cpp b/src/vulkan/application.cpp index bc97d98..ca8d315 100644 --- a/src/vulkan/application.cpp +++ b/src/vulkan/application.cpp @@ -6,41 +6,58 @@ #include "vulkan/command_pool.hpp" #include "vulkan/image.hpp" +#include + Application::Application() { new Instance; swapchain = new Swapchain(); graphicsPipeline = new Pipeline(swapchain->renderPass); - auto stagedVertexBuffer = Buffer::createStagedVertexBuffer(); - vertexBuffer = new Buffer(stagedVertexBuffer->size, - VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + std::vector vertices = { + {{0.0, -0.5, 0}, {1, 0, 0}}, + {{0.5, 0.5, 0}, {0, 1, 0}}, + {{-0.5, 0.5, 0}, {0, 0, 1}}, + {{0.8, -0.5, 0}, {1, 1, 1}} + }; + + vertices.insert(vertices.end(), vertices.begin(), vertices.end()); + + for (size_t i = 4; i < vertices.size(); i++){ + vertices[i].pos.z -= 0.5f; + } + + VkDeviceSize size = vertices.size() * sizeof(Vertex); + vertexBuffer = new Buffer(size, vertices.data(), size, + VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, + VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); - auto stagedIndexBuffer = Buffer::createStagedIndexBuffer(); - indexBuffer = new Buffer(stagedIndexBuffer->size, - VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + std::vector indices = { + 0, 1, 2, 0, 3, 1, + 4, 5, 6, 4, 7, 5 + }; + + size = indices.size() * sizeof(uint32_t); + indexBuffer = new Buffer(size, indices.data(), size, + VK_BUFFER_USAGE_INDEX_BUFFER_BIT, + VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); VkDeviceSize bufferSize = sizeof(UniformBufferObject); uniformBuffer = new Buffer(bufferSize, - VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); - vkMapMemory(Instance::instance->device, uniformBuffer->memory, 0, bufferSize, 0, &uniformBufferMapped); + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, + VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); graphicsPipeline->createDescriptorSet(uniformBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); computePipeline = new ComputePipeline(vertexBuffer); - commandPool = new CommandPool(); - - stagedVertexBuffer->copyTo(vertexBuffer, commandPool); - stagedIndexBuffer->copyTo(indexBuffer, commandPool); - - delete stagedVertexBuffer; - delete stagedIndexBuffer; createSyncObjects(); recordComputeCommandBuffer(); + + char* stats; + vmaBuildStatsString(Instance::instance->allocator, &stats, VK_TRUE); + // printf("%s", stats); } void Application::updateUniformBuffer() { @@ -56,7 +73,7 @@ void Application::updateUniformBuffer() { 0.1f, 10.f); ubo.projection[1][1] *= -1; - memcpy(uniformBufferMapped, &ubo, sizeof(UniformBufferObject)); + memcpy(uniformBuffer->allocationInfo.pMappedData, &ubo, sizeof(UniformBufferObject)); } void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { @@ -102,7 +119,7 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im vkCmdBindIndexBuffer(commandBuffer, indexBuffer->handle, 0, VK_INDEX_TYPE_UINT32); vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline->layout, 0, 1, &graphicsPipeline->descriptorSet, 0, nullptr); - vkCmdDrawIndexed(commandBuffer, indices.size(), 1, 0, 0, 0); + vkCmdDrawIndexed(commandBuffer, 12, 1, 0, 0, 0); vkCmdEndRenderPass(commandBuffer); vkEndCommandBuffer(commandBuffer); @@ -144,8 +161,8 @@ void Application::drawFrame() { vkResetFences(Instance::instance->device, 1, &renderInFlightFence); - vkResetCommandBuffer(commandPool->graphicsBuffer, 0); - recordCommandBuffer(commandPool->graphicsBuffer, imageIndex); + vkResetCommandBuffer(Instance::instance->commandPool->graphicsBuffer, 0); + recordCommandBuffer(Instance::instance->commandPool->graphicsBuffer, imageIndex); updateUniformBuffer(); @@ -158,7 +175,7 @@ void Application::drawFrame() { submitInfo.pWaitSemaphores = waitSemaphores; submitInfo.pWaitDstStageMask = waitStages; submitInfo.commandBufferCount = 1; - submitInfo.pCommandBuffers = &commandPool->graphicsBuffer; + submitInfo.pCommandBuffers = &Instance::instance->commandPool->graphicsBuffer; VkSemaphore signalSemaphores[] = {renderFinishedSemaphore}; submitInfo.signalSemaphoreCount = 1; @@ -190,7 +207,7 @@ void Application::update() { VkSubmitInfo submit {}; submit.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submit.commandBufferCount = 1; - submit.pCommandBuffers = &commandPool->computeBuffer; + submit.pCommandBuffers = &Instance::instance->commandPool->computeBuffer; submit.signalSemaphoreCount = 1; submit.pSignalSemaphores = &computeFinishedSemaphore; @@ -198,7 +215,7 @@ void Application::update() { } void Application::recordComputeCommandBuffer() { - VkCommandBuffer buffer = commandPool->computeBuffer; + VkCommandBuffer buffer = Instance::instance->commandPool->computeBuffer; VkCommandBufferBeginInfo beginInfo {}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; @@ -219,7 +236,6 @@ Application::~Application() { vkDestroySemaphore(Instance::instance->device, computeFinishedSemaphore, nullptr); vkDestroyFence(Instance::instance->device, renderInFlightFence, nullptr); vkDestroyFence(Instance::instance->device, computeInFlightFence, nullptr); - delete commandPool; delete vertexBuffer; delete indexBuffer; delete uniformBuffer; diff --git a/src/vulkan/buffer.cpp b/src/vulkan/buffer.cpp index 5fd18c5..2d02f00 100644 --- a/src/vulkan/buffer.cpp +++ b/src/vulkan/buffer.cpp @@ -1,85 +1,50 @@ #include #include #include "vulkan/buffer.hpp" -#include "vulkan/vertex.hpp" #include "vulkan/instance.hpp" #include "vulkan/command_pool.hpp" #include "vulkan/utils.h" -Buffer::Buffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties) : size(size){ - VkBufferCreateInfo bufferInfo{}; - bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; - bufferInfo.size = size; - bufferInfo.usage = usage; - bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; +#include "vk_mem_alloc.h" - vkCreateBuffer(Instance::instance->device, &bufferInfo, nullptr, &handle); +Buffer::Buffer(VkDeviceSize size, VkBufferUsageFlags bufferUsage, VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags) : size(size) { + VkBufferCreateInfo bufferCreateInfo {}; + bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; + bufferCreateInfo.size = size; + bufferCreateInfo.usage = bufferUsage; + bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - VkMemoryRequirements memoryRequirements; - vkGetBufferMemoryRequirements(Instance::instance->device, handle, &memoryRequirements); + VmaAllocationCreateInfo allocationCreateInfo {}; + allocationCreateInfo.usage = memoryUsage; + allocationCreateInfo.flags = flags; - VkMemoryAllocateInfo allocateInfo {}; - allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - allocateInfo.allocationSize = memoryRequirements.size; - allocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, properties); - - vkAllocateMemory(Instance::instance->device, &allocateInfo, nullptr, &memory); - - vkBindBufferMemory(Instance::instance->device, handle, memory, 0); -} - -Buffer::~Buffer() { - vkFreeMemory(Instance::instance->device, memory, nullptr); - vkDestroyBuffer(Instance::instance->device, handle, nullptr); + vmaCreateBuffer(Instance::instance->allocator, &bufferCreateInfo, &allocationCreateInfo, &handle, &allocation, &allocationInfo);; } -Buffer* Buffer::createStagedVertexBuffer() { - std::vector vertices = { - {{0.0, -0.5, 0}, {1, 0, 0}}, - {{0.5, 0.5, 0}, {0, 1, 0}}, - {{-0.5, 0.5, 0}, {0, 0, 1}}, - {{0.8, -0.5, 0}, {1, 1, 1}} - }; - - vertices.insert(vertices.end(), vertices.begin(), vertices.end()); +Buffer::Buffer(VkDeviceSize size, void *data, VkDeviceSize dataSize, VkBufferUsageFlags bufferUsage, + VmaMemoryUsage memoryUsage, VmaAllocationCreateFlags flags) + : Buffer(size, bufferUsage | VK_BUFFER_USAGE_TRANSFER_DST_BIT, memoryUsage, flags){ - for (size_t i = 4; i < vertices.size(); i++){ - vertices[i].pos.z -= 0.5f; - } + Buffer stagingBuffer( + size, + VK_BUFFER_USAGE_TRANSFER_SRC_BIT, + VMA_MEMORY_USAGE_AUTO, + VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT + ); - VkDeviceSize size = vertices.size() * sizeof(Vertex); - VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; - VkMemoryPropertyFlags properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; - - auto stagingBuffer = new Buffer(size, usage, properties); - - void* data; - vkMapMemory(Instance::instance->device, stagingBuffer->memory, 0, size, 0, &data); - memcpy(data, vertices.data(), size); - vkUnmapMemory(Instance::instance->device, stagingBuffer->memory); - - return stagingBuffer; + memcpy(stagingBuffer.allocationInfo.pMappedData, data, dataSize); + stagingBuffer.copyTo(this); } -Buffer *Buffer::createStagedIndexBuffer() { - VkDeviceSize size = indices.size() * sizeof(uint32_t); - VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; - VkMemoryPropertyFlags properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT; - auto stagingBuffer = new Buffer(size, usage, properties); - - uint32_t *data; - vkMapMemory(Instance::instance->device, stagingBuffer->memory, 0, size, 0, reinterpret_cast(&data)); - memcpy(data, indices.data(), sizeof(uint32_t) * indices.size()); - vkUnmapMemory(Instance::instance->device, stagingBuffer->memory); - - return stagingBuffer; +Buffer::~Buffer() { + vmaDestroyBuffer(Instance::instance->allocator, handle, allocation); } -void Buffer::copyTo(Buffer *dst, CommandPool* commandPool) { +void Buffer::copyTo(Buffer *dst) { VkCommandBufferAllocateInfo allocateInfo {}; allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - allocateInfo.commandPool = commandPool->handle; + allocateInfo.commandPool = Instance::instance->commandPool->handle; allocateInfo.commandBufferCount = 1; VkCommandBuffer commandBuffer; @@ -103,5 +68,5 @@ void Buffer::copyTo(Buffer *dst, CommandPool* commandPool) { vkQueueSubmit(Instance::instance->graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); vkDeviceWaitIdle(Instance::instance->device); - vkFreeCommandBuffers(Instance::instance->device, commandPool->handle, 1, &commandBuffer); + vkFreeCommandBuffers(Instance::instance->device, Instance::instance->commandPool->handle, 1, &commandBuffer); } diff --git a/src/vulkan/image.cpp b/src/vulkan/image.cpp index 9df1c53..5b9486f 100644 --- a/src/vulkan/image.cpp +++ b/src/vulkan/image.cpp @@ -1,10 +1,11 @@ +#include #include "vulkan/image.hpp" #include "vulkan/instance.hpp" #include "vulkan/utils.h" Image::Image(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, - VkImageUsageFlags usage, VkMemoryPropertyFlags properties) { + VkImageUsageFlags usage) { VkImageCreateInfo imageCreateInfo {}; imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; @@ -21,25 +22,17 @@ Image::Image(uint32_t width, uint32_t height, VkFormat format, VkImageTiling til imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - vkCreateImage(Instance::instance->device, &imageCreateInfo, nullptr, &handle); + VmaAllocationCreateInfo allocationCreateInfo {}; + allocationCreateInfo.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE; + allocationCreateInfo.flags = 0; - VkMemoryRequirements memoryRequirements; - vkGetImageMemoryRequirements(Instance::instance->device, handle, &memoryRequirements); - - VkMemoryAllocateInfo allocateInfo {}; - allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - allocateInfo.allocationSize = memoryRequirements.size; - allocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, properties); - - vkAllocateMemory(Instance::instance->device, &allocateInfo, nullptr, &memory); - vkBindImageMemory(Instance::instance->device, handle, memory, 0); + vmaCreateImage(Instance::instance->allocator, &imageCreateInfo, &allocationCreateInfo, &handle, &allocation, &allocationInfo); } Image::~Image() { - vkFreeMemory(Instance::instance->device, memory, nullptr); if (view) vkDestroyImageView(Instance::instance->device, view, nullptr); - vkDestroyImage(Instance::instance->device, handle, nullptr); + vmaDestroyImage(Instance::instance->allocator, handle, allocation); } VkImageView Image::createView(VkFormat format, VkImageAspectFlags aspectFlags) { diff --git a/src/vulkan/instance.cpp b/src/vulkan/instance.cpp index 9794a7d..67d53f7 100644 --- a/src/vulkan/instance.cpp +++ b/src/vulkan/instance.cpp @@ -2,6 +2,8 @@ #include #include "vulkan/instance.hpp" #include "vulkan/swapchain.hpp" +#include "vulkan/command_pool.hpp" +#include "vk_mem_alloc.h" #include const std::vector deviceExtensions = { @@ -50,13 +52,14 @@ Instance* Instance::instance = nullptr; Instance::Instance() { instance = this; - initWindow(); createInstance(); createSurface(); pickPhysicalDevice(); createLogicalDevice(); + createAllocator(); + commandPool = new CommandPool; } void Instance::initWindow() { @@ -79,7 +82,7 @@ void Instance::createInstance() { applicationInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); applicationInfo.pEngineName = "No Engine"; applicationInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); - applicationInfo.apiVersion = VK_API_VERSION_1_0; + applicationInfo.apiVersion = VK_API_VERSION_1_2; uint32_t glfwExtensionCount = 0; const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount); @@ -166,6 +169,19 @@ void Instance::createLogicalDevice() { vkGetDeviceQueue(device, indices.graphicsAndComputeFamily.value(), 0, &computeQueue); } +void Instance::createAllocator() { + VmaVulkanFunctions functions {}; + functions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr; + functions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr; + + VmaAllocatorCreateInfo allocatorCreateInfo {}; + allocatorCreateInfo.instance = handle; + allocatorCreateInfo.device = device; + allocatorCreateInfo.physicalDevice = physicalDevice; + + vmaCreateAllocator(&allocatorCreateInfo, &allocator); +} + Instance::QueueFamilyIndices Instance::findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR surface) { uint32_t queueFamilyCount = 0; vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); @@ -199,20 +215,20 @@ Instance::QueueFamilyIndices Instance::findQueueFamilies(VkPhysicalDevice device return indices; } -bool Instance::isDeviceSuitable(VkPhysicalDevice physicalDevice) { +bool Instance::isDeviceSuitable(VkPhysicalDevice potentialPhysicalDevice) { VkPhysicalDeviceProperties deviceProperties; - vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties); + vkGetPhysicalDeviceProperties(potentialPhysicalDevice, &deviceProperties); VkPhysicalDeviceFeatures deviceFeatures; - vkGetPhysicalDeviceFeatures(physicalDevice, &deviceFeatures); + vkGetPhysicalDeviceFeatures(potentialPhysicalDevice, &deviceFeatures); - QueueFamilyIndices indices = findQueueFamilies(physicalDevice, surface); + QueueFamilyIndices indices = findQueueFamilies(potentialPhysicalDevice, surface); - bool extensionsSupported = checkDeviceExtensionSupport(physicalDevice); + bool extensionsSupported = checkDeviceExtensionSupport(potentialPhysicalDevice); bool swapChainAdequate = false; if (extensionsSupported){ - SwapchainSupportDetails details = querySwapchainSupport(physicalDevice, surface); + SwapchainSupportDetails details = querySwapchainSupport(potentialPhysicalDevice, surface); swapChainAdequate = !details.formats.empty() && !details.presentModes.empty(); } @@ -236,6 +252,8 @@ bool Instance::checkDeviceExtensionSupport(VkPhysicalDevice device) { } Instance::~Instance() { + delete commandPool; + vmaDestroyAllocator(allocator); vkDestroyDevice(device, nullptr); vkDestroySurfaceKHR(handle, surface, nullptr); vkDestroyInstance(handle, nullptr); diff --git a/src/vulkan/swapchain.cpp b/src/vulkan/swapchain.cpp index 591abf0..2fcb0fe 100644 --- a/src/vulkan/swapchain.cpp +++ b/src/vulkan/swapchain.cpp @@ -210,8 +210,7 @@ void Swapchain::createDepthResources() { extent.height, VK_FORMAT_D32_SFLOAT, VK_IMAGE_TILING_OPTIMAL, - VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT ); depthImage->createView(VK_FORMAT_D32_SFLOAT, VK_IMAGE_ASPECT_DEPTH_BIT); } diff --git a/src/vulkan/vma.cpp b/src/vulkan/vma.cpp new file mode 100644 index 0000000..00317e0 --- /dev/null +++ b/src/vulkan/vma.cpp @@ -0,0 +1,2 @@ +#define VMA_IMPLEMENTATION +#include "vk_mem_alloc.h" \ No newline at end of file