Loading...
Searching...
No Matches
cachingSampledDataSource.h
1//
2// Copyright 2026 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_CACHING_SAMPLED_DATASOURCE_H
8#define PXR_IMAGING_HD_CACHING_SAMPLED_DATASOURCE_H
9
10#include "pxr/imaging/hd/dataSource.h"
11
12#include <tbb/concurrent_hash_map.h>
13
14// This file provides two classes HdCachingSampledDataSource and
15// HdCachingTypedSampledDataSource which cache values obtained from GetValue
16// and GetTypedValue across subsequent calls. The values are freed when the
17// caching data source is freed. They are thread safe using a
18// tbb::concurrent_hash_map as the container. Users should be careful to make
19// sure this is freed correctly after use as tbb::concurrent_hash_map can have
20// some unwanted memory overhead.
21
22PXR_NAMESPACE_OPEN_SCOPE
23
38{
39public:
40 HD_DECLARE_DATASOURCE(HdCachingSampledDataSource);
41 using Time = HdSampledDataSource::Time;
42 using TimeValueMap = tbb::concurrent_hash_map<Time, VtValue>;
43
45 GetValue(Time shutterOffset) override
46 {
47 if (!_input) {
48 return VtValue();
49 }
50
51 // Lookup value in cache
52 TimeValueMap::const_accessor const_acc;
53 if (_cache.find(const_acc, shutterOffset)) {
54 return const_acc->second;
55 }
56 // Cache miss compute value and store
57 TimeValueMap::accessor acc;
58 if (_cache.insert(acc, shutterOffset)) {
59 acc->second = _input->GetValue(shutterOffset);
60 }
61 return acc->second;
62 }
63
64 bool
66 Time startTime,
67 Time endTime,
68 std::vector<Time> * outSampleTimes) override
69 {
70 if (!_input) {
71 return false;
72 }
73
74 return _input->GetContributingSampleTimesForInterval(
75 startTime, endTime, outSampleTimes
76 );
77 }
78
79protected:
80 HdCachingSampledDataSource(const HdSampledDataSourceHandle& input)
81 : _input(input) {}
82
83 const HdSampledDataSourceHandle _input;
84 TimeValueMap _cache;
85};
86
87HD_DECLARE_DATASOURCE_HANDLES(HdCachingSampledDataSource);
88
102template <typename T>
104{
105public:
106 HD_DECLARE_DATASOURCE(HdCachingTypedSampledDataSource<T>);
107 using Time = HdSampledDataSource::Time;
108 using TimeValueMap = tbb::concurrent_hash_map<Time, T>;
109
110 T GetTypedValue(Time shutterOffset) override
111 {
112 if (!_input) {
113 return T();
114 }
115
116 // Lookup value in cache
117 typename TimeValueMap::const_accessor const_acc;
118 if (_cache.find(const_acc, shutterOffset)) {
119 return const_acc->second;
120 }
121 // Cache miss compute value and store
122 typename TimeValueMap::accessor acc;
123 if (_cache.insert(acc, shutterOffset)) {
124 acc->second = _input->GetTypedValue(shutterOffset);
125 }
126 return acc->second;
127 }
128
129 VtValue GetValue(Time shutterOffset) override
130 {
131 return VtValue(GetTypedValue(shutterOffset));
132 }
133
135 Time startTime,
136 Time endTime,
137 std::vector<Time> * outSampleTimes) override
138 {
139 if (!_input) {
140 return false;
141 }
142
143 return _input->GetContributingSampleTimesForInterval(
144 startTime, endTime, outSampleTimes
145 );
146 }
147
148protected:
150 : _input(input) {}
151
152 const typename HdTypedSampledDataSource<T>::Handle _input;
153 TimeValueMap _cache;
154};
155
156PXR_NAMESPACE_CLOSE_SCOPE
157
158#endif // PXR_IMAGING_HD_CACHING_SAMPLED_DATASOURCE_H
A sampled data source which maintains a cache of values from the upstream sampled data source,...
bool GetContributingSampleTimesForInterval(Time startTime, Time endTime, std::vector< Time > *outSampleTimes) override
Given a shutter window of interest (startTime and endTime relative to the current frame),...
VtValue GetValue(Time shutterOffset) override
Returns the value of this data source at frame-relative time shutterOffset.
A typed sampled data source which maintains a cache of values from the upstream sampled data source,...
bool GetContributingSampleTimesForInterval(Time startTime, Time endTime, std::vector< Time > *outSampleTimes) override
Given a shutter window of interest (startTime and endTime relative to the current frame),...
VtValue GetValue(Time shutterOffset) override
Returns the value of this data source at frame-relative time shutterOffset.
T GetTypedValue(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
A datasource representing a concretely-typed sampled value.
Definition dataSource.h:248
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition value.h:90