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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef PXR_IMAGING_HD_UTILS_H
26 #define PXR_IMAGING_HD_UTILS_H
27 
28 #include "pxr/pxr.h"
29 #include "pxr/imaging/hd/api.h"
31 
33 #include "pxr/usd/sdf/path.h"
34 
35 #include <iosfwd>
36 #include <memory>
37 #include <string>
38 #include <unordered_map>
39 
41 
43 
44 class TfToken;
45 
46 namespace HdUtils {
47 
48 /// A simple facility to associate an application object managed by
49 /// std::shared_ptr with a render instance id.
50 ///
51 /// This is useful when using the scene index callback registration facility.
52 /// The callback is registered only once, but may be invoked each time the
53 /// scene index graph is created (this currently happens during the render index
54 /// construction).
55 /// Futhermore, an application may spawn several render index instances and thus
56 /// the (same) callback may be invoked several times, necessitating a way to
57 /// map the callback back to the associated scene index instance.
58 ///
59 /// The \name RenderInstanceTracker facility below provides a simple way
60 /// to register, unregister and query an object that is tied to a render
61 /// instance id, which is provided as a callback argument.
62 ///
63 /// \note
64 /// The \em RegisterInstance method should be invoked before the scene index
65 /// callback is invoked (i.e., prior to render index construction).
66 ///
67 /// The \em UnregisterInstance method is typically invoked prior to render
68 /// index destruction.
69 ///
70 /// \note This facility isn't thread-safe.
71 ///
72 /// \sa HdSceneIndexPluginRegistry::SceneIndexAppendCallback
73 /// \sa HdSceneIndexPluginRegistry::RegisterSceneIndexForRenderer
74 ///
75 template <typename T>
77 {
78 public:
79  using TWeakPtr = std::weak_ptr<T>;
80  using TSharedPtr = std::shared_ptr<T>;
81 
83  std::string const &renderInstanceId,
84  TSharedPtr const &sp)
85  {
86  if (!sp) {
87  return;
88  }
89 
90  auto res = idInstanceMap.insert({renderInstanceId, sp});
91  if (!res.second) { // wasn't inserted
92  TWeakPtr &wp = res.first->second;
93  if (auto handle = wp.lock()) {
94  // Found entry with valid handle. This can happen if the
95  // renderInstanceId isn't unique enough. Leave the existing
96  // entry as-is.
97  TF_WARN(
98  "An instance with renderInstanceId %s was already "
99  "registered previously.", renderInstanceId.c_str());
100  return;
101  }
102  res.first->second = sp;
103  }
104  }
105 
107  std::string const &renderInstanceId)
108  {
109  idInstanceMap.erase(renderInstanceId);
110  }
111 
113  std::string const &id)
114  {
115  const auto it = idInstanceMap.find(id);
116  if (it != idInstanceMap.end()) {
117  if (TSharedPtr sp = it->second.lock()) {
118  return sp;
119  }
120  }
121  return nullptr;
122  }
123 
124 private:
125  // Use a weak reference to the object.
126  using _IdToInstanceMap = std::unordered_map<std::string, TWeakPtr>;
127  _IdToInstanceMap idInstanceMap;
128 };
129 
130 /// Retreives the active render settings prim path from the input scene index
131 /// \p si. Returns true if a data source for the associated locator was found
132 /// with the result in \p primPath, and false otherwise.
133 ///
134 HD_API
135 bool
137  const HdSceneIndexBaseRefPtr &si,
138  SdfPath *primPath = nullptr);
139 
140 /// Translate the given aspect ratio conform policy \p token into an equivalent
141 /// CameraUtilConformWindowPolicy enum.
142 ///
143 HD_API
145 ToConformWindowPolicy(const TfToken &token);
146 
147 /// Lexicographically sorts the scene index prims in the subtree rooted at
148 /// \p rootPath and writes them out.
149 ///
150 HD_API
151 void
153  std::ostream &out,
154  const HdSceneIndexBaseRefPtr &si,
155  const SdfPath &rootPath = SdfPath::AbsoluteRootPath());
156 
157 }
158 
160 
161 #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:112
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
HD_API CameraUtilConformWindowPolicy ToConformWindowPolicy(const TfToken &token)
#define HD_API
Definition: api.h:40
std::weak_ptr< T > TWeakPtr
Definition: utils.h:79
void RegisterInstance(std::string const &renderInstanceId, TSharedPtr const &sp)
Definition: utils.h:82
HD_API void PrintSceneIndex(std::ostream &out, const HdSceneIndexBaseRefPtr &si, const SdfPath &rootPath=SdfPath::AbsoluteRootPath())
Definition: token.h:87
#define TF_WARN
std::shared_ptr< T > TSharedPtr
Definition: utils.h:80
Definition: path.h:290
CameraUtilConformWindowPolicy
Definition: conformWindow.h:44
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
void UnregisterInstance(std::string const &renderInstanceId)
Definition: utils.h:106
HD_API bool HasActiveRenderSettingsPrim(const HdSceneIndexBaseRefPtr &si, SdfPath *primPath=nullptr)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91