1- module ;
2-
3- #include < memory>
4- #include < array>
5- #include < vector>
6- #include < stdexcept>
7-
81export module Drawer;
92
3+ import std;
104import vulkan_hpp;
115
126import Config;
@@ -54,7 +48,7 @@ export namespace vht {
5448 std::shared_ptr<vht::InputAssembly> m_input_assembly{ nullptr };
5549 std::shared_ptr<vht::UniformBuffer> m_uniform_buffer{ nullptr };
5650 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 };
5852 std::vector<vk::raii::Semaphore> m_image_available_semaphores;
5953 std::vector<vk::raii::Semaphore> m_render_finished_semaphores;
6054 std::vector<vk::raii::Fence> m_in_flight_fences;
@@ -72,7 +66,7 @@ export namespace vht {
7266 std::shared_ptr<vht::InputAssembly> input_assembly,
7367 std::shared_ptr<vht::UniformBuffer> uniform_buffer,
7468 std::shared_ptr<vht::Descriptor> descriptor,
75- std::shared_ptr<vht::QueryPool> query_pool // 添加查询池参数
69+ std::shared_ptr<vht::QueryPool> query_pool
7670 ): m_data_loader(std::move(data_loader)),
7771 m_window(std::move(window)),
7872 m_device(std::move(device)),
@@ -90,14 +84,14 @@ export namespace vht {
9084
9185 void draw () {
9286 // 等待当前帧的栅栏,即确保上一个帧的绘制完成
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 () );
9488 res != vk::Result::eSuccess
9589 ) throw std::runtime_error{ " waitForFences in drawFrame was failed" };
9690
9791 // 获取交换链的下一个图像索引
98- uint32_t image_index;
92+ std:: uint32_t image_index;
9993 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]);
10195 image_index = idx;
10296 } catch (const vk::OutOfDateKHRError&){
10397 m_render_pass->recreate ();
@@ -141,8 +135,8 @@ export namespace vht {
141135
142136 m_device->graphics_queue ().waitIdle ();
143137 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 (); // 输出单次绘制耗时
146140 m_query_pool->print_statistics ();
147141 m_query_pool->print_occlusion ();
148142 }
@@ -169,30 +163,35 @@ export namespace vht {
169163 m_image_available_semaphores.reserve ( MAX_FRAMES_IN_FLIGHT );
170164 m_render_finished_semaphores.reserve ( MAX_FRAMES_IN_FLIGHT );
171165 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){
173167 m_image_available_semaphores.emplace_back ( m_device->device (), semaphore_create_info );
174168 m_render_finished_semaphores.emplace_back ( m_device->device (), semaphore_create_info );
175169 m_in_flight_fences.emplace_back ( m_device->device () , fence_create_info );
176170 }
177171 }
178172 // 记录命令缓冲区
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 {
180174 command_buffer.begin ( vk::CommandBufferBeginInfo{} );
181- // 可用性查询
175+
182176 command_buffer.resetQueryPool ( m_query_pool->occlusion (), 0 , 1 );
183177 command_buffer.beginQuery (m_query_pool->occlusion (), 0 );
184- // 管线统计查询
178+
185179 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+
189182 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,
192185 m_query_pool->timestamp (), // 查询池
193186 0 // 查询池内部的查询索引
194187 );
195-
188+ // 你可以使用 `writeTimestamp2` ,这需要启用 `synchronization2` 特性
189+ // command_buffer.writeTimestamp2(
190+ // vk::PipelineStageFlagBits2::eNone,
191+ // m_query_pool->timestamp(), // 查询池
192+ // 0 // 查询池内部的查询索引
193+ // );
194+ // 重置查询池,起始索引 0, 数量 1
196195
197196
198197 vk::RenderPassBeginInfo render_pass_begin_info;
@@ -228,24 +227,30 @@ export namespace vht {
228227 command_buffer.bindVertexBuffers ( 0 , *m_input_assembly->vertex_buffer (), vk::DeviceSize{ 0 } );
229228 command_buffer.bindIndexBuffer ( m_input_assembly->index_buffer (), 0 , vk::IndexType::eUint32 );
230229
230+ const std::array<vk::DescriptorSet,2 > descriptor_sets = {
231+ m_descriptor->ubo_sets ()[m_current_frame],
232+ m_descriptor->texture_set ()
233+ };
234+
231235 command_buffer.bindDescriptorSets (
232236 vk::PipelineBindPoint::eGraphics,
233237 m_graphics_pipeline->pipeline_layout (),
234238 0 ,
235- *m_descriptor-> sets ()[m_current_frame] ,
239+ descriptor_sets ,
236240 nullptr
237241 );
238242
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 );
240244
241245 command_buffer.endRenderPass ();
242246
243- command_buffer.writeTimestamp (
247+ command_buffer.writeTimestamp ( // 在命令最后写入时间戳
244248 vk::PipelineStageFlagBits::eBottomOfPipe,
245- m_query_pool->timestamp (), 1 // 结束时间戳,索引1
249+ m_query_pool->timestamp (), 1 // 索引 1
246250 );
247251 command_buffer.endQuery (m_query_pool->statistics (), 0 );
248252 command_buffer.endQuery (m_query_pool->occlusion (), 0 );
253+
249254 command_buffer.end ();
250255 }
251256 };
0 commit comments