Skip to content

Commit c28f366

Browse files
committed
Add QueryPool
1 parent b6a46a3 commit c28f366

File tree

11 files changed

+182
-188
lines changed

11 files changed

+182
-188
lines changed

docs/codes/04/10_querypool/cppm/App.cppm renamed to docs/codes/04/14_querypool/App.cppm

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
module;
2-
3-
#include <iostream>
4-
#include <memory>
5-
6-
// #include <vulkan/vulkan.h>
7-
#include <GLFW/glfw3.h>
8-
91
export module App;
102

3+
import std;
4+
import glfw;
115
import vulkan_hpp;
126

137
import DataLoader;
@@ -41,53 +35,52 @@ export namespace vht {
4135
std::shared_ptr<vht::UniformBuffer> m_uniform_buffer{ nullptr };
4236
std::shared_ptr<vht::TextureSampler> m_texture_sampler{ nullptr };
4337
std::shared_ptr<vht::Descriptor> m_descriptor{ nullptr };
44-
std::shared_ptr<vht::QueryPool> m_query_pool; // 添加查询池成员
38+
std::shared_ptr<vht::QueryPool> m_query_pool{ nullptr };
4539
std::shared_ptr<vht::Drawer> m_drawer{ nullptr };
4640
public:
4741
void run() {
4842
init();
49-
std::cout << "begin draw" << std::endl;
50-
while (!glfwWindowShouldClose(m_window->ptr())) {
51-
glfwPollEvents();
43+
while (!glfw::window_should_close(m_window->ptr())) {
44+
glfw::poll_events();
5245
m_drawer->draw();
5346
}
54-
std::cout << "device waitIdle" << std::endl;
47+
std::println("device waitIdle");
5548
m_device->device().waitIdle();
56-
std::cout << "finished" << std::endl;
49+
std::println("finished");
5750
}
5851
private:
5952
void init() {
6053
init_data_loader();
61-
init_vulkan();
54+
init_context();
6255
init_window();
63-
std::cout << "window created" << std::endl;
56+
std::println("window created");
6457
init_device();
65-
std::cout << "device created" << std::endl;
58+
std::println("device created");
6659
init_swapchain();
67-
std::cout << "swapchain created" << std::endl;
60+
std::println("swapchain created");
6861
init_depth_image();
69-
std::cout << "depth image created" << std::endl;
62+
std::println("depth image created");
7063
init_render_pass();
71-
std::cout << "render pass created" << std::endl;
64+
std::println("render pass created");
7265
init_graphics_pipeline();
73-
std::cout << "graphics pipeline created" << std::endl;
66+
std::println("graphics pipeline created");
7467
init_command_pool();
75-
std::cout << "command pool created" << std::endl;
76-
init_input_assembly();
77-
std::cout << "input assembly created" << std::endl;
68+
std::println("command pool created");
7869
init_uniform_buffer();
79-
std::cout << "uniform buffer created" << std::endl;
70+
std::println("uniform buffer created");
8071
init_texture_sampler();
81-
std::cout << "texture sampler created" << std::endl;
72+
std::println("texture sampler created");
73+
init_input_assembly();
74+
std::println("input assembly created");
8275
init_descriptor();
83-
std::cout << "descriptor created" << std::endl;
76+
std::println("descriptor created");
8477
init_query_pool(); // 初始化查询池
85-
std::cout << "query pool created" << std::endl;
78+
std::println("query pool created");
8679
init_drawer();
87-
std::cout << "drawer created" << std::endl;
80+
std::println("drawer created");
8881
}
8982
void init_data_loader() { m_data_loader = std::make_shared<vht::DataLoader>(); }
90-
void init_vulkan() { m_context = std::make_shared<vht::Context>( true ); }
83+
void init_context() { m_context = std::make_shared<vht::Context>( true ); }
9184
void init_window() { m_window = std::make_shared<vht::Window>( m_context ); }
9285
void init_device() { m_device = std::make_shared<vht::Device>( m_context, m_window ); }
9386
void init_swapchain() { m_swapchain = std::make_shared<vht::Swapchain>( m_window, m_device ); }
@@ -112,9 +105,8 @@ export namespace vht {
112105
m_input_assembly,
113106
m_uniform_buffer,
114107
m_descriptor,
115-
m_query_pool // 传递查询池
108+
m_query_pool
116109
);
117110
}
118111
};
119112
}
120-

docs/codes/04/10_querypool/cppm/Device.cppm renamed to docs/codes/04/14_querypool/Device.cppm

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
module;
2-
3-
#include <array>
4-
#include <vector>
5-
#include <set>
6-
#include <memory>
7-
#include <stdexcept>
8-
#include <optional>
9-
101
export module Device;
112

3+
import std;
124
import vulkan_hpp;
135

146
import Context;
@@ -23,8 +15,8 @@ export namespace vht {
2315
* - is_complete(): 检查是否已找到所需的队列族索引
2416
*/
2517
struct QueueFamilyIndices {
26-
std::optional<uint32_t> graphics_family;
27-
std::optional<uint32_t> present_family;
18+
std::optional<std::uint32_t> graphics_family;
19+
std::optional<std::uint32_t> present_family;
2820
[[nodiscard]]
2921
bool is_complete() const {
3022
return graphics_family.has_value() && present_family.has_value();
@@ -72,7 +64,8 @@ export namespace vht {
7264
explicit Device(std::shared_ptr<vht::Context> context, std::shared_ptr<vht::Window> window)
7365
: m_context(std::move(context)),
7466
m_window(std::move(window)) {
75-
init();
67+
pick_physical_device();
68+
create_device();
7669
}
7770

7871
[[nodiscard]]
@@ -88,11 +81,6 @@ export namespace vht {
8881
[[nodiscard]]
8982
QueueFamilyIndices queue_family_indices() const { return m_queue_family_indices; }
9083
private:
91-
void init() {
92-
pick_physical_device();
93-
create_device();
94-
}
95-
9684
/**
9785
* @brief 挑选物理设备
9886
*/
@@ -153,7 +141,7 @@ export namespace vht {
153141
QueueFamilyIndices find_queue_families(const vk::raii::PhysicalDevice& physical_device) const {
154142
QueueFamilyIndices indices{};
155143
const auto queue_families = physical_device.getQueueFamilyProperties();
156-
for (uint32_t i = 0; const auto& queue_family : queue_families) {
144+
for (std::uint32_t i = 0; const auto& queue_family : queue_families) {
157145
if (queue_family.queueFlags & vk::QueueFlagBits::eGraphics) {
158146
indices.graphics_family = i;
159147
}
@@ -174,21 +162,21 @@ export namespace vht {
174162
m_queue_family_indices = find_queue_families(m_physical_device);
175163
const auto [graphics_family, present_family] = m_queue_family_indices;
176164

177-
std::set<uint32_t> unique_queue_families { graphics_family.value(), present_family.value() };
165+
std::set<std::uint32_t> unique_queue_families { graphics_family.value(), present_family.value() };
178166

179167
std::vector<vk::DeviceQueueCreateInfo> queue_create_infos;
180168
queue_create_infos.reserve(unique_queue_families.size());
181169
float queue_priority = 1.0f;
182-
for (const uint32_t queue_family : unique_queue_families) {
170+
for (const std::uint32_t queue_family : unique_queue_families) {
183171
vk::DeviceQueueCreateInfo queue_create_info;
184172
queue_create_info.queueFamilyIndex = queue_family;
185173
queue_create_info.setQueuePriorities( queue_priority );
186174
queue_create_infos.emplace_back( queue_create_info );
187175
}
188176

189177
vk::PhysicalDeviceFeatures features;
190-
features.pipelineStatisticsQuery = true;
191178
features.samplerAnisotropy = true;
179+
features.pipelineStatisticsQuery = true;
192180
vk::DeviceCreateInfo create_info;
193181
create_info.setQueueCreateInfos( queue_create_infos );
194182
create_info.setPEnabledFeatures( &features );

docs/codes/04/10_querypool/cppm/Drawer.cppm renamed to docs/codes/04/14_querypool/Drawer.cppm

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
module;
2-
3-
#include <memory>
4-
#include <array>
5-
#include <vector>
6-
#include <stdexcept>
7-
81
export module Drawer;
92

3+
import std;
104
import vulkan_hpp;
115

126
import 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

Comments
 (0)