From 3e2e78a468d923d3fe84f3e30b97df85525638b1 Mon Sep 17 00:00:00 2001 From: Rebekah Rowe Date: Sat, 27 Jul 2024 10:59:40 -0400 Subject: [PATCH] Started doing some re-arranging trying to brainstorm ideas on multi-pipeline setup. --- src/main.cpp | 239 +++++++++++++++++++++++++++------------------------ 1 file changed, 128 insertions(+), 111 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8d6daf9..122e8b3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,6 +74,7 @@ private: using VulkanBuffer = std::optional>; using VulkanImage = std::optional>; +public: // Window/Device setup std::optional libsdl; std::optional window; std::optional vk_ctx; @@ -83,36 +84,6 @@ private: std::optional vk_gfx_card; std::optional vk_screen_surface; std::optional vk_gpu; - std::optional vk_queue_graphics; - std::optional vk_queue_present; - -private: - vma::UniqueAllocator vk_allocator; - -private: - std::optional vk_swapchain; - vk::Format vk_swapchain_image_format; - vk::Extent2D vk_swapchain_extent; - std::vector vk_swapchain_image_views; - std::vector vk_swapchain_framebuffers; - - std::optional vk_shader_vertex; - std::optional vk_shader_frag; - - std::optional vk_pipeline_layout; - std::optional vk_render_pass; - std::optional vk_pipeline; - std::optional vk_command_pool; - - std::optional vk_descriptor_set_layout_ubo; - std::optional vk_descriptor_pool; - std::vector vk_descriptor_sets; - - uint vk_current_frame_index = 0; - std::vector vk_command_buffers; - std::vector vk_fences_in_flight; - std::vector vk_semephores_image_available; - std::vector vk_semephores_render_finished; struct VulkanDeviceQueriedInfo { std::optional screen_surface; @@ -130,10 +101,43 @@ private: } vk_physical_card_info; private: - VulkanBuffer vk_buffer_vertex; // in order to get a clean destruction sequence, instantiate the DeviceMemory for the vertex buffer first // https://github.com/KhronosGroup/Vulkan-Hpp/blob/6f72ceca515d59f40d64b64cf2734f6261e1f9f2/RAII_Samples/13_InitVertexBuffer/13_InitVertexBuffer.cpp - VulkanBuffer vk_buffer_index; // used to make rendering models more efficent by sharing values + vma::UniqueAllocator vk_allocator; - struct Vertex { +private: // Swapchain/Framebuffer + std::optional vk_queue_graphics; + std::optional vk_queue_present; + std::optional vk_swapchain; + vk::Format vk_swapchain_image_format; + vk::Extent2D vk_swapchain_extent; + std::vector vk_swapchain_image_views; + std::vector vk_swapchain_framebuffers; + + uint vk_current_frame_index = 0; + std::vector vk_command_buffers; + std::vector vk_fences_in_flight; + std::vector vk_semephores_image_available; + std::vector vk_semephores_render_finished; + +private: + struct VulkanRenderPipeline { + std::optional vk_shader_vertex; + std::optional vk_shader_frag; + + std::optional vk_pipeline_layout; + std::optional vk_render_pass; + std::optional vk_pipeline; + std::optional vk_command_pool; + + std::optional vk_descriptor_set_layout_ubo; + std::optional vk_descriptor_pool; + std::vector vk_descriptor_sets; + + VulkanBuffer vk_buffer_vertex; // in order to get a clean destruction sequence, instantiate the DeviceMemory for the vertex buffer first // https://github.com/KhronosGroup/Vulkan-Hpp/blob/6f72ceca515d59f40d64b64cf2734f6261e1f9f2/RAII_Samples/13_InitVertexBuffer/13_InitVertexBuffer.cpp + VulkanBuffer vk_buffer_index; // used to make rendering models more efficent by sharing values + }; + VulkanRenderPipeline vk_pipeline_third; + + struct Vertex3 { glm::vec3 pos; glm::vec3 color; glm::vec2 texture_coordinates; @@ -141,7 +145,7 @@ private: static constexpr vk::VertexInputBindingDescription GetBindingDescription() { constexpr vk::VertexInputBindingDescription binding_description { .binding = 0, - .stride = sizeof(Vertex), + .stride = sizeof(Vertex3), .inputRate = vk::VertexInputRate::eVertex }; return binding_description; @@ -152,23 +156,23 @@ private: .location = 0, .binding = 0, .format = vk::Format::eR32G32B32Sfloat, - .offset = offsetof(Vertex, pos) }, + .offset = offsetof(Vertex3, pos) }, vk::VertexInputAttributeDescription { .location = 2, .binding = 0, .format = vk::Format::eR32G32B32Sfloat, - .offset = offsetof(Vertex, color) }, + .offset = offsetof(Vertex3, color) }, vk::VertexInputAttributeDescription { .location = 4, .binding = 0, .format = vk::Format::eR32G32Sfloat, - .offset = offsetof(Vertex, texture_coordinates) } + .offset = offsetof(Vertex3, texture_coordinates) } }; return attribute_descriptions; } }; - static inline const std::vector quad_vertices = { + static inline const std::vector vertices_quad_sample = { { { -0.5f, -0.5f, 0.0f }, { 1.0f, 0.0f, 0.0f }, { 0.0f, 0.0f } }, { { 0.5f, -0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 1.0f, 0.0f } }, { { 0.5f, 0.5f, 0.0f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f } }, @@ -179,14 +183,26 @@ private: { { 0.5f, 0.5f, -0.5f }, { 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f } }, { { -0.5f, 0.5f, -0.5f }, { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f } } }; - static inline const std::vector quad_indexes = { - 0, 1, 2, 2, 3, 0, // Wraps a quad-box's 2 triangles around itself. // https://docs.vulkan.org/tutorial/latest/04_Vertex_buffers/03_Index_buffer.html - 4, 5, 6, 6, 7, 4 + + struct IndexMap { + struct SecondDimension { + static inline const std::vector triangle = { 0, 1, 2 }; + static inline const std::vector rectangle = { 0, 1, 2, 2, 3, 0 }; + }; + static inline const SecondDimension second; + struct ThirdDimension { + static inline const std::vector rectangle = { + 0, 1, 2, 2, 3, 0, // Wraps a quad-box's 2 triangles around itself. // https://docs.vulkan.org/tutorial/latest/04_Vertex_buffers/03_Index_buffer.html + 4, 5, 6, 6, 7, 4 + }; + }; + static inline const ThirdDimension third; }; + static inline const IndexMap index_map; private: // https://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/ - struct UniformBufferObject { // https://docs.vulkan.org/tutorial/latest/05_Uniform_buffers/01_Descriptor_pool_and_sets.html#_alignment_requirements + struct UniformBufferObject_Projection { // https://docs.vulkan.org/tutorial/latest/05_Uniform_buffers/01_Descriptor_pool_and_sets.html#_alignment_requirements alignas(16) glm::mat4 model; alignas(16) glm::mat4 view; alignas(16) glm::mat4 proj; @@ -535,6 +551,76 @@ public: { // find depth format, needed before a few things this->CreateBufferDepth(); } + { // init renderpass + const vk::AttachmentDescription attachment_description_color { + .format = this->vk_swapchain_image_format, + .samples = this->vk_msaa_samples, + .loadOp = vk::AttachmentLoadOp::eClear, + .storeOp = vk::AttachmentStoreOp::eStore, + .stencilLoadOp = vk::AttachmentLoadOp::eDontCare, + .stencilStoreOp = vk::AttachmentStoreOp::eDontCare, + .initialLayout = vk::ImageLayout::eUndefined, + .finalLayout = vk::ImageLayout::eColorAttachmentOptimal + }; + const vk::AttachmentDescription attachment_description_color_resolve { + .format = this->vk_swapchain_image_format, + .samples = vk::SampleCountFlagBits::e1, + .loadOp = vk::AttachmentLoadOp::eDontCare, + .storeOp = vk::AttachmentStoreOp::eStore, + .stencilLoadOp = vk::AttachmentLoadOp::eDontCare, + .stencilStoreOp = vk::AttachmentStoreOp::eDontCare, + .initialLayout = vk::ImageLayout::eUndefined, + .finalLayout = vk::ImageLayout::ePresentSrcKHR + }; + const vk::AttachmentDescription attachment_description_depth { + .format = this->vk_depth_format, + .samples = this->vk_msaa_samples, + .loadOp = vk::AttachmentLoadOp::eClear, + .storeOp = vk::AttachmentStoreOp::eDontCare, + .stencilLoadOp = vk::AttachmentLoadOp::eDontCare, + .stencilStoreOp = vk::AttachmentStoreOp::eDontCare, + .initialLayout = vk::ImageLayout::eUndefined, + .finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal + }; + constexpr vk::AttachmentReference attachment_ref_color { + .attachment = 0, + .layout = vk::ImageLayout::eColorAttachmentOptimal, + }; + constexpr vk::AttachmentReference attachment_ref_color_resolve { + .attachment = 1, + .layout = vk::ImageLayout::eColorAttachmentOptimal + }; + constexpr vk::AttachmentReference attachment_ref_depth { + .attachment = 2, + .layout = vk::ImageLayout::eDepthStencilAttachmentOptimal + }; + + const vk::SubpassDescription subpass_description { + .pipelineBindPoint = vk::PipelineBindPoint::eGraphics, + .colorAttachmentCount = 1, + .pColorAttachments = &attachment_ref_color, + .pResolveAttachments = &attachment_ref_color_resolve, + .pDepthStencilAttachment = &attachment_ref_depth + }; + constexpr vk::SubpassDependency subpass_dependency { + .srcSubpass = vk::SubpassExternal, + .dstSubpass = 0, + .srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eLateFragmentTests, + .dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eEarlyFragmentTests, + .srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite | vk::AccessFlagBits::eDepthStencilAttachmentWrite, + .dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite | vk::AccessFlagBits::eDepthStencilAttachmentWrite + }; + const std::array attachment_descriptions = { attachment_description_color, attachment_description_color_resolve, attachment_description_depth }; + const vk::RenderPassCreateInfo render_pass_create_info { + .attachmentCount = static_cast(attachment_descriptions.size()), + .pAttachments = attachment_descriptions.data(), + .subpassCount = 1, + .pSubpasses = &subpass_description, + .dependencyCount = 1, + .pDependencies = &subpass_dependency + }; + this->vk_render_pass.emplace(*this->vk_gpu, render_pass_create_info); + } { // Load Shaders this->vk_shader_vertex.emplace(*this->vk_gpu, vk::ShaderModuleCreateInfo { .codeSize = embeded_shader_vertex_glsl_spv.size, .pCode = reinterpret_cast(embeded_shader_vertex_glsl_spv.begin) }); this->vk_shader_frag.emplace(*this->vk_gpu, vk::ShaderModuleCreateInfo { .codeSize = embeded_shader_frag_glsl_spv.size, .pCode = reinterpret_cast(embeded_shader_frag_glsl_spv.begin) }); @@ -662,75 +748,6 @@ public: }; this->vk_pipeline_layout.emplace(*this->vk_gpu, pipeline_layout_create_info); - const vk::AttachmentDescription attachment_description_color { - .format = this->vk_swapchain_image_format, - .samples = this->vk_msaa_samples, - .loadOp = vk::AttachmentLoadOp::eClear, - .storeOp = vk::AttachmentStoreOp::eStore, - .stencilLoadOp = vk::AttachmentLoadOp::eDontCare, - .stencilStoreOp = vk::AttachmentStoreOp::eDontCare, - .initialLayout = vk::ImageLayout::eUndefined, - .finalLayout = vk::ImageLayout::eColorAttachmentOptimal - }; - const vk::AttachmentDescription attachment_description_color_resolve { - .format = this->vk_swapchain_image_format, - .samples = vk::SampleCountFlagBits::e1, - .loadOp = vk::AttachmentLoadOp::eDontCare, - .storeOp = vk::AttachmentStoreOp::eStore, - .stencilLoadOp = vk::AttachmentLoadOp::eDontCare, - .stencilStoreOp = vk::AttachmentStoreOp::eDontCare, - .initialLayout = vk::ImageLayout::eUndefined, - .finalLayout = vk::ImageLayout::ePresentSrcKHR - }; - const vk::AttachmentDescription attachment_description_depth { - .format = this->vk_depth_format, - .samples = this->vk_msaa_samples, - .loadOp = vk::AttachmentLoadOp::eClear, - .storeOp = vk::AttachmentStoreOp::eDontCare, - .stencilLoadOp = vk::AttachmentLoadOp::eDontCare, - .stencilStoreOp = vk::AttachmentStoreOp::eDontCare, - .initialLayout = vk::ImageLayout::eUndefined, - .finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal - }; - constexpr vk::AttachmentReference attachment_ref_color { - .attachment = 0, - .layout = vk::ImageLayout::eColorAttachmentOptimal, - }; - constexpr vk::AttachmentReference attachment_ref_color_resolve { - .attachment = 1, - .layout = vk::ImageLayout::eColorAttachmentOptimal - }; - constexpr vk::AttachmentReference attachment_ref_depth { - .attachment = 2, - .layout = vk::ImageLayout::eDepthStencilAttachmentOptimal - }; - - const vk::SubpassDescription subpass_description { - .pipelineBindPoint = vk::PipelineBindPoint::eGraphics, - .colorAttachmentCount = 1, - .pColorAttachments = &attachment_ref_color, - .pResolveAttachments = &attachment_ref_color_resolve, - .pDepthStencilAttachment = &attachment_ref_depth - }; - constexpr vk::SubpassDependency subpass_dependency { - .srcSubpass = vk::SubpassExternal, - .dstSubpass = 0, - .srcStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eLateFragmentTests, - .dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput | vk::PipelineStageFlagBits::eEarlyFragmentTests, - .srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite | vk::AccessFlagBits::eDepthStencilAttachmentWrite, - .dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite | vk::AccessFlagBits::eDepthStencilAttachmentWrite - }; - const std::array attachment_descriptions = { attachment_description_color, attachment_description_color_resolve, attachment_description_depth }; - const vk::RenderPassCreateInfo render_pass_create_info { - .attachmentCount = static_cast(attachment_descriptions.size()), - .pAttachments = attachment_descriptions.data(), - .subpassCount = 1, - .pSubpasses = &subpass_description, - .dependencyCount = 1, - .pDependencies = &subpass_dependency - }; - this->vk_render_pass.emplace(*this->vk_gpu, render_pass_create_info); - const vk::GraphicsPipelineCreateInfo pipeline_create_info { .stageCount = 2, .pStages = shader_stages_create_info,