1
- module ;
2
-
3
- #include < memory>
4
- #include < array>
5
- #include < vector>
6
- #include < stdexcept>
7
-
8
1
export module Drawer;
9
2
3
+ import std;
10
4
import vulkan_hpp;
11
5
12
6
import Config;
@@ -54,7 +48,7 @@ export namespace vht {
54
48
std::shared_ptr<vht::InputAssembly> m_input_assembly{ nullptr };
55
49
std::shared_ptr<vht::UniformBuffer> m_uniform_buffer{ nullptr };
56
50
std::shared_ptr<vht::Descriptor> m_descriptor{ nullptr };
57
- std::shared_ptr<vht::QueryPool> m_query_pool; // 添加查询池成员
51
+ std::shared_ptr<vht::QueryPool> m_query_pool{ nullptr };
58
52
std::vector<vk::raii::Semaphore> m_image_available_semaphores;
59
53
std::vector<vk::raii::Semaphore> m_render_finished_semaphores;
60
54
std::vector<vk::raii::Fence> m_in_flight_fences;
@@ -72,7 +66,7 @@ export namespace vht {
72
66
std::shared_ptr<vht::InputAssembly> input_assembly,
73
67
std::shared_ptr<vht::UniformBuffer> uniform_buffer,
74
68
std::shared_ptr<vht::Descriptor> descriptor,
75
- std::shared_ptr<vht::QueryPool> query_pool // 添加查询池参数
69
+ std::shared_ptr<vht::QueryPool> query_pool
76
70
): m_data_loader(std::move(data_loader)),
77
71
m_window(std::move(window)),
78
72
m_device(std::move(device)),
@@ -90,14 +84,14 @@ export namespace vht {
90
84
91
85
void draw () {
92
86
// 等待当前帧的栅栏,即确保上一个帧的绘制完成
93
- if ( const auto res = m_device->device ().waitForFences ( *m_in_flight_fences[m_current_frame], true , UINT64_MAX );
87
+ if ( const auto res = m_device->device ().waitForFences ( *m_in_flight_fences[m_current_frame], true , std::numeric_limits<std:: uint64_t >:: max () );
94
88
res != vk::Result::eSuccess
95
89
) throw std::runtime_error{ " waitForFences in drawFrame was failed" };
96
90
97
91
// 获取交换链的下一个图像索引
98
- uint32_t image_index;
92
+ std:: uint32_t image_index;
99
93
try {
100
- auto [res, idx] = m_swapchain->swapchain ().acquireNextImage (UINT64_MAX , m_image_available_semaphores[m_current_frame]);
94
+ auto [res, idx] = m_swapchain->swapchain ().acquireNextImage (std::numeric_limits<std:: uint64_t >:: max () , m_image_available_semaphores[m_current_frame]);
101
95
image_index = idx;
102
96
} catch (const vk::OutOfDateKHRError&){
103
97
m_render_pass->recreate ();
@@ -141,8 +135,8 @@ export namespace vht {
141
135
142
136
m_device->graphics_queue ().waitIdle ();
143
137
static int counter = 0 ;
144
- if ( ++counter % 2500 == 0 ){
145
- m_query_pool->print_delta_time ();
138
+ if ( ++counter % 2500 == 0 ){ // 2500 帧输出一次,可自行调整
139
+ m_query_pool->print_delta_time (); // 输出单次绘制耗时
146
140
m_query_pool->print_statistics ();
147
141
m_query_pool->print_occlusion ();
148
142
}
@@ -169,30 +163,35 @@ export namespace vht {
169
163
m_image_available_semaphores.reserve ( MAX_FRAMES_IN_FLIGHT );
170
164
m_render_finished_semaphores.reserve ( MAX_FRAMES_IN_FLIGHT );
171
165
m_in_flight_fences.reserve ( MAX_FRAMES_IN_FLIGHT );
172
- for (size_t i = 0 ; i < MAX_FRAMES_IN_FLIGHT; ++i){
166
+ for (std:: size_t i = 0 ; i < MAX_FRAMES_IN_FLIGHT; ++i){
173
167
m_image_available_semaphores.emplace_back ( m_device->device (), semaphore_create_info );
174
168
m_render_finished_semaphores.emplace_back ( m_device->device (), semaphore_create_info );
175
169
m_in_flight_fences.emplace_back ( m_device->device () , fence_create_info );
176
170
}
177
171
}
178
172
// 记录命令缓冲区
179
- void record_command_buffer (const vk::raii::CommandBuffer& command_buffer, const uint32_t image_index) const {
173
+ void record_command_buffer (const vk::raii::CommandBuffer& command_buffer, const std:: uint32_t image_index) const {
180
174
command_buffer.begin ( vk::CommandBufferBeginInfo{} );
181
- // 可用性查询
175
+
182
176
command_buffer.resetQueryPool ( m_query_pool->occlusion (), 0 , 1 );
183
177
command_buffer.beginQuery (m_query_pool->occlusion (), 0 );
184
- // 管线统计查询
178
+
185
179
command_buffer.resetQueryPool ( m_query_pool->statistics (), 0 , 1 );
186
- command_buffer.beginQuery (m_query_pool->statistics (), 0 );
187
- // 时间戳查询
188
- // 重置查询池,索引 0 和 1
180
+ command_buffer.beginQuery (m_query_pool->statistics (), 0 ); // 绑定管线统计信息查询
181
+
189
182
command_buffer.resetQueryPool ( m_query_pool->timestamp (), 0 , 2 );
190
- command_buffer.writeTimestamp (
191
- vk::PipelineStageFlagBits::eTopOfPipe, // 在管线顶部写入时间戳
183
+ command_buffer.writeTimestamp ( // 在管线顶部写入时间戳
184
+ vk::PipelineStageFlagBits::eTopOfPipe,
192
185
m_query_pool->timestamp (), // 查询池
193
186
0 // 查询池内部的查询索引
194
187
);
195
-
188
+ // 你可以使用 `writeTimestamp2` ,这需要启用 `synchronization2` 特性
189
+ // command_buffer.writeTimestamp2(
190
+ // vk::PipelineStageFlagBits2::eNone,
191
+ // m_query_pool->timestamp(), // 查询池
192
+ // 0 // 查询池内部的查询索引
193
+ // );
194
+ // 重置查询池,起始索引 0, 数量 1
196
195
197
196
198
197
vk::RenderPassBeginInfo render_pass_begin_info;
@@ -228,24 +227,30 @@ export namespace vht {
228
227
command_buffer.bindVertexBuffers ( 0 , *m_input_assembly->vertex_buffer (), vk::DeviceSize{ 0 } );
229
228
command_buffer.bindIndexBuffer ( m_input_assembly->index_buffer (), 0 , vk::IndexType::eUint32 );
230
229
230
+ const std::array<vk::DescriptorSet,2 > descriptor_sets = {
231
+ m_descriptor->ubo_sets ()[m_current_frame],
232
+ m_descriptor->texture_set ()
233
+ };
234
+
231
235
command_buffer.bindDescriptorSets (
232
236
vk::PipelineBindPoint::eGraphics,
233
237
m_graphics_pipeline->pipeline_layout (),
234
238
0 ,
235
- *m_descriptor-> sets ()[m_current_frame] ,
239
+ descriptor_sets ,
236
240
nullptr
237
241
);
238
242
239
- command_buffer.drawIndexed (static_cast <uint32_t >(m_data_loader->indices ().size ()), 1 , 0 , 0 , 0 );
243
+ command_buffer.drawIndexed (static_cast <std:: uint32_t >(m_data_loader->indices ().size ()), 1 , 0 , 0 , 0 );
240
244
241
245
command_buffer.endRenderPass ();
242
246
243
- command_buffer.writeTimestamp (
247
+ command_buffer.writeTimestamp ( // 在命令最后写入时间戳
244
248
vk::PipelineStageFlagBits::eBottomOfPipe,
245
- m_query_pool->timestamp (), 1 // 结束时间戳,索引1
249
+ m_query_pool->timestamp (), 1 // 索引 1
246
250
);
247
251
command_buffer.endQuery (m_query_pool->statistics (), 0 );
248
252
command_buffer.endQuery (m_query_pool->occlusion (), 0 );
253
+
249
254
command_buffer.end ();
250
255
}
251
256
};
0 commit comments