Loading...
Searching...
No Matches
bufferSource.h
1//
2// Copyright 2016 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_BUFFER_SOURCE_H
8#define PXR_IMAGING_HD_BUFFER_SOURCE_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/hd/api.h"
12#include "pxr/imaging/hd/version.h"
13#include "pxr/imaging/hd/bufferSpec.h"
15#include "pxr/base/tf/token.h"
16
17#include <atomic>
18#include <memory>
19#include <vector>
20
21
22PXR_NAMESPACE_OPEN_SCOPE
23
24class HdBufferSource;
25using HdBufferSourceSharedPtr = std::shared_ptr<HdBufferSource>;
26using HdBufferSourceConstSharedPtr = std::shared_ptr<HdBufferSource const>;
27using HdBufferSourceSharedPtrVector = std::vector<HdBufferSourceSharedPtr>;
28using HdBufferSourceWeakPtr = std::weak_ptr<HdBufferSource>;
29
39{
40public:
42 // No need for atomic init since _TryLock() will do the acquire
43 : _state(UNRESOLVED)
44 {
45 }
46
47 HD_API
48 virtual ~HdBufferSource();
49
51 virtual TfToken const &GetName() const = 0;
52
55 virtual void GetBufferSpecs(HdBufferSpecVector *specs) const = 0;
56
58 HD_API
59 virtual size_t ComputeHash() const;
60
69 virtual bool Resolve() = 0;
70
72
74 virtual void const* GetData() const = 0;
75
77 virtual HdTupleType GetTupleType() const = 0;
78
81 virtual size_t GetNumElements() const = 0;
82
88 bool IsResolved() const {
89 return _state.load(std::memory_order_acquire) >= RESOLVED;
90 }
91
97 bool HasResolveError() const {
98 return _state.load(std::memory_order_acquire) == RESOLVE_ERROR;
99 }
100
115
117 HD_API
118 virtual bool HasPreChainedBuffer() const;
119
121 HD_API
122 virtual HdBufferSourceSharedPtr GetPreChainedBuffer() const;
123
125 HD_API
126 virtual bool HasChainedBuffer() const;
127
129 HD_API
130 virtual HdBufferSourceSharedPtrVector GetChainedBuffers() const;
131
133
138 HD_API
139 bool IsValid() const;
140
141protected:
146 TF_VERIFY(_state.load(std::memory_order_relaxed) == BEING_RESOLVED);
147 _state.store(RESOLVED, std::memory_order_release);
148 }
149
163 TF_VERIFY(_state.load(std::memory_order_relaxed) == BEING_RESOLVED);
164 _state.store(RESOLVE_ERROR, std::memory_order_release);
165 }
166
174 bool _TryLock() {
175 State oldState = UNRESOLVED;
176 return _state.compare_exchange_strong(oldState, BEING_RESOLVED,
177 std::memory_order_acquire);
178 }
179
196 virtual bool _CheckValid() const = 0;
197
198protected:
199 enum State { UNRESOLVED=0, BEING_RESOLVED, RESOLVED, RESOLVE_ERROR};
200 HdBufferSource(State state) : _state(state) { }
201
202private:
203 // Don't allow copies
204 HdBufferSource(const HdBufferSource &) = delete;
205 HdBufferSource &operator=(const HdBufferSource &) = delete;
206
207 std::atomic<State> _state;
208};
209
213public:
215
216 HD_API
217 bool Resolve() override { return true; }
218};
219
229public:
230 HD_API
231 virtual TfToken const &GetName() const override;
232 HD_API
233 virtual size_t ComputeHash() const override;
234 HD_API
235 virtual void const* GetData() const override;
236 HD_API
237 virtual HdTupleType GetTupleType() const override;
238 HD_API
239 virtual size_t GetNumElements() const override;
240
241protected:
242 void _SetResult(HdBufferSourceSharedPtr const &result) {
243 _result = result;
244 }
245
246private:
247 HdBufferSourceSharedPtr _result;
248};
249
254public:
255 HD_API
256 virtual TfToken const &GetName() const override;
257 HD_API
258 virtual void const* GetData() const override;
259 HD_API
260 virtual size_t ComputeHash() const override;
261 HD_API
262 virtual size_t GetNumElements() const override;
263 HD_API
264 virtual HdTupleType GetTupleType() const override;
265 HD_API
266 virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override;
267};
268
269
270PXR_NAMESPACE_CLOSE_SCOPE
271
272#endif //PXR_IMAGING_HD_BUFFER_SOURCE_H
Low-level utilities for informing users of various internal and external diagnostic conditions.
A transient buffer of data that has not yet been committed.
virtual void const * GetData() const =0
Following interfaces will be called after Resolve.
virtual bool _CheckValid() const =0
Checks the validity of the source buffer.
virtual size_t GetNumElements() const =0
Returns the number of elements (e.g.
virtual TfToken const & GetName() const =0
Return the name of this buffer source.
bool IsResolved() const
Returns true it this computation has already been resolved.
virtual bool Resolve()=0
Prepare the access of GetData().
virtual HdTupleType GetTupleType() const =0
Returns the data type and count (array size) for this buffer source.
HD_API bool IsValid() const
Checks the validity of the source buffer.
virtual HD_API HdBufferSourceSharedPtr GetPreChainedBuffer() const
Returns the pre-chained buffer.
virtual HD_API bool HasPreChainedBuffer() const
Returns true if this buffer has a pre-chained buffer.
virtual HD_API bool HasChainedBuffer() const
Returns true if this buffer has any chained buffer(s)
virtual void GetBufferSpecs(HdBufferSpecVector *specs) const =0
Add the buffer spec for this buffer source into given bufferspec vector.
bool _TryLock()
Non-blocking lock acquisition.
virtual HD_API size_t ComputeHash() const
Computes and returns a hash value for the underlying data.
virtual HD_API HdBufferSourceSharedPtrVector GetChainedBuffers() const
Returns the vector of chained buffers.
void _SetResolveError()
Called during Resolve() to indicate an unrecoverable failure occurred and the results of the computat...
void _SetResolved()
Marks this buffer source as resolved.
bool HasResolveError() const
Returns true if an error occurred during resolve.
An abstract base class for cpu computation followed by buffer transfer to the GPU.
virtual HD_API void const * GetData() const override
Following interfaces will be called after Resolve.
virtual HD_API HdTupleType GetTupleType() const override
Returns the data type and count (array size) for this buffer source.
virtual HD_API TfToken const & GetName() const override
Return the name of this buffer source.
virtual HD_API size_t GetNumElements() const override
Returns the number of elements (e.g.
virtual HD_API size_t ComputeHash() const override
Computes and returns a hash value for the underlying data.
An abstract base class for pure cpu computation.
virtual HD_API void const * GetData() const override
Following interfaces will be called after Resolve.
virtual HD_API void GetBufferSpecs(HdBufferSpecVector *specs) const override
Add the buffer spec for this buffer source into given bufferspec vector.
virtual HD_API HdTupleType GetTupleType() const override
Returns the data type and count (array size) for this buffer source.
virtual HD_API TfToken const & GetName() const override
Return the name of this buffer source.
virtual HD_API size_t GetNumElements() const override
Returns the number of elements (e.g.
virtual HD_API size_t ComputeHash() const override
Computes and returns a hash value for the underlying data.
An abstract base class for a plain old data buffer source that doesn't need to be resolved.
HD_API bool Resolve() override
Prepare the access of GetData().
Token for efficient comparison, assignment, and hashing of known strings.
Definition token.h:81
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition diagnostic.h:267
HdTupleType represents zero, one, or more values of the same HdType.
Definition types.h:343
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...