Developer Documentation
ACG::ShaderGenerator Class Reference

#include <ACG/GL/ShaderGenerator.hh>

Classes

struct  DefaultIODesc
 
struct  Keywords
 

Public Member Functions

void setGLSLVersion (int _version)
 Set glsl version.
 
void addIncludeFile (QString _fileName)
 Imports another shader, same as #include.
 
void initVertexShaderIO (const ShaderGenDesc *_desc, const DefaultIODesc *_iodesc)
 Adds fitting vertex shader io for a given description. More...
 
void initTessControlShaderIO (const ShaderGenDesc *_desc, ShaderGenerator *_prevStage, const DefaultIODesc *_iodesc)
 Adds fitting tess-control shader io for a given description.
 
void initTessEvalShaderIO (const ShaderGenDesc *_desc, ShaderGenerator *_prevStage, const DefaultIODesc *_iodesc)
 Adds fitting tess-evaluation shader io for a given description.
 
void initGeometryShaderIO (const ShaderGenDesc *_desc, ShaderGenerator *_prevStage, const DefaultIODesc *_iodesc)
 Adds fitting geometry shader io for a given description.
 
void initFragmentShaderIO (const ShaderGenDesc *_desc, ShaderGenerator *_prevStage, const DefaultIODesc *_iodesc)
 Adds fitting fragment shader io for a given description.
 
void initDefaultUniforms ()
 Adds frequently used uniform parameters. More...
 
void addInput (const QString &_input)
 Add one GLSL input specifier. More...
 
void addInput (const QString &_type, const QString &_varname)
 Add one GLSL input specifier. More...
 
void addOutput (const QString &_output)
 Add one GLSL output specifier. More...
 
void addOutput (const QString &_type, const QString &_varname)
 Add one GLSL output specifier. More...
 
void addUniform (QString _uniform, QString _comment="")
 Add one GLSL uniform specifier. More...
 
void addDefine (const QString &_define)
 Add one define. More...
 
void addIODefine (const QString &_macroName, const QString &_resolvedName)
 Assign an opaque name to the abstract macro. More...
 
void addMacros (const QStringList &_macros)
 Add a list of preprocessor macros. More...
 
bool hasDefine (QString _define) const
 Check for define. More...
 
void addLayout (QString _layout)
 Add a layout directive. More...
 
void addRawIOBlock (QStringList _codeBlock)
 Add a raw glsl IO code block. More...
 
void addLight (int lightIndex_, ShaderGenLightType _light)
 Add a light description to shader:
 
void buildShaderCode (QStringList *_pMainCode, const QStringList &_defaultLightingFunctions)
 Shader assembly function. More...
 
const QStringList & getShaderCode ()
 Get result of buildShaderCode.
 
void saveToFile (const char *_fileName)
 Save generated shader code to text file. More...
 
void matchInputs (const ShaderGenerator *_previousShaderStage, bool _passToNextStage, QString _inputPrefix="outVertex", QString _outputPrefix="outGeometry")
 Perform name matching of outputs and inputs between two shader stages. More...
 
void defineIOAbstraction (const DefaultIODesc *_iodesc, bool _vs, bool _fs)
 Define abstract IO names via shader defines. More...
 
int getNumOutputs () const
 get number of outputs
 
QString getOutputName (int _id) const
 get variable name of output More...
 
int getNumInputs () const
 get number of inputs
 
QString getInputName (int _id) const
 get variable name of input More...
 
QString getIOMapName (int _inId) const
 get corresponding output name of an input id More...
 

Static Public Attributes

static const Keywords keywords
 

Private Member Functions

void addStringToList (QString _str, QStringList *_list, QString _prefix="", QString _postfix="")
 
void addIOToCode (const QStringList &_cmds)
 

Private Attributes

QStringList code_
 
QStringList imports_
 glsl code imports (with #include )
 
int version_
 
QStringList inputs_
 
QStringList outputs_
 
QStringList uniforms_
 
QStringList genDefines_
 
QStringList layouts_
 
QStringList rawIO_
 io block as glsl code
 
bool inputArrays_
 inputs of shader are arrays (tess-control, tess-eval, geometry)
 
bool outputArrays_
 outputs of shader are arrays (tess-control)
 
QString inputPrefix_
 prefix of inputs to this shader, same as prefix of ouputs of previous stage
 
QString outputPrefix_
 prefix of outputs of this shader
 

Detailed Description

The ShaderGenerator is used to collect shader io, uniforms, defines and includes.

The shader main-function is not generated here and must be provided as a parameter to the buildShaderCode function.

Definition at line 524 of file ShaderGenerator.hh.

Member Function Documentation

void ACG::ShaderGenerator::addDefine ( const QString &  _define)

Add one define.

Example:

#define SG_GOURAUD 1

Definition at line 478 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::addInput ( const QString &  _input)

Add one GLSL input specifier.

Stores string pointer only Example:

in vec4 inPosition;

Definition at line 466 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::addInput ( const QString &  _type,
const QString &  _varname 
)
inline

Add one GLSL input specifier.

Stores string pointer only Example:

in vec4 inPosition;

Definition at line 606 of file ShaderGenerator.hh.

void ACG::ShaderGenerator::addIODefine ( const QString &  _macroName,
const QString &  _resolvedName 
)

Assign an opaque name to the abstract macro.

Definition at line 484 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::addIOToCode ( const QStringList &  _cmds)
private

Adds command lines to the shader code. Eventually appends missing ';'

Definition at line 547 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::addLayout ( QString  _layout)

Add a layout directive.

Example:

layout(vertices = 3) out;

Definition at line 530 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::addMacros ( const QStringList &  _macros)

Add a list of preprocessor macros.

Definition at line 489 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::addOutput ( const QString &  _output)

Add one GLSL output specifier.

Stores string pointer only Example:

out vec4 inPosition;

Definition at line 472 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::addOutput ( const QString &  _type,
const QString &  _varname 
)
inline

Add one GLSL output specifier.

Stores string pointer only Example:

out vec4 inPosition;

Definition at line 626 of file ShaderGenerator.hh.

void ACG::ShaderGenerator::addRawIOBlock ( QStringList  _codeBlock)
inline

Add a raw glsl IO code block.

This code block is inserted between the generated IO block and the main function.

Example:

in vec3 someInput;
out vec3 outSomeOutput;
uniform vec3 paramA;
#define USE_METHOD_X
#include "imports.h"
..

Definition at line 691 of file ShaderGenerator.hh.

void ACG::ShaderGenerator::addStringToList ( QString  _str,
QStringList *  _list,
QString  _prefix = "",
QString  _postfix = "" 
)
private

aborts if string already present prefix, postfix functionality is very basic: only checks for occurrence and disregards location

Definition at line 441 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::addUniform ( QString  _uniform,
QString  _comment = "" 
)

Add one GLSL uniform specifier.

Stores string pointer only Example:

uniform sampler2D sampAmbient;

Definition at line 536 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::buildShaderCode ( QStringList *  _pMainCode,
const QStringList &  _defaultLightingFunctions 
)

Shader assembly function.

Also scans shader code for references to default lighting functions LitPointLight(), LitDirLight(), LitSpotLight() and eventually adds these to the shader.

Definition at line 562 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::defineIOAbstraction ( const DefaultIODesc _iodesc,
bool  _vs,
bool  _fs 
)

Define abstract IO names via shader defines.

Default shader inputs and outputs can be conveniently accessed via SG_INPUT_X and SG_OUTPUT_X defines without having to worry about the combinatoric problem of shader stages. For example, in a fragment-shader view-space normals can always be accessed via SG_INPUT_NORMALVS, regardless whether there is a geometry shader or not.

Parameters
_iodescshader IO descriptor
_vsIO abstraction for vertex shader
_fsIO abstraction for fragment shader

Definition at line 299 of file ShaderGenerator.cc.

QString ACG::ShaderGenerator::getInputName ( int  _id) const

get variable name of input

Parameters
_idzero based index of input

Definition at line 786 of file ShaderGenerator.cc.

QString ACG::ShaderGenerator::getIOMapName ( int  _inId) const

get corresponding output name of an input id

Parameters
_inIdzero-based index of input id
Returns
matching output name

Definition at line 804 of file ShaderGenerator.cc.

QString ACG::ShaderGenerator::getOutputName ( int  _id) const

get variable name of output

Parameters
_idzero based index of output

Definition at line 763 of file ShaderGenerator.cc.

bool ACG::ShaderGenerator::hasDefine ( QString  _define) const

Check for define.

Example:

hasDefine("#define SG_REQUEST_NORMAL")

Definition at line 501 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::initDefaultUniforms ( )

Adds frequently used uniform parameters.

Adds frequently used uniform parameters like:

  • world, view, projection matrices
  • cam pos, view dir
  • per object material names: g_cDiffuse, g_cAmbient...

Definition at line 394 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::initVertexShaderIO ( const ShaderGenDesc _desc,
const DefaultIODesc _iodesc 
)

Adds fitting vertex shader io for a given description.

TODO Setup for multiple texture coordinates as input

Definition at line 149 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::matchInputs ( const ShaderGenerator _previousShaderStage,
bool  _passToNextStage,
QString  _inputPrefix = "outVertex",
QString  _outputPrefix = "outGeometry" 
)

Perform name matching of outputs and inputs between two shader stages.

IO of this shader (for instance fragment-shader) and its previous shader (for instance vertex shader) require matching names. Example: The vertex shader has an output "out vec3 outVertexNormal", then this function simply adds the input "in vec3 outVertexNormal" to the current shader (either a geometry or fragment shader). If this is a geometry shader, _passToNextStage should be set to true so that "out vec3 outGeometryNormal" is also added to the outputs.

Parameters
_previousShaderStageshader of the previous stage, which should have all outputs setup already
_passToNextStageall outputs of the previous shader stage are passed to the next stage (should be false for fragment shader for instance)
_inputPrefixname prefix of inputs to this shader (only necessary if _passToNextStage is true)
_outputPrefixname prefix of outputs of this shader (only necessary if _passToNextStage is true)

Definition at line 692 of file ShaderGenerator.cc.

void ACG::ShaderGenerator::saveToFile ( const char *  _fileName)

Save generated shader code to text file.

Parameters
_fileNameWhere to save

Definition at line 667 of file ShaderGenerator.cc.


The documentation for this class was generated from the following files: