4 #ifndef OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
5 #define OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
11 #include <tbb/spin_mutex.h>
17 #include <type_traits>
30 template<
typename T, Index Log2Dim>
39 #ifdef OPENVDB_USE_DELAYED_LOADING
42 FileInfo(): bufpos(0) , maskpos(0) {}
43 std::streamoff bufpos;
44 std::streamoff maskpos;
45 io::MappedFile::Ptr mapping;
53 #ifdef OPENVDB_USE_DELAYED_LOADING
64 #ifdef OPENVDB_USE_DELAYED_LOADING
74 #ifdef OPENVDB_USE_DELAYED_LOADING
75 return bool(mOutOfCore);
136 inline void setOutOfCore(
bool b)
139 #ifdef OPENVDB_USE_DELAYED_LOADING
145 inline void loadValues()
const
147 #ifdef OPENVDB_USE_DELAYED_LOADING
151 inline void doLoad()
const;
152 inline bool detachFromFile();
154 using FlagsType = std::atomic<Index32>;
156 #ifdef OPENVDB_USE_DELAYED_LOADING
164 FlagsType mOutOfCore;
165 tbb::spin_mutex mMutex;
168 friend class ::TestLeaf;
177 template<
typename T, Index Log2Dim>
182 #ifdef OPENVDB_USE_DELAYED_LOADING
189 template<
typename T, Index Log2Dim>
193 #ifdef OPENVDB_USE_DELAYED_LOADING
194 if (this->isOutOfCore()) {
195 this->detachFromFile();
205 template<
typename T, Index Log2Dim>
210 , mOutOfCore(other.mOutOfCore.load())
213 #ifdef OPENVDB_USE_DELAYED_LOADING
215 mFileInfo =
new FileInfo(*other.mFileInfo);
218 if (other.mData !=
nullptr) {
223 while (n--) *target++ = *source++;
225 #ifdef OPENVDB_USE_DELAYED_LOADING
231 template<
typename T, Index Log2Dim>
237 if (mData) mData[i] =
val;
241 template<
typename T, Index Log2Dim>
245 if (&other !=
this) {
246 #ifdef OPENVDB_USE_DELAYED_LOADING
247 if (this->isOutOfCore()) {
248 this->detachFromFile();
253 mOutOfCore.store(other.mOutOfCore.load(std::memory_order_acquire),
254 std::memory_order_release);
255 mFileInfo =
new FileInfo(*other.mFileInfo);
258 if (other.mData !=
nullptr) {
263 while (n--) *target++ = *source++;
265 #ifdef OPENVDB_USE_DELAYED_LOADING
273 template<
typename T, Index Log2Dim>
277 this->detachFromFile();
278 if (mData !=
nullptr) {
281 while (n--) *target++ =
val;
286 template<
typename T, Index Log2Dim>
293 if (!target && !source)
return true;
294 if (!target || !source)
return false;
301 template<
typename T, Index Log2Dim>
306 #ifdef OPENVDB_USE_DELAYED_LOADING
311 auto tmp = other.mOutOfCore.load(std::memory_order_acquire);
312 tmp = mOutOfCore.exchange(std::move(tmp));
313 other.mOutOfCore.store(std::move(tmp), std::memory_order_release);
318 template<
typename T, Index Log2Dim>
322 size_t n =
sizeof(*this);
323 #ifdef OPENVDB_USE_DELAYED_LOADING
324 if (this->isOutOfCore()) n +=
sizeof(FileInfo);
328 #ifdef OPENVDB_USE_DELAYED_LOADING
331 return static_cast<Index>(
n);
335 template<
typename T, Index Log2Dim>
339 size_t n =
sizeof(*this);
341 return static_cast<Index>(
n);
345 template<
typename T, Index Log2Dim>
350 if (mData ==
nullptr) {
352 #ifdef OPENVDB_USE_DELAYED_LOADING
354 tbb::spin_mutex::scoped_lock lock(self->mMutex);
361 template<
typename T, Index Log2Dim>
366 if (mData ==
nullptr) {
367 #ifdef OPENVDB_USE_DELAYED_LOADING
369 tbb::spin_mutex::scoped_lock lock(mMutex);
377 template<
typename T, Index Log2Dim>
381 static const ValueType sZero = zeroVal<T>();
386 if (mData)
return mData[i];
else return sZero;
390 template<
typename T, Index Log2Dim>
392 LeafBuffer<T, Log2Dim>::deallocate()
395 if (mData !=
nullptr) {
396 #ifdef OPENVDB_USE_DELAYED_LOADING
397 if (this->isOutOfCore())
return false;
407 template<
typename T, Index Log2Dim>
409 LeafBuffer<T, Log2Dim>::doLoad()
const
411 #ifdef OPENVDB_USE_DELAYED_LOADING
412 if (!this->isOutOfCore())
return;
414 LeafBuffer<T, Log2Dim>*
self =
const_cast<LeafBuffer<T, Log2Dim>*
>(
this);
418 tbb::spin_mutex::scoped_lock lock(self->mMutex);
419 if (!this->isOutOfCore())
return;
421 std::unique_ptr<FileInfo> info(self->mFileInfo);
427 self->mData =
nullptr;
430 SharedPtr<std::streambuf>
buf = info->mapping->createBuffer();
431 std::istream is(buf.get());
436 is.seekg(info->maskpos);
439 is.seekg(info->bufpos);
442 self->setOutOfCore(
false);
447 template<
typename T, Index Log2Dim>
449 LeafBuffer<T, Log2Dim>::detachFromFile()
451 #ifdef OPENVDB_USE_DELAYED_LOADING
452 if (this->isOutOfCore()) {
455 this->setOutOfCore(
false);
467 template<Index Log2Dim>
479 static inline const bool sOn =
true;
480 static inline const bool sOff =
false;
495 if (mData.isOn(i))
return sOn;
else return sOff;
528 #endif // OPENVDB_TREE_LEAFBUFFER_HAS_BEEN_INCLUDED
Index memUsageIfLoaded() const
Index memUsageIfLoaded() const
GLenum GLuint GLenum GLsizei const GLchar * buf
LeafBuffer & operator=(const LeafBuffer &)
Copy the other buffer's values into this buffer.
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
LeafBuffer(PartialCreate, const ValueType &)
Construct a buffer but don't allocate memory for the full array of values.
OPENVDB_API bool getHalfFloat(std::ios_base &)
Return true if floating-point values should be quantized to 16 bits when writing to the given stream ...
#define OPENVDB_USE_VERSION_NAMESPACE
const bool & getValue(Index i) const
bool operator==(const LeafBuffer &) const
Return true if the contents of the other buffer exactly equal the contents of this buffer...
void swap(LeafBuffer &other)
bool empty() const
Return true if memory for this buffer has not yet been allocated.
Tag dispatch class that distinguishes constructors during file input.
#define OPENVDB_ASSERT(X)
void readCompressedValues(std::istream &is, ValueT *destBuf, Index destCount, const MaskT &valueMask, bool fromHalf)
std::shared_ptr< T > SharedPtr
LeafBuffer()
Default constructor.
Bit mask for the internal and leaf nodes of VDB. This is a 64-bit implementation. ...
void swap(LeafBuffer &)
Exchange this buffer's values with the other buffer's values.
Templated block class to hold specific data types and a fixed number of values determined by Log2Dim...
#define OPENVDB_USE_DELAYED_LOADING
bool allocate()
Allocate memory for this buffer if it has not already been allocated.
static Index size()
Return the number of values contained in this buffer.
GLsizei GLsizei GLchar * source
static const Index32 WORD_COUNT
const ValueType & getValue(Index i) const
Return a const reference to the i'th element of this buffer.
LeafBuffer(const LeafBuffer &other)
Index memUsage() const
Return the memory footprint of this buffer in bytes.
WordType * data()
Return a pointer to the C-style array of words encoding the bits.
GLboolean GLboolean GLboolean b
typename NodeMaskType::Word WordType
OPENVDB_API void setStreamMetadataPtr(std::ios_base &, SharedPtr< StreamMetadata > &, bool transfer=true)
Associate the given stream with (a shared pointer to) an object that stores metadata (file format...
const ValueType * data() const
Return a const pointer to the array of voxel values.
const ValueType & operator[](Index i) const
Return a const reference to the i'th element of this buffer.
void fill(const ValueType &)
Populate this buffer with a constant value.
LeafBuffer(const NodeMaskType &other)
bool operator==(const LeafBuffer &other) const
bool isOutOfCore() const
Return true if this buffer's values have not yet been read from disk.
Array of fixed size 23Log2Dim that stores the voxel values of a LeafNode.
LeafBuffer & operator=(const LeafBuffer &b)
void setValue(Index i, bool val)
void setValue(Index i, const ValueType &)
Set the i'th value of this buffer to the specified value.
bool operator!=(const LeafBuffer &other) const
Return true if the contents of the other buffer are not exactly equal to the contents of this buffer...
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
const WordType * data() const
Return a const pointer to the C-style array of words encoding the bits.
const bool & operator[](Index i) const
bool operator!=(const LeafBuffer &other) const