-
Notifications
You must be signed in to change notification settings - Fork 146
/
cuda_gvdb_particles.cuh
1344 lines (1118 loc) · 52.3 KB
/
cuda_gvdb_particles.cuh
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------------------------------------------------
// NVIDIA(R) GVDB VOXELS
// Copyright 2017, NVIDIA Corporation.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Version 1.0: Rama Hoetzlein, 5/1/2017
//----------------------------------------------------------------------------------
// GVDB Points
// - ClearNodeCounts - clear brick particle counts
// - InsertPoints - insert points into bricks
// - SplatPoints - splat points into bricks
#define RGBA2INT(r,g,b,a) ( uint((r)*255.0f) + (uint((g)*255.0f)<<8) + (uint((b)*255.0f)<<16) + (uint((a)*255.0f)<<24) )
#define CLR2INT(c) ( uint((c.x)*255.0f) + (uint((c.y)*255.0f)<<8) + (uint((c.z)*255.0f)<<16) + (uint((c.w)*255.0f)<<24 ) )
#define INT2CLR(c) ( make_float4( float(c & 0xFF)/255.0f, float((c>>8) & 0xFF)/255.0f, float((c>>16) & 0xFF)/255.0f, float((c>>24) & 0xFF)/255.0f ))
#define CLR2CHAR(c) ( make_uchar4( uchar(c.x*255.0f), uchar(c.y*255.0f), uchar(c.z*255.0f), uchar(c.w*255.0f) ))
#define CHAR2CLR(c) ( make_float4( float(c.x)/255.0f, float(c.y)/255.0f, float(c.z)/255.0f, float(c.w)/255.0f ))
extern "C" __global__ void gvdbInsertPoints ( VDBInfo* gvdb, int num_pnts, char* ppos, int pos_off, int pos_stride, int* pnode, int* poff, int* gcnt, float3 ptrans )
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)); // NOTE: +ptrans is below. Allows check for wpos.z==NOHIT
if ( wpos.z == NOHIT ) { pnode[i] = ID_UNDEFL; return; } // If position invalid, return.
float3 offs, vmin, vdel; // Get GVDB node at the particle point
uint64 nid;
VDBNode* node = getNodeAtPoint ( gvdb, wpos + ptrans, &offs, &vmin, &vdel, &nid );
if ( node == 0x0 ) { pnode[i] = ID_UNDEFL; return; } // If no brick at location, return.
__syncthreads();
pnode[i] = nid; // Place point in brick
poff[i] = atomicAdd ( &gcnt[nid], (uint) 1 ); // Increment brick pcount, and retrieve this point index at the same time
}
extern "C" __global__ void gvdbInsertSupportPoints ( VDBInfo* gvdb, int num_pnts, float offset, char* ppos, int pos_off, int pos_stride, int* pnode, int* poff, int* gcnt, char* pdir, int dir_off, int dir_stride, float3 ptrans )
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)); // NOTE: +ptrans is below. Allows check for wpos.z==NOHIT
float3 wdir = (*(float3*) (pdir + i*dir_stride + dir_off));
if ( wpos.z == NOHIT ) { pnode[i] = ID_UNDEFL; return; } // If position invalid, return.
float3 offs, vmin, vdel; // Get GVDB node at the particle point
uint64 nid;
VDBNode* node = getNodeAtPoint ( gvdb, wpos + ptrans + wdir * offset, &offs, &vmin, &vdel, &nid );
if ( node == 0x0 ) { pnode[i] = ID_UNDEFL; return; } // If no brick at location, return.
__syncthreads();
pnode[i] = nid; // Place point in brick
poff[i] = atomicAdd ( &gcnt[nid], (uint) 1 ); // Increment brick pcount, and retrieve this point index at the same time
}
extern "C" __global__ void gvdbSortPoints ( int num_pnts, char* ppos, int pos_off, int pos_stride, int* pnode, int* poff,
int num_nodes, int* gcnt, int* goff, float3* pout, float3 ptrans )
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
uint64 nid = pnode[i];
if ( nid > num_nodes ) return;
int ndx = goff[nid] + poff[i]; // sorted index = brick offset (for particle's nid) + particle offset in brick
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)) + ptrans ;
pout[ndx] = wpos;
}
inline __device__ int3 GetCoveringNode (float3 pos, int3 range)
{
int3 nodepos;
nodepos.x = ceil(pos.x / range.x) * range.x;
nodepos.y = ceil(pos.y / range.y) * range.y;
nodepos.z = ceil(pos.z / range.z) * range.z;
if ( pos.x < nodepos.x ) nodepos.x -= range.x;
if ( pos.y < nodepos.y ) nodepos.y -= range.y;
if ( pos.z < nodepos.z ) nodepos.z -= range.z;
return nodepos;
}
inline __device__ int3 GetCoveringNode (float3 pos, int range)
{
int3 nodepos;
nodepos.x = ceil(pos.x / range) * range;
nodepos.y = ceil(pos.y / range) * range;
nodepos.z = ceil(pos.z / range) * range;
if ( pos.x < nodepos.x ) nodepos.x -= range;
if ( pos.y < nodepos.y ) nodepos.y -= range;
if ( pos.z < nodepos.z ) nodepos.z -= range;
return nodepos;
}
inline __device__ bool IsBoxIntersection (int3 amin, int3 amax, int3 bmin, int3 bmax)
{
return (amin.x <= bmax.x && amax.x >= bmin.x) &&
(amin.y <= bmax.y && amax.y >= bmin.y) &&
(amin.z <= bmax.z && amax.z >= bmin.z);
}
extern "C" __global__ void gvdbSetFlagSubcell(VDBInfo* gvdb, int num_sc, int* sc_flag)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= num_sc) return;
VDBNode* node = getNode(gvdb, 0, i);
sc_flag[i] = (node->mParent == ID_UNDEF64) ? 0 : 1;
}
extern "C" __global__ void gvdbConvAndTransform ( int num_pnts, char* psrc, char psrcbits, char* pdest, char pdestbits,
float3 wmin, float3 wdelta, float3 trans, float3 scal )
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 pnt;
// unpack input format
switch ( psrcbits ) {
case 2: {
ushort* pbuf = (ushort*) (psrc+i*6);
pnt = make_float3( *pbuf, *(pbuf+1), *(pbuf+2) );
} break;
case 4: {
float* pbuf = (float*) (psrc+i*12);
pnt = make_float3( *pbuf, *(pbuf+1), *(pbuf+2) );
} break;
};
// scale and transform
pnt = (wmin + pnt * wdelta) * scal + trans;
*(float3*) (pdest + i*12) = pnt;
}
extern "C" __global__ void gvdbScalePntPos (int num_pnts, char* ppos, int pos_off, int pos_stride, float scale)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
(*(float3*) (ppos + i*pos_stride + pos_off)) *= scale;
}
extern "C" __global__ void gvdbInsertSubcell_fp16 (
VDBInfo* gvdb, int subcell_size, int sc_per_brick, int num_pnts,
int* sc_cnt, int* sc_offset, int* sc_mapping,
float3 pos_min, float3 pos_range, float3 vel_min, float3 vel_range,
int3 sc_range, float3 ptrans, int res, float radius,
char* ppos, int pos_off, int pos_stride, ushort3* sc_pnt_pos,
char* pvel, int vel_off, int vel_stride, ushort3* sc_pnt_vel,
char* pclr, int clr_off, int clr_stride, uint* sc_pnt_clr
)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if (i >= num_pnts) return;
float3 wpos = (*(float3*)(ppos + i*pos_stride + pos_off)) + ptrans; // NOTE: +ptrans is below. Allows check for wpos.z==NOHIT
if (wpos.z == NOHIT) return; // If position invalid, return.
if (wpos.x < 0 || wpos.y < 0 || wpos.z < 0) return; // robust test
float3 wvel = (pvel == 0x0) ? make_float3(0, 0, 0) : (*(float3*)(pvel + i*vel_stride + vel_off));
uint wclr = (pclr == 0x0) ? 0 : (*(uint*)(pclr + i*clr_stride + clr_off));
int scminx = (int(wpos.x - radius) / subcell_size) * subcell_size;
int scminy = (int(wpos.y - radius) / subcell_size) * subcell_size;
int scminz = (int(wpos.z - radius) / subcell_size) * subcell_size;
int scmaxx = (int(wpos.x + 1 + radius) / subcell_size) * subcell_size;
int scmaxy = (int(wpos.y + 1 + radius) / subcell_size) * subcell_size;
int scmaxz = (int(wpos.z + 1 + radius) / subcell_size) * subcell_size;
VDBNode* pnode;
int3 scPos, posInNode;
int pnodeId, localOffs, offset, sc_idx;
for (scPos.x = scminx; scPos.x <= scmaxx; scPos.x += subcell_size)
{
for (scPos.y = scminy; scPos.y <= scmaxy; scPos.y += subcell_size)
{
for (scPos.z = scminz; scPos.z <= scmaxz; scPos.z += subcell_size)
{
pnodeId = getPosLeafParent(gvdb, scPos);
if (pnodeId == ID_UNDEFL) continue;
//getNode ( 0, pnodeId);
pnode = (VDBNode*)(gvdb->nodelist[0] + pnodeId*gvdb->nodewid[0]);
posInNode = scPos - pnode->mPos;
posInNode.x /= subcell_size;
posInNode.y /= subcell_size;
posInNode.z /= subcell_size;
localOffs = (posInNode.z*res + posInNode.y)*res + posInNode.x;
sc_idx = sc_mapping[pnodeId] * sc_per_brick + localOffs;
offset = atomicAdd(&sc_cnt[sc_idx], (uint)1);
sc_pnt_pos[sc_offset[sc_idx] + offset] = (make_ushort3)(
(wpos.x - pos_min.x) / pos_range.x * 65535,
(wpos.y - pos_min.y) / pos_range.y * 65535,
(wpos.z - pos_min.z) / pos_range.z * 65535);
if (pvel != 0x0) sc_pnt_vel[sc_offset[sc_idx] + offset] = (make_ushort3)(
(wvel.x - vel_min.x) / vel_range.x * 65535,
(wvel.y - vel_min.y) / vel_range.y * 65535,
(wvel.z - vel_min.z) / vel_range.z * 65535);
if (pclr != 0x0) sc_pnt_clr[sc_offset[sc_idx] + offset] = wclr;
}
}
}
}
extern "C" __global__ void gvdbInsertSubcell (
VDBInfo* gvdb, int subcell_size, int sc_per_brick, int num_pnts,
int* sc_cnt, int* sc_offset, int* sc_mapping,
int3 sc_range, float3 ptrans, int res, float radius,
char* ppos, int pos_off, int pos_stride, float3* sc_pnt_pos,
char* pvel, int vel_off, int vel_stride, float3* sc_pnt_vel,
char* pclr, int clr_off, int clr_stride, uint* sc_pnt_clr
)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off));
if ( wpos.z == NOHIT ) return; // If position invalid, return.
if ( wpos.x < 0 || wpos.y < 0 || wpos.z < 0) return; // robust test
wpos += ptrans; // add ptrans here to allow check for NOHIT
float3 wvel = (pvel==0x0) ? make_float3(0,0,0) : (*(float3*) (pvel + i*vel_stride + vel_off));
uint wclr = (pclr== 0x0) ? 0 : (*(uint*)(pclr + i*clr_stride + clr_off));
int scminx = (int(wpos.x - radius) / subcell_size) * subcell_size;
int scminy = (int(wpos.y - radius) / subcell_size) * subcell_size;
int scminz = (int(wpos.z - radius) / subcell_size) * subcell_size;
int scmaxx = (int(wpos.x + 1 + radius) / subcell_size) * subcell_size;
int scmaxy = (int(wpos.y + 1 + radius) / subcell_size) * subcell_size;
int scmaxz = (int(wpos.z + 1 + radius) / subcell_size) * subcell_size;
VDBNode* pnode;
int3 scPos, posInNode;
int pnodeId, localOffs, offset, sc_idx;
for (scPos.x = scminx; scPos.x <= scmaxx; scPos.x += subcell_size)
{
for (scPos.y = scminy; scPos.y <= scmaxy; scPos.y += subcell_size)
{
for (scPos.z = scminz; scPos.z <= scmaxz; scPos.z += subcell_size)
{
pnodeId = getPosLeafParent( gvdb, scPos);
if (pnodeId == ID_UNDEFL) continue;
//getNode ( 0, pnodeId);
pnode = (VDBNode*) (gvdb->nodelist[0] + pnodeId*gvdb->nodewid[0]);
posInNode = scPos - pnode->mPos;
posInNode.x /= subcell_size;
posInNode.y /= subcell_size;
posInNode.z /= subcell_size;
localOffs = (posInNode.z*res + posInNode.y)*res+ posInNode.x;
sc_idx = sc_mapping[pnodeId] * sc_per_brick + localOffs;
offset = atomicAdd( &sc_cnt[sc_idx], (uint) 1);
sc_pnt_pos[sc_offset[sc_idx] + offset] = wpos;
if ( pvel != 0x0 ) sc_pnt_vel[sc_offset[sc_idx] + offset] = wvel;
if ( pclr != 0x0 ) sc_pnt_clr[sc_offset[sc_idx] + offset] = wclr;
}
}
}
}
extern "C" __global__ void gvdbCountSubcell (VDBInfo* gvdb, int subcell_size, int sc_per_brick, int num_pnts, char* ppos, int pos_off, int pos_stride,
int* sc_cnt, int3 sc_range, float3 ptrans, int res, float radius, int num_sc, int* sc_mapping)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off));
if ( wpos.z == NOHIT ) return; // If position invalid, return.
wpos += ptrans; // add here to allow check above for NOHIT
if ( wpos.x < 0 || wpos.y < 0 || wpos.z < 0) return; // robust test
int scminx = (int(wpos.x - radius) / subcell_size) * subcell_size;
int scminy = (int(wpos.y - radius) / subcell_size) * subcell_size;
int scminz = (int(wpos.z - radius) / subcell_size) * subcell_size;
int scmaxx = (int(wpos.x + 1 + radius) / subcell_size) * subcell_size;
int scmaxy = (int(wpos.y + 1 + radius) / subcell_size) * subcell_size;
int scmaxz = (int(wpos.z + 1 + radius) / subcell_size) * subcell_size;
VDBNode* pnode;
int3 scPos, posInNode;
int pnodeId, localOffs;
for (scPos.x = scminx; scPos.x <= scmaxx; scPos.x += subcell_size)
{
for (scPos.y = scminy; scPos.y <= scmaxy; scPos.y += subcell_size)
{
for (scPos.z = scminz; scPos.z <= scmaxz; scPos.z += subcell_size)
{
pnodeId = getPosLeafParent(gvdb, scPos);
if (pnodeId == ID_UNDEFL) continue;
pnode = (VDBNode*) (gvdb->nodelist[0] + pnodeId*gvdb->nodewid[0]);
posInNode = scPos - pnode->mPos;
posInNode.x /= subcell_size;
posInNode.y /= subcell_size;
posInNode.z /= subcell_size;
localOffs = (posInNode.z*res + posInNode.y)*res+ posInNode.x;
atomicAdd( &sc_cnt[sc_mapping[pnodeId] * sc_per_brick + localOffs], (uint) 1);
}
}
}
}
extern "C" __global__ void gvdbFindActivBricks (int num_pnts, int lev, int3 brick_range, int dim, char* ppos, int pos_off, int pos_stride, float3 orig, int3 orig_shift, int* pout)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)) + orig;
int3 brickPos = GetCoveringNode(wpos, brick_range);
int3 brickIdx3 = make_int3(brickPos.x / brick_range.x, brickPos.y / brick_range.y, brickPos.z / brick_range.z); // brick idx in global
int brickIdx = brickIdx3.x + brickIdx3.y * dim + brickIdx3.z * dim * dim;
//if ( brickIdx >= num_bricks) return;
//poff[i] = atomicAdd(&pout[brickIdx], 1);
pout[i] = brickIdx;
}
extern "C" __global__ void gvdbFindUnique ( int num_pnts, long long* pin, int* marker, int* uniqueCnt, int* levCnt)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
if ((i == 0) || (pin[i] != pin[i-1]))
{
marker[i] = 1; atomicAdd(&uniqueCnt[0],1);
int lev = ((pin[i] >> 48) & 0xFF);
if (lev == 255) return;
atomicAdd( &levCnt[lev], 1);
}
}
extern "C" __global__ void gvdbCalcBrickId ( int num_pnts, int lev_depth, int* range_res,
char* ppos, int pos_off, int pos_stride,
float3 orig, unsigned short* pout)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)) + orig;
if (wpos.x < 0 || wpos.y < 0 || wpos.z < 0)
{
for (unsigned short lev = 0; lev < lev_depth; lev++)
{
pout[i * lev_depth * 4 + lev * 4 + 3] = 255;
pout[i * lev_depth * 4 + lev * 4 + 2] = 0;
pout[i * lev_depth * 4 + lev * 4 + 1] = 0;
pout[i * lev_depth * 4 + lev * 4 + 0] = 0;
}
return;
}
for (unsigned short lev = 0; lev < lev_depth; lev++)
{
int3 brickPos = GetCoveringNode( wpos, range_res[lev]);
pout[i * lev_depth * 4 + lev * 4 + 3] = lev;
pout[i * lev_depth * 4 + lev * 4 + 2] = (unsigned short) (brickPos.x / range_res[lev]);
pout[i * lev_depth * 4 + lev * 4 + 1] = (unsigned short) (brickPos.y / range_res[lev]);
pout[i * lev_depth * 4 + lev * 4 + 0] = (unsigned short) (brickPos.z / range_res[lev]);
}
}
extern "C" __global__ void gvdbCalcIncreBrickId (VDBInfo* gvdb, float radius, int num_pnts, int lev_depth, int* range_res,
char* ppos, int pos_off, int pos_stride,
float3 orig, unsigned short* pout, int* exBrick_cnt,
int* node_markers
)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)) + orig;
if (wpos.x < 0 || wpos.y < 0 || wpos.z < 0) return;
int pnodeId, idx;
int3 brickPos;
for (unsigned short lev = 0; lev < lev_depth; lev++)
{
brickPos = GetCoveringNode(wpos, range_res[lev]);
pnodeId = getPosParent(gvdb, brickPos, lev);
if (pnodeId == ID_UNDEFL)
{
idx = atomicAdd ( &exBrick_cnt[0], (uint) 1 );
pout[idx * 4 + 0 ] = (unsigned short) (brickPos.z / range_res[lev]);
pout[idx * 4 + 1 ] = (unsigned short) (brickPos.y / range_res[lev]);
pout[idx * 4 + 2 ] = (unsigned short) (brickPos.x / range_res[lev]);
pout[idx * 4 + 3 ] = lev;
}
else
{
if(lev == 0) atomicOr ( &node_markers[pnodeId], true );
}
}
}
extern "C" __global__ void gvdbCalcIncreExtraBrickId (VDBInfo* gvdb, float radius, int num_pnts, int lev_depth, int* range_res,
char* ppos, int pos_off, int pos_stride,
float3 orig, unsigned short* pout, int* exBrick_cnt,
int* node_markers
)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)) + orig;
if (wpos.x < 0 || wpos.y < 0 || wpos.z < 0) return;
int3 bmin = make_int3(int(wpos.x) - radius, int(wpos.y) - radius, int(wpos.z) - radius);
int3 bmax = make_int3(int(wpos.x) + radius, int(wpos.y) + radius, int(wpos.z) + radius);
int3 ndPos;
int pnodeId, idx;
VDBNode* node;
//unsigned short levxyx[40];
//int ndCnt = 0;
for (unsigned short lev = 0; lev < lev_depth; lev++)
{
int rres = range_res[lev];
for (ndPos.x = bmin.x / rres * rres; ndPos.x <= bmax.x / rres * rres; ndPos.x += rres)
{
for (ndPos.y = bmin.y / rres * rres; ndPos.y <= bmax.y / rres * rres; ndPos.y += rres)
{
for (ndPos.z = bmin.z / rres * rres; ndPos.z <= bmax.z / rres * rres; ndPos.z += rres)
{
//ndPos = make_int3(ndx, ndy, ndz);
pnodeId = getPosParent(gvdb, ndPos, lev);
if (pnodeId == ID_UNDEFL)
{
idx = atomicAdd ( &exBrick_cnt[0], (uint) 1 );
pout[idx * 4 + 0 ] = (unsigned short) (ndPos.z / rres);
pout[idx * 4 + 1 ] = (unsigned short) (ndPos.y / rres);
pout[idx * 4 + 2 ] = (unsigned short) (ndPos.x / rres);
pout[idx * 4 + 3 ] = lev;
//levxyx[ndCnt * 4 + 0 ] = (ndPos.z / rres);
//levxyx[ndCnt * 4 + 1 ] = (ndPos.y / rres);
//levxyx[ndCnt * 4 + 2 ] = (ndPos.x / rres);
//levxyx[ndCnt * 4 + 3 ] = lev;
//ndCnt++;
} else {
if(lev == 0) atomicOr ( &node_markers[pnodeId], true );
}
}
}
}
}
//idx = atomicAdd ( &exBrick_cnt[0], (uint) ndCnt);
//for (int pi = 0; pi < ndCnt * 4; pi++) pout[idx * 4 + pi] = levxyx[pi];
//ndCnt = 0;
//for (unsigned short lev = 0; lev < lev_depth; lev++)
//{
// int rres = range_res[lev];
// for (ndPos.x = bmin.x / rres * rres; ndPos.x <= bmax.x / rres * rres; ndPos.x += rres)
// {
// for (ndPos.y = bmin.y / rres * rres; ndPos.y <= bmax.y / rres * rres; ndPos.y += rres)
// {
// for (ndPos.z = bmin.z / rres * rres; ndPos.z <= bmax.z / rres * rres; ndPos.z += rres)
// {
// //ndPos = make_int3(ndx, ndy, ndz);
// pnodeId = getPosParent(ndPos, lev);
// if (pnodeId == ID_UNDEFL)
// {
// //idx = atomicAdd ( &exBrick_cnt[0], (uint) 1 );
// pout[idx * 4 + 0 + ndCnt ] = (unsigned short) (ndPos.z / rres);
// pout[idx * 4 + 1 + ndCnt ] = (unsigned short) (ndPos.y / rres);
// pout[idx * 4 + 2 + ndCnt ] = (unsigned short) (ndPos.x / rres);
// pout[idx * 4 + 3 + ndCnt ] = lev;
// ndCnt += 4;
// }
// }
// }
// }
//}
}
extern "C" __global__ void gvdbCalcExtraBrickId (VDBInfo* gvdb, float radius, int num_pnts, int lev_depth, int* range_res,
char* ppos, int pos_off, int pos_stride,
float3 orig, unsigned short* pout, int* exBrick_cnt)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)) + orig;
if (wpos.x < 0 || wpos.y < 0 || wpos.z < 0) return;
//int3 bmin = make_int3(wpos.x - radius, wpos.y - radius, wpos.z - radius);
//int3 bmax = make_int3(wpos.x + radius, wpos.y + radius, wpos.z + radius);
int3 bmin = make_int3(int(wpos.x) - radius, int(wpos.y) - radius, int(wpos.z) - radius);
int3 bmax = make_int3(int(wpos.x) + 2 * radius, int(wpos.y) + 2 * radius, int(wpos.z) + 2 * radius);
//int3 ndmin, ndmax;
int3 ndPos;
int pnodeId, idx;
for (unsigned short lev = 0; lev < lev_depth; lev++)
{
//ndmin = make_int3(int(wpos.x - radius) / range_res[lev] * range_res[lev], int(wpos.y - radius) / range_res[lev] * range_res[lev], int(wpos.z - radius) / range_res[lev] * range_res[lev]);
//ndmax = make_int3(int(wpos.x + radius) / range_res[lev] * range_res[lev], int(wpos.y + radius) / range_res[lev] * range_res[lev], int(wpos.z + radius) / range_res[lev] * range_res[lev]);
int rres = range_res[lev];
for (ndPos.x = bmin.x / rres * rres; ndPos.x <= bmax.x / rres * rres; ndPos.x += rres)
{
for (ndPos.y = bmin.y / rres * rres; ndPos.y <= bmax.y / rres * rres; ndPos.y += rres)
{
for (ndPos.z = bmin.z / rres * rres; ndPos.z <= bmax.z / rres * rres; ndPos.z += rres)
{
//ndPos = make_int3(ndx, ndy, ndz);
pnodeId = getPosParent(gvdb, ndPos, lev);
if (pnodeId == ID_UNDEFL)
{
idx = atomicAdd ( &exBrick_cnt[0], (uint) 1 );
pout[idx * 4 + 0 ] = (unsigned short) (ndPos.z / rres);
pout[idx * 4 + 1 ] = (unsigned short) (ndPos.y / rres);
pout[idx * 4 + 2 ] = (unsigned short) (ndPos.x / rres);
pout[idx * 4 + 3 ] = lev;
}
}
}
}
}
}
inline __device__ float distFunc ( float3 a, float bx, float by, float bz, float r )
{
bx -= a.x; by -= a.y; bz -= a.z;
float c = (bx*bx+by*by+bz*bz) / (r*r);
return 1.0 + c*(-3 + c*(3-c));
//return (r - sqrt(bx*bx+by*by+bz*bz)) / r;
}
extern "C" __global__ void gvdbScatterPointDensity (VDBInfo* gvdb, int num_pnts, float radius, float amp, char* ppos, int pos_off, int pos_stride, char* pclr, int clr_off, int clr_stride, int* pnode, float3 ptrans, bool expand, uint* colorBuf)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
if ( pnode[i] == ID_UNDEFL ) return; // make sure point is inside a brick
// Get particle position in brick
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)) + ptrans;
float3 vmin;
float w;
VDBNode* node = getNode ( gvdb, 0, pnode[i], &vmin ); // Get node
float3 p = (wpos-vmin)/gvdb->vdel[0];
float3 pi = make_float3(int(p.x), int(p.y), int(p.z));
// range of pi.x,pi.y,pi.z = [0, gvdb->res0-1]
if ( pi.x < 0 || pi.y < 0 || pi.z < 0 || pi.x >= gvdb->res[0] || pi.y >= gvdb->res[0] || pi.z >= gvdb->res[0] ) return;
uint3 q = make_uint3(pi.x,pi.y,pi.z) + make_uint3( node->mValue );
w = tex3D<float>( gvdb->volIn[0], q.x,q.y,q.z ) + distFunc(p, pi.x, pi.y,pi.z, radius) ; surf3Dwrite ( w, gvdb->volOut[0], q.x*sizeof(float), q.y, q.z );
if ( expand ) {
w = tex3D<float> (gvdb->volIn[0], q.x-1,q.y,q.z) + distFunc(p, pi.x-1, pi.y, pi.z, radius); surf3Dwrite ( w, gvdb->volOut[0], (q.x-1)*sizeof(float), q.y, q.z );
w = tex3D<float> (gvdb->volIn[0], q.x+1,q.y,q.z) + distFunc(p, pi.x+1, pi.y, pi.z, radius); surf3Dwrite ( w, gvdb->volOut[0], (q.x+1)*sizeof(float), q.y, q.z );
w = tex3D<float> (gvdb->volIn[0], q.x,q.y-1,q.z) + distFunc(p, pi.x, pi.y-1, pi.z, radius); surf3Dwrite ( w, gvdb->volOut[0], q.x*sizeof(float), (q.y-1), q.z );
w = tex3D<float> (gvdb->volIn[0], q.x,q.y+1,q.z) + distFunc(p, pi.x, pi.y+1, pi.z, radius); surf3Dwrite ( w, gvdb->volOut[0], q.x*sizeof(float), (q.y+1), q.z );
w = tex3D<float> (gvdb->volIn[0], q.x,q.y,q.z-1) + distFunc(p, pi.x, pi.y, pi.z-1, radius); surf3Dwrite ( w, gvdb->volOut[0], q.x*sizeof(float), q.y, (q.z-1) );
w = tex3D<float> (gvdb->volIn[0], q.x,q.y,q.z+1) + distFunc(p, pi.x, pi.y, pi.z+1, radius); surf3Dwrite ( w, gvdb->volOut[0], q.x*sizeof(float), q.y, (q.z+1) );
}
if ( pclr != 0 ) {
uchar4 wclr = *(uchar4*) (pclr + i*clr_stride + clr_off );
if ( colorBuf != 0 ) {
// Increment index
uint brickres = gvdb->res[0];
uint vid = (brickres * brickres * brickres * pnode[i]) + (brickres * brickres * (uint)pi.z) + (brickres * (uint)pi.y) + (uint)pi.x;
uint colorIdx = vid * 4;
// Store in color in the colorbuf
atomicAdd(&colorBuf[colorIdx + 0], 1);
atomicAdd(&colorBuf[colorIdx + 1], wclr.x);
atomicAdd(&colorBuf[colorIdx + 2], wclr.y);
atomicAdd(&colorBuf[colorIdx + 3], wclr.z);
}
else {
surf3Dwrite(wclr, gvdb->volOut[3], q.x*sizeof(uchar4), q.y, q.z);
}
}
}
extern "C" __global__ void gvdbAddSupportVoxel (VDBInfo* gvdb, int num_pnts, float radius, float offset, float amp,
char* ppos, int pos_off, int pos_stride,
char* pdir, int dir_off, int dir_stride,
int* pnode, float3 ptrans, bool expand, uint* colorBuf)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= num_pnts ) return;
// Get brick ID
uint nid = pnode[i];
if ( nid == ID_UNDEFL ) return;
// Get particle position in brick
float3 wpos = (*(float3*) (ppos + i*pos_stride + pos_off)) + ptrans + (*(float3*) (pdir + i*dir_stride + dir_off)) * offset;
//wpos.y -=5.0;//threadIdx.y;
float3 vmin;
float w;
VDBNode* node = getNode ( gvdb, 0, pnode[i], &vmin ); // Get node
float3 p = (wpos-vmin)/gvdb->vdel[0];
float3 pi = make_float3(int(p.x), int(p.y), int(p.z));
// -- should be ok that pi.x,pi.y,pi.z = 0
if ( pi.x <= -1 || pi.y <= -1 || pi.z <= -1 || pi.x >= gvdb->res[0] || pi.y >= gvdb->res[0] || pi.z >= gvdb->res[0] ) return;
uint3 q = make_uint3(pi.x,pi.y,pi.z) + make_uint3( node->mValue );
w = tex3D<float>(gvdb->volIn[0], q.x, q.y, q.z ) + distFunc(p, pi.x, pi.y,pi.z, radius);
surf3Dwrite ( w, gvdb->volOut[0], q.x*sizeof(float), q.y, q.z );
surf3Dwrite ( (uchar)1, gvdb->volOut[1], q.x*sizeof(uchar), q.y, q.z );
//surf3Dwrite ( 1.0f, volOut[2], q.x*sizeof(float), q.y, q.z );
#if 1
// expand to 3x3 square, write to both volume and material channels
w = tex3D<float> (gvdb->volIn[0], q.x-1,q.y,q.z) + distFunc(p, pi.x-1, pi.y, pi.z, radius);
surf3Dwrite ( w, gvdb->volOut[0], (q.x-1)*sizeof(float), q.y, q.z );
surf3Dwrite ( (uchar)1, gvdb->volOut[1], (q.x-1)*sizeof(uchar), q.y, q.z );
//surf3Dwrite ( 1.0f, volOut[2], (q.x-1)*sizeof(float), q.y, q.z );
w = tex3D<float> (gvdb->volIn[0], q.x+1,q.y,q.z) + distFunc(p, pi.x+1, pi.y, pi.z, radius);
surf3Dwrite ( w, gvdb->volOut[0], (q.x+1)*sizeof(float), q.y, q.z );
surf3Dwrite ( (uchar)1, gvdb->volOut[1], (q.x+1)*sizeof(uchar), q.y, q.z );
//surf3Dwrite ( 1.0f, volOut[2], (q.x+1)*sizeof(float), q.y, q.z );
w = tex3D<float> (gvdb->volIn[0], q.x,q.y,q.z-1) + distFunc(p, pi.x, pi.y, pi.z-1, radius);
surf3Dwrite ( w, gvdb->volOut[0], q.x*sizeof(float), q.y, (q.z-1) );
surf3Dwrite ( (uchar)1, gvdb->volOut[1], q.x*sizeof(uchar), q.y, (q.z-1) );
//surf3Dwrite ( 1.0f, volOut[2], q.x*sizeof(float), q.y, (q.z-1) );
w = tex3D<float> (gvdb->volIn[0], q.x,q.y,q.z+1) + distFunc(p, pi.x, pi.y, pi.z+1, radius);
surf3Dwrite ( w, gvdb->volOut[0], q.x*sizeof(float), q.y, (q.z+1) );
surf3Dwrite ( (uchar)1, gvdb->volOut[1], q.x*sizeof(uchar), q.y, (q.z+1) );
//surf3Dwrite ( 1.0f, volOut[2], q.x*sizeof(float), q.y, (q.z+1) );
w = tex3D<float> (gvdb->volIn[0], q.x-1,q.y,q.z-1) + distFunc(p, pi.x-1, pi.y, pi.z-1, radius);
surf3Dwrite ( w, gvdb->volOut[0], (q.x-1)*sizeof(float), q.y, (q.z-1) );
surf3Dwrite ( (uchar)1, gvdb->volOut[1], (q.x-1)*sizeof(uchar), q.y, (q.z-1) );
//surf3Dwrite ( 1.0f, volOut[2], (q.x-1)*sizeof(float), q.y, (q.z-1) );
w = tex3D<float> (gvdb->volIn[0], q.x+1,q.y,q.z+1) + distFunc(p, pi.x+1, pi.y, pi.z+1, radius);
surf3Dwrite ( w, gvdb->volOut[0], (q.x+1)*sizeof(float), q.y, (q.z+1) );
surf3Dwrite ( (uchar)1, gvdb->volOut[1], (q.x+1)*sizeof(uchar), q.y, (q.z+1) );
//surf3Dwrite ( 1.0f, volOut[2], (q.x+1)*sizeof(float), q.y, (q.z+1) );
w = tex3D<float> (gvdb->volIn[0], q.x+1,q.y,q.z-1) + distFunc(p, pi.x+1, pi.y, pi.z-1, radius);
surf3Dwrite ( w, gvdb->volOut[0], (q.x+1)*sizeof(float), q.y, (q.z-1) );
surf3Dwrite ( (uchar)1, gvdb->volOut[1], (q.x+1)*sizeof(uchar), q.y, (q.z-1) );
//surf3Dwrite ( 1.0f, volOut[2], (q.x+1)*sizeof(float), q.y, (q.z-1) );
w = tex3D<float> (gvdb->volIn[0], q.x-1,q.y,q.z+1) + distFunc(p, pi.x-1, pi.y, pi.z+1, radius);
surf3Dwrite ( w, gvdb->volOut[0], (q.x-1)*sizeof(float), q.y, (q.z+1) );
surf3Dwrite ( (uchar)1, gvdb->volOut[1], (q.x-1)*sizeof(uchar), q.y, (q.z+1) );
//surf3Dwrite ( 1.0f, volOut[2], (q.x-1)*sizeof(float), q.y, (q.z+1) );
#endif
}
extern "C" __global__ void gvdbScatterPointAvgCol (VDBInfo* gvdb, int num_voxels, uint* colorBuf)
{
uint vid = blockIdx.x * blockDim.x + threadIdx.x;
if (vid >= num_voxels) return;
uint colorIdx = vid * 4;
uint count = colorBuf[colorIdx + 0];
if (count > 0)
{
// Average color dividing by count
uint colx = colorBuf[colorIdx + 1] / count;
uint coly = colorBuf[colorIdx + 2] / count;
uint colz = colorBuf[colorIdx + 3] / count;
uchar4 pclr = make_uchar4(colx, coly, colz, 255);
// Get node
uint brickres = gvdb->res[0];
uint nid = vid / (brickres * brickres * brickres);
float3 vmin;
VDBNode* node = getNode(gvdb, 0, nid, &vmin);
// Get local 3d indices
uint3 pi;
pi.x = vid % (brickres);
pi.y = vid % (brickres * brickres) / (brickres);
pi.z = vid % (brickres * brickres * brickres) / (brickres * brickres);
// Get global atlas index
uint3 q = make_uint3(pi.x, pi.y, pi.z) + make_uint3(node->mValue);
surf3Dwrite(pclr, gvdb->volOut[1], q.x*sizeof(uchar4), q.y, q.z);
}
}
extern "C" __global__ void gvdbReadGridVel (VDBInfo* gvdb, int cell_num, int3* cell_pos, float* cell_vel)
{
uint cid = blockIdx.x * blockDim.x + threadIdx.x;
if (cid >= cell_num) return;
float3 wpos = make_float3(cell_pos[cid].x, cell_pos[cid].y, cell_pos[cid].z);
float3 vmin, vdel;
VDBNode* node = getleafNodeAtPoint ( gvdb, wpos, &vmin, &vdel);
if ( node == 0x0 ) { cell_vel[cid] = 0.0f; return; }
//cell_vel[cid] = -1.0f;
int3 vox = node->mValue + make_int3((wpos.x - vmin.x)/vdel.x, (wpos.y - vmin.y)/vdel.y, (wpos.z - vmin.z)/vdel.z);
cell_vel[cid] = (tex3D<float> ( gvdb->volIn[3], vox.x + 0.5f, vox.y + 0.5f, vox.z + 0.5f));
//cell_vel[cid] = (tex3D<float> ( volIn[9], vox.x, vox.y, vox.z));
}
extern "C" __global__ void gvdbMapExtraGVDB (VDBInfo* gvdb, int numBricks, int sc_dim, int sc_per_brick, int subcell_size, int* sc_mapping, VDBInfo* obs, int* sc_obs_nid)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= numBricks) return;
VDBNode* pnode = getNode ( gvdb, 0, i );
if (pnode->mParent == ID_UNDEF64) return;
int3 pos = pnode->mPos;
int obs_nid = getPosLeafParent(obs, pos);
for (int sc = 0; sc < sc_per_brick; sc++)
{
sc_obs_nid[sc_mapping[i] * sc_per_brick + sc] = obs_nid;
}
}
extern "C" __global__ void gvdbCalcSubcellPos (VDBInfo* gvdb, int* sc_nid, int3* sc_pos, int numBricks, int sc_dim, int sc_per_brick, int subcell_size, int* sc_mapping)
{
uint i = blockIdx.x * blockDim.x + threadIdx.x;
if ( i >= numBricks) return;
//if (sc_mapping[i] < 0) return;
VDBNode* pnode = getNode ( gvdb, 0, i );
if (pnode->mParent == ID_UNDEF64) return;
int3 pos = pnode->mPos;
for (int sc = 0; sc < sc_per_brick; sc++)
{
sc_pos[sc_mapping[i] * sc_per_brick + sc].x = pos.x + sc % sc_dim * subcell_size;
sc_pos[sc_mapping[i] * sc_per_brick + sc].y = pos.y + (sc / sc_dim) % sc_dim * subcell_size;
sc_pos[sc_mapping[i] * sc_per_brick + sc].z = pos.z + (sc / sc_dim / sc_dim) % sc_dim* subcell_size;
sc_nid[sc_mapping[i] * sc_per_brick + sc] = i;
}
}
extern "C" __global__ void gvdbGatherDensity (VDBInfo* gvdb, int num_pnts, int num_sc, float radius,
int* sc_nid, int* sc_cnt, int* sc_off, int3* sc_pos,
float3* sc_pnt_pos, float3* sc_pnt_vel, uchar4* sc_pnt_clr,
int chanDensity, int chanClr, bool bAccumulate )
{
int sc_id = blockIdx.x; // current subcell ID
if (sc_id >= num_sc) return;
int3 wpos;
wpos.x = sc_pos[sc_id].x + int(threadIdx.x); wpos.y = sc_pos[sc_id].y + int(threadIdx.y); wpos.z = sc_pos[sc_id].z + int(threadIdx.z);
VDBNode* node = getNode(gvdb, 0, sc_nid[sc_id]);
float3 vmin = node->mPos * gvdb->voxelsize;
float3 vdel = gvdb->vdel[0];
int3 vox = node->mValue + make_int3((wpos.x - vmin.x) / vdel.x, (wpos.y - vmin.y) / vdel.y, (wpos.z - vmin.z) / vdel.z);
float3 jpos;
float4 clr = make_float4(0,0,0,1);
float val = 0, c = 0.0f;
if ( bAccumulate ) {
val = tex3D<float> ( gvdb->volIn[chanDensity], vox.x + 0.5f, vox.y + 0.5f, vox.z + 0.5f );
if ( sc_pnt_clr != 0x0 ) clr = CHAR2CLR(tex3D<uchar4>(gvdb->volIn[chanClr], vox.x+ 0.5f, vox.y + 0.5f, vox.z + 0.5f) );
}
for (int j = 0; j < sc_cnt[sc_id]; j++) {
jpos = sc_pnt_pos[sc_off[sc_id] + j] - make_float3(wpos);
c = sqrtf(jpos.x*jpos.x + jpos.y*jpos.y + jpos.z*jpos.z);
val = max(val, radius - c);
if (sc_pnt_clr != 0x0) clr += CHAR2CLR(sc_pnt_clr[sc_off[sc_id] + j]);
}
surf3Dwrite( val, gvdb->volOut[chanDensity], vox.x * sizeof(float), vox.y, vox.z);
if (sc_pnt_clr != 0x0) {
clr /= float(sc_cnt[sc_id] + (bAccumulate ? 1 : 0) );
surf3Dwrite( CLR2CHAR(clr), gvdb->volOut[chanClr], vox.x * sizeof(uchar4), vox.y, vox.z);
}
}
extern "C" __global__ void gvdbGatherLevelSet (VDBInfo* gvdb, int num_pnts, int num_sc, float radius,
int* sc_nid, int* sc_cnt, int* sc_off, int3* sc_pos,
float3* sc_pnt_pos, float3* sc_pnt_vel, uchar4* sc_pnt_clr,
int chanLevelset, int chanClr, bool bAccumulate)
{
int sc_id = blockIdx.x; // current subcell ID
if (sc_id >= num_sc) return;
int3 wpos;
wpos.x = sc_pos[sc_id].x + int(threadIdx.x); wpos.y = sc_pos[sc_id].y + int(threadIdx.y); wpos.z = sc_pos[sc_id].z + int(threadIdx.z);
VDBNode* node = getNode(gvdb, 0, sc_nid[sc_id]);
float3 vmin = node->mPos * gvdb->voxelsize;
float3 vdel = gvdb->vdel[0];
int3 vox = node->mValue + make_int3((wpos.x - vmin.x) / vdel.x, (wpos.y - vmin.y) / vdel.y, (wpos.z - vmin.z) / vdel.z);
float3 jpos;
float4 clr = make_float4(0,0,0,1);
float dist = 3.0f, c = 0.0f;
if ( bAccumulate ) {
dist = tex3D<float> ( gvdb->volIn[chanLevelset ], vox.x + 0.5f, vox.y + 0.5f, vox.z + 0.5f );
if ( sc_pnt_clr != 0x0 ) clr = CHAR2CLR(tex3D<uchar4>(gvdb->volIn[chanClr], vox.x + 0.5f, vox.y + 0.5f, vox.z + 0.5f) );
}
for (int j = 0; j < sc_cnt[sc_id]; j++) {
jpos = sc_pnt_pos[sc_off[sc_id] + j] - make_float3(wpos);
c = sqrtf(jpos.x*jpos.x + jpos.y*jpos.y + jpos.z*jpos.z);
dist = min(dist, c - radius);
if (sc_pnt_clr != 0x0) clr += CHAR2CLR(sc_pnt_clr[sc_off[sc_id] + j]);
}
surf3Dwrite( dist, gvdb->volOut[chanLevelset], vox.x * sizeof(float), vox.y, vox.z);
if (sc_pnt_clr != 0x0) {
clr /= float(sc_cnt[sc_id] + (bAccumulate ? 1 : 0) );
surf3Dwrite( CLR2CHAR(clr), gvdb->volOut[chanClr], vox.x * sizeof(uint), vox.y, vox.z);
}
}
extern "C" __global__ void gvdbGatherLevelSet_fp16(VDBInfo* gvdb, int num_pnts, int num_sc, float radius,
float3 pos_min, float3 pos_range, float3 vel_min, float3 vel_range,
int* sc_nid, int* sc_cnt, int* sc_off, int3* sc_pos,
ushort3* sc_pnt_pos, ushort3* sc_pnt_vel, uint* sc_pnt_clr,
int chanLevelset, int chanClr)
{
int sc_id = blockIdx.x; // current subcell ID
if (sc_id >= num_sc) return;
int3 wpos; wpos.x = sc_pos[sc_id].x + int(threadIdx.x); wpos.y = sc_pos[sc_id].y + int(threadIdx.y); wpos.z = sc_pos[sc_id].z + int(threadIdx.z);
VDBNode* node = getNode(gvdb, 0, sc_nid[sc_id]);
float3 vmin = node->mPos * gvdb->voxelsize;
float3 vdel = gvdb->vdel[node->mLev];
int3 vox = node->mValue + make_int3((wpos.x - vmin.x) / vdel.x, (wpos.y - vmin.y) / vdel.y, (wpos.z - vmin.z) / vdel.z);
float3 jpos, tmppos;
float4 clr;
float c = 0.0f;
float dist = 3.0f;
for (int j = 0; j < sc_cnt[sc_id]; j++) {
tmppos.x = sc_pnt_pos[sc_off[sc_id] + j].x / 65535.0f * pos_range.x + pos_min.x;
tmppos.y = sc_pnt_pos[sc_off[sc_id] + j].y / 65535.0f * pos_range.y + pos_min.y;
tmppos.z = sc_pnt_pos[sc_off[sc_id] + j].z / 65535.0f * pos_range.z + pos_min.z;
jpos = tmppos - make_float3(wpos);
c = sqrtf(jpos.x*jpos.x + jpos.y*jpos.y + jpos.z*jpos.z);
dist = min(dist, c - radius);
if (sc_pnt_clr != 0x0) clr += INT2CLR(sc_pnt_clr[sc_off[sc_id] + j]) / c;
}
surf3Dwrite( dist, gvdb->volOut[chanLevelset], vox.x * sizeof(float), vox.y, vox.z);
if ( sc_pnt_clr != 0x0 )
surf3Dwrite( CLR2INT(clr), gvdb->volOut[chanClr], vox.x * sizeof(float), vox.y, vox.z);
}
extern "C" __global__ void gvdbCheckVal (VDBInfo* gvdb, float slice, int3 res, int chanVx, int chanVy, int chanVz, int chanVxOld, int chanVyOld, int chanVzOld, float* outbuf1, float* outbuf2 )
{
uint3 vox = blockIdx * make_uint3(blockDim.x, blockDim.y, blockDim.z) + threadIdx;
if ( vox.y >= 1 ) return;
if ( vox.x > res.x || vox.z > res.z ) return;
float3 wpos = make_float3(vox) * 0.5;
wpos.y = slice;
float3 vmin, vdel;
VDBNode* node = getleafNodeAtPoint ( gvdb, wpos, &vmin, &vdel);
float3 p = (wpos - vmin) / vdel;
if ( node == 0x0 ) return;
//int pi(wpos.x), fj(wpos.y - 0.5f), pk(wpos.z);
int fi(wpos.x - 0.5f), pj(wpos.y), fk(wpos.z - 0.5f);
float vel_y, vel_y2;
vel_y = 0.0f;
for (int ii = 0; ii < 2; ii++) {
for (int jj = 0; jj < 2; jj++) {
for (int kk = 0; kk < 2; kk++) {
int3 vox_pos = make_int3(fi+ii, pj+jj, fk+kk);
int3 tmp_vox = node->mValue + make_int3((vox_pos.x - vmin.x)/vdel.x, (vox_pos.y - vmin.y)/vdel.y, (vox_pos.z - vmin.z)/vdel.z);
float tmp_vel = tex3D<float> ( gvdb->volIn[chanVy], tmp_vox.x, tmp_vox.y, tmp_vox.z);
vel_y += tmp_vel * (1.0f - fabs(fi + ii + 0.5f - wpos.x)) * (1.0f - fabs(pj + jj - wpos.y)) * (1.0f - fabs(fk + kk + 0.5f - wpos.z));
}
}
}
float3 sp = make_float3(node->mValue) + p + make_float3(-0.5f, 0.0f, -0.5f);
vel_y2 = tex3D<float> ( gvdb->volIn[chanVy], sp.x, sp.y, sp.z );
outbuf1[ vox.z*res.x + vox.x ] = vel_y;
outbuf2[ vox.z*res.x + vox.x ] = vel_y2;
}
#define SCAN_BLOCKSIZE 512
extern "C" __global__ void prefixFixup ( uint *input, uint *aux, int len)
{
unsigned int t = threadIdx.x;
unsigned int start = t + 2 * blockIdx.x * SCAN_BLOCKSIZE;
if (start < len) input[start] += aux[blockIdx.x] ;
if (start + SCAN_BLOCKSIZE < len) input[start + SCAN_BLOCKSIZE] += aux[blockIdx.x];
}
extern "C" __global__ void prefixSum ( uint* input, uint* output, uint* aux, int len, int zeroff )
{
__shared__ uint scan_array[SCAN_BLOCKSIZE << 1];
unsigned int t1 = threadIdx.x + 2 * blockIdx.x * SCAN_BLOCKSIZE;
unsigned int t2 = t1 + SCAN_BLOCKSIZE;
// Pre-load into shared memory
scan_array[threadIdx.x] = (t1<len) ? input[t1] : 0.0f;
scan_array[threadIdx.x + SCAN_BLOCKSIZE] = (t2<len) ? input[t2] : 0.0f;
__syncthreads();
// Reduction
int stride;
for (stride = 1; stride <= SCAN_BLOCKSIZE; stride <<= 1) {
int index = (threadIdx.x + 1) * stride * 2 - 1;
if (index < 2 * SCAN_BLOCKSIZE)
scan_array[index] += scan_array[index - stride];
__syncthreads();
}
// Post reduction
for (stride = SCAN_BLOCKSIZE >> 1; stride > 0; stride >>= 1) {
int index = (threadIdx.x + 1) * stride * 2 - 1;
if (index + stride < 2 * SCAN_BLOCKSIZE)
scan_array[index + stride] += scan_array[index];
__syncthreads();
}
__syncthreads();
// Output values & aux
if (t1+zeroff < len) output[t1+zeroff] = scan_array[threadIdx.x];
if (t2+zeroff < len) output[t2+zeroff] = (threadIdx.x==SCAN_BLOCKSIZE-1 && zeroff) ? 0 : scan_array[threadIdx.x + SCAN_BLOCKSIZE];
if ( threadIdx.x == 0 ) {
if ( zeroff ) output[0] = 0;
if (aux) aux[blockIdx.x] = scan_array[2 * SCAN_BLOCKSIZE - 1];
}
}
extern "C" __global__ void gvdbInsertTriangles ( float bdiv, int bmax, int* bcnt, int vcnt, int ecnt, float3* vbuf, int* ebuf )
{
uint n = blockIdx.x * blockDim.x + threadIdx.x;
if ( n >= ecnt ) return;
// get transformed triangle
float3 v0, v1, v2;
int3 f = make_int3( ebuf[n*3], ebuf[n*3+1], ebuf[n*3+2] );
v0 = vbuf[f.x << 1]; v0 = mul4x ( v0, cxform );