Started doing some re-arranging trying to brainstorm ideas on multi-pipeline setup.
This commit is contained in:
parent
17050968db
commit
3e2e78a468
231
src/main.cpp
231
src/main.cpp
@ -74,6 +74,7 @@ private:
|
||||
using VulkanBuffer = std::optional<std::pair<vma::UniqueBuffer, vma::UniqueAllocation>>;
|
||||
using VulkanImage = std::optional<std::pair<vma::UniqueImage, vma::UniqueAllocation>>;
|
||||
|
||||
public: // Window/Device setup
|
||||
std::optional<SDL2pp::SDL> libsdl;
|
||||
std::optional<SDL2pp::Window> window;
|
||||
std::optional<vk::raii::Context> vk_ctx;
|
||||
@ -83,36 +84,6 @@ private:
|
||||
std::optional<vk::raii::PhysicalDevice> vk_gfx_card;
|
||||
std::optional<vk::raii::SurfaceKHR> vk_screen_surface;
|
||||
std::optional<vk::raii::Device> vk_gpu;
|
||||
std::optional<vk::raii::Queue> vk_queue_graphics;
|
||||
std::optional<vk::raii::Queue> vk_queue_present;
|
||||
|
||||
private:
|
||||
vma::UniqueAllocator vk_allocator;
|
||||
|
||||
private:
|
||||
std::optional<vk::raii::SwapchainKHR> vk_swapchain;
|
||||
vk::Format vk_swapchain_image_format;
|
||||
vk::Extent2D vk_swapchain_extent;
|
||||
std::vector<vk::raii::ImageView> vk_swapchain_image_views;
|
||||
std::vector<vk::raii::Framebuffer> vk_swapchain_framebuffers;
|
||||
|
||||
std::optional<vk::raii::ShaderModule> vk_shader_vertex;
|
||||
std::optional<vk::raii::ShaderModule> vk_shader_frag;
|
||||
|
||||
std::optional<vk::raii::PipelineLayout> vk_pipeline_layout;
|
||||
std::optional<vk::raii::RenderPass> vk_render_pass;
|
||||
std::optional<vk::raii::Pipeline> vk_pipeline;
|
||||
std::optional<vk::raii::CommandPool> vk_command_pool;
|
||||
|
||||
std::optional<vk::raii::DescriptorSetLayout> vk_descriptor_set_layout_ubo;
|
||||
std::optional<vk::raii::DescriptorPool> vk_descriptor_pool;
|
||||
std::vector<vk::raii::DescriptorSet> vk_descriptor_sets;
|
||||
|
||||
uint vk_current_frame_index = 0;
|
||||
std::vector<vk::raii::CommandBuffer> vk_command_buffers;
|
||||
std::vector<vk::raii::Fence> vk_fences_in_flight;
|
||||
std::vector<vk::raii::Semaphore> vk_semephores_image_available;
|
||||
std::vector<vk::raii::Semaphore> vk_semephores_render_finished;
|
||||
|
||||
struct VulkanDeviceQueriedInfo {
|
||||
std::optional<vk::raii::SurfaceKHR> screen_surface;
|
||||
@ -130,10 +101,43 @@ private:
|
||||
} vk_physical_card_info;
|
||||
|
||||
private:
|
||||
vma::UniqueAllocator vk_allocator;
|
||||
|
||||
private: // Swapchain/Framebuffer
|
||||
std::optional<vk::raii::Queue> vk_queue_graphics;
|
||||
std::optional<vk::raii::Queue> vk_queue_present;
|
||||
std::optional<vk::raii::SwapchainKHR> vk_swapchain;
|
||||
vk::Format vk_swapchain_image_format;
|
||||
vk::Extent2D vk_swapchain_extent;
|
||||
std::vector<vk::raii::ImageView> vk_swapchain_image_views;
|
||||
std::vector<vk::raii::Framebuffer> vk_swapchain_framebuffers;
|
||||
|
||||
uint vk_current_frame_index = 0;
|
||||
std::vector<vk::raii::CommandBuffer> vk_command_buffers;
|
||||
std::vector<vk::raii::Fence> vk_fences_in_flight;
|
||||
std::vector<vk::raii::Semaphore> vk_semephores_image_available;
|
||||
std::vector<vk::raii::Semaphore> vk_semephores_render_finished;
|
||||
|
||||
private:
|
||||
struct VulkanRenderPipeline {
|
||||
std::optional<vk::raii::ShaderModule> vk_shader_vertex;
|
||||
std::optional<vk::raii::ShaderModule> vk_shader_frag;
|
||||
|
||||
std::optional<vk::raii::PipelineLayout> vk_pipeline_layout;
|
||||
std::optional<vk::raii::RenderPass> vk_render_pass;
|
||||
std::optional<vk::raii::Pipeline> vk_pipeline;
|
||||
std::optional<vk::raii::CommandPool> vk_command_pool;
|
||||
|
||||
std::optional<vk::raii::DescriptorSetLayout> vk_descriptor_set_layout_ubo;
|
||||
std::optional<vk::raii::DescriptorPool> vk_descriptor_pool;
|
||||
std::vector<vk::raii::DescriptorSet> 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 Vertex {
|
||||
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<Vertex> quad_vertices = {
|
||||
static inline const std::vector<Vertex3> 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<std::uint16_t> quad_indexes = {
|
||||
|
||||
struct IndexMap {
|
||||
struct SecondDimension {
|
||||
static inline const std::vector<std::uint16_t> triangle = { 0, 1, 2 };
|
||||
static inline const std::vector<std::uint16_t> rectangle = { 0, 1, 2, 2, 3, 0 };
|
||||
};
|
||||
static inline const SecondDimension second;
|
||||
struct ThirdDimension {
|
||||
static inline const std::vector<std::uint16_t> 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<vk::AttachmentDescription, 3> attachment_descriptions = { attachment_description_color, attachment_description_color_resolve, attachment_description_depth };
|
||||
const vk::RenderPassCreateInfo render_pass_create_info {
|
||||
.attachmentCount = static_cast<std::uint32_t>(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<const std::uint32_t*>(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<const std::uint32_t*>(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<vk::AttachmentDescription, 3> attachment_descriptions = { attachment_description_color, attachment_description_color_resolve, attachment_description_depth };
|
||||
const vk::RenderPassCreateInfo render_pass_create_info {
|
||||
.attachmentCount = static_cast<std::uint32_t>(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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user