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_TOKENS \
37 /* Task context */ \
38 (pickParams) \
39 \
40 /* Pick target */ \
41 (pickPrimsAndInstances) \
42 (pickFaces) \
43 (pickEdges) \
44 (pickPoints) \
45 (pickPointsAndInstances) \
46 \
47 /* Resolve mode */ \
48 (resolveNearestToCamera) \
49 (resolveNearestToCenter) \
50 (resolveUnique) \
51 (resolveAll) \
52 (resolveDeep)
53
54TF_DECLARE_PUBLIC_TOKENS(HdxPickTokens, HDX_API, HDX_PICK_TOKENS);
55
56class HdStRenderBuffer;
58using HdStShaderCodeSharedPtr = std::shared_ptr<class HdStShaderCode>;
59
60class Hgi;
61
66{
68 : cullStyle(HdCullStyleNothing)
69 {}
70
71 HdCullStyle cullStyle;
72};
73
77{
88 int instanceIndex;
89 int elementIndex;
90 int edgeIndex;
91 int pointIndex;
92 GfVec3d worldSpaceHitPoint;
93 GfVec3f worldSpaceHitNormal;
97
98 inline bool IsValid() const {
99 return !objectId.IsEmpty();
100 }
101
102 HDX_API
103 size_t GetHash() const;
104};
105
106using HdxPickHitVector = std::vector<HdxPickHit>;
107
111{
115 HdContainerDataSourceHandle instancerPrimOrigin;
116
120
138 HdContainerDataSourceHandle instancePrimOrigin;
141};
142
161{
163 HDX_API
164 static HdxPrimOriginInfo
165 FromPickHit(HdSceneIndexBaseRefPtr const &terminalSceneIndex,
166 const HdxPickHit &hit);
167
172 HDX_API
173 static HdxPrimOriginInfo
175 const HdxPickHit &hit);
176
180 HDX_API
181 static std::vector<HdxPrimOriginInfo>
182 FromPickHits(HdSceneIndexBaseRefPtr const &terminalSceneIndex,
183 const std::vector<HdxPickHit> &hits);
184
191 HDX_API
192 static std::vector<HdxPrimOriginInfo>
194 const std::vector<HdxPickHit> &hits);
195
202 HDX_API
204 const TfToken &nameInPrimOrigin =
205 HdPrimOriginSchemaTokens->scenePath) const;
206
213 HDX_API
214 HdInstancerContext ComputeInstancerContext(
215 const TfToken &nameInPrimOrigin =
216 HdPrimOriginSchemaTokens->scenePath) const;
217
220 std::vector<HdxInstancerContext> instancerContexts;
223 HdContainerDataSourceHandle primOrigin;
224};
225
259{
260 using DepthMaskCallback = std::function<void(void)>;
261
263 : resolution(128, 128)
264 , maxNumDeepEntries(32000)
265 , pickTarget(HdxPickTokens->pickPrimsAndInstances)
266 , resolveMode(HdxPickTokens->resolveNearestToCamera)
267 , doUnpickablesOcclude(false)
268 , viewMatrix(1)
269 , projectionMatrix(1)
270 , clipPlanes()
271 , depthMaskCallback(nullptr)
272 , collection()
273 , alphaThreshold(0.0001f)
274 , outHits(nullptr)
275 {}
276
277 GfVec2i resolution;
278 int maxNumDeepEntries;
279 TfToken pickTarget;
280 TfToken resolveMode;
281 bool doUnpickablesOcclude;
282 GfMatrix4d viewMatrix;
283 GfMatrix4d projectionMatrix;
284 std::vector<GfVec4d> clipPlanes;
285 DepthMaskCallback depthMaskCallback;
286 HdRprimCollection collection;
287 float alphaThreshold;
288 HdxPickHitVector *outHits;
289};
290
303class HdxPickTask : public HdTask
304{
305public:
307
308 HDX_API
309 HdxPickTask(HdSceneDelegate* delegate, SdfPath const& id);
310
311 HDX_API
312 ~HdxPickTask() override;
313
315 HDX_API
316 void Sync(HdSceneDelegate* delegate,
317 HdTaskContext* ctx,
318 HdDirtyBits* dirtyBits) override;
319
321 HDX_API
322 void Prepare(HdTaskContext* ctx,
323 HdRenderIndex* renderIndex) override;
324
326 HDX_API
327 void Execute(HdTaskContext* ctx) override;
328
329 HDX_API
330 const TfTokenVector &GetRenderTags() const override;
331
333 static inline int DecodeIDRenderColor(unsigned char const idColor[4]) {
334 return (int32_t(idColor[0] & 0xff) << 0) |
335 (int32_t(idColor[1] & 0xff) << 8) |
336 (int32_t(idColor[2] & 0xff) << 16) |
337 (int32_t(idColor[3] & 0xff) << 24);
338 }
339
340private:
341 HdxPickTaskParams _params;
342 HdxPickTaskContextParams _contextParams;
343 TfTokenVector _renderTags;
344 bool _useOverlayPass;
345
346 // We need to cache a pointer to the render index so Execute() can
347 // map prim ID to paths.
348 HdRenderIndex *_index;
349
350 void _InitIfNeeded();
351 void _CreateAovBindings();
352 void _CleanupAovBindings();
353 void _ResizeOrCreateBufferForAOV(
354 const HdRenderPassAovBinding& aovBinding);
355
356 void _ConditionStencilWithGLCallback(
357 HdxPickTaskContextParams::DepthMaskCallback maskCallback,
358 HdRenderBuffer const * depthStencilBuffer);
359
360 bool _UseOcclusionPass() const;
361 bool _UseOverlayPass() const;
362 void _UpdateUseOverlayPass();
363
364 void _ClearPickBuffer();
365 void _ResolveDeep();
366
367 template<typename T>
368 HdStTextureUtils::AlignedBuffer<T>
369 _ReadAovBuffer(TfToken const & aovName) const;
370
371 HdRenderBuffer const * _FindAovBuffer(TfToken const & aovName) const;
372
373 // Create a shared render pass each for pickables, unpickables, and
374 // items in overlay (which may draw on top even when occluded).
375 HdRenderPassSharedPtr _pickableRenderPass;
376 HdRenderPassSharedPtr _occluderRenderPass;
377 HdRenderPassSharedPtr _overlayRenderPass;
378
379 // Having separate render pass states allows us to use different
380 // shader mixins if we choose to (we don't currently).
381 HdRenderPassStateSharedPtr _pickableRenderPassState;
382 HdRenderPassStateSharedPtr _occluderRenderPassState;
383 HdRenderPassStateSharedPtr _overlayRenderPassState;
384
385 Hgi* _hgi;
386
387 std::vector<std::unique_ptr<HdStRenderBuffer>> _pickableAovBuffers;
388 HdRenderPassAovBindingVector _pickableAovBindings;
389 HdRenderPassAovBinding _occluderAovBinding;
390 size_t _pickableDepthIndex;
391 TfToken _depthToken;
392 std::unique_ptr<HdStRenderBuffer> _overlayDepthStencilBuffer;
393 HdRenderPassAovBindingVector _overlayAovBindings;
394
395 // pick buffer used for deep selection
396 HdBufferArrayRangeSharedPtr _pickBuffer;
397
398 HdxPickTask() = delete;
399 HdxPickTask(const HdxPickTask &) = delete;
400 HdxPickTask &operator =(const HdxPickTask &) = delete;
401};
402
405public:
406
407 // Pick result takes a tuple of ID buffers:
408 // - (primId, instanceId, elementId, edgeId, pointId)
409 // along with some geometric buffers:
410 // - (depth, Neye)
411 // ... and resolves them into a series of hits, using one of the
412 // algorithms specified below.
413 //
414 // index is used to fill in the HdxPickHit structure;
415 // pickTarget is used to determine what a valid hit is;
416 // viewMatrix, projectionMatrix, depthRange are used for unprojection
417 // to calculate the worldSpaceHitPosition and worldSpaceHitNormal.
418 // bufferSize is the size of the ID buffers, and subRect is the sub-region
419 // of the id buffers to iterate over in the resolution algorithm.
420 //
421 // All buffers need to be the same size, if passed in. It's legal for
422 // only the depth and primId buffers to be provided; everything else is
423 // optional but provides a richer picking result.
424 HDX_API
425 HdxPickResult(int const* primIds,
426 int const* instanceIds,
427 int const* elementIds,
428 int const* edgeIds,
429 int const* pointIds,
430 int const* neyes,
431 float const* depths,
432 HdRenderIndex const *index,
433 TfToken const& pickTarget,
434 GfMatrix4d const& viewMatrix,
435 GfMatrix4d const& projectionMatrix,
436 GfVec2f const& depthRange,
437 GfVec2i const& bufferSize,
438 GfVec4i const& subRect);
439
440 HDX_API
442
443 HDX_API
445 HDX_API
446 HdxPickResult& operator=(HdxPickResult &&);
447
449 HDX_API
450 bool IsValid() const;
451
455 HDX_API
456 void ResolveNearestToCamera(HdxPickHitVector* allHits) const;
457
461 HDX_API
462 void ResolveNearestToCenter(HdxPickHitVector* allHits) const;
463
466 HDX_API
467 void ResolveAll(HdxPickHitVector* allHits) const;
468
471 HDX_API
472 void ResolveUnique(HdxPickHitVector* allHits) const;
473
474private:
475 bool _ResolveHit(int index, int x, int y, float z, HdxPickHit* hit) const;
476
477 size_t _GetHash(int index) const;
478 bool _IsValidHit(int index) const;
479
480 // Provide accessors for all of the ID buffers. Since all but _primIds
481 // are optional, if the buffer doesn't exist just return -1 (== no hit).
482 int _GetPrimId(int index) const {
483 return _primIds ? _primIds[index] : -1;
484 }
485 int _GetInstanceId(int index) const {
486 return _instanceIds ? _instanceIds[index] : -1;
487 }
488 int _GetElementId(int index) const {
489 return _elementIds ? _elementIds[index] : -1;
490 }
491 int _GetEdgeId(int index) const {
492 return _edgeIds ? _edgeIds[index] : -1;
493 }
494 int _GetPointId(int index) const {
495 return _pointIds ? _pointIds[index] : -1;
496 }
497
498 // Provide an accessor for the normal buffer. If the normal buffer is
499 // provided, this function will unpack the normal. The fallback is
500 // GfVec3f(0.0f).
501 GfVec3f _GetNormal(int index) const;
502
503 int const* _primIds;
504 int const* _instanceIds;
505 int const* _elementIds;
506 int const* _edgeIds;
507 int const* _pointIds;
508 int const* _neyes;
509 float const* _depths;
510 HdRenderIndex const *_index;
511 TfToken _pickTarget;
512 GfMatrix4d _ndcToWorld;
513 GfMatrix4d _eyeToWorld;
514 GfVec2f _depthRange;
515 GfVec2i _bufferSize;
516 GfVec4i _subRect;
517};
518
519// For sorting, order hits by ndc depth.
520HDX_API
521bool operator<(HdxPickHit const& lhs, HdxPickHit const& rhs);
522
523// VtValue requirements
524HDX_API
525std::ostream& operator<<(std::ostream& out, const HdxPickHit& h);
526HDX_API
527bool operator==(const HdxPickHit& lhs,
528 const HdxPickHit& rhs);
529HDX_API
530bool operator!=(const HdxPickHit& lhs,
531 const HdxPickHit& rhs);
532
533HDX_API
534std::ostream& operator<<(std::ostream& out, const HdxPickTaskParams& pv);
535HDX_API
536bool operator==(const HdxPickTaskParams& lhs,
537 const HdxPickTaskParams& rhs);
538HDX_API
539bool operator!=(const HdxPickTaskParams& lhs,
540 const HdxPickTaskParams& rhs);
541
542HDX_API
543std::ostream& operator<<(std::ostream& out, const HdxPickTaskContextParams& pv);
544HDX_API
545bool operator==(const HdxPickTaskContextParams& lhs,
546 const HdxPickTaskContextParams& rhs);
547HDX_API
548bool operator!=(const HdxPickTaskContextParams& lhs,
549 const HdxPickTaskContextParams& rhs);
550PXR_NAMESPACE_CLOSE_SCOPE
551
552#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...
Definition: renderBuffer.h:33
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:105
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:404
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:304
static int DecodeIDRenderColor(unsigned char const idColor[4])
Utility: Given a UNorm8Vec4 pixel, unpack it into an int32 ID.
Definition: pickTask.h:333
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:274
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:398
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Standard pointer typedefs.
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:92
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:111
HdContainerDataSourceHandle instancerPrimOrigin
The prim origin data source of the instancer.
Definition: pickTask.h:115
HdContainerDataSourceHandle instancePrimOrigin
The prim origin data source of the picked (implicit) instance.
Definition: pickTask.h:138
int instanceId
Index of the picked instance.
Definition: pickTask.h:140
SdfPath instanceSceneIndexPath
For implicit instancing (native instancing in USD), the path of the picked instance in the scene inde...
Definition: pickTask.h:119
SdfPath instancerSceneIndexPath
The path of the instancer in the scene index.
Definition: pickTask.h:113
Picking hit structure.
Definition: pickTask.h:77
float normalizedDepth
normalizedDepth is in the range [0,1].
Definition: pickTask.h:96
SdfPath delegateId
delegateID of HdSceneDelegate that provided the picked prim.
Definition: pickTask.h:80
SdfPath objectId
Path computed from scenePath's in primOrigin data source of picked prim and instancers if provided by...
Definition: pickTask.h:84
SdfPath instancerId
Only supported for scene delegates, see HdxPrimOriginInfo for scene indices.
Definition: pickTask.h:87
Pick task context params.
Definition: pickTask.h:259
Pick task params.
Definition: pickTask.h:66
A helper to extract information about the picked prim that allows modern applications to identify a p...
Definition: pickTask.h:161
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:223
std::vector< HdxInstancerContext > instancerContexts
Information about the instancers instancing the picked object.
Definition: pickTask.h:220
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