-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.odin
350 lines (268 loc) · 7.67 KB
/
main.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import "core:fmt"
import "core:os"
import "core:intrinsics"
import "core:math/linalg"
import "core:mem"
import "core:strings"
import "core:bytes"
// I/O helpers
write_ptr :: proc(b: ^bytes.Buffer, data: rawptr, #any_int len: int) {
bytes.buffer_write(b, mem.byte_slice(data, len));
}
write_slice :: proc(b: ^bytes.Buffer, s: []$T) {
bytes.buffer_write(b, mem.slice_data_cast([]byte, s));
}
write_u8 :: bytes.buffer_write_byte;
write_u16 :: proc(b: ^bytes.Buffer, v: u16) {
v := v;
write_ptr(b, &v, size_of(u16));
}
write_u32 :: proc(b: ^bytes.Buffer, v: u32) {
v := v;
write_ptr(b, &v, size_of(u32));
}
write_u64 :: proc(b: ^bytes.Buffer, v: u64) {
v := v;
write_ptr(b, &v, size_of(u64));
}
write_u128 :: proc(b: ^bytes.Buffer, v: u128) {
v := v;
write_ptr(b, &v, size_of(u128));
}
write_f64 :: proc(b: ^bytes.Buffer, v: f64) {
v := v;
write_ptr(b, &v, size_of(f64));
}
write_string :: bytes.buffer_write_string;
write_vec3i :: proc(b: ^bytes.Buffer, v: [3]i32) {
write_u32(b, transmute(u32)v[0]);
write_u32(b, transmute(u32)v[1]);
write_u32(b, transmute(u32)v[2]);
}
write_name :: proc(b: ^bytes.Buffer, s: string) {
write_u32(b, u32(len(s)));
write_string(b, s);
}
write_meta_string :: proc(b: ^bytes.Buffer, name: string, s: string) {
write_name(b, name);
write_name(b, "string");
write_name(b, s);
}
write_meta_bool :: proc(b: ^bytes.Buffer, name: string, v: bool) {
write_name(b, name);
write_name(b, "bool");
write_u32(b, 1); // one byte is used to store the bool
write_u8(b, v ? 1 : 0);
}
write_meta_vec3i :: proc(b: ^bytes.Buffer, name: string, v: [3]i32) {
write_name(b, name);
write_name(b, "vec3i");
write_u32(b, 3 * size_of(i32));
write_vec3i(b, v);
}
// Tree data structure
VDB :: struct {
node_5: Node_5,
}
Node_5 :: struct {
mask: [512]u64,
nodes_4: map[u32]^Node_4,
}
Node_4 :: struct {
mask: [64]u64,
nodes_3: map[u32]^Node_3,
}
Node_3 :: struct {
mask: [8]u64,
data: [512]f16,
}
get_bit_index_4 :: proc(p: [3]u32) -> u32 {
p := p & u32(4096-1);
idx_3d := [3]u32{p.x >> 7, p.y >> 7, p.z >> 7};
idx := idx_3d.z | (idx_3d.y << 5) | (idx_3d.x << 10);
return idx;
}
get_bit_index_3 :: proc(p: [3]u32) -> u32 {
p := p & u32(128-1);
idx_3d := [3]u32{p.x >> 3, p.y >> 3, p.z >> 3};
idx := idx_3d.z | (idx_3d.y << 4) | (idx_3d.x << 8);
return idx;
}
get_bit_index_0 :: proc(p: [3]u32) -> u32 {
p := p & u32(8-1);
idx_3d := [3]u32{p.x >> 0, p.y >> 0, p.z >> 0};
idx := idx_3d.z | (idx_3d.y << 3) | (idx_3d.x << 6);
return idx;
}
set_voxel :: proc(vdb: ^VDB, p: [3]u32, v: f16) {
node_5 := &vdb.node_5;
bit_index_4 := get_bit_index_4(p);
bit_index_3 := get_bit_index_3(p);
bit_index_0 := get_bit_index_0(p);
node_4, node_4_found := node_5.nodes_4[bit_index_4];
if !node_4_found {
node_4 = new(Node_4);
map_insert(&node_5.nodes_4, bit_index_4, node_4);
}
node_3, node_3_found := node_4.nodes_3[bit_index_3];
if !node_3_found {
node_3 = new(Node_3);
map_insert(&node_4.nodes_3, bit_index_3, node_3);
}
node_5.mask[bit_index_4 >> 6] |= 1 << (bit_index_4 & (64-1));
node_4.mask[bit_index_3 >> 6] |= 1 << (bit_index_3 & (64-1));
node_3.mask[bit_index_0 >> 6] |= 1 << (bit_index_0 & (64-1));
node_3.data[bit_index_0] = v;
}
// Routines for writing the actual format
write_node_5_header :: proc(b: ^bytes.Buffer, node: ^Node_5) {
// Origin of the 5-node
write_vec3i(b, {0, 0, 0});
// Child masks
for word in node.mask do write_u64(b, word);
// Value masks are zero for now
for _ in node.mask do write_u64(b, 0);
// Write uncompressed node values, 6 means no compression
write_u8(b, 6);
for i := 0; i < 32768; i += 1 do write_u16(b, 0);
}
write_node_4_header :: proc(b: ^bytes.Buffer, node: ^Node_4) {
// Child masks
for word in node.mask do write_u64(b, word);
// Value masks are zero for now
for _ in node.mask do write_u64(b, 0);
// Write uncompressed node values, 6 means no compression
write_u8(b, 6);
for i := 0; i < 4096; i += 1 do write_u16(b, 0);
}
write_tree :: proc(b: ^bytes.Buffer, vdb: ^VDB) {
// We need to write a 1, apparently
write_u32(b, 1);
// Root node background value
write_u32(b, 0);
// Number of tiles
write_u32(b, 0);
// Number of 5-nodes
write_u32(b, 1);
node_5 := &vdb.node_5;
write_node_5_header(b, node_5);
// Iterate 4-nodes
for word, word_index in node_5.mask {
for word := word; word != 0; word &= word - 1 {
bit_index := u32(word_index) * 64 + u32(intrinsics.count_trailing_zeros(word));
node_4, node_4_found := node_5.nodes_4[bit_index];
assert(node_4_found);
write_node_4_header(b, node_4);
// Iterate 3-nodes
for word, word_index in node_4.mask {
for word := word; word != 0; word &= word - 1 {
bit_index := u32(word_index) * 64 + u32(intrinsics.count_trailing_zeros(word));
node_3, node_3_found := node_4.nodes_3[bit_index];
assert(node_3_found);
for word in node_3.mask do write_u64(b, word);
}
}
}
}
// Iterate 4-nodes
for word, word_index in node_5.mask {
for word := word; word != 0; word &= word - 1 {
bit_index := u32(word_index) * 64 + u32(intrinsics.count_trailing_zeros(word));
node_4, node_4_found := node_5.nodes_4[bit_index];
assert(node_4_found);
// Iterate 3-nodes
for word, word_index in node_4.mask {
for word := word; word != 0; word &= word - 1 {
bit_index := u32(word_index) * 64 + u32(intrinsics.count_trailing_zeros(word));
node_3, node_3_found := node_4.nodes_3[bit_index];
assert(node_3_found);
for word in node_3.mask do write_u64(b, word);
write_u8(b, 6);
write_slice(b, node_3.data[:]);
}
}
}
}
}
write_metadata :: proc(b: ^bytes.Buffer) {
// Number of entries
write_u32(b, 4);
write_meta_string(b, "class", "unknown");
write_meta_string(b, "file_compression", "none");
write_meta_bool(b, "is_saved_as_half_float", true);
write_meta_string(b, "name", "density");
}
write_transform :: proc(b: ^bytes.Buffer, mat: matrix[4, 4]f64) {
write_name(b, "AffineMap");
write_f64(b, mat[0, 0]);
write_f64(b, mat[1, 0]);
write_f64(b, mat[2, 0]);
write_f64(b, 0);
write_f64(b, mat[0, 1]);
write_f64(b, mat[1, 1]);
write_f64(b, mat[2, 1]);
write_f64(b, 0);
write_f64(b, mat[0, 2]);
write_f64(b, mat[1, 2]);
write_f64(b, mat[2, 2]);
write_f64(b, 0);
write_f64(b, mat[0, 3]);
write_f64(b, mat[1, 3]);
write_f64(b, mat[2, 3]);
write_f64(b, 1);
}
write_grid :: proc(b: ^bytes.Buffer, vdb: ^VDB, mat: matrix[4, 4]f64) {
// Grid name
write_name(b, "density");
// Grid type
write_name(b, "Tree_float_5_4_3_HalfFloat");
// No instance parent
write_u32(b, 0);
// Grid descriptor stream position
write_u64(b, u64(len(b.buf)) + size_of(u64) * 3);
write_u64(b, 0);
write_u64(b, 0);
// No compression
write_u32(b, 0);
write_metadata(b);
write_transform(b, mat);
write_tree(b, vdb);
}
write_vdb :: proc(b: ^bytes.Buffer, vdb: ^VDB, mat: matrix[4, 4]f64) {
// Magic number
write_slice(b, []byte{0x20, 0x42, 0x44, 0x56, 0x0, 0x0, 0x0, 0x0});
// File version
write_u32(b, 224);
// Library version (we're just gonna pretend we're OpenVDB 8.1)
write_u32(b, 8); // major
write_u32(b, 1); // minor
// We do not have grid offsets
write_u8(b, 0);
// Temporary UUID
write_string(b, "d2b59639-ac2f-4047-9c50-9648f951180c");
// No metadata for now
write_u32(b, 0);
// One grid
write_u32(b, 1);
write_grid(b, vdb, mat);
}
main :: proc() {
b: bytes.Buffer;
vdb: VDB;
R :: 128;
D :: R * 2;
for z in u32(0)..<D {
for y in u32(0)..<D {
for x in u32(0)..<D {
p := linalg.to_f32([3]u32{x, y, z});
if linalg.length2(p - R) < R*R {
set_voxel(&vdb, {x, y, z}, 1.0);
}
}
}
}
write_vdb(&b, &vdb, linalg.MATRIX4F64_IDENTITY);
ok := os.write_entire_file("test.vdb", b.buf[:]);
if !ok do fmt.println("Failed to write file.");
}