SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLOptixHelper.cpp
Go to the documentation of this file.
1 /**
2  * \file SLOptixHelper.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 <optix_function_table_definition.h>
13 # include <string>
14 # include <fstream>
15 # include <sstream>
16 # include <cuda_runtime.h>
17 # include <SLOptix.h>
18 # include <SLOptixHelper.h>
19 
20 //-----------------------------------------------------------------------------
21 static bool readSourceFile(string& str, const string& filename)
22 {
23  // Try to open file
24  std::ifstream file(filename.c_str());
25  if (file.good())
26  {
27  // Found usable source file
28  stringstream source_buffer;
29  source_buffer << file.rdbuf();
30  str = source_buffer.str();
31  return true;
32  }
33  return false;
34 }
35 //-----------------------------------------------------------------------------
36 static string getPtxFilename(string filename)
37 {
38  string ptxFilename;
39  ptxFilename += '/';
40  ptxFilename += "cuda_compile_ptx_1";
41  ptxFilename += "_generated_";
42  ptxFilename += filename;
43  ptxFilename += ".ptx";
44  return ptxFilename;
45 }
46 //-----------------------------------------------------------------------------
47 string getPtxStringFromFile(string filename,
48  const char** log)
49 {
50  if (log)
51  *log = NULL;
52 
53  string* ptx = new string();
54  string sourceFilePath = SLOptix::exePath +
55  "../modules/sl" +
56  getPtxFilename(filename);
57 
58  // Try to open source PTX file
59  if (!readSourceFile(*ptx, sourceFilePath))
60  {
61  string err = "Couldn't open source file " + sourceFilePath;
62  cout << err << endl;
63  throw std::runtime_error(err.c_str());
64  }
65 
66  return *ptx;
67 }
68 //-----------------------------------------------------------------------------
69 float4 make_float4(const SLVec4f& f)
70 {
71  return {f.x, f.y, f.z, f.w};
72 }
73 //-----------------------------------------------------------------------------
74 float3 make_float3(const SLVec3f& f)
75 {
76  return {f.x, f.y, f.z};
77 }
78 //-----------------------------------------------------------------------------
79 
80 #endif // SL_HAS_OPTIX
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43
T w
Definition: SLVec4.h:32
T z
Definition: SLVec4.h:32
T y
Definition: SLVec4.h:32
T x
Definition: SLVec4.h:32
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103