Loading...
Searching...
No Matches
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"
21
22#include <memory>
23#include <vector>
24#include <unordered_map>
25
26PXR_NAMESPACE_OPEN_SCOPE
27
28
29using HdTaskSharedPtr = std::shared_ptr<class HdTask>;
30using 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
34using HdTaskContext =
35 std::unordered_map<TfToken, VtValue, TfToken::HashFunctor>;
36
43class HdTask
44{
45public:
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
165protected:
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
204private:
205 SdfPath _id;
206
207 HdTask() = delete;
208 HdTask(const HdTask &) = delete;
209 HdTask &operator =(const HdTask &) = delete;
210};
211
212// Inline template body
213template <class T>
214bool
215HdTask::_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
243template <class T>
244bool
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
264template <class T>
265T
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
288PXR_NAMESPACE_CLOSE_SCOPE
289
290#endif // PXR_IMAGING_HD_TASK_H
HdDriver represents a device object, commonly a render device, that is owned by the application and p...
Definition: driver.h:23
The Hydra render index is a flattened representation of the client scene graph, which may be composed...
Definition: renderIndex.h:105
Adapter class providing data exchange with the client scene graph.
virtual HD_API VtValue Get(SdfPath const &id, TfToken const &key)
Returns a named value.
HdTask represents a unit of work to perform during a Hydra render.
Definition: task.h:44
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
HD_API HdTask(SdfPath const &id)
Construct a new task.
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.
virtual void Execute(HdTaskContext *ctx)=0
Execute Phase: Runs the task.
virtual HD_API const TfTokenVector & GetRenderTags() const
Render Tag Gather.
static T _GetDriver(HdTaskContext const *ctx, TfToken const &driverName)
Extract an object from a HdDriver inside the task context.
Definition: task.h:266
virtual void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex)=0
Prepare Phase: Resolve bindings and manage resources.
virtual HD_API bool IsConverged() const
This function returns true when a (progressive) task considers its execution results converged.
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 HdDirtyBits GetInitialDirtyBitsMask() const
Returns the minimal set of dirty bits to place in the change tracker for use in the first sync of thi...
A path value used to locate objects in layers or scenegraphs.
Definition: path.h:274
SDF_API const char * GetText() const
Returns the string representation of this path as a c string.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1002
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1046
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:205
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:68
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440