Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:49:32

0001 /**
0002 CSGOptiXRenderInteractiveTest.cc : Interactive raytrace rendering of analytic geometry
0003 ========================================================================================
0004 
0005 Analytic CSGOptiX rendering with SGLM/SGLFW interactive control/visualization.
0006 Usage with::
0007 
0008    ~/o/cx.sh
0009    ~/o/CSGOptiX/cxr_min.sh
0010 
0011 Note this provides only CSGOptiX ray trace rendering, there is no rasterized toggle
0012 with "C" key to switch between CUDA and OpenGL rendering.
0013 For that functionality (but with triangulated geometry only) use:
0014 
0015    ~/o/sysrap/tests/SGLFW_SOPTIX_Scene_test.sh
0016    ~/o/sysrap/tests/SGLFW_SOPTIX_Scene_test.cc
0017 
0018 TODO:
0019 
0020 * navigation frames currently managed in SScene(tri) but they
0021   are equally relevant to ana+tri.
0022 
0023   * Relocate the navigation frames, where ? Consider the workflow.
0024 
0025 * frame hopping like ~/o/sysrap/tests/SGLFW_SOPTIX_Scene_test.cc
0026 
0027 * interactive vizmask control to hide/show geometry
0028 
0029 * WIP: enable mix and match ana/tri geometry, by incorporation
0030   of SScene and SOPTIX
0031 
0032 * imgui reincarnation
0033 
0034 
0035 **/
0036 
0037 #include "ssys.h"
0038 #include "stree.h"
0039 #include "OPTICKS_LOG.hh"
0040 #include "SEventConfig.hh"
0041 #include "CSGFoundry.h"
0042 #include "CSGOptiX.h"
0043 #include "SGLFW.h"
0044 #include "SGLFW_CUDA.h"
0045 #include "SGLFW_Evt.h"
0046 
0047 #include "SScene.h"
0048 
0049 
0050 
0051 struct CSGOptiXRenderInteractiveTest
0052 {
0053     static constexpr const char* _level = "CSGOptiXRenderInteractiveTest__level" ;
0054     static constexpr const char* _ALLOW_REMOTE = "CSGOptiXRenderInteractiveTest__ALLOW_REMOTE" ;
0055     static int Initialize(bool allow_remote);
0056 
0057     int level ;
0058     bool ALLOW_REMOTE ;
0059     int irc ;
0060 
0061     SRecord*    ar ;
0062     SRecord*    br ;
0063 
0064     CSGFoundry* fd ;
0065     SGLM*       gm ;
0066     CSGOptiX*   cx ;
0067 
0068     SGLFW*      gl ;
0069     SGLFW_CUDA* interop ;   // holder of CUDA/OptiX buffer
0070     SGLFW_Evt*  glev ;
0071 
0072     CSGOptiXRenderInteractiveTest();
0073 
0074     void init();
0075 
0076     void handle_snap_cx();
0077     void optix_render_to_buffer();
0078     void display_optix_buffer();
0079     void optix_render();
0080 
0081     std::string desc() const ;
0082 };
0083 
0084 
0085 inline int CSGOptiXRenderInteractiveTest::Initialize(bool allow_remote)
0086 {
0087     SEventConfig::SetRGModeRender();
0088     bool is_remote_session = ssys::is_remote_session();
0089     if(is_remote_session && allow_remote == false )
0090     {
0091         std::cout << "CSGOptiXRenderInteractiveTest::Initialize : ABORTING : as detected remote session from SSH_TTY SSH_CLIENT \n";
0092         std::cout << "to override : export " << _ALLOW_REMOTE << "=1  ## warning have see gnome-shell crash with Wayland \n" ;
0093         return 1 ;
0094     }
0095     return 0 ;
0096 }
0097 
0098 
0099 inline CSGOptiXRenderInteractiveTest::CSGOptiXRenderInteractiveTest()
0100     :
0101     level(ssys::getenvint(_level,0)),
0102     ALLOW_REMOTE(ssys::getenvbool(_ALLOW_REMOTE)),
0103     irc(Initialize(ALLOW_REMOTE)),
0104     ar(SRecord::Load("$AFOLD", "$AFOLD_RECORD_SLICE")),
0105     br(SRecord::Load("$BFOLD", "$BFOLD_RECORD_SLICE")),
0106     fd(CSGFoundry::Load()),
0107     gm(new SGLM),
0108     cx(nullptr),
0109     gl(nullptr),
0110     interop(nullptr),
0111     glev(nullptr)
0112 {
0113     init();
0114 }
0115 
0116 inline void CSGOptiXRenderInteractiveTest::init()
0117 {
0118     assert( irc == 0 );
0119     assert(fd);
0120     stree* tree = fd->getTree();
0121     assert(tree);
0122     SScene* scene = fd->getScene() ;
0123     assert(scene);
0124     gm->setTreeScene(tree, scene);
0125     gm->setRecord(ar, br);
0126 
0127     cx = CSGOptiX::Create(fd) ;
0128     gl = new SGLFW(*gm);
0129     interop = new SGLFW_CUDA(*gm);
0130     glev    = new SGLFW_Evt(*gl);
0131 
0132     if(gl->level > 0) std::cout << "CSGOptiXRenderInteractiveTest::init before render loop  gl.get_wanted_frame_idx " <<  gl->get_wanted_frame_idx() << "\n" ;
0133     if(level > 0) std::cout << "CSGOptiXRenderInteractiveTest::init [" << _level << "][" << level << "]\n" << desc() ;
0134 
0135 }
0136 
0137 
0138 /**
0139 CSGOptiXRenderInteractiveTest::handle_snap_cx
0140 ----------------------------------------------
0141 
0142 Saves ray trace geometry screenshots when certain keys pressed.
0143 Formerly done between render_launch and unmap
0144 
0145 **/
0146 
0147 inline void CSGOptiXRenderInteractiveTest::handle_snap_cx()
0148 {
0149     int wanted_snap = gl->get_wanted_snap();
0150     if(cx->handle_snap(wanted_snap)) gl->set_wanted_snap(0);
0151 }
0152 
0153 /**
0154 CSGOptiXRenderInteractiveTest::optix_render_to_buffer
0155 ---------------------------------------------------------
0156 
0157 1. interop.output_buffer::map : pass "baton" to CUDA/OptiX via d_pixels,
0158    device side pointer where to write kernel output
0159 
0160 2. ray tracing OptiX launch populating the pixels
0161 
0162 3. interop.output_buffer::unmap : pass baton back to OpenGL for display
0163 
0164 **/
0165 
0166 inline void CSGOptiXRenderInteractiveTest::optix_render_to_buffer()
0167 {
0168     uchar4* d_pixels = interop->output_buffer->map() ;
0169     cx->setExternalDevicePixels(d_pixels);
0170     cx->render_launch();
0171     interop->output_buffer->unmap() ;
0172 }
0173 
0174 inline void CSGOptiXRenderInteractiveTest::display_optix_buffer()
0175 {
0176     interop->displayOutputBuffer(gl->window);
0177 }
0178 
0179 
0180 inline void CSGOptiXRenderInteractiveTest::optix_render()
0181 {
0182     handle_snap_cx();
0183     optix_render_to_buffer();
0184     display_optix_buffer();
0185 }
0186 
0187 inline std::string CSGOptiXRenderInteractiveTest::desc() const
0188 {
0189     std::stringstream ss ;
0190     ss
0191         << "[CSGOptiXRenderInteractiveTest::desc\n"
0192         << " ar\n" << ( ar ? ar->desc() : "-" ) << "\n"
0193         << " br\n" << ( br ? br->desc() : "-" ) << "\n"
0194         << "]CSGOptiXRenderInteractiveTest::desc\n"
0195         ;
0196     std::string str = ss.str() ;
0197     return str ;
0198 }
0199 
0200 
0201 
0202 int main(int argc, char** argv)
0203 {
0204     OPTICKS_LOG(argc, argv);
0205 
0206     CSGOptiXRenderInteractiveTest t ;
0207 
0208     SGLFW* gl = t.gl ;
0209 
0210     while(gl->renderloop_proceed())
0211     {
0212         gl->renderloop_head();
0213         gl->handle_frame_hop();
0214 
0215         if(gl->gm.option.O) t.optix_render(); // alt-O toggle
0216         t.glev->render(); // alt-A/B toggle record array render
0217 
0218         gl->renderloop_tail();
0219     }
0220     return 0 ;
0221 }
0222