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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_IMAGING_HD_DATASOURCE_H
25 #define PXR_IMAGING_HD_DATASOURCE_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/imaging/hd/api.h"
31 
32 #include "pxr/base/tf/token.h"
33 #include "pxr/base/vt/value.h"
34 
35 #include <iosfwd>
36 #include <memory>
37 #include <vector>
38 #include <atomic>
39 
41 
42 /// HD_DECLARE_DATASOURCE_ABSTRACT
43 /// Used for non-instantiable classes, this defines a set of functions
44 /// for manipulating handles to this type of datasource.
45 #define HD_DECLARE_DATASOURCE_ABSTRACT(type) \
46  using Handle = std::shared_ptr<type>; \
47  using AtomicHandle = Handle; \
48  static Handle AtomicLoad(AtomicHandle &ptr) { \
49  return std::atomic_load(&ptr); \
50  } \
51  static void AtomicStore(AtomicHandle &ptr, const Handle &v) { \
52  std::atomic_store(&ptr, v); \
53  } \
54  static bool AtomicCompareExchange(AtomicHandle &ptr, \
55  AtomicHandle &expected, \
56  const Handle &desired) { \
57  return std::atomic_compare_exchange_strong(&ptr, &expected, desired); \
58  } \
59  static Handle Cast(const HdDataSourceBase::Handle &v) { \
60  return std::dynamic_pointer_cast<type>(v); \
61  }
62 
63 /// HD_DECLARE_DATASOURCE
64 /// Used for instantiable classes, this defines functions for manipulating
65 /// and allocating handles to this type of datasource.
66 ///
67 /// Use of this macro in derived classes is important to make sure that
68 /// core and client code share the same handle type and allocator.
69 #define HD_DECLARE_DATASOURCE(type) \
70  HD_DECLARE_DATASOURCE_ABSTRACT(type) \
71  template <typename ... Args> \
72  static Handle New(Args&& ... args) { \
73  return Handle(new type(std::forward<Args>(args) ... )); \
74  }
75 
76 /// HD_DECLARE_DATASOURCE_INITIALIZER_LIST_NEW
77 /// Used for declaring a `New` function for datasource types that have a
78 /// constructor that takes an initializer_list<T>.
79 #define HD_DECLARE_DATASOURCE_INITIALIZER_LIST_NEW(type, T) \
80  static Handle New(std::initializer_list<T> initList) { \
81  return Handle(new type(initList)); \
82  }
83 
84 #define HD_DECLARE_DATASOURCE_HANDLES(type) \
85  using type##Handle = type::Handle; \
86  using type##AtomicHandle = type::AtomicHandle;
87 
88 /// \class HdDataSourceBase
89 ///
90 /// Represents an object which can produce scene data.
91 /// \sa HdContainerDataSource HdVectorDataSource HdSampledDataSource
92 /// Note that most derived classes will have standard API for allocation
93 /// and handle manipulation. Derived classes that don't support instantiation
94 /// should use HD_DECLARE_DATASOURCE_ABSTRACT, which omits the
95 /// definition of ::New().
96 ///
98 {
99 public:
101 
102  HD_API
103  virtual ~HdDataSourceBase() = 0;
104 };
105 
107 
108 /// \class HdContainerDataSource
109 ///
110 /// A datasource representing structured (named, hierarchical) data, for
111 /// example a geometric primitive or a sub-object like a material definition.
112 /// Note that implementations are responsible for providing cache invalidation,
113 /// if necessary.
114 ///
116 {
117 public:
119 
120  /// Returns the list of names for which \p Get(...) is expected to return
121  /// a non-null value. This call is expected to be threadsafe.
122  virtual TfTokenVector GetNames() = 0;
123 
124  /// Returns the child datasource of the given name. This call is expected
125  /// to be threadsafe.
126  virtual HdDataSourceBaseHandle Get(const TfToken &name) = 0;
127 
128  /// A convenience function: given \p container, return the descendant
129  /// identified by \p locator, which may be at any depth. Returns
130  /// \p container itself on an empty locator, or null if \p locator doesn't
131  /// identify a valid descendant.
132  HD_API
133  static HdDataSourceBaseHandle Get(
134  const Handle &container,
135  const HdDataSourceLocator &locator);
136 };
137 
139 
140 /// \class HdVectorDataSource
141 ///
142 /// A datasource representing indexed data. This should be used when a scene
143 /// index is expected to manipulate the indexing; for array-valued data, a
144 /// \p HdSampledDataSource can be used instead. Note that implementations are
145 /// responsible for providing cache invalidation, if necessary.
146 ///
148 {
149 public:
151 
152  /// Return the number of elements in this datasource. This call is
153  /// expected to be threadsafe.
154  virtual size_t GetNumElements() = 0;
155 
156  /// Return the element at position \p element in this datasource. This
157  /// is expected to return non-null for the range [0, \p numElements).
158  /// This call is expected to be threadsafe.
159  virtual HdDataSourceBaseHandle GetElement(size_t element) = 0;
160 };
161 
163 
164 /// \class HdSampledDataSource
165 ///
166 /// A datasource representing time-sampled values. Note that implementations
167 /// are responsible for providing cache invalidation, if necessary.
168 ///
170 {
171 public:
173  using Time = float;
174 
175  /// Returns the value of this data source at frame-relative time
176  /// \p shutterOffset. The caller does not track the frame; the scene
177  /// index producing this datasource is responsible for that, if applicable.
178  /// Note that, although this call returns a VtValue for each shutter
179  /// offset, the type of the held value is expected to be the same across
180  /// all shutter offsets. This call is expected to be threadsafe.
181  virtual VtValue GetValue(Time shutterOffset) = 0;
182 
183  /// Given a shutter window of interest (\p startTime and \p endTime
184  /// relative to the current frame), return a list of sample times for the
185  /// caller to query with GetValue such that the caller can reconstruct the
186  /// signal over the shutter window. For a sample-based attribute, this
187  /// might be a list of times when samples are defined. For a procedural
188  /// scene, this might be a generated distribution. Note that the returned
189  /// samples don't need to be within \p startTime and \p endTime; if
190  /// a boundary sample is outside of the window, implementers can return it,
191  /// and callers should expect it and interpolate to \p startTime or
192  /// \p endTime accordingly. If this call returns \p true, the caller is
193  /// expected to pass the list of \p outSampleTimes to \p GetValue. If this
194  /// call returns \p false, this value is uniform across the shutter window
195  /// and the caller should call \p GetValue(0) to get that uniform value.
197  Time startTime,
198  Time endTime,
199  std::vector<Time> * outSampleTimes) = 0;
200 };
201 
203 
204 /// \class HdTypedSampledDataSource
205 ///
206 /// A datasource representing a concretely-typed sampled value.
207 ///
208 template <typename T>
210 {
211 public:
213  using Type = T;
214 
215  /// Returns the value of this data source at frame-relative time
216  /// \p shutterOffset, as type \p T.
217  virtual T GetTypedValue(Time shutterOffset) = 0;
218 };
219 
220 
221 /// \class HdBlockDataSource
222 ///
223 /// A datasource representing the absence of a datasource. If a container has
224 /// a child datasource which is a block datasource, that's equivalent to that
225 /// child being null. This type is useful when composing containers, where a
226 /// block might shadow sampled data, and sampled data might shadow nullptr.
227 ///
229 {
230 public:
232 
234 };
235 
237 
238 // Utilities //////////////////////////////////////////////////////////////////
239 
240 /// Merges contributing sample times from several data sources.
241 HD_API
242 bool
244  size_t count,
245  const HdSampledDataSourceHandle *inputDataSources,
246  HdSampledDataSource::Time startTime,
248  std::vector<HdSampledDataSource::Time> * outSampleTimes);
249 
250 /// Print a datasource to a stream, for debugging/testing.
251 HD_API
252 void
254  std::ostream &,
255  HdDataSourceBaseHandle,
256  int indentLevel = 0);
257 
258 /// Print a datasource to stdout, for debugging/testing
259 HD_API
260 void
261 HdDebugPrintDataSource(HdDataSourceBaseHandle, int indentLevel = 0);
262 
264 
265 #endif // PXR_IMAGING_HD_DATASOURCE_H
virtual HdDataSourceBaseHandle GetElement(size_t element)=0
HD_DECLARE_DATASOURCE_ABSTRACT(HdContainerDataSource)
#define HD_API
Definition: api.h:40
virtual TfTokenVector GetNames()=0
virtual HdDataSourceBaseHandle Get(const TfToken &name)=0
HD_DECLARE_DATASOURCE_ABSTRACT(HdTypedSampledDataSource< T >)
Definition: token.h:87
IMATH_NAMESPACE::V2f float
virtual size_t GetNumElements()=0
std::vector< TfToken > TfTokenVector
Convenience types.
Definition: token.h:457
#define HD_DECLARE_DATASOURCE_HANDLES(type)
Definition: dataSource.h:84
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
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
HD_DECLARE_DATASOURCE_ABSTRACT(HdVectorDataSource)
#define HD_DECLARE_DATASOURCE_ABSTRACT(type)
Definition: dataSource.h:45
virtual T GetTypedValue(Time shutterOffset)=0
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
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:164
GLint GLsizei count
Definition: glcorearb.h:405
void * Handle
Definition: plugin.h:27