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 "vulkan/cereal/common/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, VkCommandBuffer>) { 40 objectType = VK_OBJECT_TYPE_COMMAND_BUFFER; 41 } else if constexpr(std::is_same_v<VkObjectT, VkCommandPool>) { 42 objectType = VK_OBJECT_TYPE_COMMAND_POOL; 43 } else if constexpr(std::is_same_v<VkObjectT, VkDescriptorSet>) { 44 objectType = VK_OBJECT_TYPE_DESCRIPTOR_SET; 45 } else if constexpr(std::is_same_v<VkObjectT, VkFramebuffer>) { 46 objectType = VK_OBJECT_TYPE_FRAMEBUFFER; 47 } else if constexpr(std::is_same_v<VkObjectT, VkImage>) { 48 objectType = VK_OBJECT_TYPE_IMAGE; 49 } else if constexpr(std::is_same_v<VkObjectT, VkImageView>) { 50 objectType = VK_OBJECT_TYPE_IMAGE_VIEW; 51 } else if constexpr(std::is_same_v<VkObjectT, VkPipeline>) { 52 objectType = VK_OBJECT_TYPE_PIPELINE; 53 } else if constexpr(std::is_same_v<VkObjectT, VkSampler>) { 54 objectType = VK_OBJECT_TYPE_SAMPLER; 55 } else { 56 static_assert(sizeof(VkObjectT) == 0, 57 "Unhandled VkObjectT. Please update DebugUtilsHelper.h."); 58 } 59 60 addDebugLabelToHandle((uint64_t)object, objectType, format, std::forward<T>(formatArgs)...); 61 } 62 63 void cmdBeginDebugLabel(VkCommandBuffer commandBuffer, const char* format, ...) const; 64 void cmdEndDebugLabel(VkCommandBuffer commandBuffer) const; 65 66 private: 67 DebugUtilsHelper(bool enabled, VkDevice device, const VulkanDispatch* dispatch); 68 69 void addDebugLabelToHandle(uint64_t object, VkObjectType objectType, const char* format, 70 ...) const; 71 72 bool m_debugUtilsEnabled = false; 73 VkDevice m_vkDevice = VK_NULL_HANDLE; 74 const VulkanDispatch* m_vk = nullptr; 75 }; 76 77 } // namespace vk 78 } // namespace gfxstream 79