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 HDPRMAN_CONTEXT_H
25 #define HDPRMAN_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 
56 typedef std::map<SdfPath, std::vector<float>> HdPrman_TimeSampleMap;
57 
58 // Context for HdPrman to communicate with an instance of PRMan.
59 struct HdPrman_Context
60 {
61  // Top-level entrypoint to PRMan.
62  // Singleton used to access RixInterfaces.
63  RixContext *rix = nullptr;
64  // RixInterface for PRManBegin/End.
65  RixRiCtl *ri = nullptr;
66  // RixInterface for Riley.
67  RixRileyManager *mgr = nullptr;
68  // Riley instance.
69  riley::Riley *riley = nullptr;
70 
71  // A fallback material to use for any geometry that
72  // does not have a bound material.
73  riley::MaterialId fallbackMaterial;
74  riley::MaterialId fallbackVolumeMaterial;
75  // The fallback set of time samples to evalate,
76  // if there is not a relevant entry in timeSampleMap.
77  std::vector<float> defaultTimeSamples;
78  // Per-scope configuration of time samples to evaluate.
79  HdPrman_TimeSampleMap timeSampleMap;
80 
81  // A helper to look up the set of time samples to evalutae
82  // for the given Hydra id.
83  HDPRMAN_API
84  const std::vector<float>& GetTimeSamplesForId(SdfPath const&);
85 
86  // Convert any Hydra primvars that should be Riley instance attributes.
87  HDPRMAN_API
88  RixParamList*
89  ConvertAttributes(HdSceneDelegate *sceneDelegate, SdfPath const& id);
90 
91  // A vector of Riley coordinate system id's.
92  typedef std::vector<riley::CoordinateSystemId> RileyCoordSysIdVec;
93  // A ref-counting ptr to a vector of coordinate systems.
94  typedef std::shared_ptr<RileyCoordSysIdVec> RileyCoordSysIdVecRefPtr;
95 
100  HDPRMAN_API
101  RileyCoordSysIdVecRefPtr
102  ConvertAndRetainCoordSysBindings(
103  HdSceneDelegate *sceneDelegate,
104  SdfPath const& id);
105 
108  HDPRMAN_API
109  void
110  ConvertCategoriesToAttributes(SdfPath const& id,
111  VtArray<TfToken> const& categories,
112  RixParamList *attrs);
113 
116  HDPRMAN_API
117  void ReleaseCoordSysBindings(SdfPath const& id);
118 
119  HDPRMAN_API
120  void IncrementLightLinkCount(TfToken const& name);
121 
122  HDPRMAN_API
123  void DecrementLightLinkCount(TfToken const& name);
124 
125  HDPRMAN_API
126  bool IsLightLinkUsed(TfToken const& name);
127 
128  virtual ~HdPrman_Context() = default;
129 
130 private:
131  // Refcounts for each category mentioned by a light link.
132  // This is used to convey information from lights back to the
133  // geometry -- in Renderman, geometry must subscribe
134  // to the linked lights.
135  std::unordered_map<TfToken, size_t, TfToken::HashFunctor> _lightLinkRefs;
136 
137  // Mutex protecting lightLinkRefs.
138  std::mutex _lightLinkMutex;
139 
140  // Map from Hydra coordinate system vector pointer to Riley equivalent.
141  typedef std::unordered_map<
142  HdIdVectorSharedPtr, RileyCoordSysIdVecRefPtr>
143  _HdToRileyCoordSysMap;
144  // Map from Hydra id to cached, converted coordinate systems.
145  typedef std::unordered_map<
146  SdfPath, HdIdVectorSharedPtr, SdfPath::Hash>
147  _GeomToHdCoordSysMap;
148 
149  // Coordinate system conversion cache.
150  _GeomToHdCoordSysMap _geomToHdCoordSysMap;
151  _HdToRileyCoordSysMap _hdToRileyCoordSysMap;
152  std::mutex _coordSysMutex;
153 };
154 
155 // Helper to convert matrix types, handling double->float conversion.
156 inline RtMatrix4x4
157 HdPrman_GfMatrixToRtMatrix(const GfMatrix4d &m)
158 {
159  const double *d = m.GetArray();
160  return RtMatrix4x4(
161  d[0], d[1], d[2], d[3],
162  d[4], d[5], d[6], d[7],
163  d[8], d[9], d[10], d[11],
164  d[12], d[13], d[14], d[15]);
165 }
166 
167 // Helper to convert matrix types, handling float->double conversion.
168 inline GfMatrix4d
169 HdPrman_RtMatrixToGfMatrix(const RtMatrix4x4 &m)
170 {
171  return GfMatrix4d(
172  m.m[0][0], m.m[0][1], m.m[0][2], m.m[0][3],
173  m.m[1][0], m.m[1][1], m.m[1][2], m.m[1][3],
174  m.m[2][0], m.m[2][1], m.m[2][2], m.m[2][3],
175  m.m[3][0], m.m[3][1], m.m[3][2], m.m[3][3]);
176 }
177 
178 // Convert any Hydra primvars that should be Riley primvars.
179 void
180 HdPrman_ConvertPrimvars(HdSceneDelegate *sceneDelegate, SdfPath const& id,
181  RixParamList *primvars, int numUniform, int numVertex,
182  int numVarying, int numFaceVarying);
183 
184 // Resolve Hd material ID to the corresponding Riley material & displacement
185 bool
186 HdPrman_ResolveMaterial(HdSceneDelegate *sceneDelegate,
187  SdfPath const& hdMaterialId,
188  riley::MaterialId *materialId,
189  riley::DisplacementId *dispId);
190 
191 
199 HDPRMAN_API
200 void
201 HdPrman_UpdateSearchPathsFromEnvironment(RixParamList *options);
202 
203 PXR_NAMESPACE_CLOSE_SCOPE
204 
205 #endif // HDPRMAN_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