Path: blob/master/thirdparty/openxr/src/common/object_info.h
9903 views
// Copyright (c) 2017-2025 The Khronos Group Inc.1// Copyright (c) 2017-2019 Valve Corporation2// Copyright (c) 2017-2019 LunarG, Inc.3// Copyright (c) 2019 Collabora, Ltd.4//5// SPDX-License-Identifier: Apache-2.0 OR MIT6//7// Initial Authors: Mark Young <[email protected]>, Rylie Pavlik <[email protected]8//9/*!10* @file11*12* The core of an XR_EXT_debug_utils implementation, used/shared by the loader and several SDK layers.13*/1415#pragma once1617#include "hex_and_handles.h"1819#include <openxr/openxr.h>2021#include <memory>22#include <string>23#include <unordered_map>24#include <vector>2526struct XrSdkGenericObject {27//! Type-erased handle value28uint64_t handle;2930//! Kind of object this handle refers to31XrObjectType type;32/// Un-erase the type of the handle and get it properly typed again.33///34/// Note: Does not check the type before doing it!35template <typename HandleType>36HandleType& GetTypedHandle() {37return TreatIntegerAsHandle<HandleType&>(handle);38}3940//! @overload41template <typename HandleType>42HandleType const& GetTypedHandle() const {43return TreatIntegerAsHandle<HandleType&>(handle);44}4546//! Create from a typed handle and object type47template <typename T>48XrSdkGenericObject(T h, XrObjectType t) : handle(MakeHandleGeneric(h)), type(t) {}4950//! Create from an untyped handle value (integer) and object type51XrSdkGenericObject(uint64_t h, XrObjectType t) : handle(h), type(t) {}52};5354struct XrSdkLogObjectInfo {55//! Type-erased handle value56uint64_t handle;5758//! Kind of object this handle refers to59XrObjectType type;6061//! To be assigned by the application - not part of this object's identity62std::string name;6364/// Un-erase the type of the handle and get it properly typed again.65///66/// Note: Does not check the type before doing it!67template <typename HandleType>68HandleType& GetTypedHandle() {69return TreatIntegerAsHandle<HandleType&>(handle);70}7172//! @overload73template <typename HandleType>74HandleType const& GetTypedHandle() const {75return TreatIntegerAsHandle<HandleType&>(handle);76}7778XrSdkLogObjectInfo() = default;7980//! Create from a typed handle and object type81template <typename T>82XrSdkLogObjectInfo(T h, XrObjectType t) : handle(MakeHandleGeneric(h)), type(t) {}8384//! Create from an untyped handle value (integer) and object type85XrSdkLogObjectInfo(uint64_t h, XrObjectType t) : handle(h), type(t) {}86//! Create from an untyped handle value (integer), object type, and name87XrSdkLogObjectInfo(uint64_t h, XrObjectType t, const char* n) : handle(h), type(t), name(n == nullptr ? "" : n) {}8889std::string ToString() const;90};9192//! True if the two object infos have the same handle value and handle type93static inline bool Equivalent(XrSdkLogObjectInfo const& a, XrSdkLogObjectInfo const& b) {94return a.handle == b.handle && a.type == b.type;95}9697//! @overload98static inline bool Equivalent(XrDebugUtilsObjectNameInfoEXT const& a, XrSdkLogObjectInfo const& b) {99return a.objectHandle == b.handle && a.objectType == b.type;100}101102//! @overload103static inline bool Equivalent(XrSdkLogObjectInfo const& a, XrDebugUtilsObjectNameInfoEXT const& b) { return Equivalent(b, a); }104105/// Object info registered with calls to xrSetDebugUtilsObjectNameEXT106class ObjectInfoCollection {107public:108void AddObjectName(uint64_t object_handle, XrObjectType object_type, const std::string& object_name);109110void RemoveObject(uint64_t object_handle, XrObjectType object_type);111112//! Find the stored object info, if any, matching handle and type.113//! Return nullptr if not found.114XrSdkLogObjectInfo const* LookUpStoredObjectInfo(XrSdkLogObjectInfo const& info) const;115116//! Find the stored object info, if any, matching handle and type.117//! Return nullptr if not found.118XrSdkLogObjectInfo* LookUpStoredObjectInfo(XrSdkLogObjectInfo const& info);119120//! Find the stored object info, if any.121//! Return nullptr if not found.122XrSdkLogObjectInfo const* LookUpStoredObjectInfo(uint64_t handle, XrObjectType type) const {123return LookUpStoredObjectInfo({handle, type});124}125126//! Find the object name, if any, and update debug utils info accordingly.127//! Return true if found and updated.128bool LookUpObjectName(XrDebugUtilsObjectNameInfoEXT& info) const;129130//! Find the object name, if any, and update logging info accordingly.131//! Return true if found and updated.132bool LookUpObjectName(XrSdkLogObjectInfo& info) const;133134//! Is the collection empty?135bool Empty() const { return object_info_.empty(); }136137private:138// Object names that have been set for given objects139std::vector<XrSdkLogObjectInfo> object_info_;140};141142struct XrSdkSessionLabel;143using XrSdkSessionLabelPtr = std::unique_ptr<XrSdkSessionLabel>;144using XrSdkSessionLabelList = std::vector<XrSdkSessionLabelPtr>;145146struct XrSdkSessionLabel {147static XrSdkSessionLabelPtr make(const XrDebugUtilsLabelEXT& label_info, bool individual);148149std::string label_name;150XrDebugUtilsLabelEXT debug_utils_label;151bool is_individual_label;152153private:154XrSdkSessionLabel(const XrDebugUtilsLabelEXT& label_info, bool individual);155};156157/// The metadata for a collection of objects. Must persist unmodified during the entire debug messenger call!158struct NamesAndLabels {159NamesAndLabels() = default;160NamesAndLabels(std::vector<XrSdkLogObjectInfo> obj, std::vector<XrDebugUtilsLabelEXT> lab);161/// C++ structure owning the data (strings) backing the objects vector.162std::vector<XrSdkLogObjectInfo> sdk_objects;163164std::vector<XrDebugUtilsObjectNameInfoEXT> objects;165std::vector<XrDebugUtilsLabelEXT> labels;166167/// Populate the debug utils callback data structure.168void PopulateCallbackData(XrDebugUtilsMessengerCallbackDataEXT& data) const;169// XrDebugUtilsMessengerCallbackDataEXT MakeCallbackData() const;170};171172struct AugmentedCallbackData {173std::vector<XrDebugUtilsLabelEXT> labels;174std::vector<XrDebugUtilsObjectNameInfoEXT> new_objects;175XrDebugUtilsMessengerCallbackDataEXT modified_data;176const XrDebugUtilsMessengerCallbackDataEXT* exported_data;177};178179/// Tracks all the data (handle names and session labels) required to fully augment XR_EXT_debug_utils-related calls.180class DebugUtilsData {181public:182DebugUtilsData() = default;183184DebugUtilsData(const DebugUtilsData&) = delete;185DebugUtilsData& operator=(const DebugUtilsData&) = delete;186187bool Empty() const { return object_info_.Empty() && session_labels_.empty(); }188189//! Core of implementation for xrSetDebugUtilsObjectNameEXT190void AddObjectName(uint64_t object_handle, XrObjectType object_type, const std::string& object_name);191192/// Core of implementation for xrSessionBeginDebugUtilsLabelRegionEXT193void BeginLabelRegion(XrSession session, const XrDebugUtilsLabelEXT& label_info);194195/// Core of implementation for xrSessionEndDebugUtilsLabelRegionEXT196void EndLabelRegion(XrSession session);197198/// Core of implementation for xrSessionInsertDebugUtilsLabelEXT199void InsertLabel(XrSession session, const XrDebugUtilsLabelEXT& label_info);200201/// Removes all labels associated with a session - call in xrDestroySession and xrDestroyInstance (for all child sessions)202void DeleteSessionLabels(XrSession session);203204/// Retrieve labels for the given session, if any, and push them in reverse order on the vector.205void LookUpSessionLabels(XrSession session, std::vector<XrDebugUtilsLabelEXT>& labels) const;206207/// Removes all data related to this object - including session labels if it's a session.208///209/// Does not take care of handling child objects - you must do this yourself.210void DeleteObject(uint64_t object_handle, XrObjectType object_type);211212/// Given the collection of objects, populate their names and list of labels213NamesAndLabels PopulateNamesAndLabels(std::vector<XrSdkLogObjectInfo> objects) const;214215void WrapCallbackData(AugmentedCallbackData* aug_data,216const XrDebugUtilsMessengerCallbackDataEXT* provided_callback_data) const;217218private:219void RemoveIndividualLabel(XrSdkSessionLabelList& label_vec);220XrSdkSessionLabelList* GetSessionLabelList(XrSession session);221XrSdkSessionLabelList& GetOrCreateSessionLabelList(XrSession session);222223// Session labels: one vector of them per session.224std::unordered_map<XrSession, std::unique_ptr<XrSdkSessionLabelList>> session_labels_;225226// Names for objects.227ObjectInfoCollection object_info_;228};229230231