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
86 void Abandon();
87
96 template< typename... Args >
97 bool LoadShaderFromStrings( SHADER_TYPE aShaderType, Args&&... aArgs )
98 {
99 const char* arr[] = { DETAIL::translateStringArg( aArgs )... };
100 return loadShaderFromStringArray( aShaderType, arr, sizeof...(Args) );
101 }
102
110 bool LoadShaderFromFile( SHADER_TYPE aShaderType, const std::string& aShaderSourceName );
111
117 bool Link();
118
122 bool IsLinked() const
123 {
124 return isShaderLinked;
125 }
126
130 inline void Use()
131 {
132 glUseProgram( programNumber );
133 active = true;
134 }
135
139 inline void Deactivate()
140 {
141 glUseProgram( 0 );
142 active = false;
143 }
144
150 inline bool IsActive() const
151 {
152 return active;
153 }
154
162 void ConfigureGeometryShader( GLuint maxVertices, GLuint geometryInputType,
163 GLuint geometryOutputType );
164
175 int AddParameter( const std::string& aParameterName );
176
183 void SetParameter( int aParameterNumber, float aValue ) const;
184 void SetParameter( int aParameterNumber, int aValue ) const;
185 void SetParameter( int aParameterNumber, const VECTOR2D& aValue ) const;
186 void SetParameter( int aParameterNumber, float f0, float f1, float f2, float f3 ) const;
187
194 int GetAttribute( const std::string& aAttributeName ) const;
195
202 static std::string ReadSource( const std::string& aShaderSourceName );
203
204private:
208 bool loadShaderFromStringArray( SHADER_TYPE aShaderType, const char** aArray, size_t aSize );
209
215 void programInfo( GLuint aProgram );
216
222 void shaderInfo( GLuint aShader );
223
224 std::deque<GLuint> shaderNumbers;
228 bool active;
230
233
236 std::deque<GLint> parameterLocation;
237};
238} // namespace KIGFX
239
240#endif /* SHADER_H_ */
OpenGL implementation of the Graphics Abstraction Layer.
Definition opengl_gal.h:72
GLuint geomOutputType
Output type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition shader.h:235
static std::string ReadSource(const std::string &aShaderSourceName)
Read the shader source file.
Definition shader.cpp:223
bool active
Is any of shaders used?
Definition shader.h:228
bool LoadShaderFromFile(SHADER_TYPE aShaderType, const std::string &aShaderSourceName)
Load one of the built-in shaders and compiles it.
Definition shader.cpp:87
bool IsLinked() const
Return true if shaders are linked correctly.
Definition shader.h:122
bool isProgramCreated
Flag for program creation.
Definition shader.h:226
std::deque< GLuint > shaderNumbers
Shader number list.
Definition shader.h:224
void Abandon()
Give up ownership of the program and shader objects without deleting them.
Definition shader.cpp:51
void SetParameter(int aParameterNumber, float aValue) const
Set a parameter of the shader.
Definition shader.cpp:147
GLuint geomInputType
Input type [e.g. GL_LINES, GL_TRIANGLES, GL_QUADS etc.].
Definition shader.h:232
bool loadShaderFromStringArray(SHADER_TYPE aShaderType, const char **aArray, size_t aSize)
Compile vertex of fragment shader source code into the program.
Definition shader.cpp:245
std::deque< GLint > parameterLocation
Location of the parameter.
Definition shader.h:236
GLuint programNumber
Shader program number.
Definition shader.h:225
bool Link()
Link the shaders.
Definition shader.cpp:105
void Use()
Use the shader.
Definition shader.h:130
bool isShaderLinked
Is the shader linked?
Definition shader.h:227
void ConfigureGeometryShader(GLuint maxVertices, GLuint geometryInputType, GLuint geometryOutputType)
Configure the geometry shader - has to be done before linking!
Definition shader.cpp:96
bool LoadShaderFromStrings(SHADER_TYPE aShaderType, Args &&... aArgs)
Add a shader and compile the shader sources.
Definition shader.h:97
bool IsActive() const
Return the current state of the shader.
Definition shader.h:150
void shaderInfo(GLuint aShader)
Get the shader information.
Definition shader.cpp:204
virtual ~SHADER()
Definition shader.cpp:59
void programInfo(GLuint aProgram)
Get the shader program information.
Definition shader.cpp:185
int AddParameter(const std::string &aParameterName)
Add a parameter to the parameter queue.
Definition shader.cpp:134
GLuint maximumVertices
The maximum of vertices to be generated.
Definition shader.h:229
int GetAttribute(const std::string &aAttributeName) const
Get an attribute location.
Definition shader.cpp:179
void Deactivate()
Deactivate the shader and use the default OpenGL program.
Definition shader.h:139
const char * translateStringArg(const std::string &str)
Definition shader.h:49
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:30
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:707