Equalizer
1.4.1
|
00001 00002 /* Copyright (c) 2007-2011, Maxim Makhinya <maxmah@gmail.com> 00003 Copyright (c) 2008, Stefan Eilemann <eile@equalizergraphics.com> 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * - Redistributions of source code must retain the above copyright notice, this 00009 * list of conditions and the following disclaimer. 00010 * - Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * - Neither the name of Eyescale Software GmbH nor the names of its 00014 * contributors may be used to endorse or promote products derived from this 00015 * software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #include "glslShaders.h" 00031 00032 namespace eVolve 00033 { 00034 void GLSLShaders::_printLog( GLhandleARB shader, const std::string &type ) 00035 { 00036 GLint length; 00037 glGetObjectParameterivARB( shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length ); 00038 00039 if( length <= 1 ) 00040 return; 00041 00042 std::vector<GLcharARB> log; 00043 log.resize( length ); 00044 00045 GLint written; 00046 glGetInfoLogARB( shader, length, &written, &log[0] ); 00047 LBERROR << "Shader error: " << type << std::endl 00048 << &log[0] << std::endl; 00049 00050 return; 00051 } 00052 00053 00054 GLhandleARB GLSLShaders::_loadShader( const std::string &shader, 00055 GLenum shaderType ) 00056 { 00057 GLhandleARB handle = glCreateShaderObjectARB( shaderType ); 00058 const char* cstr = shader.c_str(); 00059 glShaderSourceARB( handle, 1, &cstr, 0 ); 00060 glCompileShaderARB( handle ); 00061 00062 GLint status; 00063 glGetObjectParameterivARB( handle, GL_OBJECT_COMPILE_STATUS_ARB, &status ); 00064 if( status != GL_FALSE ) 00065 return handle; 00066 00067 _printLog( handle, "Compiling" ); 00068 glDeleteObjectARB( handle ); 00069 return 0; 00070 } 00071 00072 00073 bool GLSLShaders::_cleanupOnError( GLhandleARB vShader, GLhandleARB fShader ) 00074 { 00075 if( vShader ) 00076 glDeleteObjectARB( vShader ); 00077 00078 if( fShader ) 00079 glDeleteObjectARB( fShader ); 00080 00081 glDeleteObjectARB( _program ); 00082 _program = 0; 00083 return false; 00084 } 00085 00086 00087 bool GLSLShaders::loadShaders( const std::string &vShader, 00088 const std::string &fShader, 00089 const GLEWContext* glewContext ) 00090 { 00091 if( _shadersLoaded ) 00092 return true; 00093 00094 LBASSERT( glewContext ); 00095 _glewContext = glewContext; 00096 00097 _program = glCreateProgramObjectARB(); 00098 00099 GLhandleARB vertexShader = _loadShader( vShader, GL_VERTEX_SHADER_ARB ); 00100 if( !vertexShader ) 00101 return _cleanupOnError(); 00102 00103 glAttachObjectARB( _program, vertexShader ); 00104 00105 GLhandleARB fragmentShader = _loadShader( fShader, GL_FRAGMENT_SHADER_ARB ); 00106 if( !fragmentShader ) 00107 return _cleanupOnError( vertexShader ); 00108 00109 glAttachObjectARB( _program, fragmentShader ); 00110 00111 glLinkProgramARB( _program ); 00112 00113 GLint status; 00114 glGetObjectParameterivARB( _program, GL_OBJECT_LINK_STATUS_ARB, &status ); 00115 if( status != GL_FALSE ) 00116 { 00117 _shadersLoaded = true; 00118 return true; 00119 } 00120 00121 _printLog( _program, "Linking" ); 00122 return _cleanupOnError( vertexShader, fragmentShader ); 00123 } 00124 00125 00126 void GLSLShaders::unloadShaders() 00127 { 00128 if( !_shadersLoaded ) 00129 return; 00130 00131 LBASSERT( _glewContext ); 00132 LBASSERT( _program ); 00133 00134 glDeleteObjectARB( _program ); 00135 _shadersLoaded = false; 00136 _program = 0; 00137 } 00138 00139 }