Loading...
Searching...
No Matches
utils.h
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#include "pxr/imaging/cameraUtil/conformWindow.h"
31
33#include "pxr/usd/sdf/path.h"
34
35#include <iosfwd>
36#include <memory>
37#include <string>
38#include <unordered_map>
39
40PXR_NAMESPACE_OPEN_SCOPE
41
43
44class TfToken;
45
46namespace HdUtils {
47
75template <typename T>
76class RenderInstanceTracker
77{
78public:
79 using TWeakPtr = std::weak_ptr<T>;
80 using TSharedPtr = std::shared_ptr<T>;
81
82 void RegisterInstance(
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
106 void UnregisterInstance(
107 std::string const &renderInstanceId)
108 {
109 idInstanceMap.erase(renderInstanceId);
110 }
111
112 TSharedPtr GetInstance(
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
124private:
125 // Use a weak reference to the object.
126 using _IdToInstanceMap = std::unordered_map<std::string, TWeakPtr>;
127 _IdToInstanceMap idInstanceMap;
128};
129
134HD_API
135bool
136HasActiveRenderSettingsPrim(
137 const HdSceneIndexBaseRefPtr &si,
138 SdfPath *primPath = nullptr);
139
143HD_API
144CameraUtilConformWindowPolicy
145ToConformWindowPolicy(const TfToken &token);
146
150HD_API
151void
152PrintSceneIndex(
153 std::ostream &out,
154 const HdSceneIndexBaseRefPtr &si,
155 const SdfPath &rootPath = SdfPath::AbsoluteRootPath());
156
157}
158
159PXR_NAMESPACE_CLOSE_SCOPE
160
161#endif // PXR_IMAGING_HD_UTILS_H
Abstract interface to scene data.
Definition: sceneIndex.h:65
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:290
static SDF_API const SdfPath & AbsoluteRootPath()
The absolute path representing the top of the namespace hierarchy.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
Standard pointer typedefs.
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:75
#define TF_WARN(...)
Issue a warning, but continue execution.
Definition: diagnostic.h:149