File indexing completed on 2026-04-09 07:49:42
0001 #pragma once
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include "ssys.h"
0015 #include "SOPTIX_BuildInput_IA.h"
0016 #include "SOPTIX_Options.h"
0017
0018 struct SOPTIX_Scene
0019 {
0020 static int Initialize();
0021
0022 static constexpr const char* _DUMP = "SOPTIX_Scene__DUMP" ;
0023
0024 int irc ;
0025 int level ;
0026 bool DUMP ;
0027 SOPTIX_Context* ctx ;
0028 const SScene* scene ;
0029
0030 std::vector<SOPTIX_MeshGroup*> meshgroup ;
0031 std::vector<SOPTIX_Accel*> meshgas ;
0032 std::vector<OptixInstance> instances ;
0033
0034 SOPTIX_Accel* ias ;
0035
0036 std::string desc() const;
0037 std::string descGAS() const;
0038 std::string descIAS() const;
0039
0040 SOPTIX_Scene( SOPTIX_Context* ctx, const SScene* scene );
0041
0042 void init();
0043
0044 void init_GAS();
0045 void init_Instances();
0046 void init_IAS();
0047
0048 OptixTraversableHandle getHandle(int idx) const ;
0049 };
0050
0051
0052 inline int SOPTIX_Scene::Initialize()
0053 {
0054 if(SOPTIX_Options::Level()>0) std::cout << "-SOPTIX_Scene::Initialize\n" ;
0055 return 0 ;
0056 }
0057
0058 inline std::string SOPTIX_Scene::desc() const
0059 {
0060 int num_mm = scene->meshmerge.size() ;
0061 int num_mg = scene->meshgroup.size() ;
0062 std::stringstream ss ;
0063 ss << "[ SOPTIX_Scene::desc"
0064 << " num_mm " << num_mm
0065 << " num_mg " << num_mg
0066 << std::endl
0067 ;
0068 ss << descGAS() ;
0069 ss << descIAS() ;
0070 ss << "] SOPTIX_Scene::desc " << std::endl ;
0071 std::string str = ss.str();
0072 return str ;
0073 }
0074 inline std::string SOPTIX_Scene::descGAS() const
0075 {
0076 int num_gas = int(meshgroup.size());
0077 std::stringstream ss ;
0078 ss << "[ SOPTIX_Scene::descGAS num_gas " << num_gas << std::endl ;
0079 for(int i=0 ; i < num_gas ; i++ ) ss << meshgroup[i]->desc() ;
0080 ss << "] SOPTIX_Scene::descGAS num_gas " << num_gas << std::endl ;
0081 std::string str = ss.str();
0082 return str ;
0083 }
0084
0085 inline std::string SOPTIX_Scene::descIAS() const
0086 {
0087 std::stringstream ss ;
0088 ss << "[ SOPTIX_Scene::descIAS\n" ;
0089 ss << ias->desc() ;
0090 ss << "] SOPTIX_Scene::descIAS\n" ;
0091 std::string str = ss.str();
0092 return str ;
0093 }
0094
0095 inline SOPTIX_Scene::SOPTIX_Scene( SOPTIX_Context* _ctx, const SScene* _scene )
0096 :
0097 irc(Initialize()),
0098 level(SOPTIX_Options::Level()),
0099 DUMP(ssys::getenvbool(_DUMP)),
0100 ctx(_ctx),
0101 scene(_scene),
0102 ias(nullptr)
0103 {
0104 init();
0105 }
0106
0107 inline void SOPTIX_Scene::init()
0108 {
0109 if(level>0) std::cout << "[SOPTIX_Scene::init\n" ;
0110 init_GAS();
0111 init_Instances();
0112 init_IAS();
0113 if(level>0) std::cout << "]SOPTIX_Scene::init\n" ;
0114 }
0115
0116 inline void SOPTIX_Scene::init_GAS()
0117 {
0118 int num_mg = scene->meshgroup.size() ;
0119 if(level>0) std::cout << "[SOPTIX_Scene::init_GAS num_mg " << num_mg << std::endl ;
0120
0121 for(int i=0 ; i < num_mg ; i++)
0122 {
0123 if(level>0) std::cout << "-[SOPTIX_Scene::init_GAS i " << i << std::endl ;
0124 const SMeshGroup* mg = scene->meshgroup[i];
0125 SOPTIX_MeshGroup* xmg = SOPTIX_MeshGroup::Create(mg) ;
0126 meshgroup.push_back(xmg);
0127
0128 SOPTIX_Accel* gas = SOPTIX_Accel::Create( ctx->context, xmg->bis );
0129 meshgas.push_back(gas);
0130 if(level>0) std::cout << "-]SOPTIX_Scene::init_GAS i " << i << std::endl ;
0131 }
0132 if(level>0) std::cout << "]SOPTIX_Scene::init_GAS num_mg " << num_mg << std::endl ;
0133 }
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159 inline void SOPTIX_Scene::init_Instances()
0160 {
0161 size_t num_gas = scene->inst_info.size();
0162 size_t num_inst = scene->inst_tran.size();
0163 if(level>0) std::cout << "[SOPTIX_Scene::init_Instances num_gas " << num_gas << " num_inst " << num_inst << std::endl ;
0164
0165
0166 [[maybe_unused]] size_t num_col3 = scene->inst_col3.size();
0167 assert( num_inst == num_col3 );
0168
0169 const std::vector<glm::tmat4x4<float>>& inst_tran = scene->inst_tran ;
0170
0171 if(DUMP) std::cout
0172 << "SOPTIX_Scene::init_Instances"
0173 << " num_gas " << num_gas
0174 << " num_inst " << num_inst
0175 << " num_col3 " << num_col3
0176 << std::endl
0177 ;
0178
0179 unsigned tot = 0 ;
0180 unsigned flags = OPTIX_INSTANCE_FLAG_NONE ;
0181 flags |= OPTIX_INSTANCE_FLAG_DISABLE_TRIANGLE_FACE_CULLING ;
0182 flags |= OPTIX_INSTANCE_FLAG_DISABLE_ANYHIT ;
0183
0184 size_t sbtOffset = 0 ;
0185 for(unsigned i=0 ; i < num_gas ; i++)
0186 {
0187 const int4& _inst_info = scene->inst_info[i] ;
0188 unsigned ridx = _inst_info.x ;
0189 unsigned count = _inst_info.y ;
0190 unsigned offset = _inst_info.z ;
0191
0192 SOPTIX_MeshGroup* xmg = meshgroup[i] ;
0193 size_t num_bi = xmg->num_buildInputs();
0194 SOPTIX_Accel* gas = meshgas[i] ;
0195 OptixTraversableHandle handle = gas->handle ;
0196
0197 unsigned visibilityMask = ctx->props->visibilityMask(i) ;
0198
0199 if(DUMP) std::cout
0200 << "SOPTIX_Scene::init_Instances"
0201 << " i " << i
0202 << " ridx (_inst_info.x) " << ridx
0203 << " count (_inst_info.y " << count
0204 << " offset (_inst_info.z) " << offset
0205 << " num_bi " << num_bi
0206 << " visibilityMask " << visibilityMask
0207 << " sbtOffset " << sbtOffset
0208 << "\n"
0209 ;
0210
0211 assert( ridx == i );
0212 for(unsigned j=0 ; j < count ; j++)
0213 {
0214 unsigned idx = offset + j ;
0215 bool in_range = idx < num_inst ;
0216
0217 if(DUMP && in_range == false) std::cout
0218 << "SOPTIX_Scene::init_Instances"
0219 << " j " << j
0220 << " (offset + j)[idx] " << idx
0221 << " num_inst " << num_inst
0222 << " in_range " << ( in_range ? "YES" : "NO " )
0223 << " tot " << tot
0224 << std::endl
0225 ;
0226
0227 assert( in_range );
0228 assert( idx == tot );
0229 tot += 1 ;
0230
0231 const glm::tmat4x4<float>& tran = inst_tran[idx] ;
0232 OptixInstance instance = {} ;
0233
0234 stra<float>::Copy_Columns_3x4( instance.transform, tran );
0235 instance.instanceId = idx ;
0236 instance.sbtOffset = sbtOffset ;
0237 instance.visibilityMask = visibilityMask ;
0238 instance.flags = flags ;
0239 instance.traversableHandle = handle ;
0240
0241 instances.push_back(instance);
0242 }
0243 sbtOffset += num_bi ;
0244 }
0245 if(level>0) std::cout << "]SOPTIX_Scene::init_Instances num_gas " << num_gas << " num_inst " << num_inst << std::endl ;
0246 }
0247
0248 inline void SOPTIX_Scene::init_IAS()
0249 {
0250 if(level>0) std::cout << "[SOPTIX_Scene::init_IAS\n" ;
0251 SOPTIX_BuildInput* bi = new SOPTIX_BuildInput_IA(instances);
0252 ias = SOPTIX_Accel::Create( ctx->context, bi );
0253 if(level>0) std::cout << "]SOPTIX_Scene::init_IAS\n" ;
0254 }
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266 inline OptixTraversableHandle SOPTIX_Scene::getHandle(int idx) const
0267 {
0268 assert( idx < int(meshgas.size())) ;
0269 return idx == -1 ? ias->handle : meshgas[idx]->handle ;
0270 }
0271
0272