HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dataSource.h
Go to the documentation of this file.
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"
14 
15 #include "pxr/base/tf/token.h"
16 #include "pxr/base/vt/value.h"
17 
18 #include <iosfwd>
19 #include <memory>
20 #include <vector>
21 #include <atomic>
22 
24 
25 /// HD_DECLARE_DATASOURCE_ABSTRACT
26 /// Used for non-instantiable classes, this defines a set of functions
27 /// for manipulating handles to this type of datasource.
28 #define HD_DECLARE_DATASOURCE_ABSTRACT(type) \
29  using Handle = std::shared_ptr<type>; \
30  using AtomicHandle = Handle; \
31  static Handle AtomicLoad(AtomicHandle &ptr) { \
32  return std::atomic_load(&ptr); \
33  } \
34  static void AtomicStore(AtomicHandle &ptr, const Handle &v) { \
35  std::atomic_store(&ptr, v); \
36  } \
37  static bool AtomicCompareExchange(AtomicHandle &ptr, \
38  AtomicHandle &expected, \
39  const Handle &desired) { \
40  return std::atomic_compare_exchange_strong(&ptr, &expected, desired); \
41  } \
42  static Handle Cast(const HdDataSourceBase::Handle &v) { \
43  return std::dynamic_pointer_cast<type>(v); \
44  }
45 
46 /// HD_DECLARE_DATASOURCE
47 /// Used for instantiable classes, this defines functions for manipulating
48 /// and allocating handles to this type of datasource.
49 ///
50 /// Use of this macro in derived classes is important to make sure that
51 /// core and client code share the same handle type and allocator.
52 #define HD_DECLARE_DATASOURCE(type) \
53  HD_DECLARE_DATASOURCE_ABSTRACT(type) \
54  template <typename ... Args> \
55  static Handle New(Args&& ... args) { \
56  return Handle(new type(std::forward<Args>(args) ... )); \
57  }
58 
59 /// HD_DECLARE_DATASOURCE_INITIALIZER_LIST_NEW
60 /// Used for declaring a `New` function for datasource types that have a
61 /// constructor that takes an initializer_list<T>.
62 #define HD_DECLARE_DATASOURCE_INITIALIZER_LIST_NEW(type, T) \
63  static Handle New(std::initializer_list<T> initList) { \
64  return Handle(new type(initList)); \
65  }
66 
67 #define HD_DECLARE_DATASOURCE_HANDLES(type) \
68  using type##Handle = type::Handle; \
69  using type##AtomicHandle = type::AtomicHandle;
70 
71 /// \class HdDataSourceBase
72 ///
73 /// Represents an object which can produce scene data.
74 /// \sa HdContainerDataSource HdVectorDataSource HdSampledDataSource
75 /// Note that most derived classes will have standard API for allocation
76 /// and handle manipulation. Derived classes that don't support instantiation
77 /// should use HD_DECLARE_DATASOURCE_ABSTRACT, which omits the
78 /// definition of ::New().
79 ///
81 {
82 public:
84 
85  HD_API
86  virtual ~HdDataSourceBase() = 0;
87 };
88 
90 
91 /// \class HdContainerDataSource
92 ///
93 /// A datasource representing structured (named, hierarchical) data, for
94 /// example a geometric primitive or a sub-object like a material definition.
95 /// Note that implementations are responsible for providing cache invalidation,
96 /// if necessary.
97 ///
99 {
100 public:
102 
103  /// Returns the list of names for which \p Get(...) is expected to return
104  /// a non-null value. This call is expected to be threadsafe.
105  virtual TfTokenVector GetNames() = 0;
106 
107  /// Returns the child datasource of the given name. This call is expected
108  /// to be threadsafe.
109  virtual HdDataSourceBaseHandle Get(const TfToken &name) = 0;
110 
111  /// A convenience function: given \p container, return the descendant
112  /// identified by \p locator, which may be at any depth. Returns
113  /// \p container itself on an empty locator, or null if \p locator doesn't
114  /// identify a valid descendant.
115  HD_API
116  static HdDataSourceBaseHandle Get(
117  const Handle &container,
118  const HdDataSourceLocator &locator);
119 };
120 
122 
123 /// \class HdVectorDataSource
124 ///
125 /// A datasource representing indexed data. This should be used when a scene
126 /// index is expected to manipulate the indexing; for array-valued data, a
127 /// \p HdSampledDataSource can be used instead. Note that implementations are
128 /// responsible for providing cache invalidation, if necessary.
129 ///
131 {
132 public:
134 
135  /// Return the number of elements in this datasource. This call is
136  /// expected to be threadsafe.
137  virtual size_t GetNumElements() = 0;
138 
139  /// Return the element at position \p element in this datasource. This
140  /// is expected to return non-null for the range [0, \p numElements).
141  /// This call is expected to be threadsafe.
142  virtual HdDataSourceBaseHandle GetElement(size_t element) = 0;
143 };
144 
146 
147 /// \class HdSampledDataSource
148 ///
149 /// A datasource representing time-sampled values. Note that implementations
150 /// are responsible for providing cache invalidation, if necessary.
151 ///
153 {
154 public:
156  using Time = float;
157 
158  /// Returns the value of this data source at frame-relative time
159  /// \p shutterOffset. The caller does not track the frame; the scene
160  /// index producing this datasource is responsible for that, if applicable.
161  /// Note that, although this call returns a VtValue for each shutter
162  /// offset, the type of the held value is expected to be the same across
163  /// all shutter offsets. This call is expected to be threadsafe.
164  virtual VtValue GetValue(Time shutterOffset) = 0;
165 
166  /// Given a shutter window of interest (\p startTime and \p endTime
167  /// relative to the current frame), return a list of sample times for the
168  /// caller to query with GetValue such that the caller can reconstruct the
169  /// signal over the shutter window. For a sample-based attribute, this
170  /// might be a list of times when samples are defined. For a procedural
171  /// scene, this might be a generated distribution. Note that the returned
172  /// samples don't need to be within \p startTime and \p endTime; if
173  /// a boundary sample is outside of the window, implementers can return it,
174  /// and callers should expect it and interpolate to \p startTime or
175  /// \p endTime accordingly. If this call returns \p true, the caller is
176  /// expected to pass the list of \p outSampleTimes to \p GetValue. If this
177  /// call returns \p false, this value is uniform across the shutter window
178  /// and the caller should call \p GetValue(0) to get that uniform value.
180  Time startTime,
181  Time endTime,
182  std::vector<Time> * outSampleTimes) = 0;
183 };
184 
186 
187 /// \class HdTypedSampledDataSource
188 ///
189 /// A datasource representing a concretely-typed sampled value.
190 ///
191 template <typename T>
193 {
194 public:
196  using Type = T;
197 
198  /// Returns the value of this data source at frame-relative time
199  /// \p shutterOffset, as type \p T.
200  virtual T GetTypedValue(Time shutterOffset) = 0;
201 };
202 
203 
204 /// \class HdBlockDataSource
205 ///
206 /// A datasource representing the absence of a datasource. If a container has
207 /// a child datasource which is a block datasource, that's equivalent to that
208 /// child being null. This type is useful when composing containers, where a
209 /// block might shadow sampled data, and sampled data might shadow nullptr.
210 ///
212 {
213 public:
215 
217 };
218 
220 
221 // Utilities //////////////////////////////////////////////////////////////////
222 
223 /// Merges contributing sample times from several data sources.
224 HD_API
225 bool
227  size_t count,
228  const HdSampledDataSourceHandle *inputDataSources,
229  HdSampledDataSource::Time startTime,
231  std::vector<HdSampledDataSource::Time> * outSampleTimes);
232 
233 /// Print a datasource to a stream, for debugging/testing.
234 HD_API
235 void
237  std::ostream &,
238  HdDataSourceBaseHandle,
239  int indentLevel = 0);
240 
241 /// Print a datasource to stdout, for debugging/testing
242 HD_API
243 void
244 HdDebugPrintDataSource(HdDataSourceBaseHandle, int indentLevel = 0);
245 
247 
248 #endif // PXR_IMAGING_HD_DATASOURCE_H
virtual HdDataSourceBaseHandle GetElement(size_t element)=0
HD_DECLARE_DATASOURCE_ABSTRACT(HdContainerDataSource)
#define HD_API
Definition: api.h:23
virtual TfTokenVector GetNames()=0
virtual HdDataSourceBaseHandle Get(const TfToken &name)=0
HD_DECLARE_DATASOURCE_ABSTRACT(HdTypedSampledDataSource< T >)
Definition: token.h:70
virtual size_t GetNumElements()=0
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:440
#define HD_DECLARE_DATASOURCE_HANDLES(type)
Definition: dataSource.h:67
GLuint const GLchar * name
Definition: glcorearb.h:786
HD_DECLARE_DATASOURCE_ABSTRACT(HdSampledDataSource)
HD_API bool HdGetMergedContributingSampleTimesForInterval(size_t count, const HdSampledDataSourceHandle *inputDataSources, HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes)
Merges contributing sample times from several data sources.
virtual VtValue GetValue(Time shutterOffset)=0
IMATH_NAMESPACE::V2f IMATH_NAMESPACE::Box2i std::string this attribute is obsolete as of OpenEXR v3 float
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
HD_DECLARE_DATASOURCE_ABSTRACT(HdVectorDataSource)
#define HD_DECLARE_DATASOURCE_ABSTRACT(type)
Definition: dataSource.h:28
virtual T GetTypedValue(Time shutterOffset)=0
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
virtual HD_API ~HdDataSourceBase()=0
virtual bool GetContributingSampleTimesForInterval(Time startTime, Time endTime, std::vector< Time > *outSampleTimes)=0
HD_API void HdDebugPrintDataSource(std::ostream &, HdDataSourceBaseHandle, int indentLevel=0)
Print a datasource to a stream, for debugging/testing.
HD_DECLARE_DATASOURCE(HdBlockDataSource)
Definition: value.h:146
GLint GLsizei count
Definition: glcorearb.h:405
void * Handle
Definition: plugin.h:27