Classes | Static Public Member Functions | Private Member Functions

VR3ShaderManager Class Reference

The shader managing static class. More...

#include <VR3ShaderManager.h>

List of all members.

Classes

struct  BgUniformArray
 Structure identifying an array of uniforms associated to a background rendering program. More...
struct  UniformArray
 Structure identifying an array of uniforms associated to a standard rendering program. More...

Static Public Member Functions

static GLuint GetShaderProgram (bool fl_difftex, bool fl_lightmap, bool fl_normmap, bool fl_dispmap, const GLint *&unilocs)
 Gets the standard shader program to be used for a particular mesh.
static GLuint GetBgShaderProgram (GLenum textype, const GLint *&unilocs)
 Gets the background shader program to be used for the current scene.
static GLuint GetShadShaderProgram (const GLint *&unilocs)
 Gets the shadow map shader program to use when rendering shadow maps.
static void GetBlurShaderPrograms (GLuint programs[], const GLint *unilocs[])
 Gets the shader programs to use to blur render shadow maps.
static GLuint GetTextShaderProgram (const GLint *&unilocs)
 Gets the text shader program to use when rendering text string.
static void LoadExtShaderProgram (const char *vs, const char *gs, const char *fs, VR3ShaderProgram *shadprog)
 Loads an external shader program given the source code.

Private Member Functions

 VR3ShaderManager ()
 Private constructor to avoid explicit instantiation.

Static Private Member Functions

Standard Shader Source Manipulation Functions
static void RemLinesContaining (const std::string &str, std::string &shad)
 Removes lines from the shader source.
static void ReplaceIntToken (const std::string &torep, GLint value, std::string &shad)
 Replaces a given token into the source code with an integer value.
static void ReplaceAllIntToken (const std::string &torep, GLint value, std::string &shad)
 Replaces a given token into the source code with an integer value.
static void ReplaceStringToken (const std::string &torep, const std::string &rep, std::string &shad)
 Replaces a given token into the source code with another string.
static void ReplaceAllStringToken (const std::string &torep, const std::string &rep, std::string &shad)
 Replaces a given token into the source code with another string.
static void GenerateOcclusionCode (const std::string *&shad)
 This function is used to build the source code for shadow mapping.
Compiling and Linking Functions
static void LinkProgram (GLuint program)
 Links the given shader program.
static void CompileAttach (GLuint program, const char *source, GLenum type)
 Compiles and attaches a shader to a program.
Standard Shader Source Preparation Functions

The source code for standard vertex, fragment and (eventually) geometry shaders must be properly built before trying to compile it. The basis for this process is a set of shader source code fragments. A function is provided for each acceptable combination of rendering flags. Moreover, these functions will also bind the required program vertex attributes to the correct VAO attribute indices. Parameters:

  • [out] vs = Vertex shader source code
  • [out] fs = Fragment shader source code
  • [out] gs = Geometry shader source code
  • [out] es = Image-Based Lighting computation shader (part of the fragment executable)
  • [out] ss = Shadow computation shader (part of the fragment executable) this one will be a pointer to a string
  • [in] program = The shader program whose attributes must be bound
static void PrepareShadersFFFF (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (false - false - false - false)
static void PrepareShadersTFFF (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (true - false - false - false)
static void PrepareShadersFTFF (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (false - true - false - false)
static void PrepareShadersTTFF (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (true - true - false - false)
static void PrepareShadersFFTF (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (false - false - true - false)
static void PrepareShadersTFTF (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (true - false - true - false)
static void PrepareShadersFFTT (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (false - false - true - true)
static void PrepareShadersFTFT (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (false - true - false - true)
static void PrepareShadersTFTT (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (true - false - true - true)
static void PrepareShadersTTFT (GLuint program, std::string &vs, std::string &gs, std::string &fs, std::string &es, const std::string *&ss)
 Flags = (true - true - false - true)
Standard Shaders Uniform Location Discovery Functions

The following set of functions is used to write the correct uniform locations inside the uniform map. A specific function is provided for each acceptable combination of rendering flags. In these functions we also load the value of static uniform variables, that is uniforms that never change their value (as samplers). Parameters:

  • [in] program = Linked shader program to query
static void GetUniformLocsFFFF (GLuint program)
 Flags = (false - false - false - false)
static void GetUniformLocsTFFF (GLuint program)
 Flags = (true - false - false - false)
static void GetUniformLocsFTFF (GLuint program)
 Flags = (false - true - false - false)
static void GetUniformLocsTTFF (GLuint program)
 Flags = (true - true - false - false)
static void GetUniformLocsFFTF (GLuint program)
 Flags = (false - false - true - false)
static void GetUniformLocsTFTF (GLuint program)
 Flags = (true - false - true - false)
static void GetUniformLocsFFTT (GLuint program)
 Flags = (false - false - true - true)
static void GetUniformLocsFTFT (GLuint program)
 Flags = (false - true - false - true)
static void GetUniformLocsTFTT (GLuint program)
 Flags = (true - false - true - true)
static void GetUniformLocsTTFT (GLuint program)
 Flags = (true - true - false - true)
static void GetUniformLocsShadowAlgorithm (GLuint program)
 Shadow mapping algorithm uniform locations discovery function.

Static Private Attributes

Standard Mesh Rendering Programs
static GLuint ms_std_programs [2][2][2][2]
 Set of (lazily instantiated) standard mesh rendering programs.
static const f_prepareshader ms_std_prepfuncs [2][2][2][2]
 Set of functions used to prepare standard shaders.
static const f_uniformloc ms_std_unilocfuncs [2][2][2][2]
 Set of functions used to discover uniform locations in standard shaders.
static std::map< GLuint,
UniformArray
ms_std_uniforms
 Map binding a uniform parameter set to each active standard shader program.
static std::string ms_occlprob_code
 Fragment shader occlusion computation object source.
Background Rendering Programs
static std::map< GLenum, GLuint > ms_bg_programs
 Set of (lazily instantiated) background rendering programs.
static std::map< GLuint,
BgUniformArray
ms_bg_uniforms
 Map binding a uniform parameter set to each active background shader program.
Shadow Map Rendering Program
static GLuint ms_shad_program
 Lazily instantiated shadow map rendering program.
static GLint ms_shad_uniforms [VR3_NUM_SHAD_UNIFORMS]
 Array of uniforms associated to the shadow map rendering program.
Blurring Programs
static GLuint ms_blur_programs [2]
 Lazily instantiated blurring programs.
static GLint ms_blur_uniforms [2][VR3_NUM_BLUR_UNIFORMS]
 Arrays of uniforms associated to the blurring programs.
Text Rendering Program
static GLuint ms_text_program
 Lazily instantiated text rendering program.
static GLint ms_text_uniforms [VR3_NUM_TEXT_UNIFORMS]
 Array of uniforms associated to the text rendering program.

Detailed Description

The shader managing static class.

This class can be viewed as a namespace with private data members, it cannot be instantiated explicitly and should only be used through the static shader managing methods.

The default shader to be used for rendering a particular mesh in the scene depends upon the mesh parameters. Different shader programs used for different meshes may require different uniform and non-uniform parameters.

The shader manager creates a default program (by compiling GLSL source code) for a material properties combination only when requested and just once for each type of material possible (lazy instantiation). The shader program to be used by default depends upon which feature is requested when rendering:

All of the above feature is associated a texture image in the material, not all possible combination of material properties are possible using default shaders. In particular, the following rules apply when using standard shaders:

  1. When light-mapping is used, normal mapping is not supported and vice-versa, both types of mapping deal with lighting and when using static lighting (light maps) usual light computation via normals is completely disabled.
  2. When using displacement mapping, normals computed from geometry are always used to displace the vertices instead of computing lighting properties. In this case, to avoid intersection of triangles in the scene, a single normal should be computed per vertex, that is, all triangles should belong to the same smoothing group.
  3. When using displacement mapping, a normal map must be provided to enable lighting calculation in the fragment shader, or alternatively a light map must be used to compute static lighting without normals in the fragment shader.
  4. When using displacement mapping, there is an implementation-dependent limit on the number of vertex components a geometry shader may emit. Notice that the number of vertices that we may emit depends on this limit and on the type of shading we are using. In particular, the number of vertices that we try to emit depends on the Tessellation Factor (TF) used [num_vertices = TF*(TF+2)]. If this number exceeds our implementation and shading dependent limit, some triangles will not be emitted.

When shadow mapping is enabled (and the rendered mesh is not light mapped) in the fragment rendering shader we have to check the shadow maps and draw the shadows, this functionality is inserted in all default rendering shaders where possibly needed.

The shader programs used to build the shadow map are also managed by this static shader manager.

The VR3ShaderManager static module also manages a set of shaders program used for rendering the background of a scene. Scene background rendering is very simple compared with mesh rendering and is convenient to build simple programs to do it.

The user may build different (non-standard) shaders by loading files or providing the source code explicitly. When a non-standard shader is activated, it will be used to render every object and every mesh, independently from the material properties. The VR3ShaderManager class provides the functionalities used to load, compile and link external shaders.

Definition at line 111 of file VR3ShaderManager.h.


Constructor & Destructor Documentation

VR3ShaderManager::VR3ShaderManager (  ) [private]

Private constructor to avoid explicit instantiation.


Member Function Documentation

static void VR3ShaderManager::CompileAttach ( GLuint  program,
const char *  source,
GLenum  type 
) [static, private]

Compiles and attaches a shader to a program.

This utility function is used to compile a shader given its source code and type, it also attaches the shader to the provided program

Parameters:
[in]programThe shader program where to attach the shader
[in]sourceThe shader source GLSL code
[in]typeThe shader type (GL_VERTEX_SHADER, GL_GEOMETRY_SHADER or GL_FRAGMENT_SHADER)
static void VR3ShaderManager::GenerateOcclusionCode ( const std::string *&  shad ) [static, private]

This function is used to build the source code for shadow mapping.

When rendering with shadow maps, a special fragment shader object is required to compute the occlusion probability. This function is used to build the source code for this fragment shader object, depending on the implementation limits.

Parameters:
[out]shadThe occlusion computation shader object source (pointer)
static GLuint VR3ShaderManager::GetBgShaderProgram ( GLenum  textype,
const GLint *&  unilocs 
) [static]

Gets the background shader program to be used for the current scene.

When a background for the scene is set, this function is called to obtain the name of the shader program object to be used for background rendering.

Parameters:
[in]textypeType of the texture image used as background
[out]unilocsReturned array of uniform locations
Returns:
The shader program to be used
static void VR3ShaderManager::GetBlurShaderPrograms ( GLuint  programs[],
const GLint *  unilocs[] 
) [static]

Gets the shader programs to use to blur render shadow maps.

When a VR3ShadowController object is created, this function is called to obtain the name of the shader program objects to use to blur shadow maps.

Parameters:
[out]programsReturned programs identifiers
[out]unilocsReturned arrays of uniform locations
static GLuint VR3ShaderManager::GetShaderProgram ( bool  fl_difftex,
bool  fl_lightmap,
bool  fl_normmap,
bool  fl_dispmap,
const GLint *&  unilocs 
) [static]

Gets the standard shader program to be used for a particular mesh.

By specifying the mesh parameters (rendering flags), we are able to select a specific shader program. If the desired shader program is available already, we simply return its name. If the required program has not been created yet, we build it and return its name (after having updated the uniform map and the program matrix). This function also returns the array of uniform locations associated with the program, this is done to enable efficient rendering in the VR3Mesh class.

Parameters:
[in]fl_difftexDiffuse texture mapping required
[in]fl_lightmapLight mapping required
[in]fl_normmapNormal mapping required
[in]fl_dispmapDisplacement mapping required
[out]unilocsReturned array of uniform locations
Returns:
The shader program to be used (0 if no proper standard shader program was found)
static GLuint VR3ShaderManager::GetShadShaderProgram ( const GLint *&  unilocs ) [static]

Gets the shadow map shader program to use when rendering shadow maps.

When a VR3ShadowController object is created, this function is called to obtain the name of the shader program object to use to render shadow maps.

Parameters:
[out]unilocsReturned array of uniform locations
Returns:
The shader program to be used
static GLuint VR3ShaderManager::GetTextShaderProgram ( const GLint *&  unilocs ) [static]

Gets the text shader program to use when rendering text string.

When a VR3Text object is created, this function is called to obtain the name of the shader program object to use to render text strings.

Parameters:
[out]unilocsReturned array of uniform locations
Returns:
The shader program to be used
static void VR3ShaderManager::GetUniformLocsFFFF ( GLuint  program ) [static, private]

Flags = (false - false - false - false)

static void VR3ShaderManager::GetUniformLocsFFTF ( GLuint  program ) [static, private]

Flags = (false - false - true - false)

static void VR3ShaderManager::GetUniformLocsFFTT ( GLuint  program ) [static, private]

Flags = (false - false - true - true)

static void VR3ShaderManager::GetUniformLocsFTFF ( GLuint  program ) [static, private]

Flags = (false - true - false - false)

static void VR3ShaderManager::GetUniformLocsFTFT ( GLuint  program ) [static, private]

Flags = (false - true - false - true)

static void VR3ShaderManager::GetUniformLocsShadowAlgorithm ( GLuint  program ) [static, private]

Shadow mapping algorithm uniform locations discovery function.

Regardless of the combination of rendering flags, when a standard shader program is built, some uniform locations must be extracted with reference to the shadow mapping algorithm. This function extracts those uniform locations. In this function we also load the value of the shadow map samplers (that never changes).

static void VR3ShaderManager::GetUniformLocsTFFF ( GLuint  program ) [static, private]

Flags = (true - false - false - false)

static void VR3ShaderManager::GetUniformLocsTFTF ( GLuint  program ) [static, private]

Flags = (true - false - true - false)

static void VR3ShaderManager::GetUniformLocsTFTT ( GLuint  program ) [static, private]

Flags = (true - false - true - true)

static void VR3ShaderManager::GetUniformLocsTTFF ( GLuint  program ) [static, private]

Flags = (true - true - false - false)

static void VR3ShaderManager::GetUniformLocsTTFT ( GLuint  program ) [static, private]

Flags = (true - true - false - true)

static void VR3ShaderManager::LinkProgram ( GLuint  program ) [static, private]

Links the given shader program.

This utility function is used to compile a shader program.

Parameters:
[in]programThe shader program to link
static void VR3ShaderManager::LoadExtShaderProgram ( const char *  vs,
const char *  gs,
const char *  fs,
VR3ShaderProgram shadprog 
) [static]

Loads an external shader program given the source code.

When the user creates a new VR3ShaderProgram object (i.e. a new external shader program to be used for rendering) this function is used to compile and link the shader program source code. Vertex attributes are bound to enable external shaders to obtain geometry data (if the same standard input names are used for parameters in the shader).

Parameters:
[in]vsThe vertex shader source code
[in]gsThe geometry shader source code (0 if none)
[in]fsThe fragment shader source code
[out]shadprogThe VR3ShaderProgram object to initialize
static void VR3ShaderManager::PrepareShadersFFFF ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (false - false - false - false)

static void VR3ShaderManager::PrepareShadersFFTF ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (false - false - true - false)

static void VR3ShaderManager::PrepareShadersFFTT ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (false - false - true - true)

static void VR3ShaderManager::PrepareShadersFTFF ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (false - true - false - false)

static void VR3ShaderManager::PrepareShadersFTFT ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (false - true - false - true)

static void VR3ShaderManager::PrepareShadersTFFF ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (true - false - false - false)

static void VR3ShaderManager::PrepareShadersTFTF ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (true - false - true - false)

static void VR3ShaderManager::PrepareShadersTFTT ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (true - false - true - true)

static void VR3ShaderManager::PrepareShadersTTFF ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (true - true - false - false)

static void VR3ShaderManager::PrepareShadersTTFT ( GLuint  program,
std::string &  vs,
std::string &  gs,
std::string &  fs,
std::string &  es,
const std::string *&  ss 
) [static, private]

Flags = (true - true - false - true)

static void VR3ShaderManager::RemLinesContaining ( const std::string &  str,
std::string &  shad 
) [static, private]

Removes lines from the shader source.

This function is used to remove lines from a given shader string, the lines removed are the ones containing the provided string.

Parameters:
[in]strContent of the lines to remove
[in,out]shadThe shader source code string
static void VR3ShaderManager::ReplaceAllIntToken ( const std::string &  torep,
GLint  value,
std::string &  shad 
) [static, private]

Replaces a given token into the source code with an integer value.

This function is used to replace a particular token in the source code with an integer value translated into a string. The token is replaced multiple times, until is not found anymore in the string.

Parameters:
[in]torepThe token to replace
[in]valueThe integer value to insert in the shader source code
[in,out]shadThe shader source code string
static void VR3ShaderManager::ReplaceAllStringToken ( const std::string &  torep,
const std::string &  rep,
std::string &  shad 
) [static, private]

Replaces a given token into the source code with another string.

This function is used to replace a particular token in the source code with a string. The token is replaced multiple times, until is not found anymore in the string.

Parameters:
[in]torepThe token to replace
[in]repThe string the token is replaced with
[in,out]shadThe shader source code string
static void VR3ShaderManager::ReplaceIntToken ( const std::string &  torep,
GLint  value,
std::string &  shad 
) [static, private]

Replaces a given token into the source code with an integer value.

This function is used to replace a particular token in the source code with an integer value translated into a string. Just one replacement is performed.

Parameters:
[in]torepThe token to replace
[in]valueThe integer value to insert in the shader source code
[in,out]shadThe shader source code string
static void VR3ShaderManager::ReplaceStringToken ( const std::string &  torep,
const std::string &  rep,
std::string &  shad 
) [static, private]

Replaces a given token into the source code with another string.

This function is used to replace a particular token in the source code with a string. Just one replacement is performed.

Parameters:
[in]torepThe token to replace
[in]repThe string the token is replaced with
[in,out]shadThe shader source code string

Member Data Documentation

std::map<GLenum, GLuint> VR3ShaderManager::ms_bg_programs [static, private]

Set of (lazily instantiated) background rendering programs.

This is a map where the key is the type of the texture used as background.

Definition at line 178 of file VR3ShaderManager.h.

std::map< GLuint, BgUniformArray > VR3ShaderManager::ms_bg_uniforms [static, private]

Map binding a uniform parameter set to each active background shader program.

Each background shader program may need some uniform variables, those must be written every time the program is used to render the scene background. This map contains the locations used for the shader program uniform variables.

Definition at line 193 of file VR3ShaderManager.h.

GLuint VR3ShaderManager::ms_blur_programs[2] [static, private]

Lazily instantiated blurring programs.

This is an array of two programs, we always create these two programs together. The first is the horizontal blurring program, the second is the vertical blurring program.

Definition at line 227 of file VR3ShaderManager.h.

GLint VR3ShaderManager::ms_blur_uniforms[2][VR3_NUM_BLUR_UNIFORMS] [static, private]

Arrays of uniforms associated to the blurring programs.

The blurring programs needs some uniform variables, those must be written every time the program is used to blur the shadow map. These arrays contain the locations used for the programs uniform variables.

Definition at line 235 of file VR3ShaderManager.h.

std::string VR3ShaderManager::ms_occlprob_code [static, private]

Fragment shader occlusion computation object source.

The source code of the fragment shader object that computes the occlusion probability for the EVSM shadow mapping algorithm is quite tiring to build. This string is where the source code is stored; therefore, it is built just once (it only depends on the OpenGL implementation) when first required.

Definition at line 165 of file VR3ShaderManager.h.

GLuint VR3ShaderManager::ms_shad_program [static, private]

Lazily instantiated shadow map rendering program.

This is a single GL program name.

Definition at line 205 of file VR3ShaderManager.h.

GLint VR3ShaderManager::ms_shad_uniforms[VR3_NUM_SHAD_UNIFORMS] [static, private]

Array of uniforms associated to the shadow map rendering program.

The shadow map shader program needs some uniform variables, those must be written every time the program is used to render the shadow map. This array contains the locations used for the shader program uniform variables.

Definition at line 213 of file VR3ShaderManager.h.

const f_prepareshader VR3ShaderManager::ms_std_prepfuncs[2][2][2][2] [static, private]

Set of functions used to prepare standard shaders.

When a standard shader must be prepared (its source code must be built) one of the functions in the following set is called.

Definition at line 133 of file VR3ShaderManager.h.

GLuint VR3ShaderManager::ms_std_programs[2][2][2][2] [static, private]

Set of (lazily instantiated) standard mesh rendering programs.

This is a four-dimensional matrix with a cell for each shading program, if we want the program to be used for rendering when diffuse texturing and normal mapping are used, we should access m_programs[1][0][1][0] or m_programs[true][false][true][false].

Definition at line 127 of file VR3ShaderManager.h.

std::map< GLuint, UniformArray > VR3ShaderManager::ms_std_uniforms [static, private]

Map binding a uniform parameter set to each active standard shader program.

Each shader program needs some uniform variables, those must be written every time the program is used by a particular mesh. The uniform variables values are written independently while rendering each mesh because their value will usually change. This map contains the locations used for the shader program uniform variables.

Definition at line 156 of file VR3ShaderManager.h.

const f_uniformloc VR3ShaderManager::ms_std_unilocfuncs[2][2][2][2] [static, private]

Set of functions used to discover uniform locations in standard shaders.

When a standard shader must has been compiled and linked, one of the functions in the following set is used to discover the uniform locations.

Definition at line 140 of file VR3ShaderManager.h.

GLuint VR3ShaderManager::ms_text_program [static, private]

Lazily instantiated text rendering program.

This is a single GL program name.

Definition at line 250 of file VR3ShaderManager.h.

GLint VR3ShaderManager::ms_text_uniforms[VR3_NUM_TEXT_UNIFORMS] [static, private]

Array of uniforms associated to the text rendering program.

The text rendering program needs some uniform variables, those must be written every time the program is used to render some string. This array contains the locations used for the text program uniform variables.

Definition at line 258 of file VR3ShaderManager.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines