17#include "../NanoVDB.h"
20#ifndef NANOVDB_NODEMANAGER_H_HAS_BEEN_INCLUDED
21#define NANOVDB_NODEMANAGER_H_HAS_BEEN_INCLUDED
26template <
typename BuildT>
30template<
typename BufferT = HostBuffer>
31class NodeManagerHandle;
40template <
typename BuildT,
typename BufferT = HostBuffer>
42 const BufferT& buffer = BufferT());
53template<
typename BufferT>
58 template<
typename BuildT>
61 template<
typename BuildT,
typename U = BufferT>
66 static T* no_const(
const T* ptr) {
return const_cast<T*
>(ptr); }
80 mBuffer = std::move(other.mBuffer);
88 void reset() { mBuffer.clear(); }
91 BufferT&
buffer() {
return mBuffer; }
94 const BufferT&
buffer()
const {
return mBuffer; }
99 uint8_t*
data() {
return mBuffer.data(); }
104 const uint8_t*
data()
const {
return mBuffer.data(); }
107 uint64_t
size()
const {
return mBuffer.size(); }
112 template<
typename BuildT>
118 template<
typename BuildT>
124 template<
typename BuildT,
typename U = BufferT>
126 deviceMgr()
const {
return this->
template getDeviceMgr<BuildT>(); }
131 template<
typename BuildT,
typename U = BufferT>
133 deviceMgr() {
return no_const(this->
template getDeviceMgr<BuildT>()); }
138 template<
typename U = BufferT>
139 typename std::enable_if<BufferTraits<U>::hasDeviceDual,
void>::type
140 deviceUpload(
void* deviceGrid,
void* stream =
nullptr,
bool sync =
true)
144 void *tmp =
data->mGrid;
145 data->mGrid = deviceGrid;
146 mBuffer.deviceUpload(stream, sync);
153 template<
typename U = BufferT>
154 typename std::enable_if<BufferTraits<U>::hasDeviceDual,
void>::type
158 void *tmp =
data->mGrid;
159 mBuffer.deviceDownload(stream, sync);
164template<
typename BufferT>
165template<
typename BuildT>
166inline const NodeManager<BuildT>* NodeManagerHandle<BufferT>::getMgr()
const
168 using T =
const NodeManager<BuildT>*;
169 T mgr =
reinterpret_cast<T
>(mBuffer.data());
170 return mgr && mgr->grid().gridType() == mapToGridType<BuildT>() ? mgr :
nullptr;
173template<
typename BufferT>
174template<
typename BuildT,
typename U>
175inline typename std::enable_if<BufferTraits<U>::hasDeviceDual,
const NodeManager<BuildT>*>::type
176NodeManagerHandle<BufferT>::getDeviceMgr()
const
178 using T =
const NodeManager<BuildT>*;
179 T mgr =
reinterpret_cast<T
>(mBuffer.data());
180 return mgr && mgr->grid().gridType() == mapToGridType<BuildT>() ?
reinterpret_cast<T
>(mBuffer.deviceData()) :
nullptr;
187template<
typename BuildT>
195 using RootT = NodeT<3>;
196 using Node2 = NodeT<2>;
197 using Node1 = NodeT<1>;
198 using Node0 = NodeT<0>;
199 static constexpr bool FIXED_SIZE = Node0::FIXED_SIZE && Node1::FIXED_SIZE && Node2::FIXED_SIZE;
221 const uint32_t *p =
grid.
tree().data()->mNodeCount;
222 size +=
sizeof(int64_t)*(p[0]+p[1]+p[2]);
250 const NodeT<LEVEL>* ptr =
nullptr;
264 NodeT<LEVEL>* ptr =
nullptr;
288template <
typename BuildT,
typename BufferT>
290 const BufferT& buffer)
299 data->mOff[0] =
PtrDiff(grid.
tree().template getFirstNode<0>(), &grid);
300 data->mOff[1] =
PtrDiff(grid.
tree().template getFirstNode<1>(), &grid);
301 data->mOff[2] =
PtrDiff(grid.
tree().template getFirstNode<2>(), &grid);
303 int64_t *ptr0 = data->mPtr[0] =
reinterpret_cast<int64_t*
>(data + 1);
304 int64_t *ptr1 = data->mPtr[1] = data->mPtr[0] + grid.
tree().nodeCount(0);
305 int64_t *ptr2 = data->mPtr[2] = data->mPtr[1] + grid.
tree().nodeCount(1);
307 for (
auto it2 = grid.
tree().root().beginChild(); it2; ++it2) {
308 *ptr2++ =
PtrDiff(&*it2, &grid);
309 for (
auto it1 = it2->beginChild(); it1; ++it1) {
310 *ptr1++ =
PtrDiff(&*it1, &grid);
311 for (
auto it0 = it1->beginChild(); it0; ++it0) {
312 *ptr0++ =
PtrDiff(&*it0, &grid);
HostBuffer - a buffer that contains a shared or private bump pool to either externally or internally ...
#define __hostdev__
Definition: NanoVDB.h:192
#define NANOVDB_ASSERT(x)
Definition: NanoVDB.h:173
#define NANOVDB_MAGIC_NUMBER
Definition: NanoVDB.h:121
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:2556
bool isBreadthFirst() const
Definition: NanoVDB.h:2687
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:2598
NodeManagerHandle manages the memory of a NodeManager.
Definition: NodeManager.h:55
NodeManagerHandle & operator=(const NodeManagerHandle &)=delete
Disallow copy assignment operation.
std::enable_if< BufferTraits< U >::hasDeviceDual, NodeManager< BuildT > * >::type deviceMgr()
Return a const pointer to the NodeManager encoded in this NodeManagerHandle on the device,...
Definition: NodeManager.h:133
const uint8_t * data() const
Returns a const pointer to the data.
Definition: NodeManager.h:104
BufferT & buffer()
Return a reference to the buffer.
Definition: NodeManager.h:91
std::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceDownload(void *stream=nullptr, bool sync=true)
Download the NodeManager to from the device, e.g. from GPU to CPU.
Definition: NodeManager.h:155
NodeManagerHandle(BufferT &&buffer)
Move constructor from a buffer.
Definition: NodeManager.h:70
NodeManagerHandle & operator=(NodeManagerHandle &&other) noexcept
Move copy assignment operation.
Definition: NodeManager.h:78
uint64_t size() const
Returns the size in bytes of the raw memory buffer managed by this NodeManagerHandle's allocator.
Definition: NodeManager.h:107
std::enable_if< BufferTraits< U >::hasDeviceDual, constNodeManager< BuildT > * >::type deviceMgr() const
Return a const pointer to the NodeManager encoded in this NodeManagerHandle on the device,...
Definition: NodeManager.h:126
const BufferT & buffer() const
Return a const reference to the buffer.
Definition: NodeManager.h:94
NodeManager< BuildT > * mgr()
Returns a pointer to the NodeManager encoded in this NodeManagerHandle.
Definition: NodeManager.h:119
uint8_t * data()
Returns a non-const pointer to the data.
Definition: NodeManager.h:99
std::enable_if< BufferTraits< U >::hasDeviceDual, void >::type deviceUpload(void *deviceGrid, void *stream=nullptr, bool sync=true)
Upload the NodeManager to the device, e.g. from CPU to GPU.
Definition: NodeManager.h:140
const NodeManager< BuildT > * mgr() const
Returns a const pointer to the NodeManager encoded in this NodeManagerHandle.
Definition: NodeManager.h:113
NodeManagerHandle()=default
Empty ctor.
NodeManagerHandle(const NodeManagerHandle &)=delete
Disallow copy-construction.
void reset()
clear the buffer
Definition: NodeManager.h:88
NodeManagerHandle(NodeManagerHandle &&other) noexcept
Move copy-constructor.
Definition: NodeManager.h:84
~NodeManagerHandle()
Default destructor.
Definition: NodeManager.h:86
NodeManager allows for sequential access to nodes.
Definition: NodeManager.h:189
NodeManager & operator=(const NodeManager &)=delete
Node1 & lower(uint32_t i)
Definition: NodeManager.h:280
bool isLinear() const
return true if the nodes have both fixed size and are arranged breadth-first in memory....
Definition: NodeManager.h:215
NodeManager & operator=(NodeManager &&)=delete
static uint64_t memUsage(const GridT &grid)
Return the memory footprint in bytes of the NodeManager derived from the specified grid.
Definition: NodeManager.h:218
uint64_t nodeCount(int level) const
Return the number of tree nodes at the specified level.
Definition: NodeManager.h:244
const Node2 & upper(uint32_t i) const
Return the i'th upper internal node with respect to breadth-first ordering.
Definition: NodeManager.h:283
NodeManager(const NodeManager &)=delete
const Node0 & leaf(uint32_t i) const
Return the i'th leaf node with respect to breadth-first ordering.
Definition: NodeManager.h:275
Node2 & upper(uint32_t i)
Definition: NodeManager.h:284
NodeManager(NodeManager &&)=delete
GridT & grid()
Return a reference to the grid.
Definition: NodeManager.h:231
Node0 & leaf(uint32_t i)
Definition: NodeManager.h:276
static bool isLinear(const GridT &grid)
return true if the nodes have both fixed size and are arranged breadth-first in memory....
Definition: NodeManager.h:211
uint64_t memUsage() const
Return the memory footprint in bytes of this instance.
Definition: NodeManager.h:228
NodeT< LEVEL > & node(uint32_t i)
Return the i'th node with respect to breadth-first ordering.
Definition: NodeManager.h:262
const NodeT< LEVEL > & node(uint32_t i) const
Return the i'th leaf node with respect to breadth-first ordering.
Definition: NodeManager.h:248
const TreeT & tree() const
Definition: NodeManager.h:236
RootT & root()
Return a reference to the root.
Definition: NodeManager.h:239
TreeT & tree()
Return a reference to the tree.
Definition: NodeManager.h:235
const GridT & grid() const
Definition: NodeManager.h:232
const RootT & root() const
Definition: NodeManager.h:240
const Node1 & lower(uint32_t i) const
Return the i'th lower internal node with respect to breadth-first ordering.
Definition: NodeManager.h:279
Definition: NanoVDB.h:208
static int64_t PtrDiff(const T1 *p, const T2 *q)
Definition: NanoVDB.h:535
NodeManagerHandle< BufferT > createNodeManager(const NanoGrid< BuildT > &grid, const BufferT &buffer=BufferT())
brief Construct a NodeManager and return its handle
Definition: NodeManager.h:289
static bool isValid(const void *p)
return true if the specified pointer is aligned and not NULL
Definition: NanoVDB.h:504
typename GridT::TreeType type
Definition: NanoVDB.h:2785
Definition: NodeManager.h:45
int64_t * mPtr[3]
Definition: NodeManager.h:48
uint8_t mPadding[7]
Definition: NodeManager.h:49
int64_t mOff[3]
Definition: NodeManager.h:48
uint64_t mMagic
Definition: NodeManager.h:46
void * mGrid
Definition: NodeManager.h:47
uint8_t mLinear
Definition: NodeManager.h:49
Struct to derive node type from its level in a given grid, tree or root while preserving constness.
Definition: NanoVDB.h:2343