• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 auto queryType = (VkQueryType)desc_.queryType;
34     const auto pipelineStatisticsFlags = (VkQueryPipelineStatisticFlags)desc_.queryPipelineStatisticsFlags;
35     const VkQueryPoolCreateInfo createInfo {
36         VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO, // sType
37         nullptr,                                  // pNext
38         createFlags,                              // flags
39         queryType,                                // queryType
40         QUERY_COUNT_PER_POOL,                     // queryCount
41         pipelineStatisticsFlags,                  // pipelineStatistics
42     };
43 
44     const VkDevice vkDevice = ((const DevicePlatformDataVk&)device_.GetPlatformData()).device;
45     for (auto& ref : plats_) {
46         VALIDATE_VK_RESULT(vkCreateQueryPool(vkDevice, // device
47             &createInfo,                               // pCreateInfo
48             nullptr,                                   // pAllocator
49             &ref.queryPool));                          // pQueryPool
50     }
51 }
52 
~GpuQueryVk()53 GpuQueryVk::~GpuQueryVk()
54 {
55     const VkDevice device = ((const DevicePlatformDataVk&)device_.GetPlatformData()).device;
56     for (auto& ref : plats_) {
57         if (ref.queryPool) {
58             vkDestroyQueryPool(device, // device
59                 ref.queryPool,         // queryPool
60                 nullptr);              // pAllocator
61         }
62     }
63 }
64 
NextQueryIndex()65 void GpuQueryVk::NextQueryIndex()
66 {
67     queryIndex_ = (queryIndex_ + 1) % ((uint32_t)plats_.size());
68 }
69 
GetDesc() const70 const GpuQueryDesc& GpuQueryVk::GetDesc() const
71 {
72     return desc_;
73 }
74 
GetPlatformData() const75 const GpuQueryPlatformData& GpuQueryVk::GetPlatformData() const
76 {
77     return plats_[queryIndex_];
78 }
79 
CreateGpuQueryVk(Device & device,const GpuQueryDesc & desc)80 BASE_NS::unique_ptr<GpuQuery> CreateGpuQueryVk(Device& device, const GpuQueryDesc& desc)
81 {
82     return BASE_NS::make_unique<GpuQueryVk>(device, desc);
83 }
84 RENDER_END_NAMESPACE()
85