Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:24

0001 #pragma once
0002 /**
0003 GL_CHECK.h
0004 ============
0005 
0006 Adapted from SDK/sutil/Exception.h 
0007 
0008 **/
0009 
0010 #include <iostream>
0011 #include <sstream>
0012 #include <stdexcept>
0013 #include <string>
0014 
0015 #define DO_GL_CHECK
0016 #ifdef DO_GL_CHECK
0017 
0018 #define GL_CHECK( call )                                                       \
0019     do                                                                         \
0020     {                                                                          \
0021         call;                                                                  \
0022            ::glCheck( #call, __FILE__, __LINE__ );                         \
0023     } while( false )
0024 
0025 
0026 #define GL_CHECK_ERRORS() ::glCheckErrors( __FILE__, __LINE__ )
0027 
0028 #else
0029 #define GL_CHECK( call )                                                       \
0030     do                                                                         \
0031     {                                                                          \
0032         call;                                                                  \
0033     } while( 0 )
0034 #define GL_CHECK_ERRORS()                                                      \
0035     do                                                                         \
0036     {                                                                          \
0037         ;                                                                      \
0038     } while( 0 )
0039 #endif
0040 
0041 inline const char* getGLErrorString( GLenum error )
0042 {
0043     switch( error )
0044     {
0045         case GL_NO_ERROR:          return "No error";
0046         case GL_INVALID_ENUM:      return "Invalid enum";
0047         case GL_INVALID_VALUE:     return "Invalid value";
0048         case GL_INVALID_OPERATION: return "Invalid operation";
0049         //case GL_STACK_OVERFLOW:  return "Stack overflow";
0050         //case GL_STACK_UNDERFLOW: return "Stack underflow";
0051         case GL_OUT_OF_MEMORY:     return "Out of memory";
0052         //case GL_TABLE_TOO_LARGE: return "Table too large";
0053         default:                   return "Unknown GL error";
0054     }
0055 }
0056 
0057 inline void glCheck( const char* call, const char* file, unsigned int line )
0058 {
0059     GLenum err = glGetError();
0060     if( err != GL_NO_ERROR )
0061     {
0062         std::stringstream ss;
0063         ss << "GL error " << getGLErrorString( err ) << " at " << file << "("
0064            << line << "): " << call << '\n';
0065         std::cerr << ss.str() << std::endl;
0066         throw std::runtime_error( ss.str().c_str() );
0067     }
0068 }
0069 
0070 inline void glCheckErrors( const char* file, unsigned int line )
0071 {
0072     GLenum err = glGetError();
0073     if( err != GL_NO_ERROR )
0074     {
0075         std::stringstream ss;
0076         ss << "GL error " << getGLErrorString( err ) << " at " << file << "("
0077            << line << ")";
0078         std::cerr << ss.str() << std::endl;
0079         throw std::runtime_error( ss.str().c_str() );
0080     }
0081 }
0082 
0083 inline void checkGLError()
0084 {
0085     GLenum err = glGetError();
0086     if( err != GL_NO_ERROR )
0087     {
0088         std::ostringstream oss;
0089         do
0090         {
0091             oss << "GL error: " << getGLErrorString( err ) << '\n';
0092             err = glGetError();
0093         } while( err != GL_NO_ERROR );
0094 
0095         throw std::runtime_error( oss.str().c_str() );
0096     }
0097 }
0098