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 #include "logging.h"
32 
33 
38 #define LOG_GL "[OpenGL] "
39 
44 #define GL(x) { x; glCheckError( #x, __FILE__, __LINE__ ); }
45 
50 #define GL_VERIFY(x) { x; if(glCheckError( #x, __FILE__, __LINE__ )) return false; }
51 
56 #define GL_VERIFYN(x) { x; if(glCheckError( #x, __FILE__, __LINE__ )) return NULL; }
57 
62 #define GL_CHECK(msg) { glCheckError(msg, __FILE__, __LINE__); }
63 
68 inline bool glCheckError(const char* msg, const char* file, int line)
69 {
70  GLenum err = glGetError();
71 
72  if( err == GL_NO_ERROR )
73  return false;
74 
75  const char* e = NULL;
76 
77  switch(err)
78  {
79  case GL_INVALID_ENUM: e = "invalid enum"; break;
80  case GL_INVALID_VALUE: e = "invalid value"; break;
81  case GL_INVALID_OPERATION: e = "invalid operation"; break;
82  case GL_STACK_OVERFLOW: e = "stack overflow"; break;
83  case GL_STACK_UNDERFLOW: e = "stack underflow"; break;
84  case GL_OUT_OF_MEMORY: e = "out of memory"; break;
85  #ifdef GL_TABLE_TOO_LARGE_EXT
86  case GL_TABLE_TOO_LARGE_EXT: e = "table too large"; break;
87  #endif
88  #ifdef GL_TEXTURE_TOO_LARGE_EXT
89  case GL_TEXTURE_TOO_LARGE_EXT: e = "texture too large"; break;
90  #endif
91  default: e = "unknown error";
92  }
93 
94  LogError(LOG_GL "Error %i - '%s'\n", (uint)err, e);
95  LogError(LOG_GL " %s::%i\n", file, line );
96  LogError(LOG_GL " %s\n", msg );
97 
98  return true;
99 }
100 
101 
106 inline bool glCheckError(const char* msg)
107 {
108  GLenum err = glGetError();
109 
110  if( err == GL_NO_ERROR )
111  return false;
112 
113  const char* e = NULL;
114 
115  switch(err)
116  {
117  case GL_INVALID_ENUM: e = "invalid enum"; break;
118  case GL_INVALID_VALUE: e = "invalid value"; break;
119  case GL_INVALID_OPERATION: e = "invalid operation"; break;
120  case GL_STACK_OVERFLOW: e = "stack overflow"; break;
121  case GL_STACK_UNDERFLOW: e = "stack underflow"; break;
122  case GL_OUT_OF_MEMORY: e = "out of memory"; break;
123  #ifdef GL_TABLE_TOO_LARGE_EXT
124  case GL_TABLE_TOO_LARGE_EXT: e = "table too large"; break;
125  #endif
126  #ifdef GL_TEXTURE_TOO_LARGE_EXT
127  case GL_TEXTURE_TOO_LARGE_EXT: e = "texture too large"; break;
128  #endif
129  default: e = "unknown error";
130  }
131 
132  LogError(LOG_GL "%s (error %i - %s)\n", msg, (uint)err, e);
133  return true;
134 }
135 
136 
141 inline void glPrintFreeMem()
142 {
143  GLint total_mem_kb = 0;
144  GLint cur_avail_mem_kb = 0;
145 
146  const GLenum GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX = 0x9048;
147  const GLenum GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX = 0x9049;
148 
149  glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX, &total_mem_kb);
150  glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
151 
152  LogInfo(LOG_GL "GPU memory free %i / %i kb\n", cur_avail_mem_kb, total_mem_kb);
153 }
154 
155 
161 inline void glDrawLine( float x1, float y1, float x2, float y2, float r, float g, float b, float a=1.0f, float thickness=2.0f )
162 {
163  glLineWidth(thickness);
164  glBegin(GL_LINES);
165 
166  glColor4f(r, g, b, a);
167 
168  glVertex2f(x1, y1);
169  glVertex2f(x2, y2);
170 
171  glEnd();
172 }
173 
174 
180 inline void glDrawOutline( float x, float y, float width, float height, float r, float g, float b, float a=1.0f, float thickness=2.0f )
181 {
182  const float right = x + width;
183  const float bottom = y + height;
184 
185  glLineWidth(thickness);
186  glBegin(GL_LINE_LOOP);
187 
188  glColor4f(r, g, b, a);
189 
190  glVertex2f(x, y);
191  glVertex2f(right, y);
192  glVertex2f(right, bottom);
193  glVertex2f(x, bottom);
194 
195  glEnd();
196 }
197 
198 
204 inline void glDrawRect( float x, float y, float width, float height, float r, float g, float b, float a=1.0f )
205 {
206  const float right = x + width;
207  const float bottom = y + height;
208 
209  glBegin(GL_QUADS);
210 
211  glColor4f(r, g, b, a);
212 
213  glVertex2f(x, y);
214  glVertex2f(right, y);
215  glVertex2f(right, bottom);
216  glVertex2f(x, bottom);
217 
218  glEnd();
219 }
220 
221 
222 #endif
223 
LOG_GL
#define LOG_GL
OpenGL logging prefix.
Definition: glUtility.h:38
LogInfo
#define LogInfo(format, args...)
Log a printf-style info message (Log::INFO)
Definition: logging.h:168
glDrawRect
void glDrawRect(float x, float y, float width, float height, float r, float g, float b, float a=1.0f)
Render a filled rect in screen coordinates with the specified color.
Definition: glUtility.h:204
glDrawLine
void glDrawLine(float x1, float y1, float x2, float y2, float r, float g, float b, float a=1.0f, float thickness=2.0f)
Render a line in screen coordinates with the specified color.
Definition: glUtility.h:161
glDrawOutline
void glDrawOutline(float x, float y, float width, float height, float r, float g, float b, float a=1.0f, float thickness=2.0f)
Render the outline of a rect in screen coordinates with the specified color.
Definition: glUtility.h:180
uint
unsigned int uint
Definition: cudaMath.h:36
glPrintFreeMem
void glPrintFreeMem()
Print the amount of free GPU memory.
Definition: glUtility.h:141
logging.h
LogError
#define LogError(format, args...)
Log a printf-style error message (Log::ERROR)
Definition: logging.h:150
glCheckError
bool glCheckError(const char *msg, const char *file, int line)
OpenGL error-checking messsage function.
Definition: glUtility.h:68