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 , pointSize(3.0f)
286 , pointSelectedSize(5.0f)
287 , depthBiasEnable(false)
288 , depthBiasConstantFactor(0.0f)
289 , depthBiasSlopeFactor(0.0f)
290 , outHits(nullptr)
291 {}
292
293 GfVec2i resolution;
294 int maxNumDeepEntries;
295 TfToken pickTarget;
296 TfToken resolveMode;
297 bool doUnpickablesOcclude;
298 GfMatrix4d viewMatrix;
299 GfMatrix4d projectionMatrix;
300 std::vector<GfVec4d> clipPlanes;
301 DepthMaskCallback depthMaskCallback;
302 HdRprimCollection collection;
303 float alphaThreshold;
304 float pointSize;
305 float pointSelectedSize;
306 bool depthBiasEnable;
307 float depthBiasConstantFactor;
308 float depthBiasSlopeFactor;
309 HdxPickHitVector *outHits;
310};
311
324class HdxPickTask : public HdTask
325{
326public:
328
329 HDX_API
330 HdxPickTask(HdSceneDelegate* delegate, SdfPath const& id);
331
332 HDX_API
333 ~HdxPickTask() override;
334
336 HDX_API
337 void Sync(HdSceneDelegate* delegate,
338 HdTaskContext* ctx,
339 HdDirtyBits* dirtyBits) override;
340
342 HDX_API
343 void Prepare(HdTaskContext* ctx,
344 HdRenderIndex* renderIndex) override;
345
347 HDX_API
348 void Execute(HdTaskContext* ctx) override;
349
350 HDX_API
351 const TfTokenVector &GetRenderTags() const override;
352
354 static inline int DecodeIDRenderColor(unsigned char const idColor[4]) {
355 return (int32_t(idColor[0] & 0xff) << 0) |
356 (int32_t(idColor[1] & 0xff) << 8) |
357 (int32_t(idColor[2] & 0xff) << 16) |
358 (int32_t(idColor[3] & 0xff) << 24);
359 }
360
361private:
362 HdxPickTaskParams _params;
363 HdxPickTaskContextParams _contextParams;
364 TfTokenVector _renderTags;
365 bool _useOverlayPass;
366
367 // We need to cache a pointer to the render index so Execute() can
368 // map prim ID to paths.
369 HdRenderIndex *_index;
370
371 void _InitIfNeeded();
372 void _CreateAovBindings();
373 void _CleanupAovBindings();
374 void _ResizeOrCreateBufferForAOV(
375 const HdRenderPassAovBinding& aovBinding);
376
377 void _ConditionStencilWithGLCallback(
378 HdxPickTaskContextParams::DepthMaskCallback maskCallback,
379 HdRenderBuffer const * depthStencilBuffer);
380
381 bool _UseOcclusionPass() const;
382 bool _UseOverlayPass() const;
383 void _UpdateUseOverlayPass();
384
385 void _ClearPickBuffer();
386 void _ResolveDeep();
387
388 template<typename T>
389 HdStTextureUtils::AlignedBuffer<T>
390 _ReadAovBuffer(TfToken const & aovName) const;
391
392 template<typename T>
393 HdStTextureUtils::AlignedBuffer<T>
394 _ReadAovBuffer(TfToken const & aovName,
395 HdRenderPassAovBindingVector const & bindings) const;
396
397 HdRenderBuffer const * _FindAovBuffer(
398 TfToken const & aovName,
399 HdRenderPassAovBindingVector const & bindings) const;
400
401 // Create a shared render pass each for pickables, unpickables, and
402 // items in overlay (which may draw on top even when occluded).
403 HdRenderPassSharedPtr _pickableRenderPass;
404 HdRenderPassSharedPtr _occluderRenderPass;
405 HdRenderPassSharedPtr _overlayRenderPass;
406
407 // Having separate render pass states allows us to use different
408 // shader mixins if we choose to (we don't currently).
409 HdRenderPassStateSharedPtr _pickableRenderPassState;
410 HdRenderPassStateSharedPtr _occluderRenderPassState;
411 HdRenderPassStateSharedPtr _overlayRenderPassState;
412
413 Hgi* _hgi;
414
415 std::vector<std::unique_ptr<HdStRenderBuffer>> _pickableAovBuffers;
416 HdRenderPassAovBindingVector _pickableAovBindings;
417 HdRenderPassAovBinding _occluderAovBinding;
418 size_t _pickableDepthIndex;
419 TfToken _depthToken;
420 std::unique_ptr<HdStRenderBuffer> _overlayDepthStencilBuffer;
421 std::vector<std::unique_ptr<HdStRenderBuffer>> _overlayAovBuffers;
422 HdRenderPassAovBindingVector _overlayAovBindings;
423
424 // pick buffer used for deep selection
425 HdBufferArrayRangeSharedPtr _pickBuffer;
426
427 HdxPickTask() = delete;
428 HdxPickTask(const HdxPickTask &) = delete;
429 HdxPickTask &operator =(const HdxPickTask &) = delete;
430};
431
434public:
435
436 // Pick result takes a tuple of ID buffers:
437 // - (primId, instanceId, elementId, edgeId, pointId)
438 // along with some geometric buffers:
439 // - (depth, Neye)
440 // ... and resolves them into a series of hits, using one of the
441 // algorithms specified below.
442 //
443 // index is used to fill in the HdxPickHit structure;
444 // pickTarget is used to determine what a valid hit is;
445 // viewMatrix, projectionMatrix, depthRange are used for unprojection
446 // to calculate the worldSpaceHitPosition and worldSpaceHitNormal.
447 // bufferSize is the size of the ID buffers, and subRect is the sub-region
448 // of the id buffers to iterate over in the resolution algorithm.
449 //
450 // All buffers need to be the same size, if passed in. It's legal for
451 // only the depth and primId buffers to be provided; everything else is
452 // optional but provides a richer picking result.
453 HDX_API
454 HdxPickResult(int const* primIds,
455 int const* instanceIds,
456 int const* elementIds,
457 int const* edgeIds,
458 int const* pointIds,
459 int const* neyes,
460 float const* depths,
461 HdRenderIndex const *index,
462 TfToken const& pickTarget,
463 GfMatrix4d const& viewMatrix,
464 GfMatrix4d const& projectionMatrix,
465 GfVec2f const& depthRange,
466 GfVec2i const& bufferSize,
467 GfVec4i const& subRect);
468
469 HDX_API
471
472 HDX_API
474 HDX_API
475 HdxPickResult& operator=(HdxPickResult &&);
476
478 HDX_API
479 bool IsValid() const;
480
484 HDX_API
485 void ResolveNearestToCamera(HdxPickHitVector* allHits) const;
486
490 HDX_API
491 void ResolveNearestToCenter(HdxPickHitVector* allHits) const;
492
495 HDX_API
496 void ResolveAll(HdxPickHitVector* allHits) const;
497
500 HDX_API
501 void ResolveUnique(HdxPickHitVector* allHits) const;
502
503private:
504 bool _ResolveHit(int index, int x, int y, float z, HdxPickHit* hit) const;
505
506 size_t _GetHash(int index) const;
507 bool _IsValidHit(int index) const;
508
509 // Provide accessors for all of the ID buffers. Since all but _primIds
510 // are optional, if the buffer doesn't exist just return -1 (== no hit).
511 int _GetPrimId(int index) const {
512 return _primIds ? _primIds[index] : -1;
513 }
514 int _GetInstanceId(int index) const {
515 return _instanceIds ? _instanceIds[index] : -1;
516 }
517 int _GetElementId(int index) const {
518 return _elementIds ? _elementIds[index] : -1;
519 }
520 int _GetEdgeId(int index) const {
521 return _edgeIds ? _edgeIds[index] : -1;
522 }
523 int _GetPointId(int index) const {
524 return _pointIds ? _pointIds[index] : -1;
525 }
526
527 // Provide an accessor for the normal buffer. If the normal buffer is
528 // provided, this function will unpack the normal. The fallback is
529 // GfVec3f(0.0f).
530 GfVec3f _GetNormal(int index) const;
531
532 int const* _primIds;
533 int const* _instanceIds;
534 int const* _elementIds;
535 int const* _edgeIds;
536 int const* _pointIds;
537 int const* _neyes;
538 float const* _depths;
539 HdRenderIndex const *_index;
540 TfToken _pickTarget;
541 GfMatrix4d _ndcToWorld;
542 GfMatrix4d _eyeToWorld;
543 GfVec2f _depthRange;
544 GfVec2i _bufferSize;
545 GfVec4i _subRect;
546};
547
548// For sorting, order hits by ndc depth.
549HDX_API
550bool operator<(HdxPickHit const& lhs, HdxPickHit const& rhs);
551
552// VtValue requirements
553HDX_API
554std::ostream& operator<<(std::ostream& out, const HdxPickHit& h);
555HDX_API
556bool operator==(const HdxPickHit& lhs,
557 const HdxPickHit& rhs);
558HDX_API
559bool operator!=(const HdxPickHit& lhs,
560 const HdxPickHit& rhs);
561
562HDX_API
563std::ostream& operator<<(std::ostream& out, const HdxPickTaskParams& pv);
564HDX_API
565bool operator==(const HdxPickTaskParams& lhs,
566 const HdxPickTaskParams& rhs);
567HDX_API
568bool operator!=(const HdxPickTaskParams& lhs,
569 const HdxPickTaskParams& rhs);
570
571HDX_API
572std::ostream& operator<<(std::ostream& out, const HdxPickTaskContextParams& pv);
573HDX_API
574bool operator==(const HdxPickTaskContextParams& lhs,
575 const HdxPickTaskContextParams& rhs);
576HDX_API
577bool operator!=(const HdxPickTaskContextParams& lhs,
578 const HdxPickTaskContextParams& rhs);
579PXR_NAMESPACE_CLOSE_SCOPE
580
581#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:47
Basic type for a vector of 3 float components.
Definition vec3f.h:47
Basic type for a vector of 4 int components.
Definition vec4i.h:46
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:433
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:325
static int DecodeIDRenderColor(unsigned char const idColor[4])
Utility: Given a UNorm8Vec4 pixel, unpack it into an int32 ID.
Definition pickTask.h:354
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:81
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:489