• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Android Open Source Project
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 expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <vulkan/vulkan.h>
18 
19 #include "goldfish_vk_dispatch.h"
20 
21 namespace gfxstream {
22 namespace vk {
23 
24 class DebugUtilsHelper {
25    public:
26     static DebugUtilsHelper withUtilsEnabled(VkDevice device, const VulkanDispatch* dispatch);
27 
28     static DebugUtilsHelper withUtilsDisabled();
29 
30     ~DebugUtilsHelper() = default;
31 
32     template <typename VkObjectT, typename... T>
addDebugLabel(VkObjectT object,const char * format,T &&...formatArgs)33     void addDebugLabel(VkObjectT object, const char* format, T&&... formatArgs) const {
34         if (!m_debugUtilsEnabled) {
35             return;
36         }
37 
38         VkObjectType objectType = VK_OBJECT_TYPE_UNKNOWN;
39         if constexpr (std::is_same_v<VkObjectT, VkBuffer>) {
40             objectType = VK_OBJECT_TYPE_BUFFER;
41         } else if constexpr (std::is_same_v<VkObjectT, VkCommandBuffer>) {
42             objectType = VK_OBJECT_TYPE_COMMAND_BUFFER;
43         } else if constexpr (std::is_same_v<VkObjectT, VkCommandPool>) {
44             objectType = VK_OBJECT_TYPE_COMMAND_POOL;
45         } else if constexpr (std::is_same_v<VkObjectT, VkDescriptorSet>) {
46             objectType = VK_OBJECT_TYPE_DESCRIPTOR_SET;
47         } else if constexpr (std::is_same_v<VkObjectT, VkDeviceMemory>) {
48             objectType = VK_OBJECT_TYPE_DEVICE_MEMORY;
49         } else if constexpr (std::is_same_v<VkObjectT, VkFence>) {
50             objectType = VK_OBJECT_TYPE_FENCE;
51         } else if constexpr (std::is_same_v<VkObjectT, VkFramebuffer>) {
52             objectType = VK_OBJECT_TYPE_FRAMEBUFFER;
53         } else if constexpr (std::is_same_v<VkObjectT, VkImage>) {
54             objectType = VK_OBJECT_TYPE_IMAGE;
55         } else if constexpr (std::is_same_v<VkObjectT, VkImageView>) {
56             objectType = VK_OBJECT_TYPE_IMAGE_VIEW;
57         } else if constexpr (std::is_same_v<VkObjectT, VkPipeline>) {
58             objectType = VK_OBJECT_TYPE_PIPELINE;
59         } else if constexpr (std::is_same_v<VkObjectT, VkSampler>) {
60             objectType = VK_OBJECT_TYPE_SAMPLER;
61         } else {
62             static_assert(sizeof(VkObjectT) == 0,
63                           "Unhandled VkObjectT. Please update DebugUtilsHelper.h.");
64         }
65 
66         addDebugLabelToHandle((uint64_t)object, objectType, format, std::forward<T>(formatArgs)...);
67     }
68 
69     void cmdBeginDebugLabel(VkCommandBuffer commandBuffer, const char* format, ...) const;
70     void cmdEndDebugLabel(VkCommandBuffer commandBuffer) const;
71 
isEnabled()72     bool isEnabled() const { return m_debugUtilsEnabled; }
73 
74    private:
75     DebugUtilsHelper(bool enabled, VkDevice device, const VulkanDispatch* dispatch);
76 
77     void addDebugLabelToHandle(uint64_t object, VkObjectType objectType, const char* format,
78                                ...) const;
79 
80     bool m_debugUtilsEnabled = false;
81     VkDevice m_vkDevice = VK_NULL_HANDLE;
82     const VulkanDispatch* m_vk = nullptr;
83 };
84 
85 }  // namespace vk
86 }  // namespace gfxstream
87