SLProject  4.2.000
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 <SLOptix.h>
14 # include <SLOptixHelper.h>
15 
16 //-----------------------------------------------------------------------------
17 // Global statics
18 OptixDeviceContext SLOptix::context = {};
19 CUstream SLOptix::stream = {};
20 string SLOptix::exePath;
21 //-----------------------------------------------------------------------------
22 //! callback function for optix
23 void context_log_cb(unsigned int level,
24  const char* tag,
25  const char* message,
26  void* /*cbdata */)
27 {
28  std::cerr << "[" << std::setw(2) << level << "][" << std::setw(12) << tag << "]: "
29  << message << "\n";
30 }
31 //-----------------------------------------------------------------------------
32 //! creates the optix and cuda context for the application
33 void SLOptix::createStreamAndContext()
34 {
35  // Initialize CUDA
36  CUcontext cu_ctx;
37  CUDA_CHECK(cuInit(0));
38  CUDA_CHECK(cuMemFree(0));
39  CUDA_CHECK(cuCtxCreate(&cu_ctx, 0, 0));
40  CUDA_CHECK(cuStreamCreate(&SLOptix::stream,
41  CU_STREAM_DEFAULT));
42 
43  // Initialize OptiX
44  OPTIX_CHECK(optixInit());
45  OptixDeviceContextOptions options = {};
46  options.logCallbackFunction = &context_log_cb;
47  options.logCallbackLevel = 4;
48  OPTIX_CHECK(optixDeviceContextCreate(cu_ctx,
49  &options,
50  &SLOptix::context));
51 }
52 //-----------------------------------------------------------------------------
53 #endif