Loading...
Searching...
No Matches
retainedDataSource.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_RETAINEDDATASOURCE_H
8#define PXR_IMAGING_HD_RETAINEDDATASOURCE_H
9
10#include "pxr/base/vt/value.h"
11#include "pxr/pxr.h"
12
13#include "pxr/imaging/hd/api.h"
14#include "pxr/imaging/hd/dataSource.h"
15
16#include "pxr/usd/sdf/pathExpression.h"
17
18#include "pxr/base/arch/align.h"
21
22#include <utility>
23#include <vector>
24
25PXR_NAMESPACE_OPEN_SCOPE
26
36{
37public:
38 HD_DECLARE_DATASOURCE_ABSTRACT(HdRetainedContainerDataSource);
39
40 HD_API
41 static Handle New();
42
43 HD_API
44 static Handle New(
45 size_t count,
46 const TfToken *names,
47 const HdDataSourceBaseHandle *values);
48
49 HD_API
50 static Handle New(
51 const TfToken &name1,
52 const HdDataSourceBaseHandle &value1);
53
54 HD_API
55 static Handle New(
56 const TfToken &name1,
57 const HdDataSourceBaseHandle &value1,
58 const TfToken &name2,
59 const HdDataSourceBaseHandle &value2);
60
61 HD_API
62 static Handle New(
63 const TfToken &name1,
64 const HdDataSourceBaseHandle &value1,
65 const TfToken &name2,
66 const HdDataSourceBaseHandle &value2,
67 const TfToken &name3,
68 const HdDataSourceBaseHandle &value3);
69
70 HD_API
71 static Handle New(
72 const TfToken &name1,
73 const HdDataSourceBaseHandle &value1,
74 const TfToken &name2,
75 const HdDataSourceBaseHandle &value2,
76 const TfToken &name3,
77 const HdDataSourceBaseHandle &value3,
78 const TfToken &name4,
79 const HdDataSourceBaseHandle &value4);
80
81 HD_API
82 static Handle New(
83 const TfToken &name1,
84 const HdDataSourceBaseHandle &value1,
85 const TfToken &name2,
86 const HdDataSourceBaseHandle &value2,
87 const TfToken &name3,
88 const HdDataSourceBaseHandle &value3,
89 const TfToken &name4,
90 const HdDataSourceBaseHandle &value4,
91 const TfToken &name5,
92 const HdDataSourceBaseHandle &value5);
93
94 HD_API
95 static Handle New(
96 const TfToken &name1,
97 const HdDataSourceBaseHandle &value1,
98 const TfToken &name2,
99 const HdDataSourceBaseHandle &value2,
100 const TfToken &name3,
101 const HdDataSourceBaseHandle &value3,
102 const TfToken &name4,
103 const HdDataSourceBaseHandle &value4,
104 const TfToken &name5,
105 const HdDataSourceBaseHandle &value5,
106 const TfToken &name6,
107 const HdDataSourceBaseHandle &value6);
108};
109
110HD_DECLARE_DATASOURCE_HANDLES(HdRetainedContainerDataSource);
111
112//-----------------------------------------------------------------------------
113
120{
121public:
122 HD_DECLARE_DATASOURCE(HdRetainedSampledDataSource);
123
125 HdSampledDataSource::Time startTime,
126 HdSampledDataSource::Time endTime,
127 std::vector<HdSampledDataSource::Time> *outSampleTimes) override
128 {
129 return false;
130 }
131
132 VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
133 {
134 return _value;
135 }
136
137protected:
139 : _value(std::move(value))
140 { }
141
142 VtValue _value;
143};
144
145HD_DECLARE_DATASOURCE_HANDLES(HdRetainedSampledDataSource);
146
147//-----------------------------------------------------------------------------
148
154template <typename T>
156{
157public:
158 HD_DECLARE_DATASOURCE(HdRetainedTypedSampledDataSource<T>);
159
161 HdSampledDataSource::Time startTime,
162 HdSampledDataSource::Time endTime,
163 std::vector<HdSampledDataSource::Time> *outSampleTimes) override
164 {
165 return false;
166 }
167
168 VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
169 {
170 return VtValue(_value);
171 }
172
173 T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
174 {
175 return _value;
176 }
177
178 // This New() overload exists to support braced init lists for T passed
179 // directly to New(), which the standard variadic New() from
180 // HD_DECLARE_DATASOURCE cannot handle.
181 static
183 New(const T &value)
184 {
185 return std::allocate_shared<HdRetainedTypedSampledDataSource<T>>(
187 value);
188 }
189
190protected:
192 : _value(value) {}
193
194 T _value;
195};
196
197// Specialization for bool that will let us use singletons. The specialization
198// must happen at the class level so we can use alignas to separate the
199// singletons from their control blocks. Keeping them out of the same cache
200// line prevents false-sharing, which we actually observed with the prior,
201// function-level specialization pattern. This class-level specialization
202// pattern is the recommended way to create type-specific specializations
203// of HdRetainedTypedSampledDataSource that use singletons for efficiency.
204
205template <>
206class alignas(ARCH_CACHE_LINE_SIZE) HdRetainedTypedSampledDataSource<bool>
207 : public HdTypedSampledDataSource<bool>
208{
209public:
210 HD_DECLARE_DATASOURCE_ABSTRACT(HdRetainedTypedSampledDataSource<bool>)
211 _HD_ALLOCATOR_FRIEND
212
214 HdSampledDataSource::Time,
215 HdSampledDataSource::Time,
216 std::vector<HdSampledDataSource::Time>*) override
217 {
218 return false;
219 }
220
221 VtValue GetValue(HdSampledDataSource::Time) override
222 {
223 return VtValue(_value);
224 }
225
226 bool GetTypedValue(HdSampledDataSource::Time) override
227 {
228 return _value;
229 }
230
231 HD_API
232 static Handle New(const bool& value);
233
234protected:
236 : _value(value) { }
237
238 bool _value;
239};
240
241//-----------------------------------------------------------------------------
242
248template <typename T>
249class HdRetainedTypedMultisampledDataSource : public HdTypedSampledDataSource<T>
250{
251public:
252 HD_DECLARE_DATASOURCE(HdRetainedTypedMultisampledDataSource<T>);
253
254 HdRetainedTypedMultisampledDataSource(
255 size_t count,
256 HdSampledDataSource::Time *sampleTimes,
257 T *sampleValues);
258
259 bool GetContributingSampleTimesForInterval(
260 HdSampledDataSource::Time startTime,
261 HdSampledDataSource::Time endTime,
262 std::vector<HdSampledDataSource::Time> *outSampleTimes) override;
263
264 VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
265 {
266 return VtValue(GetTypedValue(shutterOffset));
267 }
268
269 T GetTypedValue(HdSampledDataSource::Time shutterOffset) override;
270
271private:
272 typedef std::pair<HdSampledDataSource::Time, T> _SamplePair;
273 TfSmallVector<_SamplePair, 6> _sampledValues;
274};
275
276template <typename T>
277HdRetainedTypedMultisampledDataSource<T>::HdRetainedTypedMultisampledDataSource(
278 size_t count,
279 HdSampledDataSource::Time *sampleTimes,
280 T *sampleValues)
281{
282 _sampledValues.reserve(count);
283
284 // XXX: For now, assume that sample times are ordered.
285 // We could sort them if needed.
286 for (size_t i = 0; i < count; ++i) {
287 _sampledValues.emplace_back(sampleTimes[i], sampleValues[i]);
288 }
289}
290
291template <typename T>
292bool
293HdRetainedTypedMultisampledDataSource<T>::GetContributingSampleTimesForInterval(
294 HdSampledDataSource::Time startTime,
295 HdSampledDataSource::Time endTime,
296 std::vector<HdSampledDataSource::Time> *outSampleTimes)
297{
298 if (_sampledValues.size() < 2) {
299 return false;
300 }
301
302 if (outSampleTimes) {
303 outSampleTimes->clear();
304
305 // XXX: Include all stored samples for now.
306 outSampleTimes->reserve(_sampledValues.size());
307
308 for (const auto & iter : _sampledValues) {
309 outSampleTimes->push_back(iter.first);
310 }
311 }
312
313 return true;
314}
315
316template <typename T>
317T
318HdRetainedTypedMultisampledDataSource<T>::GetTypedValue(
319 HdSampledDataSource::Time shutterOffset)
320{
321 if (_sampledValues.empty()) {
322 return T();
323 }
324
325 const HdSampledDataSource::Time epsilon = 0.0001;
326
327 for (size_t i = 0, e = _sampledValues.size(); i < e; ++i) {
328
329 const HdSampledDataSource::Time & sampleTime = _sampledValues[i].first;
330
331 if (sampleTime > shutterOffset) {
332
333 // If we're first and we're already bigger, return us.
334 if (i < 1) {
335 return _sampledValues[i].second;
336 } else {
337
338 // This will always be positive
339 const HdSampledDataSource::Time delta =
340 sampleTime - shutterOffset;
341
342 // If we're kinda equal, go for it
343 if (delta < epsilon) {
344 return _sampledValues[i].second;
345 }
346
347 // Since we're already over the requested time, let's see
348 // if it's closer, use it instead of me. In the case of a
349 // tie, use the earlier.
350 const HdSampledDataSource::Time previousDelta =
351 shutterOffset - _sampledValues[i - 1].first;
352
353 if (previousDelta <= delta) {
354 return _sampledValues[i - 1].second;
355 } else {
356 return _sampledValues[i].second;
357 }
358 }
359 } else {
360 if (fabs(sampleTime - shutterOffset) < epsilon) {
361 return _sampledValues[i].second;
362 }
363 }
364 }
365
366 // Never were in range, return our last sample
367 return _sampledValues.back().second;
368}
369
370//-----------------------------------------------------------------------------
371
380{
381public:
382 HD_DECLARE_DATASOURCE(HdRetainedSmallVectorDataSource);
383
384 HD_API
385 size_t GetNumElements() override;
386
387 HD_API
388 HdDataSourceBaseHandle GetElement(size_t element) override;
389
390protected:
391 HD_API
393 size_t count,
394 const HdDataSourceBaseHandle *values);
395
396private:
398};
399
400HD_DECLARE_DATASOURCE_HANDLES(HdRetainedSmallVectorDataSource);
401
402// Utilities //////////////////////////////////////////////////////////////////
403
406HD_API
407HdSampledDataSourceHandle
408HdCreateTypedRetainedDataSource(VtValue const &v);
409
412HD_API
413HdDataSourceBaseHandle
414HdMakeStaticCopy(HdDataSourceBaseHandle const &ds);
415
418HD_API
419HdContainerDataSourceHandle
420HdMakeStaticCopy(HdContainerDataSourceHandle const &ds);
421
422PXR_NAMESPACE_CLOSE_SCOPE
423
424#endif // PXR_IMAGING_HD_RETAINEDDATASOURCE_H
Provide architecture-specific memory-alignment information.
A datasource representing structured (named, hierarchical) data, for example a geometric primitive or...
Definition dataSource.h:148
A retained container data source is a data source whose data are available locally,...
A retained data source for sampled data.
bool GetContributingSampleTimesForInterval(HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes) override
Given a shutter window of interest (startTime and endTime relative to the current frame),...
VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
Returns the value of this data source at frame-relative time shutterOffset.
A retained data source version of HdVectorDataSource.
HD_API HdDataSourceBaseHandle GetElement(size_t element) override
Return the element at position element in this datasource.
HD_API size_t GetNumElements() override
Return the number of elements in this datasource.
Similar to HdRetainedSampledDataSource but provides strongly typed semantics.
bool GetContributingSampleTimesForInterval(HdSampledDataSource::Time startTime, HdSampledDataSource::Time endTime, std::vector< HdSampledDataSource::Time > *outSampleTimes) override
Given a shutter window of interest (startTime and endTime relative to the current frame),...
VtValue GetValue(HdSampledDataSource::Time shutterOffset) override
Returns the value of this data source at frame-relative time shutterOffset.
T GetTypedValue(HdSampledDataSource::Time shutterOffset) override
Returns the value of this data source at frame-relative time shutterOffset, as type T.
A datasource representing time-sampled values.
Definition dataSource.h:202
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:248
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:180
This is a small-vector class with local storage optimization, the local storage can be specified via ...
void reserve(size_type newCapacity)
Reserve storage for newCapacity entries.
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
STL namespace.
HdDataSourceAllocator.
Definition dataSource.h:33