1
- module ;
2
-
3
- #include < memory>
4
- #include < vector>
5
-
6
1
export module GraphicsPipeline;
7
2
3
+ import std;
8
4
import vulkan_hpp;
9
5
10
6
import DataLoader;
11
- import Utility ;
7
+ import Tools ;
12
8
import Device;
13
9
import RenderPass;
14
10
@@ -31,7 +27,7 @@ export namespace vht {
31
27
class GraphicsPipeline {
32
28
std::shared_ptr<vht::Device> m_device;
33
29
std::shared_ptr<vht::RenderPass> m_render_pass;
34
- vk::raii::DescriptorSetLayout m_descriptor_set_layout{ nullptr } ;
30
+ std::vector< vk::raii::DescriptorSetLayout> m_descriptor_set_layouts ;
35
31
vk::raii::PipelineLayout m_pipeline_layout{ nullptr };
36
32
vk::raii::Pipeline m_pipeline{ nullptr };
37
33
public:
@@ -42,7 +38,7 @@ export namespace vht {
42
38
}
43
39
44
40
[[nodiscard]]
45
- const vk::raii::DescriptorSetLayout& descriptor_set_layout () const { return m_descriptor_set_layout ; }
41
+ const std::vector< vk::raii::DescriptorSetLayout>& descriptor_set_layouts () const { return m_descriptor_set_layouts ; }
46
42
[[nodiscard]]
47
43
const vk::raii::PipelineLayout& pipeline_layout () const { return m_pipeline_layout; }
48
44
[[nodiscard]]
@@ -55,28 +51,30 @@ export namespace vht {
55
51
}
56
52
// 创建描述符集布局
57
53
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 );
54
+ vk::DescriptorSetLayoutBinding uboLayoutBinding;
55
+ uboLayoutBinding.binding = 0 ;
56
+ uboLayoutBinding.descriptorType = vk::DescriptorType::eUniformBuffer;
57
+ uboLayoutBinding.descriptorCount = 1 ;
58
+ uboLayoutBinding.stageFlags = vk::ShaderStageFlagBits::eVertex;
59
+
60
+ vk::DescriptorSetLayoutCreateInfo uboLayoutInfo;
61
+ uboLayoutInfo.setBindings ( uboLayoutBinding );
62
+ m_descriptor_set_layouts.emplace_back ( m_device->device ().createDescriptorSetLayout ( uboLayoutInfo ) );
63
+
64
+ vk::DescriptorSetLayoutBinding samplerLayoutBinding;
65
+ samplerLayoutBinding.binding = 0 ;
66
+ samplerLayoutBinding.descriptorType = vk::DescriptorType::eCombinedImageSampler;
67
+ samplerLayoutBinding.descriptorCount = 1 ;
68
+ samplerLayoutBinding.stageFlags = vk::ShaderStageFlagBits::eFragment;
69
+ vk::DescriptorSetLayoutCreateInfo samplerLayoutInfo;
70
+
71
+ samplerLayoutInfo.setBindings ( samplerLayoutBinding );
72
+ m_descriptor_set_layouts.emplace_back ( m_device->device ().createDescriptorSetLayout ( samplerLayoutInfo ) );
75
73
}
76
74
// 创建图形管线
77
75
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" );
76
+ const auto vertex_shader_code = vht::read_shader (" shaders/graphics. vert.spv" );
77
+ const auto fragment_shader_code = vht::read_shader (" shaders/graphics. frag.spv" );
80
78
const auto vertex_shader_module = vht::create_shader_module (m_device->device (), vertex_shader_code);
81
79
const auto fragment_shader_module = vht::create_shader_module (m_device->device (), fragment_shader_code);
82
80
vk::PipelineShaderStageCreateInfo vertex_shader_create_info;
@@ -93,24 +91,26 @@ export namespace vht {
93
91
mapEntry.size = sizeof (float );
94
92
// 3. 特化信息
95
93
vk::SpecializationInfo specializationInfo;
96
- specializationInfo.setMapEntries (mapEntry);
94
+ specializationInfo.setMapEntries (mapEntry); // 如果需要,可以设置多个映射条目
97
95
specializationInfo.setData <float >(my_color);
98
96
// 此模板设置了 指针 和 数据大小 ,不能放右值
99
97
100
- vk::PipelineShaderStageCreateInfo fragment_shader_create_info; // 片段着色器
98
+ vk::PipelineShaderStageCreateInfo fragment_shader_create_info;
101
99
fragment_shader_create_info.stage = vk::ShaderStageFlagBits::eFragment;
102
100
fragment_shader_create_info.module = fragment_shader_module;
103
101
fragment_shader_create_info.pName = " main" ;
104
- fragment_shader_create_info.pSpecializationInfo = &specializationInfo; // 特化信息
102
+
103
+ // 直接在着色器创建信息中绑定“特化信息”
104
+ fragment_shader_create_info.pSpecializationInfo = &specializationInfo;
105
105
106
106
const auto shader_stages = { vertex_shader_create_info, fragment_shader_create_info };
107
107
108
108
const auto dynamic_states = { vk::DynamicState::eViewport, vk::DynamicState::eScissor };
109
109
vk::PipelineDynamicStateCreateInfo dynamic_state;
110
110
dynamic_state.setDynamicStates (dynamic_states);
111
111
112
- auto binding_description = vht::Vertex::get_binding_description ();
113
- auto attribute_description = vht::Vertex::get_attribute_description ();
112
+ const auto binding_description = vht::Vertex::get_binding_description ();
113
+ const auto attribute_description = vht::Vertex::get_attribute_description ();
114
114
vk::PipelineVertexInputStateCreateInfo vertex_input;
115
115
vertex_input.setVertexBindingDescriptions (binding_description);
116
116
vertex_input.setVertexAttributeDescriptions (attribute_description);
@@ -152,15 +152,19 @@ export namespace vht {
152
152
color_blend.setAttachments ( color_blend_attachment );
153
153
154
154
vk::PipelineLayoutCreateInfo layout_create_info;
155
- layout_create_info.setSetLayouts ( *m_descriptor_set_layout );
155
+
156
+ // 将 raii 类型转换回 vk::DescriptorSetLayout
157
+ const auto set_layouts = m_descriptor_set_layouts
158
+ | std::views::transform ([](const auto & layout) -> vk::DescriptorSetLayout { return layout; })
159
+ | std::ranges::to<std::vector>();
160
+
161
+ layout_create_info.setSetLayouts ( set_layouts );
156
162
m_pipeline_layout = m_device->device ().createPipelineLayout ( layout_create_info );
157
163
158
164
vk::GraphicsPipelineCreateInfo create_info;
159
- create_info.setStages ( shader_stages );
160
-
161
165
create_info.layout = m_pipeline_layout;
162
166
163
-
167
+ create_info. setStages ( shader_stages );
164
168
create_info.pVertexInputState = &vertex_input;
165
169
create_info.pInputAssemblyState = &input_assembly;
166
170
create_info.pDynamicState = &dynamic_state;
0 commit comments