From 335bf720607baaa1511d7b1523f96b6ef92792d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Sumi=C5=84ski?= Date: Sun, 30 Jun 2013 15:37:46 +0200 Subject: [PATCH] More debug information in case of failure compilation of shaders. --- common/gal/opengl/opengl_gal.cpp | 12 +++++++++-- common/gal/opengl/shader.cpp | 36 +++++++++++++++++++++++++++++++- include/gal/opengl/shader.h | 9 +++++++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index 919e1c1282..b984655e13 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -359,8 +359,16 @@ void OPENGL_GAL::BeginDrawing() // Compile the shaders if( !isShaderInitialized && isUseShader ) { - shader.AddSource( shaderPath + std::string( "/shader.vert" ), SHADER_TYPE_VERTEX ); - shader.AddSource( shaderPath + std::string( "/shader.frag" ), SHADER_TYPE_FRAGMENT ); + if( !shader.AddSource( shaderPath + std::string( "/shader.vert" ), SHADER_TYPE_VERTEX ) ) + { + wxLogFatalError( wxT( "Cannot compile vertex shader!" ) ); + } + + if( !shader.AddSource( shaderPath + std::string( "/shader.frag" ), SHADER_TYPE_FRAGMENT ) ) + { + wxLogFatalError( wxT( "Cannot compile fragment shader!" ) ); + } + if( !shader.Link() ) { wxLogFatalError( wxT( "Cannot link the shaders!" ) ); diff --git a/common/gal/opengl/shader.cpp b/common/gal/opengl/shader.cpp index 83eab3d2a7..8baf9f68c1 100644 --- a/common/gal/opengl/shader.cpp +++ b/common/gal/opengl/shader.cpp @@ -83,6 +83,27 @@ void SHADER::ProgramInfo( GLuint aProgram ) } +void SHADER::ShaderInfo( GLuint aShader ) +{ + GLint glInfoLogLength = 0; + GLint writtenChars = 0; + + // Get the length of the info string + glGetShaderiv( aShader, GL_INFO_LOG_LENGTH, &glInfoLogLength ); + + // Print the information + if( glInfoLogLength > 2 ) + { + GLchar* glInfoLog = new GLchar[glInfoLogLength]; + glGetShaderInfoLog( aShader, glInfoLogLength, &writtenChars, glInfoLog ); + + wxLogInfo( wxString::FromUTF8( (char*) glInfoLog ) ); + + delete glInfoLog; + } +} + + std::string SHADER::ReadSource( std::string aShaderSourceName ) { // Open the shader source for reading @@ -109,7 +130,7 @@ std::string SHADER::ReadSource( std::string aShaderSourceName ) } -void SHADER::AddSource( const std::string& aShaderSourceName, ShaderType aShaderType ) +bool SHADER::AddSource( const std::string& aShaderSourceName, ShaderType aShaderType ) { if( isShaderLinked ) { @@ -144,6 +165,17 @@ void SHADER::AddSource( const std::string& aShaderSourceName, ShaderType aShader // Compile and attach shader to the program glCompileShader( shaderNumber ); + GLint status; + glGetShaderiv( shaderNumber, GL_COMPILE_STATUS, &status ); + if( status != GL_TRUE ) + { + wxLogError( wxT( "Shader compilation error" ) ); + + ShaderInfo( shaderNumber ); + + return false; + } + glAttachShader( programNumber, shaderNumber ); ProgramInfo( programNumber ); @@ -157,6 +189,8 @@ void SHADER::AddSource( const std::string& aShaderSourceName, ShaderType aShader // Delete the allocated char array delete[] source; + + return true; } diff --git a/include/gal/opengl/shader.h b/include/gal/opengl/shader.h index 115fa02e49..f4804c3b66 100644 --- a/include/gal/opengl/shader.h +++ b/include/gal/opengl/shader.h @@ -76,7 +76,7 @@ public: * @param aShaderSourceName is the shader source file name. * @param aShaderType is the type of the shader. */ - void AddSource( const std::string& aShaderSourceName, ShaderType aShaderType ); + bool AddSource( const std::string& aShaderSourceName, ShaderType aShaderType ); /** * @brief Link the shaders. @@ -159,6 +159,13 @@ private: */ void ProgramInfo( GLuint aProgram ); + /** + * @brief Get the shader information. + * + * @param aShader is the shader number. + */ + void ShaderInfo( GLuint aShader ); + /** * @brief Read the shader source file *