-
Notifications
You must be signed in to change notification settings - Fork 0
/
OGeo.cc
1281 lines (974 loc) · 40 KB
/
OGeo.cc
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
/*
* Copyright (c) 2019 Opticks Team. All Rights Reserved.
*
* This file is part of Opticks
* (see https://bitbucket.org/simoncblyth/opticks).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "OGeo.hh"
#include "OGeoStat.hh"
#include "OContext.hh"
#ifdef WITH_OPENGL
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#endif
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <csignal>
#include <optix_world.h>
// opticks-
#include "Opticks.hh"
#include "OpticksConst.hh"
// optixrap-
#include "OContext.hh"
#include "OConfig.hh"
#include "OFormat.hh"
#include "OGeometry.hh"
#include "GGeo.hh"
#include "GGeoLib.hh"
#include "GMergedMesh.hh"
#include "GParts.hh"
#include "PLOG.hh"
#include "BStr.hh"
// npy-
#include "NPY.hpp"
#include "NGPU.hpp"
#include "NSlice.hpp"
#include "GLMFormat.hpp"
#include "OConfig.hh"
/**0
OGeo Details
-----------------
Table 2, OptiX Manual
~~~~~~~~~~~~~~~~~~~~~~~~~
================= ================================ =========================
Parent Node Type Child Node Types Associated Node Types
================= ================================ =========================
Geometry None Material
Acceleration None
GeometryInstance Geometry Material
GeometryGroup GeometryInstance Acceleration
Transform GeometryGroup
Selector Transform
Group GeometryGroup Acceleration
================= ================================ =========================
* Group contains : rtGroup, rtGeometryGroup, rtTransform, or rtSelector
* Transform houses single child : rtGroup, rtGeometryGroup, rtTransform, or rtSelector (NB not GeometryInstance)
* GeometryGroup is a container for an arbitrary number of geometry instances, and must be assigned an Acceleration
* Selector contains : rtGroup, rtGeometryGroup, rtTransform, and rtSelector
Geometry tree that allows instance identity
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JUNO has 6 repeated pieces of geometry.
The two different types of photomultiplier tubes (PMTs) are
by far the most prolific with 20k of one type (20inch)
and 36k of another (3inch)
The geometry tree follows that show in OptiX 6.0.0 manual Fig 3.4 x6
::
m_top (Group) m_top_accel
ggg (GeometryGroup) m_ggg_accel global non-instanced geometry from merged mesh 0
ggi (GeometryInstance)
assembly.0 (Group) m_assembly_accel 1:1 with instanced merged mesh (~6 of these for JUNO)
xform.0 (Transform) (at most 20k/36k different transforms)
perxform (GeometryGroup)
accel[0] m_instance_accel common accel within each assembly
pergi (GeometryInstance) distinct pergi for every instance, with instance_index assigned
omm (Geometry) the same omm and mat are child of all xform/perxform/pergi
mat (Material)
xform.1 (Transform)
perxform (GeometryGroup)
pergi (GeometryInstance)
accel[0]
omm (Geometry)
mat (Material)
... for all the many thousands of instances of repeated geometry ...
assembly.1 (Group) (order ~6 repeated assemblies for JUNO)
xform.0
... just like above ...
* transforms can only be contained in "group" or another transform so add top level group with
another acceleration structure
* transforms must be assigned exactly one child of type rtGroup, rtGeometryGroup, rtTransform, or rtSelector,
Alternate Tree Layout
~~~~~~~~~~~~~~~~~~~~~~~~
(Group)
(Transform)
(GeometryGroup)
(GeometryInstance)
(Geometry)
(Material)
OptiX 7 terminology change
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OptiX 7 changes terminology in a way which may inform
concerning which trees can be handled in RT cores
* Geometry Group -> Geometry AS (only primitives)
* Group -> Instance AS
* Transform -> just input to Instance AS at build
Why proliferate the *pergi* ? So can assign an instance index to it : ie know which PMT gets hit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Need to assign an index to each instance means need a GeometryInstance beneath the xform ?
* "Geometry" and "Material" can also hold variables, but that doesnt help for instance_index
as there is only one geometry and material instance for each assembly
Could the perxform GeometryGroup be common to all ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NO, needs to be a separate GeometryGroup into which to place
the distinct pergi GeometryInstance required for instanced identity
Where to put the RayLOD Selector ? RAYLOD IS NOT CURRENTLY IN USE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* LOD : Level Of Detail
* level0 : most precise/expensive used for ray.origin inside instance sphere
* level1 : cheaper alternative used for ray.origin outside instance sphere
The RayLOD idea is to switch geometry based on the distance from it, using
radius of outermost solid origin centered bounding sphere with safety margin
see notes/issues/can-optix-selector-defer-expensive-csg.rst
Given that the same omm is used for all pergi...
it would seem most appropriate to arrange the selector in common also,
as all instances have the same simplified version of their geometry too..
TODO:
Currently all the accelerations are using Sbvh/Bvh.
Investigate if container groups might be better as "pass through"
NoAccel as the geometryGroup and groups they contain have all the
geometry.
0**/
const plog::Severity OGeo::LEVEL = PLOG::EnvLevel("OGeo", "DEBUG") ;
const char* OGeo::ACCEL = "Sbvh" ;
OGeo::OGeo(OContext* ocontext, Opticks* ok, GGeo* ggeo )
:
m_ocontext(ocontext),
m_context(m_ocontext->getContext()),
m_ok(ok),
m_ggeo(ggeo),
m_geolib(ggeo->getGeoLib()),
m_verbosity(m_ok->getVerbosity()),
m_mmidx(0),
m_top_accel(ACCEL),
m_ggg_accel(ACCEL),
m_assembly_accel(ACCEL),
m_instance_accel(ACCEL)
{
init();
}
void OGeo::init()
{
bool is_gparts_transform_offset = m_ok->isGPartsTransformOffset() ;
LOG(info) << " is_gparts_transform_offset " << is_gparts_transform_offset ;
if( is_gparts_transform_offset )
{
LOG(fatal)
<< " using the old pre7 optixrap machinery with option --gparts_transform_offset enabled will result in mangled transforms " ;
LOG(fatal)
<< " the --gparts_transform_offset is only appropriate when using the new optix7 machinery, eg CSG/CSGOptiX/CSG_GGeo/.. " ;
std::raise(SIGINT);
}
const char* accel = m_ok->getAccel();
std::vector<std::string> elem ;
BStr::split(elem, accel, ',');
unsigned nelem = elem.size();
if(nelem > 0) m_top_accel = strdup(elem[0].c_str()) ;
if(nelem > 1) m_ggg_accel = strdup(elem[1].c_str()) ;
if(nelem > 2) m_assembly_accel = strdup(elem[2].c_str()) ;
if(nelem > 3) m_instance_accel = strdup(elem[3].c_str()) ;
setTopGroup(m_ocontext->getTopGroup());
initWayControl();
LOG(info) << description() ;
}
/**
OGeo::initWayControl
----------------------
**/
void OGeo::initWayControl()
{
int node = m_ggeo->getFirstNodeIndexForPVName(); // --pvname pInnerWater
int boundary = m_ggeo->getSignedBoundary() ; // --boundary Water///Acrylic
unsigned waymask = m_ok->getWayMask() ; // --waymask 0/1/2/3 controls way_select
optix::int4 way_control = optix::make_int4(node,boundary,0,waymask);
LOG(LEVEL)
<< " way_control.x (node) " << way_control.x
<< " way_control.y (boundary) " << way_control.y
<< " way_control.z " << way_control.z
<< " way_control.w (waymask) " << way_control.w
;
m_context["way_control"]->setInt(way_control);
}
void OGeo::setTopGroup(optix::Group top)
{
m_top = top ;
}
std::string OGeo::description() const
{
std::stringstream ss ;
ss << "OGeo "
<< " top " << m_top_accel
<< " ggg " << m_ggg_accel
<< " assembly " << m_assembly_accel
<< " instance " << m_instance_accel
;
return ss.str();
}
/**1
OGeo::convert
---------------
Converts the GMergedMesh instances from (m_geolib)GGeoLib
into optix::Group and optix::GeometryGroup and adds them
to m_top(optix::Group).
1**/
void OGeo::convert()
{
std::string s = m_geolib->summary("OGeo::convert");
LOG(info) << std::endl << s ;
unsigned int nmm = m_geolib->getNumMergedMesh();
LOG(info) << "[ nmm " << nmm ;
for(unsigned i=0 ; i < nmm ; i++)
{
bool enabled = m_ok->isEnabledMergedMesh(i) ;
if( enabled )
{
convertMergedMesh(i);
}
else
{
LOG(error) << "MergedMesh " << i << " IS NOT ENABLED " ;
}
}
m_top->setAcceleration( makeAcceleration(m_top_accel, false) );
if(m_verbosity > 0) dumpStats();
LOG(info) << "] nmm " << nmm ;
}
/**
OGeo::convertMergedMesh
-------------------------
Only invoked from OGeo::convert
**/
void OGeo::convertMergedMesh(unsigned i)
{
LOG(LEVEL) << "( " << i ;
m_mmidx = i ;
GMergedMesh* mm = m_geolib->getMergedMesh(i);
bool raylod = m_ok->isRayLOD() ;
if(raylod) LOG(fatal) << " RayLOD enabled " ;
assert( raylod == false );
bool is_null = mm == NULL ;
bool is_skip = mm->isSkip() ;
bool is_empty = mm->isEmpty() ;
if( is_null || is_skip || is_empty )
{
LOG(error) << " not converting mesh " << i << " is_null " << is_null << " is_skip " << is_skip << " is_empty " << is_empty ;
return ;
}
unsigned numInstances = 0 ;
if( i == 0 ) // global non-instanced geometry in slot 0
{
optix::GeometryGroup ggg = makeGlobalGeometryGroup(mm);
m_top->addChild(ggg);
numInstances = 1 ;
}
else // repeated geometry
{
optix::Group assembly = makeRepeatedAssembly(mm) ;
assembly->setAcceleration( makeAcceleration(m_assembly_accel, false) );
numInstances = assembly->getChildCount() ;
m_top->addChild(assembly);
}
LOG(LEVEL) << ") " << i << " numInstances " << numInstances ;
}
/**
OGeo::makeGlobalGeometryGroup
-------------------------------
Invoked only from OGeo::convertMergedMesh.
**/
optix::GeometryGroup OGeo::makeGlobalGeometryGroup(GMergedMesh* mm)
{
int dbgmm = m_ok->getDbgMM() ;
if(dbgmm == 0) mm->dumpVolumesSelected("OGeo::makeGlobalGeometryGroup [--dbgmm 0] ");
optix::Material mat = makeMaterial();
OGeometry* omm = makeOGeometry( mm );
unsigned instance_index = 0u ; // so same code can run Instanced or not
optix::GeometryInstance ggi = makeGeometryInstance(omm, mat, instance_index );
ggi["primitive_count"]->setUint( 0u ); // non-instanced
ggi["repeat_index"]->setUint( mm->getIndex() ); // non-instanced
optix::Acceleration accel = makeAcceleration(m_ggg_accel, false) ;
optix::GeometryGroup ggg = makeGeometryGroup(ggi, accel );
#if OPTIX_VERSION_MAJOR >= 6
RTinstanceflags instflags = RT_INSTANCE_FLAG_DISABLE_ANYHIT ;
ggg->setFlags(instflags);
#endif
return ggg ;
}
/**
OGeo::makeRepeatedAssembly
---------------------------
Invoked only from OGeo::convertMergedMesh.
**/
optix::Group OGeo::makeRepeatedAssembly(GMergedMesh* mm)
{
bool raylod = false ;
unsigned mmidx = mm->getIndex();
unsigned imodulo = m_ok->getInstanceModulo( mmidx );
LOG(LEVEL)
<< " mmidx " << mmidx
<< " imodulo " << imodulo
;
NPY<float>* itransforms = mm->getITransformsBuffer();
NSlice* islice = mm->getInstanceSlice();
if(!islice) islice = new NSlice(0, itransforms->getNumItems()) ;
unsigned int numTransforms = islice->count();
assert(itransforms && numTransforms > 0);
NPY<unsigned int>* ibuf = mm->getInstancedIdentityBuffer();
unsigned int numIdentity = ibuf->getNumItems();
assert(numIdentity % numTransforms == 0 && "expecting numIdentity to be integer multiple of numTransforms");
unsigned int numSolids = numIdentity/numTransforms ;
LOG(LEVEL)
<< " numTransforms " << numTransforms
<< " numIdentity " << numIdentity
<< " numSolids " << numSolids
<< " islice " << islice->description()
;
OGeometry* omm[2] ;
omm[0] = makeOGeometry( mm );
//omm[1] = raylod ? makeOGeometry( mm, 1u ) : NULL ;
omm[1] = NULL ;
optix::Material mat = makeMaterial();
#ifdef OLD_LOD
optix::Program visit ;
if(raylod)
{
visit = m_ocontext->createProgram("visit_instance.cu", "visit_instance");
float instance_bounding_radius = mm->getBoundingRadiusCE(0) ;
visit["instance_bounding_radius"]->setFloat( instance_bounding_radius*2.f );
}
#endif
unsigned count(0);
if(imodulo == 0u)
{
count = islice->count() ;
}
else
{
for(unsigned int i=islice->low ; i<islice->high ; i+=islice->step) // CAUTION HEAVY LOOP eg 20k PMTs
{
if( i % imodulo != 0u ) continue ;
count++ ;
}
}
optix::Group assembly = m_context->createGroup();
assembly->setChildCount( count );
optix::Acceleration accel[2] ;
accel[0] = makeAcceleration(m_instance_accel, false) ; // common accel for all instances as same geometry
accel[1] = makeAcceleration(m_instance_accel, false) ; // NB accel is not created inside the below instance loop
unsigned ichild = 0 ;
for(unsigned int i=islice->low ; i<islice->high ; i+=islice->step) // CAUTION HEAVY LOOP eg 20k PMTs
{
if( imodulo > 0u && i % imodulo != 0u ) continue ; // modulo scaledown for debugging
optix::Transform xform = m_context->createTransform();
glm::mat4 m4 = itransforms->getMat4(i) ;
const float* tdata = glm::value_ptr(m4) ;
setTransformMatrix(xform, tdata);
assembly->setChild(ichild, xform);
ichild++ ;
unsigned instance_index = i ;
if(raylod == false)
{
/*
assembly (Group)
xform (Transform)
perxform (GeometryGroup)
pergi (GeometryInstance)
accel[0] (Acceleration)
*/
optix::GeometryInstance pergi = makeGeometryInstance(omm[0], mat, instance_index);
optix::GeometryGroup perxform = makeGeometryGroup(pergi, accel[0] );
xform->setChild(perxform);
#if OPTIX_VERSION_MAJOR >= 6
RTinstanceflags instflags = RT_INSTANCE_FLAG_DISABLE_ANYHIT ;
perxform->setFlags(instflags);
#endif
}
#ifdef OLD_LOD
else
{
assert(0);
optix::GeometryInstance gi[2] ;
gi[0] = makeGeometryInstance( omm[0] , mat, instance_index );
gi[1] = makeGeometryInstance( omm[1] , mat, instance_index );
optix::GeometryGroup gg[2] ;
gg[0] = makeGeometryGroup(gi[0], accel[0]);
gg[1] = makeGeometryGroup(gi[1], accel[1]);
optix::Selector selector = m_context->createSelector();
selector->setChildCount(2) ;
selector->setChild(0, gg[0] );
selector->setChild(1, gg[1] );
selector->setVisitProgram( visit );
xform->setChild(selector);
}
#endif
}
assert( ichild == count );
return assembly ;
}
void OGeo::setTransformMatrix(optix::Transform& xform, const float* tdata )
{
bool transpose = true ;
optix::Matrix4x4 m(tdata) ;
xform->setMatrix(transpose, m.getData(), 0);
//dump("OGeo::setTransformMatrix", m.getData());
}
void OGeo::dump(const char* msg, const float* f)
{
printf("%s\n", msg);
for(unsigned int i=0 ; i < 16 ; i++) printf(" %10.3f ", *(f+i) ) ;
printf("\n");
}
optix::Acceleration OGeo::makeAcceleration(const char* accel, bool accel_props)
{
LOG(debug)
<< " accel " << accel
<< " accel_props " << accel_props
;
optix::Acceleration acceleration = m_context->createAcceleration(accel);
if(accel_props == true)
{
acceleration->setProperty( "vertex_buffer_name", "vertexBuffer" );
acceleration->setProperty( "index_buffer_name", "indexBuffer" );
}
return acceleration ;
}
optix::Material OGeo::makeMaterial()
{
LOG(verbose)
<< " radiance_ray " << OContext::e_radiance_ray
<< " propagate_ray " << OContext::e_propagate_ray
;
LOG(LEVEL) << "[" ;
optix::Material material = m_context->createMaterial();
material->setClosestHitProgram(OContext::e_radiance_ray, m_ocontext->createProgram("material1_radiance.cu", "closest_hit_radiance"));
bool angular = m_ok->isAngularEnabled() ;
const char* ch_module = angular ? "closest_hit_angular_propagate.cu" : "closest_hit_propagate.cu" ;
const char* ch_func = angular ? "closest_hit_angular_propagate" : "closest_hit_propagate" ;
LOG(LEVEL)
<< " angular " << angular
<< " ch_module " << ch_module
<< " ch_func " << ch_func
;
material->setClosestHitProgram(OContext::e_propagate_ray, m_ocontext->createProgram(ch_module, ch_func));
LOG(LEVEL) << "]" ;
return material ;
}
optix::GeometryGroup OGeo::makeGeometryGroup(optix::GeometryInstance gi, optix::Acceleration accel )
{
optix::GeometryGroup gg = m_context->createGeometryGroup();
gg->addChild(gi);
gg->setAcceleration( accel );
return gg ;
}
optix::GeometryInstance OGeo::makeGeometryInstance(OGeometry* ogeom, optix::Material mat, unsigned instance_index)
{
optix::GeometryInstance gi = m_context->createGeometryInstance() ;
gi->setMaterialCount(1);
gi->setMaterial(0, mat );
if( ogeom->g.get() != NULL )
{
gi->setGeometry( ogeom->g );
}
#if OPTIX_VERSION >= 60000
else if ( ogeom->gt.get() != NULL )
{
gi->setGeometryTriangles( ogeom->gt );
}
#endif
else
{
LOG(fatal) << " given OGeometry instance holding no geometry " ;
assert(0);
}
gi["instance_index"]->setUint(instance_index);
return gi ;
}
/**
OGeo::makeOGeometry : creating the OptiX GPU geometry
-------------------------------------------------------
* NB --xanalytic option switches to analytic geometry, ignoring the 'T' or 'A' geocode of the mergedmesh
The OGeo::OGeometry internal struct is returned to enable optix::Geometry and optix::GeometryTriangles
to be handled uniformly. Initially tried returning optix::GeometryInstance to effect such a
uniform handling but that was too slow as it forced recreation of optix geometry
for every instance.
**/
OGeometry* OGeo::makeOGeometry(GMergedMesh* mergedmesh)
{
OGeometry* ogeom = new OGeometry ;
/*
int rtxmode = m_ok->getRTX();
char ugeocode ;
if( m_ok->isXAnalytic() )
{
ugeocode = OpticksConst::GEOCODE_ANALYTIC ;
}
else if( m_ok->isXGeometryTriangles() || rtxmode == 2 )
{
ugeocode = OpticksConst::GEOCODE_GEOMETRYTRIANGLES ;
}
else
{
ugeocode = mergedmesh->getGeoCode() ;
}
*/
char ugeocode = mergedmesh->getCurrentGeoCode();
LOG(LEVEL) << "ugeocode [" << (char)ugeocode << "]" ;
if(ugeocode == OpticksConst::GEOCODE_TRIANGULATED )
{
ogeom->g = makeTriangulatedGeometry(mergedmesh);
}
else if(ugeocode == OpticksConst::GEOCODE_ANALYTIC)
{
ogeom->g = makeAnalyticGeometry(mergedmesh);
}
else if(ugeocode == OpticksConst::GEOCODE_GEOMETRYTRIANGLES)
{
#if OPTIX_VERSION_MAJOR >= 6
ogeom->gt = makeGeometryTriangles(mergedmesh);
#else
assert(0 && "Require at least OptiX 6.0.0 to use GeometryTriangles ");
#endif
}
else
{
LOG(fatal) << "geocode must be triangulated or analytic, not [" << (char)ugeocode << "]" ;
assert(0);
}
#if OPTIX_VERSION_MAJOR >= 6
LOG(verbose) << " DISABLE_ANYHIT " ;
RTgeometryflags flags = RT_GEOMETRY_FLAG_DISABLE_ANYHIT ;
if(ogeom->isGeometry())
{
ogeom->g->setFlags( flags );
}
else if( ogeom->isGeometryTriangles())
{
unsigned int material_index = 0u ;
ogeom->gt->setFlagsPerMaterial( material_index, flags );
}
#endif
return ogeom ;
}
/**2
OGeo::makeAnalyticGeometry
----------------------------
The GParts instance that this operates from will usually
have been concatenated from multiple other GParts instances,
one for each NCSG solid. GParts concatenation happens during
GMergedMesh formation in GMergedMesh::mergeVolumeAnalytic.
For repeated geometry note how all bar one of the geometry buffers
are small. Only the idBuf is large and usage GPU side requires
use of the instance_index.
2**/
optix::Geometry OGeo::makeAnalyticGeometry(GMergedMesh* mm)
{
bool dbgmm = m_ok->getDbgMM() == int(mm->getIndex()) ;
bool dbganalytic = m_ok->hasOpt("dbganalytic") ;
if(m_verbosity > 2 || dbgmm)
LOG(info)
<< "["
<< " verbosity " << m_verbosity
<< " mm " << mm->getIndex()
;
// when using --test eg PmtInBox or BoxInBox the mesh is fabricated in GGeoTest
GParts* pts = mm->getParts(); assert(pts && "GMergedMesh with GEOCODE_ANALYTIC must have associated GParts, see GGeo::modifyGeometry ");
if(pts->getPrimBuffer() == NULL)
{
LOG(LEVEL) << "( GParts::close " ;
pts->close();
LOG(LEVEL) << ") GParts::close " ;
}
else
{
LOG(LEVEL) << " skip GParts::close " ;
}
LOG(LEVEL) << "mm " << mm->getIndex()
<< " verbosity: " << m_verbosity
<< ( dbgmm ? " --dbgmm " : " " )
<< ( dbganalytic ? " --dbganalytic " : " " )
<< " pts: " << pts->desc()
;
if(dbgmm)
{
LOG(fatal) << "dumping as instructed by : --dbgmm " << m_ok->getDbgMM() ;
mm->dumpVolumesSelected("OGeo::makeAnalyticGeometry");
}
if(m_verbosity > 3 || dbganalytic || dbgmm ) pts->fulldump("--dbganalytic/--dbgmm", 10) ;
NPY<float>* partBuf = pts->getPartBuffer(); assert(partBuf && partBuf->hasShape(-1,4,4)); // node buffer
NPY<float>* tranBuf = pts->getTranBuffer(); assert(tranBuf && tranBuf->hasShape(-1,3,4,4)); // transform triples (t,v,q)
NPY<float>* planBuf = pts->getPlanBuffer(); assert(planBuf && planBuf->hasShape(-1,4)); // planes used for convex polyhedra such as trapezoid
NPY<int>* primBuf = pts->getPrimBuffer(); assert(primBuf && primBuf->hasShape(-1,4)); // prim
// NB these buffers are concatenations of the corresponding buffers for multiple prim
unsigned numPrim = primBuf->getNumItems();
NPY<float>* itransforms = mm->getITransformsBuffer(); assert(itransforms && itransforms->hasShape(-1,4,4) ) ;
unsigned numInstances = itransforms->getNumItems();
NPY<unsigned>* idBuf = mm->getInstancedIdentityBuffer(); assert(idBuf);
LOG(LEVEL)
<< " mmidx " << mm->getIndex()
<< " numInstances " << numInstances
<< " numPrim " << numPrim
<< " idBuf " << idBuf->getShapeString()
;
if( mm->getIndex() > 0 ) // volume level buffers do not honour selection unless using globalinstance
{
bool expect = idBuf->hasShape(numInstances,numPrim,4) ;
if(!expect)
LOG(fatal)
<< " UNEXPECTED "
<< " idBuf " << idBuf->getShapeString()
<< " numInstance " << numInstances
<< " numPrim " << numPrim
<< " mm.index " << mm->getIndex()
;
assert(expect);
}
unsigned numPart = partBuf->getNumItems();
unsigned numTran = tranBuf->getNumItems();
unsigned numPlan = planBuf->getNumItems();
unsigned numVolumes = mm->getNumVolumes();
unsigned numVolumesSelected = mm->getNumVolumesSelected();
if( pts->isNodeTree() )
{
bool match = numPrim == numVolumes ;
if(!match)
{
LOG(fatal)
<< " NodeTree : MISMATCH (numPrim != numVolumes) "
<< " (this happens when using --csgskiplv) "
<< " numVolumes " << numVolumes
<< " numVolumesSelected " << numVolumesSelected
<< " numPrim " << numPrim
<< " numPart " << numPart
<< " numTran " << numTran
<< " numPlan " << numPlan
;
}
//assert( match && "NodeTree Sanity check failed " );
// hmm tgltf-;tgltf-- violates this ?
}
//assert( numPrim < 10 ); // expecting small number
assert( numTran <= numPart ) ;
unsigned analytic_version = pts->getAnalyticVersion();
OGeoStat stat(mm->getIndex(), numPrim, numPart, numTran, numPlan );
m_stats.push_back(stat);
if(m_verbosity > 2)
LOG(info)
<< stat.desc()
<< " analytic_version " << analytic_version
;
optix::Geometry geometry = m_context->createGeometry();
bool someprim = numPrim >= 1 ;
if(!someprim)
LOG(fatal)
<< " someprim fails "
<< " mm.index " << mm->getIndex()
<< " numPrim " << numPrim
<< " numPart " << numPart
<< " numTran " << numTran
<< " numPlan " << numPlan
;
assert( someprim );
#ifdef OLD_LOD
geometry->setPrimitiveCount( lod > 0 ? 1 : numPrim ); // lazy lod, dont change buffers, just ignore all but the 1st prim for lod > 0
#else
geometry->setPrimitiveCount( numPrim );
#endif
geometry["primitive_count"]->setUint( numPrim ); // needed GPU side, for instanced offset into buffers
geometry["repeat_index"]->setUint( mm->getIndex() ); // ridx
geometry["analytic_version"]->setUint(analytic_version);
optix::Program intersectProg = m_ocontext->createProgram("intersect_analytic.cu", "intersect") ;
optix::Program boundsProg = m_ocontext->createProgram("intersect_analytic.cu", "bounds") ;
geometry->setIntersectionProgram(intersectProg );
geometry->setBoundingBoxProgram( boundsProg );
assert(sizeof(int) == 4);
optix::Buffer primBuffer = createInputUserBuffer<int>( primBuf, 4*4, "primBuffer");
geometry["primBuffer"]->setBuffer(primBuffer);
// hmm perhaps prim and id should be handled together ?
assert(sizeof(float) == 4);
optix::Buffer partBuffer = createInputUserBuffer<float>( partBuf, 4*4*4, "partBuffer");
geometry["partBuffer"]->setBuffer(partBuffer);
assert(sizeof(optix::Matrix4x4) == 4*4*4);
optix::Buffer tranBuffer = createInputUserBuffer<float>( tranBuf, sizeof(optix::Matrix4x4), "tranBuffer");
geometry["tranBuffer"]->setBuffer(tranBuffer);
optix::Buffer identityBuffer = createInputBuffer<optix::uint4, unsigned int>( idBuf, RT_FORMAT_UNSIGNED_INT4, 1 , "identityBuffer");
geometry["identityBuffer"]->setBuffer(identityBuffer);
optix::Buffer planBuffer = createInputUserBuffer<float>( planBuf, 4*4, "planBuffer");
geometry["planBuffer"]->setBuffer(planBuffer);
// TODO: prismBuffer is misnamed it contains planes, TODO:migrate to use the planBuffer
optix::Buffer prismBuffer = m_context->createBuffer(RT_BUFFER_INPUT_OUTPUT);
prismBuffer->setFormat(RT_FORMAT_FLOAT4);
prismBuffer->setSize(5);
geometry["prismBuffer"]->setBuffer(prismBuffer);
if(m_verbosity > 2 || dbgmm)
LOG(info)
<< "]"
<< " verbosity " << m_verbosity
<< " mm " << mm->getIndex()
;
return geometry ;
}
void OGeo::dumpStats(const char* msg)
{
LOG(info) << msg << " num_stats " << m_stats.size() ;
for(unsigned i=0 ; i < m_stats.size() ; i++) std::cout << m_stats[i].desc() << std::endl ;
}
/**
OGeo::makeGeometryTriangles
-----------------------------
Cannot use int3 index format::
what(): Invalid context (Details: Function "RTresult _rtContextValidate(RTcontext)" caught exception:
Validation error: GeometryTriangles has invalid index format, must be RT_FORMAT_UNSIGNED_INT3, or RT_FORMAT_UNSIGNED_SHORT3)
**/
#if OPTIX_VERSION >= 60000
optix::GeometryTriangles OGeo::makeGeometryTriangles(GMergedMesh* mm)
{
unsigned numFaces = mm->getNumFaces();
unsigned numVertices = mm->getNumVertices();
GBuffer* vtx = mm->getVerticesBuffer() ;
GBuffer* rib = mm->getAppropriateRepeatedIdentityBuffer() ;
GBuffer* idb = mm->getIndicesBuffer() ;
optix::GeometryTriangles gtri = m_context->createGeometryTriangles();
RTformat identityFormat = RT_FORMAT_UNSIGNED_INT4 ;
optix::Buffer identityBuffer = createInputBuffer<optix::uint4>( rib, identityFormat, 1, "identityBuffer");
gtri["identityBuffer"]->setBuffer(identityBuffer);
RTformat vertexFormat = RT_FORMAT_FLOAT3 ;
optix::Buffer vertexBuffer = createInputBuffer<optix::float3>( vtx , vertexFormat, 1, "vertexBuffer");
gtri["vertexBuffer"]->setBuffer(vertexBuffer);
RTformat indexFormat = RT_FORMAT_UNSIGNED_INT3 ; // are "coercing" from underlying int buffer
optix::Buffer indexBuffer = createInputBuffer<optix::uint3>( idb, indexFormat, 3 , "indexBuffer"); // need the 3 to fold for faces
gtri["indexBuffer"]->setBuffer(indexBuffer);
optix::Buffer emptyBuffer = m_context->createBuffer(RT_BUFFER_INPUT_OUTPUT, RT_FORMAT_FLOAT3, 0);
gtri["tangentBuffer"]->setBuffer(emptyBuffer);
gtri["bitangentBuffer"]->setBuffer(emptyBuffer);
gtri["normalBuffer"]->setBuffer(emptyBuffer);
gtri["texCoordBuffer"]->setBuffer(emptyBuffer);
gtri["primitive_count"]->setUint( numFaces ); // needed for instanced offsets into buffers, so must describe the buffer, NOT the intent
gtri["repeat_index"]->setUint(mm->getIndex());
gtri->setPrimitiveCount( numFaces );
gtri->setTriangleIndices( indexBuffer, indexFormat );
gtri->setVertices( numVertices, vertexBuffer, vertexFormat );
gtri->setBuildFlags( RTgeometrybuildflags( 0 ) );