14 #ifndef NANOVDB_GRIDCHECKSUM_H_HAS_BEEN_INCLUDED 15 #define NANOVDB_GRIDCHECKSUM_H_HAS_BEEN_INCLUDED 23 #include <type_traits> 33 #define NANOVDB_CRC32_LOG2_BLOCK_SIZE 12 49 template <
typename BuildT>
57 template <
typename BuildT>
64 template <
typename BuildT>
74 uint32_t &cs = lut[n] = n;
75 for (
int i = 0; i < 8; ++i) cs = (cs >> 1) ^ ((cs & 1) ? 0xEDB88320 : 0);
86 std::unique_ptr<uint32_t[]> lut(
new uint32_t[256]);
99 for (
auto *p = (
const uint8_t*)data, *q = p + size; p != q; ++p) {
101 for (
int j = 0; j < 8; ++j) crc = (crc >> 1) ^ (0xEDB88320 & (-(crc & 1)));
115 return checksum(begin, (
const char*)end - (
const char*)begin, crc);
124 inline __hostdev__ uint32_t
checksum(
const void *data,
size_t size,
const uint32_t lut[256], uint32_t crc = 0)
127 for (
auto *p = (
const uint8_t*)data, *q = p + size; p != q; ++p) crc = lut[(crc ^ *p) & 0xFF] ^ (crc >> 8);
137 inline __hostdev__ uint32_t
checksum(
const void *begin,
const void *end,
const uint32_t lut[256], uint32_t crc = 0)
141 return checksum(begin, (
const char*)end - (
const char*)begin, lut, crc);
154 union {uint32_t mCRC[2]; uint64_t mChecksum; };
155 static constexpr uint32_t EMPTY32 = ~uint32_t{0};
159 static constexpr uint64_t EMPTY = ~uint64_t(0);
191 bool isPartial()
const {
return mCRC[0] != EMPTY32 && mCRC[1] == EMPTY32; }
194 bool isFull()
const {
return mCRC[0] != EMPTY32 && mCRC[1] != EMPTY32; }
197 bool isEmpty()
const {
return mChecksum == EMPTY; }
205 #ifdef NANOVDB_CRC32_LOG2_BLOCK_SIZE 215 template <
typename ValueT>
229 #ifdef NANOVDB_CRC32_LOG2_BLOCK_SIZE 238 const uint8_t *begin = (
const uint8_t*)(&gridData), *mid = gridData.template nodePtr<2>(), *end = begin + gridData.
mGridSize;
239 if (mid ==
nullptr) {
250 uint64_t size = end - mid;
252 std::unique_ptr<uint32_t[]> checksums(
new uint32_t[blockCount]);
255 uint32_t *p = checksums.get() + r.begin();
256 for (
auto i = r.begin(); i != r.end(); ++i) {
258 *p++ =
crc32::checksum(mid + (i<<NANOVDB_CRC32_LOG2_BLOCK_SIZE), blockSize, lut.get());
261 mCRC[1] =
crc32::checksum(checksums.get(),
sizeof(uint32_t)*blockCount, lut.get());
266 #else// NANOVDB_CRC32_LOG2_BLOCK_SIZE 268 template <
typename ValueT>
272 static_assert(offsetof(
GridData, mMagic) == 0,
"Unexpected offset to magic number");
273 static_assert(offsetof(
GridData, mChecksum) == 8,
"Unexpected offset to checksum");
274 static_assert(offsetof(
GridData, mVersion) == 16,
"Unexpected offset to version number");
281 const uint8_t *begin =
reinterpret_cast<const uint8_t*
>(&grid), *mid = grid.template nodePtr<2>();
287 const auto &tree = grid.
tree();
288 const auto &root = tree.root();
290 auto *nodeMgr = nodeMgrHandle.template mgr<ValueT>();
292 const auto nodeCount = tree.nodeCount(0) + tree.nodeCount(1) + tree.nodeCount(2);
293 std::vector<uint32_t> checksums(nodeCount, 0);
295 auto kernel2 = [&](
const Range1D &r) {
296 uint32_t *p = checksums.data() + r.begin();
297 for (
auto i = r.begin(); i != r.end(); ++i) {
298 const auto &node = nodeMgr->upper(static_cast<uint32_t>(i));
303 auto kernel1 = [&](
const Range1D &r) {
304 uint32_t *p = checksums.data() + r.begin() + tree.nodeCount(2);
305 for (
auto i = r.begin(); i != r.end(); ++i) {
306 const auto &node = nodeMgr->lower(static_cast<uint32_t>(i));
311 auto kernel0 = [&](
const Range1D &r) {
312 uint32_t *p = checksums.data() + r.begin() + tree.nodeCount(1) + tree.nodeCount(2);
313 for (
auto i = r.begin(); i != r.end(); ++i) {
314 const auto &leaf = nodeMgr->leaf(static_cast<uint32_t>(i));
318 forEach(0, tree.nodeCount(2), 1, kernel2);
319 forEach(0, tree.nodeCount(1), 1, kernel1);
320 forEach(0, tree.nodeCount(0), 8, kernel0);
321 mCRC[1] =
crc32::checksum(checksums.data(),
sizeof(uint32_t)*checksums.size(), lut.get());
324 #endif// NANOVDB_CRC32_LOG2_BLOCK_SIZE 326 template <
typename ValueT>
334 template <
typename ValueT>
338 cs2(grid, cs1.mode() );
342 template <
typename ValueT>
352 #ifdef NANOVDB_CRC32_LOG2_BLOCK_SIZE 358 switch (data->mGridType){
423 std::stringstream ss;
424 ss <<
"Cannot update checksum for grid of unknown type \"" <<
toStr(data->mGridType);
425 throw std::runtime_error(ss.str() +
"\"");
438 const auto mode = cs.
mode();
454 if (cs.
isEmpty())
return false;
462 #endif // NANOVDB_GRIDCHECKSUM_H_HAS_BEEN_INCLUDED bool updateGridCount(GridData *data, uint32_t gridIndex, uint32_t gridCount)
Updates the ground index and count, as well as the partial checksum if needed.
Definition: GridChecksum.h:447
bool isEmpty() const
test if the grid is empty, e.i the root table has size 0
Definition: NanoVDB.h:3679
__hostdev__ uint32_t checksum(const void *data, size_t size, uint32_t crc=0)
Compute crc32 checksum of data of size bytes (without a lookup table))
Definition: GridChecksum.h:96
Highest level of the data structure. Contains a tree and a world->index transform (that currently onl...
Definition: NanoVDB.h:3698
__hostdev__ void initLut(uint32_t lut[256])
Initiate entire look-up-table for CRC32 computations.
Definition: GridChecksum.h:80
A unified wrapper for tbb::parallel_for and a naive std::thread fallback.
ChecksumMode
List of different modes for computing for a checksum.
Definition: GridChecksum.h:38
uint64_t mGridSize
Definition: NanoVDB.h:3520
Class that encapsulates two CRC32 checksums, one for the Grid, Tree and Root node meta data and one f...
Definition: GridChecksum.h:148
uint32_t mGridCount
Definition: NanoVDB.h:3519
const TreeT & tree() const
Return a const reference to the tree.
Definition: NanoVDB.h:3753
#define NANOVDB_CRC32_LOG2_BLOCK_SIZE
Definition: GridChecksum.h:33
void forEach(RangeT range, const FuncT &func)
simple wrapper for tbb::parallel_for with a naive std fallback
Definition: ForEach.h:40
bool validateChecksum(const NanoGrid< BuildT > &grid, ChecksumMode mode=ChecksumMode::Default)
Return true if the checksum of the grid matches the expected value already encoded into the grid's me...
bool operator!=(const GridChecksum &rhs) const
return true if the checksums are not identical
Definition: GridChecksum.h:224
Implements a light-weight self-contained VDB data-structure in a single file! In other words...
Definition: NanoVDB.h:247
uint64_t mChecksum
Definition: NanoVDB.h:3515
bool operator==(const GridChecksum &rhs) const
return true if the checksums are identical
Definition: GridChecksum.h:220
uint64_t mChecksum
Definition: GridChecksum.h:154
std::unique_ptr< uint32_t[]> createLut()
Create and initiate entire look-up-table for CRC32 computations.
Definition: GridChecksum.h:84
GridChecksum(uint32_t head, uint32_t tail)
Constructor that allows the two 32bit checksums to be initiated explicitly.
Definition: GridChecksum.h:167
uint32_t mBlindMetadataCount
Definition: NanoVDB.h:3528
void updateChecksum(NanoGrid< BuildT > &grid, ChecksumMode mode=ChecksumMode::Default)
Updates the checksum of a grid.
uint64_t checksum(const NanoGrid< BuildT > &grid, ChecksumMode mode=ChecksumMode::Default)
Return the (2 x CRC32) checksum of the specified grid.
bool isEmpty() const
return true if the 64 bit checksum is disables (unset)
Definition: GridChecksum.h:197
int64_t mBlindMetadataOffset
Definition: NanoVDB.h:3527
uint32_t & checksum(int i)
return 32 bit (crc32) checksum of this instance
Definition: GridChecksum.h:183
bool isFull() const
return true if the 64 bit checksum is fill, i.e. of both had and nodes
Definition: GridChecksum.h:194
DataType * data()
Definition: NanoVDB.h:3722
uint32_t mGridIndex
Definition: NanoVDB.h:3518
const char * toStr(GridType gridType)
Maps a GridType to a c-string.
Definition: NanoVDB.h:349
#define NANOVDB_ASSERT(x)
Definition: NanoVDB.h:190
uint64_t checksum() const
Return checksum of the grid buffer.
Definition: NanoVDB.h:3864
Struct with all the member data of the Grid (useful during serialization of an openvdb grid) ...
Definition: NanoVDB.h:3511
bool isPartial() const
return true if the 64 bit checksum is partial, i.e. of head only
Definition: GridChecksum.h:191
GridChecksum(uint64_t checksum, ChecksumMode mode=ChecksumMode::Full)
Definition: GridChecksum.h:172
NodeManagerHandle< BufferT > createNodeManager(const NanoGrid< BuildT > &grid, const BufferT &buffer=BufferT())
brief Construct a NodeManager and return its handle
Definition: NodeManager.h:284
ChecksumMode operator()(const GridData &gridData, ChecksumMode mode=ChecksumMode::Full)
compute checksum of gridData using a 4KB blocked approach
Definition: GridChecksum.h:231
GridChecksum()
default constructor initiates checksum to EMPTY
Definition: GridChecksum.h:162
static bool isValid(const void *p)
return true if the specified pointer is aligned and not NULL
Definition: NanoVDB.h:743
#define __hostdev__
Definition: NanoVDB.h:213
ChecksumMode mode() const
return the mode of the 64 bit checksum
Definition: GridChecksum.h:200
uint32_t checksum(int i) const
return 32 bit (crc32) checksum of this instance
Definition: GridChecksum.h:188
uint64_t checksum() const
return the 64 bit checksum of this instance
Definition: GridChecksum.h:178