From 8832357258936b4473694992afd8f9c5ca41d2e6 Mon Sep 17 00:00:00 2001 From: Johannes Schneider Date: Sat, 13 Jul 2024 00:55:59 +0200 Subject: [PATCH] [WIP] Work in progress --- .../inexor/vulkan-renderer/application.hpp | 4 +- .../render-graph/pipeline_builder.hpp | 243 -------------- .../render-graph/render_graph.hpp | 19 -- .../vulkan-renderer/renderers/imgui.hpp | 4 +- .../wrapper/pipelines/pipeline_builder.hpp | 8 +- .../{render-graph => wrapper}/shader.hpp | 6 +- src/CMakeLists.txt | 2 +- src/vulkan-renderer/application.cpp | 37 ++- .../render-graph/pipeline_builder.cpp | 300 ------------------ .../render-graph/render_graph.cpp | 14 +- src/vulkan-renderer/renderers/imgui.cpp | 11 +- .../{render-graph => wrapper}/shader.cpp | 6 +- 12 files changed, 39 insertions(+), 615 deletions(-) delete mode 100644 include/inexor/vulkan-renderer/render-graph/pipeline_builder.hpp rename include/inexor/vulkan-renderer/{render-graph => wrapper}/shader.hpp (91%) delete mode 100644 src/vulkan-renderer/render-graph/pipeline_builder.cpp rename src/vulkan-renderer/{render-graph => wrapper}/shader.cpp (94%) diff --git a/include/inexor/vulkan-renderer/application.hpp b/include/inexor/vulkan-renderer/application.hpp index dd3ded8cb..db967a206 100644 --- a/include/inexor/vulkan-renderer/application.hpp +++ b/include/inexor/vulkan-renderer/application.hpp @@ -50,8 +50,8 @@ class Application { std::shared_ptr m_index_buffer; std::shared_ptr m_vertex_buffer; std::shared_ptr m_uniform_buffer; - std::shared_ptr m_octree_vert; - std::shared_ptr m_octree_frag; + std::shared_ptr m_octree_vert; + std::shared_ptr m_octree_frag; VkDescriptorSetLayout m_descriptor_set_layout{VK_NULL_HANDLE}; VkDescriptorSet m_descriptor_set{VK_NULL_HANDLE}; diff --git a/include/inexor/vulkan-renderer/render-graph/pipeline_builder.hpp b/include/inexor/vulkan-renderer/render-graph/pipeline_builder.hpp deleted file mode 100644 index 615d9fd51..000000000 --- a/include/inexor/vulkan-renderer/render-graph/pipeline_builder.hpp +++ /dev/null @@ -1,243 +0,0 @@ -#pragma once - -#include - -#include "inexor/vulkan-renderer/wrapper/make_info.hpp" - -#include -#include -#include - -namespace inexor::vulkan_renderer::wrapper { - -/// Builder class for VkPipelineCreateInfo -/// @note that all members are initialized in the method ``reset()`` (This method is also called by the constructor) -/// Calling ``reset()`` allows you to re-use this builder for the next graphics pipeline you want to build, so you don't -/// need to create one builder per graphics pipeline you build -class GraphicsPipelineBuilder { -private: - std::vector m_vertex_input_binding_descriptions; - std::vector m_vertex_input_attribute_descriptions; - - // With the builder we can fill vertex binding descriptions and vertex attribute descriptions in here - VkPipelineVertexInputStateCreateInfo m_vertex_input_sci; - - // With the builder we can set topology in here - VkPipelineInputAssemblyStateCreateInfo m_input_assembly_sci; - - // With the builder we can set the patch control point count in here - VkPipelineTessellationStateCreateInfo m_tesselation_sci; - - std::vector m_viewports; - std::vector m_scissors; - - // With the builder we can set viewport(s) and scissor(s) in here - VkPipelineViewportStateCreateInfo m_viewport_sci; - - // With the builder we can set polygon mode, cull mode, front face, and line width - // TODO: Implement methods to enable depth bias and for setting the depth bias parameters - VkPipelineRasterizationStateCreateInfo m_rasterization_sci; - - // With the builder we can set rasterization samples and min sample shading - // TODO: Expose more multisampling parameters if desired - VkPipelineMultisampleStateCreateInfo m_multisample_sci; - - // With the builder we can't set individial fields of this struct, - // since it's easier to specify an entire VkPipelineDepthStencilStateCreateInfo struct to the builder instead - VkPipelineDepthStencilStateCreateInfo m_depth_stencil_sci; - - // With the builder we can't set individial fields of this struct, - // since it's easier to specify an entire VkPipelineColorBlendStateCreateInfo struct to the builder instead - VkPipelineColorBlendStateCreateInfo m_color_blend_sci; - - std::vector m_dynamic_states; - - // This will be filled in the build() method - // With the builder we can specify a std::vector - VkPipelineDynamicStateCreateInfo m_dynamic_states_sci; - - VkPipelineLayout m_pipeline_layout{VK_NULL_HANDLE}; - VkRenderPass m_render_pass{VK_NULL_HANDLE}; - - // With the builder we can either call add_shader or set_shaders - std::vector m_shader_stages; - - // With the builder we can either call add_color_blend_attachment or set_color_blend_attachments - std::vector m_color_blend_attachment_states; - - /// Default constructor is private, so only rendergraph can access it - /// @param device The device wrapper - explicit GraphicsPipelineBuilder(); - -public: - GraphicsPipelineBuilder(const GraphicsPipelineBuilder &) = delete; - GraphicsPipelineBuilder(GraphicsPipelineBuilder &&other) noexcept; - ~GraphicsPipelineBuilder() = default; - - GraphicsPipelineBuilder &operator=(const GraphicsPipelineBuilder &) = delete; - GraphicsPipelineBuilder &operator=(GraphicsPipelineBuilder &&) = delete; - - /// Add a shader stage - /// @param shader The shader stage to add - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &add_shader(const VkPipelineShaderStageCreateInfo &shader); - - /// Add a vertex input attribute description - /// @param description The vertex input attribute description - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder & - add_vertex_input_attribute(const VkVertexInputAttributeDescription &description); - - /// Add a vertex input binding description - /// @param description The vertex input binding descriptions - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &add_vertex_input_binding(const VkVertexInputBindingDescription &description); - - /// Add a color blend attachment - /// @param attachment The color blend attachment - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder & - add_color_blend_attachment(const VkPipelineColorBlendAttachmentState &attachment); - - /// Build the graphics pipeline with specified pipeline create flags - /// @return The graphics pipeline create info that was filled by the builder - [[nodiscard]] void build(); - - /// Reset all data in this class so the builder can be re-used - /// @note This is called by the constructor - void reset(); - - /// Set the color blend manually - /// @param color_blend The color blend - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_color_blend(const VkPipelineColorBlendStateCreateInfo &color_blend); - - /// Set all color blend attachments manually - /// @note You should prefer to use ``add_color_blend_attachment`` instead - /// @param attachments The color blend attachments - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder & - set_color_blend_attachments(const std::vector &attachments); - - /// Enable or disable culling - /// @warning Disabling culling will have a significant performance impact - /// @param culling_enabled ``true`` if culling is enabled - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_culling_mode(VkBool32 culling_enabled); - - /// Set the depth stencil - /// @param depth_stencil The depth stencil - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder & - set_depth_stencil(const VkPipelineDepthStencilStateCreateInfo &depth_stencil); - - /// Set the dynamic states - /// @param dynamic_states The dynamic states - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_dynamic_states(const std::vector &dynamic_states); - - /// Set the input assembly state create info - /// @note If you just want to set the triangle topology, call ``set_triangle_topology`` instead, because this is the - /// most powerful method of this method in case you really need to overwrite it - /// @param input_assembly The pipeline input state create info - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder & - set_input_assembly(const VkPipelineInputAssemblyStateCreateInfo &input_assembly); - - /// Set the line width of rasterization - /// @param line_width The line width of rasterization - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_line_width(float width); - - /// Set the most important MSAA settings - /// @param sample_count The number of samples used in rasterization - /// @param min_sample_shading A minimum fraction of sample shading - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_multisampling(VkSampleCountFlagBits sample_count, - float min_sample_shading); - - /// Store the pipeline layout - /// @param layout The pipeline layout - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_pipeline_layout(VkPipelineLayout layout); - - /// Set the triangle topology - /// @param topology the primitive topology - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_primitive_topology(VkPrimitiveTopology topology); - - /// Set the rasterization state of the graphics pipeline manually - /// @param rasterization The rasterization state - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder & - set_rasterization(const VkPipelineRasterizationStateCreateInfo &rasterization); - - /// Set the render pass - /// @param render_pass The render pass - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_render_pass(VkRenderPass render_pass); - - /// Set the scissor data in VkPipelineViewportStateCreateInfo - /// There is another method called set_scissors in case multiple scissors will be used - /// @param scissors The scissors in in VkPipelineViewportStateCreateInfo - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_scissor(const VkRect2D &scissor); - - /// Set the scissor data in VkPipelineViewportStateCreateInfo (convert VkExtent2D to VkRect2D) - /// @param extent The extent of the scissor - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_scissor(const VkExtent2D &extent); - - /// Set the viewport data in VkPipelineViewportStateCreateInfo - /// There is another method called set_scissors in case multiple scissors will be used - /// @param scissor The scissor in in VkPipelineViewportStateCreateInfo - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_scissors(const std::vector &scissors); - - /// Set the shader stage - /// @param shader_stages The shader stages - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_shaders(const std::vector &shaders); - - /// Set the tesselation control point count - /// @param control_point_count The tesselation control point count - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_tesselation_control_point_count(std::uint32_t control_point_count); - - /// Set the vertex input attribute descriptions manually - /// You should prefer to use ``add_vertex_input_attribute`` instead - /// @param descriptions The vertex input attribute descriptions - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder & - set_vertex_input_attributes(const std::vector &descriptions); - - /// Set the vertex input binding descriptions manually - /// You should prefer to use ``add_vertex_input_binding`` instead - /// @param descriptions The vertex input binding descriptions - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder & - set_vertex_input_bindings(const std::vector &descriptions); - - /// Set the viewport in VkPipelineViewportStateCreateInfo - /// There is another method called set_viewports in case multiple viewports will be used - /// @param viewport The viewport in VkPipelineViewportStateCreateInfo - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_viewport(const VkViewport &viewport); - - /// Set the viewport in VkPipelineViewportStateCreateInfo (convert VkExtent2D to VkViewport) - /// @param extent The extent of the viewport - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_viewport(const VkExtent2D &extent); - - /// Set the viewport in VkPipelineViewportStateCreateInfo - /// @param viewports The viewports in VkPipelineViewportStateCreateInfo - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_viewports(const std::vector &viewports); - - /// Set the wireframe mode - /// @param wireframe ``true`` if wireframe is enabled - /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] GraphicsPipelineBuilder &set_wireframe(VkBool32 wireframe); -}; - -} // namespace inexor::vulkan_renderer::wrapper diff --git a/include/inexor/vulkan-renderer/render-graph/render_graph.hpp b/include/inexor/vulkan-renderer/render-graph/render_graph.hpp index 726e5c546..0134671ae 100644 --- a/include/inexor/vulkan-renderer/render-graph/render_graph.hpp +++ b/include/inexor/vulkan-renderer/render-graph/render_graph.hpp @@ -3,7 +3,6 @@ #include "inexor/vulkan-renderer/render-graph/buffer.hpp" #include "inexor/vulkan-renderer/render-graph/graphics_pass.hpp" #include "inexor/vulkan-renderer/render-graph/graphics_pass_builder.hpp" -#include "inexor/vulkan-renderer/render-graph/shader.hpp" #include "inexor/vulkan-renderer/render-graph/texture.hpp" #include "inexor/vulkan-renderer/wrapper/descriptors/descriptor_set_allocator.hpp" #include "inexor/vulkan-renderer/wrapper/descriptors/descriptor_set_layout_builder.hpp" @@ -118,12 +117,6 @@ class RenderGraph { // Note that we must keep the data as std::vector of std::unique_ptr in order to keep entries consistent std::vector> m_buffers; - // TODO: Shaders should not be in rendergraph! They should be part of the renderer which uses them! - // --------------------------------------------------------------------------------------------------------- - // SHADERS - // --------------------------------------------------------------------------------------------------------- - std::vector> m_shaders; - // --------------------------------------------------------------------------------------------------------- // TEXTURES // --------------------------------------------------------------------------------------------------------- @@ -218,18 +211,6 @@ class RenderGraph { [[nodiscard]] std::shared_ptr add_buffer(std::string buffer_name, BufferType buffer_type, std::function on_update); - // TODO: Use a SPIR-V library like spirv-cross to deduce shader type from the SPIR-V file automatically! - // TODO: Shaders should not be part of rendergraph! - - /// Load a SPIR-V shader from a file - /// @param name The internal debug name of the shader (not the file name) - /// @param shader_stage The shader stage - /// @param file_name The shader file name - /// @note We do not check if ``file_name`` is empty because this is done in the Shader wrapper - /// @return A shared pointer to the shader that was loaded from the SPIR-V file - [[nodiscard]] std::shared_ptr - add_shader(std::string shader_name, VkShaderStageFlagBits shader_stage, std::string file_name); - /// Add a texture which will be initialized inside of rendergraph (not outside of it) /// @param texture_name The name of the texture /// @param usage diff --git a/include/inexor/vulkan-renderer/renderers/imgui.hpp b/include/inexor/vulkan-renderer/renderers/imgui.hpp index 9badcceeb..5615e0f24 100644 --- a/include/inexor/vulkan-renderer/renderers/imgui.hpp +++ b/include/inexor/vulkan-renderer/renderers/imgui.hpp @@ -32,8 +32,8 @@ class ImGuiRenderer { std::shared_ptr m_index_buffer; std::shared_ptr m_vertex_buffer; std::shared_ptr m_imgui_texture; - std::shared_ptr m_vertex_shader; - std::shared_ptr m_fragment_shader; + std::shared_ptr m_vertex_shader; + std::shared_ptr m_fragment_shader; std::shared_ptr m_imgui_pipeline; std::shared_ptr m_imgui_pass; diff --git a/include/inexor/vulkan-renderer/wrapper/pipelines/pipeline_builder.hpp b/include/inexor/vulkan-renderer/wrapper/pipelines/pipeline_builder.hpp index baf7e1547..bb2bcedda 100644 --- a/include/inexor/vulkan-renderer/wrapper/pipelines/pipeline_builder.hpp +++ b/include/inexor/vulkan-renderer/wrapper/pipelines/pipeline_builder.hpp @@ -2,9 +2,9 @@ #include -#include "inexor/vulkan-renderer/render-graph/shader.hpp" #include "inexor/vulkan-renderer/wrapper/make_info.hpp" #include "inexor/vulkan-renderer/wrapper/pipelines/pipeline.hpp" +#include "inexor/vulkan-renderer/wrapper/shader.hpp" #include @@ -122,10 +122,10 @@ class GraphicsPipelineBuilder { /// Add a shader to the graphics pipeline /// @param shader The shader /// @return A reference to the dereferenced this pointer (allows method calls to be chained) - [[nodiscard]] auto &uses_shader(std::shared_ptr shader) { + [[nodiscard]] auto &uses_shader(std::weak_ptr shader) { m_shader_stages.push_back(make_info({ - .stage = shader->m_shader_stage, - .module = shader->m_shader_module, + .stage = shader.lock()->m_shader_stage, + .module = shader.lock()->m_shader_module, .pName = "main", })); diff --git a/include/inexor/vulkan-renderer/render-graph/shader.hpp b/include/inexor/vulkan-renderer/wrapper/shader.hpp similarity index 91% rename from include/inexor/vulkan-renderer/render-graph/shader.hpp rename to include/inexor/vulkan-renderer/wrapper/shader.hpp index 766892e6e..0f5ab8321 100644 --- a/include/inexor/vulkan-renderer/render-graph/shader.hpp +++ b/include/inexor/vulkan-renderer/wrapper/shader.hpp @@ -15,7 +15,7 @@ namespace inexor::vulkan_renderer::wrapper::pipelines { class GraphicsPipelineBuilder; } // namespace inexor::vulkan_renderer::wrapper::pipelines -namespace inexor::vulkan_renderer::render_graph { +namespace inexor::vulkan_renderer::wrapper { // Forward declaration class RenderGraph; @@ -26,8 +26,6 @@ class RenderGraph; using inexor::vulkan_renderer::wrapper::Device; using inexor::vulkan_renderer::wrapper::pipelines::GraphicsPipelineBuilder; -// TODO: This should not be in rendergraph! - /// RAII wrapper class for VkShaderModule class Shader { private: @@ -60,4 +58,4 @@ class Shader { Shader &operator=(Shader &&) = delete; }; -} // namespace inexor::vulkan_renderer::render_graph +} // namespace inexor::vulkan_renderer::wrapper diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 935260919..b266a7caa 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,7 +17,6 @@ set(INEXOR_SOURCE_FILES vulkan-renderer/render-graph/graphics_pass.cpp vulkan-renderer/render-graph/graphics_pass_builder.cpp vulkan-renderer/render-graph/render_graph.cpp - vulkan-renderer/render-graph/shader.cpp vulkan-renderer/render-graph/texture.cpp vulkan-renderer/tools/cla_parser.cpp @@ -30,6 +29,7 @@ set(INEXOR_SOURCE_FILES vulkan-renderer/wrapper/instance.cpp vulkan-renderer/wrapper/make_info.cpp vulkan-renderer/wrapper/sampler.cpp + vulkan-renderer/wrapper/shader.cpp vulkan-renderer/wrapper/swapchain.cpp vulkan-renderer/wrapper/window.cpp vulkan-renderer/wrapper/window_surface.cpp diff --git a/src/vulkan-renderer/application.cpp b/src/vulkan-renderer/application.cpp index 4c2a188bf..ca057ac38 100644 --- a/src/vulkan-renderer/application.cpp +++ b/src/vulkan-renderer/application.cpp @@ -435,21 +435,6 @@ void Application::setup_render_graph() { m_depth_buffer = m_render_graph->add_texture("Depth", DEPTH_STENCIL_BUFFER, VK_FORMAT_D32_SFLOAT_S8_UINT); m_msaa_depth = m_render_graph->add_texture("MSAA-Depth", MSAA_DEPTH_STENCIL_BUFFER, VK_FORMAT_D32_SFLOAT_S8_UINT); - const std::vector vert_input_attr_desc{ - { - .location = 0, - .binding = 0, - .format = VK_FORMAT_R32G32B32_SFLOAT, - .offset = offsetof(OctreeGpuVertex, position), - }, - { - .location = 1, - .binding = 0, - .format = VK_FORMAT_R32G32B32_SFLOAT, - .offset = offsetof(OctreeGpuVertex, color), - }, - }; - m_vertex_buffer = m_render_graph->add_buffer("Octree", VERTEX_BUFFER, [&]() { // If the key N was pressed once, generate a new octree if (m_input_data->was_key_pressed_once(GLFW_KEY_N)) { @@ -462,8 +447,10 @@ void Application::setup_render_graph() { // TODO: How to prevent duplicate loading of shaders or how to deal with object lifetime? // (std::unordered_map>) - m_octree_vert = m_render_graph->add_shader("Octree", VK_SHADER_STAGE_VERTEX_BIT, "shaders/main.vert.spv"); - m_octree_frag = m_render_graph->add_shader("Octree", VK_SHADER_STAGE_FRAGMENT_BIT, "shaders/main.frag.spv"); + m_octree_vert = + std::make_shared(*m_device, "Octree", VK_SHADER_STAGE_VERTEX_BIT, "shaders/main.vert.spv"); + m_octree_frag = + std::make_shared(*m_device, "Octree", VK_SHADER_STAGE_FRAGMENT_BIT, "shaders/main.frag.spv"); // Note that the index buffer is updated together with the vertex buffer to keep data consistent // This means for m_index_buffer, on_init and on_update are defaulted to std::nullopt here! @@ -517,14 +504,26 @@ void Application::setup_render_graph() { .add_default_color_blend_attachment() .add_color_attachment(m_swapchain->image_format()) .set_depth_attachment_format(VK_FORMAT_D32_SFLOAT_S8_UINT) - .set_vertex_input_attributes(vert_input_attr_desc) + .set_vertex_input_attributes({ + { + .location = 0, + .binding = 0, + .format = VK_FORMAT_R32G32B32_SFLOAT, + .offset = offsetof(OctreeGpuVertex, position), + }, + { + .location = 1, + .binding = 0, + .format = VK_FORMAT_R32G32B32_SFLOAT, + .offset = offsetof(OctreeGpuVertex, color), + }, + }) .set_viewport(m_swapchain->extent()) .set_scissor(m_swapchain->extent()) .set_descriptor_set_layout(m_descriptor_set_layout) .uses_shader(m_octree_vert) .uses_shader(m_octree_frag) .build("Octree"); - return m_octree_pipeline; }); diff --git a/src/vulkan-renderer/render-graph/pipeline_builder.cpp b/src/vulkan-renderer/render-graph/pipeline_builder.cpp deleted file mode 100644 index 7be661ee1..000000000 --- a/src/vulkan-renderer/render-graph/pipeline_builder.cpp +++ /dev/null @@ -1,300 +0,0 @@ -#include "inexor/vulkan-renderer/wrapper/pipeline_builder.hpp" - -#include "inexor/vulkan-renderer/wrapper/device.hpp" - -#include - -#include -#include - -namespace inexor::vulkan_renderer::wrapper { - -GraphicsPipelineBuilder::GraphicsPipelineBuilder() { - reset(); -} - -GraphicsPipelineBuilder::GraphicsPipelineBuilder(GraphicsPipelineBuilder &&other) noexcept { - m_vertex_input_sci = std::move(other.m_vertex_input_sci); - m_input_assembly_sci = std::move(other.m_input_assembly_sci); - m_tesselation_sci = std::move(other.m_tesselation_sci); - m_viewport_sci = std::move(other.m_viewport_sci); - m_rasterization_sci = std::move(m_rasterization_sci); - m_multisample_sci = std::move(other.m_multisample_sci); - m_depth_stencil_sci = std::move(other.m_depth_stencil_sci); - m_color_blend_sci = std::move(other.m_color_blend_sci); - m_dynamic_states_sci = std::move(other.m_dynamic_states_sci); - m_pipeline_layout = std::exchange(other.m_pipeline_layout, VK_NULL_HANDLE); - m_render_pass = std::exchange(other.m_render_pass, VK_NULL_HANDLE); - m_dynamic_states = std::move(other.m_dynamic_states); - m_viewports = std::move(other.m_viewports); - m_scissors = std::move(other.m_scissors); - m_shader_stages = std::move(other.m_shader_stages); - m_vertex_input_binding_descriptions = std::move(other.m_vertex_input_binding_descriptions); - m_vertex_input_attribute_descriptions = std::move(other.m_vertex_input_attribute_descriptions); - m_color_blend_attachment_states = std::move(other.m_color_blend_attachment_states); -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::add_shader(const VkPipelineShaderStageCreateInfo &shader_stage) { - m_shader_stages.push_back(shader_stage); - return *this; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::add_color_blend_attachment(const VkPipelineColorBlendAttachmentState &attachment) { - m_color_blend_attachment_states.push_back(attachment); - return *this; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::add_vertex_input_attribute(const VkVertexInputAttributeDescription &description) { - m_vertex_input_attribute_descriptions.push_back(description); - return *this; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::add_vertex_input_binding(const VkVertexInputBindingDescription &description) { - m_vertex_input_binding_descriptions.push_back(description); - return *this; -} - -VkGraphicsPipelineCreateInfo GraphicsPipelineBuilder::build() { - m_vertex_input_sci = make_info({ - .vertexBindingDescriptionCount = static_cast(m_vertex_input_binding_descriptions.size()), - .pVertexBindingDescriptions = m_vertex_input_binding_descriptions.data(), - .vertexAttributeDescriptionCount = static_cast(m_vertex_input_attribute_descriptions.size()), - .pVertexAttributeDescriptions = m_vertex_input_attribute_descriptions.data(), - - }); - - m_viewport_sci = make_info({ - .viewportCount = static_cast(m_viewports.size()), - .pViewports = m_viewports.data(), - .scissorCount = static_cast(m_scissors.size()), - .pScissors = m_scissors.data(), - }); - - if (!m_dynamic_states.empty()) { - m_dynamic_states_sci = make_info({ - .dynamicStateCount = static_cast(m_dynamic_states.size()), - .pDynamicStates = m_dynamic_states.data(), - }); - } - - return make_info({ - .stageCount = static_cast(m_shader_stages.size()), - .pStages = m_shader_stages.data(), - .pVertexInputState = &m_vertex_input_sci, - .pInputAssemblyState = &m_input_assembly_sci, - .pTessellationState = &m_tesselation_sci, - .pViewportState = &m_viewport_sci, - .pRasterizationState = &m_rasterization_sci, - .pMultisampleState = &m_multisample_sci, - .pDepthStencilState = &m_depth_stencil_sci, - .pColorBlendState = &m_color_blend_sci, - .pDynamicState = &m_dynamic_states_sci, - .layout = m_pipeline_layout, - .renderPass = m_render_pass, - }); -} - -void GraphicsPipelineBuilder::reset() { - m_vertex_input_binding_descriptions.clear(); - m_vertex_input_attribute_descriptions.clear(); - m_vertex_input_sci = { - make_info(), - }; - - m_input_assembly_sci = { - make_info({ - .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, - .primitiveRestartEnable = VK_FALSE, - }), - }; - - m_tesselation_sci = { - make_info(), - }; - - m_viewports.clear(); - m_scissors.clear(); - m_viewport_sci = { - make_info(), - }; - - m_rasterization_sci = { - make_info({ - .polygonMode = VK_POLYGON_MODE_FILL, - .cullMode = VK_CULL_MODE_BACK_BIT, - .frontFace = VK_FRONT_FACE_CLOCKWISE, - .lineWidth = 1.0f, - }), - }; - - m_multisample_sci = { - make_info({ - .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT, - .sampleShadingEnable = VK_FALSE, - .minSampleShading = 1.0f, - }), - }; - - m_depth_stencil_sci = { - make_info(), - }; - - m_color_blend_sci = { - make_info(), - }; - - m_dynamic_states.clear(); - m_dynamic_states_sci = { - make_info(), - }; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::set_color_blend(const VkPipelineColorBlendStateCreateInfo &color_blend) { - m_color_blend_sci = color_blend; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_color_blend_attachments( - const std::vector &attachments) { - assert(!attachments.empty()); - m_color_blend_attachment_states = attachments; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_culling_mode(const VkBool32 culling_enabled) { - spdlog::warn("Culling is disabled, which could have negative effects on the performance!"); - m_rasterization_sci.cullMode = culling_enabled ? VK_CULL_MODE_BACK_BIT : VK_CULL_MODE_NONE; - return *this; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::set_depth_stencil(const VkPipelineDepthStencilStateCreateInfo &depth_stencil) { - m_depth_stencil_sci = depth_stencil; - return *this; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::set_dynamic_states(const std::vector &dynamic_states) { - assert(!dynamic_states.empty()); - m_dynamic_states = dynamic_states; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_line_width(const float width) { - m_rasterization_sci.lineWidth = width; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_multisampling(const VkSampleCountFlagBits sample_count, - const float min_sample_shading) { - m_multisample_sci.rasterizationSamples = sample_count; - m_multisample_sci.minSampleShading = min_sample_shading; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_pipeline_layout(const VkPipelineLayout layout) { - assert(layout); - m_pipeline_layout = layout; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_primitive_topology(const VkPrimitiveTopology topology) { - m_input_assembly_sci.topology = topology; - return *this; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::set_rasterization(const VkPipelineRasterizationStateCreateInfo &rasterization) { - m_rasterization_sci = rasterization; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_render_pass(const VkRenderPass render_pass) { - assert(render_pass); - m_render_pass = render_pass; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_scissor(const VkRect2D &scissor) { - m_scissors = {scissor}; - m_viewport_sci.scissorCount = 1; - m_viewport_sci.pScissors = m_scissors.data(); - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_scissor(const VkExtent2D &extent) { - return set_scissor({ - // Convert VkExtent2D to VkRect2D - .extent = extent, - }); -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_scissors(const std::vector &scissors) { - assert(!scissors.empty()); - m_scissors = scissors; - m_viewport_sci.scissorCount = static_cast(scissors.size()); - m_viewport_sci.pScissors = scissors.data(); - return *this; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::set_shaders(const std::vector &shader_stages) { - assert(!shader_stages.empty()); - m_shader_stages = shader_stages; - return *this; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::set_tesselation_control_point_count(const std::uint32_t control_point_count) { - m_tesselation_sci.patchControlPoints = control_point_count; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_vertex_input_attributes( - const std::vector &descriptions) { - assert(!descriptions.empty()); - m_vertex_input_attribute_descriptions = descriptions; - return *this; -} - -GraphicsPipelineBuilder & -GraphicsPipelineBuilder::set_vertex_input_bindings(const std::vector &descriptions) { - assert(!descriptions.empty()); - m_vertex_input_binding_descriptions = descriptions; - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_viewport(const VkViewport &viewport) { - m_viewports = {viewport}; - m_viewport_sci.viewportCount = 1; - m_viewport_sci.pViewports = m_viewports.data(); - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_viewport(const VkExtent2D &extent) { - return set_viewport({ - // Convert VkExtent2D to VkViewport - .width = static_cast(extent.width), - .height = static_cast(extent.height), - .maxDepth = 1.0f, - }); -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_viewports(const std::vector &viewports) { - assert(!viewports.empty()); - m_viewports = viewports; - m_viewport_sci.viewportCount = static_cast(m_viewports.size()); - m_viewport_sci.pViewports = m_viewports.data(); - return *this; -} - -GraphicsPipelineBuilder &GraphicsPipelineBuilder::set_wireframe(const VkBool32 wireframe) { - m_rasterization_sci.polygonMode = (wireframe == VK_TRUE) ? VK_POLYGON_MODE_LINE : VK_POLYGON_MODE_FILL; - return *this; -} - -} // namespace inexor::vulkan_renderer::wrapper diff --git a/src/vulkan-renderer/render-graph/render_graph.cpp b/src/vulkan-renderer/render-graph/render_graph.cpp index 6710b703a..975984589 100644 --- a/src/vulkan-renderer/render-graph/render_graph.cpp +++ b/src/vulkan-renderer/render-graph/render_graph.cpp @@ -32,14 +32,6 @@ RenderGraph::add_buffer(std::string buffer_name, const BufferType buffer_type, s return m_buffers.back(); } -// TODO: Shaders should not be part of rendergraph! -std::shared_ptr -RenderGraph::add_shader(std::string shader_name, const VkShaderStageFlagBits shader_stage, std::string file_name) { - m_shaders.emplace_back( - std::make_shared(m_device, std::move(shader_name), shader_stage, std::move(file_name))); - return m_shaders.back(); -} - std::shared_ptr RenderGraph::add_texture(std::string texture_name, const TextureUsage usage, const VkFormat format) { m_textures.emplace_back(std::make_shared(m_device, std::move(texture_name), usage, format)); @@ -169,8 +161,7 @@ void RenderGraph::record_command_buffer_for_pass(const CommandBuffer &cmd_buf, // TODO: Remove img_index and implement swapchain.get_current_image() // TODO: Or do we need the image index for buffers? (We want to automatically double or triple buffer them) - // Start a new debug label for this graphics pass (debug labels are visible in graphics debuggers like - // RenderDoc) + // Start a new debug label for this graphics pass (visible in graphics debuggers like RenderDoc) // TODO: Generate color gradient depending on the number of passes? (Interpolate e.g. in 12 steps for 12 passes) cmd_buf.begin_debug_label_region(pass.m_name, {1.0f, 0.0f, 0.0f, 1.0f}); @@ -254,8 +245,7 @@ void RenderGraph::record_command_buffer_for_pass(const CommandBuffer &cmd_buf, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR); } - // End the debug label for this graphics pass (debug labels are visible in graphics debuggers like - // RenderDoc) + // End the debug label for this graphics pass (visible in graphics debuggers like RenderDoc) cmd_buf.end_debug_label_region(); } diff --git a/src/vulkan-renderer/renderers/imgui.cpp b/src/vulkan-renderer/renderers/imgui.cpp index 4f891a4f5..79793a31f 100644 --- a/src/vulkan-renderer/renderers/imgui.cpp +++ b/src/vulkan-renderer/renderers/imgui.cpp @@ -1,11 +1,9 @@ #include "inexor/vulkan-renderer/renderers/imgui.hpp" #include "inexor/vulkan-renderer/render-graph/graphics_pass_builder.hpp" -#include "inexor/vulkan-renderer/render-graph/pipeline_builder.hpp" #include "inexor/vulkan-renderer/render-graph/render_graph.hpp" -#include "inexor/vulkan-renderer/render-graph/shader.hpp" -#include "inexor/vulkan-renderer/wrapper/descriptors/descriptor_set_layout_builder.hpp" #include "inexor/vulkan-renderer/wrapper/make_info.hpp" +#include "inexor/vulkan-renderer/wrapper/shader.hpp" #include "inexor/vulkan-renderer/wrapper/swapchain.hpp" #include @@ -65,8 +63,10 @@ ImGuiRenderer::ImGuiRenderer(const Device &device, // TODO: Shaders should be part of the renderer, but not the rendergraph itself! // TODO: Implement a ShaderManager (ShaderCache?) inside of Device wrapper? - m_vertex_shader = render_graph.add_shader("ImGui", VK_SHADER_STAGE_VERTEX_BIT, "shaders/ui.vert.spv"); - m_fragment_shader = render_graph.add_shader("ImGui", VK_SHADER_STAGE_FRAGMENT_BIT, "shaders/ui.frag.spv"); + m_vertex_shader = + std::make_shared(m_device, "ImGui", VK_SHADER_STAGE_VERTEX_BIT, "shaders/ui.vert.spv"); + m_fragment_shader = + std::make_shared(m_device, "ImGui", VK_SHADER_STAGE_FRAGMENT_BIT, "shaders/ui.frag.spv"); using render_graph::TextureUsage; m_imgui_texture = render_graph.add_texture("ImGui-Font", TextureUsage::NORMAL, [&]() { @@ -153,7 +153,6 @@ ImGuiRenderer::ImGuiRenderer(const Device &device, .set_descriptor_set_layout(m_descriptor_set_layout) .add_push_constant_range(VK_SHADER_STAGE_VERTEX_BIT, sizeof(m_push_const_block)) .build("ImGui"); - return m_imgui_pipeline; }); diff --git a/src/vulkan-renderer/render-graph/shader.cpp b/src/vulkan-renderer/wrapper/shader.cpp similarity index 94% rename from src/vulkan-renderer/render-graph/shader.cpp rename to src/vulkan-renderer/wrapper/shader.cpp index 1351e9e90..8d718ad7b 100644 --- a/src/vulkan-renderer/render-graph/shader.cpp +++ b/src/vulkan-renderer/wrapper/shader.cpp @@ -1,4 +1,4 @@ -#include "inexor/vulkan-renderer/render-graph/shader.hpp" +#include "inexor/vulkan-renderer/wrapper/shader.hpp" #include "inexor/vulkan-renderer/exception.hpp" #include "inexor/vulkan-renderer/wrapper/device.hpp" @@ -9,7 +9,7 @@ #include #include -namespace inexor::vulkan_renderer::render_graph { +namespace inexor::vulkan_renderer::wrapper { Shader::Shader(const Device &device, std::string name, const VkShaderStageFlagBits type, std::string file_name) : m_device(device), m_name(std::move(name)), m_shader_stage(type), m_file_name(file_name) { @@ -59,4 +59,4 @@ Shader::~Shader() { vkDestroyShaderModule(m_device.device(), m_shader_module, nullptr); } -} // namespace inexor::vulkan_renderer::render_graph +} // namespace inexor::vulkan_renderer::wrapper