racer home

GLSL shaders

 

Home Out with Cg, in with GLSL.


Dolphinity Organiser - free planning, project management and organizing software for all your action lists

Introduction

GLSL has since long been a part of the OpenGL standard. Back when Racer started using GPU shaders, the language wasn't quite stable yet. But it has grown since then, and has some features that make it interesting now.

There are some drawbacks as well though.

Therefore, Racer has created its own shell around the thin layer of GLSL, to be more programmer-friendly.

Racer's GLSL implementation

Racer uses GLSL but has some additions and prerequisites:

out vec4 fragColor;
void main(void)
{
  fragColor=vec4(1.0, 0.0, 0.0, 1.0);
}

Uniform block parameters

GLSL allows 'uniform blocks'. These are GPU-located buffers where a number of parameters are set. These parameters can be shared across all programs. For example, with Cg, Racer had to set the ModelViewProject matrix (MVP) for all shaders separately each frame. With GLSL, you can set one global block of parameters that apply to all GLSL programs, and be done with it.

Racer hardcodes a uniform block that is defined in data/renderer/common/globals.glsl. This is defined as:

layout(std140) uniform WorldGlobals
{
... (parameter definitions)
} globals;

This defines a structure that should not be modified by the user, as it is reflected in Racer itself (in C++). The name 'WorldGlobals' stems from 'World', which is the internal name for the scenegraph/rendering engine that Racer uses.

Parameters are then referenced using for example globals.modelViewProj. A number of parameters that are the same for each program is listed below:

Name (globals.*) Description
exposure Exposure value, somewhere between 0 and 1 probably. (i.e. 'uniform float exposure').

Uniform names

GLSL uses 'uniform' variables to pass parameters that don't change per vertex, but at most only once per object/shader. In principle it would be possible to create a binding from a uniform name to a uniform's contextual meaning (like 'bind mvp ModelViewMatrix') but that means that you have to learn the binding meanings anyway. Racer is taking the way that the uniform names must be specific, so they can be found and set to their intended value.

These uniforms normally vary per material (Racer shader); a lot of parameters are in the 'globals' uniform block above. However, some parameters such as diffuse/ambient colors can differ per material, so are defined locally per GLSL program.

Here is a list of the currently defined uniform names. They must be used exactly for Racer to find them, and set them to the correct value.

Name Type Description
diffuse vec3 RGB color defining the diffuse color of the material
fragColor vec4 The output color for a fragment shader. I.e. 'out vec4 fragColor'.
tex0 vec2 Texture in first texture unit (i.e. 'uniform sampler tex0')
tex1 vec2 Texture in 2nd texture unit
tex2 vec2 Texture in 3rd texture unit
tex3 vec2 Texture in 4th texture unit
tex4 vec2 Texture in 5th texture unit
tex5 vec2 Texture in 6th texture unit
tex6 vec2 Texture in 7th texture unit
tex7 vec2 Texture in 8th texture unit

Attribute names

GLSL uses 'attribute' variables to pass per-vertex data. Similary to uniforms, these variables must be given explicit names to work ok. According to nVidia, a lot of these attribute indices are hardcoded, to be compatible with older software. For example, vertex data is attribute index 0, normals are attribute index 2 etc.

Here is a list of the currently defined attribute names. They must be used exactly for Racer to find them, and set them to the correct value. For example:

Attribute name Attribute index Description
inVertex 0 in vec4 inVertex; incoming vertex position XYZ and W.
inNormal 2 in vec3 inNormal; incoming normals.
inColor 3 RGB
inSecondaryColor 4 RGB; probably unusable in Racer, since there's no way to set it for a shader.
inFogCoord 5 Unusable in Racer
inMultiTexCoord0 8 First set of texture coordinates; the most used.
inMultiTexCoord1 9 2nd set of texture coordinates
inMultiTexCoord2 10 3rd set
inMultiTexCoord3 11 4th set
inMultiTexCoord4 12 5th set
inMultiTexCoord5 13 6th set
inMultiTexCoord6 14 7th set
inMultiTexCoord7 15 8th set

Convert Cg into GLSL

Racer has used Cg for a long time, so you might be familiar with it. Here is a list of things you will encounter when transitioning from Cg to GLSL:

 


Dolphinity Organiser - free planning, project management and organizing software for all your action lists

(last updated November 13, 2012 )