KiCad PCB EDA Suite
Loading...
Searching...
No Matches
shader.h
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-2020 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#ifndef SHADER_H_
30#define SHADER_H_
31
32#include <gal/opengl/kiglew.h> // Must be included first
33
34#include <math/vector2d.h>
35
36#include <string>
37#include <deque>
38
39namespace KIGFX
40{
41class OPENGL_GAL;
42
45{
46 SHADER_TYPE_VERTEX = GL_VERTEX_SHADER,
47 SHADER_TYPE_FRAGMENT = GL_FRAGMENT_SHADER,
48 SHADER_TYPE_GEOMETRY = GL_GEOMETRY_SHADER
49};
50
51namespace DETAIL {
52
53inline const char* translateStringArg( const std::string& str )
54{
55 return str.c_str();
56}
57
58inline const char* translateStringArg( const char* str )
59{
60 return str;
61}
62
63}
64
65
76class SHADER
77{
78public:
79
80 SHADER();
81
82 virtual ~SHADER();
83
92 template< typename... Args >
93 bool LoadShaderFromStrings( SHADER_TYPE aShaderType, Args&&... aArgs )
94 {
95 const char* arr[] = { DETAIL::translateStringArg( aArgs )... };
96 return loadShaderFromStringArray( aShaderType, arr, sizeof...(Args) );
97 }
98
106 bool LoadShaderFromFile( SHADER_TYPE aShaderType, const std::string& aShaderSourceName );
107
113 bool Link();
114
118 bool IsLinked() const
119 {
120 return isShaderLinked;
121 }
122
126 inline void Use()
127 {
128 glUseProgram( programNumber );
129 active = true;
130 }
131
135 inline void Deactivate()
136 {
137 glUseProgram( 0 );
138 active = false;
139 }
140
146 inline bool IsActive() const
147 {
148 return active;
149 }
150
158 void ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
159 GLuint geometryOutputType );
160
171 int AddParameter( const std::string& aParameterName );
172
179 void SetParameter( int aParameterNumber, float aValue ) const;
180 void SetParameter( int aParameterNumber, int aValue ) const;
181 void SetParameter( int aParameterNumber, const VECTOR2D& aValue ) const;
182 void SetParameter( int aParameterNumber, float f0, float f1, float f2, float f3 ) const;
183
190 int GetAttribute( const std::string& aAttributeName ) const;
191
198 static std::string ReadSource( const std::string& aShaderSourceName );
199
200private:
204 bool loadShaderFromStringArray( SHADER_TYPE aShaderType, const char** aArray, size_t aSize );
205
211 void programInfo( GLuint aProgram );
212
218 void shaderInfo( GLuint aShader );
219
220 std::deque<GLuint> shaderNumbers;
224 bool active;
226
229
232 std::deque<GLint> parameterLocation;
233};
234} // namespace KIGFX
235
236#endif /* SHADER_H_ */
Provide the access to the OpenGL shaders.
Definition: shader.h:77
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 IsLinked() const
Return true if shaders are linked correctly.
Definition: shader.h:118
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
void Use()
Use the shader.
Definition: shader.h:126
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
bool IsActive() const
Return the current state of the shader.
Definition: shader.h:146
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
const char * translateStringArg(const std::string &str)
Definition: shader.h:53
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_VERTEX
Vertex shader.
Definition: shader.h:46
@ SHADER_TYPE_FRAGMENT
Fragment shader.
Definition: shader.h:47
@ SHADER_TYPE_GEOMETRY
Geometry shader.
Definition: shader.h:48