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 (C) 2012-2021 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
158void SHADER::SetParameter( int parameterNumber, float f0, float f1, float f2, float f3 ) const
159{
160 assert( (unsigned) parameterNumber < parameterLocation.size() );
161 float arr[4] = { f0, f1, f2, f3 };
162 glUniform4fv( parameterLocation[parameterNumber], 1, arr );
163}
164
165void SHADER::SetParameter( int aParameterNumber, const VECTOR2D& aValue ) const
166{
167 assert( (unsigned) aParameterNumber < parameterLocation.size() );
168 glUniform2f( parameterLocation[aParameterNumber], static_cast<GLfloat>( aValue.x ), static_cast<GLfloat>( aValue.y ) );
169}
170
171
172int SHADER::GetAttribute( const std::string& aAttributeName ) const
173{
174 return glGetAttribLocation( programNumber, aAttributeName.c_str() );
175}
176
177
178void SHADER::programInfo( GLuint aProgram )
179{
180 GLint glInfoLogLength = 0;
181 GLint writtenChars = 0;
182
183 // Get the length of the info string
184 glGetProgramiv( aProgram, GL_INFO_LOG_LENGTH, &glInfoLogLength );
185
186 // Print the information
187 if( glInfoLogLength > 2 )
188 {
189 GLchar* glInfoLog = new GLchar[glInfoLogLength];
190 glGetProgramInfoLog( aProgram, glInfoLogLength, &writtenChars, glInfoLog );
191
192 delete[] glInfoLog;
193 }
194}
195
196
197void SHADER::shaderInfo( GLuint aShader )
198{
199 GLint glInfoLogLength = 0;
200 GLint writtenChars = 0;
201
202 // Get the length of the info string
203 glGetShaderiv( aShader, GL_INFO_LOG_LENGTH, &glInfoLogLength );
204
205 // Print the information
206 if( glInfoLogLength > 2 )
207 {
208 GLchar* glInfoLog = new GLchar[glInfoLogLength];
209 glGetShaderInfoLog( aShader, glInfoLogLength, &writtenChars, glInfoLog );
210
211 delete[] glInfoLog;
212 }
213}
214
215
216std::string SHADER::ReadSource( const std::string& aShaderSourceName )
217{
218 // Open the shader source for reading
219 std::ifstream inputFile( aShaderSourceName.c_str(), std::ifstream::in );
220 std::string shaderSource;
221
222 if( !inputFile )
223 throw std::runtime_error( "Can't read the shader source: " + aShaderSourceName );
224
225 std::string shaderSourceLine;
226
227 // Read all lines from the text file
228 while( getline( inputFile, shaderSourceLine ) )
229 {
230 shaderSource += shaderSourceLine;
231 shaderSource += "\n";
232 }
233
234 return shaderSource;
235}
236
237
238bool SHADER::loadShaderFromStringArray( SHADER_TYPE aShaderType, const char** aArray, 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, static_cast<GLsizei>( aSize ), (const GLchar**) aArray, nullptr );
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}
GLuint geomOutputType
Definition: shader.h:231
static std::string ReadSource(const std::string &aShaderSourceName)
Read the shader source file.
Definition: shader.cpp:216
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:238
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:197
virtual ~SHADER()
Definition: shader.cpp:55
void programInfo(GLuint aProgram)
Get the shader program information.
Definition: shader.cpp:178
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:172
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