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