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 
38 using namespace KIGFX;
39 
41  isProgramCreated( false ),
42  isShaderLinked( false ),
43  active( false ),
44  maximumVertices( 4 ),
45  geomInputType( GL_LINES ),
46  geomOutputType( GL_LINES )
47 
48 {
49  // Do not have uninitialized members:
50  programNumber = 0;
51 }
52 
53 
55 {
56  if( active )
57  Deactivate();
58 
59  if( isProgramCreated )
60  {
61  // Delete the shaders and the program
62  for( std::deque<GLuint>::iterator it = shaderNumbers.begin();
63  it != shaderNumbers.end(); ++it )
64  {
65  GLuint shader = *it;
66 
67  if( glIsShader( shader ) )
68  {
69  glDetachShader( programNumber, shader );
70  glDeleteShader( shader );
71  }
72  }
73 
74  glDeleteProgram( programNumber );
75  }
76 }
77 
78 bool SHADER::LoadShaderFromFile( SHADER_TYPE aShaderType, const std::string& aShaderSourceName )
79 {
80  // Load shader sources
81  const std::string shaderSource = ReadSource( aShaderSourceName );
82 
83  return LoadShaderFromStrings( aShaderType, shaderSource );
84 }
85 
86 
87 void SHADER::ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
88  GLuint geometryOutputType )
89 {
90  maximumVertices = maxVertices;
91  geomInputType = geometryInputType;
92  geomOutputType = geometryOutputType;
93 }
94 
95 
97 {
98  // Shader linking
99  glLinkProgram( programNumber );
101 
102  // Check the Link state
103  glGetObjectParameterivARB( programNumber, GL_OBJECT_LINK_STATUS_ARB,
104  (GLint*) &isShaderLinked );
105 
106 #ifdef DEBUG
107  if( !isShaderLinked )
108  {
109  int maxLength;
110  glGetProgramiv( programNumber, GL_INFO_LOG_LENGTH, &maxLength );
111  maxLength = maxLength + 1;
112  char* linkInfoLog = new char[maxLength];
113  glGetProgramInfoLog( programNumber, maxLength, &maxLength, linkInfoLog );
114  std::cerr << "Shader linking error:" << std::endl;
115  std::cerr << linkInfoLog;
116  delete[] linkInfoLog;
117  }
118 #endif /* DEBUG */
119 
120  return isShaderLinked;
121 }
122 
123 
124 int SHADER::AddParameter( const std::string& aParameterName )
125 {
126  GLint location = glGetUniformLocation( programNumber, aParameterName.c_str() );
127 
128  if( location >= 0 )
129  parameterLocation.push_back( location );
130  else
131  throw std::runtime_error( "Could not find shader uniform: " + aParameterName );
132 
133  return parameterLocation.size() - 1;
134 }
135 
136 
137 void SHADER::SetParameter( int parameterNumber, float value ) const
138 {
139  assert( (unsigned) parameterNumber < parameterLocation.size() );
140 
141  glUniform1f( parameterLocation[parameterNumber], value );
142 }
143 
144 
145 void SHADER::SetParameter( int parameterNumber, int value ) const
146 {
147  assert( (unsigned) parameterNumber < parameterLocation.size() );
148 
149  glUniform1i( parameterLocation[parameterNumber], value );
150 }
151 
152 void SHADER::SetParameter( int parameterNumber, float f0, float f1, float f2, float f3 ) const
153 {
154  assert( (unsigned)parameterNumber < parameterLocation.size() );
155  float arr[4] = { f0, f1, f2, f3 };
156  glUniform4fv( parameterLocation[parameterNumber], 1, arr );
157 }
158 
159 
160 int SHADER::GetAttribute( const std::string& aAttributeName ) const
161 {
162  return glGetAttribLocation( programNumber, aAttributeName.c_str() );
163 }
164 
165 
167 {
168  GLint glInfoLogLength = 0;
169  GLint writtenChars = 0;
170 
171  // Get the length of the info string
172  glGetProgramiv( aProgram, GL_INFO_LOG_LENGTH, &glInfoLogLength );
173 
174  // Print the information
175  if( glInfoLogLength > 2 )
176  {
177  GLchar* glInfoLog = new GLchar[glInfoLogLength];
178  glGetProgramInfoLog( aProgram, glInfoLogLength, &writtenChars, glInfoLog );
179 
180  std::cerr << glInfoLog << std::endl;
181 
182  delete[] glInfoLog;
183  }
184 }
185 
186 
187 void SHADER::shaderInfo( GLuint aShader )
188 {
189  GLint glInfoLogLength = 0;
190  GLint writtenChars = 0;
191 
192  // Get the length of the info string
193  glGetShaderiv( aShader, GL_INFO_LOG_LENGTH, &glInfoLogLength );
194 
195  // Print the information
196  if( glInfoLogLength > 2 )
197  {
198  GLchar* glInfoLog = new GLchar[glInfoLogLength];
199  glGetShaderInfoLog( aShader, glInfoLogLength, &writtenChars, glInfoLog );
200 
201  std::cerr << glInfoLog << std::endl;
202 
203  delete[] glInfoLog;
204  }
205 }
206 
207 
208 std::string SHADER::ReadSource( const std::string& aShaderSourceName )
209 {
210  // Open the shader source for reading
211  std::ifstream inputFile( aShaderSourceName.c_str(), std::ifstream::in );
212  std::string shaderSource;
213 
214  if( !inputFile )
215  throw std::runtime_error( "Can't read the shader source: " + aShaderSourceName );
216 
217  std::string shaderSourceLine;
218 
219  // Read all lines from the text file
220  while( getline( inputFile, shaderSourceLine ) )
221  {
222  shaderSource += shaderSourceLine;
223  shaderSource += "\n";
224  }
225 
226  return shaderSource;
227 }
228 
229 
230 bool SHADER::loadShaderFromStringArray( SHADER_TYPE aShaderType, const char** aArray,
231  size_t aSize )
232 {
233  assert( !isShaderLinked );
234 
235  // Create the program
236  if( !isProgramCreated )
237  {
238  programNumber = glCreateProgram();
239  isProgramCreated = true;
240  }
241 
242  // Create a shader
243  GLuint shaderNumber = glCreateShader( aShaderType );
244  shaderNumbers.push_back( shaderNumber );
245 
246  // Get the program info
248 
249  // Attach the sources
250  glShaderSource( shaderNumber, aSize, (const GLchar**) aArray, NULL );
252 
253  // Compile and attach shader to the program
254  glCompileShader( shaderNumber );
255  GLint status;
256  glGetShaderiv( shaderNumber, GL_COMPILE_STATUS, &status );
257 
258  if( status != GL_TRUE )
259  {
260  shaderInfo( shaderNumber );
261  throw std::runtime_error( "Shader compilation error" );
262  }
263 
264  glAttachShader( programNumber, shaderNumber );
266 
267  // Special handling for the geometry shader
268  if( aShaderType == SHADER_TYPE_GEOMETRY )
269  {
270  glProgramParameteriEXT( programNumber, GL_GEOMETRY_VERTICES_OUT_EXT, maximumVertices );
271  glProgramParameteriEXT( programNumber, GL_GEOMETRY_INPUT_TYPE_EXT, geomInputType );
272  glProgramParameteriEXT( programNumber, GL_GEOMETRY_OUTPUT_TYPE_EXT, geomOutputType );
273  }
274 
275  return true;
276 }
SHADER_TYPE
Type definition for the shader.
Definition: shader.h:42
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
GLuint geomInputType
Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:231
void SetParameter(int aParameterNumber, float aValue) const
Set a parameter of the shader.
Definition: shader.cpp:137
bool LoadShaderFromStrings(SHADER_TYPE aShaderType, Args &&...aArgs)
Add a shader and compile the shader sources.
Definition: shader.h:97
bool Link()
Link the shaders.
Definition: shader.cpp:96
GLuint geomOutputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition: shader.h:232
void programInfo(GLuint aProgram)
Get the shader program information.
Definition: shader.cpp:166
bool LoadShaderFromFile(SHADER_TYPE aShaderType, const std::string &aShaderSourceName)
Loads one of the built-in shaders and compiles it.
Definition: shader.cpp:78
bool isShaderLinked
Is the shader linked?
Definition: shader.h:228
GLuint maximumVertices
The maximum of vertices to be generated.
Definition: shader.h:230
std::deque< GLuint > shaderNumbers
Shader number list.
Definition: shader.h:225
bool loadShaderFromStringArray(SHADER_TYPE aShaderType, const char **aArray, size_t aSize)
Compile vertex of fragment shader source code into the program.
Definition: shader.cpp:230
int AddParameter(const std::string &aParameterName)
Add a parameter to the parameter queue.
Definition: shader.cpp:124
void shaderInfo(GLuint aShader)
Get the shader information.
Definition: shader.cpp:187
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition: shader.h:139
static std::string ReadSource(const std::string &aShaderSourceName)
Read the shader source file.
Definition: shader.cpp:208
int GetAttribute(const std::string &aAttributeName) const
Gets an attribute location.
Definition: shader.cpp:160
virtual ~SHADER()
Destructor.
Definition: shader.cpp:54
Geometry shader.
Definition: shader.h:46
SHADER()
Constructor.
Definition: shader.cpp:40
bool isProgramCreated
Flag for program creation.
Definition: shader.h:227
GLuint programNumber
Shader program number.
Definition: shader.h:226
bool active
Is any of shaders used?
Definition: shader.h:229
void ConfigureGeometryShader(GLuint maxVertices, GLuint geometryInputType, GLuint geometryOutputType)
Configure the geometry shader - has to be done before linking!
Definition: shader.cpp:87
int PGM_BASE * aProgram
Definition: pcbnew.cpp:205
std::deque< GLint > parameterLocation
Location of the parameter.
Definition: shader.h:233