Free Electron
HydraDelegate.h
1 //
2 // Copyright 2016 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 // originally from pxr/imaging/hd/unitHydraDelegate.h
25 // also from pxr/imaging/hdSt/unitTestDelegate.h
26 // adapted for Free Electron
27 
28 #ifndef __usd_HydraDelegate__
29 #define __usd_HydraDelegate__
30 
31 #include "pxr/imaging/hd/sceneDelegate.h"
32 #include "pxr/imaging/hd/tokens.h"
33 #include "pxr/imaging/pxOsd/tokens.h"
34 #include "pxr/imaging/glf/texture.h"
35 
36 #include "pxr/base/gf/vec3f.h"
37 #include "pxr/base/gf/vec3d.h"
38 #include "pxr/base/gf/vec4f.h"
39 #include "pxr/base/gf/vec4d.h"
40 #include "pxr/base/gf/matrix4f.h"
41 #include "pxr/base/gf/matrix4d.h"
42 #include "pxr/base/vt/array.h"
43 #include "pxr/base/tf/staticTokens.h"
44 
45 #define TEST_TOKENS (geometryAndGuides)
46 
47 TF_DECLARE_PUBLIC_TOKENS(TestTokens,TEST_TOKENS);
48 
49 /// \class HydraDelegate
50 /// A simple delegate class for unit test driver.
51 class HydraDelegate: public HdSceneDelegate
52 {
53  public:
54  HydraDelegate(HdRenderIndex *parentIndex,
55  SdfPath const& delegateID);
56 
57  void SetUseInstancePrimVars(bool v) { m_hasInstancePrimVars = v; }
58 
59  void SetRefineLevel(int level);
60 
61  void SetCamera(GfMatrix4d const &viewMatrix,
62  GfMatrix4d const &projMatrix);
63  void SetCamera(SdfPath const &id, GfMatrix4d const &viewMatrix,
64  GfMatrix4d const &projMatrix);
65  void AddCamera(SdfPath const &id);
66 
67  bool HasLight(SdfPath const &id);
68  void AddLight(SdfPath const &id, GlfSimpleLight const &light);
69  void SetLight(SdfPath const &id, TfToken const &key, VtValue value);
70 
71  void AddRenderTask(SdfPath const &id);
72  void AddRenderSetupTask(SdfPath const &id);
73  void AddSimpleLightTask(SdfPath const &id);
74  void AddShadowTask(SdfPath const &id);
75  void AddSelectionTask(SdfPath const &id);
76 
77  void AdjustRenderSetupTask(SdfPath const &id,BWORD a_lighting,
78  const GfVec4f& viewport);
79 
80  void SetTaskParam(SdfPath const &id,
81  TfToken const &name, VtValue val);
82  VtValue GetTaskParam(SdfPath const &id, TfToken const &name);
83 
84  HdTaskSharedPtrVector GetRenderTasks(void);
85 
86  void AddMesh(SdfPath const &id,
87  GfMatrix4f const &transform,
88  VtVec3rArray const &points,
89  VtIntArray const &numVerts,
90  VtIntArray const &verts,
91  VtVec3rArray const &normals,
92  PxOsdSubdivTags const &subdivTags,
93  VtValue const &uv,
94  HdInterpolation uvInterpolation,
95  VtValue const &color,
96  HdInterpolation colorInterpolation,
97  bool guide=false,
98  SdfPath const &instancerId=SdfPath(),
99  TfToken const &scheme=PxOsdOpenSubdivTokens->catmullClark,
100  TfToken const &orientation=HdTokens->rightHanded,
101  bool doubleSided=false);
102 
103  void AddBasisCurves(SdfPath const &id,
104  GfMatrix4f const &transform,
105  VtVec3rArray const &points,
106  VtIntArray const &curveVertexCounts,
107  VtVec3rArray const &normals,
108  TfToken const &basis,
109  VtValue const &color,
110  HdInterpolation colorInterpolation,
111  VtValue const &width,
112  HdInterpolation widthInterpolation,
113  SdfPath const &instancerId=SdfPath());
114 
115  void AddPoints(SdfPath const &id,
116  GfMatrix4f const &transform,
117  VtVec3rArray const &points,
118  VtVec3rArray const &normals,
119  VtValue const &color,
120  HdInterpolation colorInterpolation,
121  VtValue const &width,
122  HdInterpolation widthInterpolation,
123  SdfPath const &instancerId=SdfPath());
124 
125  void AddInstancer(SdfPath const &id,
126  SdfPath const &parentId=SdfPath(),
127  GfMatrix4f const &rootTransform=GfMatrix4f(1));
128 
129  void SetInstancerProperties(SdfPath const &id,
130  VtIntArray const &prototypeIndex,
131  VtVec3rArray const &scale,
132  VtVec4fArray const &rotate,
133  VtVec3rArray const &translate);
134 
135 // void AddSurfaceShader(SdfPath const &id,
136 // std::string const &source,
137 // HdShaderParamVector const &params);
138 
139  void AddTexture(SdfPath const& id,
140  GlfTextureRefPtr const& texture);
141 
142  /// Material
143  void AddMaterialResource(SdfPath const &id,
144  VtValue materialResource);
145 
146  /// Update a material resource
147  void UpdateMaterialResource(SdfPath const &materialId,
148  VtValue materialResource);
149 
150  void BindMaterial(SdfPath const &rprimId,
151  SdfPath const &materialId);
152 
153  /// Example to update a material binding on the fly
154  void RebindMaterial(SdfPath const &rprimId,
155  SdfPath const &materialId);
156 
157 virtual SdfPath GetMaterialId(SdfPath const& rprimId) override;
158 
159 virtual VtValue GetMaterialResource(SdfPath const &materialId) override;
160 
161  void Remove(SdfPath const &id);
162 
163  /// Remove all
164  void Clear(void);
165 
166  /// Hides an rprim, invalidating all collections it was in.
167  void HideRprim(SdfPath const &id);
168 
169  /// Un-hides an rprim, invalidating all collections it was in.
170  void UnhideRprim(SdfPath const &id);
171 
172  /// set per-prim repr
173  void SetReprSelector(SdfPath const &id,
174  HdReprSelector const &reprSelector);
175 
176  /// set per-prim refine level
177  void SetRefineLevel(SdfPath const &id, int refineLevel);
178 
179  /// Marks an rprim in the RenderIndex as dirty
180  /// with the given dirty flags.
181  void MarkRprimDirty(SdfPath path,
182  HdChangeTracker::RprimDirtyBits flag);
183 
184  void UpdatePositions(SdfPath const &id, float time);
185  void UpdateRprims(float time);
186  void UpdateInstancerPrimVars(float time);
187  void UpdateInstancerPrototypes(float time);
188 
189  void UpdateCamera(SdfPath const &id,
190  TfToken const &key, VtValue value);
191 
192  void BindSurfaceShader(SdfPath const &rprimId,
193  SdfPath const &shaderId)
194  { m_surfaceShaderBindings[rprimId] = shaderId; }
195 
196  GfRange3d GetExtents(void);
197 
198 virtual bool IsInCollection(SdfPath const& id,
199  TfToken const& collectionName);
200 virtual HdBasisCurvesTopology GetBasisCurvesTopology(SdfPath const& id);
201 virtual PxOsdSubdivTags GetSubdivTags(SdfPath const& id);
202 virtual GfRange3d GetExtent(SdfPath const & id) override;
203 virtual GfMatrix4d GetTransform(SdfPath const & id) override;
204 virtual bool GetVisible(SdfPath const & id) override;
205 virtual HdMeshTopology GetMeshTopology(SdfPath const& id) override;
206 virtual bool GetDoubleSided(SdfPath const & id);
207 //virtual int GetRefineLevel(SdfPath const & id);
208 virtual HdDisplayStyle GetDisplayStyle(SdfPath const & id) override;
209 virtual VtValue Get(SdfPath const& id, TfToken const& key);
210 
211 virtual HdReprSelector GetReprSelector(SdfPath const &id) override;
212 
213 #if HD_API_VERSION>=32
214 virtual HdPrimvarDescriptorVector GetPrimvarDescriptors(SdfPath const& id,
215  HdInterpolation interpolation) override;
216 #else
217 virtual TfTokenVector GetPrimVarVertexNames(SdfPath const& id);
218 virtual TfTokenVector GetPrimVarVaryingNames(SdfPath const& id);
219 virtual TfTokenVector GetPrimVarFacevaryingNames(SdfPath const& id);
220 virtual TfTokenVector GetPrimVarUniformNames(SdfPath const& id);
221 virtual TfTokenVector GetPrimVarConstantNames(SdfPath const& id);
222 virtual TfTokenVector GetPrimVarInstanceNames(SdfPath const& id);
223 virtual int GetPrimVarDataType(SdfPath const& id,
224  TfToken const& key);
225 virtual int GetPrimVarComponents(SdfPath const& id,
226  TfToken const& key);
227 #endif
228 
229 virtual VtIntArray GetInstanceIndices(SdfPath const& instancerId,
230  SdfPath const& prototypeId) override;
231 virtual GfMatrix4d GetInstancerTransform(
232  SdfPath const& instancerId) override;
233 virtual VtValue GetCameraParamValue(
234  SdfPath const &cameraId,
235  TfToken const &paramName) override;
236 
237 //virtual std::string GetSurfaceShaderSource(SdfPath const &shaderId);
238 //virtual TfTokenVector GetSurfaceShaderParamNames(SdfPath const &shaderId);
239 //virtual HdShaderParamVector GetSurfaceShaderParams(SdfPath const &shaderId);
240 //virtual VtValue GetSurfaceShaderParamValue(SdfPath const &shaderId,
241 // TfToken const &paramName);
242 
243 //virtual HdTextureResource::ID GetTextureResourceID(SdfPath const& textureId);
244 //virtual HdTextureResourceSharedPtr GetTextureResource(SdfPath const& textureId);
245 
246  void SetTransform(SdfPath const& id,
247  GfMatrix4f const &transform);
248 
249 private:
250 
251  struct Mesh
252  {
253  Mesh(void) { }
254  Mesh(TfToken const &scheme,
255  TfToken const &orientation,
256  GfMatrix4f const &transform,
257  VtVec3rArray const &points,
258  VtIntArray const &numVerts,
259  VtIntArray const &verts,
260  VtVec3rArray const &normals,
261  PxOsdSubdivTags const &subdivTags,
262  VtValue const &uv,
263  HdInterpolation uvInterpolation,
264  VtValue const &color,
265  HdInterpolation colorInterpolation,
266  bool guide,
267  bool doubleSided):
268  scheme(scheme),
269  orientation(orientation),
270  transform(transform),
271  points(points),
272  numVerts(numVerts),
273  verts(verts),
274  normals(normals),
275  subdivTags(subdivTags),
276  uv(uv),
277  uvInterpolation(uvInterpolation),
278  color(color),
279  colorInterpolation(colorInterpolation),
280  guide(guide),
281  doubleSided(doubleSided) {}
282 
283  TfToken scheme;
284  TfToken orientation;
285  GfMatrix4f transform;
286  VtVec3rArray points;
287  VtIntArray numVerts;
288  VtIntArray verts;
289  VtVec3rArray normals;
290  PxOsdSubdivTags subdivTags;
291  VtValue uv;
292  HdInterpolation uvInterpolation;
293  VtValue color;
294  HdInterpolation colorInterpolation;
295  bool guide;
296  bool doubleSided;
297  HdReprSelector reprSelector;
298  };
299 
300  struct Curves
301  {
302  Curves(void) {}
303 
304  Curves(
305  GfMatrix4f const &transform,
306  VtIntArray const &curveVertexCounts,
307  VtIntArray const &curveIndices,
308  VtVec3rArray const &points,
309  VtVec3rArray const &normals,
310  TfToken const &basis,
311  VtValue const &color,
312  HdInterpolation colorInterpolation,
313  VtValue const &width,
314  HdInterpolation widthInterpolation) :
315  curveVertexCounts(curveVertexCounts),
316  curveIndices(curveIndices),
317  points(points),
318  normals(normals),
319  basis(basis),
320  color(color),
321  colorInterpolation(colorInterpolation),
322  width(width),
323  widthInterpolation(widthInterpolation) {}
324 
325  GfMatrix4f transform;
326  VtIntArray curveVertexCounts;
327  VtIntArray curveIndices;
328  VtVec3rArray points;
329  VtVec3rArray normals;
330  TfToken basis;
331  VtValue color;
332  HdInterpolation colorInterpolation;
333  VtValue width;
334  HdInterpolation widthInterpolation;
335  HdReprSelector reprSelector;
336  };
337 
338  struct Points
339  {
340  Points(void) {}
341 
342  Points(
343  GfMatrix4f const &transform,
344  VtVec3rArray const &points,
345  VtVec3rArray const &normals,
346  VtValue const &color,
347  HdInterpolation colorInterpolation,
348  VtValue const &width,
349  HdInterpolation widthInterpolation):
350  points(points),
351  normals(normals),
352  color(color),
353  colorInterpolation(colorInterpolation),
354  width(width),
355  widthInterpolation(widthInterpolation) {}
356 
357  GfMatrix4f transform;
358  VtVec3rArray points;
359  VtVec3rArray normals;
360  VtValue color;
361  HdInterpolation colorInterpolation;
362  VtValue width;
363  HdInterpolation widthInterpolation;
364  HdReprSelector reprSelector;
365  };
366 
367  struct Instancer
368  {
369  Instancer(void) {}
370 
371  Instancer(VtVec3rArray const &scale,
372  VtVec4fArray const &rotate,
373  VtVec3rArray const &translate,
374  GfMatrix4f const &rootTransform):
375  scale(scale),
376  rotate(rotate),
377  translate(translate),
378  rootTransform(rootTransform) {}
379 
380  VtVec3rArray scale;
381  VtVec4fArray rotate;
382  VtVec3rArray translate;
383  VtIntArray prototypeIndices;
384  GfMatrix4f rootTransform;
385  std::vector<SdfPath> prototypes;
386  };
387 
388 // struct SurfaceShader
389 // {
390 // SurfaceShader(void) {}
391 //
392 // SurfaceShader(std::string const &src,
393 // HdShaderParamVector const &pms):
394 // source(src),
395 // params(pms) {}
396 //
397 // std::string source;
398 // HdShaderParamVector params;
399 // };
400 
401  struct Texture
402  {
403  Texture(void) {}
404 
405  Texture(GlfTextureRefPtr const &tex):
406  texture(tex) {}
407  GlfTextureRefPtr texture;
408  };
409 
410  std::map<SdfPath,Mesh> m_meshes;
411  std::map<SdfPath,Curves> m_curves;
412  std::map<SdfPath,Points> m_points;
413  std::map<SdfPath,Instancer> m_instancers;
414 // std::map<SdfPath,SurfaceShader> m_surfaceShaders;
415  std::map<SdfPath,SdfPath> m_surfaceShaderBindings;
416  std::map<SdfPath,Texture> m_textures;
417  std::map<SdfPath, VtValue> m_materials;
418  TfHashSet<SdfPath,SdfPath::Hash> m_hiddenRprims;
419 
420  typedef std::map<SdfPath, SdfPath> SdfPathMap;
421  SdfPathMap m_materialBindings;
422 
423  SdfPath m_cameraId;
424  bool m_runShadows;
425  bool m_hasInstancePrimVars;
426  int m_refineLevel;
427  std::map<SdfPath, int> m_refineLevels;
428 
429  typedef TfHashMap<TfToken, VtValue, TfToken::HashFunctor> ValueCache;
430  typedef TfHashMap<SdfPath, ValueCache, SdfPath::Hash> ValueCacheMap;
431 
432  ValueCacheMap m_valueCacheMap;
433 };
434 
435 #endif // __usd_HydraDelegate__
void AddMaterialResource(SdfPath const &id, VtValue materialResource)
Material.
Definition: HydraDelegate.cc:713
void SetReprSelector(SdfPath const &id, HdReprSelector const &reprSelector)
set per-prim repr
Definition: HydraDelegate.cc:787
void Clear(void)
Remove all.
Definition: HydraDelegate.cc:1744
void UnhideRprim(SdfPath const &id)
Un-hides an rprim, invalidating all collections it was in.
Definition: HydraDelegate.cc:781
A simple delegate class for unit test driver.
Definition: HydraDelegate.h:51
void MarkRprimDirty(SdfPath path, HdChangeTracker::RprimDirtyBits flag)
Marks an rprim in the RenderIndex as dirty with the given dirty flags.
Definition: HydraDelegate.cc:1749
void HideRprim(SdfPath const &id)
Hides an rprim, invalidating all collections it was in.
Definition: HydraDelegate.cc:775
void UpdateMaterialResource(SdfPath const &materialId, VtValue materialResource)
Update a material resource.
Definition: HydraDelegate.cc:721
void RebindMaterial(SdfPath const &rprimId, SdfPath const &materialId)
Example to update a material binding on the fly.
Definition: HydraDelegate.cc:747