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_Pipeline.h : Create OptixPipeline from OptixModule
0004 ==========================================================
0005 
0006 **/
0007 
0008 #include "SOPTIX_Options.h"
0009 
0010 
0011 struct SOPTIX_Pipeline
0012 {
0013     static int Initialize();
0014 
0015     static constexpr const char* RG = "__raygen__rg" ;
0016     static constexpr const char* MS = "__miss__ms" ;
0017     static constexpr const char* IS = "__intersection__is" ;
0018     static constexpr const char* CH = "__closesthit__ch" ;
0019     static constexpr const char* AH = "__anyhit__ah" ;
0020 
0021     int irc ;
0022     OptixDeviceContext& context ;
0023     OptixModule& module ;
0024     const SOPTIX_Options& options ;
0025 
0026     OptixProgramGroup raygen_pg   = nullptr;
0027     OptixProgramGroup miss_pg     = nullptr;
0028     OptixProgramGroup hitgroup_pg = nullptr;
0029 
0030     OptixPipeline pipeline = nullptr;
0031 
0032     OptixStackSizes stackSizes = {};
0033 
0034     uint32_t max_trace_depth = 1;   // only RG invokes trace, no recursion
0035     uint32_t max_cc_depth = 0;
0036     uint32_t max_dc_depth = 0;
0037 
0038     uint32_t directCallableStackSizeFromTraversal;
0039     uint32_t directCallableStackSizeFromState;
0040     uint32_t continuationStackSize;
0041 
0042     // see optix7-;optix7-host : it states that IAS->GAS needs to be two
0043     unsigned maxTraversableGraphDepth = 2 ;
0044 
0045 
0046 
0047     std::string desc() const ;
0048 
0049     SOPTIX_Pipeline(
0050         OptixDeviceContext& context,
0051         OptixModule& module,
0052         const SOPTIX_Options& options
0053         );
0054 
0055     void init();
0056     void initRaygen();
0057     void initMiss();
0058     void initHitgroup();
0059     void initPipeline();
0060 
0061     std::string descStack() const ;
0062     static std::string DescStackSizes(const OptixStackSizes& stackSizes );
0063 
0064 };
0065 
0066 
0067 inline int SOPTIX_Pipeline::Initialize()
0068 {
0069     if(SOPTIX_Options::Level()>0) std::cout << "-SOPTIX_Pipeline::Initialize\n";
0070     return 0 ;
0071 }
0072 
0073 inline std::string SOPTIX_Pipeline::desc() const
0074 {
0075     std::stringstream ss ;
0076     ss << "[SOPTIX_Pipeline::desc"  ;
0077     ss << " options\n" << options.desc() << "\n" ;
0078     ss << " pipeline " << ( pipeline ? "YES" : "NO " ) << "\n" ;
0079     ss << descStack() << "\n" ;
0080     ss << "]SOPTIX_Pipeline::desc"  ;
0081     std::string str = ss.str() ;
0082     return str ;
0083 }
0084 
0085 
0086 
0087 inline SOPTIX_Pipeline::SOPTIX_Pipeline(
0088     OptixDeviceContext& _context,
0089     OptixModule& _module,
0090     const SOPTIX_Options& _options
0091     )
0092     :
0093     irc(Initialize()),
0094     context(_context),
0095     module(_module),
0096     options(_options)
0097 {
0098     init();
0099 }
0100 
0101 inline void SOPTIX_Pipeline::init()
0102 {
0103     if(SOPTIX_Options::Level()>0) std::cout << "[SOPTIX_Pipeline::init\n";
0104     initRaygen();
0105     initMiss();
0106     initHitgroup();
0107     initPipeline();
0108     if(SOPTIX_Options::Level()>0) std::cout << "]SOPTIX_Pipeline::init\n";
0109 }
0110 
0111 
0112 inline void SOPTIX_Pipeline::initRaygen()
0113 {
0114     if(SOPTIX_Options::Level()>0) std::cout << "[SOPTIX_Pipeline::initRaygen\n";
0115     OptixProgramGroupKind kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN ;
0116     OptixProgramGroupFlags flags = OPTIX_PROGRAM_GROUP_FLAGS_NONE ;
0117 
0118     OptixProgramGroupDesc desc = {};
0119     desc.kind = kind ;
0120     desc.flags = flags ;
0121     desc.raygen.module = module;
0122     desc.raygen.entryFunctionName = RG ;
0123 
0124     size_t sizeof_log = 0 ;
0125     char log[2048];
0126     unsigned num_program_groups = 1 ;
0127 
0128     OPTIX_CHECK_LOG( optixProgramGroupCreate(
0129                 context,
0130                 &desc,
0131                 num_program_groups,
0132                 &(options.programGroupOptions),
0133                 log,
0134                 &sizeof_log,
0135                 &raygen_pg
0136                 ) );
0137 
0138     if(sizeof_log > 0) std::cout << log << std::endl ;
0139     assert( sizeof_log == 0);
0140     if(SOPTIX_Options::Level()>0) std::cout << "]SOPTIX_Pipeline::initRaygen\n";
0141 }
0142 
0143 inline void SOPTIX_Pipeline::initMiss()
0144 {
0145     if(SOPTIX_Options::Level()>0) std::cout << "[SOPTIX_Pipeline::initMiss\n";
0146     OptixProgramGroupKind kind = OPTIX_PROGRAM_GROUP_KIND_MISS ;
0147     OptixProgramGroupFlags flags = OPTIX_PROGRAM_GROUP_FLAGS_NONE ;
0148 
0149     OptixProgramGroupDesc desc = {};
0150     desc.kind = kind ;
0151     desc.flags = flags ;
0152     desc.miss.module = module;
0153     desc.miss.entryFunctionName = MS ;
0154 
0155     size_t sizeof_log = 0 ;
0156     char log[2048];
0157     unsigned num_program_groups = 1 ;
0158 
0159     OPTIX_CHECK_LOG( optixProgramGroupCreate(
0160                 context,
0161                 &desc,
0162                 num_program_groups,
0163                 &(options.programGroupOptions),
0164                 log,
0165                 &sizeof_log,
0166                 &miss_pg
0167                 ) );
0168 
0169     if(sizeof_log > 0) std::cout << log << std::endl ;
0170     assert( sizeof_log == 0);
0171     if(SOPTIX_Options::Level()>0) std::cout << "]SOPTIX_Pipeline::initMiss\n";
0172 }
0173 
0174 
0175 /**
0176 SOPTIX_Pipeline::initHitgroup
0177 --------------------------------
0178 
0179 HMM: no IS for tri ?
0180 
0181 **/
0182 
0183 inline void SOPTIX_Pipeline::initHitgroup()
0184 {
0185     if(SOPTIX_Options::Level()>0) std::cout << "[SOPTIX_Pipeline::initHitgroup\n";
0186     OptixProgramGroupKind kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP  ;
0187     OptixProgramGroupFlags flags = OPTIX_PROGRAM_GROUP_FLAGS_NONE ;
0188     OptixProgramGroupDesc desc = {};
0189     desc.kind = kind ;
0190     desc.flags = flags ;
0191 
0192     desc.hitgroup.moduleCH            = module  ;
0193     desc.hitgroup.entryFunctionNameCH = CH ;
0194 
0195     desc.hitgroup.moduleAH            = nullptr ;
0196     desc.hitgroup.entryFunctionNameAH = nullptr ;
0197 
0198     desc.hitgroup.moduleIS            = nullptr ;
0199     desc.hitgroup.entryFunctionNameIS = nullptr ;
0200 
0201 
0202     size_t sizeof_log = 0 ;
0203     char log[2048];
0204     unsigned num_program_groups = 1 ;
0205 
0206 
0207     OPTIX_CHECK_LOG( optixProgramGroupCreate(
0208                 context,
0209                 &desc,
0210                 num_program_groups,
0211                 &(options.programGroupOptions),
0212                 log,
0213                 &sizeof_log,
0214                 &hitgroup_pg
0215                 ) );
0216 
0217     if(sizeof_log > 0) std::cout << log << std::endl ;
0218     assert( sizeof_log == 0);
0219     if(SOPTIX_Options::Level()>0) std::cout << "]SOPTIX_Pipeline::initHitgroup\n";
0220 }
0221 
0222 inline void SOPTIX_Pipeline::initPipeline()
0223 {
0224     if(SOPTIX_Options::Level()>0) std::cout << "[SOPTIX_Pipeline::initPipeline\n";
0225     OptixProgramGroup program_groups[] = { raygen_pg, miss_pg, hitgroup_pg };
0226     unsigned num_program_groups = sizeof( program_groups ) / sizeof( program_groups[0] ) ;
0227 
0228     size_t sizeof_log = 0 ;
0229     char log[2048];
0230 
0231     if(SOPTIX_Options::Level()>0) std::cout << "[SOPTIX_Pipeline::initPipeline.Create\n";
0232     OPTIX_CHECK_LOG( optixPipelineCreate(
0233                 context,
0234                 &(options.pipelineCompileOptions),
0235                 &(options.pipelineLinkOptions),
0236                 program_groups,
0237                 num_program_groups,
0238                 log,
0239                 &sizeof_log,
0240                 &pipeline
0241                 ) );
0242 
0243     if(SOPTIX_Options::Level()>0) std::cout << "]SOPTIX_Pipeline::initPipeline.Create\n";
0244     if(sizeof_log > 0) std::cout << "[log\n" << log << "\n]log\n" ;
0245     assert( sizeof_log == 0);
0246 
0247 
0248     for(unsigned i=0 ; i < num_program_groups ; i++)
0249     {
0250         OptixProgramGroup& pg = program_groups[i] ;
0251 
0252 #if OPTIX_VERSION <= 70600
0253         OPTIX_CHECK( optixUtilAccumulateStackSizes( pg, &stackSizes ) );
0254 #else
0255         OPTIX_CHECK( optixUtilAccumulateStackSizes( pg, &stackSizes, pipeline ) );
0256 #endif
0257     }
0258 
0259     OPTIX_CHECK( optixUtilComputeStackSizes(
0260                 &stackSizes,
0261                 max_trace_depth,
0262                 max_cc_depth,
0263                 max_dc_depth,
0264                 &directCallableStackSizeFromTraversal,
0265                 &directCallableStackSizeFromState,
0266                 &continuationStackSize
0267                 ) );
0268 
0269 
0270     OPTIX_CHECK( optixPipelineSetStackSize( pipeline,
0271                                        directCallableStackSizeFromTraversal,
0272                                        directCallableStackSizeFromState,
0273                                        continuationStackSize,
0274                                        maxTraversableGraphDepth ) ) ;
0275     if(SOPTIX_Options::Level()>0) std::cout << "]SOPTIX_Pipeline::initPipeline\n";
0276 }
0277 
0278 
0279 
0280 std::string SOPTIX_Pipeline::descStack() const
0281 {
0282     std::stringstream ss ;
0283     ss << "SOPTIX_Pipeline::descStack"
0284        << std::endl
0285        << "(inputs to optixUtilComputeStackSizes)"
0286        << std::endl
0287        << " max_trace_depth " << max_trace_depth
0288        << " max_cc_depth " << max_cc_depth
0289        << " max_dc_depth " << max_dc_depth
0290        << std::endl
0291        << " program_group stackSizes "
0292        << DescStackSizes(stackSizes)
0293        << "(outputs from optixUtilComputeStackSizes) "
0294        << std::endl
0295        << " directCallableStackSizeFromTraversal " << directCallableStackSizeFromTraversal
0296        << std::endl
0297        << " directCallableStackSizeFromState " << directCallableStackSizeFromState
0298        << std::endl
0299        << " continuationStackSize " << continuationStackSize
0300        << std::endl
0301        << "(further inputs to optixPipelineSetStackSize)"
0302        << std::endl
0303        << " maxTraversableGraphDepth " << maxTraversableGraphDepth
0304        << std::endl
0305        ;
0306 
0307     std::string str = ss.str() ;
0308     return str ;
0309 }
0310 
0311 
0312 std::string SOPTIX_Pipeline::DescStackSizes(const OptixStackSizes& stackSizes ) // static
0313 {
0314     std::stringstream ss ;
0315     ss
0316         << " stackSizes.cssRG " << stackSizes.cssRG << "\n"
0317         << " stackSizes.cssMS " << stackSizes.cssMS << "\n"
0318         << " stackSizes.cssCH " << stackSizes.cssCH << "\n"
0319         << " stackSizes.cssAH " << stackSizes.cssAH << "\n"
0320         << " stackSizes.cssIS " << stackSizes.cssIS << "\n"
0321         << " stackSizes.cssCC " << stackSizes.cssCC << "\n"
0322         << " stackSizes.dssDC " << stackSizes.dssDC << "\n"
0323         ;
0324     std::string str = ss.str() ;
0325     return str ;
0326 }
0327