SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLOptix.cpp
Go to the documentation of this file.
1 /**
2  * \file SLOptix.cpp
3  * \authors Nic Dorner
4  * \date October 2019
5  * \authors Nic Dorner
6  * \copyright http://opensource.org/licenses/GPL-3.0
7  * \remarks Please use clangformat to format the code. See more code style on
8  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
9 */
10 
11 #ifdef SL_HAS_OPTIX
12 # include <iomanip>
13 # include <SL.h>
14 # include <SLOptix.h>
15 # include <SLOptixHelper.h>
16 
17 //-----------------------------------------------------------------------------
18 // Global statics
19 OptixDeviceContext SLOptix::context = {};
20 CUstream SLOptix::stream = {};
21 bool SLOptix::available = false;
22 string SLOptix::exePath;
23 //-----------------------------------------------------------------------------
24 //! callback function for optix
25 void context_log_cb(unsigned int level,
26  const char* tag,
27  const char* message,
28  void* /*cbdata */)
29 {
30  std::cerr << "[" << std::setw(2) << level << "][" << std::setw(12) << tag << "]: "
31  << message << "\n";
32 }
33 //-----------------------------------------------------------------------------
34 //! creates the optix and cuda context for the application
35 /*! Every failure in here is reported by OPTIX_CHECK and CUDA_CHECK as a thrown
36 SLOptixException, and none of them is fatal to the application: an OptiX context
37 is a bonus and the OpenGL, ray tracing and path tracing renderers do not need
38 one. So the exception is caught, SLOptix::available stays false, and the OptiX
39 menu entries stay greyed out. Letting it escape would abort the process before
40 the first window appears, which is what used to happen on a machine whose driver
41 no longer serves this OptiX ABI. */
42 void SLOptix::createStreamAndContext()
43 {
44  try
45  {
46  createStreamAndContextOrThrow();
47  }
48  catch (std::exception& e)
49  {
50  SLOptix::available = false;
51  SL_LOG("**** OptiX is not available on this machine ****");
52  SL_LOG("%s", e.what());
53  SL_LOG("The OptiX menu entries stay disabled. The usual causes are a");
54  SL_LOG("display driver too old for the OptiX ABI of the headers in");
55  SL_LOG("externals/lib-optix, or no NVIDIA GPU at all.");
56  }
57 }
58 //-----------------------------------------------------------------------------
59 //! Does the work of createStreamAndContext and throws on any failure
60 void SLOptix::createStreamAndContextOrThrow()
61 {
62  // Initialize CUDA
63  CUcontext cu_ctx;
64  CUDA_CHECK(cuInit(0));
65  CUDA_CHECK(cuMemFree(0));
66  // CUDA 13 renamed cuCtxCreate to cuCtxCreate_v4 and gave it a fourth
67  // argument, a CUctxCreateParams* that carries the green context and
68  // affinity settings this code does not use. The macro in cuda.h points
69  // cuCtxCreate at whichever version the installed toolkit ships, so the
70  // call has to follow it. The project declares CUDA 10 as its minimum, so
71  // both forms stay reachable.
72 #if CUDA_VERSION >= 13000
73  CUDA_CHECK(cuCtxCreate(&cu_ctx, nullptr, 0, 0));
74 #else
75  CUDA_CHECK(cuCtxCreate(&cu_ctx, 0, 0));
76 #endif
77  CUDA_CHECK(cuStreamCreate(&SLOptix::stream,
78  CU_STREAM_DEFAULT));
79 
80  // Initialize OptiX
81  OPTIX_CHECK(optixInit());
82  OptixDeviceContextOptions options = {};
83  options.logCallbackFunction = &context_log_cb;
84  options.logCallbackLevel = 4;
85  OPTIX_CHECK(optixDeviceContextCreate(cu_ctx,
86  &options,
87  &SLOptix::context));
88 
89  SLOptix::available = true;
90 }
91 //-----------------------------------------------------------------------------
92 #endif
#define SL_LOG(...)
Some debugging and error handling macros.
Definition: SL.h:279
Error checking macros and helpers for the OptiX ray tracer.