From 1c8f87dab5f4e94639f3ff7edb13675fa333d18e Mon Sep 17 00:00:00 2001 From: Leonardo Romor Date: Sun, 30 Aug 2020 14:05:12 +0200 Subject: [PATCH 1/4] Moved swapchain dependent objects in separate struct. --- scene.cc | 91 ++++++++++++++++++++++++++++++-------------------------- scene.h | 33 ++++++++++---------- space.cc | 1 + 3 files changed, 67 insertions(+), 58 deletions(-) diff --git a/scene.cc b/scene.cc index c3de31e..778c29c 100644 --- a/scene.cc +++ b/scene.cc @@ -37,36 +37,51 @@ #define FENCE_TIMEOUT 100000000 Scene::Scene(space::core::VkAppContext *vk_ctx) - : vk_ctx_(vk_ctx), r_ctx_(InitRenderingContext()), - current_buffer_(0), + : vk_ctx_(vk_ctx), current_buffer_(0), draw_fence_(vk_ctx->device->createFenceUnique(vk::FenceCreateInfo())), camera_{glm::vec3(0.0f, 0.0f, -15.0f), glm::vec3(0.0f, 2.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)}, projection_matrices_(UpdateProjectionMatrices()) {} -Scene::RenderingContext Scene::InitRenderingContext() { - vk::PhysicalDevice &physical_device = vk_ctx_->physical_device; - space::core::SurfaceData &surface_data = vk_ctx_->surface_data; +void Scene::Init() { vk::UniqueDevice &device = vk_ctx_->device; + const uint32_t graphics_queue_family_index = vk_ctx_->graphics_queue_family_index; + const uint32_t present_queue_family_index = vk_ctx_->present_queue_family_index; + // For multi threaded applications, we should create a command pool // for each thread. For this example, we just need one as we go // with async single core app. + command_pool_ = + space::core::CreateCommandPool(vk_ctx_->device, graphics_queue_family_index); + graphics_queue_ = device->getQueue(graphics_queue_family_index, 0); + present_queue_ = device->getQueue(present_queue_family_index, 0); + + descriptor_set_layout_ = + space::core::CreateDescriptorSetLayout( + device, { {vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex} }); + pipeline_layout_ = + device->createPipelineLayoutUnique( + vk::PipelineLayoutCreateInfo( + vk::PipelineLayoutCreateFlags(), 1, &descriptor_set_layout_.get())); + + pipeline_cache_ = + device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); + + CreateSwapChain(); +} + +void Scene::CreateSwapChain() { + vk::PhysicalDevice &physical_device = vk_ctx_->physical_device; + space::core::SurfaceData &surface_data = vk_ctx_->surface_data; + vk::UniqueDevice &device = vk_ctx_->device; const uint32_t graphics_queue_family_index = vk_ctx_->graphics_queue_family_index; const uint32_t present_queue_family_index = vk_ctx_->present_queue_family_index; - vk::UniqueCommandPool command_pool = - space::core::CreateCommandPool(vk_ctx_->device, graphics_queue_family_index); vk::UniqueCommandBuffer command_buffer = std::move( device->allocateCommandBuffersUnique( vk::CommandBufferAllocateInfo( - command_pool.get(), - vk::CommandBufferLevel::ePrimary, 1)).front()); - - vk::Queue graphics_queue = - device->getQueue(graphics_queue_family_index, 0); - vk::Queue present_queue = - device->getQueue(present_queue_family_index, 0); + *command_pool_, vk::CommandBufferLevel::ePrimary, 1)).front()); space::core::SwapChainData swap_chain_data( physical_device, device, *surface_data.surface, surface_data.extent, @@ -81,14 +96,6 @@ Scene::RenderingContext Scene::InitRenderingContext() { physical_device, device, sizeof(glm::mat4x4), vk::BufferUsageFlagBits::eUniformBuffer); - vk::UniqueDescriptorSetLayout descriptor_set_layout = - space::core::CreateDescriptorSetLayout( - device, { {vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex} }); - vk::UniquePipelineLayout pipeline_layout = - device->createPipelineLayoutUnique( - vk::PipelineLayoutCreateInfo( - vk::PipelineLayoutCreateFlags(), 1, &descriptor_set_layout.get())); - vk::UniqueRenderPass render_pass = space::core::CreateRenderPass( device, space::core::PickSurfaceFormat( @@ -105,7 +112,7 @@ Scene::RenderingContext Scene::InitRenderingContext() { vk::UniqueDescriptorSet descriptor_set = std::move( device->allocateDescriptorSetsUnique( - vk::DescriptorSetAllocateInfo(*descriptor_pool, 1, &*descriptor_set_layout)).front()); + vk::DescriptorSetAllocateInfo(*descriptor_pool, 1, &*descriptor_set_layout_)).front()); space::core::UpdateDescriptorSets( device, descriptor_set, @@ -114,34 +121,32 @@ Scene::RenderingContext Scene::InitRenderingContext() { vk::UniquePipelineCache pipeline_cache = device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); - Scene::RenderingContext r_ctx{ - std::move(command_pool), std::move(command_buffer), graphics_queue, present_queue, - std::move(swap_chain_data), std::move(depth_buffer_data), std::move(uniform_buffer_data), - std::move(descriptor_set_layout), std::move(pipeline_layout), - std::move(render_pass), std::move(framebuffers), std::move(descriptor_pool), - std::move(descriptor_set), std::move(pipeline_cache)}; + struct SwapChainContext *swap_chain_context = new SwapChainContext{ + std::move(command_buffer), std::move(swap_chain_data), std::move(depth_buffer_data), + std::move(uniform_buffer_data), std::move(render_pass), std::move(framebuffers), + std::move(descriptor_pool), std::move(descriptor_set)}; - return r_ctx; + swap_chain_context_.reset(swap_chain_context); } void Scene::AddEntity(space::Entity *entity) { // Initialize entity - entity->Register(vk_ctx_, &r_ctx_.pipeline_layout, &r_ctx_.render_pass, - &r_ctx_.pipeline_cache); + entity->Register(vk_ctx_, &pipeline_layout_, &swap_chain_context_->render_pass, + &pipeline_cache_); entities_.push_back(entity); } void Scene::SubmitRendering() { const vk::UniqueDevice &device = vk_ctx_->device; - const space::core::SwapChainData &swap_chain_data = r_ctx_.swap_chain_data; - const vk::Queue &graphics_queue = r_ctx_.graphics_queue; - const vk::UniqueCommandBuffer &command_buffer = r_ctx_.command_buffer; - const vk::UniqueRenderPass &render_pass = r_ctx_.render_pass; - const std::vector &framebuffers = r_ctx_.framebuffers; - const vk::UniquePipelineLayout &pipeline_layout = r_ctx_.pipeline_layout; - const vk::UniqueDescriptorSet &descriptor_set = r_ctx_.descriptor_set; + const space::core::SwapChainData &swap_chain_data = swap_chain_context_->swap_chain_data; + const vk::Queue &graphics_queue = graphics_queue_; + const vk::UniqueCommandBuffer &command_buffer = swap_chain_context_->command_buffer; + const vk::UniqueRenderPass &render_pass = swap_chain_context_->render_pass; + const std::vector &framebuffers = swap_chain_context_->framebuffers; + const vk::UniquePipelineLayout &pipeline_layout = pipeline_layout_; + const vk::UniqueDescriptorSet &descriptor_set = swap_chain_context_->descriptor_set; const space::core::SurfaceData &surface_data = vk_ctx_->surface_data; - const space::core::BufferData &uniform_buffer_data = r_ctx_.uniform_buffer_data; + const space::core::BufferData &uniform_buffer_data = swap_chain_context_->uniform_buffer_data; // Update the projection matrices with the current values of camera, model, fov, etc.. projection_matrices_ = UpdateProjectionMatrices(); @@ -158,7 +163,7 @@ void Scene::SubmitRendering() { swap_chain_data.swap_chain.get(), FENCE_TIMEOUT, imageAcquiredSemaphore.get(), nullptr); assert(res.result == vk::Result::eSuccess); - assert(res.value < r_ctx_.framebuffers.size()); + assert(res.value < swap_chain_context_->framebuffers.size()); current_buffer_ = res.value; command_buffer->begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlags())); @@ -200,8 +205,8 @@ void Scene::SubmitRendering() { void Scene::Present() { vk::UniqueDevice &device = vk_ctx_->device; - space::core::SwapChainData &swap_chain_data = r_ctx_.swap_chain_data; - vk::Queue &present_queue = r_ctx_.present_queue; + space::core::SwapChainData &swap_chain_data = swap_chain_context_->swap_chain_data; + vk::Queue &present_queue = present_queue_; while (vk::Result::eTimeout == device->waitForFences(draw_fence_.get(), VK_TRUE, FENCE_TIMEOUT)) { usleep(1000); } diff --git a/scene.h b/scene.h index 853fd8b..484d3e3 100644 --- a/scene.h +++ b/scene.h @@ -44,6 +44,7 @@ class Scene { public: Scene(space::core::VkAppContext *context); + void Init(); void AddEntity(space::Entity *entity); void Input(CameraControls &input); void SubmitRendering(); @@ -51,13 +52,21 @@ class Scene { private: space::core::VkAppContext *const vk_ctx_; + vk::UniqueCommandPool command_pool_; + vk::Queue graphics_queue_; + vk::Queue present_queue_; - struct RenderingContext { - vk::UniqueCommandPool command_pool; - vk::UniqueCommandBuffer command_buffer; - vk::Queue graphics_queue; - vk::Queue present_queue; + vk::UniqueDescriptorSetLayout descriptor_set_layout_; + + // The pipeline layout used to describe + // how descriptors should be used. + vk::UniquePipelineLayout pipeline_layout_; + vk::UniquePipelineCache pipeline_cache_; + // Define the set of objects to be recreated + // in case of an out-of-date swapchain. + struct SwapChainContext { + vk::UniqueCommandBuffer command_buffer; space::core::SwapChainData swap_chain_data; // Depth buffer data. Contains the resulting @@ -68,23 +77,17 @@ class Scene { // and other shared buffers. space::core::BufferData uniform_buffer_data; - vk::UniqueDescriptorSetLayout descriptor_set_layout; - - // The pipeline layout used to describe - // how descriptors should be used. - vk::UniquePipelineLayout pipeline_layout; - // We are using a single render pass vk::UniqueRenderPass render_pass; std::vector framebuffers; vk::UniqueDescriptorPool descriptor_pool; vk::UniqueDescriptorSet descriptor_set; - vk::UniquePipelineCache pipeline_cache; }; - // Initialize the rendering context - struct RenderingContext InitRenderingContext(); - RenderingContext r_ctx_; + std::unique_ptr swap_chain_context_; + + // Creates a new swapchain and returns the old one. + void CreateSwapChain(); uint32_t current_buffer_; diff --git a/space.cc b/space.cc index 84545e2..c1a31a0 100644 --- a/space.cc +++ b/space.cc @@ -139,6 +139,7 @@ int main(int argc, char *argv[]) { // the display is closed with XCloseDisplay(). { Scene scene(&vk_ctx); + scene.Init(); ReferenceGrid reference_grid; Curve curve; From 3e39315bf07a0b9e03bd74176b362d36fba861db Mon Sep 17 00:00:00 2001 From: Leonardo Romor Date: Sun, 30 Aug 2020 20:52:50 +0200 Subject: [PATCH 2/4] added swap chain rebuild to enable resize --- scene.cc | 86 +++++++++++++++++++++++++++++---------------- scene.h | 9 +++-- space.cc | 21 +++++++++-- vulkan-core.cc | 29 +++++---------- vulkan-core.h | 19 +++++----- vulkan-rendering.cc | 5 +-- 6 files changed, 104 insertions(+), 65 deletions(-) diff --git a/scene.cc b/scene.cc index 778c29c..779a90a 100644 --- a/scene.cc +++ b/scene.cc @@ -16,7 +16,6 @@ // // This file contains the basic ingredients to render a basic wireframed scene. // This simple scene allows you to add meshes and a freely "movable" camera. - #include #include #include @@ -36,11 +35,11 @@ #define FENCE_TIMEOUT 100000000 -Scene::Scene(space::core::VkAppContext *vk_ctx) - : vk_ctx_(vk_ctx), current_buffer_(0), +Scene::Scene(space::core::VkAppContext *vk_ctx, const QueryExtentCallback &fn) + : vk_ctx_(vk_ctx), QueryExtent(fn), current_buffer_(0), draw_fence_(vk_ctx->device->createFenceUnique(vk::FenceCreateInfo())), camera_{glm::vec3(0.0f, 0.0f, -15.0f), glm::vec3(0.0f, 2.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)}, - projection_matrices_(UpdateProjectionMatrices()) {} + projection_matrices_({0}) {} void Scene::Init() { vk::UniqueDevice &device = vk_ctx_->device; @@ -67,30 +66,35 @@ void Scene::Init() { pipeline_cache_ = device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); - CreateSwapChain(); + CreateSwapChainContext(); } -void Scene::CreateSwapChain() { +void Scene::CreateSwapChainContext() { vk::PhysicalDevice &physical_device = vk_ctx_->physical_device; - space::core::SurfaceData &surface_data = vk_ctx_->surface_data; + vk::UniqueSurfaceKHR &surface = vk_ctx_->surface; vk::UniqueDevice &device = vk_ctx_->device; const uint32_t graphics_queue_family_index = vk_ctx_->graphics_queue_family_index; const uint32_t present_queue_family_index = vk_ctx_->present_queue_family_index; + const vk::Extent2D extent = QueryExtent(); vk::UniqueCommandBuffer command_buffer = std::move( device->allocateCommandBuffersUnique( vk::CommandBufferAllocateInfo( *command_pool_, vk::CommandBufferLevel::ePrimary, 1)).front()); + // Wait device to be idle before destroying everything + if (swap_chain_context_) + device->waitIdle(); + space::core::SwapChainData swap_chain_data( - physical_device, device, *surface_data.surface, surface_data.extent, + physical_device, device, *surface, extent, vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferSrc, - vk::UniqueSwapchainKHR(), graphics_queue_family_index, - present_queue_family_index); + swap_chain_context_ ? std::move(swap_chain_context_->swap_chain_data.swap_chain) : vk::UniqueSwapchainKHR(), + graphics_queue_family_index, present_queue_family_index); space::core::DepthBufferData depth_buffer_data( - physical_device, device, vk::Format::eD16Unorm, surface_data.extent); + physical_device, device, vk::Format::eD16Unorm, swap_chain_data.extent); space::core::BufferData uniform_buffer_data( physical_device, device, sizeof(glm::mat4x4), @@ -100,12 +104,12 @@ void Scene::CreateSwapChain() { space::core::CreateRenderPass( device, space::core::PickSurfaceFormat( physical_device.getSurfaceFormatsKHR( - surface_data.surface.get()))->format, depth_buffer_data.format); + *surface))->format, depth_buffer_data.format); std::vector framebuffers = space::core::CreateFramebuffers( device, render_pass, swap_chain_data.image_views, - depth_buffer_data.image_view, surface_data.extent); + depth_buffer_data.image_view, swap_chain_data.extent); vk::UniqueDescriptorPool descriptor_pool = space::core::CreateDescriptorPool(device, { {vk::DescriptorType::eUniformBuffer, 1} }); @@ -127,6 +131,11 @@ void Scene::CreateSwapChain() { std::move(descriptor_pool), std::move(descriptor_set)}; swap_chain_context_.reset(swap_chain_context); + + for (const auto entity : entities_) { + entity->Register(vk_ctx_, &pipeline_layout_, &swap_chain_context_->render_pass, + &pipeline_cache_); + } } void Scene::AddEntity(space::Entity *entity) { @@ -145,7 +154,6 @@ void Scene::SubmitRendering() { const std::vector &framebuffers = swap_chain_context_->framebuffers; const vk::UniquePipelineLayout &pipeline_layout = pipeline_layout_; const vk::UniqueDescriptorSet &descriptor_set = swap_chain_context_->descriptor_set; - const space::core::SurfaceData &surface_data = vk_ctx_->surface_data; const space::core::BufferData &uniform_buffer_data = swap_chain_context_->uniform_buffer_data; // Update the projection matrices with the current values of camera, model, fov, etc.. @@ -158,13 +166,27 @@ void Scene::SubmitRendering() { // Get the index of the next available swapchain image: vk::UniqueSemaphore imageAcquiredSemaphore = device->createSemaphoreUnique(vk::SemaphoreCreateInfo()); - vk::ResultValue res = - device->acquireNextImageKHR( - swap_chain_data.swap_chain.get(), FENCE_TIMEOUT, - imageAcquiredSemaphore.get(), nullptr); - assert(res.result == vk::Result::eSuccess); - assert(res.value < swap_chain_context_->framebuffers.size()); - current_buffer_ = res.value; + + bool out_of_date = false; + try { + vk::ResultValue res = + device->acquireNextImageKHR( + swap_chain_data.swap_chain.get(), FENCE_TIMEOUT, + imageAcquiredSemaphore.get(), nullptr); + if (res.result == vk::Result::eSuboptimalKHR) + out_of_date = true; + assert(res.value < swap_chain_context_->framebuffers.size()); + current_buffer_ = res.value; + } catch (vk::OutOfDateKHRError &) { + out_of_date = true; + } + if (out_of_date) { + // Re-create the swapchain context. + CreateSwapChainContext(); + // Re-submit rendering + return SubmitRendering(); + } + command_buffer->begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlags())); vk::ClearValue clear_values[2]; @@ -174,7 +196,7 @@ void Scene::SubmitRendering() { vk::ClearDepthStencilValue(1.0f, 0); vk::RenderPassBeginInfo renderPassBeginInfo( render_pass.get(), framebuffers[current_buffer_].get(), - vk::Rect2D(vk::Offset2D(0, 0), surface_data.extent), 2, clear_values); + vk::Rect2D(vk::Offset2D(0, 0), swap_chain_data.extent), 2, clear_values); command_buffer->beginRenderPass( renderPassBeginInfo, vk::SubpassContents::eInline); @@ -185,10 +207,10 @@ void Scene::SubmitRendering() { command_buffer->setViewport( 0, vk::Viewport( 0.0f, 0.0f, - static_cast(surface_data.extent.width), - static_cast(surface_data.extent.height), 0.0f, 1.0f)); + static_cast(swap_chain_data.extent.width), + static_cast(swap_chain_data.extent.height), 0.0f, 1.0f)); command_buffer->setScissor( - 0, vk::Rect2D(vk::Offset2D(0, 0), surface_data.extent)); + 0, vk::Rect2D(vk::Offset2D(0, 0), swap_chain_data.extent)); for (const auto entity : entities_) { entity->Draw(&command_buffer); @@ -210,14 +232,18 @@ void Scene::Present() { while (vk::Result::eTimeout == device->waitForFences(draw_fence_.get(), VK_TRUE, FENCE_TIMEOUT)) { usleep(1000); } - present_queue.presentKHR( - vk::PresentInfoKHR(0, nullptr, 1, &swap_chain_data.swap_chain.get(), ¤t_buffer_)); -} + try { + present_queue.presentKHR( + vk::PresentInfoKHR(0, nullptr, 1, &swap_chain_data.swap_chain.get(), ¤t_buffer_)); + } catch (vk::OutOfDateKHRError &) { + // Re-create the swapchain context. + CreateSwapChainContext(); + } +} struct Scene::Projection Scene::UpdateProjectionMatrices() { - - vk::Extent2D &extent = vk_ctx_->surface_data.extent; + vk::Extent2D extent = swap_chain_context_->swap_chain_data.extent; float fov = glm::radians(60.0f); const auto aspect_ratio = diff --git a/scene.h b/scene.h index 484d3e3..52806d2 100644 --- a/scene.h +++ b/scene.h @@ -42,7 +42,10 @@ struct CameraControls { // perform rendering of the entities. class Scene { public: - Scene(space::core::VkAppContext *context); + + typedef std::function QueryExtentCallback; + + Scene(space::core::VkAppContext *context, const QueryExtentCallback &fn); void Init(); void AddEntity(space::Entity *entity); @@ -52,6 +55,8 @@ class Scene { private: space::core::VkAppContext *const vk_ctx_; + const QueryExtentCallback QueryExtent; + vk::UniqueCommandPool command_pool_; vk::Queue graphics_queue_; vk::Queue present_queue_; @@ -87,7 +92,7 @@ class Scene { std::unique_ptr swap_chain_context_; // Creates a new swapchain and returns the old one. - void CreateSwapChain(); + void CreateSwapChainContext(); uint32_t current_buffer_; diff --git a/space.cc b/space.cc index c1a31a0..5a046d8 100644 --- a/space.cc +++ b/space.cc @@ -13,6 +13,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see +#include #include #include #include @@ -28,6 +29,13 @@ #include "gamepad.h" #include "curve.h" +std::optional get_xlib_window_extent(Display *display, Window window) { + XWindowAttributes attrs; + if (XGetWindowAttributes(display, window, &attrs)) { + return vk::Extent2D(attrs.width, attrs.height); + } + return {}; +} static void gamepad2camera( CameraControls *camera_controls, const struct EventData &data) { @@ -138,7 +146,12 @@ int main(int argc, char *argv[]) { // as we want to avoid its destruction after // the display is closed with XCloseDisplay(). { - Scene scene(&vk_ctx); + Scene scene(&vk_ctx, [display, window] () { + XWindowAttributes attrs; + XGetWindowAttributes(display, window, &attrs); + return vk::Extent2D(attrs.width, attrs.height); + }); + scene.Init(); ReferenceGrid reference_grid; Curve curve; @@ -148,6 +161,7 @@ int main(int argc, char *argv[]) { XSelectInput(display, window, ExposureMask | KeyPressMask + | StructureNotifyMask | PointerMotionMask); XMapWindow(display, window); XFlush(display); @@ -189,8 +203,11 @@ int main(int argc, char *argv[]) { if (FD_ISSET(x11_fd, &read_fds)) { while(XPending(display)) { XNextEvent(display, &e); - if (e.type == KeyPress) { + + switch (e.type) { + case KeyPress: exit = true; + break; } } } diff --git a/vulkan-core.cc b/vulkan-core.cc index 631ecb1..9fc248e 100644 --- a/vulkan-core.cc +++ b/vulkan-core.cc @@ -74,14 +74,6 @@ std::optional> FindGraphicsAndPresentQueueFamilyIn return {}; } -std::optional get_xlib_window_extent(Display *display, Window window) { - XWindowAttributes attrs; - if (XGetWindowAttributes(display, window, &attrs)) { - return vk::Extent2D(attrs.width, attrs.height); - } - return {}; -} - vk::UniqueDevice CreateDevice( vk::PhysicalDevice physical_device, uint32_t queue_family_index, std::vector const& extensions = {}, @@ -321,23 +313,15 @@ namespace space { // possibly configurable policy. vk::PhysicalDevice physical_device = instance->enumeratePhysicalDevices().front(); - vk::Extent2D extent; - if (auto res = get_xlib_window_extent(display, window)) { - extent = res.value(); - } else { - fprintf(stderr, "Coudldn't guess the xlib window extent."); - return {}; - } // Init the surface - struct SurfaceData surface_data = { + vk::UniqueSurfaceKHR surface = instance->createXlibSurfaceKHRUnique( - vk::XlibSurfaceCreateInfoKHR(vk::XlibSurfaceCreateFlagsKHR(), display, window)), - extent}; + vk::XlibSurfaceCreateInfoKHR(vk::XlibSurfaceCreateFlagsKHR(), display, window)); // Find devices for present and graphics. std::pair graphics_and_present_queue_family_index; if (const auto o = FindGraphicsAndPresentQueueFamilyIndex( - physical_device, *surface_data.surface)) { + physical_device, *surface)) { graphics_and_present_queue_family_index = o.value(); } else { fprintf(stderr, "Couldn't find suitable Present or Graphics queues."); @@ -356,9 +340,12 @@ namespace space { VULKAN_HPP_DEFAULT_DISPATCHER.init(*device); return VkAppContext{ - std::move(dl), std::move(instance), std::move(device), + std::move(dl), + std::move(instance), + std::move(surface), + std::move(device), std::move(debug_utils_messenger), - physical_device, std::move(surface_data), + physical_device, graphics_and_present_queue_family_index.first, graphics_and_present_queue_family_index.second}; } diff --git a/vulkan-core.h b/vulkan-core.h index b1ab915..273eb5c 100644 --- a/vulkan-core.h +++ b/vulkan-core.h @@ -14,7 +14,7 @@ // limitations under the License. // Modifications copyright (C) 2020 Leonardo Romor // -// This file contains the represents an intermediate interface simplify vulkan. +// This file contains the represents an intermediate interface to simplify vulkan. #ifndef __SPACE_CORE_H_ #define __SPACE_CORE_H_ @@ -47,12 +47,9 @@ VULKAN_HPP_INLINE TargetType checked_cast(SourceType value) { namespace space { namespace core { - // Vulkan initialization routines - struct SurfaceData { - vk::UniqueSurfaceKHR surface; - vk::Extent2D extent; - }; - + // Hold the Vulkan configuration data + // such as application name, engine, + // and requested instance layers and extensions. struct VkAppConfig { const char *app_name; const char *engine_name; @@ -60,13 +57,18 @@ namespace space { std::vector instance_extensions; }; + // Holds the vulkan datacstructures + // used to represent the vulkan implementation, + // instantiation and configuration. It does not + // include any rendering related vullkan calls or + // data structures. struct VkAppContext { vk::DynamicLoader dynamic_loader; vk::UniqueInstance instance; + vk::UniqueSurfaceKHR surface; vk::UniqueDevice device; vk::UniqueDebugUtilsMessengerEXT debug_utils_messenger; vk::PhysicalDevice physical_device; - SurfaceData surface_data; uint32_t graphics_queue_family_index; uint32_t present_queue_family_index; }; @@ -86,6 +88,7 @@ namespace space { uint32_t graphics_family_index, uint32_t present_family_index); vk::Format color_format; + vk::Extent2D extent; vk::UniqueSwapchainKHR swap_chain; std::vector images; std::vector image_views; diff --git a/vulkan-rendering.cc b/vulkan-rendering.cc index 015bc1f..3c5e745 100644 --- a/vulkan-rendering.cc +++ b/vulkan-rendering.cc @@ -19,6 +19,7 @@ // found here. #include +#include #include "vulkan-core.h" @@ -82,8 +83,7 @@ namespace space { vk::SurfaceKHR const& surface, vk::Extent2D const& extent, vk::ImageUsageFlags usage, vk::UniqueSwapchainKHR const& old_swap_chain, uint32_t graphics_queue_family_index, uint32_t present_queue_family_index) { - - vk::SurfaceFormatKHR surface_format =PickSurfaceFormat( + vk::SurfaceFormatKHR surface_format = ::PickSurfaceFormat( physical_device.getSurfaceFormatsKHR(surface)).value(); color_format = surface_format.format; @@ -137,6 +137,7 @@ namespace space { swapChainCreateInfo.pQueueFamilyIndices = queueFamilyIndices; } swap_chain = device->createSwapchainKHRUnique(swapChainCreateInfo); + this->extent = swap_chain_extent; images = device->getSwapchainImagesKHR(swap_chain.get()); From 87298315979c8d66c769558c610cbc631f63e30b Mon Sep 17 00:00:00 2001 From: Leonardo Romor Date: Wed, 2 Sep 2020 17:11:53 +0200 Subject: [PATCH 3/4] Removed unused pipelien_cache creation --- scene.cc | 3 --- 1 file changed, 3 deletions(-) diff --git a/scene.cc b/scene.cc index 779a90a..8fb990a 100644 --- a/scene.cc +++ b/scene.cc @@ -122,9 +122,6 @@ void Scene::CreateSwapChainContext() { device, descriptor_set, {{vk::DescriptorType::eUniformBuffer, uniform_buffer_data.buffer, vk::UniqueBufferView()}}); - vk::UniquePipelineCache pipeline_cache = - device->createPipelineCacheUnique(vk::PipelineCacheCreateInfo()); - struct SwapChainContext *swap_chain_context = new SwapChainContext{ std::move(command_buffer), std::move(swap_chain_data), std::move(depth_buffer_data), std::move(uniform_buffer_data), std::move(render_pass), std::move(framebuffers), From ac6f4708c61cd7b178b57ceac9881ab30eed4fda Mon Sep 17 00:00:00 2001 From: Leonardo Romor Date: Wed, 2 Sep 2020 17:18:00 +0200 Subject: [PATCH 4/4] Resetting the points vector for the curve --- curve.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/curve.cc b/curve.cc index 55d2c14..baddc5f 100644 --- a/curve.cc +++ b/curve.cc @@ -146,6 +146,8 @@ void Curve::Register( vk::UniqueRenderPass *render_pass, vk::UniquePipelineCache *pipeline_cache) { + points_.clear(); + // Instantiate the shaders vk::UniqueShaderModule vertex = context->device->createShaderModuleUnique(