Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:53

0001 #include <vector>
0002 #include <iostream>
0003 #include <iomanip>
0004 #include <cassert>
0005 
0006 #include "cuda.h"
0007 #include "CU.h"
0008 
0009 #include "scuda.h"
0010 #include "CSGPrim.h"
0011 
0012 
0013 CSGPrim make_prim( float extent, unsigned idx )
0014 {
0015      CSGPrim pr = {} ;
0016      pr.setAABB( extent );
0017      pr.setSbtIndexOffset(idx);
0018      return pr ;
0019 }
0020 
0021 
0022 
0023 void test_AABB()
0024 {
0025     CSGPrim p0 = {} ;
0026     p0.setAABB( 42.42f );
0027     std::cout << "p0 " << p0.desc() << std::endl ;
0028 
0029     CSGPrim p1 = {} ;
0030     p1.setAABB( p0.AABB() );
0031     std::cout << "p1 " << p1.desc() << std::endl ;
0032 }
0033 
0034 
0035 
0036 void test_offsets()
0037 {
0038      std::cout << "test_offsets " << std::endl ;
0039      std::cout
0040         <<  "offsetof(struct CSGPrim, q0) " <<  offsetof(struct CSGPrim,  q0) << std::endl
0041         <<  "offsetof(struct CSGPrim, q0)/sizeof(float) " <<  offsetof(struct CSGPrim, q0)/sizeof(float) << std::endl
0042         ;
0043      std::cout
0044         <<  "offsetof(struct CSGPrim, q1) " <<  offsetof(struct CSGPrim,  q1) << std::endl
0045         <<  "offsetof(struct CSGPrim, q1)/sizeof(float) " <<  offsetof(struct CSGPrim, q1)/sizeof(float) << std::endl
0046         ;
0047 
0048      std::cout
0049         <<  "offsetof(struct CSGPrim, q2) " <<  offsetof(struct CSGPrim,  q2) << std::endl
0050         <<  "offsetof(struct CSGPrim, q2)/sizeof(float) " <<  offsetof(struct CSGPrim, q2)/sizeof(float) << std::endl
0051         ;
0052      std::cout
0053         <<  "offsetof(struct CSGPrim, q3) " <<  offsetof(struct CSGPrim,  q3) << std::endl
0054         <<  "offsetof(struct CSGPrim, q3)/sizeof(float) " <<  offsetof(struct CSGPrim, q3)/sizeof(float) << std::endl
0055         ;
0056 }
0057 
0058 
0059 void test_spec( const std::vector<CSGPrim>& prim )
0060 {
0061      std::cout << "test_spec " << std::endl ;
0062      SCSGPrimSpec psa = CSGPrim::MakeSpec(prim.data(), 0, prim.size() );
0063      psa.dump();
0064 
0065      std::vector<float> out ;
0066      psa.gather(out);
0067      SCSGPrimSpec::Dump(out);
0068 }
0069 
0070 void test_partial( const std::vector<CSGPrim>& prim )
0071 {
0072      std::cout << "test_partial " << std::endl ;
0073      unsigned h = prim.size()/2 ;
0074 
0075      SCSGPrimSpec ps0 = CSGPrim::MakeSpec(prim.data(), 0, h );
0076      ps0.dump();
0077 
0078      SCSGPrimSpec ps1 = CSGPrim::MakeSpec(prim.data(), h, h );
0079      ps1.dump();
0080 }
0081 
0082 CSGPrim* test_upload( const CSGPrim* prim, unsigned num )
0083 {
0084      std::cout << "test_upload" << std::endl ;
0085      CSGPrim* d_prim = CU::UploadArray<CSGPrim>(prim, num ) ;
0086      assert( d_prim );
0087      return d_prim ;
0088 }
0089 void test_download( const CSGPrim* d_prim, unsigned num )
0090 {
0091      std::cout << "test_download" << std::endl ;
0092      CSGPrim* prim2 = CU::DownloadArray<CSGPrim>( d_prim,  num ) ;
0093      for(unsigned i=0 ; i < num ; i++)
0094      {
0095          CSGPrim* p = prim2 + i  ;
0096          std::cout << i << std::endl << p->desc() << std::endl ;
0097      }
0098 }
0099 
0100 /**
0101 Highly inefficienct noddy appoach as not worth getting into
0102 thrust (or cudaMemcpy2D) complications for strided downloads just for this debug check
0103 **/
0104 
0105 void DownloadDump( const SCSGPrimSpec& d_ps )
0106 {
0107      for(unsigned i=0 ; i < d_ps.num_prim ; i++)
0108      {
0109          const unsigned* u_ptr = d_ps.sbtIndexOffset + (d_ps.stride_in_bytes/sizeof(unsigned))*i ;
0110          const float*    f_ptr = d_ps.aabb           + (d_ps.stride_in_bytes/sizeof(float))*i ;
0111 
0112          float*     f = CU::DownloadArray<float>( f_ptr, 6 );
0113          unsigned*  u = CU::DownloadArray<unsigned>( u_ptr, 1 );
0114 
0115          std::cout << " off " << *(u) << " aabb (" << i << ") " ;
0116          for( unsigned i=0 ; i < 6 ; i++ ) std::cout << *(f+i) << " " ;
0117          std::cout << std::endl ;
0118 
0119          delete [] f ;
0120          delete [] u ;
0121      }
0122 }
0123 
0124 SCSGPrimSpec test_dspec( CSGPrim* d_prim , unsigned num)
0125 {
0126      std::cout << "test_dspec" << std::endl ;
0127      SCSGPrimSpec d_ps = CSGPrim::MakeSpec( d_prim, 0, num );
0128      DownloadDump(d_ps);
0129 
0130      return d_ps ;
0131 }
0132 
0133 
0134 void test_pointer( const void* d, const char* label )
0135 {
0136      std::cout << "test_pointer " << label << std::endl ;
0137 
0138      const void* vd = (const void*) d ;
0139      uintptr_t ud = (uintptr_t)d ; // uintptr_t is an unsigned integer type that is capable of storing a data pointer.
0140      CUdeviceptr cd = (CUdeviceptr) (uintptr_t) d ;  // CUdeviceptr is typedef to unsigned long lonh
0141 
0142      std::cout << "            (const void*) d " << vd << std::endl ;
0143      std::cout << "               (uintptr_t)d " << std::dec << ud << " " << std::hex << ud  << std::dec << std::endl ;
0144      std::cout << "  (CUdeviceptr)(uintptr_t)d " << std::dec << cd << " " << std::hex << cd  << std::dec << std::endl ;
0145 }
0146 
0147 int main(int argc, char** argv)
0148 {
0149      test_AABB();
0150      test_offsets();
0151 
0152      std::vector<CSGPrim> prim ;
0153      for(unsigned i=0 ; i < 10 ; i++) prim.push_back(make_prim(float(i+1), i*10 ));
0154 
0155      test_spec(prim);
0156      test_partial(prim);
0157 
0158      CSGPrim* d_prim = test_upload(prim.data(), prim.size());
0159      test_download( d_prim, prim.size() );
0160 
0161      SCSGPrimSpec d_ps = test_dspec(d_prim, prim.size() ) ;
0162 
0163      test_pointer( d_prim, "d_prim" );
0164      test_pointer( d_ps.aabb ,           "d_ps.aabb" );
0165      test_pointer( d_ps.sbtIndexOffset , "d_ps.sbtIndexOffset" );
0166 
0167      return 0 ;
0168 }