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{
93 int instanceIndex;
94 int elementIndex;
95 int edgeIndex;
96 int pointIndex;
97 GfVec3d worldSpaceHitPoint;
98 GfVec3f worldSpaceHitNormal;
102
103 inline bool IsValid() const {
104 return !objectId.IsEmpty();
105 }
106
107 HDX_API
108 size_t GetHash() const;
109};
110
111using HdxPickHitVector = std::vector<HdxPickHit>;
112
116{
120 HdContainerDataSourceHandle instancerPrimOrigin;
121
125
143 HdContainerDataSourceHandle instancePrimOrigin;
146};
147
166{
168 HDX_API
169 static HdxPrimOriginInfo
170 FromPickHit(HdSceneIndexBaseRefPtr const &terminalSceneIndex,
171 const HdxPickHit &hit);
172
177 HDX_API
178 static HdxPrimOriginInfo
180 const HdxPickHit &hit);
181
185 HDX_API
186 static std::vector<HdxPrimOriginInfo>
187 FromPickHits(HdSceneIndexBaseRefPtr const &terminalSceneIndex,
188 const std::vector<HdxPickHit> &hits);
189
196 HDX_API
197 static std::vector<HdxPrimOriginInfo>
199 const std::vector<HdxPickHit> &hits);
200
207 HDX_API
209 const TfToken &nameInPrimOrigin =
210 HdPrimOriginSchemaTokens->scenePath) const;
211
218 HDX_API
219 HdInstancerContext ComputeInstancerContext(
220 const TfToken &nameInPrimOrigin =
221 HdPrimOriginSchemaTokens->scenePath) const;
222
225 std::vector<HdxInstancerContext> instancerContexts;
228 HdContainerDataSourceHandle primOrigin;
229};
230
264{
265 using DepthMaskCallback = std::function<void(void)>;
266
268 : resolution(128, 128)
269 , maxNumDeepEntries(32000)
270 , pickTarget(HdxPickTokens->pickPrimsAndInstances)
271 , resolveMode(HdxPickTokens->resolveNearestToCamera)
272 , doUnpickablesOcclude(false)
273 , viewMatrix(1)
274 , projectionMatrix(1)
275 , clipPlanes()
276 , depthMaskCallback(nullptr)
277 , collection()
278 , alphaThreshold(0.0001f)
279 , outHits(nullptr)
280 {}
281
282 GfVec2i resolution;
283 int maxNumDeepEntries;
284 TfToken pickTarget;
285 TfToken resolveMode;
286 bool doUnpickablesOcclude;
287 GfMatrix4d viewMatrix;
288 GfMatrix4d projectionMatrix;
289 std::vector<GfVec4d> clipPlanes;
290 DepthMaskCallback depthMaskCallback;
291 HdRprimCollection collection;
292 float alphaThreshold;
293 HdxPickHitVector *outHits;
294};
295
308class HdxPickTask : public HdTask
309{
310public:
312
313 HDX_API
314 HdxPickTask(HdSceneDelegate* delegate, SdfPath const& id);
315
316 HDX_API
317 ~HdxPickTask() override;
318
320 HDX_API
321 void Sync(HdSceneDelegate* delegate,
322 HdTaskContext* ctx,
323 HdDirtyBits* dirtyBits) override;
324
326 HDX_API
327 void Prepare(HdTaskContext* ctx,
328 HdRenderIndex* renderIndex) override;
329
331 HDX_API
332 void Execute(HdTaskContext* ctx) override;
333
334 HDX_API
335 const TfTokenVector &GetRenderTags() const override;
336
338 static inline int DecodeIDRenderColor(unsigned char const idColor[4]) {
339 return (int32_t(idColor[0] & 0xff) << 0) |
340 (int32_t(idColor[1] & 0xff) << 8) |
341 (int32_t(idColor[2] & 0xff) << 16) |
342 (int32_t(idColor[3] & 0xff) << 24);
343 }
344
345private:
346 HdxPickTaskParams _params;
347 HdxPickTaskContextParams _contextParams;
348 TfTokenVector _renderTags;
349 bool _useOverlayPass;
350
351 // We need to cache a pointer to the render index so Execute() can
352 // map prim ID to paths.
353 HdRenderIndex *_index;
354
355 void _InitIfNeeded();
356 void _CreateAovBindings();
357 void _CleanupAovBindings();
358 void _ResizeOrCreateBufferForAOV(
359 const HdRenderPassAovBinding& aovBinding);
360
361 void _ConditionStencilWithGLCallback(
362 HdxPickTaskContextParams::DepthMaskCallback maskCallback,
363 HdRenderBuffer const * depthStencilBuffer);
364
365 bool _UseOcclusionPass() const;
366 bool _UseOverlayPass() const;
367 void _UpdateUseOverlayPass();
368
369 void _ClearPickBuffer();
370 void _ResolveDeep();
371
372 template<typename T>
373 HdStTextureUtils::AlignedBuffer<T>
374 _ReadAovBuffer(TfToken const & aovName) const;
375
376 HdRenderBuffer const * _FindAovBuffer(TfToken const & aovName) const;
377
378 // Create a shared render pass each for pickables, unpickables, and
379 // items in overlay (which may draw on top even when occluded).
380 HdRenderPassSharedPtr _pickableRenderPass;
381 HdRenderPassSharedPtr _occluderRenderPass;
382 HdRenderPassSharedPtr _overlayRenderPass;
383
384 // Having separate render pass states allows us to use different
385 // shader mixins if we choose to (we don't currently).
386 HdRenderPassStateSharedPtr _pickableRenderPassState;
387 HdRenderPassStateSharedPtr _occluderRenderPassState;
388 HdRenderPassStateSharedPtr _overlayRenderPassState;
389
390 Hgi* _hgi;
391
392 std::vector<std::unique_ptr<HdStRenderBuffer>> _pickableAovBuffers;
393 HdRenderPassAovBindingVector _pickableAovBindings;
394 HdRenderPassAovBinding _occluderAovBinding;
395 size_t _pickableDepthIndex;
396 TfToken _depthToken;
397 std::unique_ptr<HdStRenderBuffer> _overlayDepthStencilBuffer;
398 HdRenderPassAovBindingVector _overlayAovBindings;
399
400 // pick buffer used for deep selection
401 HdBufferArrayRangeSharedPtr _pickBuffer;
402
403 HdxPickTask() = delete;
404 HdxPickTask(const HdxPickTask &) = delete;
405 HdxPickTask &operator =(const HdxPickTask &) = delete;
406};
407
410public:
411
412 // Pick result takes a tuple of ID buffers:
413 // - (primId, instanceId, elementId, edgeId, pointId)
414 // along with some geometric buffers:
415 // - (depth, Neye)
416 // ... and resolves them into a series of hits, using one of the
417 // algorithms specified below.
418 //
419 // index is used to fill in the HdxPickHit structure;
420 // pickTarget is used to determine what a valid hit is;
421 // viewMatrix, projectionMatrix, depthRange are used for unprojection
422 // to calculate the worldSpaceHitPosition and worldSpaceHitNormal.
423 // bufferSize is the size of the ID buffers, and subRect is the sub-region
424 // of the id buffers to iterate over in the resolution algorithm.
425 //
426 // All buffers need to be the same size, if passed in. It's legal for
427 // only the depth and primId buffers to be provided; everything else is
428 // optional but provides a richer picking result.
429 HDX_API
430 HdxPickResult(int const* primIds,
431 int const* instanceIds,
432 int const* elementIds,
433 int const* edgeIds,
434 int const* pointIds,
435 int const* neyes,
436 float const* depths,
437 HdRenderIndex const *index,
438 TfToken const& pickTarget,
439 GfMatrix4d const& viewMatrix,
440 GfMatrix4d const& projectionMatrix,
441 GfVec2f const& depthRange,
442 GfVec2i const& bufferSize,
443 GfVec4i const& subRect);
444
445 HDX_API
447
448 HDX_API
450 HDX_API
451 HdxPickResult& operator=(HdxPickResult &&);
452
454 HDX_API
455 bool IsValid() const;
456
460 HDX_API
461 void ResolveNearestToCamera(HdxPickHitVector* allHits) const;
462
466 HDX_API
467 void ResolveNearestToCenter(HdxPickHitVector* allHits) const;
468
471 HDX_API
472 void ResolveAll(HdxPickHitVector* allHits) const;
473
476 HDX_API
477 void ResolveUnique(HdxPickHitVector* allHits) const;
478
479private:
480 bool _ResolveHit(int index, int x, int y, float z, HdxPickHit* hit) const;
481
482 size_t _GetHash(int index) const;
483 bool _IsValidHit(int index) const;
484
485 // Provide accessors for all of the ID buffers. Since all but _primIds
486 // are optional, if the buffer doesn't exist just return -1 (== no hit).
487 int _GetPrimId(int index) const {
488 return _primIds ? _primIds[index] : -1;
489 }
490 int _GetInstanceId(int index) const {
491 return _instanceIds ? _instanceIds[index] : -1;
492 }
493 int _GetElementId(int index) const {
494 return _elementIds ? _elementIds[index] : -1;
495 }
496 int _GetEdgeId(int index) const {
497 return _edgeIds ? _edgeIds[index] : -1;
498 }
499 int _GetPointId(int index) const {
500 return _pointIds ? _pointIds[index] : -1;
501 }
502
503 // Provide an accessor for the normal buffer. If the normal buffer is
504 // provided, this function will unpack the normal. The fallback is
505 // GfVec3f(0.0f).
506 GfVec3f _GetNormal(int index) const;
507
508 int const* _primIds;
509 int const* _instanceIds;
510 int const* _elementIds;
511 int const* _edgeIds;
512 int const* _pointIds;
513 int const* _neyes;
514 float const* _depths;
515 HdRenderIndex const *_index;
516 TfToken _pickTarget;
517 GfMatrix4d _ndcToWorld;
518 GfMatrix4d _eyeToWorld;
519 GfVec2f _depthRange;
520 GfVec2i _bufferSize;
521 GfVec4i _subRect;
522};
523
524// For sorting, order hits by ndc depth.
525HDX_API
526bool operator<(HdxPickHit const& lhs, HdxPickHit const& rhs);
527
528// VtValue requirements
529HDX_API
530std::ostream& operator<<(std::ostream& out, const HdxPickHit& h);
531HDX_API
532bool operator==(const HdxPickHit& lhs,
533 const HdxPickHit& rhs);
534HDX_API
535bool operator!=(const HdxPickHit& lhs,
536 const HdxPickHit& rhs);
537
538HDX_API
539std::ostream& operator<<(std::ostream& out, const HdxPickTaskParams& pv);
540HDX_API
541bool operator==(const HdxPickTaskParams& lhs,
542 const HdxPickTaskParams& rhs);
543HDX_API
544bool operator!=(const HdxPickTaskParams& lhs,
545 const HdxPickTaskParams& rhs);
546
547HDX_API
548std::ostream& operator<<(std::ostream& out, const HdxPickTaskContextParams& pv);
549HDX_API
550bool operator==(const HdxPickTaskContextParams& lhs,
551 const HdxPickTaskContextParams& rhs);
552HDX_API
553bool operator!=(const HdxPickTaskContextParams& lhs,
554 const HdxPickTaskContextParams& rhs);
555PXR_NAMESPACE_CLOSE_SCOPE
556
557#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:409
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:309
static int DecodeIDRenderColor(unsigned char const idColor[4])
Utility: Given a UNorm8Vec4 pixel, unpack it into an int32 ID.
Definition: pickTask.h:338
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:116
HdContainerDataSourceHandle instancerPrimOrigin
The prim origin data source of the instancer.
Definition: pickTask.h:120
HdContainerDataSourceHandle instancePrimOrigin
The prim origin data source of the picked (implicit) instance.
Definition: pickTask.h:143
int instanceId
Index of the picked instance.
Definition: pickTask.h:145
SdfPath instanceSceneIndexPath
For implicit instancing (native instancing in USD), the path of the picked instance in the scene inde...
Definition: pickTask.h:124
SdfPath instancerSceneIndexPath
The path of the instancer in the scene index.
Definition: pickTask.h:118
Picking hit structure.
Definition: pickTask.h:77
float normalizedDepth
normalizedDepth is in the range [0,1].
Definition: pickTask.h:101
SdfPath delegateId
delegateID of HdSceneDelegate that provided the picked prim.
Definition: pickTask.h:82
SdfPath objectId
Path computed from scenePath's in primOrigin data source of picked prim and instancers if provided by...
Definition: pickTask.h:86
SdfPath instancerId
Only supported for scene delegates, see HdxPrimOriginInfo for scene indices.
Definition: pickTask.h:92
Pick task context params.
Definition: pickTask.h:264
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:166
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:228
std::vector< HdxInstancerContext > instancerContexts
Information about the instancers instancing the picked object.
Definition: pickTask.h:225
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