All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 {
44public:
50 HD_API
51 HdTask(SdfPath const& id);
52
53 HD_API
54 virtual ~HdTask();
55
84
85 virtual void Sync(HdSceneDelegate* delegate,
86 HdTaskContext* ctx,
87 HdDirtyBits* dirtyBits) = 0;
88
108 virtual void Prepare(HdTaskContext* ctx,
109 HdRenderIndex* renderIndex) = 0;
110
120 virtual void Execute(HdTaskContext* ctx) = 0;
121
140 HD_API
141 virtual const TfTokenVector &GetRenderTags() const;
142
143 SdfPath const& GetId() const { return _id; }
144
148 HD_API
149 virtual HdDirtyBits GetInitialDirtyBitsMask() const;
150
151
152protected:
154 HD_API
155 static bool _HasTaskContextData(HdTaskContext const* ctx,
156 TfToken const& id);
157
165 template <class T>
166 static bool _GetTaskContextData(HdTaskContext const* ctx,
167 TfToken const& id,
168 T* outValue);
169
177 template <class T>
178 bool _GetTaskParams(HdSceneDelegate* delegate,
179 T* outValue);
180
181 HD_API
182 TfTokenVector _GetTaskRenderTags(HdSceneDelegate* delegate);
183
186 template <class T>
187 static T _GetDriver(
188 HdTaskContext const* ctx,
189 TfToken const& driverName);
190
191private:
192 SdfPath _id;
193
194 HdTask() = delete;
195 HdTask(const HdTask &) = delete;
196 HdTask &operator =(const HdTask &) = delete;
197};
198
199// Inline template body
200template <class T>
201bool
202HdTask::_GetTaskContextData(HdTaskContext const* ctx,
203 TfToken const& id,
204 T* outValue)
205{
206 TF_DEV_AXIOM(outValue != nullptr);
207
208 if (!ctx) {
209 return false;
210 }
211
212 HdTaskContext::const_iterator valueIt = ctx->find(id);
213 if (valueIt == ctx->cend()) {
214 TF_CODING_ERROR("Token %s missing from task context", id.GetText());
215 return false;
216 }
217
218 const VtValue &valueVt = (valueIt->second);
219 if (!valueVt.IsHolding<T>()) {
220 TF_CODING_ERROR("Token %s in task context is of mismatched type",
221 id.GetText());
222 return false;
223 }
224
225 *outValue = valueVt.UncheckedGet<T>();
226
227 return true;
228}
229
230template <class T>
231bool
233 T* outValue)
234{
235 TF_DEV_AXIOM(outValue != nullptr);
236
237 SdfPath const& taskId = GetId();
238
239 VtValue valueVt = delegate->Get(taskId, HdTokens->params);
240 if (!valueVt.IsHolding<T>()) {
241 TF_CODING_ERROR("Task params for %s is of unexpected type",
242 taskId.GetText());
243 return false;
244 }
245
246 *outValue = valueVt.UncheckedGet<T>();
247
248 return true;
249}
250
251template <class T>
252T
254 HdTaskContext const* ctx,
255 TfToken const& driverName)
256{
257 auto it = ctx->find(HdTokens->drivers);
258 if (it != ctx->end()) {
259 VtValue const& value = it->second;
260 if (value.IsHolding<HdDriverVector>()) {
261 HdDriverVector const& drivers= value.UncheckedGet<HdDriverVector>();
262 for (HdDriver* hdDriver : drivers) {
263 if (hdDriver->name == driverName) {
264 if (hdDriver->driver.IsHolding<T>()) {
265 return hdDriver->driver.UncheckedGet<T>();
266 }
267 }
268 }
269 }
270 }
271
272 return nullptr;
273}
274
275PXR_NAMESPACE_CLOSE_SCOPE
276
277#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:104
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:43
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:202
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:232
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:253
virtual void Prepare(HdTaskContext *ctx, HdRenderIndex *renderIndex)=0
Prepare Phase: Resolve bindings and manage resources.
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:147
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definition: value.h:1064
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1104
#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