HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
context.h
Go to the documentation of this file.
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 "hdPrman/xcpt.h"
30 #include "pxr/base/gf/matrix4d.h"
31 #include "pxr/base/tf/token.h"
32 #include "pxr/usd/sdf/path.h"
35 
36 #include "Riley.h"
37 #include <thread>
38 #include <unordered_map>
39 #include <mutex>
40 
41 // Compile-time limit on max time samples.
42 // The idea is to avoid heap allocation of sample buffers in the Sync()
43 // calls by using fixed-size stack arrays with configured capacity.
44 // The capacity is indicated to the scene delegate when requesting
45 // time samples.
46 #define HDPRMAN_MAX_TIME_SAMPLES 4
47 
48 class RixRiCtl;
49 
51 
52 class SdfPath;
53 class HdSceneDelegate;
55 
56 /// Hierarchical map of scene ids to sample times.
57 typedef std::map<SdfPath, std::vector<float>> HdPrman_TimeSampleMap;
58 
59 // Context for HdPrman to communicate with an instance of PRMan.
61 {
62  // Top-level entrypoint to PRMan.
63  // Singleton used to access RixInterfaces.
64  RixContext *rix = nullptr;
65  // RixInterface for PRManBegin/End.
66  RixRiCtl *ri = nullptr;
67  // RixInterface for Riley.
68  RixRileyManager *mgr = nullptr;
69  // Riley instance.
70  riley::Riley *riley = nullptr;
71 
72  // Xcpt Handler
74 
75  // A fallback material to use for any geometry that
76  // does not have a bound material.
77  riley::MaterialId fallbackMaterial;
78  riley::MaterialId fallbackVolumeMaterial;
79  // The fallback set of time samples to evalate,
80  // if there is not a relevant entry in timeSampleMap.
81  std::vector<float> defaultTimeSamples;
82  // Per-scope configuration of time samples to evaluate.
84 
85  // A helper to look up the set of time samples to evalutae
86  // for the given Hydra id.
88  const std::vector<float>& GetTimeSamplesForId(SdfPath const&);
89 
90  // Convert any Hydra primvars that should be Riley instance attributes.
92  RtParamList
93  ConvertAttributes(HdSceneDelegate *sceneDelegate, SdfPath const& id);
94 
95  // A vector of Riley coordinate system id's.
96  typedef std::vector<riley::CoordinateSystemId> RileyCoordSysIdVec;
97  // A ref-counting ptr to a vector of coordinate systems.
98  typedef std::shared_ptr<RileyCoordSysIdVec> RileyCoordSysIdVecRefPtr;
99 
100  /// Convert any coordinate system bindings for the given rprim id
101  /// into a Riley equivalent form. Retain the result internally
102  /// in a cache, so that we may re-use the result with other
103  /// rprims with the same set of bindings.
107  HdSceneDelegate *sceneDelegate,
108  SdfPath const& id);
109 
110  /// Convert a list of categories returned by Hydra to
111  /// equivalent Prman grouping attributes.
113  void
116  RtParamList& attrs);
117 
118  /// Release any coordinate system bindings cached for the given
119  /// rprim id.
121  void ReleaseCoordSysBindings(SdfPath const& id);
122 
124  void IncrementLightLinkCount(TfToken const& name);
125 
127  void DecrementLightLinkCount(TfToken const& name);
128 
130  bool IsLightLinkUsed(TfToken const& name);
131 
134  RtParamList& options);
135 
136  // Set integrator params from the HdRenderSettingsMap
139  std::string& integratorName,
140  RtParamList& params);
141 
143  bool IsInteractive() const;
144 
146  void SetIsInteractive(bool isInteractive);
147 
149  bool IsShutterInstantaneous() const;
150 
152  void SetInstantaneousShutter(bool instantaneousShutter);
153 
154  virtual ~HdPrman_Context() = default;
155 
156 private:
157  // Refcounts for each category mentioned by a light link.
158  // This is used to convey information from lights back to the
159  // geometry -- in Renderman, geometry must subscribe
160  // to the linked lights.
161  std::unordered_map<TfToken, size_t, TfToken::HashFunctor> _lightLinkRefs;
162 
163  // Mutex protecting lightLinkRefs.
164  std::mutex _lightLinkMutex;
165 
166  // Map from Hydra coordinate system vector pointer to Riley equivalent.
167  typedef std::unordered_map<
169  _HdToRileyCoordSysMap;
170  // Map from Hydra id to cached, converted coordinate systems.
171  typedef std::unordered_map<
173  _GeomToHdCoordSysMap;
174 
175  // Coordinate system conversion cache.
176  _GeomToHdCoordSysMap _geomToHdCoordSysMap;
177  _HdToRileyCoordSysMap _hdToRileyCoordSysMap;
178  std::mutex _coordSysMutex;
179 
180  // If we are in interactive/viewport or not
181  bool _isInteractive = true;
182 
183  bool _instantaneousShutter;
184 
185 };
186 
187 // Helper to convert matrix types, handling double->float conversion.
188 inline RtMatrix4x4
190 {
191  const double *d = m.GetArray();
192  return RtMatrix4x4(
193  d[0], d[1], d[2], d[3],
194  d[4], d[5], d[6], d[7],
195  d[8], d[9], d[10], d[11],
196  d[12], d[13], d[14], d[15]);
197 }
198 
199 // Helper to convert matrix types, handling float->double conversion.
200 inline GfMatrix4d
201 HdPrman_RtMatrixToGfMatrix(const RtMatrix4x4 &m)
202 {
203  return GfMatrix4d(
204  m.m[0][0], m.m[0][1], m.m[0][2], m.m[0][3],
205  m.m[1][0], m.m[1][1], m.m[1][2], m.m[1][3],
206  m.m[2][0], m.m[2][1], m.m[2][2], m.m[2][3],
207  m.m[3][0], m.m[3][1], m.m[3][2], m.m[3][3]);
208 }
209 
210 // Convert any Hydra primvars that should be Riley primvars.
211 void
212 HdPrman_ConvertPrimvars(HdSceneDelegate *sceneDelegate, SdfPath const& id,
213  RtParamList& primvars, int numUniform, int numVertex,
214  int numVarying, int numFaceVarying);
215 
216 // Resolve Hd material ID to the corresponding Riley material & displacement
217 bool
219  SdfPath const& hdMaterialId,
220  riley::MaterialId *materialId,
221  riley::DisplacementId *dispId);
222 
223 
224 /// Update the supplied list of options using searchpaths
225 /// pulled from envrionment variables:
226 ///
227 /// - RMAN_SHADERPATH
228 /// - RMAN_TEXTUREPATH
229 /// - RMAN_RIXPLUGINPATH
230 /// - RMAN_PROCEDURALPATH
231 ///
233 void
234 HdPrman_UpdateSearchPathsFromEnvironment(RtParamList& options);
235 
237 
238 #endif // HDPRMAN_CONTEXT_H
std::shared_ptr< SdfPathVector > HdIdVectorSharedPtr
A shared pointer to a vector of id's.
Definition: sceneDelegate.h:56
HDPRMAN_API void HdPrman_UpdateSearchPathsFromEnvironment(RtParamList &options)
RtMatrix4x4 HdPrman_GfMatrixToRtMatrix(const GfMatrix4d &m)
Definition: context.h:189
GLuint const GLchar * name
Definition: glew.h:1814
riley::MaterialId fallbackVolumeMaterial
Definition: context.h:78
HDPRMAN_API void SetInstantaneousShutter(bool instantaneousShutter)
HDPRMAN_API void ConvertCategoriesToAttributes(SdfPath const &id, VtArray< TfToken > const &categories, RtParamList &attrs)
GfMatrix4d HdPrman_RtMatrixToGfMatrix(const RtMatrix4x4 &m)
Definition: context.h:201
HDPRMAN_API void DecrementLightLinkCount(TfToken const &name)
RixRileyManager * mgr
Definition: context.h:68
HDPRMAN_API const std::vector< float > & GetTimeSamplesForId(SdfPath const &)
HDPRMAN_API void SetIsInteractive(bool isInteractive)
HdPrman_Xcpt xcpt
Definition: context.h:73
HDPRMAN_API RileyCoordSysIdVecRefPtr ConvertAndRetainCoordSysBindings(HdSceneDelegate *sceneDelegate, SdfPath const &id)
riley::MaterialId fallbackMaterial
Definition: context.h:77
const GLdouble * m
Definition: glew.h:9124
HDPRMAN_API RtParamList ConvertAttributes(HdSceneDelegate *sceneDelegate, SdfPath const &id)
RixContext * rix
Definition: context.h:64
std::vector< float > defaultTimeSamples
Definition: context.h:81
HDPRMAN_API void SetOptionsFromRenderSettings(HdPrmanRenderDelegate *renderDelegate, RtParamList &options)
HDPRMAN_API bool IsShutterInstantaneous() const
HDPRMAN_API void IncrementLightLinkCount(TfToken const &name)
bool HdPrman_ResolveMaterial(HdSceneDelegate *sceneDelegate, SdfPath const &hdMaterialId, riley::MaterialId *materialId, riley::DisplacementId *dispId)
RixRiCtl * ri
Definition: context.h:66
Definition: token.h:89
GLsizei GLenum * categories
Definition: glew.h:2581
virtual ~HdPrman_Context()=default
riley::Riley * riley
Definition: context.h:70
double * GetArray()
Returns vector components as an array of double values.
Definition: matrix4d.h:285
Definition: path.h:287
HDPRMAN_API bool IsLightLinkUsed(TfToken const &name)
void HdPrman_ConvertPrimvars(HdSceneDelegate *sceneDelegate, SdfPath const &id, RtParamList &primvars, int numUniform, int numVertex, int numVarying, int numFaceVarying)
HDPRMAN_API void SetIntegratorParamsFromRenderSettings(HdPrmanRenderDelegate *renderDelegate, std::string &integratorName, RtParamList &params)
std::shared_ptr< RileyCoordSysIdVec > RileyCoordSysIdVecRefPtr
Definition: context.h:98
GLsizei const GLchar *const * string
Definition: glew.h:1844
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1245
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:83
#define HDPRMAN_API
Definition: api.h:40
std::map< SdfPath, std::vector< float > > HdPrman_TimeSampleMap
Hierarchical map of scene ids to sample times.
Definition: context.h:54
const GLfloat * params
Definition: glew.h:1531
HDPRMAN_API bool IsInteractive() const
HdPrman_TimeSampleMap timeSampleMap
Definition: context.h:83
std::vector< riley::CoordinateSystemId > RileyCoordSysIdVec
Definition: context.h:96
HDPRMAN_API void ReleaseCoordSysBindings(SdfPath const &id)