All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
131GetCurrentFrame(const HdSceneIndexBaseRefPtr &si, double *frame);
132
136HD_API
137CameraUtilConformWindowPolicy
138ToConformWindowPolicy(const TfToken &token);
139
143HD_API
144void
145PrintSceneIndex(
146 std::ostream &out,
147 const HdSceneIndexBaseRefPtr &si,
148 const SdfPath &rootPath = SdfPath::AbsoluteRootPath());
149
153HD_API
154HdContainerDataSourceHandle
155ConvertHdMaterialNetworkToHdMaterialNetworkSchema(
156 const HdMaterialNetworkMap& hdNetworkMap);
157
161HD_API
162HdContainerDataSourceHandle
163ConvertHdMaterialNetworkToHdMaterialSchema(
164 const HdMaterialNetworkMap &hdNetworkMap);
165
166}
167
168PXR_NAMESPACE_CLOSE_SCOPE
169
170#endif // PXR_IMAGING_HD_UTILS_H
Abstract interface to scene data.
Definition: sceneIndex.h:48
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
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:112