1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "gpu_query_vk.h"
17 
18 #include <vulkan/vulkan_core.h>
19 
20 #include <render/namespace.h>
21 
22 #include "device/device.h"
23 #include "perf/gpu_query.h"
24 #include "vulkan/device_vk.h"
25 #include "vulkan/validate_vk.h"
26 
RENDER_BEGIN_NAMESPACE()27 RENDER_BEGIN_NAMESPACE()
28 GpuQueryVk::GpuQueryVk(Device& device, const GpuQueryDesc& desc) : device_(device), desc_(desc)
29 {
30     plats_.resize(device_.GetCommandBufferingCount() + 1);
31 
32     constexpr VkQueryPoolCreateFlags createFlags { 0 };
33     const VkQueryType queryType = (VkQueryType)desc_.queryType;
34     const VkQueryPipelineStatisticFlags pipelineStatisticsFlags =
35         (VkQueryPipelineStatisticFlags)desc_.queryPipelineStatisticsFlags;
36     const VkQueryPoolCreateInfo createInfo {
37         VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, // sType
38         nullptr,                                  // pNext
39         createFlags,                              // flags
40         queryType,                                // queryType
41         QUERY_COUNT_PER_POOL,                     // queryCount
42         pipelineStatisticsFlags,                  // pipelineStatistics
43     };
44 
45     const VkDevice vkDevice = ((const DevicePlatformDataVk&)device_.GetPlatformData()).device;
46     for (auto& ref : plats_) {
47         VALIDATE_VK_RESULT(vkCreateQueryPool(vkDevice, // device
48             &createInfo,                               // pCreateInfo
49             nullptr,                                   // pAllocator
50             &ref.queryPool));                          // pQueryPool
51     }
52 }
53 
~GpuQueryVk()54 GpuQueryVk::~GpuQueryVk()
55 {
56     const VkDevice device = ((const DevicePlatformDataVk&)device_.GetPlatformData()).device;
57     for (auto& ref : plats_) {
58         if (ref.queryPool) {
59             vkDestroyQueryPool(device, // device
60                 ref.queryPool,         // queryPool
61                 nullptr);              // pAllocator
62         }
63     }
64 }
65 
NextQueryIndex()66 void GpuQueryVk::NextQueryIndex()
67 {
68     queryIndex_ = (queryIndex_ + 1) % (static_cast<uint32_t>(plats_.size()));
69 }
70 
GetDesc() const71 const GpuQueryDesc& GpuQueryVk::GetDesc() const
72 {
73     return desc_;
74 }
75 
GetPlatformData() const76 const GpuQueryPlatformData& GpuQueryVk::GetPlatformData() const
77 {
78     return plats_[queryIndex_];
79 }
80 
CreateGpuQueryVk(Device & device,const GpuQueryDesc & desc)81 BASE_NS::unique_ptr<GpuQuery> CreateGpuQueryVk(Device& device, const GpuQueryDesc& desc)
82 {
83     return BASE_NS::make_unique<GpuQueryVk>(device, desc);
84 }
85 RENDER_END_NAMESPACE()
86