COVISE Core
helper_cuda_gl.h
Go to the documentation of this file.
1
12#ifndef HELPER_CUDA_GL_H
13#define HELPER_CUDA_GL_H
14
15#include <stdio.h>
16#include <string.h>
17#include <stdlib.h>
18
19// includes, graphics
20#if defined (__APPLE__) || defined(MACOSX)
21#include <OpenGL/gl.h>
22#include <OpenGL/glu.h>
23#else
24#include <GL/gl.h>
25#include <GL/glu.h>
26#endif
27
28#ifndef EXIT_WAIVED
29#define EXIT_WAIVED 2
30#endif
31
32#ifdef __DRIVER_TYPES_H__
33#ifndef DEVICE_RESET
34#define DEVICE_RESET cudaDeviceReset()
35#endif
36#else
37#ifndef DEVICE_RESET
38#define DEVICE_RESET
39#endif
40#endif
41
42#ifdef __CUDA_GL_INTEROP_H__
44// These are CUDA OpenGL Helper functions
45
46inline int gpuGLDeviceInit(int ARGC, const char **ARGV)
47{
48 int deviceCount;
49 checkCudaErrors(cudaGetDeviceCount(&deviceCount));
50
51 if (deviceCount == 0)
52 {
53 fprintf(stderr, "CUDA error: no devices supporting CUDA.\n");
54 exit(EXIT_FAILURE);
55 }
56
57 int dev = 0;
58 dev = getCmdLineArgumentInt(ARGC, ARGV, "device=");
59
60 if (dev < 0)
61 {
62 dev = 0;
63 }
64
65 if (dev > deviceCount-1)
66 {
67 fprintf(stderr, "\n");
68 fprintf(stderr, ">> %d CUDA capable GPU device(s) detected. <<\n", deviceCount);
69 fprintf(stderr, ">> gpuGLDeviceInit (-device=%d) is not a valid GPU device. <<\n", dev);
70 fprintf(stderr, "\n");
71 return -dev;
72 }
73
74 cudaDeviceProp deviceProp;
75 checkCudaErrors(cudaGetDeviceProperties(&deviceProp, dev));
76
77 if (deviceProp.computeMode == cudaComputeModeProhibited)
78 {
79 fprintf(stderr, "Error: device is running in <Compute Mode Prohibited>, no threads can use ::cudaSetDevice().\n");
80 return -1;
81 }
82
83 if (deviceProp.major < 1)
84 {
85 fprintf(stderr, "Error: device does not support CUDA.\n");
86 exit(EXIT_FAILURE);
87 }
88
89 if (checkCmdLineFlag(ARGC, ARGV, "quiet") == false)
90 {
91 fprintf(stderr, "Using device %d: %s\n", dev, deviceProp.name);
92 }
93
94 return dev;
95}
96
97// This function will pick the best CUDA device available with OpenGL interop
98inline int findCudaGLDevice(int argc, const char **argv)
99{
100 int devID = 0;
101
102 // If the command-line has a device number specified, use it
103 if (checkCmdLineFlag(argc, (const char **)argv, "device"))
104 {
105 devID = gpuGLDeviceInit(argc, (const char **)argv);
106
107 if (devID < 0)
108 {
109 printf("no CUDA capable devices found, exiting...\n");
111 exit(EXIT_SUCCESS);
112 }
113 }
114 else
115 {
116 // Otherwise pick the device with highest Gflops/s
117 devID = gpuGetMaxGflopsDeviceId();
118 }
119
120 return devID;
121}
122
131inline bool
132sdkCheckErrorGL(const char *file, const int line)
133{
134 bool ret_val = true;
135
136 // check for error
137 GLenum gl_error = glGetError();
138
139 if (gl_error != GL_NO_ERROR)
140 {
141#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
142 char tmpStr[512];
143 // NOTE: "%s(%i) : " allows Visual Studio to directly jump to the file at the right line
144 // when the user double clicks on the error line in the Output pane. Like any compile error.
145 sprintf_s(tmpStr, 255, "\n%s(%i) : GL Error : %s\n\n", file, line, gluErrorString(gl_error));
146 fprintf(stderr, "%s", tmpStr);
147#endif
148 fprintf(stderr, "GL Error in file '%s' in line %d :\n", file, line);
149 fprintf(stderr, "%s\n", gluErrorString(gl_error));
150 ret_val = false;
151 }
152
153 return ret_val;
154}
155
156#define SDK_CHECK_ERROR_GL() \
157 if( false == sdkCheckErrorGL( __FILE__, __LINE__)) { \
158 DEVICE_RESET \
159 exit(EXIT_FAILURE); \
160 }
161#endif
162
163#endif
typedef GLenum(APIENTRYP PFNGLCHECKFRAMEBUFFERSTATUSPROC)(GLenum target)
#define DEVICE_RESET
Definition: helper_cuda_gl.h:38
bool checkCmdLineFlag(const int argc, const char **argv, const char *string_ref)
Definition: helper_string.h:125
int getCmdLineArgumentInt(const int argc, const char **argv, const char *string_ref)
Definition: helper_string.h:183