Loading...
Searching...
No Matches
utils.h
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"
13#include "pxr/imaging/hd/dataSource.h"
14#include "pxr/imaging/hd/material.h"
15#include "pxr/imaging/cameraUtil/conformWindow.h"
16
18#include "pxr/usd/sdf/path.h"
19
20#include <iosfwd>
21#include <memory>
22#include <string>
23#include <unordered_map>
24
25PXR_NAMESPACE_OPEN_SCOPE
26
28
29class TfToken;
30
31namespace HdUtils {
32
60template <typename T>
61class RenderInstanceTracker
62{
63public:
64 using TWeakPtr = std::weak_ptr<T>;
65 using TSharedPtr = std::shared_ptr<T>;
66
67 void RegisterInstance(
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
91 void UnregisterInstance(
92 std::string const &renderInstanceId)
93 {
94 idInstanceMap.erase(renderInstanceId);
95 }
96
97 TSharedPtr GetInstance(
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
109private:
110 // Use a weak reference to the object.
111 using _IdToInstanceMap = std::unordered_map<std::string, TWeakPtr>;
112 _IdToInstanceMap idInstanceMap;
113};
114
119HD_API
120bool
121HasActiveRenderSettingsPrim(
122 const HdSceneIndexBaseRefPtr &si,
123 SdfPath *primPath = nullptr);
124
129HD_API
130bool
131HasActiveRenderPassPrim(
132 const HdSceneIndexBaseRefPtr &si,
133 SdfPath *primPath = nullptr);
134
139HD_API
140bool
141GetCurrentFrame(const HdSceneIndexBaseRefPtr &si, double *frame);
142
146HD_API
147CameraUtilConformWindowPolicy
148ToConformWindowPolicy(const TfToken &token);
149
153HD_API
154void
155PrintSceneIndex(
156 std::ostream &out,
157 const HdSceneIndexBaseRefPtr &si,
158 const SdfPath &rootPath = SdfPath::AbsoluteRootPath());
159
163HD_API
164HdContainerDataSourceHandle
165ConvertHdMaterialNetworkToHdMaterialNetworkSchema(
166 const HdMaterialNetworkMap& hdNetworkMap);
167
171HD_API
172HdContainerDataSourceHandle
173ConvertHdMaterialNetworkToHdMaterialSchema(
174 const HdMaterialNetworkMap &hdNetworkMap);
175
176HD_API
177HdContainerDataSourceHandle
178ConvertVtDictionaryToContainerDS(const VtDictionary &dict);
179
180}
181
182PXR_NAMESPACE_CLOSE_SCOPE
183
184#endif // PXR_IMAGING_HD_UTILS_H
Abstract interface to scene data.
Definition: sceneIndex.h:54
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
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:71
A map with string keys and VtValue values.
Definition: dictionary.h:43
Standard pointer typedefs.
#define TF_DECLARE_REF_PTRS(type)
Define standard ref pointer types.
Definition: declarePtrs.h:58
#define TF_WARN(...)
Issue a warning, but continue execution.
Definition: diagnostic.h:132
Describes a map from network type to network.
Definition: material.h:116