SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLOptixHelper.h
Go to the documentation of this file.
1 /**
2  * \file SLOptixHelper.h
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 # ifndef SLOPTIXHELPER_H
13 # define SLOPTIXHELPER_H
14 
15 # include <iostream> // std::cout, std::ios
16 # include <sstream> // std::ostringstream
17 # include <stdexcept>
18 # include <string>
19 # include <functional>
20 # include <chrono>
21 # include <SLVec3.h>
22 # include <SLVec4.h>
23 
24 using namespace std;
25 
26 using namespace std::placeholders;
27 using namespace std::chrono;
28 
29 // Optix error-checking and CUDA error-checking are copied from nvidia optix sutil
30 //------------------------------------------------------------------------------
31 // OptiX error-checking
32 //------------------------------------------------------------------------------
33 
34 # include <optix_stubs.h>
35 // clang-format off
36 //------------------------------------------------------------------------------
37 #define OPTIX_CHECK( call ) \
38  { \
39  OptixResult res = call; \
40  if( res != OPTIX_SUCCESS ) \
41  { \
42  stringstream ss; \
43  ss << "Optix call '" << #call << "' failed: " __FILE__ ":" \
44  << __LINE__ << ")\n"; \
45  throw SLOptixException( res, ss.str().c_str() ); \
46  } \
47  }
48 //------------------------------------------------------------------------------
49 #define OPTIX_CHECK_LOG( call ) \
50  { \
51  OptixResult res = call; \
52  if( res != OPTIX_SUCCESS ) \
53  { \
54  stringstream ss; \
55  ss << "Optix call '" << #call << "' failed: " __FILE__ ":" \
56  << __LINE__ << ")\nLog:\n" << log \
57  << ( sizeof_log > sizeof( log ) ? "<TRUNCATED>" : "" ) \
58  << "\n"; \
59  throw SLOptixException( res, ss.str().c_str() ); \
60  } \
61  }
62 //------------------------------------------------------------------------------
63 // CUDA error-checking
64 //------------------------------------------------------------------------------
65 
66 #define CUDA_CHECK( call ) \
67  { \
68  CUresult result = call; \
69  if( result != CUDA_SUCCESS ) \
70  { \
71  const char *errorstr; \
72  cuGetErrorString(result, &errorstr); \
73  stringstream ss; \
74  ss << "CUDA call (" << #call << " ) failed with error: '" \
75  << errorstr \
76  << "' (" __FILE__ << ":" << __LINE__ << ")\n" \
77  << result << "\n"; \
78  throw SLOptixException( ss.str().c_str() ); \
79  } \
80  }
81 //------------------------------------------------------------------------------
82 #define CUDA_SYNC_CHECK( call ) \
83  { \
84  CUstream stream = call; \
85  CUresult result = cuStreamSynchronize(stream); \
86  if( result != CUDA_SUCCESS ) \
87  { \
88  const char *errorstr; \
89  cuGetErrorString(result, &errorstr); \
90  stringstream ss; \
91  ss << "CUDA error on synchronize with error '" \
92  << errorstr \
93  << "' (" __FILE__ << ":" << __LINE__ << ")\n"; \
94  throw SLOptixException( ss.str().c_str() ); \
95  } \
96  }
97 //------------------------------------------------------------------------------
98 // clang-format on
99 class SLOptixException : public std::runtime_error
100 {
101 public:
102  SLOptixException(const char* msg)
103  : std::runtime_error(msg)
104  {
105  }
106 
107  SLOptixException(OptixResult res, const char* msg)
108  : std::runtime_error(createMessage(res, msg).c_str())
109  {
110  }
111 
112 private:
113  string createMessage(OptixResult res, const char* msg)
114  {
115  std::ostringstream os;
116  os << optixGetErrorName(res) << ": " << msg;
117  return os.str();
118  }
119 };
120 //------------------------------------------------------------------------------
121 // Get PTX string from File
122 string getPtxStringFromFile(
123  string filename, // Cuda C input file name
124  const char** log = NULL); // (Optional) pointer to compiler log string. If *log == NULL there is no output.
125 //------------------------------------------------------------------------------
126 float4 make_float4(const SLVec4f& f);
127 //------------------------------------------------------------------------------
128 float3 make_float3(const SLVec3f& f);
129 //------------------------------------------------------------------------------
130 # endif // SLOPTIXHELPER_H
131 #endif // SL_HAS_OPTIX
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103