Developer Documentation
ACG::ShaderGenerator Class Reference

#include <OpenFlipper/libs_required/ACG/GL/ShaderGenerator.hh>

Classes

struct  DefaultIODesc
 
struct  Keywords
 

Public Member Functions

void setGLSLVersion (int _version)
 Set glsl version. More...
 
void addIncludeFile (QString _fileName)
 Imports another shader, same as #include. More...
 
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. More...
 
void initTessEvalShaderIO (const ShaderGenDesc *_desc, ShaderGenerator *_prevStage, const DefaultIODesc *_iodesc)
 Adds fitting tess-evaluation shader io for a given description. More...
 
void initGeometryShaderIO (const ShaderGenDesc *_desc, ShaderGenerator *_prevStage, const DefaultIODesc *_iodesc)
 Adds fitting geometry shader io for a given description. More...
 
void initFragmentShaderIO (const ShaderGenDesc *_desc, ShaderGenerator *_prevStage, const DefaultIODesc *_iodesc)
 Adds fitting fragment shader io for a given description. More...
 
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: More...
 
void buildShaderCode (QStringList *_pMainCode, const QStringList &_defaultLightingFunctions)
 Shader assembly function. More...
 
const QStringList & getShaderCode ()
 Get result of buildShaderCode. More...
 
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 More...
 
QString getOutputName (int _id) const
 get variable name of output More...
 
int getNumInputs () const
 get number of inputs More...
 
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 ) More...
 
int version_
 
QStringList inputs_
 
QStringList outputs_
 
QStringList uniforms_
 
QStringList genDefines_
 
QStringList layouts_
 
QStringList rawIO_
 io block as glsl code More...
 
bool inputArrays_
 inputs of shader are arrays (tess-control, tess-eval, geometry) More...
 
bool outputArrays_
 outputs of shader are arrays (tess-control) More...
 
QString inputPrefix_
 prefix of inputs to this shader, same as prefix of ouputs of previous stage More...
 
QString outputPrefix_
 prefix of outputs of this shader More...
 

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 557 of file ShaderGenerator.hh.

Constructor & Destructor Documentation

◆ ShaderGenerator()

ACG::ShaderGenerator::ShaderGenerator ( )

Definition at line 132 of file ShaderGenerator.cc.

◆ ~ShaderGenerator()

ACG::ShaderGenerator::~ShaderGenerator ( )
virtual

Definition at line 137 of file ShaderGenerator.cc.

Member Function Documentation

◆ addDefine()

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

Add one define.

Example:

#define SG_GOURAUD 1

Definition at line 473 of file ShaderGenerator.cc.

◆ addIncludeFile()

void ACG::ShaderGenerator::addIncludeFile ( QString  _fileName)

Imports another shader, same as #include.

Definition at line 628 of file ShaderGenerator.cc.

◆ addInput() [1/2]

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

Add one GLSL input specifier.

Stores string pointer only Example:

in vec4 inPosition;

Definition at line 461 of file ShaderGenerator.cc.

◆ addInput() [2/2]

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 639 of file ShaderGenerator.hh.

◆ addIODefine()

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

Assign an opaque name to the abstract macro.

Definition at line 479 of file ShaderGenerator.cc.

◆ addIOToCode()

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

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

Definition at line 542 of file ShaderGenerator.cc.

◆ addLayout()

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

Add a layout directive.

Example:

layout(vertices = 3) out;

Definition at line 525 of file ShaderGenerator.cc.

◆ addLight()

void ACG::ShaderGenerator::addLight ( int  lightIndex_,
ShaderGenLightType  _light 
)

Add a light description to shader:

Definition at line 408 of file ShaderGenerator.cc.

◆ addMacros()

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

Add a list of preprocessor macros.

Definition at line 484 of file ShaderGenerator.cc.

◆ addOutput() [1/2]

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

Add one GLSL output specifier.

Stores string pointer only Example:

out vec4 inPosition;

Definition at line 467 of file ShaderGenerator.cc.

◆ addOutput() [2/2]

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 659 of file ShaderGenerator.hh.

◆ addRawIOBlock()

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 724 of file ShaderGenerator.hh.

◆ addStringToList()

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 436 of file ShaderGenerator.cc.

◆ addUniform()

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

Add one GLSL uniform specifier.

Stores string pointer only Example:

uniform sampler2D sampAmbient;

Definition at line 531 of file ShaderGenerator.cc.

◆ buildShaderCode()

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 557 of file ShaderGenerator.cc.

◆ defineIOAbstraction()

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 292 of file ShaderGenerator.cc.

◆ getInputName()

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

get variable name of input

Parameters
_idzero based index of input

Definition at line 778 of file ShaderGenerator.cc.

◆ getIOMapName()

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 796 of file ShaderGenerator.cc.

◆ getNumInputs()

int ACG::ShaderGenerator::getNumInputs ( ) const

get number of inputs

Definition at line 773 of file ShaderGenerator.cc.

◆ getNumOutputs()

int ACG::ShaderGenerator::getNumOutputs ( ) const

get number of outputs

Definition at line 750 of file ShaderGenerator.cc.

◆ getOutputName()

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

get variable name of output

Parameters
_idzero based index of output

Definition at line 755 of file ShaderGenerator.cc.

◆ getShaderCode()

const QStringList & ACG::ShaderGenerator::getShaderCode ( )

Get result of buildShaderCode.

Definition at line 676 of file ShaderGenerator.cc.

◆ hasDefine()

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

Check for define.

Example:

hasDefine("#define SG_REQUEST_NORMAL")
bool hasDefine(QString _define) const
Check for define.

Definition at line 496 of file ShaderGenerator.cc.

◆ initDefaultUniforms()

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 387 of file ShaderGenerator.cc.

◆ initFragmentShaderIO()

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

Adds fitting fragment shader io for a given description.

Definition at line 277 of file ShaderGenerator.cc.

◆ initGeometryShaderIO()

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

Adds fitting geometry shader io for a given description.

Definition at line 262 of file ShaderGenerator.cc.

◆ initTessControlShaderIO()

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

Adds fitting tess-control shader io for a given description.

Definition at line 236 of file ShaderGenerator.cc.

◆ initTessEvalShaderIO()

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

Adds fitting tess-evaluation shader io for a given description.

Definition at line 249 of file ShaderGenerator.cc.

◆ initVertexShaderIO()

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 143 of file ShaderGenerator.cc.

◆ matchInputs()

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 686 of file ShaderGenerator.cc.

◆ saveToFile()

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

Save generated shader code to text file.

Parameters
_fileNameWhere to save

Definition at line 661 of file ShaderGenerator.cc.

◆ setGLSLVersion()

void ACG::ShaderGenerator::setGLSLVersion ( int  _version)

Set glsl version.

Definition at line 681 of file ShaderGenerator.cc.

Member Data Documentation

◆ code_

QStringList ACG::ShaderGenerator::code_
private

Definition at line 882 of file ShaderGenerator.hh.

◆ genDefines_

QStringList ACG::ShaderGenerator::genDefines_
private

Definition at line 893 of file ShaderGenerator.hh.

◆ imports_

QStringList ACG::ShaderGenerator::imports_
private

glsl code imports (with #include )

Definition at line 886 of file ShaderGenerator.hh.

◆ inputArrays_

bool ACG::ShaderGenerator::inputArrays_
private

inputs of shader are arrays (tess-control, tess-eval, geometry)

Definition at line 900 of file ShaderGenerator.hh.

◆ inputPrefix_

QString ACG::ShaderGenerator::inputPrefix_
private

prefix of inputs to this shader, same as prefix of ouputs of previous stage

Definition at line 906 of file ShaderGenerator.hh.

◆ inputs_

QStringList ACG::ShaderGenerator::inputs_
private

Definition at line 890 of file ShaderGenerator.hh.

◆ keywords

const ShaderGenerator::Keywords ACG::ShaderGenerator::keywords
static

Definition at line 867 of file ShaderGenerator.hh.

◆ layouts_

QStringList ACG::ShaderGenerator::layouts_
private

Definition at line 894 of file ShaderGenerator.hh.

◆ outputArrays_

bool ACG::ShaderGenerator::outputArrays_
private

outputs of shader are arrays (tess-control)

Definition at line 903 of file ShaderGenerator.hh.

◆ outputPrefix_

QString ACG::ShaderGenerator::outputPrefix_
private

prefix of outputs of this shader

Definition at line 909 of file ShaderGenerator.hh.

◆ outputs_

QStringList ACG::ShaderGenerator::outputs_
private

Definition at line 891 of file ShaderGenerator.hh.

◆ rawIO_

QStringList ACG::ShaderGenerator::rawIO_
private

io block as glsl code

Definition at line 897 of file ShaderGenerator.hh.

◆ uniforms_

QStringList ACG::ShaderGenerator::uniforms_
private

Definition at line 892 of file ShaderGenerator.hh.

◆ version_

int ACG::ShaderGenerator::version_
private

Definition at line 888 of file ShaderGenerator.hh.


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