Loading...
Searching...
No Matches
dataSource.h
1//
2// Copyright 2021 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_DATASOURCE_H
8#define PXR_IMAGING_HD_DATASOURCE_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/imaging/hd/api.h"
13#include "pxr/imaging/hd/dataSourceLocator.h"
14
15#include "pxr/base/tf/token.h"
16#include "pxr/base/vt/value.h"
17#include "pxr/base/vt/visitValue.h"
18
19#include <iosfwd>
20#include <memory>
21#include <vector>
22#include <atomic>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
29#define HD_DECLARE_DATASOURCE_ABSTRACT(type) \
30 using Handle = std::shared_ptr<type>; \
31 using AtomicHandle = Handle; \
32 static Handle AtomicLoad(AtomicHandle &ptr) { \
33 return std::atomic_load(&ptr); \
34 } \
35 static void AtomicStore(AtomicHandle &ptr, const Handle &v) { \
36 std::atomic_store(&ptr, v); \
37 } \
38 static bool AtomicCompareExchange(AtomicHandle &ptr, \
39 AtomicHandle &expected, \
40 const Handle &desired) { \
41 return std::atomic_compare_exchange_strong(&ptr, &expected, desired); \
42 } \
43 static Handle Cast(const HdDataSourceBase::Handle &v) { \
44 return std::dynamic_pointer_cast<type>(v); \
45 }
46
53#define HD_DECLARE_DATASOURCE(type) \
54 HD_DECLARE_DATASOURCE_ABSTRACT(type) \
55 template <typename ... Args> \
56 static Handle New(Args&& ... args) { \
57 return Handle(new type(std::forward<Args>(args) ... )); \
58 }
59
63#define HD_DECLARE_DATASOURCE_INITIALIZER_LIST_NEW(type, T) \
64 static Handle New(std::initializer_list<T> initList) { \
65 return Handle(new type(initList)); \
66 }
67
68#define HD_DECLARE_DATASOURCE_HANDLES(type) \
69 using type##Handle = type::Handle; \
70 using type##AtomicHandle = type::AtomicHandle;
71
82{
83public:
84 HD_DECLARE_DATASOURCE_ABSTRACT(HdDataSourceBase)
85
86 HD_API
87 virtual ~HdDataSourceBase() = 0;
88};
89
90HD_DECLARE_DATASOURCE_HANDLES(HdDataSourceBase);
91
100{
101public:
102 HD_DECLARE_DATASOURCE_ABSTRACT(HdContainerDataSource);
103
106 virtual TfTokenVector GetNames() = 0;
107
110 virtual HdDataSourceBaseHandle Get(const TfToken &name) = 0;
111
116 HD_API
117 static HdDataSourceBaseHandle Get(
118 const Handle &container,
119 const HdDataSourceLocator &locator);
120};
121
122HD_DECLARE_DATASOURCE_HANDLES(HdContainerDataSource);
123
132{
133public:
134 HD_DECLARE_DATASOURCE_ABSTRACT(HdVectorDataSource);
135
138 virtual size_t GetNumElements() = 0;
139
143 virtual HdDataSourceBaseHandle GetElement(size_t element) = 0;
144};
145
146HD_DECLARE_DATASOURCE_HANDLES(HdVectorDataSource);
147
154{
155public:
156 HD_DECLARE_DATASOURCE_ABSTRACT(HdSampledDataSource);
157 using Time = float;
158
165 virtual VtValue GetValue(Time shutterOffset) = 0;
166
181 Time startTime,
182 Time endTime,
183 std::vector<Time> * outSampleTimes) = 0;
184
185protected:
186 friend class Hd_SampledDataSourceDefaultValueAccessor;
187
188 HD_API
189 virtual VtValue _GetDefaultValue();
190};
191
192HD_DECLARE_DATASOURCE_HANDLES(HdSampledDataSource);
193
198template <typename T>
200{
201public:
202 HD_DECLARE_DATASOURCE_ABSTRACT(HdTypedSampledDataSource<T>);
203 using Type = T;
204
207 virtual T GetTypedValue(Time shutterOffset) = 0;
208
209protected:
210 virtual VtValue _GetDefaultValue() override {
211 if constexpr (VtIsKnownValueType<T>()) {
212 return VtValue(T());
213 } else {
214 return VtValue();
215 }
216 }
217};
218
227{
228public:
229 HD_DECLARE_DATASOURCE(HdBlockDataSource);
230
232};
233
234HD_DECLARE_DATASOURCE_HANDLES(HdBlockDataSource);
235
236// Utilities //////////////////////////////////////////////////////////////////
237
238// Helper to let HdVisitSampledDataSourceType access a source's value type.
239class Hd_SampledDataSourceDefaultValueAccessor
240{
241public:
242 static VtValue
243 GetDefaultValue(const HdSampledDataSourceHandle &dataSource) {
244 return dataSource ? dataSource->_GetDefaultValue() : VtValue {};
245 }
246};
247
259template <
260 template <class T, class ...> class Visitor,
261 typename ...TypeArgs,
262 typename ...FnArgs
263 >
264auto
265HdVisitSampledDataSourceType(
266 const HdSampledDataSourceHandle& dataSource, FnArgs&&... args)
267{
268 return VtVisitValueType<Visitor, TypeArgs...>(
269 Hd_SampledDataSourceDefaultValueAccessor::GetDefaultValue(dataSource),
270 std::forward<FnArgs>(args)...);
271}
272
279template <
280 template <class T, template <class...> class, class ...> class Visitor,
281 template <class...> class Tmpl,
282 typename ...TypeArgs,
283 typename ...FnArgs
284 >
285auto
286HdVisitSampledDataSourceType(
287 const HdSampledDataSourceHandle& dataSource, FnArgs&&... args)
288{
289 return VtVisitValueType<Visitor, Tmpl, TypeArgs...>(
290 Hd_SampledDataSourceDefaultValueAccessor::GetDefaultValue(dataSource),
291 std::forward<FnArgs>(args)...);
292}
293
298template <
299 typename T,
300 template <typename...> class DataSource,
301 class UntypedDataSource>
302struct Hd_CopySampledDataSourceTypeVisitor
303{
304 template <class ...Args>
305 static HdDataSourceBaseHandle Visit(Args&&... args)
306 {
307 if constexpr (std::is_same_v<T, VtValue>) {
308 if constexpr (std::is_void_v<UntypedDataSource>) {
309 return nullptr;
310 }
311 else {
312 return UntypedDataSource::New(std::forward<Args>(args)...);
313 }
314 }
315 else {
316 return DataSource<T>::New(std::forward<Args>(args)...);
317 }
318 }
319};
320
325template <
326 template <typename...> class DataSource,
327 class UntypedDataSource = void,
328 typename... Args>
329HdDataSourceBaseHandle
330HdCopySampledDataSourceType(
331 const HdSampledDataSourceHandle& dataSource, Args&&... args)
332{
333 return HdVisitSampledDataSourceType<
334 Hd_CopySampledDataSourceTypeVisitor, DataSource, UntypedDataSource>(
335 dataSource, std::forward<Args>(args)...);
336}
337
339HD_API
340bool
341HdGetMergedContributingSampleTimesForInterval(
342 size_t count,
343 const HdSampledDataSourceHandle *inputDataSources,
344 HdSampledDataSource::Time startTime,
345 HdSampledDataSource::Time endTime,
346 std::vector<HdSampledDataSource::Time> * outSampleTimes);
347
349HD_API
350void
351HdDebugPrintDataSource(
352 std::ostream &,
353 HdDataSourceBaseHandle,
354 int indentLevel = 0);
355
357HD_API
358void
359HdDebugPrintDataSource(HdDataSourceBaseHandle, int indentLevel = 0);
360
361PXR_NAMESPACE_CLOSE_SCOPE
362
363#endif // PXR_IMAGING_HD_DATASOURCE_H
A datasource representing the absence of a datasource.
Definition: dataSource.h:227
A datasource representing structured (named, hierarchical) data, for example a geometric primitive or...
Definition: dataSource.h:100
virtual TfTokenVector GetNames()=0
Returns the list of names for which Get(...) is expected to return a non-null value.
virtual HdDataSourceBaseHandle Get(const TfToken &name)=0
Returns the child datasource of the given name.
static HD_API HdDataSourceBaseHandle Get(const Handle &container, const HdDataSourceLocator &locator)
A convenience function: given container, return the descendant identified by locator,...
Represents an object which can produce scene data.
Definition: dataSource.h:82
Represents an object that can identify the location of a data source.
A datasource representing time-sampled values.
Definition: dataSource.h:154
virtual bool GetContributingSampleTimesForInterval(Time startTime, Time endTime, std::vector< Time > *outSampleTimes)=0
Given a shutter window of interest (startTime and endTime relative to the current frame),...
virtual VtValue GetValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset.
A datasource representing a concretely-typed sampled value.
Definition: dataSource.h:200
virtual T GetTypedValue(Time shutterOffset)=0
Returns the value of this data source at frame-relative time shutterOffset, as type T.
A datasource representing indexed data.
Definition: dataSource.h:132
virtual size_t GetNumElements()=0
Return the number of elements in this datasource.
virtual HdDataSourceBaseHandle GetElement(size_t element)=0
Return the element at position element in this datasource.
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
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440