KiCad PCB EDA Suite
utils.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016-2017 CERN
5  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <confirm.h> // DisplayError
26 
27 #include <GL/glew.h>
28 #include <stdexcept>
29 
30 int checkGlError( const std::string& aInfo, bool aThrow )
31 {
32  int result = glGetError();
33  wxString errorMsg;
34 
35  switch( result )
36  {
37  case GL_NO_ERROR:
38  // all good
39  break;
40 
41  case GL_INVALID_ENUM:
42  errorMsg = wxString::Format( "Error: %s: invalid enum", aInfo );
43  break;
44 
45  case GL_INVALID_VALUE:
46  errorMsg = wxString::Format( "Error: %s: invalid value", aInfo );
47  break;
48 
49  case GL_INVALID_OPERATION:
50  errorMsg = wxString::Format( "Error: %s: invalid operation", aInfo );
51  break;
52 
53  case GL_INVALID_FRAMEBUFFER_OPERATION:
54  {
55  GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
56 
57  if( status != GL_FRAMEBUFFER_COMPLETE_EXT )
58  {
59  switch( status )
60  {
61  case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
62  errorMsg = "The framebuffer attachment points are incomplete.";
63  break;
64  case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
65  errorMsg = "No images attached to the framebuffer.";
66  break;
67  case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
68  errorMsg = "The framebuffer does not have at least one image attached to it.";
69  break;
70  case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
71  errorMsg = "The framebuffer read buffer is incomplete.";
72  break;
73  case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
74  errorMsg = "The combination of internal formats of the attached images violates an implementation-dependent set of restrictions.";
75  break;
76  case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
77  errorMsg = "GL_RENDERBUFFER_SAMPLES is not the same for all attached renderbuffers.";
78  break;
79  case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_EXT:
80  errorMsg = "Framebuffer incomplete layer targets errors.";
81  break;
82  case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
83  errorMsg = "Framebuffer attachments have different dimensions";
84  break;
85  default:
86  errorMsg = "Unknown incomplete framebufer error";
87  }
88  }
89  else
90  errorMsg = wxString::Format( "Error: %s: invalid framebuffer operation", aInfo );
91  }
92  break;
93 
94  case GL_OUT_OF_MEMORY:
95  errorMsg = wxString::Format( "Error: %s: out of memory", aInfo );
96  break;
97 
98  case GL_STACK_UNDERFLOW:
99  errorMsg = wxString::Format( "Error: %s: stack underflow", aInfo );
100  break;
101 
102  case GL_STACK_OVERFLOW:
103  errorMsg = wxString::Format( "Error: %s: stack overflow", aInfo );
104  break;
105 
106  default:
107  errorMsg = wxString::Format( "Error: %s: unknown error", aInfo );
108  break;
109  }
110 
111  if( result != GL_NO_ERROR )
112  {
113  if( aThrow )
114  throw std::runtime_error( (const char*) errorMsg.char_str() );
115  else
116  DisplayErrorMessage( nullptr, "OpenGL error occurred", errorMsg );
117  }
118 
119  return result;
120 }
121 
122 
123 // debugMsgCallback is a callback function for glDebugMessageCallback.
124 // It must have the right type ( GLAPIENTRY )
125 static void GLAPIENTRY debugMsgCallback( GLenum aSource, GLenum aType, GLuint aId,
126  GLenum aSeverity, GLsizei aLength, const GLchar* aMessage, const void* aUserParam )
127 {
128  switch( aSeverity )
129  {
130  case GL_DEBUG_SEVERITY_HIGH: printf( "OpenGL ERROR: " ); break;
131  case GL_DEBUG_SEVERITY_MEDIUM: printf( "OpenGL WARNING: " ); break;
132  case GL_DEBUG_SEVERITY_LOW: printf( "OpenGL INFO: " ); break;
133  case GL_DEBUG_SEVERITY_NOTIFICATION: return;
134  }
135 
136  printf( "%s\n", aMessage );
137 }
138 
139 
140 void enableGlDebug( bool aEnable )
141 {
142  if( aEnable )
143  {
144  glEnable( GL_DEBUG_OUTPUT );
145  glDebugMessageCallback( (GLDEBUGPROC) debugMsgCallback, nullptr );
146  }
147  else
148  {
149  glDisable( GL_DEBUG_OUTPUT );
150  }
151 }
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:259
static void GLAPIENTRY debugMsgCallback(GLenum aSource, GLenum aType, GLuint aId, GLenum aSeverity, GLsizei aLength, const GLchar *aMessage, const void *aUserParam)
Definition: utils.cpp:125
This file is part of the common library.
int checkGlError(const std::string &aInfo, bool aThrow)
Checks if one of recent OpenGL operations has failed.
Definition: utils.cpp:30
void enableGlDebug(bool aEnable)
Enables/disables OpenGL driver messages output.
Definition: utils.cpp:140
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205