Developer Documentation
Shader Generator

Automatically generated Shader Uniforms

Matrix

mat4 g_mWVP - world-view-projection transform
mat4 g_mWV - world-view transform
mat4 g_mWVIT - inverse transpose of world view matrix
mat4 g_mP - projection matrix

Color

vec3 g_cDiffuse - diffuse color
vec3 g_cAmbient - ambient color
vec3 g_cEmissive - emissive color
vec3 g_cSpecular - specular color
vec4 g_vMaterial - vec4(shininess, alpha, unused, unused)

Lighting

// light parameters denoted by zero-based index i
vec3 g_cLightDiffuse_i - diffuse color
vec3 g_cLightAmbient_i - ambient color
vec3 g_cLightSpecular_i - specular color
vec3 g_vLightPos_i - position in view space (for point and spot lights only)
vec3 g_vLightAtten_i - [constant, linear, quadratic] attenuation factors (for point and spot lights only)
vec3 g_vLightDir_i - light direction in view space (for spot and directional lights only)
vec2 g_vLightAngleExp_i - [cos(spotCutOffAngle), spot exponent] (for spot lights only)

Texturing

sampler2D g_Texture0 - texture sampler (if textured is enabled in ShaderGenDesc)

Automatically generated shader input/output:

Vertex shader IO

in vec4 inPosition - vertex position in model space
in vec3 inNormal - vertex normal (only if ShaderGenDesc.shadeMode != unlit)
in vec4 inColor - vertex color (only if ShaderGenDesc.vertexColors == true)
in vec2 inTexCoord - texture coordinate (only if ShaderGenDesc.textured == true)
out vec4 outPosCS - position in clip space
out vec3 outNormal - normal in view space (only if ShaderGenDesc.shadeMode == phong)
out vec4 outPosVS - position in view space (only if ShaderGenDesc.shadeMode == phong)
out vec4 outColor - vertex color after lighting (only if ShaderGenDesc.shadeMode == flat or gouraud)
out vec2 outTexCoord - texture coordinate (only if ShaderGenDesc.textured == true)
// insert input/outputs added from modifiers here

Fragment shader IO

See vertex shader outputs for fragment inputs.

out vec4 outFragment - fragment color
// insert input/outputs added from modifiers here

Automatically generated shader defines

These come in handy when you need to keep compatibility with custom shaders.

Depending on the shademode of ShaderGenDesc, exactly one of the following defines is active.

  • SG_GOURAUD
  • SG_FLAT
  • SG_UNLIT
  • SG_PHONG

Additional defines:

SG_NORMALS // defined if vertex normals are available
SG_TEXTURE // defined if ShaderGenDesc.textured == true
SG_VERTEX_COLOR // defined if ShaderGenDesc.vertexColors == true

Lighting:

SG_NUM_LIGHTS // number of lights
//for each light i:
SG_LIGHT_TYPE_i // Determine the type of light i; set to one of {SG_LIGHT_DIRECTIONAL, SG_LIGHT_POINT, SG_LIGHT_SPOT}
// Example:
#define SG_LIGHT_TYPE_0 SG_LIGHT_DIRECTIONAL
#define SG_LIGHT_TYPE_1 SG_LIGHT_DIRECTIONAL
#define SG_LIGHT_TYPE_2 SG_LIGHT_POINT

Vertex shader generation structure

void main()
{
vec4 sg_vPosPS = g_mWVP * inPosition;
vec4 sg_vPosVS = g_mWV * inPosition;
vec3 sg_vNormalVS = vec3(0.0, 1.0, 0.0);
vec2 sg_vTexCoord = vec2(0.0, 0.0);
vec4 sg_cColor = vec4(g_cEmissive, SG_ALPHA);
#if normals available
sg_vNormalVS = g_mWVIT * inNormal;
#if textured
sg_vTexCoord = inTexCoord;
#if vertexcolors
sg_cColor = inColor;
#if vertex-lighting enabled
lighting code, save lit color in sg_cColor
// -------------------------------------------------------------------------
// begin customized code
// - insert registered begin-code modifiers, that ideally operate on generated sg_* variables
// - loaded code from template file is added here
// make sure to use #ifdef #endif if you make use of conditional inputs such as normals, texcoords..
// end of customized code
// -------------------------------------------------------------------------
gl_Position = sv_vPosPS;
outPosCS = sg_vPosPS;
#if textured
outTexCoord = sg_vTexCoord;
#if vertex-lighting
outColor = sg_cColor;
#if fragment-lighting
outNormal = sg_vNormalVS;
outPosVS = sg_vPosVS;
// end-code modifiers are added here
}

Fragment shader generation structure

void main()
{
// compute screen-projected coordinates, useful for various post-processing effects
vec2 sg_vScreenPos = outPosCS.xy / outPosCS.w * 0.5 + vec2(0.5, 0.5);
vec4 sg_cColor = vec4(g_cEmisive, SG_ALPHA);
#if vertex-lighting
sg_cColor = outColor;
#if fragment lighting
vec4 sg_vPosVS = outPosVS;
vec3 sg_vNormalVS = outNormal;
lighting code here, save color to sg_cColor
#if textured
vec4 sg_cTex = texture(g_Texture0, outTexCoord);
sg_cColor *= sg_cTex;
// -------------------------------------------------------------------------
// begin customized code
// - insert registered begin-code modifiers, that ideally operate on generated sg_* variables
// - loaded code from template file is added here
// make sure to use #ifdef #endif if you make use of conditional inputs such as normals, texcoords..
// end of customized code
// -------------------------------------------------------------------------
outFragment = sg_cColor;
// end-code modifiers are added here
}