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
37class HdTask {
38public:
44 HD_API
45 HdTask(SdfPath const& id);
46
47 HD_API
48 virtual ~HdTask();
49
78
79 virtual void Sync(HdSceneDelegate* delegate,
80 HdTaskContext* ctx,
81 HdDirtyBits* dirtyBits) = 0;
82
102 virtual void Prepare(HdTaskContext* ctx,
103 HdRenderIndex* renderIndex) = 0;
104
114 virtual void Execute(HdTaskContext* ctx) = 0;
115
134 HD_API
135 virtual const TfTokenVector &GetRenderTags() const;
136
137 SdfPath const& GetId() const { return _id; }
138
142 HD_API
143 virtual HdDirtyBits GetInitialDirtyBitsMask() const;
144
145
146protected:
148 HD_API
149 static bool _HasTaskContextData(HdTaskContext const* ctx,
150 TfToken const& id);
151
159 template <class T>
160 static bool _GetTaskContextData(HdTaskContext const* ctx,
161 TfToken const& id,
162 T* outValue);
163
171 template <class T>
172 bool _GetTaskParams(HdSceneDelegate* delegate,
173 T* outValue);
174
175 HD_API
176 TfTokenVector _GetTaskRenderTags(HdSceneDelegate* delegate);
177
180 template <class T>
181 static T _GetDriver(
182 HdTaskContext const* ctx,
183 TfToken const& driverName);
184
185private:
186 SdfPath _id;
187
188 HdTask() = delete;
189 HdTask(const HdTask &) = delete;
190 HdTask &operator =(const HdTask &) = delete;
191};
192
193// Inline template body
194template <class T>
195bool
196HdTask::_GetTaskContextData(HdTaskContext const* ctx,
197 TfToken const& id,
198 T* outValue)
199{
200 TF_DEV_AXIOM(outValue != nullptr);
201
202 if (!ctx) {
203 return false;
204 }
205
206 HdTaskContext::const_iterator valueIt = ctx->find(id);
207 if (valueIt == ctx->cend()) {
208 TF_CODING_ERROR("Token %s missing from task context", id.GetText());
209 return false;
210 }
211
212 const VtValue &valueVt = (valueIt->second);
213 if (!valueVt.IsHolding<T>()) {
214 TF_CODING_ERROR("Token %s in task context is of mismatched type",
215 id.GetText());
216 return false;
217 }
218
219 *outValue = valueVt.UncheckedGet<T>();
220
221 return true;
222}
223
224template <class T>
225bool
226HdTask::_GetTaskParams(HdSceneDelegate* delegate,
227 T* outValue)
228{
229 TF_DEV_AXIOM(outValue != nullptr);
230
231 SdfPath const& taskId = GetId();
232
233 VtValue valueVt = delegate->Get(taskId, HdTokens->params);
234 if (!valueVt.IsHolding<T>()) {
235 TF_CODING_ERROR("Task params for %s is of unexpected type",
236 taskId.GetText());
237 return false;
238 }
239
240 *outValue = valueVt.UncheckedGet<T>();
241
242 return true;
243}
244
245template <class T>
246T
247HdTask::_GetDriver(
248 HdTaskContext const* ctx,
249 TfToken const& driverName)
250{
251 auto it = ctx->find(HdTokens->drivers);
252 if (it != ctx->end()) {
253 VtValue const& value = it->second;
254 if (value.IsHolding<HdDriverVector>()) {
255 HdDriverVector const& drivers= value.UncheckedGet<HdDriverVector>();
256 for (HdDriver* hdDriver : drivers) {
257 if (hdDriver->name == driverName) {
258 if (hdDriver->driver.IsHolding<T>()) {
259 return hdDriver->driver.UncheckedGet<T>();
260 }
261 }
262 }
263 }
264 }
265
266 return nullptr;
267}
268
269PXR_NAMESPACE_CLOSE_SCOPE
270
271#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.
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