HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
retainedDataSource.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_RETAINEDDATASOURCE_H
25 #define PXR_IMAGING_HD_RETAINEDDATASOURCE_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/imaging/hd/api.h"
31 
34 
36 
37 /// \class HdRetainedContainerDataSource
38 ///
39 /// A retained container data source is a data source whose data are available
40 /// locally, meaning that they are fully stored and contained within the data
41 /// source. These are typically used for the kinds of operations that need to
42 /// break away from the more live data sources (e.g., those that query a
43 /// backing scene).
44 ///
46 {
47 public:
49 
50  HD_API
51  static Handle New();
52 
53  HD_API
54  static Handle New(
55  size_t count,
56  const TfToken *names,
57  const HdDataSourceBaseHandle *values);
58 
59  HD_API
60  static Handle New(
61  const TfToken &name1,
62  const HdDataSourceBaseHandle &value1);
63 
64  HD_API
65  static Handle New(
66  const TfToken &name1,
67  const HdDataSourceBaseHandle &value1,
68  const TfToken &name2,
69  const HdDataSourceBaseHandle &value2);
70 
71  HD_API
72  static Handle New(
73  const TfToken &name1,
74  const HdDataSourceBaseHandle &value1,
75  const TfToken &name2,
76  const HdDataSourceBaseHandle &value2,
77  const TfToken &name3,
78  const HdDataSourceBaseHandle &value3);
79 
80  HD_API
81  static Handle New(
82  const TfToken &name1,
83  const HdDataSourceBaseHandle &value1,
84  const TfToken &name2,
85  const HdDataSourceBaseHandle &value2,
86  const TfToken &name3,
87  const HdDataSourceBaseHandle &value3,
88  const TfToken &name4,
89  const HdDataSourceBaseHandle &value4);
90 
91  HD_API
92  static Handle New(
93  const TfToken &name1,
94  const HdDataSourceBaseHandle &value1,
95  const TfToken &name2,
96  const HdDataSourceBaseHandle &value2,
97  const TfToken &name3,
98  const HdDataSourceBaseHandle &value3,
99  const TfToken &name4,
100  const HdDataSourceBaseHandle &value4,
101  const TfToken &name5,
102  const HdDataSourceBaseHandle &value5);
103 
104  HD_API
105  static Handle New(
106  const TfToken &name1,
107  const HdDataSourceBaseHandle &value1,
108  const TfToken &name2,
109  const HdDataSourceBaseHandle &value2,
110  const TfToken &name3,
111  const HdDataSourceBaseHandle &value3,
112  const TfToken &name4,
113  const HdDataSourceBaseHandle &value4,
114  const TfToken &name5,
115  const HdDataSourceBaseHandle &value5,
116  const TfToken &name6,
117  const HdDataSourceBaseHandle &value6);
118 };
119 
121 
122 //-----------------------------------------------------------------------------
123 
124 /// \class HdRetainedSampledDataSource
125 ///
126 /// A retained data source for sampled data. Typically used when the data needs
127 /// to be locally stored and cut off from any backing scene data.
128 ///
130 {
131 public:
133 
135  : _value(value) {}
136 
138  HdSampledDataSource::Time startTime,
140  std::vector<HdSampledDataSource::Time> *outSampleTimes) override
141  {
142  return false;
143  }
144 
146  {
147  return _value;
148  }
149 
150 private:
151  VtValue _value;
152 };
153 
155 
156 //-----------------------------------------------------------------------------
157 
158 /// \class HdRetainedTypedSampledDataSource
159 ///
160 /// Similar to HdRetainedSampledDataSource but provides strongly typed
161 /// semantics.
162 ///
163 template <typename T>
165 {
166 public:
167  //abstract to implement New outside in service of specialization
169 
171  : _value(value) {}
172 
174  HdSampledDataSource::Time startTime,
176  std::vector<HdSampledDataSource::Time> *outSampleTimes) override
177  {
178  return false;
179  }
180 
182  {
183  return VtValue(_value);
184  }
185 
186  T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
187  {
188  return _value;
189  }
190 
191  static
193 
194 protected:
196 };
197 
198 // New is specializable for cases where instances may be shared for efficiency
199 template <typename T>
202 {
205 }
206 
207 template <>
210 
211 //-----------------------------------------------------------------------------
212 
213 /// \class HdRetainedTypedMultisampleDataSource
214 ///
215 /// Similar to HdRetainedTypedSampledDataSource but is capable of holding on to
216 /// multiple samples at once.
217 ///
218 template <typename T>
220 {
221 public:
223 
225  size_t count,
226  HdSampledDataSource::Time *sampleTimes,
227  T *sampleValues);
228 
230  HdSampledDataSource::Time startTime,
232  std::vector<HdSampledDataSource::Time> *outSampleTimes) override;
233 
235  {
236  return VtValue(GetTypedValue(shutterOffset));
237  }
238 
239  T GetTypedValue(HdSampledDataSource::Time shutterOffset) override;
240 
241 private:
242  typedef std::pair<HdSampledDataSource::Time, T> _SamplePair;
243  TfSmallVector<_SamplePair, 6> _sampledValues;
244 };
245 
246 template <typename T>
248  size_t count,
249  HdSampledDataSource::Time *sampleTimes,
250  T *sampleValues)
251 {
252  _sampledValues.reserve(count);
253 
254  // XXX: For now, assume that sample times are ordered.
255  // We could sort them if needed.
256  for (size_t i = 0; i < count; ++i) {
257  _sampledValues.emplace_back(sampleTimes[i], sampleValues[i]);
258  }
259 }
260 
261 template <typename T>
262 bool
264  HdSampledDataSource::Time startTime,
266  std::vector<HdSampledDataSource::Time> *outSampleTimes)
267 {
268  if (_sampledValues.size() < 2) {
269  return false;
270  }
271 
272  if (outSampleTimes) {
273  outSampleTimes->clear();
274 
275  // XXX: Include all stored samples for now.
276  outSampleTimes->reserve(_sampledValues.size());
277 
278  for (const auto & iter : _sampledValues) {
279  outSampleTimes->push_back(iter.first);
280  }
281  }
282 
283  return true;
284 }
285 
286 template <typename T>
287 T
289  HdSampledDataSource::Time shutterOffset)
290 {
291  if (_sampledValues.empty()) {
292  return T();
293  }
294 
295  const HdSampledDataSource::Time epsilon = 0.0001;
296 
297  for (size_t i = 0, e = _sampledValues.size(); i < e; ++i) {
298 
299  const HdSampledDataSource::Time & sampleTime = _sampledValues[i].first;
300 
301  if (sampleTime > shutterOffset) {
302 
303  // If we're first and we're already bigger, return us.
304  if (i < 1) {
305  return _sampledValues[i].second;
306  } else {
307 
308  // This will always be positive
309  const HdSampledDataSource::Time delta =
310  sampleTime - shutterOffset;
311 
312  // If we're kinda equal, go for it
313  if (delta < epsilon) {
314  return _sampledValues[i].second;
315  }
316 
317  // Since we're already over the requested time, let's see
318  // if it's closer, use it instead of me. In the case of a
319  // tie, use the earlier.
320  const HdSampledDataSource::Time previousDelta =
321  shutterOffset - _sampledValues[i - 1].first;
322 
323  if (previousDelta <= delta) {
324  return _sampledValues[i - 1].second;
325  } else {
326  return _sampledValues[i].second;
327  }
328  }
329  } else {
330  if (fabs(sampleTime - shutterOffset) < epsilon) {
331  return _sampledValues[i].second;
332  }
333  }
334  }
335 
336  // Never were in range, return our last sample
337  return _sampledValues.back().second;
338 }
339 
340 //-----------------------------------------------------------------------------
341 
342 /// \class HdRetainedSmallVectorDataSource
343 ///
344 /// A retained data source version of HdVectorDataSource.
345 ///
346 /// Internally it uses a TfSmallVector with up to 32 locally stored entries
347 /// for storage.
348 ///
350 {
351 public:
353 
354  HD_API
356  size_t count,
357  const HdDataSourceBaseHandle *values);
358 
359  HD_API
360  size_t GetNumElements() override;
361 
362  HD_API
363  HdDataSourceBaseHandle GetElement(size_t element) override;
364 
365 private:
367 };
368 
370 
371 // Utilities //////////////////////////////////////////////////////////////////
372 
373 /// Given a VtValue, attempt to create a RetainedTypedSampledDataSource of
374 /// the appropriate type via type dispatch.
375 HD_API
376 HdSampledDataSourceHandle
378 
380 
381 #endif // PXR_IMAGING_HD_RETAINEDDATASOURCE_H
T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
HD_API HdRetainedSmallVectorDataSource(size_t count, const HdDataSourceBaseHandle *values)
HD_DECLARE_DATASOURCE(HdRetainedTypedMultisampledDataSource< T >)
const GLdouble * v
Definition: glcorearb.h:837
#define HD_API
Definition: api.h:40
static HD_API Handle New()
HD_DECLARE_DATASOURCE_ABSTRACT(HdRetainedContainerDataSource)
HD_API HdDataSourceBaseHandle GetElement(size_t element) override
HD_DECLARE_DATASOURCE_ABSTRACT(HdRetainedTypedSampledDataSource< T >)
Definition: token.h:87
HD_DECLARE_DATASOURCE(HdRetainedSampledDataSource)
VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
HD_DECLARE_DATASOURCE(HdRetainedSmallVectorDataSource)
bool GetContributingSampleTimesForInterval(HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes) override
static HdRetainedTypedSampledDataSource< T >::Handle New(const T &value)
HD_DECLARE_DATASOURCE_HANDLES(HdRetainedContainerDataSource)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
HD_API HdSampledDataSourceHandle HdCreateTypedRetainedDataSource(VtValue const &v)
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
HD_API size_t GetNumElements() override
HdRetainedTypedMultisampledDataSource(size_t count, HdSampledDataSource::Time *sampleTimes, T *sampleValues)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
bool GetContributingSampleTimesForInterval(HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes) override
VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
Definition: core.h:1131
bool GetContributingSampleTimesForInterval(HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes) override
HdRetainedSampledDataSource(VtValue value)
HdRetainedTypedSampledDataSource(const T &value)
T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
Definition: value.h:167
GLint GLsizei count
Definition: glcorearb.h:405
void * Handle
Definition: plugin.h:27