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"
30 
32 
33 #include <memory>
34 #include <string>
35 #include <unordered_map>
36 
38 
40 
41 class SdfPath;
42 
43 namespace HdUtils {
44 
45 /// A simple facility to associate an application object managed by
46 /// std::shared_ptr with a render instance id.
47 ///
48 /// This is useful when using the scene index callback registration facility.
49 /// The callback is registered only once, but may be invoked each time the
50 /// scene index graph is created (this currently happens during the render index
51 /// construction).
52 /// Futhermore, an application may spawn several render index instances and thus
53 /// the (same) callback may be invoked several times, necessitating a way to
54 /// map the callback back to the associated scene index instance.
55 ///
56 /// The \name RenderInstanceTracker facility below provides a simple way
57 /// to register, unregister and query an object that is tied to a render
58 /// instance id, which is provided as a callback argument.
59 ///
60 /// \note
61 /// The \em RegisterInstance method should be invoked before the scene index
62 /// callback is invoked (i.e., prior to render index construction).
63 ///
64 /// The \em UnregisterInstance method is typically invoked prior to render
65 /// index destruction.
66 ///
67 /// \note This facility isn't thread-safe.
68 ///
69 /// \sa HdSceneIndexPluginRegistry::SceneIndexAppendCallback
70 /// \sa HdSceneIndexPluginRegistry::RegisterSceneIndexForRenderer
71 ///
72 template <typename T>
74 {
75 public:
76  using TWeakPtr = std::weak_ptr<T>;
77  using TSharedPtr = std::shared_ptr<T>;
78 
80  std::string const &renderInstanceId,
81  TSharedPtr const &sp)
82  {
83  if (!sp) {
84  return;
85  }
86 
87  auto res = idInstanceMap.insert({renderInstanceId, sp});
88  if (!res.second) { // wasn't inserted
89  TWeakPtr &wp = res.first->second;
90  if (auto handle = wp.lock()) {
91  // Found entry with valid handle. This can happen if the
92  // renderInstanceId isn't unique enough. Leave the existing
93  // entry as-is.
94  TF_WARN(
95  "An instance with renderInstanceId %s was already "
96  "registered previously.", renderInstanceId.c_str());
97  return;
98  }
99  res.first->second = sp;
100  }
101  }
102 
104  std::string const &renderInstanceId)
105  {
106  idInstanceMap.erase(renderInstanceId);
107  }
108 
110  std::string const &id)
111  {
112  const auto it = idInstanceMap.find(id);
113  if (it != idInstanceMap.end()) {
114  if (TSharedPtr sp = it->second.lock()) {
115  return sp;
116  }
117  }
118  return nullptr;
119  }
120 
121 private:
122  // Use a weak reference to the object.
123  using _IdToInstanceMap = std::unordered_map<std::string, TWeakPtr>;
124  _IdToInstanceMap idInstanceMap;
125 };
126 
127 /// Retreives the active render settings prim path from the input scene index
128 /// \p si. Returns true if a data source for the associated locator was found
129 /// with the result in \p primPath, and false otherwise.
130 ///
131 HD_API
132 bool
134  const HdSceneIndexBaseRefPtr &si,
135  SdfPath *primPath = nullptr);
136 
137 }
138 
140 
141 #endif // PXR_IMAGING_HD_UTILS_H
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_REF_PTRS(HdSceneIndexBase)
TSharedPtr GetInstance(std::string const &id)
Definition: utils.h:109
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
#define HD_API
Definition: api.h:40
std::weak_ptr< T > TWeakPtr
Definition: utils.h:76
void RegisterInstance(std::string const &renderInstanceId, TSharedPtr const &sp)
Definition: utils.h:79
#define TF_WARN
std::shared_ptr< T > TSharedPtr
Definition: utils.h:77
Definition: path.h:291
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
void UnregisterInstance(std::string const &renderInstanceId)
Definition: utils.h:103
HD_API bool HasActiveRenderSettingsPrim(const HdSceneIndexBaseRefPtr &si, SdfPath *primPath=nullptr)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91