task.h
1 //
2 // Copyright 2016 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_HD_TASK_H
8 #define PXR_IMAGING_HD_TASK_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/imaging/hd/api.h"
12 #include "pxr/imaging/hd/driver.h"
13 #include "pxr/imaging/hd/version.h"
14 
15 #include "pxr/imaging/hd/sceneDelegate.h"
16 
17 #include "pxr/usd/sdf/path.h"
18 #include "pxr/base/tf/hashmap.h"
19 #include "pxr/base/vt/value.h"
20 #include "pxr/base/vt/dictionary.h"
21 
22 #include <memory>
23 #include <vector>
24 #include <unordered_map>
25 
26 PXR_NAMESPACE_OPEN_SCOPE
27 
28 
29 using HdTaskSharedPtr = std::shared_ptr<class HdTask>;
30 using HdTaskSharedPtrVector = std::vector<HdTaskSharedPtr>;
31 
32 // We want to use token as a key not std::string, so use an unordered_map over
33 // VtDictionary
34 using HdTaskContext =
35  std::unordered_map<TfToken, VtValue, TfToken::HashFunctor>;
36 
43 class HdTask
44 {
45 public:
51  HD_API
52  HdTask(SdfPath const& id);
53 
54  HD_API
55  virtual ~HdTask();
56 
66  HD_API
67  virtual bool IsConverged() const;
68 
97 
98  virtual void Sync(HdSceneDelegate* delegate,
99  HdTaskContext* ctx,
100  HdDirtyBits* dirtyBits) = 0;
101 
121  virtual void Prepare(HdTaskContext* ctx,
122  HdRenderIndex* renderIndex) = 0;
123 
133  virtual void Execute(HdTaskContext* ctx) = 0;
134 
153  HD_API
154  virtual const TfTokenVector &GetRenderTags() const;
155 
156  SdfPath const& GetId() const { return _id; }
157 
161  HD_API
162  virtual HdDirtyBits GetInitialDirtyBitsMask() const;
163 
164 
165 protected:
167  HD_API
168  static bool _HasTaskContextData(HdTaskContext const* ctx,
169  TfToken const& id);
170 
178  template <class T>
179  static bool _GetTaskContextData(HdTaskContext const* ctx,
180  TfToken const& id,
181  T* outValue);
182 
190  template <class T>
191  bool _GetTaskParams(HdSceneDelegate* delegate,
192  T* outValue);
193 
194  HD_API
195  TfTokenVector _GetTaskRenderTags(HdSceneDelegate* delegate);
196 
199  template <class T>
200  static T _GetDriver(
201  HdTaskContext const* ctx,
202  TfToken const& driverName);
203 
204 private:
205  SdfPath _id;
206 
207  HdTask() = delete;
208  HdTask(const HdTask &) = delete;
209  HdTask &operator =(const HdTask &) = delete;
210 };
211 
212 // Inline template body
213 template <class T>
214 bool
215 HdTask::_GetTaskContextData(HdTaskContext const* ctx,
216  TfToken const& id,
217  T* outValue)
218 {
219  TF_DEV_AXIOM(outValue != nullptr);
220 
221  if (!ctx) {
222  return false;
223  }
224 
225  HdTaskContext::const_iterator valueIt = ctx->find(id);
226  if (valueIt == ctx->cend()) {
227  TF_CODING_ERROR("Token %s missing from task context", id.GetText());
228  return false;
229  }
230 
231  const VtValue &valueVt = (valueIt->second);
232  if (!valueVt.IsHolding<T>()) {
233  TF_CODING_ERROR("Token %s in task context is of mismatched type",
234  id.GetText());
235  return false;
236  }
237 
238  *outValue = valueVt.UncheckedGet<T>();
239 
240  return true;
241 }
242 
243 template <class T>
244 bool
246  T* outValue)
247 {
248  TF_DEV_AXIOM(outValue != nullptr);
249 
250  SdfPath const& taskId = GetId();
251 
252  VtValue valueVt = delegate->Get(taskId, HdTokens->params);
253  if (!valueVt.IsHolding<T>()) {
254  TF_CODING_ERROR("Task params for %s is of unexpected type",
255  taskId.GetText());
256  return false;
257  }
258 
259  *outValue = valueVt.UncheckedGet<T>();
260 
261  return true;
262 }
263 
264 template <class T>
265 T
267  HdTaskContext const* ctx,
268  TfToken const& driverName)
269 {
270  auto it = ctx->find(HdTokens->drivers);
271  if (it != ctx->end()) {
272  VtValue const& value = it->second;
273  if (value.IsHolding<HdDriverVector>()) {
274  HdDriverVector const& drivers= value.UncheckedGet<HdDriverVector>();
275  for (HdDriver* hdDriver : drivers) {
276  if (hdDriver->name == driverName) {
277  if (hdDriver->driver.IsHolding<T>()) {
278  return hdDriver->driver.UncheckedGet<T>();
279  }
280  }
281  }
282  }
283  }
284 
285  return nullptr;
286 }
287 
288 PXR_NAMESPACE_CLOSE_SCOPE
289 
290 #endif // PXR_IMAGING_HD_TASK_H
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:206
The render index is part of the Hydra 1.0 API and is only used for emulation purposes so that HdScene...
Definition: renderIndex.h:110
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1105
virtual void Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits)=0
Sync Phase: Obtain task state from Scene delegate based on change processing.
virtual HD_API VtValue Get(SdfPath const &id, TfToken const &key)
Returns a named value.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:69
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.
static bool _GetTaskContextData(HdTaskContext const *ctx, TfToken const &id, T *outValue)
Extracts a typed value out of the task context at the given id.
Definition: task.h:215
virtual HD_API const TfTokenVector & GetRenderTags() const
Render Tag Gather.
virtual void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex)=0
Prepare Phase: Resolve bindings and manage resources.
virtual HD_API HdDirtyBits GetInitialDirtyBitsMask() const
Returns the minimal set of dirty bits to place in the change tracker for use in the first sync of thi...
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:80
HdDriver represents a device object, commonly a render device, that is owned by the application and p...
Definition: driver.h:23
virtual void Execute(HdTaskContext *ctx)=0
Execute Phase: Runs the task.
Adapter class providing data exchange with the client scene graph.
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:489
HdTask represents a unit of work to perform during a Hydra render.
Definition: task.h:43
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:280
virtual HD_API bool IsConverged() const
This function returns true when a (progressive) task considers its execution results converged.
static T _GetDriver(HdTaskContext const *ctx, TfToken const &driverName)
Extract an object from a HdDriver inside the task context.
Definition: task.h:266
bool _GetTaskParams(HdSceneDelegate *delegate, T *outValue)
Extracts a typed value out of the task context at the given id.
Definition: task.h:245
static HD_API bool _HasTaskContextData(HdTaskContext const *ctx, TfToken const &id)
Check if the shared context contains a value for the given id.
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1061
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:89