All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sceneDelegate.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 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 PXRUSDMAYAGL_SCENE_DELEGATE_H
25 #define PXRUSDMAYAGL_SCENE_DELEGATE_H
26 
28 
29 #include "pxr/pxr.h"
30 
31 #include "pxrUsdMayaGL/api.h"
33 
34 #include "pxr/base/gf/matrix4d.h"
35 #include "pxr/base/gf/vec4d.h"
36 #include "pxr/base/tf/diagnostic.h"
37 #include "pxr/base/tf/hashmap.h"
38 #include "pxr/base/tf/token.h"
39 #include "pxr/base/vt/value.h"
40 #include "pxr/imaging/glf/simpleLightingContext.h"
41 #include "pxr/imaging/hd/renderIndex.h"
42 #include "pxr/imaging/hd/rprimCollection.h"
43 #include "pxr/imaging/hd/sceneDelegate.h"
44 #include "pxr/imaging/hd/task.h"
45 #include "pxr/usd/sdf/path.h"
46 
47 #include <maya/MDrawContext.h>
48 
49 #include <memory>
50 #include <unordered_map>
51 
52 
53 PXR_NAMESPACE_OPEN_SCOPE
54 
55 struct PxrMayaHdPrimFilter {
56  HdRprimCollection collection;
57  TfTokenVector renderTags;
58 };
59 
60 using PxrMayaHdPrimFilterVector = std::vector<PxrMayaHdPrimFilter>;
61 
62 class PxrMayaHdSceneDelegate : public HdSceneDelegate
63 {
64  public:
65  PXRUSDMAYAGL_API
66  PxrMayaHdSceneDelegate(
67  HdRenderIndex* renderIndex,
68  const SdfPath& delegateID);
69 
70  // HdSceneDelegate interface
71  PXRUSDMAYAGL_API
72  VtValue Get(const SdfPath& id, const TfToken& key) override;
73 
74  PXRUSDMAYAGL_API
76  SdfPath const& cameraId,
77  TfToken const& paramName) override;
78 
79  PXRUSDMAYAGL_API
80  TfTokenVector GetTaskRenderTags(SdfPath const& taskId) override;
81 
82  PXRUSDMAYAGL_API
83  void SetCameraState(
84  const GfMatrix4d& worldToViewMatrix,
85  const GfMatrix4d& projectionMatrix,
86  const GfVec4d& viewport);
87 
88  // VP 1.0 only.
89  PXRUSDMAYAGL_API
90  void SetLightingStateFromVP1(
91  const GfMatrix4d& worldToViewMatrix,
92  const GfMatrix4d& projectionMatrix);
93 
94  // VP 2.0 only.
95  PXRUSDMAYAGL_API
96  void SetLightingStateFromMayaDrawContext(
97  const MHWRender::MDrawContext& context);
98 
99  PXRUSDMAYAGL_API
100  HdTaskSharedPtrVector GetSetupTasks();
101 
102  PXRUSDMAYAGL_API
103  HdTaskSharedPtrVector GetRenderTasks(
104  const size_t hash,
105  const PxrMayaHdRenderParams& renderParams,
106  const PxrMayaHdPrimFilterVector& primFilters);
107 
108  PXRUSDMAYAGL_API
109  HdTaskSharedPtrVector GetPickingTasks(
110  const TfTokenVector& renderTags);
111 
112  protected:
113  PXRUSDMAYAGL_API
114  void _SetLightingStateFromLightingContext();
115 
116  template <typename T>
117  const T& _GetValue(const SdfPath& id, const TfToken& key) {
118  VtValue vParams = _valueCacheMap[id][key];
119  if (!TF_VERIFY(vParams.IsHolding<T>(),
120  "For Id = %s, Key = %s",
121  id.GetText(), key.GetText())) {
122  static T ERROR_VALUE;
123  return ERROR_VALUE;
124  }
125  return vParams.UncheckedGet<T>();
126  }
127 
128  template <typename T>
129  void _SetValue(const SdfPath& id, const TfToken& key, const T& value) {
130  _valueCacheMap[id][key] = value;
131  }
132 
133  private:
134  SdfPath _rootId;
135 
136  SdfPath _cameraId;
137  GfVec4d _viewport;
138 
139  SdfPath _simpleLightTaskId;
140  SdfPathVector _lightIds;
141  GlfSimpleLightingContextRefPtr _lightingContext;
142 
143  SdfPath _shadowTaskId;
144 
145  // XXX: While this is correct, that we are using
146  // hash in forming the task id, so the map is valid.
147  // It is possible for the hash to collide, so the id
148  // formed from the combination of hash and collection name is not
149  // necessarily unique.
150  struct _RenderTaskIdMapKey
151  {
152  size_t hash;
153  TfToken collectionName;
154 
155  struct HashFunctor {
156  size_t operator()(const _RenderTaskIdMapKey& value) const;
157  };
158 
159  bool operator==(const _RenderTaskIdMapKey& other) const;
160  };
161 
162  typedef std::unordered_map<
163  _RenderTaskIdMapKey,
164  SdfPath,
165  _RenderTaskIdMapKey::HashFunctor> _RenderTaskIdMap;
166 
167  typedef std::unordered_map<size_t, SdfPath> _RenderParamTaskIdMap;
168 
169 
170 
171  _RenderParamTaskIdMap _renderSetupTaskIdMap;
172  _RenderTaskIdMap _renderTaskIdMap;
173  _RenderParamTaskIdMap _selectionTaskIdMap;
174 
175  SdfPath _pickingTaskId;
176 
177  typedef TfHashMap<TfToken, VtValue, TfToken::HashFunctor> _ValueCache;
178  typedef TfHashMap<SdfPath, _ValueCache, SdfPath::Hash> _ValueCacheMap;
179  _ValueCacheMap _valueCacheMap;
180 };
181 
182 typedef std::shared_ptr<PxrMayaHdSceneDelegate> PxrMayaHdSceneDelegateSharedPtr;
183 
184 
185 PXR_NAMESPACE_CLOSE_SCOPE
186 
187 
188 #endif
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:119
T const & UncheckedGet() const
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:836
virtual HD_API VtValue Get(SdfPath const &id, TfToken const &key)
Returns a named value.
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:808
Basic type for a vector of 4 double components.
Definition: vec4d.h:63
virtual HD_API VtValue GetCameraParamValue(SdfPath const &cameraId, TfToken const &paramName)
Returns a single value for a given camera and parameter.
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
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:289
Adapter class providing data exchange with the client scene graph.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
A named, semantic collection of objects.
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:182
char const * GetText() const
Return the text that this token represents.
Definition: token.h:198