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