Blender V5.0
vk_query.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "vk_query.hh"
10#include "vk_backend.hh"
11#include "vk_context.hh"
12
13#include "GPU_debug.hh"
14
15namespace blender::gpu {
16
18{
19 VKBackend &backend = VKBackend::get();
20 const VKDevice &device = backend.device;
21
22 while (!vk_query_pools_.is_empty()) {
23 VkQueryPool vk_query_pool = vk_query_pools_.pop_last();
24 vkDestroyQueryPool(device.vk_handle(), vk_query_pool, nullptr);
25 }
26}
27
28uint32_t VKQueryPool::query_index_in_pool() const
29{
30 return queries_issued_ - (vk_query_pools_.size() - 1) * query_chunk_len_;
31}
32
34{
35 BLI_assert(vk_query_pools_.is_empty());
36 queries_issued_ = 0;
37 vk_query_type_ = to_vk_query_type(type);
38}
39
41{
42 VKBackend &backend = VKBackend::get();
43 const VKDevice &device = backend.device;
44
45 uint32_t pool_index = queries_issued_ / query_chunk_len_;
46 bool is_new_pool = (queries_issued_ % query_chunk_len_) == 0;
47
48 if (pool_index == vk_query_pools_.size()) {
49 BLI_assert(is_new_pool);
50
51 VkQueryPoolCreateInfo create_info = {};
52 create_info.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
53 create_info.queryType = vk_query_type_;
54 create_info.queryCount = query_chunk_len_;
55
56 VkQueryPool vk_query_pool = VK_NULL_HANDLE;
57 vkCreateQueryPool(device.vk_handle(), &create_info, nullptr, &vk_query_pool);
58 vk_query_pools_.append(vk_query_pool);
59 }
60 BLI_assert(pool_index < vk_query_pools_.size());
61
62 /* When using a new query pool make sure to reset it before first usage. */
63 VKContext &context = *VKContext::get();
64 VkQueryPool vk_query_pool = vk_query_pools_[pool_index];
65 if (is_new_pool) {
66 render_graph::VKResetQueryPoolNode::Data reset_query_pool = {};
67 reset_query_pool.vk_query_pool = vk_query_pool;
68 reset_query_pool.first_query = 0;
69 reset_query_pool.query_count = query_chunk_len_;
70 context.render_graph().add_node(reset_query_pool);
71 }
72
74 begin_query.vk_query_pool = vk_query_pool;
75 begin_query.query_index = query_index_in_pool();
76 context.render_graph().add_node(begin_query);
77}
78
80{
81 VKContext &context = *VKContext::get();
83 end_query.vk_query_pool = vk_query_pools_.last();
84 end_query.query_index = query_index_in_pool();
85 context.render_graph().add_node(end_query);
86 queries_issued_ += 1;
87}
88
90{
91 VKContext &context = *VKContext::get();
92 /* During selection the frame buffer is still rendering. It needs to finish the render scope to
93 * ensure the END_RENDERING node */
94 context.rendering_end();
95 context.flush_render_graph(RenderGraphFlushFlags::SUBMIT |
98
99 int queries_left = queries_issued_;
100 int pool_index = 0;
101 VKBackend &backend = VKBackend::get();
102 const VKDevice &device = backend.device;
103 while (queries_left) {
104 VkQueryPool vk_query_pool = vk_query_pools_[pool_index];
105 uint32_t *r_values_chunk = &r_values[pool_index * query_chunk_len_];
106 uint32_t values_chunk_size = min_ii(queries_left, query_chunk_len_);
107 vkGetQueryPoolResults(device.vk_handle(),
108 vk_query_pool,
109 0,
110 values_chunk_size,
111 values_chunk_size * sizeof(uint32_t),
112 r_values_chunk,
113 sizeof(uint32_t),
114 VK_QUERY_RESULT_WAIT_BIT);
115
116 queries_left = max_ii(queries_left - query_chunk_len_, 0);
117 pool_index += 1;
118 }
119}
120
121} // namespace blender::gpu
#define BLI_assert(a)
Definition BLI_assert.h:46
MINLINE int min_ii(int a, int b)
MINLINE int max_ii(int a, int b)
int64_t size() const
static VKBackend & get()
Definition vk_backend.hh:91
static VKContext * get()
VkDevice vk_handle() const
Definition vk_device.hh:311
void init(GPUQueryType type) override
Definition vk_query.cc:33
void end_query() override
Definition vk_query.cc:79
void begin_query() override
Definition vk_query.cc:40
void get_occlusion_result(MutableSpan< uint32_t > r_values) override
Definition vk_query.cc:89
VkQueryType to_vk_query_type(const GPUQueryType query_type)
Definition vk_common.cc:465