Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:42

0001 #pragma once
0002 /**
0003 SOPTIX.h : top level coordinator of triangulated raytrace render
0004 =================================================================
0005 
0006 envvars
0007 --------
0008 
0009 SOPTIX__HANDLE
0010 
0011 
0012 
0013 **/
0014 
0015 #include "ssys.h"
0016 #include "config.h"
0017 #include "SOPTIX_Context.h"
0018 #include "SOPTIX_Desc.h"
0019 #include "SOPTIX_MeshGroup.h"
0020 #include "SOPTIX_Accel.h"
0021 #include "SOPTIX_Scene.h"
0022 #include "SOPTIX_Module.h"
0023 #include "SOPTIX_Pipeline.h"
0024 #include "SOPTIX_SBT.h"
0025 #include "SOPTIX_Params.h"
0026 #include "SOPTIX_Pixels.h"
0027 #include "SOPTIX_Options.h"
0028 
0029 
0030 struct SOPTIX
0031 {
0032     static constexpr const char* __HANDLE = "SOPTIX__HANDLE" ;
0033     static int  Initialize();
0034 
0035     int             irc ;
0036     SGLM&           gm  ;
0037     std::string     optixpath;
0038 
0039     SOPTIX_Context  ctx ;
0040     SOPTIX_Options  opt ;
0041     SOPTIX_Module   mod ;
0042     SOPTIX_Pipeline pip ;
0043     SOPTIX_Scene    scn ;
0044     SOPTIX_SBT      sbt ;
0045     SOPTIX_Params   par = {} ;
0046     SOPTIX_Params*  d_param ;
0047 
0048     CUstream stream = 0 ;
0049     unsigned depth = 1 ;
0050 
0051     int HANDLE ;
0052     OptixTraversableHandle handle ;
0053 
0054     SOPTIX_Pixels*  pix ;   // optional internally managed pixels
0055 
0056     SOPTIX(SGLM& _gm );
0057     void init();
0058 
0059     void set_param(uchar4* d_pixels);
0060     void render(uchar4* d_pixels);
0061     void render_ppm(const char* _path);
0062 
0063     std::string desc() const ;
0064 };
0065 
0066 
0067 inline int SOPTIX::Initialize()
0068 {
0069     if(SOPTIX_Options::Level() > 0) std::cout << "[SOPTIX::Initialize\n" ;
0070     return 0 ;
0071 }
0072 
0073 
0074 inline SOPTIX::SOPTIX(SGLM& _gm)
0075     :
0076     irc(Initialize()),
0077     gm(_gm),
0078     optixpath(gphox::Config::PtxPath("SOPTIX.ptx")),
0079     mod(ctx.context, opt, optixpath.c_str()),
0080     pip(ctx.context, mod.module, opt ),
0081     scn(&ctx, gm.scene ),
0082     sbt(pip, scn ),
0083     d_param(SOPTIX_Params::DeviceAlloc()),
0084     HANDLE(ssys::getenvint(__HANDLE, -1)),
0085     handle(scn.getHandle(HANDLE)),
0086     pix(nullptr)
0087 {
0088     init();
0089 }
0090 
0091 inline void SOPTIX::init()
0092 {
0093     if(SOPTIX_Options::Level() > 0)  std::cout << "]SOPTIX::init \n" ;
0094     if(SOPTIX_Options::Level() > 1)  std::cout << desc() ;
0095 }
0096 
0097 
0098 /**
0099 SOPTIX::set_param
0100 ------------------
0101 
0102 cf CSGOptiX::prepareParamRender
0103 
0104 **/
0105 
0106 
0107 inline void SOPTIX::set_param(uchar4* d_pixels)
0108 {
0109     par.width = gm.Width() ;
0110     par.height = gm.Height() ;
0111     par.pixels = d_pixels ;
0112     par.tmin = gm.get_near_abs() ;
0113     par.tmax = gm.get_far_abs() ;
0114     par.cameratype = gm.cam ;
0115     par.vizmask = gm.vizmask ;
0116 
0117     SGLM::Copy(&par.eye.x, gm.e );
0118     SGLM::Copy(&par.U.x  , gm.u );
0119     SGLM::Copy(&par.V.x  , gm.v );
0120     SGLM::Copy(&par.W.x  , gm.w );
0121 
0122     SGLM::Copy(&par.WNORM.x, gm.wnorm );
0123     SGLM::Copy(&par.ZPROJ.x, gm.zproj );
0124 
0125 
0126     par.handle = handle ;
0127 
0128     par.upload(d_param);
0129 }
0130 
0131 inline void SOPTIX::render(uchar4* d_pixels)
0132 {
0133     set_param(d_pixels);
0134 
0135     OPTIX_CHECK( optixLaunch(
0136                  pip.pipeline,
0137                  stream,
0138                  (CUdeviceptr)d_param,
0139                  sizeof( SOPTIX_Params ),
0140                  &(sbt.sbt),
0141                  gm.Width(),
0142                  gm.Height(),
0143                  depth
0144                  ) );
0145 
0146     CUDA_SYNC_CHECK();
0147 }
0148 
0149 
0150 inline void SOPTIX::render_ppm(const char* _path)
0151 {
0152     const char* path = spath::Resolve(_path);
0153 
0154     std::cout << "SOPTIX::render_ppm [" << ( path ? path : "-" ) << "]\n" ;
0155 
0156     if(!pix) pix = new SOPTIX_Pixels(gm) ;
0157     render(pix->d_pixels);
0158 
0159     pix->download();
0160 
0161     pix->save_ppm(path);
0162 }
0163 
0164 
0165 inline std::string SOPTIX::desc() const
0166 {
0167     std::stringstream ss ;
0168     ss
0169         << "[SOPTIX::desc\n"
0170         << " optixpath  [" << optixpath << "]\n"
0171         << " [" << __HANDLE << "] " << HANDLE << "\n"
0172         << "]SOPTIX::desc\n"
0173         ;
0174     std::string str = ss.str() ;
0175     return str ;
0176 }
0177