Jetson Inference
DNN Vision Library
glUtility.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __OPENGL_UTILITY_H
24 #define __OPENGL_UTILITY_H
25 
26 
27 #include <GL/glew.h>
28 #include <GL/glx.h>
29 
30 #include <stdio.h>
31 
32 
37 #define LOG_GL "[OpenGL] "
38 
43 #define GL(x) { x; glCheckError( #x, __FILE__, __LINE__ ); }
44 
49 #define GL_VERIFY(x) { x; if(glCheckError( #x, __FILE__, __LINE__ )) return false; }
50 
55 #define GL_VERIFYN(x) { x; if(glCheckError( #x, __FILE__, __LINE__ )) return NULL; }
56 
61 #define GL_CHECK(msg) { glCheckError(msg, __FILE__, __LINE__); }
62 
67 inline bool glCheckError(const char* msg, const char* file, int line)
68 {
69  GLenum err = glGetError();
70 
71  if( err == GL_NO_ERROR )
72  return false;
73 
74  const char* e = NULL;
75 
76  switch(err)
77  {
78  case GL_INVALID_ENUM: e = "invalid enum"; break;
79  case GL_INVALID_VALUE: e = "invalid value"; break;
80  case GL_INVALID_OPERATION: e = "invalid operation"; break;
81  case GL_STACK_OVERFLOW: e = "stack overflow"; break;
82  case GL_STACK_UNDERFLOW: e = "stack underflow"; break;
83  case GL_OUT_OF_MEMORY: e = "out of memory"; break;
84  #ifdef GL_TABLE_TOO_LARGE_EXT
85  case GL_TABLE_TOO_LARGE_EXT: e = "table too large"; break;
86  #endif
87  #ifdef GL_TEXTURE_TOO_LARGE_EXT
88  case GL_TEXTURE_TOO_LARGE_EXT: e = "texture too large"; break;
89  #endif
90  default: e = "unknown error";
91  }
92 
93  printf(LOG_GL "Error %i - '%s'\n", (uint)err, e);
94  printf(LOG_GL " %s::%i\n", file, line );
95  printf(LOG_GL " %s\n", msg );
96 
97  return true;
98 }
99 
100 
105 inline bool glCheckError(const char* msg)
106 {
107  GLenum err = glGetError();
108 
109  if( err == GL_NO_ERROR )
110  return false;
111 
112  const char* e = NULL;
113 
114  switch(err)
115  {
116  case GL_INVALID_ENUM: e = "invalid enum"; break;
117  case GL_INVALID_VALUE: e = "invalid value"; break;
118  case GL_INVALID_OPERATION: e = "invalid operation"; break;
119  case GL_STACK_OVERFLOW: e = "stack overflow"; break;
120  case GL_STACK_UNDERFLOW: e = "stack underflow"; break;
121  case GL_OUT_OF_MEMORY: e = "out of memory"; break;
122  #ifdef GL_TABLE_TOO_LARGE_EXT
123  case GL_TABLE_TOO_LARGE_EXT: e = "table too large"; break;
124  #endif
125  #ifdef GL_TEXTURE_TOO_LARGE_EXT
126  case GL_TEXTURE_TOO_LARGE_EXT: e = "texture too large"; break;
127  #endif
128  default: e = "unknown error";
129  }
130 
131  printf(LOG_GL "%s (error %i - %s)\n", msg, (uint)err, e);
132  return true;
133 }
134 
135 
136 
137 #define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX 0x9048
138 #define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX 0x9049
139 
140 
145 inline void glPrintFreeMem()
146 {
147  GLint total_mem_kb = 0;
148  GLint cur_avail_mem_kb = 0;
149 
150  glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX, &total_mem_kb);
151  glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
152 
153  printf(LOG_GL "GPU memory free %i / %i kb\n", cur_avail_mem_kb, total_mem_kb);
154 }
155 
156 
157 
158 #endif
159 
#define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX
Definition: glUtility.h:137
#define LOG_GL
OpenGL logging prefix.
Definition: glUtility.h:37
bool glCheckError(const char *msg, const char *file, int line)
OpenGL error-checking messsage function.
Definition: glUtility.h:67
void glPrintFreeMem()
Print the amount of free GPU memory.
Definition: glUtility.h:145
#define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX
Definition: glUtility.h:138