Equalizer  1.6.1
glslShaders.cpp
1 
2 /* Copyright (c) 2007-2011, Maxim Makhinya <maxmah@gmail.com>
3  Copyright (c) 2008, Stefan Eilemann <eile@equalizergraphics.com>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * - Neither the name of Eyescale Software GmbH nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "glslShaders.h"
31 
32 namespace eVolve
33 {
34 void GLSLShaders::_printLog( GLhandleARB shader, const std::string &type )
35 {
36  GLint length;
37  glGetObjectParameterivARB( shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length );
38 
39  if( length <= 1 )
40  return;
41 
42  std::vector<GLcharARB> log;
43  log.resize( length );
44 
45  GLint written;
46  glGetInfoLogARB( shader, length, &written, &log[0] );
47  LBERROR << "Shader error: " << type << std::endl
48  << &log[0] << std::endl;
49 
50  return;
51 }
52 
53 
54 GLhandleARB GLSLShaders::_loadShader( const std::string &shader,
55  GLenum shaderType )
56 {
57  GLhandleARB handle = glCreateShaderObjectARB( shaderType );
58  const char* cstr = shader.c_str();
59  glShaderSourceARB( handle, 1, &cstr, 0 );
60  glCompileShaderARB( handle );
61 
62  GLint status;
63  glGetObjectParameterivARB( handle, GL_OBJECT_COMPILE_STATUS_ARB, &status );
64  if( status != GL_FALSE )
65  return handle;
66 
67  _printLog( handle, "Compiling" );
68  glDeleteObjectARB( handle );
69  return 0;
70 }
71 
72 
73 bool GLSLShaders::_cleanupOnError( GLhandleARB vShader, GLhandleARB fShader )
74 {
75  if( vShader )
76  glDeleteObjectARB( vShader );
77 
78  if( fShader )
79  glDeleteObjectARB( fShader );
80 
81  glDeleteObjectARB( _program );
82  _program = 0;
83  return false;
84 }
85 
86 
87 bool GLSLShaders::loadShaders( const std::string &vShader,
88  const std::string &fShader,
89  const GLEWContext* glewContext )
90 {
91  if( _shadersLoaded )
92  return true;
93 
94  LBASSERT( glewContext );
95  _glewContext = glewContext;
96 
97  _program = glCreateProgramObjectARB();
98 
99  GLhandleARB vertexShader = _loadShader( vShader, GL_VERTEX_SHADER_ARB );
100  if( !vertexShader )
101  return _cleanupOnError();
102 
103  glAttachObjectARB( _program, vertexShader );
104 
105  GLhandleARB fragmentShader = _loadShader( fShader, GL_FRAGMENT_SHADER_ARB );
106  if( !fragmentShader )
107  return _cleanupOnError( vertexShader );
108 
109  glAttachObjectARB( _program, fragmentShader );
110 
111  glLinkProgramARB( _program );
112 
113  GLint status;
114  glGetObjectParameterivARB( _program, GL_OBJECT_LINK_STATUS_ARB, &status );
115  if( status != GL_FALSE )
116  {
117  _shadersLoaded = true;
118  return true;
119  }
120 
121  _printLog( _program, "Linking" );
122  return _cleanupOnError( vertexShader, fragmentShader );
123 }
124 
125 
126 void GLSLShaders::unloadShaders()
127 {
128  if( !_shadersLoaded )
129  return;
130 
131  LBASSERT( _glewContext );
132  LBASSERT( _program );
133 
134  glDeleteObjectARB( _program );
135  _shadersLoaded = false;
136  _program = 0;
137 }
138 
139 }