Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #pragma once
0002 
0003 #if defined(__CUDACC__) || defined(__CUDABE__)
0004 #else
0005 #include <cstdlib>
0006 #include <vector>
0007 #include <string>
0008 #endif
0009 
0010 /**
0011 SCSGPrimSpec :
0012 =========================================================================
0013 
0014 This was migrated down from CSG/CSGPrimSpec.h 
0015 
0016 * *SCSGPrimSpec* provides the specification to access the AABB and sbtIndexOffset of all CSGPrim of a CSGSolid.  
0017 * The specification includes pointers, counts and stride.
0018 * Instances are created for a solidIdx by CSGFoundry::getPrimSpec using CSGPrim::MakeSpec
0019 * Crucially *SCSGPrimSpec* is used to pass the AABB for a solid to CSGOptix/GAS_Builder.
0020 
0021 Previously assumed that the *sbtIndexOffset* indices were global 
0022 to the entire geometry, but the 2nd-GAS-last-prim-only bug indicates 
0023 that the indices need to be local to each GAS, counting 
0024 from 0 to numPrim-1 for that GAS.
0025 
0026 **/
0027 
0028 struct SCSGPrimSpec
0029 {
0030     static constexpr const unsigned CSGPrim__value_offsetof_sbtIndexOffset = 4 ;
0031     static constexpr const unsigned CSGPrim__value_offsetof_AABB = 8 ; 
0032   
0033     const float*    aabb ; 
0034     const unsigned* sbtIndexOffset ;   
0035     unsigned        num_prim ; 
0036     unsigned        stride_in_bytes ; 
0037     bool            device ; 
0038     unsigned        primitiveIndexOffset ;   // offsets optixGetPrimitiveIndex() see GAS_Builder::MakeCustomPrimitivesBI_11N
0039 
0040 #if defined(__CUDACC__) || defined(__CUDABE__)
0041 #else
0042     void downloadDump(const char* msg="SCSGPrimSpec::downloadDump") const ; 
0043     void gather(std::vector<float>& out) const ;
0044     static void Dump(std::vector<float>& out);
0045     void dump(const char* msg="SCSGPrimSpec::dump", int modulo=100) const ; 
0046     std::string desc() const ; 
0047 #endif
0048 };
0049 
0050 
0051 
0052 #if defined(__CUDACC__) || defined(__CUDABE__)
0053 #else
0054 
0055 #include <vector>
0056 #include <iostream>
0057 #include <iomanip>
0058 #include <cassert>
0059 #include <cstring>
0060 
0061 #include "scuda.h"
0062 
0063 #include "SCU.h"
0064 
0065 /**
0066 SCSGPrimSpec::gather
0067 ---------------------
0068 
0069 Writes num_prim*6 bbox floats into out. 
0070 
0071 **/
0072 
0073 inline void SCSGPrimSpec::gather(std::vector<float>& out) const 
0074 {
0075     //assert( device == false ); 
0076     unsigned size_in_floats = 6 ; 
0077     out.resize( num_prim*size_in_floats ); 
0078 
0079     unsigned stride_in_floats = stride_in_bytes/sizeof(float) ; 
0080     for(unsigned i=0 ; i < num_prim ; i++) 
0081     {   
0082         float* dst = out.data() + size_in_floats*i ;   
0083         const float* src = aabb + stride_in_floats*i ;   
0084         memcpy(dst, src,  sizeof(float)*size_in_floats );  
0085     }   
0086 }
0087 
0088 inline void SCSGPrimSpec::Dump(std::vector<float>& out)  // static 
0089 {
0090      std::cout << " gather " << out.size() << std::endl ; 
0091      for(unsigned i=0 ; i < out.size() ; i++) 
0092      {    
0093          if(i % 6 == 0) std::cout << std::endl ; 
0094          std::cout << std::setw(10) << out[i] << " " ; 
0095      } 
0096      std::cout << std::endl ; 
0097 }
0098 
0099 
0100 inline void SCSGPrimSpec::dump(const char* msg, int modulo) const 
0101 {
0102     assert( stride_in_bytes % sizeof(float) == 0 ); 
0103     unsigned stride_in_floats = stride_in_bytes/sizeof(float) ; 
0104     std::cout 
0105         << msg 
0106         << " num_prim " << num_prim 
0107         << " stride_in_bytes " << stride_in_bytes 
0108         << " stride_in_floats " << stride_in_floats 
0109         << " modulo " << modulo
0110         << std::endl 
0111         ; 
0112 
0113     for(unsigned i=0 ; i < num_prim ; i++)
0114     {   
0115         if( modulo == 0 || ( modulo > 0 && i % modulo == 0 ) )
0116         {
0117             std::cout 
0118                 << " i " << std::setw(4) << i 
0119                 << " sbtIndexOffset " << std::setw(4) << *(sbtIndexOffset + i*stride_in_floats)   
0120                 ; 
0121             for(unsigned j=0 ; j < 6 ; j++)  
0122                 std::cout << std::setw(10) << std::fixed << std::setprecision(3) << *(aabb + i*stride_in_floats + j ) << " "  ;   
0123             std::cout << std::endl ; 
0124         }
0125     }   
0126 }
0127 
0128 
0129 inline std::string SCSGPrimSpec::desc() const 
0130 {
0131     std::stringstream ss ; 
0132 
0133     ss << "SCSGPrimSpec"
0134        << " primitiveIndexOffset " << std::setw(4) << primitiveIndexOffset
0135        << " num_prim " << std::setw(4) << num_prim 
0136        << " stride_in_bytes " << std::setw(5) << stride_in_bytes 
0137        << " device " << std::setw(2) << device
0138        ;
0139 
0140     std::string s = ss.str(); 
0141     return s ; 
0142 }
0143 
0144 
0145 /**
0146 SCSGPrimSpec::downloadDump
0147 ---------------------------
0148 
0149 As are starting the read from within the structure, 
0150 need to trim offsets to avoid reading beyond the array.
0151 
0152 **/
0153 
0154 inline void SCSGPrimSpec::downloadDump(const char* msg) const 
0155 {
0156     assert( device == true ); 
0157     unsigned stride_in_values = stride_in_bytes/sizeof(float) ; 
0158     unsigned numValues = stride_in_values*num_prim ;   
0159     unsigned nff = numValues - CSGPrim__value_offsetof_AABB ;  
0160     unsigned nuu = numValues - CSGPrim__value_offsetof_sbtIndexOffset ;
0161     std::cout 
0162         << "[ " << msg 
0163         << " num_prim " << num_prim 
0164         << " stride_in_values " << stride_in_values
0165         << " numValues " << numValues
0166         << " nff " << nff
0167         << " nuu " << nuu
0168         << " CSGPrim__value_offsetof_AABB " << CSGPrim__value_offsetof_AABB
0169         << " CSGPrim__value_offsetof_sbtIndexOffset " << CSGPrim__value_offsetof_sbtIndexOffset
0170         << std::endl 
0171         ; 
0172 
0173     assert( stride_in_values == 16 ); 
0174 
0175     std::vector<unsigned> uu ; 
0176     std::vector<float> ff ; 
0177 
0178     SCU::DownloadVec(ff,           aabb, nff); 
0179     SCU::DownloadVec(uu, sbtIndexOffset, nuu ); 
0180 
0181     for(unsigned i=0 ; i < num_prim ; i++)
0182     {
0183         std::cout << std::setw(5) << *(uu.data()+stride_in_values*i + 0) << " " ;
0184         for(int j=0 ; j < 6 ; j++) 
0185         {
0186             std::cout 
0187                 << std::fixed << std::setw(8) << std::setprecision(2) 
0188                 << *(ff.data()+stride_in_values*i + j) 
0189                 << " " 
0190                 ;
0191         } 
0192         std::cout << std::endl ; 
0193     }
0194     std::cout << "] " << msg << std::endl ; 
0195 }
0196 
0197 
0198 #endif
0199