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/hd/dataSource.h"
31#include "pxr/imaging/hd/material.h"
32#include "pxr/imaging/cameraUtil/conformWindow.h"
33
35#include "pxr/usd/sdf/path.h"
36
37#include <iosfwd>
38#include <memory>
39#include <string>
40#include <unordered_map>
41
42PXR_NAMESPACE_OPEN_SCOPE
43
45
46class TfToken;
47
48namespace HdUtils {
49
77template <typename T>
78class RenderInstanceTracker
79{
80public:
81 using TWeakPtr = std::weak_ptr<T>;
82 using TSharedPtr = std::shared_ptr<T>;
83
84 void RegisterInstance(
85 std::string const &renderInstanceId,
86 TSharedPtr const &sp)
87 {
88 if (!sp) {
89 return;
90 }
91
92 auto res = idInstanceMap.insert({renderInstanceId, sp});
93 if (!res.second) { // wasn't inserted
94 TWeakPtr &wp = res.first->second;
95 if (auto handle = wp.lock()) {
96 // Found entry with valid handle. This can happen if the
97 // renderInstanceId isn't unique enough. Leave the existing
98 // entry as-is.
99 TF_WARN(
100 "An instance with renderInstanceId %s was already "
101 "registered previously.", renderInstanceId.c_str());
102 return;
103 }
104 res.first->second = sp;
105 }
106 }
107
108 void UnregisterInstance(
109 std::string const &renderInstanceId)
110 {
111 idInstanceMap.erase(renderInstanceId);
112 }
113
114 TSharedPtr GetInstance(
115 std::string const &id)
116 {
117 const auto it = idInstanceMap.find(id);
118 if (it != idInstanceMap.end()) {
119 if (TSharedPtr sp = it->second.lock()) {
120 return sp;
121 }
122 }
123 return nullptr;
124 }
125
126private:
127 // Use a weak reference to the object.
128 using _IdToInstanceMap = std::unordered_map<std::string, TWeakPtr>;
129 _IdToInstanceMap idInstanceMap;
130};
131
136HD_API
137bool
138HasActiveRenderSettingsPrim(
139 const HdSceneIndexBaseRefPtr &si,
140 SdfPath *primPath = nullptr);
141
145HD_API
146CameraUtilConformWindowPolicy
147ToConformWindowPolicy(const TfToken &token);
148
152HD_API
153void
154PrintSceneIndex(
155 std::ostream &out,
156 const HdSceneIndexBaseRefPtr &si,
157 const SdfPath &rootPath = SdfPath::AbsoluteRootPath());
158
162HD_API
163HdContainerDataSourceHandle
164ConvertHdMaterialNetworkToHdMaterialNetworkSchema(
165 const HdMaterialNetworkMap& hdNetworkMap);
166
170HD_API
171HdContainerDataSourceHandle
172ConvertHdMaterialNetworkToHdMaterialSchema(
173 const HdMaterialNetworkMap &hdNetworkMap);
174
175}
176
177PXR_NAMESPACE_CLOSE_SCOPE
178
179#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
Describes a map from network type to network.
Definition: material.h:129