File indexing completed on 2026-04-09 07:49:00
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 #include <iostream>
0033 #include <cstdlib>
0034 #include <chrono>
0035
0036 #include <optix.h>
0037 #if OPTIX_VERSION < 70000
0038 #else
0039 #include <optix_stubs.h>
0040 #endif
0041
0042 #include <cuda_runtime.h>
0043 #include <glm/glm.hpp>
0044
0045
0046 #include "sproc.h"
0047 #include "ssys.h"
0048 #include "spath.h"
0049 #include "smeta.h"
0050 #include "SProf.hh"
0051
0052 #include "SGLM.h"
0053 #include "NP.hh"
0054 #include "SRG.h"
0055 #include "SCAM.h"
0056 #include "SEventConfig.hh"
0057 #include "SGeoConfig.hh"
0058 #include "SSim.hh"
0059 #include "SStr.hh"
0060 #include "SEvt.hh"
0061 #include "SMeta.hh"
0062 #include "SPath.hh"
0063 #include "SVec.hh"
0064 #include "SLOG.hh"
0065 #include "scuda.h"
0066 #include "squad.h"
0067 #include "sframe.h"
0068
0069
0070 #include "CSGPrim.h"
0071 #include "CSGFoundry.h"
0072 #include "CSGView.h"
0073
0074
0075 #include "qrng.h"
0076 #include "QU.hh"
0077 #include "QSim.hh"
0078 #include "qsim.h"
0079 #include "QEvt.hh"
0080
0081
0082 #include "Frame.h"
0083 #include "Params.h"
0084 #include "config.h"
0085
0086 #if OPTIX_VERSION < 70000
0087 #include "Six.h"
0088 #else
0089 #include "Ctx.h"
0090 #include "CUDA_CHECK.h"
0091 #include "OPTIX_CHECK.h"
0092 #include "PIP.h"
0093 #include "SBT.h"
0094 #endif
0095
0096 #include "CSGOptiX.h"
0097
0098 const plog::Severity CSGOptiX::LEVEL = SLOG::EnvLevel("CSGOptiX", "DEBUG" );
0099 CSGOptiX* CSGOptiX::INSTANCE = nullptr ;
0100 CSGOptiX* CSGOptiX::Get()
0101 {
0102 return INSTANCE ;
0103 }
0104
0105
0106 int CSGOptiX::Version()
0107 {
0108 int vers = 0 ;
0109 #if OPTIX_VERSION < 70000
0110 vers = 6 ;
0111 #else
0112 vers = 7 ;
0113 #endif
0114 return vers ;
0115 }
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144 int CSGOptiX::RenderMain()
0145 {
0146 SEventConfig::SetRGModeRender();
0147 CSGFoundry* fd = CSGFoundry::Load();
0148 CSGOptiX* cx = CSGOptiX::Create(fd) ;
0149 cx->render();
0150 delete cx ;
0151 return 0 ;
0152 }
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168 int CSGOptiX::SimtraceMain()
0169 {
0170 SEventConfig::SetRGModeSimtrace();
0171 CSGFoundry* fd = CSGFoundry::Load();
0172 CSGOptiX* cx = CSGOptiX::Create(fd) ;
0173 cx->simtrace(0);
0174 delete cx ;
0175 return 0 ;
0176 }
0177
0178
0179
0180
0181
0182
0183
0184
0185 int CSGOptiX::SimulateMain()
0186 {
0187 SProf::Add("CSGOptiX__SimulateMain_HEAD");
0188 SEventConfig::SetRGModeSimulate();
0189 CSGFoundry* fd = CSGFoundry::Load();
0190 CSGOptiX* cx = CSGOptiX::Create(fd) ;
0191 bool reset = true ;
0192 for(int i=0 ; i < SEventConfig::NumEvent() ; i++) cx->simulate(i, reset);
0193 SProf::UnsetTag();
0194 SProf::Add("CSGOptiX__SimulateMain_TAIL");
0195 SProf::Write();
0196 cx->write_Ctx_log();
0197 delete cx ;
0198 return 0 ;
0199 }
0200
0201
0202
0203
0204
0205 const char* CSGOptiX::Desc()
0206 {
0207 std::stringstream ss ;
0208 ss << "CSGOptiX::Desc"
0209 << " Version " << Version()
0210 #ifdef WITH_CUSTOM4
0211 << " WITH_CUSTOM4 "
0212 #else
0213 << " NOT:WITH_CUSTOM4 "
0214 #endif
0215 ;
0216 std::string str = ss.str();
0217 return strdup(str.c_str());
0218 }
0219
0220
0221 const char* CSGOptiX::desc() const
0222 {
0223 std::stringstream ss ;
0224 ss << Desc() ;
0225 #if OPTIX_VERSION < 70000
0226 #else
0227 ss << pip->desc() ;
0228 #endif
0229 std::string s = ss.str();
0230 return strdup(s.c_str());
0231 }
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251 void CSGOptiX::InitEvt( CSGFoundry* fd )
0252 {
0253 SEvt* sev = SEvt::CreateOrReuse(SEvt::EGPU) ;
0254
0255 #ifdef WITH_OLD_FRAME
0256 sev->setGeo((SGeo*)fd);
0257 #else
0258 const SSim* ssim = fd->getSim();
0259 sev->setSim(ssim);
0260 #endif
0261
0262 std::string* rms = SEvt::RunMetaString() ;
0263 assert(rms);
0264
0265 bool stamp = false ;
0266 smeta::Collect(*rms, "CSGOptiX__InitEvt", stamp );
0267 }
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278 void CSGOptiX::InitSim( SSim* ssim )
0279 {
0280 LOG(LEVEL) << "[" ; ;
0281
0282 if(SEventConfig::IsRGModeRender()) return ;
0283
0284 LOG_IF(fatal, ssim == nullptr ) << "simulate/simtrace modes require SSim/QSim setup" ;
0285 assert(ssim);
0286
0287
0288 if( ssim->hasTop() == false )
0289 {
0290 ssim->serialize() ;
0291 }
0292 else
0293 {
0294 LOG(LEVEL) << " NOT calling SSim::serialize : as already done, loaded ? " ;
0295 }
0296
0297 QSim::UploadComponents(ssim);
0298
0299 QSim* qs = QSim::Create() ;
0300
0301 LOG(LEVEL) << "]" << qs->desc() ;
0302 }
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314 void CSGOptiX::InitMeta()
0315 {
0316 std::string gm = SEventConfig::GetGPUMeta() ;
0317 SEvt::SetRunMetaString("GPUMeta", gm.c_str() );
0318
0319 std::string switches = QSim::Switches() ;
0320 SEvt::SetRunMetaString("QSim__Switches", switches.c_str() );
0321
0322 #ifdef WITH_CUSTOM4
0323 std::string c4 = "TBD" ;
0324 SEvt::SetRunMetaString("C4Version", c4.c_str());
0325 #else
0326 SEvt::SetRunMetaString("C4Version", "NOT-WITH_CUSTOM4" );
0327 #endif
0328
0329 }
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346 void CSGOptiX::InitGeo( CSGFoundry* fd )
0347 {
0348 LOG(LEVEL) << "[" ; ;
0349 fd->upload();
0350 LOG(LEVEL) << "]" ; ;
0351 }
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367 CSGOptiX* CSGOptiX::Create(CSGFoundry* fd )
0368 {
0369 SProf::Add("CSGOptiX__Create_HEAD");
0370 LOG(LEVEL) << "[ fd.descBase " << ( fd ? fd->descBase() : "-" ) ;
0371
0372 SSim* ssim = const_cast<SSim*>(fd->sim) ;
0373
0374
0375 InitEvt(fd);
0376 InitSim(ssim);
0377 InitMeta();
0378 InitGeo(fd);
0379
0380 CSGOptiX* cx = new CSGOptiX(fd) ;
0381
0382 if(!SEventConfig::IsRGModeRender())
0383 {
0384 QSim* qs = QSim::Get() ;
0385 qs->setLauncher(cx);
0386 }
0387
0388
0389 LOG(LEVEL) << "]" ;
0390 SProf::Add("CSGOptiX__Create_TAIL");
0391 return cx ;
0392 }
0393
0394
0395
0396
0397
0398
0399
0400
0401 Params* CSGOptiX::InitParams( int raygenmode, const SGLM* sglm )
0402 {
0403 LOG(LEVEL) << "[" ;
0404 return new Params(raygenmode, sglm->Width(), sglm->Height(), 1 ) ;
0405 LOG(LEVEL) << "]" ;
0406 }
0407
0408
0409
0410
0411
0412 CSGOptiX::~CSGOptiX()
0413 {
0414 destroy();
0415 }
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435 CSGOptiX::CSGOptiX(const CSGFoundry* foundry_)
0436 :
0437 sglm(SGLM::Get()),
0438 flight(SGeoConfig::FlightConfig()),
0439 foundry(foundry_),
0440 outdir(SEventConfig::OutFold()),
0441 _optixpath(std::getenv("CSGOptiX__optixpath")),
0442 optixpath(spath::Resolve(gphox::Config::PtxPath("CSGOptiX7.ptx").c_str())),
0443 tmin_model(ssys::getenvfloat("TMIN",0.1)),
0444 kernel_count(0),
0445 raygenmode(SEventConfig::RGMode()),
0446 params(InitParams(raygenmode,sglm)),
0447 ctx(nullptr),
0448 pip(nullptr),
0449 sbt(nullptr),
0450 framebuf(nullptr),
0451 meta(new SMeta),
0452 kernel_dt(0.),
0453 sctx(nullptr),
0454 sim(QSim::Get()),
0455 qev(sim == nullptr ? nullptr : sim->qev)
0456 {
0457 init();
0458 }
0459
0460 void CSGOptiX::init()
0461 {
0462 sglm->addlog("CSGOptiX::init", "start");
0463
0464 LOG(LEVEL)
0465 << "["
0466 << " raygenmode " << raygenmode
0467 << " SRG::Name(raygenmode) " << SRG::Name(raygenmode)
0468 << " sim " << sim
0469 << " qev " << qev
0470 ;
0471
0472 assert( outdir && "expecting OUTDIR envvar " );
0473
0474 LOG(LEVEL) << " _optixpath " << _optixpath ;
0475 LOG(LEVEL) << " optixpath " << optixpath ;
0476
0477 initMeta();
0478 initCtx();
0479 initPIP();
0480 initSBT();
0481 initCheckSim();
0482 initStack();
0483 initParams();
0484 initGeometry();
0485 initSimulate();
0486
0487 #ifdef WITH_OLD_FRAME
0488 initFrame();
0489 #endif
0490
0491 initRender();
0492 initPIDXYZ();
0493
0494 LOG(LEVEL) << "]" ;
0495 }
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506 void CSGOptiX::initMeta()
0507 {
0508 int64_t mtime = spath::last_write_time(optixpath);
0509 std::string str = sstamp::Format(mtime);
0510 int64_t age_secs = sstamp::age_seconds(mtime);
0511 int64_t age_days = sstamp::age_days(mtime);
0512
0513 SEvt::SetRunMetaString("optixpath", optixpath );
0514 SEvt::SetRunMetaString("optixpath_mtime_str", str.c_str() );
0515 SEvt::SetRunMeta<int64_t>("optixpath_mtime", mtime );
0516 SEvt::SetRunMeta<int64_t>("optixpath_age_secs", age_secs );
0517 SEvt::SetRunMeta<int64_t>("optixpath_age_days", age_days );
0518 }
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529 void CSGOptiX::initCtx()
0530 {
0531 LOG(LEVEL) << "[" ;
0532 ctx = new Ctx ;
0533 LOG(LEVEL) << std::endl << ctx->desc() ;
0534 LOG(LEVEL) << "]" ;
0535 }
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546 void CSGOptiX::initPIP()
0547 {
0548 LOG(LEVEL) << "[" ;
0549 LOG(LEVEL)
0550 << " optixpath " << ( optixpath ? optixpath : "-" )
0551 ;
0552
0553 pip = new PIP(optixpath, ctx->props ) ;
0554 LOG(LEVEL) << "]" ;
0555 }
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566 void CSGOptiX::initSBT()
0567 {
0568 LOG(LEVEL) << "[" ;
0569 sbt = new SBT(pip) ;
0570 LOG(LEVEL) << "]" ;
0571 }
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584 void CSGOptiX::initCheckSim()
0585 {
0586 if(SEventConfig::IsRGModeRender()) return ;
0587 LOG(LEVEL) << " sim " << sim << " qev " << qev ;
0588 LOG_IF(fatal, sim == nullptr) << "simtrace/simulate modes require instanciation of QSim before CSGOptiX " ;
0589 assert(sim);
0590 }
0591
0592
0593 void CSGOptiX::initStack()
0594 {
0595 LOG(LEVEL);
0596 pip->configureStack();
0597 }
0598
0599 void CSGOptiX::initParams()
0600 {
0601 params->device_alloc();
0602 }
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614 void CSGOptiX::initGeometry()
0615 {
0616 LOG(LEVEL) << "[" ;
0617 params->node = foundry->d_node ;
0618 params->plan = foundry->d_plan ;
0619 params->tran = nullptr ;
0620 params->itra = foundry->d_itra ;
0621
0622 bool is_uploaded = params->node != nullptr ;
0623 LOG_IF(fatal, !is_uploaded) << "foundry must be uploaded prior to CSGOptiX::initGeometry " ;
0624 assert( is_uploaded );
0625
0626 LOG(LEVEL) << "[ sbt.setFoundry " ;
0627 sbt->setFoundry(foundry);
0628 params->handle = sbt->getTOPHandle() ;
0629 LOG(LEVEL) << "] sbt.setFoundry " ;
0630 LOG(LEVEL) << "]" ;
0631 }
0632
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651 void CSGOptiX::initSimulate()
0652 {
0653 LOG(LEVEL) ;
0654 params->sim = sim ? sim->getDevicePtr() : nullptr ;
0655 params->evt = qev ? qev->getDevicePtr() : nullptr ;
0656
0657 params->tmin0 = SEventConfig::PropagateEpsilon0() ;
0658 params->PropagateEpsilon0Mask = SEventConfig::PropagateEpsilon0Mask();
0659
0660 params->PropagateRefine = SEventConfig::PropagateRefine();
0661 params->PropagateRefineDistance = SEventConfig::PropagateRefineDistance();
0662
0663 params->tmin = SEventConfig::PropagateEpsilon() ;
0664 params->tmax = 1000000.f ;
0665 params->max_time = SEventConfig::MaxTime() ;
0666
0667
0668 }
0669
0670
0671
0672
0673
0674
0675
0676
0677 #ifdef WITH_OLD_FRAME
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698 void CSGOptiX::initFrame()
0699 {
0700 assert(0);
0701
0702 sframe _fr = foundry->getFrameE() ;
0703 LOG(LEVEL) << _fr ;
0704
0705 SEvt::SetFrame(_fr) ;
0706
0707 sfr _lfr = _fr.spawn_lite();
0708 setFrame(_lfr);
0709 }
0710
0711 #endif
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726 void CSGOptiX::initRender()
0727 {
0728 LOG(LEVEL) << "[" ;
0729 framebuf = new Frame(params->width, params->height, params->depth, nullptr ) ;
0730 LOG(LEVEL) << "]" ;
0731
0732 if(SEventConfig::IsRGModeRender())
0733 {
0734 LOG(LEVEL) << "FORMERLY CALLED CSGOptiX::setFrame FROM HERE" ;
0735
0736 }
0737
0738 params->pixels = framebuf->d_pixel ;
0739 params->isect = framebuf->d_isect ;
0740 #ifdef WITH_FRAME_PHOTON
0741 params->fphoton = framebuf->d_photon ;
0742 #else
0743 params->fphoton = nullptr ;
0744 #endif
0745 }
0746
0747 void CSGOptiX::initPIDXYZ()
0748 {
0749 qvals(params->pidxyz, "PIDXYZ", "-1:-1:-1", -1 ) ;
0750 const char* PIDXYZ = ssys::getenvvar("PIDXYZ") ;
0751 if(PIDXYZ && strcmp(PIDXYZ,"MIDDLE") == 0 )
0752 {
0753 LOG(info) << " special casing PIDXYZ MIDDLE " ;
0754 params->pidxyz.x = params->width/2 ;
0755 params->pidxyz.y = params->height/2 ;
0756 params->pidxyz.z = params->depth/2 ;
0757 }
0758
0759 LOG(LEVEL) << " params->pidxyz " << params->pidxyz ;
0760 }
0761
0762
0763 void CSGOptiX::setExternalDevicePixels(uchar4* _d_pixel )
0764 {
0765 framebuf->setExternalDevicePixels(_d_pixel) ;
0766 params->pixels = framebuf->d_pixel ;
0767 }
0768
0769
0770 void CSGOptiX::destroy()
0771 {
0772 LOG(LEVEL);
0773 delete sbt ;
0774 delete pip ;
0775 }
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798 double CSGOptiX::simulate(int eventID, bool reset)
0799 {
0800 assert(sim);
0801 double dt = sim->simulate(eventID, reset) ;
0802 return dt ;
0803 }
0804
0805
0806 void CSGOptiX::reset(int eventID)
0807 {
0808 assert(sim);
0809 sim->reset(eventID);
0810 }
0811
0812
0813
0814
0815
0816
0817
0818
0819
0820
0821
0822
0823 NP* CSGOptiX::simulate(const NP* gs, int eventID)
0824 {
0825 return sim->simulate(gs, eventID);
0826 }
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836
0837
0838
0839
0840
0841
0842
0843
0844 double CSGOptiX::simtrace(int eventID)
0845 {
0846 LOG(LEVEL) << "[" ;
0847 assert(sim);
0848 double dt = sim->simtrace(eventID) ;
0849 LOG(LEVEL) << "] " << dt ;
0850 return dt ;
0851 }
0852
0853
0854
0855
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884
0885 void CSGOptiX::setFrame()
0886 {
0887 assert(0 && " DONT USE THIS - DIRECTLY USE sglm INSTEAD" );
0888 setFrame(ssys::getenvvar("MOI", "-1"));
0889 }
0890
0891 void CSGOptiX::setFrame(const char* frs)
0892 {
0893 assert(0 && " DONT USE THIS - DIRECTLY USE sglm INSTEAD" );
0894 LOG(LEVEL) << " frs " << frs ;
0895 sframe fr = foundry->getFrame(frs) ;
0896 sfr lfr = fr.spawn_lite();
0897 setFrame(lfr);
0898
0899 }
0900 void CSGOptiX::setFrame(const float4& ce )
0901 {
0902 assert(0 && " DONT USE THIS - DIRECTLY USE sglm INSTEAD" );
0903 sfr lfr ;
0904
0905 lfr.ce.x = ce.x ;
0906 lfr.ce.y = ce.y ;
0907 lfr.ce.z = ce.z ;
0908 lfr.ce.w = ce.w ;
0909
0910 setFrame(lfr);
0911 }
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929
0930
0931
0932
0933
0934 void CSGOptiX::setFrame(const sfr& lfr )
0935 {
0936 assert(0 && " DONT USE THIS - DIRECTLY USE sglm INSTEAD" );
0937 sglm->set_frame(lfr);
0938
0939 LOG(LEVEL) << "sglm.desc:" << std::endl << sglm->desc() ;
0940
0941 LOG(LEVEL) << lfr.desc() ;
0942
0943 LOG(LEVEL)
0944 << " sglm.TMIN " << sglm->TMIN
0945 << " sglm.tmin_abs " << sglm->tmin_abs()
0946 ;
0947
0948 LOG(LEVEL) << "]" ;
0949 }
0950
0951
0952
0953
0954
0955
0956
0957 void CSGOptiX::prepareParamRender()
0958 {
0959 int prepareParamRender_DEBUG = ssys::getenvint(_prepareParamRender_DEBUG, 0) ;
0960
0961 float extent = sglm->fr.ce.w ;
0962 const glm::vec3& eye = sglm->e ;
0963 const glm::vec3& U = sglm->u ;
0964 const glm::vec3& V = sglm->v ;
0965 const glm::vec3& W = sglm->w ;
0966 const glm::vec3& WNORM = sglm->wnorm ;
0967 const glm::vec4& ZPROJ = sglm->zproj ;
0968
0969 float tmin = sglm->get_near_abs() ;
0970 float tmax = sglm->get_far_abs() ;
0971 unsigned vizmask = sglm->vizmask ;
0972 unsigned cameratype = sglm->cam ;
0973 int traceyflip = sglm->traceyflip ;
0974 int rendertype = sglm->rendertype ;
0975 float length = 0.f ;
0976
0977 LOG_IF(info, prepareParamRender_DEBUG > 0 && kernel_count == 0)
0978 << _prepareParamRender_DEBUG << ":" << prepareParamRender_DEBUG
0979 << std::endl
0980 << std::setw(20) << " kernel_count " << kernel_count << std::endl
0981 << std::setw(20) << " extent " << extent << std::endl
0982 << std::setw(20) << " sglm.fr.ce.w " << sglm->fr.ce.w << std::endl
0983 << std::setw(20) << " sglm.getGazeLength " << sglm->getGazeLength() << std::endl
0984 << std::setw(20) << " comp.length" << length
0985 << std::endl
0986 << std::setw(20) << " tmin " << tmin << std::endl
0987 << std::setw(20) << " tmax " << tmax << std::endl
0988 << std::setw(20) << " vizmask " << vizmask << std::endl
0989 << std::endl
0990 << std::setw(20) << " sglm.near " << sglm->near << std::endl
0991 << std::setw(20) << " sglm.get_near_abs " << sglm->get_near_abs() << std::endl
0992 << std::endl
0993 << std::setw(20) << " sglm.far " << sglm->far << std::endl
0994 << std::setw(20) << " sglm.get_far_abs " << sglm->get_far_abs() << std::endl
0995 << std::endl
0996 << std::setw(25) << " sglm.get_nearfar_basis " << sglm->get_nearfar_basis()
0997 << std::setw(25) << " sglm.get_nearfar_mode " << sglm->get_nearfar_mode()
0998 << std::endl
0999 << std::setw(25) << " sglm.get_focal_basis " << sglm->get_focal_basis()
1000 << std::setw(25) << " sglm.get_focal_mode " << sglm->get_focal_mode()
1001 << std::endl
1002 << std::setw(20) << " eye (" << eye.x << " " << eye.y << " " << eye.z << " ) " << std::endl
1003 << std::setw(20) << " U (" << U.x << " " << U.y << " " << U.z << " ) " << std::endl
1004 << std::setw(20) << " V (" << V.x << " " << V.y << " " << V.z << " ) " << std::endl
1005 << std::setw(20) << " W (" << W.x << " " << W.y << " " << W.z << " ) " << std::endl
1006 << std::setw(20) << " WNORM (" << WNORM.x << " " << WNORM.y << " " << WNORM.z << " ) " << std::endl
1007 << std::endl
1008 << std::setw(20) << " cameratype " << cameratype << " " << SCAM::Name(cameratype) << std::endl
1009 << std::setw(20) << " traceyflip " << traceyflip << std::endl
1010 << std::setw(20) << " sglm.cam " << sglm->cam << " " << SCAM::Name(sglm->cam) << std::endl
1011 << std::setw(20) << " ZPROJ (" << ZPROJ.x << " " << ZPROJ.y << " " << ZPROJ.z << " " << ZPROJ.w << " ) " << std::endl
1012 << std::endl
1013 << "SGLM::DescEyeBasis (sglm->e,w,v,w)\n"
1014 << SGLM::DescEyeBasis( sglm->e, sglm->u, sglm->v, sglm->w )
1015 << std::endl
1016 << std::endl
1017 << "sglm.descEyeBasis\n"
1018 << sglm->descEyeBasis()
1019 << std::endl
1020 << "Composition basis SGLM::DescEyeBasis( eye, U, V, W ) \n"
1021 << SGLM::DescEyeBasis( eye, U, V, W )
1022 << std::endl
1023 << "sglm.descELU \n"
1024 << sglm->descELU()
1025 << std::endl
1026 << std::endl
1027 << "sglm.descLog "
1028 << std::endl
1029 << sglm->descLog()
1030 << std::endl
1031 ;
1032
1033
1034
1035 params->setView(eye, U, V, W, WNORM );
1036 params->setCamera(tmin, tmax, cameratype, traceyflip, rendertype, ZPROJ );
1037 params->setVizmask(vizmask);
1038
1039 LOG(level) << std::endl << params->desc() ;
1040
1041 if(flight) return ;
1042
1043 LOG(level)
1044 << "sglm.desc " << std::endl
1045 << sglm->desc()
1046 ;
1047
1048 }
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061 void CSGOptiX::prepareParamSimulate()
1062 {
1063 LOG(LEVEL);
1064 params->set_photon_slot_offset(sim->get_photon_slot_offset());
1065 }
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082 void CSGOptiX::prepareParam()
1083 {
1084 const glm::tvec4<double>& ce = sglm->fr.ce ;
1085
1086 params->setCenterExtent(ce.x, ce.y, ce.z, ce.w);
1087 switch(raygenmode)
1088 {
1089 case SRG_RENDER : prepareParamRender() ; break ;
1090 case SRG_SIMTRACE : prepareParamSimulate() ; break ;
1091 case SRG_SIMULATE : prepareParamSimulate() ; break ;
1092 }
1093
1094 params->upload();
1095 LOG_IF(level, !flight) << params->detail();
1096 }
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122 double CSGOptiX::launch()
1123 {
1124 bool DEBUG_SKIP_LAUNCH = ssys::getenvbool("CSGOptiX__launch_DEBUG_SKIP_LAUNCH") ;
1125
1126 prepareParam();
1127 if(raygenmode != SRG_RENDER) assert(qev) ;
1128
1129 unsigned width = 0 ;
1130 unsigned height = 0 ;
1131 unsigned depth = 0 ;
1132 switch(raygenmode)
1133 {
1134 case SRG_RENDER: { width = params->width ; height = params->height ; depth = params->depth ; } ; break ;
1135 case SRG_SIMTRACE: { width = qev->getNumSimtrace() ; height = 1 ; depth = 1 ; } ; break ;
1136 case SRG_SIMULATE: { width = qev->getNumPhoton() ; height = 1 ; depth = 1 ; } ; break ;
1137 }
1138
1139 bool expect = width > 0 ;
1140 LOG_IF(fatal, !expect)
1141 << " qev.getNumSimtrace " << ( qev ? qev->getNumSimtrace() : -1 )
1142 << " qev.getNumPhoton " << ( qev ? qev->getNumPhoton() : -1 )
1143 << " width " << width
1144 << " height " << height
1145 << " depth " << depth
1146 << " expect " << ( expect ? "YES" : "NO " )
1147 ;
1148
1149 assert(expect );
1150
1151 typedef std::chrono::time_point<std::chrono::high_resolution_clock> TP ;
1152 typedef std::chrono::duration<double> DT ;
1153 TP _t0 = std::chrono::high_resolution_clock::now();
1154 int64_t t0 = sstamp::Now();
1155
1156 LOG(LEVEL)
1157 << " raygenmode " << raygenmode
1158 << " SRG::Name(raygenmode) " << SRG::Name(raygenmode)
1159 << " width " << width
1160 << " height " << height
1161 << " depth " << depth
1162 << " DEBUG_SKIP_LAUNCH " << ( DEBUG_SKIP_LAUNCH ? "YES" : "NO " )
1163 ;
1164
1165 if(DEBUG_SKIP_LAUNCH == false)
1166 {
1167 CUdeviceptr d_param = (CUdeviceptr)Params::d_param ; ;
1168 assert( d_param && "must alloc and upload params before launch");
1169
1170
1171 cudaStream_t stream = 0 ;
1172 OPTIX_CHECK( optixLaunch( pip->pipeline, (CUstream)stream, d_param, sizeof( Params ), &(sbt->sbt), width, height, depth ) );
1173
1174 CUDA_SYNC_CHECK();
1175
1176
1177 kernel_count += 1 ;
1178 }
1179
1180
1181 TP _t1 = std::chrono::high_resolution_clock::now();
1182 DT _dt = _t1 - _t0;
1183
1184 int64_t t1 = sstamp::Now();
1185 int64_t dt = t1 - t0 ;
1186
1187 kernel_dt = _dt.count() ;
1188 kernel_times.push_back(kernel_dt);
1189
1190 kernel_times_.push_back(dt);
1191
1192 LOG(LEVEL)
1193 << " (params.width, params.height, params.depth) ( "
1194 << params->width << "," << params->height << "," << params->depth << ")"
1195 << std::fixed << std::setw(7) << std::setprecision(4) << kernel_dt
1196 ;
1197 return kernel_dt ;
1198 }
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220 double CSGOptiX::render_launch()
1221 {
1222 assert(raygenmode == SRG_RENDER) ;
1223 return launch() ;
1224 }
1225 double CSGOptiX::simtrace_launch()
1226 {
1227 assert(raygenmode == SRG_SIMTRACE) ;
1228 return launch() ;
1229 }
1230 double CSGOptiX::simulate_launch()
1231 {
1232 assert(raygenmode == SRG_SIMULATE) ;
1233 return launch() ;
1234 }
1235
1236 const CSGFoundry* CSGOptiX::getFoundry() const
1237 {
1238 return foundry ;
1239 }
1240
1241 std::string CSGOptiX::AnnotationTime( double dt, const char* extra )
1242 {
1243 std::stringstream ss ;
1244 ss << std::fixed << std::setw(10) << std::setprecision(4) << dt ;
1245 if(extra) ss << " " << extra << " " ;
1246 std::string str = ss.str();
1247 return str ;
1248 }
1249 std::string CSGOptiX::Annotation( double dt, const char* bot_line, const char* extra )
1250 {
1251 std::stringstream ss ;
1252 ss << AnnotationTime(dt, extra) ;
1253 if(bot_line) ss << std::setw(30) << " " << bot_line ;
1254 std::string str = ss.str();
1255 return str ;
1256 }
1257
1258 const char* CSGOptiX::getDefaultSnapPath() const
1259 {
1260 assert( foundry );
1261 const char* cfbase = foundry->getOriginCFBase();
1262 assert( cfbase );
1263 const char* path = SPath::Resolve(cfbase, "CSGOptiX/snap.jpg" , FILEPATH );
1264 return path ;
1265 }
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298 const char* CSGOptiX::getRenderStemDefault() const
1299 {
1300 const std::string& fr_name = sglm->fr.get_name() ;
1301
1302 std::stringstream ss ;
1303 ss << ssys::getenvvar("NAMEPREFIX","nonamepfx") ;
1304 ss << "_" ;
1305 ss << ( fr_name.empty() ? "no_frame_name" : fr_name ) ;
1306
1307 std::string str = ss.str();
1308 return strdup(str.c_str());
1309 }
1310
1311
1312
1313
1314 bool CSGOptiX::handle_snap(int wanted_snap)
1315 {
1316 bool can_handle = wanted_snap == 1 || wanted_snap == 2 ;
1317 if(!can_handle) return false ;
1318 switch(wanted_snap)
1319 {
1320 case 1: render_save() ; break ;
1321 case 2: render_save_inverted() ; break ;
1322 }
1323 return true ;
1324 }
1325
1326
1327
1328
1329
1330
1331
1332 double CSGOptiX::render( const char* stem_ )
1333 {
1334 render_launch();
1335 render_save(stem_);
1336 return kernel_dt ;
1337 }
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348 void CSGOptiX::render_save(const char* stem_)
1349 {
1350 render_save_(stem_, false);
1351 }
1352 void CSGOptiX::render_save_inverted(const char* stem_)
1353 {
1354 render_save_(stem_, true);
1355 }
1356
1357
1358 void CSGOptiX::render_save_(const char* stem_, bool inverted)
1359 {
1360 const char* outdir = SEventConfig::OutDir();
1361 const char* stem = stem_ ? stem_ : getRenderStemDefault() ;
1362
1363 bool unique = true ;
1364 const char* outpath = SEventConfig::OutPath(stem, -1, ".jpg", unique );
1365
1366 LOG(LEVEL)
1367 << SEventConfig::DescOutPath(stem, -1, ".jpg", unique );
1368 ;
1369
1370 std::string u_outdir ;
1371 std::string u_stem ;
1372 std::string u_ext ;
1373
1374 [[maybe_unused]] int rc = spath::SplitExt( u_outdir, u_stem, u_ext, outpath ) ;
1375 assert(rc == 0 );
1376
1377 sglm->addlog("CSGOptiX::render_snap", u_stem.c_str() );
1378
1379
1380 const char* topline = ssys::getenvvar("TOPLINE", sproc::ExecutableName() );
1381 std::string _extra = SEventConfig::GetGPUMeta();
1382 const char* extra = strdup(_extra.c_str()) ;
1383
1384 const char* botline_ = ssys::getenvvar("BOTLINE", nullptr );
1385 std::string bottom_line = CSGOptiX::Annotation(kernel_dt, botline_, extra );
1386 const char* botline = bottom_line.c_str() ;
1387
1388
1389 LOG(LEVEL)
1390 << " stem " << stem
1391 << " outpath " << outpath
1392 << " outdir " << ( outdir ? outdir : "-" )
1393 << " kernel_dt " << kernel_dt
1394 << " topline [" << topline << "]"
1395 << " botline [" << botline << "]"
1396 ;
1397
1398 LOG(info) << outpath << " : " << AnnotationTime(kernel_dt, extra) ;
1399
1400 unsigned line_height = 24 ;
1401 snap(outpath, botline, topline, line_height, inverted );
1402
1403
1404 sglm->save( u_outdir.c_str(), u_stem.c_str() );
1405 }
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423 void CSGOptiX::snap(const char* path_, const char* bottom_line, const char* top_line, unsigned line_height, bool inverted )
1424 {
1425 const char* path = path_ ? SPath::Resolve(path_, FILEPATH ) : getDefaultSnapPath() ;
1426 LOG(LEVEL) << " path " << path ;
1427
1428 #if OPTIX_VERSION < 70000
1429 const char* top_extra = nullptr ;
1430 #else
1431 const char* top_extra = pip->desc();
1432 #endif
1433 const char* topline = SStr::Concat(top_line, top_extra);
1434
1435 LOG(LEVEL) << " path_ [" << path_ << "]" ;
1436 LOG(LEVEL) << " topline " << topline ;
1437
1438 LOG(LEVEL) << "[ frame.download " ;
1439 if( inverted == false )
1440 {
1441 framebuf->download();
1442 }
1443 else
1444 {
1445 framebuf->download_inverted();
1446 }
1447 LOG(LEVEL) << "] frame.download " ;
1448
1449 LOG(LEVEL) << "[ frame.annotate " ;
1450 framebuf->annotate( bottom_line, topline, line_height );
1451 LOG(LEVEL) << "] frame.annotate " ;
1452
1453 LOG(LEVEL) << "[ frame.snap " ;
1454 framebuf->snap( path );
1455 LOG(LEVEL) << "] frame.snap " ;
1456
1457 if(!flight || SStr::Contains(path,"00000"))
1458 {
1459 saveMeta(path);
1460 }
1461 }
1462
1463 #ifdef WITH_FRAME_PHOTON
1464 void CSGOptiX::writeFramePhoton(const char* dir, const char* name)
1465 {
1466 #if OPTIX_VERSION < 70000
1467 assert(0 && "not implemented pre-7");
1468 #else
1469 framebuf->writePhoton(dir, name);
1470 #endif
1471 }
1472 #endif
1473
1474
1475 int CSGOptiX::render_flightpath()
1476 {
1477 LOG(fatal) << "flightpath rendering not yet implemented in now default SGLM branch " ;
1478 return 1 ;
1479 }
1480
1481 void CSGOptiX::saveMeta(const char* jpg_path) const
1482 {
1483 const char* json_path = SStr::ReplaceEnd(jpg_path, ".jpg", ".json");
1484 LOG(LEVEL) << "[ json_path " << json_path ;
1485
1486 nlohmann::json& js = meta->js ;
1487 js["jpg"] = jpg_path ;
1488 js["emm"] = SGeoConfig::EnabledMergedMesh() ;
1489
1490 if(foundry->hasMeta())
1491 {
1492 js["cfmeta"] = foundry->meta ;
1493 }
1494
1495 std::string extra = SEventConfig::GetGPUMeta();
1496 js["scontext"] = extra.empty() ? "-" : strdup(extra.c_str()) ;
1497
1498 const std::vector<double>& t = kernel_times ;
1499 if( t.size() > 0 )
1500 {
1501 double mn, mx, av ;
1502 SVec<double>::MinMaxAvg(t,mn,mx,av);
1503
1504 js["mn"] = mn ;
1505 js["mx"] = mx ;
1506 js["av"] = av ;
1507 }
1508
1509 meta->save(json_path);
1510 LOG(LEVEL) << "] json_path " << json_path ;
1511 }
1512
1513
1514
1515 void CSGOptiX::write_Ctx_log(const char* dir) const
1516 {
1517 #if OPTIX_VERSION < 70000
1518 #else
1519 std::string ctxlog = Ctx::GetLOG() ;
1520 spath::Write(ctxlog.c_str() , dir, CTX_LOGNAME );
1521 #endif
1522 }
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537 #define xstr(s) str(s)
1538 #define str(s) #s
1539
1540 int CSGOptiX::_OPTIX_VERSION()
1541 {
1542 char vers[16] ;
1543 snprintf(vers, 16, "%s",xstr(OPTIX_VERSION));
1544 return std::atoi(vers) ;
1545 }