|
| 1 | +module; |
| 2 | + |
| 3 | +#include <memory> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +export module GraphicsPipeline; |
| 7 | + |
| 8 | +import vulkan_hpp; |
| 9 | + |
| 10 | +import DataLoader; |
| 11 | +import Utility; |
| 12 | +import Device; |
| 13 | +import RenderPass; |
| 14 | + |
| 15 | +export namespace vht { |
| 16 | + |
| 17 | + /** |
| 18 | + * @brief 图形管线相关 |
| 19 | + * @details |
| 20 | + * - 依赖: |
| 21 | + * - m_device: 逻辑设备与队列 |
| 22 | + * - m_render_pass: 渲染通道 |
| 23 | + * - 工作: |
| 24 | + * - 创建描述符集布局 |
| 25 | + * - 创建图形管线布局和图形管线 |
| 26 | + * - 可访问成员: |
| 27 | + * - descriptor_set_layout(): 描述符集布局 |
| 28 | + * - pipeline_layout(): 管线布局 |
| 29 | + * - pipeline(): 图形管线 |
| 30 | + */ |
| 31 | + class GraphicsPipeline { |
| 32 | + std::shared_ptr<vht::Device> m_device; |
| 33 | + std::shared_ptr<vht::RenderPass> m_render_pass; |
| 34 | + vk::raii::DescriptorSetLayout m_descriptor_set_layout{ nullptr }; |
| 35 | + vk::raii::PipelineLayout m_pipeline_layout{ nullptr }; |
| 36 | + vk::raii::Pipeline m_pipeline{ nullptr }; |
| 37 | + public: |
| 38 | + explicit GraphicsPipeline(std::shared_ptr<vht::Device> device, std::shared_ptr<vht::RenderPass> render_pass) |
| 39 | + : m_device(std::move(device)), |
| 40 | + m_render_pass(std::move(render_pass)) { |
| 41 | + init(); |
| 42 | + } |
| 43 | + |
| 44 | + [[nodiscard]] |
| 45 | + const vk::raii::DescriptorSetLayout& descriptor_set_layout() const { return m_descriptor_set_layout; } |
| 46 | + [[nodiscard]] |
| 47 | + const vk::raii::PipelineLayout& pipeline_layout() const { return m_pipeline_layout; } |
| 48 | + [[nodiscard]] |
| 49 | + const vk::raii::Pipeline& pipeline() const { return m_pipeline; } |
| 50 | + |
| 51 | + private: |
| 52 | + void init() { |
| 53 | + create_descriptor_set_layout(); |
| 54 | + create_graphics_pipeline(); |
| 55 | + } |
| 56 | + // 创建描述符集布局 |
| 57 | + void create_descriptor_set_layout() { |
| 58 | + vk::DescriptorSetLayoutBinding ubo_layout_binging; |
| 59 | + ubo_layout_binging.binding = 0; |
| 60 | + ubo_layout_binging.descriptorType = vk::DescriptorType::eUniformBuffer; |
| 61 | + ubo_layout_binging.descriptorCount = 1; |
| 62 | + ubo_layout_binging.stageFlags = vk::ShaderStageFlagBits::eVertex; |
| 63 | + |
| 64 | + vk::DescriptorSetLayoutBinding sampler_layout_binding; |
| 65 | + sampler_layout_binding.binding = 1; |
| 66 | + sampler_layout_binding.descriptorType = vk::DescriptorType::eCombinedImageSampler; |
| 67 | + sampler_layout_binding.descriptorCount = 1; |
| 68 | + sampler_layout_binding.stageFlags = vk::ShaderStageFlagBits::eFragment; |
| 69 | + |
| 70 | + auto bindings = { ubo_layout_binging, sampler_layout_binding }; |
| 71 | + vk::DescriptorSetLayoutCreateInfo layoutInfo; |
| 72 | + layoutInfo.setBindings( bindings ); |
| 73 | + |
| 74 | + m_descriptor_set_layout = m_device->device().createDescriptorSetLayout( layoutInfo ); |
| 75 | + } |
| 76 | + // 创建图形管线 |
| 77 | + void create_graphics_pipeline() { |
| 78 | + const auto vertex_shader_code = vht::read_shader("shaders/vert.spv"); |
| 79 | + const auto fragment_shader_code = vht::read_shader("shaders/frag.spv"); |
| 80 | + const auto vertex_shader_module = vht::create_shader_module(m_device->device(), vertex_shader_code); |
| 81 | + const auto fragment_shader_module = vht::create_shader_module(m_device->device(), fragment_shader_code); |
| 82 | + vk::PipelineShaderStageCreateInfo vertex_shader_create_info; |
| 83 | + vertex_shader_create_info.stage = vk::ShaderStageFlagBits::eVertex; |
| 84 | + vertex_shader_create_info.module = vertex_shader_module; |
| 85 | + vertex_shader_create_info.pName = "main"; |
| 86 | + |
| 87 | + // 1. 数据源 |
| 88 | + float my_color = 0.4f; |
| 89 | + // 2. 特化映射条目 |
| 90 | + vk::SpecializationMapEntry mapEntry; |
| 91 | + mapEntry.constantID = 0; // 对应 GLSL 中的 constant_id 和 SPIR-V 中的 SpecId |
| 92 | + mapEntry.offset = 0; // 源数据的起始偏移量 |
| 93 | + mapEntry.size = sizeof(float); |
| 94 | + // 3. 特化信息 |
| 95 | + vk::SpecializationInfo specializationInfo; |
| 96 | + specializationInfo.setMapEntries(mapEntry); |
| 97 | + specializationInfo.setData<float>(my_color); |
| 98 | + // 此模板设置了 指针 和 数据大小 ,不能放右值 |
| 99 | + |
| 100 | + vk::PipelineShaderStageCreateInfo fragment_shader_create_info; // 片段着色器 |
| 101 | + fragment_shader_create_info.stage = vk::ShaderStageFlagBits::eFragment; |
| 102 | + fragment_shader_create_info.module = fragment_shader_module; |
| 103 | + fragment_shader_create_info.pName = "main"; |
| 104 | + fragment_shader_create_info.pSpecializationInfo = &specializationInfo; // 特化信息 |
| 105 | + |
| 106 | + const auto shader_stages = { vertex_shader_create_info, fragment_shader_create_info }; |
| 107 | + |
| 108 | + const auto dynamic_states = { vk::DynamicState::eViewport, vk::DynamicState::eScissor }; |
| 109 | + vk::PipelineDynamicStateCreateInfo dynamic_state; |
| 110 | + dynamic_state.setDynamicStates(dynamic_states); |
| 111 | + |
| 112 | + auto binding_description = vht::Vertex::get_binding_description(); |
| 113 | + auto attribute_description = vht::Vertex::get_attribute_description(); |
| 114 | + vk::PipelineVertexInputStateCreateInfo vertex_input; |
| 115 | + vertex_input.setVertexBindingDescriptions(binding_description); |
| 116 | + vertex_input.setVertexAttributeDescriptions(attribute_description); |
| 117 | + |
| 118 | + vk::PipelineInputAssemblyStateCreateInfo input_assembly; |
| 119 | + input_assembly.topology = vk::PrimitiveTopology::eTriangleList; |
| 120 | + |
| 121 | + vk::PipelineViewportStateCreateInfo viewport_state; |
| 122 | + viewport_state.viewportCount = 1; |
| 123 | + viewport_state.scissorCount = 1; |
| 124 | + |
| 125 | + vk::PipelineDepthStencilStateCreateInfo depth_stencil; |
| 126 | + depth_stencil.depthTestEnable = true; |
| 127 | + depth_stencil.depthWriteEnable = true; |
| 128 | + depth_stencil.depthCompareOp = vk::CompareOp::eLess; |
| 129 | + depth_stencil.depthBoundsTestEnable = false; // Optional |
| 130 | + depth_stencil.stencilTestEnable = false; // Optional |
| 131 | + |
| 132 | + vk::PipelineRasterizationStateCreateInfo rasterizer; |
| 133 | + rasterizer.depthClampEnable = false; |
| 134 | + rasterizer.rasterizerDiscardEnable = false; |
| 135 | + rasterizer.polygonMode = vk::PolygonMode::eFill; |
| 136 | + rasterizer.lineWidth = 1.0f; |
| 137 | + rasterizer.cullMode = vk::CullModeFlagBits::eBack; |
| 138 | + rasterizer.frontFace = vk::FrontFace::eCounterClockwise; |
| 139 | + rasterizer.depthBiasEnable = false; |
| 140 | + |
| 141 | + vk::PipelineMultisampleStateCreateInfo multisampling; |
| 142 | + multisampling.rasterizationSamples = vk::SampleCountFlagBits::e1; |
| 143 | + multisampling.sampleShadingEnable = false; // default |
| 144 | + |
| 145 | + vk::PipelineColorBlendAttachmentState color_blend_attachment; |
| 146 | + color_blend_attachment.blendEnable = false; // default |
| 147 | + color_blend_attachment.colorWriteMask = vk::FlagTraits<vk::ColorComponentFlagBits>::allFlags; |
| 148 | + |
| 149 | + vk::PipelineColorBlendStateCreateInfo color_blend; |
| 150 | + color_blend.logicOpEnable = false; |
| 151 | + color_blend.logicOp = vk::LogicOp::eCopy; |
| 152 | + color_blend.setAttachments( color_blend_attachment ); |
| 153 | + |
| 154 | + vk::PipelineLayoutCreateInfo layout_create_info; |
| 155 | + layout_create_info.setSetLayouts( *m_descriptor_set_layout ); |
| 156 | + m_pipeline_layout = m_device->device().createPipelineLayout( layout_create_info ); |
| 157 | + |
| 158 | + vk::GraphicsPipelineCreateInfo create_info; |
| 159 | + create_info.setStages( shader_stages ); |
| 160 | + |
| 161 | + create_info.layout = m_pipeline_layout; |
| 162 | + |
| 163 | + |
| 164 | + create_info.pVertexInputState = &vertex_input; |
| 165 | + create_info.pInputAssemblyState = &input_assembly; |
| 166 | + create_info.pDynamicState = &dynamic_state; |
| 167 | + create_info.pViewportState = &viewport_state; |
| 168 | + create_info.pDepthStencilState = &depth_stencil; |
| 169 | + create_info.pRasterizationState = &rasterizer; |
| 170 | + create_info.pMultisampleState = &multisampling; |
| 171 | + create_info.pColorBlendState = &color_blend; |
| 172 | + |
| 173 | + create_info.renderPass = m_render_pass->render_pass(); |
| 174 | + create_info.subpass = 0; |
| 175 | + |
| 176 | + m_pipeline = m_device->device().createGraphicsPipeline( nullptr, create_info ); |
| 177 | + } |
| 178 | + }; |
| 179 | +} |
| 180 | + |
| 181 | + |
0 commit comments