KiCad PCB EDA Suite
shader.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) 2012 Torsten Hueter, torstenhtr <at> gmx.de
5  * Copyright (C) 2012 Kicad Developers, see change_log.txt for contributors.
6  *
7  * Graphics Abstraction Layer (GAL) for OpenGL
8  *
9  * Shader class
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <iostream>
30 #include <fstream>
31 #include <stdexcept>
32 
33 #include <cstring>
34 #include <cassert>
35 
36 #include <gal/opengl/shader.h>
37 #include <vector>
38 
39 using namespace KIGFX;
40 
42  isProgramCreated( false ),
43  isShaderLinked( false ),
44  active( false ),
45  maximumVertices( 4 ),
46  geomInputType( GL_LINES ),
47  geomOutputType( GL_LINES )
48 
49 {
50  // Do not have uninitialized members:
51  programNumber = 0;
52 }
53 
54 
56 {
57  if( active )
58  Deactivate();
59 
60  if( isProgramCreated )
61  {
62  // Delete the shaders and the program
63  for( std::deque<GLuint>::iterator it = shaderNumbers.begin();
64  it != shaderNumbers.end(); ++it )
65  {
66  GLuint shader = *it;
67 
68  if( glIsShader( shader ) )
69  {
70  glDetachShader( programNumber, shader );
71  glDeleteShader( shader );
72  }
73  }
74 
75  glDeleteProgram( programNumber );
76  }
77 }
78 
79 bool SHADER::LoadShaderFromFile( SHADER_TYPE aShaderType, const std::string& aShaderSourceName )
80 {
81  // Load shader sources
82  const std::string shaderSource = ReadSource( aShaderSourceName );
83 
84  return LoadShaderFromStrings( aShaderType, shaderSource );
85 }
86 
87 
88 void SHADER::ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
89  GLuint geometryOutputType )
90 {
91  maximumVertices = maxVertices;
92  geomInputType = geometryInputType;
93  geomOutputType = geometryOutputType;
94 }
95 
96 
98 {
99  // Shader linking
100  glLinkProgram( programNumber );
102 
103  // Check the Link state
104  glGetObjectParameterivARB( programNumber, GL_OBJECT_LINK_STATUS_ARB,
105  (GLint*) &isShaderLinked );
106 
107 #ifdef DEBUG
108  if( !isShaderLinked )
109  {
110  int maxLength;
111  glGetProgramiv( programNumber, GL_INFO_LOG_LENGTH, &maxLength );
112  maxLength = maxLength + 1;
113  char* linkInfoLog = new char[maxLength];
114  glGetProgramInfoLog( programNumber, maxLength, &maxLength, linkInfoLog );
115  std::cerr << "Shader linking error:" << std::endl;
116  std::cerr << linkInfoLog;
117  delete[] linkInfoLog;
118  }
119 #endif /* DEBUG */
120 
121  return isShaderLinked;
122 }
123 
124 
125 int SHADER::AddParameter( const std::string& aParameterName )
126 {
127  GLint location = glGetUniformLocation( programNumber, aParameterName.c_str() );
128 
129  if( location >= 0 )
130  parameterLocation.push_back( location );
131  else
132  throw std::runtime_error( "Could not find shader uniform: " + aParameterName );
133 
134  return parameterLocation.size() - 1;
135 }
136 
137 
138 void SHADER::SetParameter( int parameterNumber, float value ) const
139 {
140  assert( (unsigned) parameterNumber < parameterLocation.size() );
141 
142  glUniform1f( parameterLocation[parameterNumber], value );
143 }
144 
145 
146 void SHADER::SetParameter( int parameterNumber, int value ) const
147 {
148  assert( (unsigned) parameterNumber < parameterLocation.size() );
149 
150  glUniform1i( parameterLocation[parameterNumber], value );
151 }
152 
153 void SHADER::SetParameter( int parameterNumber, float f0, float f1, float f2, float f3 ) const
154 {
155  assert( (unsigned)parameterNumber < parameterLocation.size() );
156  float arr[4] = { f0, f1, f2, f3 };
157  glUniform4fv( parameterLocation[parameterNumber], 1, arr );
158 }
159 
160 void SHADER::SetParameter( int aParameterNumber, const VECTOR2D& aValue ) const
161 {
162  assert( (unsigned)aParameterNumber < parameterLocation.size() );
163  glUniform2f( parameterLocation[aParameterNumber], aValue.x, aValue.y );
164 }
165 
166 
167 int SHADER::GetAttribute( const std::string& aAttributeName ) const
168 {
169  return glGetAttribLocation( programNumber, aAttributeName.c_str() );
170 }
171 
172 
174 {
175  GLint glInfoLogLength = 0;
176  GLint writtenChars = 0;
177 
178  // Get the length of the info string
179  glGetProgramiv( aProgram, GL_INFO_LOG_LENGTH, &glInfoLogLength );
180 
181  // Print the information
182  if( glInfoLogLength > 2 )
183  {
184  GLchar* glInfoLog = new GLchar[glInfoLogLength];
185  glGetProgramInfoLog( aProgram, glInfoLogLength, &writtenChars, glInfoLog );
186 
187  std::cerr << glInfoLog << std::endl;
188 
189  delete[] glInfoLog;
190  }
191 }
192 
193 
194 void SHADER::shaderInfo( GLuint aShader )
195 {
196  GLint glInfoLogLength = 0;
197  GLint writtenChars = 0;
198 
199  // Get the length of the info string
200  glGetShaderiv( aShader, GL_INFO_LOG_LENGTH, &glInfoLogLength );
201 
202  // Print the information
203  if( glInfoLogLength > 2 )
204  {
205  GLchar* glInfoLog = new GLchar[glInfoLogLength];
206  glGetShaderInfoLog( aShader, glInfoLogLength, &writtenChars, glInfoLog );
207 
208  std::cerr << glInfoLog << std::endl;
209 
210  delete[] glInfoLog;
211  }
212 }
213 
214 
215 std::string SHADER::ReadSource( const std::string& aShaderSourceName )
216 {
217  // Open the shader source for reading
218  std::ifstream inputFile( aShaderSourceName.c_str(), std::ifstream::in );
219  std::string shaderSource;
220 
221  if( !inputFile )
222  throw std::runtime_error( "Can't read the shader source: " + aShaderSourceName );
223 
224  std::string shaderSourceLine;
225 
226  // Read all lines from the text file
227  while( getline( inputFile, shaderSourceLine ) )
228  {
229  shaderSource += shaderSourceLine;
230  shaderSource += "\n";
231  }
232 
233  return shaderSource;
234 }
235 
236 
237 bool SHADER::loadShaderFromStringArray( SHADER_TYPE aShaderType, const char** aArray,
238  size_t aSize )
239 {
240  assert( !isShaderLinked );
241 
242  // Create the program
243  if( !isProgramCreated )
244  {
245  programNumber = glCreateProgram();
246  isProgramCreated = true;
247  }
248 
249  // Create a shader
250  GLuint shaderNumber = glCreateShader( aShaderType );
251  shaderNumbers.push_back( shaderNumber );
252 
253  // Get the program info
255 
256  // Attach the sources
257  glShaderSource( shaderNumber, aSize, (const GLchar**) aArray, NULL );
259 
260  // Compile and attach shader to the program
261  glCompileShader( shaderNumber );
262  GLint status;
263  glGetShaderiv( shaderNumber, GL_COMPILE_STATUS, &status );
264 
265  if( status != GL_TRUE )
266  {
267  shaderInfo( shaderNumber );
268 
269  GLint maxLength = 0;
270  glGetShaderiv( shaderNumber, GL_INFO_LOG_LENGTH, &maxLength );
271 
272  // The maxLength includes the NULL character
273  std::vector<GLchar> errorLog( (size_t) maxLength );
274  glGetShaderInfoLog( shaderNumber, maxLength, &maxLength, &errorLog[0] );
275 
276  // Provide the infolog in whatever manor you deem best.
277  // Exit with failure.
278  glDeleteShader( shaderNumber ); // Don't leak the shader.
279 
280  throw std::runtime_error( &errorLog[0] );
281  }
282 
283  glAttachShader( programNumber, shaderNumber );
285 
286  // Special handling for the geometry shader
287  if( aShaderType == SHADER_TYPE_GEOMETRY )
288  {
289  glProgramParameteriEXT( programNumber, GL_GEOMETRY_VERTICES_OUT_EXT, maximumVertices );
290  glProgramParameteriEXT( programNumber, GL_GEOMETRY_INPUT_TYPE_EXT, geomInputType );
291  glProgramParameteriEXT( programNumber, GL_GEOMETRY_OUTPUT_TYPE_EXT, geomOutputType );
292  }
293 
294  return true;
295 }
bool LoadShaderFromStrings(SHADER_TYPE aShaderType, Args &&... aArgs)
Add a shader and compile the shader sources.
Definition: shader.h:99
SHADER_TYPE
Type definition for the shader.
Definition: shader.h:44
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
int GetAttribute(const std::string &aAttributeName) const
Gets an attribute location.
Definition: shader.cpp:167
GLuint geomInputType
Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:234
void SetParameter(int aParameterNumber, float aValue) const
Set a parameter of the shader.
Definition: shader.cpp:138
bool Link()
Link the shaders.
Definition: shader.cpp:97
GLuint geomOutputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:235
void programInfo(GLuint aProgram)
Get the shader program information.
Definition: shader.cpp:173
bool LoadShaderFromFile(SHADER_TYPE aShaderType, const std::string &aShaderSourceName)
Loads one of the built-in shaders and compiles it.
Definition: shader.cpp:79
bool isShaderLinked
Is the shader linked?
Definition: shader.h:231
GLuint maximumVertices
The maximum of vertices to be generated.
Definition: shader.h:233
std::deque< GLuint > shaderNumbers
Shader number list.
Definition: shader.h:228
bool loadShaderFromStringArray(SHADER_TYPE aShaderType, const char **aArray, size_t aSize)
Compile vertex of fragment shader source code into the program.
Definition: shader.cpp:237
int AddParameter(const std::string &aParameterName)
Add a parameter to the parameter queue.
Definition: shader.cpp:125
void shaderInfo(GLuint aShader)
Get the shader information.
Definition: shader.cpp:194
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:141
static std::string ReadSource(const std::string &aShaderSourceName)
Read the shader source file.
Definition: shader.cpp:215
virtual ~SHADER()
Destructor.
Definition: shader.cpp:55
Geometry shader.
Definition: shader.h:48
SHADER()
Constructor.
Definition: shader.cpp:41
bool isProgramCreated
Flag for program creation.
Definition: shader.h:230
GLuint programNumber
Shader program number.
Definition: shader.h:229
bool active
Is any of shaders used?
Definition: shader.h:232
void ConfigureGeometryShader(GLuint maxVertices, GLuint geometryInputType, GLuint geometryOutputType)
Configure the geometry shader - has to be done before linking!
Definition: shader.cpp:88
int PGM_BASE * aProgram
Definition: pcbnew.cpp:184
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:236