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 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, see <https://www.gnu.org/licenses/>.
23 */
24
25#ifndef SHADER_H_
26#define SHADER_H_
27
28#include <kicad_gl/kiglad.h> // Must be included first
29
30#include <math/vector2d.h>
31
32#include <string>
33#include <deque>
34
35namespace KIGFX
36{
37class OPENGL_GAL;
38
41{
42 SHADER_TYPE_VERTEX = GL_VERTEX_SHADER,
43 SHADER_TYPE_FRAGMENT = GL_FRAGMENT_SHADER,
44 SHADER_TYPE_GEOMETRY = GL_GEOMETRY_SHADER
45};
46
47namespace DETAIL {
48
49inline const char* translateStringArg( const std::string& str )
50{
51 return str.c_str();
52}
53
54inline const char* translateStringArg( const char* str )
55{
56 return str;
57}
58
59}
60
61
72class SHADER
73{
74public:
75
76 SHADER();
77
78 virtual ~SHADER();
79
88 template< typename... Args >
89 bool LoadShaderFromStrings( SHADER_TYPE aShaderType, Args&&... aArgs )
90 {
91 const char* arr[] = { DETAIL::translateStringArg( aArgs )... };
92 return loadShaderFromStringArray( aShaderType, arr, sizeof...(Args) );
93 }
94
102 bool LoadShaderFromFile( SHADER_TYPE aShaderType, const std::string& aShaderSourceName );
103
109 bool Link();
110
114 bool IsLinked() const
115 {
116 return isShaderLinked;
117 }
118
122 inline void Use()
123 {
124 glUseProgram( programNumber );
125 active = true;
126 }
127
131 inline void Deactivate()
132 {
133 glUseProgram( 0 );
134 active = false;
135 }
136
142 inline bool IsActive() const
143 {
144 return active;
145 }
146
154 void ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
155 GLuint geometryOutputType );
156
167 int AddParameter( const std::string& aParameterName );
168
175 void SetParameter( int aParameterNumber, float aValue ) const;
176 void SetParameter( int aParameterNumber, int aValue ) const;
177 void SetParameter( int aParameterNumber, const VECTOR2D& aValue ) const;
178 void SetParameter( int aParameterNumber, float f0, float f1, float f2, float f3 ) const;
179
186 int GetAttribute( const std::string& aAttributeName ) const;
187
194 static std::string ReadSource( const std::string& aShaderSourceName );
195
196private:
200 bool loadShaderFromStringArray( SHADER_TYPE aShaderType, const char** aArray, size_t aSize );
201
207 void programInfo( GLuint aProgram );
208
214 void shaderInfo( GLuint aShader );
215
216 std::deque<GLuint> shaderNumbers;
220 bool active;
222
225
228 std::deque<GLint> parameterLocation;
229};
230} // namespace KIGFX
231
232#endif /* SHADER_H_ */
OpenGL implementation of the Graphics Abstraction Layer.
Definition opengl_gal.h:70
GLuint geomOutputType
Definition shader.h:227
static std::string ReadSource(const std::string &aShaderSourceName)
Read the shader source file.
Definition shader.cpp:215
bool active
Is any of shaders used?
Definition shader.h:220
bool LoadShaderFromFile(SHADER_TYPE aShaderType, const std::string &aShaderSourceName)
Load one of the built-in shaders and compiles it.
Definition shader.cpp:79
bool IsLinked() const
Return true if shaders are linked correctly.
Definition shader.h:114
bool isProgramCreated
Flag for program creation.
Definition shader.h:218
std::deque< GLuint > shaderNumbers
Shader number list.
Definition shader.h:216
void SetParameter(int aParameterNumber, float aValue) const
Set a parameter of the shader.
Definition shader.cpp:139
GLuint geomInputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition shader.h:224
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
std::deque< GLint > parameterLocation
Location of the parameter.
Definition shader.h:228
GLuint programNumber
Shader program number.
Definition shader.h:217
bool Link()
Link the shaders.
Definition shader.cpp:97
void Use()
Use the shader.
Definition shader.h:122
bool isShaderLinked
Is the shader linked?
Definition shader.h:219
void ConfigureGeometryShader(GLuint maxVertices, GLuint geometryInputType, GLuint geometryOutputType)
Configure the geometry shader - has to be done before linking!
Definition shader.cpp:88
bool LoadShaderFromStrings(SHADER_TYPE aShaderType, Args &&... aArgs)
Add a shader and compile the shader sources.
Definition shader.h:89
bool IsActive() const
Return the current state of the shader.
Definition shader.h:142
void shaderInfo(GLuint aShader)
Get the shader information.
Definition shader.cpp:196
virtual ~SHADER()
Definition shader.cpp:51
void programInfo(GLuint aProgram)
Get the shader program information.
Definition shader.cpp:177
int AddParameter(const std::string &aParameterName)
Add a parameter to the parameter queue.
Definition shader.cpp:126
GLuint maximumVertices
The maximum of vertices to be generated.
Definition shader.h:221
int GetAttribute(const std::string &aAttributeName) const
Get an attribute location.
Definition shader.cpp:171
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition shader.h:131
const char * translateStringArg(const std::string &str)
Definition shader.h:49
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
SHADER_TYPE
Type definition for the shader.
Definition shader.h:41
@ SHADER_TYPE_VERTEX
Vertex shader.
Definition shader.h:42
@ SHADER_TYPE_FRAGMENT
Fragment shader.
Definition shader.h:43
@ SHADER_TYPE_GEOMETRY
Geometry shader.
Definition shader.h:44
VECTOR2< double > VECTOR2D
Definition vector2d.h:682