Loading...
Searching...
No Matches
pickTask.h
1//
2// Copyright 2019 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_IMAGING_HDX_PICK_TASK_H
8#define PXR_IMAGING_HDX_PICK_TASK_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/hdx/api.h"
12
13#include "pxr/imaging/hdSt/textureUtils.h"
14#include "pxr/imaging/hd/dataSource.h"
15#include "pxr/imaging/hd/enums.h"
17#include "pxr/imaging/hd/renderPass.h"
18#include "pxr/imaging/hd/renderPassState.h"
19#include "pxr/imaging/hd/rprimCollection.h"
20#include "pxr/imaging/hd/task.h"
21
22#include "pxr/base/arch/align.h"
25#include "pxr/base/gf/vec2i.h"
26#include "pxr/base/gf/vec2f.h"
27#include "pxr/base/gf/vec4i.h"
28#include "pxr/base/gf/vec4d.h"
29#include "pxr/usd/sdf/path.h"
30
31#include <vector>
32#include <memory>
33
34PXR_NAMESPACE_OPEN_SCOPE
35
36#define HDX_PICK_RESOLVE_MODE_TOKENS \
37 (resolveNearestToCamera) \
38 (resolveNearestToCenter) \
39 (resolveUnique) \
40 (resolveAll) \
41 (resolveDeep)
42
44 HdxPickResolveModeTokens, HDX_API, HDX_PICK_RESOLVE_MODE_TOKENS);
45
46#define HDX_PICK_TOKENS \
47 /* Task context */ \
48 (pickParams) \
49 \
50 /* Pick target */ \
51 (pickPrimsAndInstances) \
52 (pickFaces) \
53 (pickEdges) \
54 (pickPoints) \
55 (pickPointsAndInstances) \
56 \
57 HDX_PICK_RESOLVE_MODE_TOKENS
58
60 HdxPickTokens, HDX_API, HDX_PICK_TOKENS);
61
62class HdStRenderBuffer;
64using HdStShaderCodeSharedPtr = std::shared_ptr<class HdStShaderCode>;
65
66class Hgi;
67
72{
74 : cullStyle(HdCullStyleNothing)
75 {}
76
77 HdCullStyle cullStyle;
78};
79
83{
99 int instanceIndex;
100 int elementIndex;
101 int edgeIndex;
102 int pointIndex;
103 GfVec3d worldSpaceHitPoint;
104 GfVec3f worldSpaceHitNormal;
108
109 inline bool IsValid() const {
110 return !objectId.IsEmpty();
111 }
112
113 HDX_API
114 size_t GetHash() const;
115};
116
117using HdxPickHitVector = std::vector<HdxPickHit>;
118
153
172{
174 HDX_API
175 static HdxPrimOriginInfo
176 FromPickHit(HdSceneIndexBaseRefPtr const &terminalSceneIndex,
177 const HdxPickHit &hit);
178
183 HDX_API
184 static HdxPrimOriginInfo
186 const HdxPickHit &hit);
187
191 HDX_API
192 static std::vector<HdxPrimOriginInfo>
193 FromPickHits(HdSceneIndexBaseRefPtr const &terminalSceneIndex,
194 const std::vector<HdxPickHit> &hits);
195
202 HDX_API
203 static std::vector<HdxPrimOriginInfo>
205 const std::vector<HdxPickHit> &hits);
206
213 HDX_API
215 const TfToken &nameInPrimOrigin =
216 HdPrimOriginSchemaTokens->scenePath) const;
217
224 HDX_API
225 HdInstancerContext ComputeInstancerContext(
226 const TfToken &nameInPrimOrigin =
227 HdPrimOriginSchemaTokens->scenePath) const;
228
231 std::vector<HdxInstancerContext> instancerContexts;
234 HdContainerDataSourceHandle primOrigin;
235};
236
270{
271 using DepthMaskCallback = std::function<void(void)>;
272
274 : resolution(128, 128)
275 , maxNumDeepEntries(32000)
276 , pickTarget(HdxPickTokens->pickPrimsAndInstances)
277 , resolveMode(HdxPickTokens->resolveNearestToCamera)
278 , doUnpickablesOcclude(false)
279 , viewMatrix(1)
280 , projectionMatrix(1)
281 , clipPlanes()
282 , depthMaskCallback(nullptr)
283 , collection()
284 , alphaThreshold(0.0001f)
285 , outHits(nullptr)
286 {}
287
288 GfVec2i resolution;
289 int maxNumDeepEntries;
290 TfToken pickTarget;
291 TfToken resolveMode;
292 bool doUnpickablesOcclude;
293 GfMatrix4d viewMatrix;
294 GfMatrix4d projectionMatrix;
295 std::vector<GfVec4d> clipPlanes;
296 DepthMaskCallback depthMaskCallback;
297 HdRprimCollection collection;
298 float alphaThreshold;
299 HdxPickHitVector *outHits;
300};
301
314class HdxPickTask : public HdTask
315{
316public:
318
319 HDX_API
320 HdxPickTask(HdSceneDelegate* delegate, SdfPath const& id);
321
322 HDX_API
323 ~HdxPickTask() override;
324
326 HDX_API
327 void Sync(HdSceneDelegate* delegate,
328 HdTaskContext* ctx,
329 HdDirtyBits* dirtyBits) override;
330
332 HDX_API
333 void Prepare(HdTaskContext* ctx,
334 HdRenderIndex* renderIndex) override;
335
337 HDX_API
338 void Execute(HdTaskContext* ctx) override;
339
340 HDX_API
341 const TfTokenVector &GetRenderTags() const override;
342
344 static inline int DecodeIDRenderColor(unsigned char const idColor[4]) {
345 return (int32_t(idColor[0] & 0xff) << 0) |
346 (int32_t(idColor[1] & 0xff) << 8) |
347 (int32_t(idColor[2] & 0xff) << 16) |
348 (int32_t(idColor[3] & 0xff) << 24);
349 }
350
351private:
352 HdxPickTaskParams _params;
353 HdxPickTaskContextParams _contextParams;
354 TfTokenVector _renderTags;
355 bool _useOverlayPass;
356
357 // We need to cache a pointer to the render index so Execute() can
358 // map prim ID to paths.
359 HdRenderIndex *_index;
360
361 void _InitIfNeeded();
362 void _CreateAovBindings();
363 void _CleanupAovBindings();
364 void _ResizeOrCreateBufferForAOV(
365 const HdRenderPassAovBinding& aovBinding);
366
367 void _ConditionStencilWithGLCallback(
368 HdxPickTaskContextParams::DepthMaskCallback maskCallback,
369 HdRenderBuffer const * depthStencilBuffer);
370
371 bool _UseOcclusionPass() const;
372 bool _UseOverlayPass() const;
373 void _UpdateUseOverlayPass();
374
375 void _ClearPickBuffer();
376 void _ResolveDeep();
377
378 template<typename T>
379 HdStTextureUtils::AlignedBuffer<T>
380 _ReadAovBuffer(TfToken const & aovName) const;
381
382 HdRenderBuffer const * _FindAovBuffer(TfToken const & aovName) const;
383
384 // Create a shared render pass each for pickables, unpickables, and
385 // items in overlay (which may draw on top even when occluded).
386 HdRenderPassSharedPtr _pickableRenderPass;
387 HdRenderPassSharedPtr _occluderRenderPass;
388 HdRenderPassSharedPtr _overlayRenderPass;
389
390 // Having separate render pass states allows us to use different
391 // shader mixins if we choose to (we don't currently).
392 HdRenderPassStateSharedPtr _pickableRenderPassState;
393 HdRenderPassStateSharedPtr _occluderRenderPassState;
394 HdRenderPassStateSharedPtr _overlayRenderPassState;
395
396 Hgi* _hgi;
397
398 std::vector<std::unique_ptr<HdStRenderBuffer>> _pickableAovBuffers;
399 HdRenderPassAovBindingVector _pickableAovBindings;
400 HdRenderPassAovBinding _occluderAovBinding;
401 size_t _pickableDepthIndex;
402 TfToken _depthToken;
403 std::unique_ptr<HdStRenderBuffer> _overlayDepthStencilBuffer;
404 HdRenderPassAovBindingVector _overlayAovBindings;
405
406 // pick buffer used for deep selection
407 HdBufferArrayRangeSharedPtr _pickBuffer;
408
409 HdxPickTask() = delete;
410 HdxPickTask(const HdxPickTask &) = delete;
411 HdxPickTask &operator =(const HdxPickTask &) = delete;
412};
413
416public:
417
418 // Pick result takes a tuple of ID buffers:
419 // - (primId, instanceId, elementId, edgeId, pointId)
420 // along with some geometric buffers:
421 // - (depth, Neye)
422 // ... and resolves them into a series of hits, using one of the
423 // algorithms specified below.
424 //
425 // index is used to fill in the HdxPickHit structure;
426 // pickTarget is used to determine what a valid hit is;
427 // viewMatrix, projectionMatrix, depthRange are used for unprojection
428 // to calculate the worldSpaceHitPosition and worldSpaceHitNormal.
429 // bufferSize is the size of the ID buffers, and subRect is the sub-region
430 // of the id buffers to iterate over in the resolution algorithm.
431 //
432 // All buffers need to be the same size, if passed in. It's legal for
433 // only the depth and primId buffers to be provided; everything else is
434 // optional but provides a richer picking result.
435 HDX_API
436 HdxPickResult(int const* primIds,
437 int const* instanceIds,
438 int const* elementIds,
439 int const* edgeIds,
440 int const* pointIds,
441 int const* neyes,
442 float const* depths,
443 HdRenderIndex const *index,
444 TfToken const& pickTarget,
445 GfMatrix4d const& viewMatrix,
446 GfMatrix4d const& projectionMatrix,
447 GfVec2f const& depthRange,
448 GfVec2i const& bufferSize,
449 GfVec4i const& subRect);
450
451 HDX_API
453
454 HDX_API
456 HDX_API
457 HdxPickResult& operator=(HdxPickResult &&);
458
460 HDX_API
461 bool IsValid() const;
462
466 HDX_API
467 void ResolveNearestToCamera(HdxPickHitVector* allHits) const;
468
472 HDX_API
473 void ResolveNearestToCenter(HdxPickHitVector* allHits) const;
474
477 HDX_API
478 void ResolveAll(HdxPickHitVector* allHits) const;
479
482 HDX_API
483 void ResolveUnique(HdxPickHitVector* allHits) const;
484
485private:
486 bool _ResolveHit(int index, int x, int y, float z, HdxPickHit* hit) const;
487
488 size_t _GetHash(int index) const;
489 bool _IsValidHit(int index) const;
490
491 // Provide accessors for all of the ID buffers. Since all but _primIds
492 // are optional, if the buffer doesn't exist just return -1 (== no hit).
493 int _GetPrimId(int index) const {
494 return _primIds ? _primIds[index] : -1;
495 }
496 int _GetInstanceId(int index) const {
497 return _instanceIds ? _instanceIds[index] : -1;
498 }
499 int _GetElementId(int index) const {
500 return _elementIds ? _elementIds[index] : -1;
501 }
502 int _GetEdgeId(int index) const {
503 return _edgeIds ? _edgeIds[index] : -1;
504 }
505 int _GetPointId(int index) const {
506 return _pointIds ? _pointIds[index] : -1;
507 }
508
509 // Provide an accessor for the normal buffer. If the normal buffer is
510 // provided, this function will unpack the normal. The fallback is
511 // GfVec3f(0.0f).
512 GfVec3f _GetNormal(int index) const;
513
514 int const* _primIds;
515 int const* _instanceIds;
516 int const* _elementIds;
517 int const* _edgeIds;
518 int const* _pointIds;
519 int const* _neyes;
520 float const* _depths;
521 HdRenderIndex const *_index;
522 TfToken _pickTarget;
523 GfMatrix4d _ndcToWorld;
524 GfMatrix4d _eyeToWorld;
525 GfVec2f _depthRange;
526 GfVec2i _bufferSize;
527 GfVec4i _subRect;
528};
529
530// For sorting, order hits by ndc depth.
531HDX_API
532bool operator<(HdxPickHit const& lhs, HdxPickHit const& rhs);
533
534// VtValue requirements
535HDX_API
536std::ostream& operator<<(std::ostream& out, const HdxPickHit& h);
537HDX_API
538bool operator==(const HdxPickHit& lhs,
539 const HdxPickHit& rhs);
540HDX_API
541bool operator!=(const HdxPickHit& lhs,
542 const HdxPickHit& rhs);
543
544HDX_API
545std::ostream& operator<<(std::ostream& out, const HdxPickTaskParams& pv);
546HDX_API
547bool operator==(const HdxPickTaskParams& lhs,
548 const HdxPickTaskParams& rhs);
549HDX_API
550bool operator!=(const HdxPickTaskParams& lhs,
551 const HdxPickTaskParams& rhs);
552
553HDX_API
554std::ostream& operator<<(std::ostream& out, const HdxPickTaskContextParams& pv);
555HDX_API
556bool operator==(const HdxPickTaskContextParams& lhs,
557 const HdxPickTaskContextParams& rhs);
558HDX_API
559bool operator!=(const HdxPickTaskContextParams& lhs,
560 const HdxPickTaskContextParams& rhs);
561PXR_NAMESPACE_CLOSE_SCOPE
562
563#endif // PXR_IMAGING_HDX_PICK_TASK_H
Provide architecture-specific memory-alignment information.
Stores a 4x4 matrix of double elements.
Definition matrix4d.h:71
Basic type for a vector of 2 float components.
Definition vec2f.h:46
Basic type for a vector of 2 int components.
Definition vec2i.h:44
Basic type for a vector of 3 double components.
Definition vec3d.h:46
Basic type for a vector of 3 float components.
Definition vec3f.h:46
Basic type for a vector of 4 int components.
Definition vec4i.h:44
A render buffer is a handle to a data resource that can be rendered into, such as a 2d image for a dr...
The render index is part of the Hydra 1.0 API and is only used for emulation purposes so that HdScene...
A named, semantic collection of objects.
Adapter class providing data exchange with the client scene graph.
A set of rendering parameters used among render passes.
HdTask represents a unit of work to perform during a Hydra render.
Definition task.h:44
A utility class for resolving ID buffers into hits.
Definition pickTask.h:415
HDX_API void ResolveUnique(HdxPickHitVector *allHits) const
Return the set of unique hit points, keeping only the nearest depth value.
HDX_API void ResolveNearestToCenter(HdxPickHitVector *allHits) const
Return the nearest single hit point from the center of the viewport.
HDX_API bool IsValid() const
Return whether the result was given well-formed parameters.
HDX_API void ResolveAll(HdxPickHitVector *allHits) const
Return all hit points.
HDX_API void ResolveNearestToCamera(HdxPickHitVector *allHits) const
Return the nearest single hit point.
A task for running picking queries against the current scene.
Definition pickTask.h:315
static int DecodeIDRenderColor(unsigned char const idColor[4])
Utility: Given a UNorm8Vec4 pixel, unpack it into an int32 ID.
Definition pickTask.h:344
HDX_API const TfTokenVector & GetRenderTags() const override
Render Tag Gather.
HDX_API void Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits) override
Sync the render pass resources.
HDX_API void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex) override
Prepare the pick task.
HDX_API void Execute(HdTaskContext *ctx) override
Execute the pick task.
Hydra Graphics Interface.
Definition hgi.h:95
A path value used to locate objects in layers or scenegraphs.
Definition path.h:281
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition path.h:405
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:71
Standard pointer typedefs.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
A renderpass AOV represents a binding of some output of the rendering process to an output buffer.
Definition aov.h:100
Information about an instancer instancing a picked object (or an instancer instancing such an instanc...
Definition pickTask.h:122
HdContainerDataSourceHandle instancerPrimOrigin
The prim origin data source of the instancer.
Definition pickTask.h:126
HdContainerDataSourceHandle instancePrimOrigin
The prim origin data source of the picked (implicit) instance.
Definition pickTask.h:149
int instanceId
Index of the picked instance.
Definition pickTask.h:151
SdfPath instanceSceneIndexPath
For implicit instancing (native instancing in USD), the path of the picked instance in the scene inde...
Definition pickTask.h:130
SdfPath instancerSceneIndexPath
The path of the instancer in the scene index.
Definition pickTask.h:124
Picking hit structure.
Definition pickTask.h:83
float normalizedDepth
normalizedDepth is in the range [0,1].
Definition pickTask.h:107
SdfPath delegateId
delegateID of HdSceneDelegate that provided the picked prim.
Definition pickTask.h:88
SdfPath objectId
Path computed from scenePath's in primOrigin data source of picked prim and instancers if provided by...
Definition pickTask.h:92
SdfPath instancerId
Only supported for scene delegates, see HdxPrimOriginInfo for scene indices.
Definition pickTask.h:98
Pick task context params.
Definition pickTask.h:270
Pick task params.
Definition pickTask.h:72
A helper to extract information about the picked prim that allows modern applications to identify a p...
Definition pickTask.h:172
static HDX_API std::vector< HdxPrimOriginInfo > FromPickHits(HdRenderIndex *renderIndex, const std::vector< HdxPickHit > &hits)
Vectorized implementation of function to query terminal scene index of render index for information a...
static HDX_API std::vector< HdxPrimOriginInfo > FromPickHits(HdSceneIndexBaseRefPtr const &terminalSceneIndex, const std::vector< HdxPickHit > &hits)
Vectorized implementation of function to query terminal scene index for information about picked prim...
static HDX_API HdxPrimOriginInfo FromPickHit(HdSceneIndexBaseRefPtr const &terminalSceneIndex, const HdxPickHit &hit)
Query terminal scene index for information about picked prim.
HdContainerDataSourceHandle primOrigin
The prim origin data source for the picked prim if provided by the scene index.
Definition pickTask.h:234
std::vector< HdxInstancerContext > instancerContexts
Information about the instancers instancing the picked object.
Definition pickTask.h:231
static HDX_API HdxPrimOriginInfo FromPickHit(HdRenderIndex *renderIndex, const HdxPickHit &hit)
Query terminal scene index of render index for information about picked prim.
HDX_API HdInstancerContext ComputeInstancerContext(const TfToken &nameInPrimOrigin=HdPrimOriginSchemaTokens->scenePath) const
Computes an HdInstancerContext object (equivalent to the one computed by GetScenePrimPath) from the v...
HDX_API SdfPath GetFullPath(const TfToken &nameInPrimOrigin=HdPrimOriginSchemaTokens->scenePath) const
Combines instance scene paths and prim scene path to obtain the full scene path.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition token.h:440