Loading...
Searching...
No Matches
bufferSource.h
1//
2// Copyright 2016 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_BUFFER_SOURCE_H
25#define PXR_IMAGING_HD_BUFFER_SOURCE_H
26
27#include "pxr/pxr.h"
28#include "pxr/imaging/hd/api.h"
29#include "pxr/imaging/hd/version.h"
30#include "pxr/imaging/hd/bufferSpec.h"
32#include "pxr/base/tf/token.h"
33
34#include <atomic>
35#include <memory>
36#include <vector>
37
38
39PXR_NAMESPACE_OPEN_SCOPE
40
41class HdBufferSource;
42using HdBufferSourceSharedPtr = std::shared_ptr<HdBufferSource>;
43using HdBufferSourceConstSharedPtr = std::shared_ptr<HdBufferSource const>;
44using HdBufferSourceSharedPtrVector = std::vector<HdBufferSourceSharedPtr>;
45using HdBufferSourceWeakPtr = std::weak_ptr<HdBufferSource>;
46
56{
57public:
58 HdBufferSource() : _state(UNRESOLVED) { }
59
60 HD_API
61 virtual ~HdBufferSource();
62
64 virtual TfToken const &GetName() const = 0;
65
68 virtual void GetBufferSpecs(HdBufferSpecVector *specs) const = 0;
69
71 HD_API
72 virtual size_t ComputeHash() const;
73
82 virtual bool Resolve() = 0;
83
85
87 virtual void const* GetData() const = 0;
88
90 virtual HdTupleType GetTupleType() const = 0;
91
94 virtual size_t GetNumElements() const = 0;
95
97 bool IsResolved() const {
98 return _state >= RESOLVED;
99 }
100
102 bool HasResolveError() const {
103 return _state == RESOLVE_ERROR;
104 }
105
120
122 HD_API
123 virtual bool HasPreChainedBuffer() const;
124
126 HD_API
127 virtual HdBufferSourceSharedPtr GetPreChainedBuffer() const;
128
130 HD_API
131 virtual bool HasChainedBuffer() const;
132
134 HD_API
135 virtual HdBufferSourceSharedPtrVector GetChainedBuffers() const;
136
138
143 HD_API
144 bool IsValid() const;
145
146protected:
150 TF_VERIFY(_state == BEING_RESOLVED);
151 _state = RESOLVED;
152 }
153
164 TF_VERIFY(_state == BEING_RESOLVED);
165 _state = RESOLVE_ERROR;
166 }
167
173 bool _TryLock() {
174 State oldState = UNRESOLVED;
175 return _state.compare_exchange_strong(oldState, BEING_RESOLVED);
176 }
177
194 virtual bool _CheckValid() const = 0;
195
196private:
197 // Don't allow copies
198 HdBufferSource(const HdBufferSource &) = delete;
199 HdBufferSource &operator=(const HdBufferSource &) = delete;
200
201 enum State { UNRESOLVED=0, BEING_RESOLVED, RESOLVED, RESOLVE_ERROR};
202 std::atomic<State> _state;
203};
204
214public:
215 HD_API
216 virtual TfToken const &GetName() const override;
217 HD_API
218 virtual size_t ComputeHash() const override;
219 HD_API
220 virtual void const* GetData() const override;
221 HD_API
222 virtual HdTupleType GetTupleType() const override;
223 HD_API
224 virtual size_t GetNumElements() const override;
225
226protected:
227 void _SetResult(HdBufferSourceSharedPtr const &result) {
228 _result = result;
229 }
230
231private:
232 HdBufferSourceSharedPtr _result;
233};
234
239public:
240 HD_API
241 virtual TfToken const &GetName() const override;
242 HD_API
243 virtual void const* GetData() const override;
244 HD_API
245 virtual size_t ComputeHash() const override;
246 HD_API
247 virtual size_t GetNumElements() const override;
248 HD_API
249 virtual HdTupleType GetTupleType() const override;
250 HD_API
251 virtual void GetBufferSpecs(HdBufferSpecVector *specs) const override;
252};
253
254
255PXR_NAMESPACE_CLOSE_SCOPE
256
257#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.
Definition: bufferSource.h:56
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.
Definition: bufferSource.h:97
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.
Definition: bufferSource.h:173
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...
Definition: bufferSource.h:163
void _SetResolved()
Marks this buffer source as resolved.
Definition: bufferSource.h:149
bool HasResolveError() const
Returns true if an error occurred during resolve.
Definition: bufferSource.h:102
A abstract base class for cpu computation followed by buffer transfer to the GPU.
Definition: bufferSource.h:213
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.
A abstract base class for pure cpu computation.
Definition: bufferSource.h:238
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.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:283
HdTupleType represents zero, one, or more values of the same HdType.
Definition: types.h:358
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...