|
| 1 | +export module GraphicsPipeline; |
| 2 | + |
| 3 | +import std; |
| 4 | +import vulkan_hpp; |
| 5 | + |
| 6 | +import DataLoader; |
| 7 | +import Tools; |
| 8 | +import Device; |
| 9 | +import RenderPass; |
| 10 | + |
| 11 | +export namespace vht { |
| 12 | + |
| 13 | + /** |
| 14 | + * @brief 图形管线相关 |
| 15 | + * @details |
| 16 | + * - 依赖: |
| 17 | + * - m_device: 逻辑设备与队列 |
| 18 | + * - m_render_pass: 渲染通道 |
| 19 | + * - 工作: |
| 20 | + * - 创建描述符集布局 |
| 21 | + * - 创建图形管线布局和图形管线 |
| 22 | + * - 可访问成员: |
| 23 | + * - descriptor_set_layout(): 描述符集布局 |
| 24 | + * - pipeline_layout(): 管线布局 |
| 25 | + * - pipeline(): 图形管线 |
| 26 | + */ |
| 27 | + class GraphicsPipeline { |
| 28 | + std::shared_ptr<vht::Device> m_device; |
| 29 | + std::shared_ptr<vht::RenderPass> m_render_pass; |
| 30 | + std::vector<vk::raii::DescriptorSetLayout> m_descriptor_set_layouts; |
| 31 | + vk::raii::PipelineCache m_pipeline_cache{ nullptr }; |
| 32 | + vk::raii::PipelineLayout m_pipeline_layout{ nullptr }; |
| 33 | + vk::raii::Pipeline m_pipeline{ nullptr }; |
| 34 | + public: |
| 35 | + explicit GraphicsPipeline(std::shared_ptr<vht::Device> device, std::shared_ptr<vht::RenderPass> render_pass) |
| 36 | + : m_device(std::move(device)), |
| 37 | + m_render_pass(std::move(render_pass)) { |
| 38 | + init(); |
| 39 | + } |
| 40 | + |
| 41 | + [[nodiscard]] |
| 42 | + const std::vector<vk::raii::DescriptorSetLayout>& descriptor_set_layouts() const { return m_descriptor_set_layouts; } |
| 43 | + [[nodiscard]] |
| 44 | + const vk::raii::PipelineLayout& pipeline_layout() const { return m_pipeline_layout; } |
| 45 | + [[nodiscard]] |
| 46 | + const vk::raii::Pipeline& pipeline() const { return m_pipeline; } |
| 47 | + |
| 48 | + private: |
| 49 | + void init() { |
| 50 | + create_descriptor_set_layout(); |
| 51 | + create_pipeline_cache(); |
| 52 | + create_graphics_pipeline(); |
| 53 | + save_pipeline_cache(); |
| 54 | + } |
| 55 | + |
| 56 | + void create_pipeline_cache() { |
| 57 | + vk::PipelineCacheCreateInfo pipelineCacheInfo; |
| 58 | + std::vector<char> data; |
| 59 | + if (std::ifstream in("pipeline_cache.data", std::ios::binary | std::ios::ate); |
| 60 | + in |
| 61 | + ) { |
| 62 | + const size_t size = in.tellg(); |
| 63 | + in.seekg(0); |
| 64 | + data.resize(size); |
| 65 | + in.read(data.data(), size); |
| 66 | + pipelineCacheInfo.setInitialData<char>(data); |
| 67 | + std::println("Pipeline cache loaded from file."); |
| 68 | + } |
| 69 | + m_pipeline_cache = m_device->device().createPipelineCache(pipelineCacheInfo); |
| 70 | + } |
| 71 | + |
| 72 | + void save_pipeline_cache() const { |
| 73 | + // std::vector<::uint8_t> |
| 74 | + const auto cacheData = m_pipeline_cache.getData(); |
| 75 | + std::ofstream out("pipeline_cache.data", std::ios::binary); |
| 76 | + out.write(reinterpret_cast<const char*>(cacheData.data()), cacheData.size()); |
| 77 | + } |
| 78 | + |
| 79 | + // 创建描述符集布局 |
| 80 | + void create_descriptor_set_layout() { |
| 81 | + vk::DescriptorSetLayoutBinding uboLayoutBinding; |
| 82 | + uboLayoutBinding.binding = 0; |
| 83 | + uboLayoutBinding.descriptorType = vk::DescriptorType::eUniformBuffer; |
| 84 | + uboLayoutBinding.descriptorCount = 1; |
| 85 | + uboLayoutBinding.stageFlags = vk::ShaderStageFlagBits::eVertex; |
| 86 | + |
| 87 | + vk::DescriptorSetLayoutCreateInfo uboLayoutInfo; |
| 88 | + uboLayoutInfo.setBindings( uboLayoutBinding ); |
| 89 | + m_descriptor_set_layouts.emplace_back( m_device->device().createDescriptorSetLayout( uboLayoutInfo ) ); |
| 90 | + |
| 91 | + vk::DescriptorSetLayoutBinding samplerLayoutBinding; |
| 92 | + samplerLayoutBinding.binding = 0; |
| 93 | + samplerLayoutBinding.descriptorType = vk::DescriptorType::eCombinedImageSampler; |
| 94 | + samplerLayoutBinding.descriptorCount = 1; |
| 95 | + samplerLayoutBinding.stageFlags = vk::ShaderStageFlagBits::eFragment; |
| 96 | + vk::DescriptorSetLayoutCreateInfo samplerLayoutInfo; |
| 97 | + |
| 98 | + samplerLayoutInfo.setBindings( samplerLayoutBinding ); |
| 99 | + m_descriptor_set_layouts.emplace_back( m_device->device().createDescriptorSetLayout( samplerLayoutInfo ) ); |
| 100 | + } |
| 101 | + // 创建图形管线 |
| 102 | + void create_graphics_pipeline() { |
| 103 | + const auto vertex_shader_code = vht::read_shader("shaders/graphics.vert.spv"); |
| 104 | + const auto fragment_shader_code = vht::read_shader("shaders/graphics.frag.spv"); |
| 105 | + const auto vertex_shader_module = vht::create_shader_module(m_device->device(), vertex_shader_code); |
| 106 | + const auto fragment_shader_module = vht::create_shader_module(m_device->device(), fragment_shader_code); |
| 107 | + vk::PipelineShaderStageCreateInfo vertex_shader_create_info; |
| 108 | + vertex_shader_create_info.stage = vk::ShaderStageFlagBits::eVertex; |
| 109 | + vertex_shader_create_info.module = vertex_shader_module; |
| 110 | + vertex_shader_create_info.pName = "main"; |
| 111 | + |
| 112 | + vk::PipelineShaderStageCreateInfo fragment_shader_create_info; |
| 113 | + fragment_shader_create_info.stage = vk::ShaderStageFlagBits::eFragment; |
| 114 | + fragment_shader_create_info.module = fragment_shader_module; |
| 115 | + fragment_shader_create_info.pName = "main"; |
| 116 | + |
| 117 | + const auto shader_stages = { vertex_shader_create_info, fragment_shader_create_info }; |
| 118 | + |
| 119 | + const auto dynamic_states = { vk::DynamicState::eViewport, vk::DynamicState::eScissor }; |
| 120 | + vk::PipelineDynamicStateCreateInfo dynamic_state; |
| 121 | + dynamic_state.setDynamicStates(dynamic_states); |
| 122 | + |
| 123 | + const auto binding_description = vht::Vertex::get_binding_description(); |
| 124 | + const auto attribute_description = vht::Vertex::get_attribute_description(); |
| 125 | + vk::PipelineVertexInputStateCreateInfo vertex_input; |
| 126 | + vertex_input.setVertexBindingDescriptions(binding_description); |
| 127 | + vertex_input.setVertexAttributeDescriptions(attribute_description); |
| 128 | + |
| 129 | + vk::PipelineInputAssemblyStateCreateInfo input_assembly; |
| 130 | + input_assembly.topology = vk::PrimitiveTopology::eTriangleList; |
| 131 | + |
| 132 | + vk::PipelineViewportStateCreateInfo viewport_state; |
| 133 | + viewport_state.viewportCount = 1; |
| 134 | + viewport_state.scissorCount = 1; |
| 135 | + |
| 136 | + vk::PipelineDepthStencilStateCreateInfo depth_stencil; |
| 137 | + depth_stencil.depthTestEnable = true; |
| 138 | + depth_stencil.depthWriteEnable = true; |
| 139 | + depth_stencil.depthCompareOp = vk::CompareOp::eLess; |
| 140 | + depth_stencil.depthBoundsTestEnable = false; // Optional |
| 141 | + depth_stencil.stencilTestEnable = false; // Optional |
| 142 | + |
| 143 | + vk::PipelineRasterizationStateCreateInfo rasterizer; |
| 144 | + rasterizer.depthClampEnable = false; |
| 145 | + rasterizer.rasterizerDiscardEnable = false; |
| 146 | + rasterizer.polygonMode = vk::PolygonMode::eFill; |
| 147 | + rasterizer.lineWidth = 1.0f; |
| 148 | + rasterizer.cullMode = vk::CullModeFlagBits::eBack; |
| 149 | + rasterizer.frontFace = vk::FrontFace::eCounterClockwise; |
| 150 | + rasterizer.depthBiasEnable = false; |
| 151 | + |
| 152 | + vk::PipelineMultisampleStateCreateInfo multisampling; |
| 153 | + multisampling.rasterizationSamples = vk::SampleCountFlagBits::e1; |
| 154 | + multisampling.sampleShadingEnable = false; // default |
| 155 | + |
| 156 | + vk::PipelineColorBlendAttachmentState color_blend_attachment; |
| 157 | + color_blend_attachment.blendEnable = false; // default |
| 158 | + color_blend_attachment.colorWriteMask = vk::FlagTraits<vk::ColorComponentFlagBits>::allFlags; |
| 159 | + |
| 160 | + vk::PipelineColorBlendStateCreateInfo color_blend; |
| 161 | + color_blend.logicOpEnable = false; |
| 162 | + color_blend.logicOp = vk::LogicOp::eCopy; |
| 163 | + color_blend.setAttachments( color_blend_attachment ); |
| 164 | + |
| 165 | + vk::PipelineLayoutCreateInfo layout_create_info; |
| 166 | + |
| 167 | + // 将 raii 类型转换回 vk::DescriptorSetLayout |
| 168 | + const auto set_layouts = m_descriptor_set_layouts |
| 169 | + | std::views::transform([](const auto& layout) -> vk::DescriptorSetLayout { return layout; }) |
| 170 | + | std::ranges::to<std::vector>(); |
| 171 | + |
| 172 | + layout_create_info.setSetLayouts( set_layouts ); |
| 173 | + m_pipeline_layout = m_device->device().createPipelineLayout( layout_create_info ); |
| 174 | + |
| 175 | + vk::GraphicsPipelineCreateInfo create_info; |
| 176 | + create_info.layout = m_pipeline_layout; |
| 177 | + |
| 178 | + create_info.setStages( shader_stages ); |
| 179 | + create_info.pVertexInputState = &vertex_input; |
| 180 | + create_info.pInputAssemblyState = &input_assembly; |
| 181 | + create_info.pDynamicState = &dynamic_state; |
| 182 | + create_info.pViewportState = &viewport_state; |
| 183 | + create_info.pDepthStencilState = &depth_stencil; |
| 184 | + create_info.pRasterizationState = &rasterizer; |
| 185 | + create_info.pMultisampleState = &multisampling; |
| 186 | + create_info.pColorBlendState = &color_blend; |
| 187 | + |
| 188 | + create_info.renderPass = m_render_pass->render_pass(); |
| 189 | + create_info.subpass = 0; |
| 190 | + |
| 191 | + m_pipeline = m_device->device().createGraphicsPipeline( m_pipeline_cache, create_info ); |
| 192 | + } |
| 193 | + }; |
| 194 | +} |
| 195 | + |
| 196 | + |
0 commit comments