From a9b0918235b386a4c90a30ee902b0e5911db72ab Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Thu, 6 Oct 2022 16:26:29 +0100 Subject: [PATCH] Start adding depth buffer --- src/gfx_device_vulkan.cpp | 68 +++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 9256d45..3359a66 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -71,7 +71,6 @@ namespace engine { m_swapchain = std::make_unique(device); // owns the device - INFO("Instance use count: {}", instance.use_count()); } private: @@ -565,6 +564,27 @@ namespace engine { vkGetDeviceQueue(m_handle, q.familyIndex, q.queueIndex, &q.handle); } + Queue gfxQueue = getGraphicsQueue(); + + VkCommandPoolCreateInfo gfxCmdPoolInfo{ + .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, + .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, + .queueFamilyIndex = gfxQueue.familyIndex, + }; + + res = vkCreateCommandPool(m_handle, &gfxCmdPoolInfo, nullptr, &m_gfxCommandPool); + assert(res == VK_SUCCESS); + + VkCommandBufferAllocateInfo gfxCmdBufInfo{ + .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, + .commandPool = m_gfxCommandPool, + .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, + .commandBufferCount = 1 + }; + + res = vkAllocateCommandBuffers(m_handle, &gfxCmdBufInfo, &m_gfxCommandBuffer); + assert(res == VK_SUCCESS); + } Device(const Device&) = delete; Device& operator=(const Device&) = delete; @@ -572,6 +592,7 @@ namespace engine { ~Device() { TRACE("Destroying device..."); + vkDestroyCommandPool(m_handle, m_gfxCommandPool, nullptr); vkDestroyDevice(m_handle, nullptr); } @@ -601,6 +622,16 @@ namespace engine { VkQueue handle; }; + VkCommandBuffer getGraphicsCommandBuffer() + { + return m_gfxCommandBuffer; + } + + std::shared_ptr getSurface() + { + return m_surface; + } + Queue getGraphicsQueue() { for (const auto& queue : m_queues) { @@ -617,18 +648,13 @@ namespace engine { throw std::runtime_error("Unable to find transfer queue"); } - Queue getComputeQueue() + /*Queue getComputeQueue() { for (const auto& queue : m_queues) { if (queue.supportsCompute) return queue; } throw std::runtime_error("Unable to find compute queue"); - } - - std::shared_ptr getSurface() - { - return m_surface; - } + }*/ private: std::shared_ptr m_surface; @@ -639,10 +665,15 @@ namespace engine { std::vector m_queues{}; + VkCommandPool m_gfxCommandPool = VK_NULL_HANDLE; + VkCommandBuffer m_gfxCommandBuffer = VK_NULL_HANDLE; + }; class Swapchain { + // this class also creates a depth buffer image + public: Swapchain(std::shared_ptr device) : m_device(device) { @@ -765,6 +796,24 @@ namespace engine { assert (res == VK_SUCCESS); m_imageViews.push_back(imageView); + + // create depth buffer + + VkImageCreateInfo depthImageInfo{ + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .flags = 0, + .imageType = VK_IMAGE_TYPE_2D, + .format = VK_FORMAT_D16_UNORM, + .extent = { + .width = chosenSwapExtent.width, + .height = chosenSwapExtent.height, + .depth = 1, + }, + .mipLevels = 1, + .arrayLayers = 1, + .samples = + }; + } } @@ -794,6 +843,7 @@ namespace engine { }; + std::unique_ptr m_debugMessenger; // uses instance std::unique_ptr m_swapchain; @@ -824,7 +874,7 @@ namespace engine { void GFXDevice::draw() { - TRACE("Drawing"); + } }