OpenVDB: nanovdb/unittest/pnanovdb_validate_strides.h Source File

OpenVDB 13.0.0

Loading...
Searching...
No Matches
pnanovdb_validate_strides.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: Apache-2.0
3
4/*!
5 \file pnanovdb_validate_strides.h
6
7 \author Andrew Reidmeyer
8
9 \brief This header implements validation tests for the strides used
10 in PNanaoVDB.h (instead of pointers). It can be used both for
11 unit-testing (hence its location), but also to update PNanoVDB.h
12 if the ABI changes in NanoVDB.h.
13*/
14
15#ifndef NANOVDB_PNANOVDB_VALIDATE_STRIDES_H_HAS_BEEN_INCLUDED
16#define NANOVDB_PNANOVDB_VALIDATE_STRIDES_H_HAS_BEEN_INCLUDED
17
18#include <nanovdb/PNanoVDB.h>
19
20 static pnanovdb_uint32_t allocate(pnanovdb_uint32_t* poffset, pnanovdb_uint32_t size, pnanovdb_uint32_t alignment)
21{
22 if (alignment > 0u)
23 {
24 (*poffset) = alignment * (((*poffset) + (alignment - 1)) / alignment);
25 }
26 pnanovdb_uint32_t ret = (*poffset);
27 (*poffset) += size;
28 return ret;
29}
30
31 static void compute_root_strides(
32 pnanovdb_uint32_t grid_type,
33 pnanovdb_uint32_t* background_off,
34 pnanovdb_uint32_t* min_off, pnanovdb_uint32_t* max_off,
35 pnanovdb_uint32_t* ave_off, pnanovdb_uint32_t* stddev_off,
36 pnanovdb_uint32_t* total_size)
37{
38 pnanovdb_uint32_t offset = 0u;
39 allocate(&offset, PNANOVDB_ROOT_BASE_SIZE, 32u);
40
41 pnanovdb_uint32_t minmaxStride = pnanovdb_grid_type_minmax_strides_bits[grid_type] / 8u;
42 pnanovdb_uint32_t minmaxAlign = pnanovdb_grid_type_minmax_aligns_bits[grid_type] / 8u;
43 pnanovdb_uint32_t statStride = pnanovdb_grid_type_stat_strides_bits[grid_type] / 8u;
44
45 *background_off = allocate(&offset, minmaxStride, minmaxAlign);
46 *min_off = allocate(&offset, minmaxStride, minmaxAlign);
47 *max_off = allocate(&offset, minmaxStride, minmaxAlign);
48 *ave_off = allocate(&offset, statStride, statStride);
49 *stddev_off = allocate(&offset, statStride, statStride);
50 *total_size = allocate(&offset, 0u, 32u);
51}
52
53 static void compute_tile_strides(pnanovdb_uint32_t grid_type, pnanovdb_uint32_t* value_off, pnanovdb_uint32_t* total_size)
54{
55 pnanovdb_uint32_t offset = 0u;
56 allocate(&offset, PNANOVDB_ROOT_TILE_BASE_SIZE, 32u);
57
58 pnanovdb_uint32_t valueStride = pnanovdb_grid_type_minmax_strides_bits[grid_type] / 8u;
59 pnanovdb_uint32_t valueAlign = pnanovdb_grid_type_minmax_aligns_bits[grid_type] / 8u;
60
61 *value_off = allocate(&offset, valueStride, valueAlign);
62 *total_size = allocate(&offset, 0u, 32u);
63}
64
65 static void compute_node_strides(
66 pnanovdb_uint32_t grid_type,
67 pnanovdb_uint32_t nodeLevel,
68 pnanovdb_uint32_t* min_off, pnanovdb_uint32_t* max_off,
69 pnanovdb_uint32_t* ave_off, pnanovdb_uint32_t* stddev_off,
70 pnanovdb_uint32_t* table_off,
71 pnanovdb_uint32_t* total_size)
72{
73 static const pnanovdb_uint32_t node_size[3] = { PNANOVDB_LEAF_BASE_SIZE, PNANOVDB_LOWER_BASE_SIZE, PNANOVDB_UPPER_BASE_SIZE };
74 static const pnanovdb_uint32_t node_elements[3] = { PNANOVDB_LEAF_TABLE_COUNT, PNANOVDB_LOWER_TABLE_COUNT, PNANOVDB_UPPER_TABLE_COUNT };
75 pnanovdb_uint32_t offset = 0u;
76 allocate(&offset, node_size[nodeLevel], 32u);
77
78 pnanovdb_uint32_t valueStrideBits = pnanovdb_grid_type_value_strides_bits[grid_type];
79 pnanovdb_uint32_t tableStrideBits = nodeLevel == 0u ? valueStrideBits : pnanovdb_grid_type_table_strides_bits[grid_type];
80 pnanovdb_uint32_t tableAlign = 32u;
81 pnanovdb_uint32_t tableFullStride = (tableStrideBits * node_elements[nodeLevel]) / 8u;
82
83 pnanovdb_uint32_t minmaxStride = pnanovdb_grid_type_minmax_strides_bits[grid_type] / 8u;
84 pnanovdb_uint32_t minmaxAlign = pnanovdb_grid_type_minmax_aligns_bits[grid_type] / 8u;
85 pnanovdb_uint32_t statStride = pnanovdb_grid_type_stat_strides_bits[grid_type] / 8u;
86 if (nodeLevel == 0u)
87 {
88 if (pnanovdb_grid_type_leaf_type[grid_type] == PNANOVDB_LEAF_TYPE_LITE)
89 {
90 minmaxStride = 0u;
91 minmaxAlign = 0u;
92 statStride = 0u;
93 }
94 else if (pnanovdb_grid_type_leaf_type[grid_type] == PNANOVDB_LEAF_TYPE_FP)
95 {
96 minmaxStride = 2u;
97 minmaxAlign = 2u;
98 statStride = 2u;
99 // allocate minimum and quantum
100 allocate(&offset, 4u, 4u);
101 allocate(&offset, 4u, 4u);
102 }
103 else if (pnanovdb_grid_type_leaf_type[grid_type] == PNANOVDB_LEAF_TYPE_INDEX)
104 {
105 minmaxStride = 0u;
106 minmaxAlign = 0u;
107 statStride = 0u;
108 tableAlign = 8u;
109 tableFullStride = 8u;
110 }
111 else if (pnanovdb_grid_type_leaf_type[grid_type] == PNANOVDB_LEAF_TYPE_POINTINDEX)
112 {
113 minmaxStride = 8u;
114 minmaxAlign = 8u;
115 statStride = 0u;
116 tableAlign = 2u;
117 tableFullStride = (16u * node_elements[nodeLevel]) / 8u;
118 }
119 }
120 *min_off = allocate(&offset, minmaxStride, minmaxAlign);
121 *max_off = allocate(&offset, minmaxStride, minmaxAlign);
122 *ave_off = allocate(&offset, statStride, statStride);
123 *stddev_off = allocate(&offset, statStride, statStride);
124 *table_off = allocate(&offset, tableFullStride, tableAlign);
125 *total_size = allocate(&offset, 0u, 32u);
126}
127
128 static bool validate_strides(int(*local_printf)(const char* format, ...))
129{
130 pnanovdb_grid_type_constants_t constants[PNANOVDB_GRID_TYPE_END];
131
132 for (pnanovdb_uint32_t idx = 0u; idx < PNANOVDB_GRID_TYPE_END; idx++)
133 {
134
135 pnanovdb_uint32_t root_background, root_min, root_max, root_ave, root_stddev, root_size;
136 compute_root_strides(idx, &root_background, &root_min, &root_max, &root_ave, &root_stddev, &root_size);
137
138 pnanovdb_uint32_t tile_value, tile_size;
139 compute_tile_strides(idx, &tile_value, &tile_size);
140
141 pnanovdb_uint32_t upper_min, upper_max, upper_ave, upper_stddev, upper_table, upper_size;
142 compute_node_strides(idx, 2, &upper_min, &upper_max, &upper_ave, &upper_stddev, &upper_table, &upper_size);
143
144 pnanovdb_uint32_t lower_min, lower_max, lower_ave, lower_stddev, lower_table, lower_size;
145 compute_node_strides(idx, 1, &lower_min, &lower_max, &lower_ave, &lower_stddev, &lower_table, &lower_size);
146
147 pnanovdb_uint32_t leaf_min, leaf_max, leaf_ave, leaf_stddev, leaf_table, leaf_size;
148 compute_node_strides(idx, 0, &leaf_min, &leaf_max, &leaf_ave, &leaf_stddev, &leaf_table, &leaf_size);
149
150 pnanovdb_uint32_t valueStrideBits = pnanovdb_grid_type_value_strides_bits[idx];
151 pnanovdb_uint32_t tableStrideBits = pnanovdb_grid_type_table_strides_bits[idx];
152 pnanovdb_uint32_t tableStride = tableStrideBits / 8u;
153
154 // For FP, always return the base of the table
155 if (pnanovdb_grid_type_leaf_type[idx] == PNANOVDB_LEAF_TYPE_FP)
156 {
157 valueStrideBits = 0u;
158 }
159
160 pnanovdb_grid_type_constants_t local_constants = {
161 root_background, root_min, root_max, root_ave, root_stddev, root_size,
162 valueStrideBits, tableStride, tile_value, tile_size,
163 upper_min, upper_max, upper_ave, upper_stddev, upper_table, upper_size,
164 lower_min, lower_max, lower_ave, lower_stddev, lower_table, lower_size,
165 leaf_min, leaf_max, leaf_ave, leaf_stddev, leaf_table, leaf_size
166 };
167 constants[idx] = local_constants;
168 }
169
170 bool mismatch = false;
171 for (pnanovdb_uint32_t idx = 0u; idx < PNANOVDB_GRID_TYPE_END; idx++)
172 {
173 pnanovdb_grid_type_constants_t c = constants[idx];
174 pnanovdb_grid_type_constants_t t = pnanovdb_grid_type_constants[idx];
175 if (memcmp(&c, &t, sizeof(pnanovdb_grid_type_constants_t)) != 0)
176 {
177 mismatch = true;
178 }
179 }
180 if (mismatch)
181 {
182 local_printf("Error: Mismatch between constant tables.\n");
183 for (pnanovdb_uint32_t pass = 0u; pass < 2u; pass++)
184 {
185 if (pass == 0u)
186 {
187 local_printf("Printing expected values:\n");
188 }
189 else
190 {
191 local_printf("Printing current header values:\n");
192 }
193 for (pnanovdb_uint32_t idx = 0u; idx < PNANOVDB_GRID_TYPE_END; idx++)
194 {
195 pnanovdb_grid_type_constants_t c = (pass == 0u) ? constants[idx] : pnanovdb_grid_type_constants[idx];
196 local_printf("{%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d},\n",
197 c.root_off_background, c.root_off_min, c.root_off_max, c.root_off_ave, c.root_off_stddev, c.root_size,
198 c.value_stride_bits, c.table_stride, c.root_tile_off_value, c.root_tile_size,
199 c.upper_off_min, c.upper_off_max, c.upper_off_ave, c.upper_off_stddev, c.upper_off_table, c.upper_size,
200 c.lower_off_min, c.lower_off_max, c.lower_off_ave, c.lower_off_stddev, c.lower_off_table, c.lower_size,
201 c.leaf_off_min, c.leaf_off_max, c.leaf_off_ave, c.leaf_off_stddev, c.leaf_off_table, c.leaf_size
202 );
203 }
204 }
205 }
206 return !mismatch;
207}
208
209#endif// end of NANOVDB_PNANOVDB_VALIDATE_STRIDES_H_HAS_BEEN_INCLUDED
PNanoVDB.h
This file is a portable (e.g. pointer-less) C99/GLSL/HLSL port of NanoVDB.h, which is compatible with...
PNANOVDB_LEAF_TYPE_LITE
#define PNANOVDB_LEAF_TYPE_LITE
Definition PNanoVDB.h:1145
pnanovdb_grid_type_value_strides_bits
PNANOVDB_STATIC_CONST pnanovdb_uint32_t pnanovdb_grid_type_value_strides_bits[32]
Definition PNanoVDB.h:1153
pnanovdb_grid_type_table_strides_bits
PNANOVDB_STATIC_CONST pnanovdb_uint32_t pnanovdb_grid_type_table_strides_bits[32]
Definition PNanoVDB.h:1155
pnanovdb_grid_type_minmax_strides_bits
PNANOVDB_STATIC_CONST pnanovdb_uint32_t pnanovdb_grid_type_minmax_strides_bits[32]
Definition PNanoVDB.h:1157
PNANOVDB_ROOT_BASE_SIZE
#define PNANOVDB_ROOT_BASE_SIZE
Definition PNanoVDB.h:1613
pnanovdb_grid_type_stat_strides_bits
PNANOVDB_STATIC_CONST pnanovdb_uint32_t pnanovdb_grid_type_stat_strides_bits[32]
Definition PNanoVDB.h:1161
PNANOVDB_LEAF_TYPE_FP
#define PNANOVDB_LEAF_TYPE_FP
Definition PNanoVDB.h:1146
pnanovdb_grid_type_minmax_aligns_bits
PNANOVDB_STATIC_CONST pnanovdb_uint32_t pnanovdb_grid_type_minmax_aligns_bits[32]
Definition PNanoVDB.h:1159
pnanovdb_grid_type_constants
PNANOVDB_STATIC_CONST pnanovdb_grid_type_constants_t pnanovdb_grid_type_constants[32]
Definition PNanoVDB.h:1883
PNANOVDB_UPPER_BASE_SIZE
#define PNANOVDB_UPPER_BASE_SIZE
Definition PNanoVDB.h:1692
PNANOVDB_LOWER_BASE_SIZE
#define PNANOVDB_LOWER_BASE_SIZE
Definition PNanoVDB.h:1754
PNANOVDB_GRID_TYPE_END
#define PNANOVDB_GRID_TYPE_END
Definition PNanoVDB.h:1120
PNANOVDB_LEAF_TYPE_INDEX
#define PNANOVDB_LEAF_TYPE_INDEX
Definition PNanoVDB.h:1147
PNANOVDB_ROOT_TILE_BASE_SIZE
#define PNANOVDB_ROOT_TILE_BASE_SIZE
Definition PNanoVDB.h:1651
PNANOVDB_LOWER_TABLE_COUNT
#define PNANOVDB_LOWER_TABLE_COUNT
Definition PNanoVDB.h:1753
PNANOVDB_LEAF_TABLE_COUNT
#define PNANOVDB_LEAF_TABLE_COUNT
Definition PNanoVDB.h:1813
PNANOVDB_UPPER_TABLE_COUNT
#define PNANOVDB_UPPER_TABLE_COUNT
Definition PNanoVDB.h:1691
pnanovdb_grid_type_leaf_type
PNANOVDB_STATIC_CONST pnanovdb_uint32_t pnanovdb_grid_type_leaf_type[32]
Definition PNanoVDB.h:1163
PNANOVDB_LEAF_BASE_SIZE
#define PNANOVDB_LEAF_BASE_SIZE
Definition PNanoVDB.h:1814
PNANOVDB_LEAF_TYPE_POINTINDEX
#define PNANOVDB_LEAF_TYPE_POINTINDEX
Definition PNanoVDB.h:1149
compute_root_strides
static void compute_root_strides(pnanovdb_uint32_t grid_type, pnanovdb_uint32_t *background_off, pnanovdb_uint32_t *min_off, pnanovdb_uint32_t *max_off, pnanovdb_uint32_t *ave_off, pnanovdb_uint32_t *stddev_off, pnanovdb_uint32_t *total_size)
Definition pnanovdb_validate_strides.h:31
compute_node_strides
static void compute_node_strides(pnanovdb_uint32_t grid_type, pnanovdb_uint32_t nodeLevel, pnanovdb_uint32_t *min_off, pnanovdb_uint32_t *max_off, pnanovdb_uint32_t *ave_off, pnanovdb_uint32_t *stddev_off, pnanovdb_uint32_t *table_off, pnanovdb_uint32_t *total_size)
Definition pnanovdb_validate_strides.h:65
allocate
static pnanovdb_uint32_t allocate(pnanovdb_uint32_t *poffset, pnanovdb_uint32_t size, pnanovdb_uint32_t alignment)
Definition pnanovdb_validate_strides.h:20
compute_tile_strides
static void compute_tile_strides(pnanovdb_uint32_t grid_type, pnanovdb_uint32_t *value_off, pnanovdb_uint32_t *total_size)
Definition pnanovdb_validate_strides.h:53
validate_strides
static bool validate_strides(int(*local_printf)(const char *format,...))
Definition pnanovdb_validate_strides.h:128
pnanovdb_grid_type_constants_t
Definition PNanoVDB.h:1850
pnanovdb_grid_type_constants_t::leaf_off_stddev
pnanovdb_uint32_t leaf_off_stddev
Definition PNanoVDB.h:1876
pnanovdb_grid_type_constants_t::leaf_off_ave
pnanovdb_uint32_t leaf_off_ave
Definition PNanoVDB.h:1875
pnanovdb_grid_type_constants_t::upper_off_min
pnanovdb_uint32_t upper_off_min
Definition PNanoVDB.h:1861
pnanovdb_grid_type_constants_t::leaf_size
pnanovdb_uint32_t leaf_size
Definition PNanoVDB.h:1878
pnanovdb_grid_type_constants_t::root_size
pnanovdb_uint32_t root_size
Definition PNanoVDB.h:1856
pnanovdb_grid_type_constants_t::leaf_off_table
pnanovdb_uint32_t leaf_off_table
Definition PNanoVDB.h:1877
pnanovdb_grid_type_constants_t::upper_off_max
pnanovdb_uint32_t upper_off_max
Definition PNanoVDB.h:1862
pnanovdb_grid_type_constants_t::upper_off_ave
pnanovdb_uint32_t upper_off_ave
Definition PNanoVDB.h:1863
pnanovdb_grid_type_constants_t::table_stride
pnanovdb_uint32_t table_stride
Definition PNanoVDB.h:1858
pnanovdb_grid_type_constants_t::upper_off_table
pnanovdb_uint32_t upper_off_table
Definition PNanoVDB.h:1865
pnanovdb_grid_type_constants_t::root_tile_size
pnanovdb_uint32_t root_tile_size
Definition PNanoVDB.h:1860
pnanovdb_grid_type_constants_t::leaf_off_min
pnanovdb_uint32_t leaf_off_min
Definition PNanoVDB.h:1873
pnanovdb_grid_type_constants_t::lower_size
pnanovdb_uint32_t lower_size
Definition PNanoVDB.h:1872
pnanovdb_grid_type_constants_t::lower_off_min
pnanovdb_uint32_t lower_off_min
Definition PNanoVDB.h:1867
pnanovdb_grid_type_constants_t::upper_size
pnanovdb_uint32_t upper_size
Definition PNanoVDB.h:1866
pnanovdb_grid_type_constants_t::value_stride_bits
pnanovdb_uint32_t value_stride_bits
Definition PNanoVDB.h:1857
pnanovdb_grid_type_constants_t::root_off_min
pnanovdb_uint32_t root_off_min
Definition PNanoVDB.h:1852
pnanovdb_grid_type_constants_t::root_tile_off_value
pnanovdb_uint32_t root_tile_off_value
Definition PNanoVDB.h:1859
pnanovdb_grid_type_constants_t::root_off_ave
pnanovdb_uint32_t root_off_ave
Definition PNanoVDB.h:1854
pnanovdb_grid_type_constants_t::lower_off_stddev
pnanovdb_uint32_t lower_off_stddev
Definition PNanoVDB.h:1870
pnanovdb_grid_type_constants_t::root_off_stddev
pnanovdb_uint32_t root_off_stddev
Definition PNanoVDB.h:1855
pnanovdb_grid_type_constants_t::root_off_background
pnanovdb_uint32_t root_off_background
Definition PNanoVDB.h:1851
pnanovdb_grid_type_constants_t::root_off_max
pnanovdb_uint32_t root_off_max
Definition PNanoVDB.h:1853
pnanovdb_grid_type_constants_t::lower_off_table
pnanovdb_uint32_t lower_off_table
Definition PNanoVDB.h:1871
pnanovdb_grid_type_constants_t::lower_off_max
pnanovdb_uint32_t lower_off_max
Definition PNanoVDB.h:1868
pnanovdb_grid_type_constants_t::leaf_off_max
pnanovdb_uint32_t leaf_off_max
Definition PNanoVDB.h:1874
pnanovdb_grid_type_constants_t::upper_off_stddev
pnanovdb_uint32_t upper_off_stddev
Definition PNanoVDB.h:1864
pnanovdb_grid_type_constants_t::lower_off_ave
pnanovdb_uint32_t lower_off_ave
Definition PNanoVDB.h:1869