• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Dawn Authors
2 //
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 #include "dawn_native/vulkan/QuerySetVk.h"
16 
17 #include "dawn_native/vulkan/DeviceVk.h"
18 #include "dawn_native/vulkan/FencedDeleter.h"
19 #include "dawn_native/vulkan/UtilsVulkan.h"
20 #include "dawn_native/vulkan/VulkanError.h"
21 #include "dawn_platform/DawnPlatform.h"
22 
23 namespace dawn_native { namespace vulkan {
24 
25     namespace {
VulkanQueryType(wgpu::QueryType type)26         VkQueryType VulkanQueryType(wgpu::QueryType type) {
27             switch (type) {
28                 case wgpu::QueryType::Occlusion:
29                     return VK_QUERY_TYPE_OCCLUSION;
30                 case wgpu::QueryType::PipelineStatistics:
31                     return VK_QUERY_TYPE_PIPELINE_STATISTICS;
32                 case wgpu::QueryType::Timestamp:
33                     return VK_QUERY_TYPE_TIMESTAMP;
34             }
35             UNREACHABLE();
36         }
37 
VulkanQueryPipelineStatisticFlags(std::vector<wgpu::PipelineStatisticName> pipelineStatisticsSet)38         VkQueryPipelineStatisticFlags VulkanQueryPipelineStatisticFlags(
39             std::vector<wgpu::PipelineStatisticName> pipelineStatisticsSet) {
40             VkQueryPipelineStatisticFlags pipelineStatistics = 0;
41             for (size_t i = 0; i < pipelineStatisticsSet.size(); ++i) {
42                 switch (pipelineStatisticsSet[i]) {
43                     case wgpu::PipelineStatisticName::ClipperInvocations:
44                         pipelineStatistics |= VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT;
45                         break;
46                     case wgpu::PipelineStatisticName::ClipperPrimitivesOut:
47                         pipelineStatistics |= VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT;
48                         break;
49                     case wgpu::PipelineStatisticName::ComputeShaderInvocations:
50                         pipelineStatistics |=
51                             VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT;
52                         break;
53                     case wgpu::PipelineStatisticName::FragmentShaderInvocations:
54                         pipelineStatistics |=
55                             VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT;
56                         break;
57                     case wgpu::PipelineStatisticName::VertexShaderInvocations:
58                         pipelineStatistics |=
59                             VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT;
60                         break;
61                 }
62             }
63 
64             return pipelineStatistics;
65         }
66     }  // anonymous namespace
67 
68     // static
Create(Device * device,const QuerySetDescriptor * descriptor)69     ResultOrError<Ref<QuerySet>> QuerySet::Create(Device* device,
70                                                   const QuerySetDescriptor* descriptor) {
71         Ref<QuerySet> queryset = AcquireRef(new QuerySet(device, descriptor));
72         DAWN_TRY(queryset->Initialize());
73         return queryset;
74     }
75 
Initialize()76     MaybeError QuerySet::Initialize() {
77         VkQueryPoolCreateInfo createInfo;
78         createInfo.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
79         createInfo.pNext = NULL;
80         createInfo.flags = 0;
81         createInfo.queryType = VulkanQueryType(GetQueryType());
82         createInfo.queryCount = std::max(GetQueryCount(), uint32_t(1u));
83         if (GetQueryType() == wgpu::QueryType::PipelineStatistics) {
84             createInfo.pipelineStatistics =
85                 VulkanQueryPipelineStatisticFlags(GetPipelineStatistics());
86         }
87 
88         Device* device = ToBackend(GetDevice());
89         DAWN_TRY(CheckVkOOMThenSuccess(
90             device->fn.CreateQueryPool(device->GetVkDevice(), &createInfo, nullptr, &*mHandle),
91             "vkCreateQueryPool"));
92 
93         SetLabelImpl();
94 
95         return {};
96     }
97 
GetHandle() const98     VkQueryPool QuerySet::GetHandle() const {
99         return mHandle;
100     }
101 
102     QuerySet::~QuerySet() = default;
103 
DestroyImpl()104     void QuerySet::DestroyImpl() {
105         QuerySetBase::DestroyImpl();
106         if (mHandle != VK_NULL_HANDLE) {
107             ToBackend(GetDevice())->GetFencedDeleter()->DeleteWhenUnused(mHandle);
108             mHandle = VK_NULL_HANDLE;
109         }
110     }
111 
SetLabelImpl()112     void QuerySet::SetLabelImpl() {
113         SetDebugName(ToBackend(GetDevice()), VK_OBJECT_TYPE_QUERY_POOL,
114                      reinterpret_cast<uint64_t&>(mHandle), "Dawn_QuerySet", GetLabel());
115     }
116 
117 }}  // namespace dawn_native::vulkan
118