All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pickTask.h
1 //
2 // Copyright 2019 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 HDX_PICK_TASK_H
25 #define HDX_PICK_TASK_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hdx/api.h"
29 
30 #include "pxr/imaging/hd/enums.h"
31 #include "pxr/imaging/hd/renderPass.h"
32 #include "pxr/imaging/hd/renderPassState.h"
33 #include "pxr/imaging/hd/rprimCollection.h"
34 #include "pxr/imaging/hd/task.h"
35 
36 #include "pxr/imaging/glf/drawTarget.h"
37 
38 #include "pxr/base/tf/declarePtrs.h"
39 #include "pxr/base/gf/matrix4d.h"
40 #include "pxr/base/gf/vec2i.h"
41 #include "pxr/base/gf/vec2f.h"
42 #include "pxr/base/gf/vec4i.h"
43 #include "pxr/base/gf/vec4d.h"
44 #include "pxr/usd/sdf/path.h"
45 
46 #include <vector>
47 #include <memory>
48 
49 PXR_NAMESPACE_OPEN_SCOPE
50 
51 #define HDX_PICK_TOKENS \
52  /* Task context */ \
53  (pickParams) \
54  \
55  /* Hit mode */ \
56  (hitFirst) \
57  (hitAll) \
58  \
59  /* Pick target */ \
60  (pickPrimsAndInstances) \
61  (pickFaces) \
62  (pickEdges) \
63  (pickPoints) \
64  \
65  /* Resolve mode */ \
66  (resolveNearestToCamera) \
67  (resolveNearestToCenter) \
68  (resolveUnique) \
69  (resolveAll)
70 
71 TF_DECLARE_PUBLIC_TOKENS(HdxPickTokens, HDX_API, HDX_PICK_TOKENS);
72 
74 class HdStShaderCode;
75 typedef boost::shared_ptr<HdStShaderCode> HdStShaderCodeSharedPtr;
76 
81 {
83  : cullStyle(HdCullStyleNothing)
84  , enableSceneMaterials(true)
85  {}
86 
87  HdCullStyle cullStyle;
88  bool enableSceneMaterials;
89 };
90 
93 struct HdxPickHit {
94  SdfPath delegateId;
95  SdfPath objectId;
96  SdfPath instancerId;
97  int instanceIndex;
98  int elementIndex;
99  int edgeIndex;
100  int pointIndex;
101  GfVec3f worldSpaceHitPoint;
102  GfVec3f worldSpaceHitNormal;
103  float ndcDepth;
104 
105  inline bool IsValid() const {
106  return !objectId.IsEmpty();
107  }
108 
109  HDX_API
110  size_t GetHash() const;
111 };
112 
113 typedef std::vector<HdxPickHit> HdxPickHitVector;
114 
120 {
121  typedef std::function<void(void)> DepthMaskCallback;
122 
124  : resolution(128, 128)
125  , hitMode(HdxPickTokens->hitFirst)
126  , pickTarget(HdxPickTokens->pickPrimsAndInstances)
127  , resolveMode(HdxPickTokens->resolveNearestToCamera)
128  , doUnpickablesOcclude(false)
129  , viewMatrix(1)
130  , projectionMatrix(1)
131  , clipPlanes()
132  , depthMaskCallback(nullptr)
133  , collection()
134  , outHits(nullptr)
135  {}
136 
137  GfVec2i resolution;
138  TfToken hitMode;
139  TfToken pickTarget;
140  TfToken resolveMode;
141  bool doUnpickablesOcclude;
142  GfMatrix4d viewMatrix;
143  GfMatrix4d projectionMatrix;
144  std::vector<GfVec4d> clipPlanes;
145  DepthMaskCallback depthMaskCallback;
146  HdRprimCollection collection;
147  HdxPickHitVector *outHits;
148 };
149 
162 class HdxPickTask : public HdTask
163 {
164 public:
165  HDX_API
166  HdxPickTask(HdSceneDelegate* delegate, SdfPath const& id);
167 
168  HDX_API
169  virtual ~HdxPickTask();
170 
172  HDX_API
173  virtual void Sync(HdSceneDelegate* delegate,
174  HdTaskContext* ctx,
175  HdDirtyBits* dirtyBits) override;
176 
178  HDX_API
179  virtual void Prepare(HdTaskContext* ctx,
180  HdRenderIndex* renderIndex) override;
181 
183  HDX_API
184  virtual void Execute(HdTaskContext* ctx) override;
185 
186  HDX_API
187  virtual const TfTokenVector &GetRenderTags() const override;
188 
190  static inline int DecodeIDRenderColor(unsigned char const idColor[4]) {
191  return (int32_t(idColor[0] & 0xff) << 0) |
192  (int32_t(idColor[1] & 0xff) << 8) |
193  (int32_t(idColor[2] & 0xff) << 16) |
194  (int32_t(idColor[3] & 0xff) << 24);
195  }
196 
197 private:
198  HdxPickTaskParams _params;
199  HdxPickTaskContextParams _contextParams;
200  TfTokenVector _renderTags;
201 
202  // We need to cache a pointer to the render index so Execute() can
203  // map prim ID to paths.
204  HdRenderIndex *_index;
205 
206  void _Init(GfVec2i const& widthHeight);
207  void _SetResolution(GfVec2i const& widthHeight);
208  void _ConditionStencilWithGLCallback(
209  HdxPickTaskContextParams::DepthMaskCallback maskCallback);
210  void _ConfigureSceneMaterials(
211  bool enableSceneMaterials, HdStRenderPassState *renderPassState);
212 
213  bool _UseOcclusionPass() const;
214 
215  // Create a shared render pass each for pickables and unpickables
216  HdRenderPassSharedPtr _pickableRenderPass;
217  HdRenderPassSharedPtr _occluderRenderPass;
218 
219  // Override shader is used when scene materials are disabled
220  HdStShaderCodeSharedPtr _overrideShader;
221 
222  // Having separate render pass states allows us to use different
223  // shader mixins if we choose to (we don't currently).
224  HdRenderPassStateSharedPtr _pickableRenderPassState;
225  HdRenderPassStateSharedPtr _occluderRenderPassState;
226 
227  // A single draw target is shared for all contexts. Since the FBO cannot
228  // be shared, we clone the attachments on each request.
229  GlfDrawTargetRefPtr _drawTarget;
230 
231  HdxPickTask() = delete;
232  HdxPickTask(const HdxPickTask &) = delete;
233  HdxPickTask &operator =(const HdxPickTask &) = delete;
234 };
235 
238 public:
239 
240  // Pick result takes a tuple of ID buffers:
241  // - (primId, instanceId, elementId, edgeId, pointId)
242  // along with some geometric buffers:
243  // - (depth, Neye)
244  // ... and resolves them into a series of hits, using one of the
245  // algorithms specified below.
246  //
247  // index is used to fill in the HdxPickHit structure;
248  // pickTarget is used to determine what a valid hit is;
249  // viewMatrix, projectionMatrix, depthRange are used for unprojection
250  // to calculate the worldSpaceHitPosition and worldSpaceHitNormal.
251  // bufferSize is the size of the ID buffers, and subRect is the sub-region
252  // of the id buffers to iterate over in the resolution algorithm.
253  //
254  // All buffers need to be the same size, if passed in. It's legal for
255  // only the depth and primId buffers to be provided; everything else is
256  // optional but provides a richer picking result.
257  HDX_API
258  HdxPickResult(int const* primIds,
259  int const* instanceIds,
260  int const* elementIds,
261  int const* edgeIds,
262  int const* pointIds,
263  int const* neyes,
264  float const* depths,
265  HdRenderIndex const *index,
266  TfToken const& pickTarget,
267  GfMatrix4d const& viewMatrix,
268  GfMatrix4d const& projectionMatrix,
269  GfVec2f const& depthRange,
270  GfVec2i const& bufferSize,
271  GfVec4i const& subRect);
272 
273  HDX_API
274  ~HdxPickResult();
275 
276  HDX_API
277  HdxPickResult(HdxPickResult &&);
278  HDX_API
279  HdxPickResult& operator=(HdxPickResult &&);
280 
282  HDX_API
283  bool IsValid() const;
284 
288  HDX_API
289  void ResolveNearestToCamera(HdxPickHitVector* allHits) const;
290 
294  HDX_API
295  void ResolveNearestToCenter(HdxPickHitVector* allHits) const;
296 
299  HDX_API
300  void ResolveAll(HdxPickHitVector* allHits) const;
301 
304  HDX_API
305  void ResolveUnique(HdxPickHitVector* allHits) const;
306 
307 private:
308  bool _ResolveHit(int index, int x, int y, float z, HdxPickHit* hit) const;
309  size_t _GetHash(int index) const;
310  bool _IsValidHit(int index) const;
311 
312  // Provide accessors for all of the ID buffers. Since all but _primIds
313  // are optional, if the buffer doesn't exist just return -1 (== no hit).
314  int _GetPrimId(int index) const {
315  return _primIds ? _primIds[index] : -1;
316  }
317  int _GetInstanceId(int index) const {
318  return _instanceIds ? _instanceIds[index] : -1;
319  }
320  int _GetElementId(int index) const {
321  return _elementIds ? _elementIds[index] : -1;
322  }
323  int _GetEdgeId(int index) const {
324  return _edgeIds ? _edgeIds[index] : -1;
325  }
326  int _GetPointId(int index) const {
327  return _pointIds ? _pointIds[index] : -1;
328  }
329 
330  // Provide an accessor for the normal buffer. If the normal buffer is
331  // provided, this function will unpack the normal. The fallback is
332  // GfVec3f(0.0f).
333  GfVec3f _GetNormal(int index) const;
334 
335  int const* _primIds;
336  int const* _instanceIds;
337  int const* _elementIds;
338  int const* _edgeIds;
339  int const* _pointIds;
340  int const* _neyes;
341  float const* _depths;
342  HdRenderIndex const *_index;
343  TfToken _pickTarget;
344  GfMatrix4d _ndcToWorld;
345  GfMatrix4d _eyeToWorld;
346  GfVec2f _depthRange;
347  GfVec2i _bufferSize;
348  GfVec4i _subRect;
349 };
350 
351 // For sorting, order hits by ndc depth.
352 HDX_API
353 bool operator<(HdxPickHit const& lhs, HdxPickHit const& rhs);
354 
355 // VtValue requirements
356 HDX_API
357 std::ostream& operator<<(std::ostream& out, const HdxPickHit& h);
358 HDX_API
359 bool operator==(const HdxPickHit& lhs,
360  const HdxPickHit& rhs);
361 HDX_API
362 bool operator!=(const HdxPickHit& lhs,
363  const HdxPickHit& rhs);
364 
365 HDX_API
366 std::ostream& operator<<(std::ostream& out, const HdxPickTaskParams& pv);
367 HDX_API
368 bool operator==(const HdxPickTaskParams& lhs,
369  const HdxPickTaskParams& rhs);
370 HDX_API
371 bool operator!=(const HdxPickTaskParams& lhs,
372  const HdxPickTaskParams& rhs);
373 
374 HDX_API
375 std::ostream& operator<<(std::ostream& out, const HdxPickTaskContextParams& pv);
376 HDX_API
377 bool operator==(const HdxPickTaskContextParams& lhs,
378  const HdxPickTaskContextParams& rhs);
379 HDX_API
380 bool operator!=(const HdxPickTaskContextParams& lhs,
381  const HdxPickTaskContextParams& rhs);
382 PXR_NAMESPACE_CLOSE_SCOPE
383 
384 #endif // HDX_PICK_TASK_H
Basic type for a vector of 4 int components.
Definition: vec4i.h:61
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:119
HDX_API void ResolveAll(HdxPickHitVector *allHits) const
Return all hit points.
Pick task params.
Definition: pickTask.h:80
Basic type for a vector of 2 int components.
Definition: vec2i.h:61
Pick task context params.
Definition: pickTask.h:119
virtual HDX_API void Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits) override
Sync the render pass resources.
HDX_API void ResolveUnique(HdxPickHitVector *allHits) const
Return the set of unique hit points, keeping only the nearest depth value.
Basic type for a vector of 3 float components.
Definition: vec3f.h:63
AR_API bool operator!=(const ArAssetInfo &lhs, const ArAssetInfo &rhs)
A set of rendering parameters used among render passes.
AR_API bool operator==(const ArAssetInfo &lhs, const ArAssetInfo &rhs)
Picking hit structure.
Definition: pickTask.h:93
HDX_API void ResolveNearestToCenter(HdxPickHitVector *allHits) const
Return the nearest single hit point from the center of the viewport.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
HDX_API void ResolveNearestToCamera(HdxPickHitVector *allHits) const
Return the nearest single hit point.
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
Adapter class providing data exchange with the client scene graph.
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:118
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:438
A named, semantic collection of objects.
A utility class for resolving ID buffers into hits.
Definition: pickTask.h:237
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:287
HDX_API bool IsValid() const
Return whether the result was given well-formed parameters.
virtual HDX_API void Execute(HdTaskContext *ctx) override
Execute the pick task.
A base class representing the implementation (code) of a shader, used in conjunction with HdRenderPas...
Definition: shaderCode.h:59
static int DecodeIDRenderColor(unsigned char const idColor[4])
Utility: Given a UNorm8Vec4 pixel, unpack it into an int32 ID.
Definition: pickTask.h:190
GF_API std::ostream & operator<<(std::ostream &, const GfBBox3d &)
Output a GfBBox3d using the format [(range) matrix zeroArea].
Basic type for a vector of 2 float components.
Definition: vec2f.h:63
A task for running picking queries against the current scene.
Definition: pickTask.h:162
virtual HDX_API void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex) override
Prepare the pick task.
bool IsEmpty() const noexcept
Returns true if this is the empty path (SdfPath::EmptyPath()).
Definition: path.h:409