object_info.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright (c) 2017-2025 The Khronos Group Inc.
  2. // Copyright (c) 2017-2019 Valve Corporation
  3. // Copyright (c) 2017-2019 LunarG, Inc.
  4. // Copyright (c) 2019 Collabora, Ltd.
  5. //
  6. // SPDX-License-Identifier: Apache-2.0 OR MIT
  7. //
  8. // Initial Authors: Mark Young <[email protected]>, Rylie Pavlik <[email protected]
  9. //
  10. /*!
  11. * @file
  12. *
  13. * The core of an XR_EXT_debug_utils implementation, used/shared by the loader and several SDK layers.
  14. */
  15. #pragma once
  16. #include "hex_and_handles.h"
  17. #include <openxr/openxr.h>
  18. #include <memory>
  19. #include <string>
  20. #include <unordered_map>
  21. #include <vector>
  22. struct XrSdkGenericObject {
  23. //! Type-erased handle value
  24. uint64_t handle;
  25. //! Kind of object this handle refers to
  26. XrObjectType type;
  27. /// Un-erase the type of the handle and get it properly typed again.
  28. ///
  29. /// Note: Does not check the type before doing it!
  30. template <typename HandleType>
  31. HandleType& GetTypedHandle() {
  32. return TreatIntegerAsHandle<HandleType&>(handle);
  33. }
  34. //! @overload
  35. template <typename HandleType>
  36. HandleType const& GetTypedHandle() const {
  37. return TreatIntegerAsHandle<HandleType&>(handle);
  38. }
  39. //! Create from a typed handle and object type
  40. template <typename T>
  41. XrSdkGenericObject(T h, XrObjectType t) : handle(MakeHandleGeneric(h)), type(t) {}
  42. //! Create from an untyped handle value (integer) and object type
  43. XrSdkGenericObject(uint64_t h, XrObjectType t) : handle(h), type(t) {}
  44. };
  45. struct XrSdkLogObjectInfo {
  46. //! Type-erased handle value
  47. uint64_t handle;
  48. //! Kind of object this handle refers to
  49. XrObjectType type;
  50. //! To be assigned by the application - not part of this object's identity
  51. std::string name;
  52. /// Un-erase the type of the handle and get it properly typed again.
  53. ///
  54. /// Note: Does not check the type before doing it!
  55. template <typename HandleType>
  56. HandleType& GetTypedHandle() {
  57. return TreatIntegerAsHandle<HandleType&>(handle);
  58. }
  59. //! @overload
  60. template <typename HandleType>
  61. HandleType const& GetTypedHandle() const {
  62. return TreatIntegerAsHandle<HandleType&>(handle);
  63. }
  64. XrSdkLogObjectInfo() = default;
  65. //! Create from a typed handle and object type
  66. template <typename T>
  67. XrSdkLogObjectInfo(T h, XrObjectType t) : handle(MakeHandleGeneric(h)), type(t) {}
  68. //! Create from an untyped handle value (integer) and object type
  69. XrSdkLogObjectInfo(uint64_t h, XrObjectType t) : handle(h), type(t) {}
  70. //! Create from an untyped handle value (integer), object type, and name
  71. XrSdkLogObjectInfo(uint64_t h, XrObjectType t, const char* n) : handle(h), type(t), name(n == nullptr ? "" : n) {}
  72. std::string ToString() const;
  73. };
  74. //! True if the two object infos have the same handle value and handle type
  75. static inline bool Equivalent(XrSdkLogObjectInfo const& a, XrSdkLogObjectInfo const& b) {
  76. return a.handle == b.handle && a.type == b.type;
  77. }
  78. //! @overload
  79. static inline bool Equivalent(XrDebugUtilsObjectNameInfoEXT const& a, XrSdkLogObjectInfo const& b) {
  80. return a.objectHandle == b.handle && a.objectType == b.type;
  81. }
  82. //! @overload
  83. static inline bool Equivalent(XrSdkLogObjectInfo const& a, XrDebugUtilsObjectNameInfoEXT const& b) { return Equivalent(b, a); }
  84. /// Object info registered with calls to xrSetDebugUtilsObjectNameEXT
  85. class ObjectInfoCollection {
  86. public:
  87. void AddObjectName(uint64_t object_handle, XrObjectType object_type, const std::string& object_name);
  88. void RemoveObject(uint64_t object_handle, XrObjectType object_type);
  89. //! Find the stored object info, if any, matching handle and type.
  90. //! Return nullptr if not found.
  91. XrSdkLogObjectInfo const* LookUpStoredObjectInfo(XrSdkLogObjectInfo const& info) const;
  92. //! Find the stored object info, if any, matching handle and type.
  93. //! Return nullptr if not found.
  94. XrSdkLogObjectInfo* LookUpStoredObjectInfo(XrSdkLogObjectInfo const& info);
  95. //! Find the stored object info, if any.
  96. //! Return nullptr if not found.
  97. XrSdkLogObjectInfo const* LookUpStoredObjectInfo(uint64_t handle, XrObjectType type) const {
  98. return LookUpStoredObjectInfo({handle, type});
  99. }
  100. //! Find the object name, if any, and update debug utils info accordingly.
  101. //! Return true if found and updated.
  102. bool LookUpObjectName(XrDebugUtilsObjectNameInfoEXT& info) const;
  103. //! Find the object name, if any, and update logging info accordingly.
  104. //! Return true if found and updated.
  105. bool LookUpObjectName(XrSdkLogObjectInfo& info) const;
  106. //! Is the collection empty?
  107. bool Empty() const { return object_info_.empty(); }
  108. private:
  109. // Object names that have been set for given objects
  110. std::vector<XrSdkLogObjectInfo> object_info_;
  111. };
  112. struct XrSdkSessionLabel;
  113. using XrSdkSessionLabelPtr = std::unique_ptr<XrSdkSessionLabel>;
  114. using XrSdkSessionLabelList = std::vector<XrSdkSessionLabelPtr>;
  115. struct XrSdkSessionLabel {
  116. static XrSdkSessionLabelPtr make(const XrDebugUtilsLabelEXT& label_info, bool individual);
  117. std::string label_name;
  118. XrDebugUtilsLabelEXT debug_utils_label;
  119. bool is_individual_label;
  120. private:
  121. XrSdkSessionLabel(const XrDebugUtilsLabelEXT& label_info, bool individual);
  122. };
  123. /// The metadata for a collection of objects. Must persist unmodified during the entire debug messenger call!
  124. struct NamesAndLabels {
  125. NamesAndLabels() = default;
  126. NamesAndLabels(std::vector<XrSdkLogObjectInfo> obj, std::vector<XrDebugUtilsLabelEXT> lab);
  127. /// C++ structure owning the data (strings) backing the objects vector.
  128. std::vector<XrSdkLogObjectInfo> sdk_objects;
  129. std::vector<XrDebugUtilsObjectNameInfoEXT> objects;
  130. std::vector<XrDebugUtilsLabelEXT> labels;
  131. /// Populate the debug utils callback data structure.
  132. void PopulateCallbackData(XrDebugUtilsMessengerCallbackDataEXT& data) const;
  133. // XrDebugUtilsMessengerCallbackDataEXT MakeCallbackData() const;
  134. };
  135. struct AugmentedCallbackData {
  136. std::vector<XrDebugUtilsLabelEXT> labels;
  137. std::vector<XrDebugUtilsObjectNameInfoEXT> new_objects;
  138. XrDebugUtilsMessengerCallbackDataEXT modified_data;
  139. const XrDebugUtilsMessengerCallbackDataEXT* exported_data;
  140. };
  141. /// Tracks all the data (handle names and session labels) required to fully augment XR_EXT_debug_utils-related calls.
  142. class DebugUtilsData {
  143. public:
  144. DebugUtilsData() = default;
  145. DebugUtilsData(const DebugUtilsData&) = delete;
  146. DebugUtilsData& operator=(const DebugUtilsData&) = delete;
  147. bool Empty() const { return object_info_.Empty() && session_labels_.empty(); }
  148. //! Core of implementation for xrSetDebugUtilsObjectNameEXT
  149. void AddObjectName(uint64_t object_handle, XrObjectType object_type, const std::string& object_name);
  150. /// Core of implementation for xrSessionBeginDebugUtilsLabelRegionEXT
  151. void BeginLabelRegion(XrSession session, const XrDebugUtilsLabelEXT& label_info);
  152. /// Core of implementation for xrSessionEndDebugUtilsLabelRegionEXT
  153. void EndLabelRegion(XrSession session);
  154. /// Core of implementation for xrSessionInsertDebugUtilsLabelEXT
  155. void InsertLabel(XrSession session, const XrDebugUtilsLabelEXT& label_info);
  156. /// Removes all labels associated with a session - call in xrDestroySession and xrDestroyInstance (for all child sessions)
  157. void DeleteSessionLabels(XrSession session);
  158. /// Retrieve labels for the given session, if any, and push them in reverse order on the vector.
  159. void LookUpSessionLabels(XrSession session, std::vector<XrDebugUtilsLabelEXT>& labels) const;
  160. /// Removes all data related to this object - including session labels if it's a session.
  161. ///
  162. /// Does not take care of handling child objects - you must do this yourself.
  163. void DeleteObject(uint64_t object_handle, XrObjectType object_type);
  164. /// Given the collection of objects, populate their names and list of labels
  165. NamesAndLabels PopulateNamesAndLabels(std::vector<XrSdkLogObjectInfo> objects) const;
  166. void WrapCallbackData(AugmentedCallbackData* aug_data,
  167. const XrDebugUtilsMessengerCallbackDataEXT* provided_callback_data) const;
  168. private:
  169. void RemoveIndividualLabel(XrSdkSessionLabelList& label_vec);
  170. XrSdkSessionLabelList* GetSessionLabelList(XrSession session);
  171. XrSdkSessionLabelList& GetOrCreateSessionLabelList(XrSession session);
  172. // Session labels: one vector of them per session.
  173. std::unordered_map<XrSession, std::unique_ptr<XrSdkSessionLabelList>> session_labels_;
  174. // Names for objects.
  175. ObjectInfoCollection object_info_;
  176. };