OpenVDB 10.0.1
Loading...
Searching...
No Matches
GridHandle.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: MPL-2.0
3
4/*!
5 \file GridHandle.h
6
7 \author Ken Museth
8
9 \date January 8, 2020
10
11 \brief Defines two classes, a GridRegister the defines the value type (e.g. Double, Float etc)
12 of a NanoVDB grid, and a GridHandle and manages the memory of a NanoVDB grid.
13
14 \note This file has NO dependency on OpenVDB.
15*/
16
17#ifndef NANOVDB_GRID_HANDLE_H_HAS_BEEN_INCLUDED
18#define NANOVDB_GRID_HANDLE_H_HAS_BEEN_INCLUDED
19
20#include "../NanoVDB.h"// for mapToGridType
21#include "HostBuffer.h"
22
23namespace nanovdb {
24
25// --------------------------> GridHandleBase <------------------------------------
26
28{
29public:
30 virtual ~GridHandleBase() {}
31
32 /// @brief Returns the size in bytes of the raw memory buffer managed by this GridHandle's allocator.
33 virtual uint64_t size() const = 0;
34
35 virtual uint8_t* data() = 0;
36 virtual const uint8_t* data() const = 0;
37
38 /// @brief Return true if this handle is empty, i.e. has no allocated memory
39 bool empty() const { return size() == 0; }
40
41 /// @brief Return true if this handle contains a grid
42 operator bool() const { return !this->empty(); }
43
44 /// @brief Returns a const point to the grid meta data (see definition above).
45 ///
46 /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized
47 const GridMetaData* gridMetaData() const { return reinterpret_cast<const GridMetaData*>(data()); }
48
49 /// @brief Returns the GridType handled by this instance, and GridType::End if empty
51 {
52 const GridMetaData* ptr = this->gridMetaData();
53 return ptr ? ptr->gridType() : GridType::End;
54 }
55
56 /// @brief Return the number of grids contained in this buffer
57 uint32_t gridCount() const
58 {
59 auto *ptr = this->gridMetaData();
60 return ptr ? ptr->gridCount() : 0;
61 }
62};// GridHandleBase
63
64// --------------------------> GridHandle <------------------------------------
65
66/// @brief This class serves to manage a raw memory buffer of a NanoVDB Grid.
67///
68/// @note It is important to note that this class does NOT depend on OpenVDB.
69template<typename BufferT = HostBuffer>
71{
72 BufferT mBuffer;
73
74 template<typename ValueT>
75 const NanoGrid<ValueT>* getGrid(uint32_t n = 0) const;
76
77 template<typename ValueT, typename U = BufferT>
78 typename std::enable_if<BufferTraits<U>::hasDeviceDual, const NanoGrid<ValueT>*>::type
79 getDeviceGrid(uint32_t n = 0) const;
80
81 template <typename T>
82 static T* no_const(const T* ptr) { return const_cast<T*>(ptr); }
83
84public:
85 using BufferType = BufferT;
86
87 /// @brief Move constructor from a buffer
88 GridHandle(BufferT&& buffer) { mBuffer = std::move(buffer); }
89 /// @brief Empty ctor
90 GridHandle() = default;
91 /// @brief Disallow copy-construction
92 GridHandle(const GridHandle&) = delete;
93 /// @brief Disallow copy assignment operation
94 GridHandle& operator=(const GridHandle&) = delete;
95 /// @brief Move copy assignment operation
96 GridHandle& operator=(GridHandle&& other) noexcept
97 {
98 mBuffer = std::move(other.mBuffer);
99 return *this;
100 }
101 /// @brief Move copy-constructor
102 GridHandle(GridHandle&& other) noexcept { mBuffer = std::move(other.mBuffer); }
103 /// @brief Default destructor
104 ~GridHandle() override { reset(); }
105 /// @brief clear the buffer
106 void reset() { mBuffer.clear(); }
107
108 /// @brief Return a reference to the buffer
109 BufferT& buffer() { return mBuffer; }
110
111 /// @brief Return a const reference to the buffer
112 const BufferT& buffer() const { return mBuffer; }
113
114 /// @brief Returns a non-const pointer to the data.
115 ///
116 /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized
117 uint8_t* data() override { return mBuffer.data(); }
118
119 /// @brief Returns a const pointer to the data.
120 ///
121 /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized
122 const uint8_t* data() const override { return mBuffer.data(); }
123
124 /// @brief Returns the size in bytes of the raw memory buffer managed by this GridHandle's allocator.
125 uint64_t size() const override { return mBuffer.size(); }
126
127 /// @brief Returns a const pointer to the @a n'th NanoVDB grid encoded in this GridHandle.
128 ///
129 /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized, @a n is invalid
130 /// or if the template parameter does not match the specified grid!
131 template<typename ValueT>
132 const NanoGrid<ValueT>* grid(uint32_t n = 0) const { return this->template getGrid<ValueT>(n); }
133
134 /// @brief Returns a pointer to the @a n'th NanoVDB grid encoded in this GridHandle.
135 ///
136 /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized, @a n is invalid
137 /// or if the template parameter does not match the specified grid!
138 template<typename ValueT>
139 NanoGrid<ValueT>* grid(uint32_t n = 0) { return no_const(this->template getGrid<ValueT>(n)); }
140
141 /// @brief Return a const pointer to the @a n'th grid encoded in this GridHandle on the device, e.g. GPU
142 ///
143 /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized, @a n is invalid
144 /// or if the template parameter does not match the specified grid!
145 template<typename ValueT, typename U = BufferT>
146 typename std::enable_if<BufferTraits<U>::hasDeviceDual, const NanoGrid<ValueT>*>::type
147 deviceGrid(uint32_t n = 0) const { return this->template getDeviceGrid<ValueT>(n); }
148
149 /// @brief Return a const pointer to the @a n'th grid encoded in this GridHandle on the device, e.g. GPU
150 ///
151 /// @warning Note that the return pointer can be NULL if the GridHandle was not initialized, @a n is invalid
152 /// or if the template parameter does not match the specified grid!
153 template<typename ValueT, typename U = BufferT>
154 typename std::enable_if<BufferTraits<U>::hasDeviceDual, NanoGrid<ValueT>*>::type
155 deviceGrid(uint32_t n = 0) { return no_const(this->template getDeviceGrid<ValueT>(n)); }
156
157 /// @brief Upload the grid to the device, e.g. from CPU to GPU
158 ///
159 /// @note This method is only available if the buffer supports devices
160 template<typename U = BufferT>
161 typename std::enable_if<BufferTraits<U>::hasDeviceDual, void>::type
162 deviceUpload(void* stream = nullptr, bool sync = true) { mBuffer.deviceUpload(stream, sync); }
163
164 /// @brief Download the grid to from the device, e.g. from GPU to CPU
165 ///
166 /// @note This method is only available if the buffer supports devices
167 template<typename U = BufferT>
168 typename std::enable_if<BufferTraits<U>::hasDeviceDual, void>::type
169 deviceDownload(void* stream = nullptr, bool sync = true) { mBuffer.deviceDownload(stream, sync); }
170}; // GridHandle
171
172// --------------------------> Implementation of private methods in GridHandle <------------------------------------
173
174template<typename BufferT>
175template<typename ValueT>
176inline const NanoGrid<ValueT>* GridHandle<BufferT>::getGrid(uint32_t index) const
177{
178 using GridT = const NanoGrid<ValueT>;
179 auto *data = mBuffer.data();
180 GridT *grid = reinterpret_cast<GridT*>(data);
181 if (grid == nullptr || index >= grid->gridCount()) {// un-initialized or index is out of range
182 return nullptr;
183 }
184 while(index != grid->gridIndex()) {
185 data += grid->gridSize();
186 grid = reinterpret_cast<GridT*>(data);
187 }
188 return grid->gridType() == mapToGridType<ValueT>() ? grid : nullptr;
189}
190
191template<typename BufferT>
192template<typename ValueT, typename U>
193inline typename std::enable_if<BufferTraits<U>::hasDeviceDual, const NanoGrid<ValueT>*>::type
194GridHandle<BufferT>::getDeviceGrid(uint32_t index) const
195{
196 using GridT = const NanoGrid<ValueT>;
197 auto *data = mBuffer.data();
198 GridT *grid = reinterpret_cast<GridT*>(data);
199 if (grid == nullptr || index >= grid->gridCount()) {// un-initialized or index is out of range
200 return nullptr;
201 }
202 auto* dev = mBuffer.deviceData();
203 while(index != grid->gridIndex()) {
204 data += grid->gridSize();
205 dev += grid->gridSize();
206 grid = reinterpret_cast<GridT*>(data);
207 }
208 return grid->gridType() == mapToGridType<ValueT>() ? reinterpret_cast<GridT*>(dev) : nullptr;
209}
210
211} // namespace nanovdb
212
213#endif // NANOVDB_GRID_HANDLE_H_HAS_BEEN_INCLUDED
HostBuffer - a buffer that contains a shared or private bump pool to either externally or internally ...
Definition: GridHandle.h:28
bool empty() const
Return true if this handle is empty, i.e. has no allocated memory.
Definition: GridHandle.h:39
GridType gridType() const
Returns the GridType handled by this instance, and GridType::End if empty.
Definition: GridHandle.h:50
virtual uint8_t * data()=0
const GridMetaData * gridMetaData() const
Returns a const point to the grid meta data (see definition above).
Definition: GridHandle.h:47
virtual ~GridHandleBase()
Definition: GridHandle.h:30
uint32_t gridCount() const
Return the number of grids contained in this buffer.
Definition: GridHandle.h:57
virtual uint64_t size() const =0
Returns the size in bytes of the raw memory buffer managed by this GridHandle's allocator.
virtual const uint8_t * data() const =0
This class serves to manage a raw memory buffer of a NanoVDB Grid.
Definition: GridHandle.h:71
~GridHandle() override
Default destructor.
Definition: GridHandle.h:104
std::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceUpload(void *stream=nullptr, bool sync=true)
Upload the grid to the device, e.g. from CPU to GPU.
Definition: GridHandle.h:162
GridHandle & operator=(const GridHandle &)=delete
Disallow copy assignment operation.
GridHandle(BufferT &&buffer)
Move constructor from a buffer.
Definition: GridHandle.h:88
NanoGrid< ValueT > * grid(uint32_t n=0)
Returns a pointer to the n'th NanoVDB grid encoded in this GridHandle.
Definition: GridHandle.h:139
BufferT & buffer()
Return a reference to the buffer.
Definition: GridHandle.h:109
uint64_t size() const override
Returns the size in bytes of the raw memory buffer managed by this GridHandle's allocator.
Definition: GridHandle.h:125
std::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceDownload(void *stream=nullptr, bool sync=true)
Download the grid to from the device, e.g. from GPU to CPU.
Definition: GridHandle.h:169
GridHandle(const GridHandle &)=delete
Disallow copy-construction.
GridHandle & operator=(GridHandle &&other) noexcept
Move copy assignment operation.
Definition: GridHandle.h:96
const NanoGrid< ValueT > * grid(uint32_t n=0) const
Returns a const pointer to the n'th NanoVDB grid encoded in this GridHandle.
Definition: GridHandle.h:132
std::enable_if< BufferTraits< U >::hasDeviceDual, NanoGrid< ValueT > * >::type deviceGrid(uint32_t n=0)
Return a const pointer to the n'th grid encoded in this GridHandle on the device, e....
Definition: GridHandle.h:155
uint8_t * data() override
Returns a non-const pointer to the data.
Definition: GridHandle.h:117
const BufferT & buffer() const
Return a const reference to the buffer.
Definition: GridHandle.h:112
GridHandle(GridHandle &&other) noexcept
Move copy-constructor.
Definition: GridHandle.h:102
std::enable_if< BufferTraits< U >::hasDeviceDual, constNanoGrid< ValueT > * >::type deviceGrid(uint32_t n=0) const
Return a const pointer to the n'th grid encoded in this GridHandle on the device, e....
Definition: GridHandle.h:147
const uint8_t * data() const override
Returns a const pointer to the data.
Definition: GridHandle.h:122
BufferT BufferType
Definition: GridHandle.h:85
void reset()
clear the buffer
Definition: GridHandle.h:106
GridHandle()=default
Empty ctor.
This is a convenient class that allows for access to grid meta-data that are independent of the value...
Definition: NanoVDB.h:5420
GridType gridType() const
Definition: NanoVDB.h:5435
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:2556
Definition: NanoVDB.h:208
GridType
List of types that are currently supported by NanoVDB.
Definition: NanoVDB.h:243