All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
context.h
1 //
2 // Copyright 2019 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 #ifndef EXT_RMANPKG_22_0_PLUGIN_RENDERMAN_PLUGIN_HD_PRMAN_CONTEXT_H
25 #define EXT_RMANPKG_22_0_PLUGIN_RENDERMAN_PLUGIN_HD_PRMAN_CONTEXT_H
26 
27 #include "pxr/pxr.h"
28 #include "hdPrman/api.h"
29 #include "pxr/base/gf/matrix4d.h"
30 #include "pxr/base/tf/token.h"
31 #include "pxr/usd/sdf/path.h"
32 #include "pxr/imaging/hd/sceneDelegate.h"
33 #include "pxr/imaging/hd/coordSys.h"
34 
35 #include "Riley.h"
36 #include <thread>
37 #include <unordered_map>
38 #include <mutex>
39 
40 // Compile-time limit on max time samples.
41 // The idea is to avoid heap allocation of sample buffers in the Sync()
42 // calls by using fixed-size stack arrays with configured capacity.
43 // The capacity is indicated to the scene delegate when requesting
44 // time samples.
45 #define HDPRMAN_MAX_TIME_SAMPLES 4
46 
47 class RixRiCtl;
48 class RixParamList;
49 
50 PXR_NAMESPACE_OPEN_SCOPE
51 
52 class SdfPath;
53 class HdSceneDelegate;
54 
55 // Context for HdPrman to communicate with an instance of PRMan.
56 struct HdPrman_Context
57 {
58  // Top-level entrypoint to PRMan.
59  // Singleton used to access RixInterfaces.
60  RixContext *rix = nullptr;
61  // RixInterface for PRManBegin/End.
62  RixRiCtl *ri = nullptr;
63  // RixInterface for Riley.
64  RixRileyManager *mgr = nullptr;
65  // Riley instance.
66  riley::Riley *riley = nullptr;
67 
68  // A fallback material to use for any geometry that
69  // does not have a bound material.
70  riley::MaterialId fallbackMaterial;
71  riley::MaterialId fallbackVolumeMaterial;
72 
73  // Convert any Hydra primvars that should be Riley instance attributes.
74  HDPRMAN_API
75  RixParamList*
76  ConvertAttributes(HdSceneDelegate *sceneDelegate, SdfPath const& id);
77 
78  // A vector of Riley coordinate system id's.
79  typedef std::vector<riley::CoordinateSystemId> RileyCoordSysIdVec;
80  // A ref-counting ptr to a vector of coordinate systems.
81  typedef std::shared_ptr<RileyCoordSysIdVec> RileyCoordSysIdVecRefPtr;
82 
87  HDPRMAN_API
88  RileyCoordSysIdVecRefPtr
89  ConvertAndRetainCoordSysBindings(
90  HdSceneDelegate *sceneDelegate,
91  SdfPath const& id);
92 
95  HDPRMAN_API
96  void
97  ConvertCategoriesToAttributes(SdfPath const& id,
98  VtArray<TfToken> const& categories,
99  RixParamList *attrs);
100 
103  HDPRMAN_API
104  void ReleaseCoordSysBindings(SdfPath const& id);
105 
106  HDPRMAN_API
107  void IncrementLightLinkCount(TfToken const& name);
108 
109  HDPRMAN_API
110  void DecrementLightLinkCount(TfToken const& name);
111 
112  HDPRMAN_API
113  bool IsLightLinkUsed(TfToken const& name);
114 
115  HDPRMAN_API
116  void IncrementLightFilterCount(TfToken const& name);
117 
118  HDPRMAN_API
119  void DecrementLightFilterCount(TfToken const& name);
120 
121  HDPRMAN_API
122  bool IsLightFilterUsed(TfToken const& name);
123 
124  virtual ~HdPrman_Context() = default;
125 
126 private:
127  // Refcounts for each category mentioned by a light link.
128  // This is used to convey information from lights back to the
129  // geometry -- in Renderman, geometry must subscribe
130  // to the linked lights.
131  std::unordered_map<TfToken, size_t, TfToken::HashFunctor> _lightLinkRefs;
132 
133  // Mutex protecting lightLinkRefs.
134  std::mutex _lightLinkMutex;
135 
136  std::unordered_map<TfToken, size_t, TfToken::HashFunctor> _lightFilterRefs;
137 
138  // Mutex protecting lightFilterRefs.
139  std::mutex _lightFilterMutex;
140 
141  // Map from Hydra coordinate system vector pointer to Riley equivalent.
142  typedef std::unordered_map<
143  HdIdVectorSharedPtr, RileyCoordSysIdVecRefPtr>
144  _HdToRileyCoordSysMap;
145  // Map from Hydra id to cached, converted coordinate systems.
146  typedef std::unordered_map<
147  SdfPath, HdIdVectorSharedPtr, SdfPath::Hash>
148  _GeomToHdCoordSysMap;
149 
150  // Coordinate system conversion cache.
151  _GeomToHdCoordSysMap _geomToHdCoordSysMap;
152  _HdToRileyCoordSysMap _hdToRileyCoordSysMap;
153  std::mutex _coordSysMutex;
154 };
155 
156 // Helper to convert matrix types, handling double->float conversion.
157 inline RtMatrix4x4
158 HdPrman_GfMatrixToRtMatrix(const GfMatrix4d &m)
159 {
160  const double *d = m.GetArray();
161  return RtMatrix4x4(
162  d[0], d[1], d[2], d[3],
163  d[4], d[5], d[6], d[7],
164  d[8], d[9], d[10], d[11],
165  d[12], d[13], d[14], d[15]);
166 }
167 
168 // Helper to convert matrix types, handling float->double conversion.
169 inline GfMatrix4d
170 HdPrman_RtMatrixToGfMatrix(const RtMatrix4x4 &m)
171 {
172  return GfMatrix4d(
173  m.m[0][0], m.m[0][1], m.m[0][2], m.m[0][3],
174  m.m[1][0], m.m[1][1], m.m[1][2], m.m[1][3],
175  m.m[2][0], m.m[2][1], m.m[2][2], m.m[2][3],
176  m.m[3][0], m.m[3][1], m.m[3][2], m.m[3][3]);
177 }
178 
179 // Convert any Hydra primvars that should be Riley primvars.
180 void
181 HdPrman_ConvertPrimvars(HdSceneDelegate *sceneDelegate, SdfPath const& id,
182  RixParamList *primvars, int numUniform, int numVertex,
183  int numVarying, int numFaceVarying);
184 
185 // Resolve Hd material ID to the corresponding Riley material & displacement
186 bool
187 HdPrman_ResolveMaterial(HdSceneDelegate *sceneDelegate,
188  SdfPath const& hdMaterialId,
189  riley::MaterialId *materialId,
190  riley::DisplacementId *dispId);
191 
192 
200 HDPRMAN_API
201 void
202 HdPrman_UpdateSearchPathsFromEnvironment(RixParamList *options);
203 
204 PXR_NAMESPACE_CLOSE_SCOPE
205 
206 #endif // EXT_RMANPKG_22_0_PLUGIN_RENDERMAN_PLUGIN_HD_PRMAN_CONTEXT_H
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Adapter class providing data exchange with the client scene graph.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
double * GetArray()
Returns vector components as an array of double values.
Definition: matrix4d.h:285