All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
batchRenderer.h
Go to the documentation of this file.
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 #ifndef PXRUSDMAYAGL_BATCH_RENDERER_H
25 #define PXRUSDMAYAGL_BATCH_RENDERER_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxrUsdMayaGL/api.h"
36 #include "usdMaya/notice.h"
37 #include "usdMaya/util.h"
38 
39 #include "pxr/base/gf/matrix4d.h"
40 #include "pxr/base/gf/vec2i.h"
41 #include "pxr/base/gf/vec3f.h"
42 #include "pxr/base/gf/vec4d.h"
43 #include "pxr/base/tf/singleton.h"
44 #include "pxr/base/tf/weakBase.h"
45 #include "pxr/imaging/hd/engine.h"
46 #include "pxr/imaging/hd/renderIndex.h"
47 #include "pxr/imaging/hd/rprimCollection.h"
48 #include "pxr/imaging/hdSt/renderDelegate.h"
49 #include "pxr/imaging/hdx/selectionTracker.h"
50 #include "pxr/imaging/hdx/pickTask.h"
51 #include "pxr/usd/sdf/path.h"
52 
53 #include <maya/M3dView.h>
54 #include <maya/MDrawContext.h>
55 #include <maya/MDrawRequest.h>
56 #include <maya/MObjectHandle.h>
57 #include <maya/MMessage.h>
58 #include <maya/MSelectInfo.h>
59 #include <maya/MSelectionContext.h>
60 #include <maya/MTypes.h>
61 #include <maya/MUserData.h>
62 
63 #include <memory>
64 #include <utility>
65 #include <unordered_map>
66 #include <unordered_set>
67 
68 
69 PXR_NAMESPACE_OPEN_SCOPE
70 
71 
96  : public TfSingleton<UsdMayaGLBatchRenderer>, public TfWeakBase
97 {
98 public:
99 
104  PXRUSDMAYAGL_API
105  static void Init();
106 
108  PXRUSDMAYAGL_API
110 
115  PXRUSDMAYAGL_API
116  HdRenderIndex* GetRenderIndex() const;
117 
125  PXRUSDMAYAGL_API
126  SdfPath GetDelegatePrefix(const bool isViewport2) const;
127 
132  PXRUSDMAYAGL_API
133  bool AddShapeAdapter(PxrMayaHdShapeAdapter* shapeAdapter);
134 
139  PXRUSDMAYAGL_API
140  bool RemoveShapeAdapter(PxrMayaHdShapeAdapter* shapeAdapter);
141 
148  PXRUSDMAYAGL_API
149  static void Reset();
150 
164  PXRUSDMAYAGL_API
166  const MDagPath& dagPath,
167  PxrMayaHdPrimFilter& primFilter);
168 
170  PXRUSDMAYAGL_API
171  void Draw(const MDrawRequest& request, M3dView& view);
172 
174  PXRUSDMAYAGL_API
175  void Draw(
176  const MHWRender::MDrawContext& context,
177  const MUserData* userData);
178 
182  PXRUSDMAYAGL_API
184 
192  PXRUSDMAYAGL_API
193  void SetSelectionResolution(const GfVec2i& widthHeight);
194 
196  PXRUSDMAYAGL_API
197  bool IsDepthSelectionEnabled() const;
198 
200  PXRUSDMAYAGL_API
201  void SetDepthSelectionEnabled(const bool enabled);
202 
212  PXRUSDMAYAGL_API
213  const HdxPickHitVector* TestIntersection(
214  const PxrMayaHdShapeAdapter* shapeAdapter,
215  MSelectInfo& selectInfo);
216 
226  PXRUSDMAYAGL_API
227  const HdxPickHitVector* TestIntersection(
228  const PxrMayaHdShapeAdapter* shapeAdapter,
229  const MHWRender::MSelectionInfo& selectionInfo,
230  const MHWRender::MDrawContext& context);
231 
241  PXRUSDMAYAGL_API
243  const PxrMayaHdPrimFilter& primFilter,
244  const GfMatrix4d& viewMatrix,
245  const GfMatrix4d& projectionMatrix,
246  HdxPickHitVector* outResult);
247 
253  PXRUSDMAYAGL_API
254  static const HdxPickHit* GetNearestHit(
255  const HdxPickHitVector* hitSet);
256 
260  PXRUSDMAYAGL_API
261  int GetInstancerIndexForHit(const HdxPickHit& hit) const;
262 
264  PXRUSDMAYAGL_API
266  { return _objectSoftSelectEnabled; }
267 
271  PXRUSDMAYAGL_API
273 
274 private:
275 
276  friend class TfSingleton<UsdMayaGLBatchRenderer>;
277 
278  PXRUSDMAYAGL_API
280 
281  PXRUSDMAYAGL_API
282  virtual ~UsdMayaGLBatchRenderer();
283 
287  PXRUSDMAYAGL_API
288  const UsdMayaGLSoftSelectHelper& GetSoftSelectHelper();
289 
291  friend PxrMayaHdShapeAdapter;
292 
293  typedef std::pair<PxrMayaHdRenderParams, PxrMayaHdPrimFilterVector>
294  _RenderItem;
295 
299  void _Render(
300  const GfMatrix4d& worldToViewMatrix,
301  const GfMatrix4d& projectionMatrix,
302  const GfVec4d& viewport,
303  const std::vector<_RenderItem>& items);
304 
310  void _RenderBatches(
311  const MHWRender::MDrawContext* vp2Context,
312  const M3dView* view3d,
313  const GfMatrix4d& worldToViewMatrix,
314  const GfMatrix4d& projectionMatrix,
315  const GfVec4d& viewport);
316 
321  bool _TestIntersection(
322  const HdRprimCollection& rprimCollection,
323  const TfTokenVector& renderTags,
324  const GfMatrix4d& viewMatrix,
325  const GfMatrix4d& projectionMatrix,
326  const bool singleSelection,
327  HdxPickHitVector* result);
328 
329  // Handler for Maya scene resets (e.g. new scene or switch scenes).
330  void _OnMayaSceneReset(const UsdMayaSceneResetNotice& notice);
331 
340  static void _OnMayaEndRenderCallback(
341  MHWRender::MDrawContext& context,
342  void* clientData);
343 
348  static void _OnSoftSelectOptionsChangedCallback(void* clientData);
349 
357  void _MayaRenderDidEnd(const MHWRender::MDrawContext* vp2Context);
358 
363  bool _UpdateIsSelectionPending(const bool isPending);
364 
367  bool _isSelectionPending;
368 
369  bool _objectSoftSelectEnabled;
370  MCallbackId _softSelectOptionsCallbackId;
371 
373  typedef std::unordered_set<PxrMayaHdShapeAdapter*> _ShapeAdapterSet;
374 
379  typedef std::pair<PxrMayaHdRenderParams, _ShapeAdapterSet> _ShapeAdapterBucket;
380 
384  typedef std::unordered_map<size_t, _ShapeAdapterBucket> _ShapeAdapterBucketsMap;
385 
388  _ShapeAdapterBucketsMap _shapeAdapterBuckets;
389 
390  _ShapeAdapterBucketsMap _legacyShapeAdapterBuckets;
391 
395  _ShapeAdapterHandleMap;
396 
399  _ShapeAdapterHandleMap _shapeAdapterHandleMap;
400 
401  _ShapeAdapterHandleMap _legacyShapeAdapterHandleMap;
402 
410  bool _viewport2UsesLegacySelection;
411 
424  PxrMayaHdPrimFilterVector _GetIntersectionPrimFilters(
425  _ShapeAdapterBucketsMap& bucketsMap,
426  const M3dView* view,
427  const bool useDepthSelection) const;
428 
435  void _ComputeSelection(
436  _ShapeAdapterBucketsMap& bucketsMap,
437  const M3dView* view3d,
438  const GfMatrix4d& viewMatrix,
439  const GfMatrix4d& projectionMatrix,
440  const bool singleSelection);
441 
445  std::unordered_map<SdfPath, HdxPickHitVector, SdfPath::Hash> _selectResults;
446 
450  typedef std::tuple<GfMatrix4d, GfMatrix4d, bool> _SelectResultsKey;
451  _SelectResultsKey _selectResultsKey;
452 
461  HdEngine _hdEngine;
462  HdStRenderDelegate _renderDelegate;
463  std::unique_ptr<HdRenderIndex> _renderIndex;
464 
468  SdfPath _rootId;
469  SdfPath _legacyViewportPrefix;
470  SdfPath _viewport2Prefix;
471 
474  HdRprimCollection _legacyViewportRprimCollection;
475  HdRprimCollection _viewport2RprimCollection;
476 
477  PxrMayaHdSceneDelegateSharedPtr _taskDelegate;
478 
479  GfVec2i _selectionResolution;
480  bool _enableDepthSelection;
481 
482  HdxSelectionTrackerSharedPtr _selectionTracker;
483 
484  UsdMayaGLSoftSelectHelper _softSelectHelper;
485 
490  std::unique_ptr<UsdMayaDiagnosticBatchContext> _sharedDiagBatchCtx;
491 };
492 
493 PXRUSDMAYAGL_API_TEMPLATE_CLASS(TfSingleton<UsdMayaGLBatchRenderer>);
494 
495 
496 PXR_NAMESPACE_CLOSE_SCOPE
497 
498 
499 #endif
PXRUSDMAYAGL_API void SetDepthSelectionEnabled(const bool enabled)
Sets whether to enable depth selection.
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:119
Manage a single instance of an object (see.
Definition: singleton.h:122
PXRUSDMAYAGL_API GfVec2i GetSelectionResolution() const
Gets the resolution of the draw target used for computing selections.
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
PXRUSDMAYAGL_API bool RemoveShapeAdapter(PxrMayaHdShapeAdapter *shapeAdapter)
Remove the given shape adapter from batched rendering and selection.
static PXRUSDMAYAGL_API void Reset()
Reset the internal state of the global UsdMayaGLBatchRenderer.
PXRUSDMAYAGL_API bool GetObjectSoftSelectEnabled()
Returns whether soft selection for proxy shapes is currently enabled.
Basic type for a vector of 4 double components.
Definition: vec4d.h:63
static PXRUSDMAYAGL_API const HdxPickHit * GetNearestHit(const HdxPickHitVector *hitSet)
Utility function for finding the nearest hit (in terms of ndcDepth) in the given hitSet.
PXRUSDMAYAGL_API void StartBatchingFrameDiagnostics()
Starts batching all diagnostics until the end of the current frame draw.
PXRUSDMAYAGL_API HdRenderIndex * GetRenderIndex() const
Get the render index owned by the batch renderer.
Abstract base class for objects that manage translation of Maya shape node data and viewport state fo...
Definition: shapeAdapter.h:64
Picking hit structure.
Definition: pickTask.h:93
PXRUSDMAYAGL_API int GetInstancerIndexForHit(const HdxPickHit &hit) const
Returns the index within the point instancer for hit.
Helper class to store soft (&quot;rich&quot;) selection state while computing render params for a frame...
HdStRenderDelegate.
PXRUSDMAYAGL_API bool AddShapeAdapter(PxrMayaHdShapeAdapter *shapeAdapter)
Add the given shape adapter for batched rendering and selection.
PXRUSDMAYAGL_API bool PopulateCustomPrimFilter(const MDagPath &dagPath, PxrMayaHdPrimFilter &primFilter)
Replaces the contents of the given primFilter with dagPath, if a shape adapter for dagPath has alread...
PXRUSDMAYAGL_API const HdxPickHitVector * TestIntersection(const PxrMayaHdShapeAdapter *shapeAdapter, MSelectInfo &selectInfo)
Tests the object from the given shape adapter for intersection with a given selection context in the ...
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
The application-facing entry point top-level entry point for accessing Hydra.
Definition: engine.h:48
PXRUSDMAYAGL_API void Draw(const MDrawRequest &request, M3dView &view)
Render batch or bounding box in the legacy viewport based on request.
static PXRUSDMAYAGL_API void Init()
Initialize the batch renderer.
static PXRUSDMAYAGL_API UsdMayaGLBatchRenderer & GetInstance()
Get the singleton instance of the batch renderer.
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
PXRUSDMAYAGL_API SdfPath GetDelegatePrefix(const bool isViewport2) const
Get the delegate ID prefix for the specified viewport.
Notice sent when the Maya scene resets, either by opening a new scene or switching to a new scene...
Definition: notice.h:42
std::unordered_map< MObjectHandle, V, _HashObjectHandle > MObjectHandleUnorderedMap
Unordered mapping of Maya object handles to an arbitrary type.
Definition: util.h:114
UsdMayaGLBatchRenderer is a singleton that shapes can use to get consistent batched drawing via Hydra...
Definition: batchRenderer.h:95
PXRUSDMAYAGL_API bool TestIntersectionCustomPrimFilter(const PxrMayaHdPrimFilter &primFilter, const GfMatrix4d &viewMatrix, const GfMatrix4d &projectionMatrix, HdxPickHitVector *outResult)
Tests the contents of the given prim filter (previously obtained via PopulateCustomFilter) for inters...
PXRUSDMAYAGL_API void SetSelectionResolution(const GfVec2i &widthHeight)
Sets the resolution of the draw target used for computing selections.
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:142
PXRUSDMAYAGL_API bool IsDepthSelectionEnabled() const
Gets whether depth selection has been enabled.