HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
utils.h
Go to the documentation of this file.
1 //
2 // Copyright 2023 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_IMAGING_HD_UTILS_H
9 #define PXR_IMAGING_HD_UTILS_H
10 
11 #include "pxr/pxr.h"
12 #include "pxr/imaging/hd/api.h"
16 
18 #include "pxr/usd/sdf/path.h"
19 
20 #include <iosfwd>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 
26 
28 
29 class TfToken;
30 
31 namespace HdUtils {
32 
33 /// A simple facility to associate an application object managed by
34 /// std::shared_ptr with a render instance id.
35 ///
36 /// This is useful when using the scene index callback registration facility.
37 /// The callback is registered only once, but may be invoked each time the
38 /// scene index graph is created (this currently happens during the render index
39 /// construction).
40 /// Futhermore, an application may spawn several render index instances and thus
41 /// the (same) callback may be invoked several times, necessitating a way to
42 /// map the callback back to the associated scene index instance.
43 ///
44 /// The \name RenderInstanceTracker facility below provides a simple way
45 /// to register, unregister and query an object that is tied to a render
46 /// instance id, which is provided as a callback argument.
47 ///
48 /// \note
49 /// The \em RegisterInstance method should be invoked before the scene index
50 /// callback is invoked (i.e., prior to render index construction).
51 ///
52 /// The \em UnregisterInstance method is typically invoked prior to render
53 /// index destruction.
54 ///
55 /// \note This facility isn't thread-safe.
56 ///
57 /// \sa HdSceneIndexPluginRegistry::SceneIndexAppendCallback
58 /// \sa HdSceneIndexPluginRegistry::RegisterSceneIndexForRenderer
59 ///
60 template <typename T>
62 {
63 public:
64  using TWeakPtr = std::weak_ptr<T>;
65  using TSharedPtr = std::shared_ptr<T>;
66 
68  std::string const &renderInstanceId,
69  TSharedPtr const &sp)
70  {
71  if (!sp) {
72  return;
73  }
74 
75  auto res = idInstanceMap.insert({renderInstanceId, sp});
76  if (!res.second) { // wasn't inserted
77  TWeakPtr &wp = res.first->second;
78  if (auto handle = wp.lock()) {
79  // Found entry with valid handle. This can happen if the
80  // renderInstanceId isn't unique enough. Leave the existing
81  // entry as-is.
82  TF_WARN(
83  "An instance with renderInstanceId %s was already "
84  "registered previously.", renderInstanceId.c_str());
85  return;
86  }
87  res.first->second = sp;
88  }
89  }
90 
92  std::string const &renderInstanceId)
93  {
94  idInstanceMap.erase(renderInstanceId);
95  }
96 
98  std::string const &id)
99  {
100  const auto it = idInstanceMap.find(id);
101  if (it != idInstanceMap.end()) {
102  if (TSharedPtr sp = it->second.lock()) {
103  return sp;
104  }
105  }
106  return nullptr;
107  }
108 
109 private:
110  // Use a weak reference to the object.
111  using _IdToInstanceMap = std::unordered_map<std::string, TWeakPtr>;
112  _IdToInstanceMap idInstanceMap;
113 };
114 
115 /// Retreives the active render settings prim path from the input scene index
116 /// \p si. Returns true if a data source for the associated locator was found
117 /// with the result in \p primPath, and false otherwise.
118 ///
119 HD_API
120 bool
122  const HdSceneIndexBaseRefPtr &si,
123  SdfPath *primPath = nullptr);
124 
125 /// Retreives the current frame number from the input scene index \p si.
126 /// Returns true if a data source for the associated locator was found
127 /// with the result in \p frame, and false otherwise.
128 ///
129 HD_API
130 bool
131 GetCurrentFrame(const HdSceneIndexBaseRefPtr &si, double *frame);
132 
133 /// Translate the given aspect ratio conform policy \p token into an equivalent
134 /// CameraUtilConformWindowPolicy enum.
135 ///
136 HD_API
138 ToConformWindowPolicy(const TfToken &token);
139 
140 /// Lexicographically sorts the scene index prims in the subtree rooted at
141 /// \p rootPath and writes them out.
142 ///
143 HD_API
144 void
146  std::ostream &out,
147  const HdSceneIndexBaseRefPtr &si,
148  const SdfPath &rootPath = SdfPath::AbsoluteRootPath());
149 
150 /// Convert the supplied HdMaterialNetworkMap to an HdMaterialNetworkSchema
151 /// container data source.
152 ///
153 HD_API
154 HdContainerDataSourceHandle
156  const HdMaterialNetworkMap& hdNetworkMap);
157 
158 /// Convert the supplied HdMaterialNetworkMap to an HdMaterialSchema
159 /// container data source.
160 ///
161 HD_API
162 HdContainerDataSourceHandle
164  const HdMaterialNetworkMap &hdNetworkMap);
165 
166 HD_API
167 HdContainerDataSourceHandle
169 
170 }
171 
173 
174 #endif // PXR_IMAGING_HD_UTILS_H
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_REF_PTRS(HdSceneIndexBase)
static SDF_API const SdfPath & AbsoluteRootPath()
TSharedPtr GetInstance(std::string const &id)
Definition: utils.h:97
HD_API CameraUtilConformWindowPolicy ToConformWindowPolicy(const TfToken &token)
#define HD_API
Definition: api.h:23
HD_API bool GetCurrentFrame(const HdSceneIndexBaseRefPtr &si, double *frame)
std::weak_ptr< T > TWeakPtr
Definition: utils.h:64
void RegisterInstance(std::string const &renderInstanceId, TSharedPtr const &sp)
Definition: utils.h:67
HD_API void PrintSceneIndex(std::ostream &out, const HdSceneIndexBaseRefPtr &si, const SdfPath &rootPath=SdfPath::AbsoluteRootPath())
Definition: token.h:70
#define TF_WARN
std::shared_ptr< T > TSharedPtr
Definition: utils.h:65
HD_API HdContainerDataSourceHandle ConvertVtDictionaryToContainerDS(const VtDictionary &dict)
Definition: path.h:273
CameraUtilConformWindowPolicy
Definition: conformWindow.h:27
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
void UnregisterInstance(std::string const &renderInstanceId)
Definition: utils.h:91
HD_API bool HasActiveRenderSettingsPrim(const HdSceneIndexBaseRefPtr &si, SdfPath *primPath=nullptr)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
HD_API HdContainerDataSourceHandle ConvertHdMaterialNetworkToHdMaterialNetworkSchema(const HdMaterialNetworkMap &hdNetworkMap)
HD_API HdContainerDataSourceHandle ConvertHdMaterialNetworkToHdMaterialSchema(const HdMaterialNetworkMap &hdNetworkMap)