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_Module.h : Create OptixModule loaded from file
0004 ================================================================
0005 
0006 WIP : Generalize this to work with optixir binary as well as PTX text
0007 
0008 7.5 p48
0009 
0010 After using modules to create an OptixPipeline through the OptixProgramGroup
0011 objects, modules may be destroyed with optixModuleDestroy.
0012 
0013 
0014 **/
0015 
0016 #include "SOPTIX_Options.h" 
0017 #include "spath.h"
0018 #include "sstr.h"
0019 
0020 struct SOPTIX_Module
0021 { 
0022     static int Initialize(); 
0023 
0024     enum { UNKNOWN, PTX, OPTIXIR } ; 
0025     static constexpr const char* UNKNOWN_ = "UNKNOWN" ; 
0026     static constexpr const char* PTX_     = "PTX" ; 
0027     static constexpr const char* OPTIXIR_ = "OPTIXIR" ; 
0028     static const char* Type(int type) ; 
0029 
0030     int irc ; 
0031     OptixDeviceContext& context ; 
0032     const SOPTIX_Options& options ; 
0033     const char* path ; 
0034     int type ; 
0035     std::vector<char> bin ; 
0036     OptixModule module ; 
0037     
0038     std::string desc() const ;
0039 
0040     SOPTIX_Module( 
0041         OptixDeviceContext& context, 
0042         const SOPTIX_Options& options,
0043         const char* _path
0044         ); 
0045 
0046     void init(); 
0047 };
0048 
0049 inline int SOPTIX_Module::Initialize()
0050 {
0051     if(SOPTIX_Options::Level()>0) std::cout << "-SOPTIX_Module::Initialize\n" ; 
0052     return 0 ;
0053 }
0054 
0055 inline const char* SOPTIX_Module::Type(int type)
0056 {
0057     const char* str = nullptr ; 
0058     switch(type)
0059     {
0060         case UNKNOWN: str = UNKNOWN_ ; break ; 
0061         case PTX:     str = PTX_     ; break ; 
0062         case OPTIXIR: str = OPTIXIR_ ; break ; 
0063     }
0064     return str ; 
0065 
0066 }
0067 
0068 inline std::string SOPTIX_Module::desc() const 
0069 {
0070     std::stringstream ss ; 
0071     ss << "[SOPTIX_Module::desc"  ;
0072     ss << " path\n" << path << "\n" ; 
0073     ss << " type\n" << type << "\n" ; 
0074     ss << " Type\n" << Type(type) << "\n" ; 
0075     ss << " bin.size\n" << bin.size() << "\n" ; 
0076     ss << " options\n" << options.desc() << "\n" ; 
0077     ss << "]SOPTIX_Module::desc"  ;
0078     std::string str = ss.str() ; 
0079     return str ; 
0080 }
0081 
0082 
0083 inline SOPTIX_Module::SOPTIX_Module( 
0084     OptixDeviceContext&   _context, 
0085     const SOPTIX_Options& _options, 
0086     const char*           _path
0087     )
0088     :
0089     irc(Initialize()),
0090     context(_context),
0091     options(_options),
0092     path(spath::Resolve(_path)),
0093     type(UNKNOWN)
0094 {
0095     init();
0096 }
0097 
0098 
0099 /**
0100 SOPTIX_Module::init
0101 ---------------------
0102 
0103 HMM: in OptiX 7.5 seems the optixModuleCreateFromPTX 
0104 also does optixir ? 
0105 
0106 **/
0107 
0108 inline void SOPTIX_Module::init()
0109 {
0110     int level = SOPTIX_Options::Level() ; 
0111     if(level>0) std::cout << "[SOPTIX_Module::init level " << level << "\n" ; 
0112     if(      sstr::EndsWith(path, "ptx") )    type = PTX  ; 
0113     else if( sstr::EndsWith(path, "optixir")) type = OPTIXIR ; 
0114 
0115     bool read_ok = spath::Read(bin, path );  // should work with PTX as well as OPTIXIR
0116     if(level > 1) std::cout << "-SOPTIX_Module::init\n" << desc() << "\n" ; 
0117 
0118     assert(  read_ok );    
0119 
0120     size_t sizeof_log = 0 ;
0121     char log[2048]; 
0122 
0123 #if OPTIX_VERSION <= 70600
0124     OPTIX_CHECK_LOG( optixModuleCreateFromPTX(
0125                 context,
0126                 &options.moduleCompileOptions,
0127                 &options.pipelineCompileOptions,
0128                 bin.data(),
0129                 bin.size(),
0130                 log,
0131                 &sizeof_log,
0132                 &module
0133                 ) );
0134 #else
0135     OPTIX_CHECK_LOG( optixModuleCreate(
0136                 context,
0137                 &options.moduleCompileOptions,
0138                 &options.pipelineCompileOptions,
0139                 bin.data(),
0140                 bin.size(),
0141                 log,
0142                 &sizeof_log,
0143                 &module
0144                 ) );
0145 
0146 #endif
0147 
0148     std::string _log( log, log+sizeof_log ); 
0149     if(sizeof_log > 0) std::cout << "[optixModuleCreate _log \n" << _log  << "\n]optixModuleCreate _log\n" ;
0150     assert( sizeof_log == 0 ); 
0151     if(level > 0) std::cout << "]SOPTIX_Module::init\n" ; 
0152 }
0153