NvGLSLProgram Class Reference
Convenience wrapper for GLSL shader programs. More...
#include <NvGLSLProgram.h>
Classes | |
struct | ShaderSourceItem |
Represents a piece of shader source and the shader type Used with creation functions to pass in arrays of multiple shader source types. More... | |
Public Member Functions | |
NvGLSLProgram () | |
Default constructor. | |
bool | setSourceFromFiles (const char *vertFilename, const char *fragFilename, bool strict=false) |
Initializes an existing shader object from a pair of filenames/paths Uses NvAssetLoaderRead to load the files. | |
bool | setSourceFromStrings (const char *vertSrc, const char *fragSrc, bool strict=false) |
Creates and returns a shader object from a pair of source strings. | |
bool | setSourceFromStrings (const char **vertSrcArray, int32_t vertSrcCount, const char **fragSrcArray, int32_t fragSrcCount, bool strict=false) |
Creates and returns a shader object from a pair of source strings Convenience function. | |
bool | setSourceFromStrings (ShaderSourceItem *src, int32_t count, bool strict=false) |
Creates and returns a shader object from an array of ShaderSourceItem source objects. | |
void | enable () |
Binds the given shader program as current in the GL context. | |
void | disable () |
Unbinds the given shader program from the GL context (binds shader 0). | |
void | bindTexture2D (const char *name, int32_t unit, GLuint tex) |
Binds a 2D texture to a shader uniform by name. | |
void | bindTexture2D (GLint index, int32_t unit, GLuint tex) |
Binds a 2D texture to a shader uniform by index. | |
void | bindTexture2DMultisample (const char *name, int32_t unit, GLuint tex) |
Binds a 2D Multisample texture to a shader uniform by name. | |
void | bindTexture2DMultisample (GLint index, int32_t unit, GLuint tex) |
Binds a 2D Multisample texture to a shader uniform by index. | |
void | bindTextureRect (const char *name, int32_t unit, GLuint tex) |
Binds a Rect texture to a shader uniform by name. | |
void | bindTextureRect (GLint index, int32_t unit, GLuint tex) |
Binds a Rect texture to a shader uniform by index. | |
void | bindTextureArray (const char *name, int32_t unit, GLuint tex) |
Binds an array texture to a shader uniform by name. | |
void | bindTextureArray (GLint index, int32_t unit, GLuint tex) |
Binds an array texture to a shader uniform by index. | |
void | setUniformMatrix4fv (const GLchar *name, GLfloat *m, int32_t count=1, bool transpose=false) |
Set matrix array program uniform array by name Assumes that the given shader is bound via enable. | |
void | setUniformMatrix4fv (GLint index, GLfloat *m, int32_t count=1, bool transpose=false) |
Set matrix array program uniform array by index Assumes that the given shader is bound via enable. | |
GLint | getAttribLocation (const char *attribute, bool isOptional=false) |
Returns the index containing the named vertex attribute. | |
GLint | getUniformLocation (const char *uniform, bool isOptional=false) |
Returns the index containing the named uniform. | |
GLuint | getProgram () |
Returns the GL program object for the shader. | |
bool | relink () |
Relinks an existing shader program to update based on external changes. | |
void | setUniform1i (const char *name, int32_t value) |
Set scalar program uniform by string name Assumes that the given shader is bound via enable. | |
void | setUniform1f (const char *name, float value) |
Set scalar program uniform by string name Assumes that the given shader is bound via enable. | |
void | setUniform2i (const char *name, int32_t x, int32_t y) |
Set 2-vec program uniform by string name Assumes that the given shader is bound via enable. | |
void | setUniform2f (const char *name, float x, float y) |
Set 2-vec program uniform by string name Assumes that the given shader is bound via enable. | |
void | setUniform3i (const char *name, int32_t x, int32_t y, int32_t z) |
Set 3-vec program uniform by string name Assumes that the given shader is bound via enable. | |
void | setUniform3f (const char *name, float x, float y, float z) |
Set 3-vec program uniform by string name Assumes that the given shader is bound via enable. | |
void | setUniform4f (const char *name, float x, float y, float z, float w) |
Set 4-vec program uniform by string name Assumes that the given shader is bound via enable. | |
void | setUniform3fv (const char *name, const float *value, int32_t count=1) |
Set vector program uniform array by string name Assumes that the given shader is bound via enable. | |
void | setUniform4fv (const char *name, const float *value, int32_t count=1) |
Set vector program uniform array by string name Assumes that the given shader is bound via enable. | |
void | setUniform1i (GLint index, int32_t value) |
void | setUniform1f (GLint index, float value) |
void | setUniform2i (GLint index, int32_t x, int32_t y) |
void | setUniform2f (GLint index, float x, float y) |
void | setUniform3i (GLint index, int32_t x, int32_t y, int32_t z) |
void | setUniform3f (GLint index, float x, float y, float z) |
void | setUniform4f (GLint index, float x, float y, float z, float w) |
void | setUniform3fv (GLint index, const float *value, int32_t count=1) |
Set vector program uniform array by index Assumes that the given shader is bound via enable. | |
void | setUniform4fv (GLint index, const float *value, int32_t count=1) |
Set vector program uniform array by index Assumes that the given shader is bound via enable. | |
Static Public Member Functions | |
static NvGLSLProgram * | createFromFiles (const char *vertFilename, const char *fragFilename, bool strict=false) |
Creates and returns a shader object from a pair of filenames/paths Uses NvAssetLoaderRead to load the files. | |
static NvGLSLProgram * | createFromStrings (const char *vertSrc, const char *fragSrc, bool strict=false) |
Creates and returns a shader object from a pair of source strings Convenience function. | |
static NvGLSLProgram * | createFromStrings (const char **vertSrcArray, int32_t vertSrcCount, const char **fragSrcArray, int32_t fragSrcCount, bool strict=false) |
Creates and returns a shader object from a pair of arrays of source strings Convenience function. | |
static void | setGlobalShaderHeader (const char *header) |
Sets a global shader text header to be prepended to all shaders created through this class. | |
static const char * | getGlobalShaderHeader () |
Returns the current global shader header or NULL if none is active. | |
static void | setLogAllMissing (bool logMissing) |
Enables logging of missing uniforms even for non-strict shaders. | |
Protected Member Functions | |
bool | checkCompileError (GLuint object, int32_t target) |
GLuint | compileProgram (const char *vsource, const char *fsource) |
GLuint | compileProgram (const char **vertSrcArray, int32_t vertSrcCount, const char **fragSrcArray, int32_t fragSrcCount) |
GLuint | compileProgram (ShaderSourceItem *src, int32_t count) |
Protected Attributes | |
bool | m_strict |
GLuint | m_program |
Static Protected Attributes | |
static bool | ms_logAllMissing |
static const char * | ms_shaderHeader |
Detailed Description
Convenience wrapper for GLSL shader programs.Wraps shader programs and simplifies creation, setting uniforms and setting vertex attributes. Supports all forms of shaders, but has simple paths for the common case of shader programs consisting of only vertex and fragment shaders.
Constructor & Destructor Documentation
NvGLSLProgram::NvGLSLProgram | ( | ) |
Default constructor.
Creates an empty object with no shader program, because we cannot set the source in the constructor. Doing so would require the possibility of failure, and we cannot fail a constructor.
Member Function Documentation
void NvGLSLProgram::bindTexture2D | ( | GLint | index, | |
int32_t | unit, | |||
GLuint | tex | |||
) |
Binds a 2D texture to a shader uniform by index.
Binds the given texture to the supplied texture unit and the unit to the given uniform Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform unit] the texture unit which will be used for the texture tex] the texture to be bound
void NvGLSLProgram::bindTexture2D | ( | const char * | name, | |
int32_t | unit, | |||
GLuint | tex | |||
) |
Binds a 2D texture to a shader uniform by name.
Binds the given texture to the supplied texture unit and the unit to the given uniform Assumes that the given shader is bound via enable
- Parameters:
-
[in] name the null-terminated string name of the uniform unit] the texture unit which will be used for the texture tex] the texture to be bound
void NvGLSLProgram::bindTexture2DMultisample | ( | GLint | index, | |
int32_t | unit, | |||
GLuint | tex | |||
) |
Binds a 2D Multisample texture to a shader uniform by index.
Binds the given texture to the supplied texture unit and the unit to the given uniform Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform unit] the texture unit which will be used for the texture tex] the texture to be bound
- Note:
- Requires GL 3.2 or GLES 3.1
void NvGLSLProgram::bindTexture2DMultisample | ( | const char * | name, | |
int32_t | unit, | |||
GLuint | tex | |||
) |
Binds a 2D Multisample texture to a shader uniform by name.
Binds the given texture to the supplied texture unit and the unit to the given uniform Assumes that the given shader is bound via enable
- Parameters:
-
[in] name the null-terminated string name of the uniform unit] the texture unit which will be used for the texture tex] the texture to be bound
- Note:
- Requires GL 3.2 or GLES 3.1
void NvGLSLProgram::bindTextureArray | ( | GLint | index, | |
int32_t | unit, | |||
GLuint | tex | |||
) |
Binds an array texture to a shader uniform by index.
Binds the given texture to the supplied texture unit and the unit to the given uniform Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform unit] the texture unit which will be used for the texture tex] the texture to be bound
void NvGLSLProgram::bindTextureArray | ( | const char * | name, | |
int32_t | unit, | |||
GLuint | tex | |||
) |
Binds an array texture to a shader uniform by name.
Binds the given texture to the supplied texture unit and the unit to the given uniform Assumes that the given shader is bound via enable
- Parameters:
-
[in] name the null-terminated string name of the uniform unit] the texture unit which will be used for the texture tex] the texture to be bound
void NvGLSLProgram::bindTextureRect | ( | GLint | index, | |
int32_t | unit, | |||
GLuint | tex | |||
) |
Binds a Rect texture to a shader uniform by index.
Binds the given texture to the supplied texture unit and the unit to the given uniform Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform unit] the texture unit which will be used for the texture tex] the texture to be bound
void NvGLSLProgram::bindTextureRect | ( | const char * | name, | |
int32_t | unit, | |||
GLuint | tex | |||
) |
Binds a Rect texture to a shader uniform by name.
Binds the given texture to the supplied texture unit and the unit to the given uniform Assumes that the given shader is bound via enable
- Parameters:
-
[in] name the null-terminated string name of the uniform unit] the texture unit which will be used for the texture tex] the texture to be bound
static NvGLSLProgram* NvGLSLProgram::createFromFiles | ( | const char * | vertFilename, | |
const char * | fragFilename, | |||
bool | strict = false | |||
) | [static] |
Creates and returns a shader object from a pair of filenames/paths Uses NvAssetLoaderRead to load the files.
Convenience function.
- Parameters:
-
[in] vertFilename the filename and partial path to the text file containing the vertex shader source [in] fragFilename the filename and partial path to the text file containing the fragment shader source [in] strict if set to true, then later calls to retrieve the locations of non- existent uniforms and vertex attributes will log a warning to the output
- Returns:
- a pointer to an NvGLSLProgram on success and NULL on failure
static NvGLSLProgram* NvGLSLProgram::createFromStrings | ( | const char ** | vertSrcArray, | |
int32_t | vertSrcCount, | |||
const char ** | fragSrcArray, | |||
int32_t | fragSrcCount, | |||
bool | strict = false | |||
) | [static] |
Creates and returns a shader object from a pair of arrays of source strings Convenience function.
- Parameters:
-
[in] vertSrcArray the array of null-terminated strings containing the vertex shader source [in] vertSrcCount the number of strings in vertSrcArray [in] fragSrcArray the array of null-terminated strings containing the fragment shader source [in] fragSrcCount the number of strings in fragSrcArray [in] strict if set to true, then later calls to retrieve the locations of non- existent uniforms and vertex attributes will log a warning to the output
- Returns:
- a pointer to an NvGLSLProgram on success and NULL on failure
static NvGLSLProgram* NvGLSLProgram::createFromStrings | ( | const char * | vertSrc, | |
const char * | fragSrc, | |||
bool | strict = false | |||
) | [static] |
Creates and returns a shader object from a pair of source strings Convenience function.
- Parameters:
-
[in] vertSrc the null-terminated string containing the vertex shader source [in] fragSrc the null-terminated string containing the fragment shader source [in] strict if set to true, then later calls to retrieve the locations of non- existent uniforms and vertex attributes will log a warning to the output
- Returns:
- a pointer to an NvGLSLProgram on success and NULL on failure
GLint NvGLSLProgram::getAttribLocation | ( | const char * | attribute, | |
bool | isOptional = false | |||
) |
Returns the index containing the named vertex attribute.
- Parameters:
-
[in] uniform the null-terminated string name of the attribute [in] isOptional if true, the function logs an error if the attribute is not found
- Returns:
- the non-negative index of the attribute if found. -1 if not found
static const char* NvGLSLProgram::getGlobalShaderHeader | ( | ) | [inline, static] |
Returns the current global shader header or NULL if none is active.
- Returns:
- the current global shader header or NULL if none is active
GLuint NvGLSLProgram::getProgram | ( | ) | [inline] |
Returns the GL program object for the shader.
- Returns:
- the GL shader object ID
GLint NvGLSLProgram::getUniformLocation | ( | const char * | uniform, | |
bool | isOptional = false | |||
) |
Returns the index containing the named uniform.
- Parameters:
-
[in] uniform the null-terminated string name of the uniform [in] isOptional if true, the function logs an error if the uniform is not found
- Returns:
- the non-negative index of the uniform if found. -1 if not found
static void NvGLSLProgram::setGlobalShaderHeader | ( | const char * | header | ) | [inline, static] |
Sets a global shader text header to be prepended to all shaders created through this class.
This can be useful in cases where the shaders need a different version header on different platforms.
- Parameters:
-
[in] header the string to be appended or NULL if no header. NULL also disables the feature as desired. String is not copied or destroyed.
static void NvGLSLProgram::setLogAllMissing | ( | bool | logMissing | ) | [inline, static] |
Enables logging of missing uniforms even for non-strict shaders.
- Parameters:
-
[in] logMissing if set to true, missing uniforms are logged when set, even if the shader was not created with the strict flag
bool NvGLSLProgram::setSourceFromFiles | ( | const char * | vertFilename, | |
const char * | fragFilename, | |||
bool | strict = false | |||
) |
Initializes an existing shader object from a pair of filenames/paths Uses NvAssetLoaderRead to load the files.
Convenience function.
- Parameters:
-
[in] vertFilename the filename and partial path to the text file containing the vertex shader source [in] fragFilename the filename and partial path to the text file containing the fragment shader source [in] strict if set to true, then later calls to retrieve the locations of non- existent uniforms and vertex attributes will log a warning to the output
- Returns:
- true on success and false on failure
bool NvGLSLProgram::setSourceFromStrings | ( | ShaderSourceItem * | src, | |
int32_t | count, | |||
bool | strict = false | |||
) |
Creates and returns a shader object from an array of ShaderSourceItem source objects.
- Parameters:
-
[in] src an array of ShaderSourceItem objects containing the shaders sources to be loaded. Unlike the vert/frag-only creation functions, this version can accept additional shader types such as geometry and tessellation shaders (if supported) [in] count the number of elements in src array [in] strict if set to true, then later calls to retrieve the locations of non- existent uniforms and vertex attributes will log a warning to the output
- Returns:
- true on success and false on failure
bool NvGLSLProgram::setSourceFromStrings | ( | const char ** | vertSrcArray, | |
int32_t | vertSrcCount, | |||
const char ** | fragSrcArray, | |||
int32_t | fragSrcCount, | |||
bool | strict = false | |||
) |
Creates and returns a shader object from a pair of source strings Convenience function.
- Parameters:
-
[in] vertSrc the null-terminated string containing the vertex shader source [in] fragSrc the null-terminated string containing the fragment shader source [in] strict if set to true, then later calls to retrieve the locations of non- existent uniforms and vertex attributes will log a warning to the output
- Returns:
- a pointer to an NvGLSLProgram on success and NULL on failure
bool NvGLSLProgram::setSourceFromStrings | ( | const char * | vertSrc, | |
const char * | fragSrc, | |||
bool | strict = false | |||
) |
Creates and returns a shader object from a pair of source strings.
- Parameters:
-
[in] vertSrc the null-terminated string containing the vertex shader source [in] fragSrc the null-terminated string containing the fragment shader source [in] strict if set to true, then later calls to retrieve the locations of non- existent uniforms and vertex attributes will log a warning to the output
- Returns:
- true on success and false on failure
void NvGLSLProgram::setUniform1f | ( | GLint | index, | |
float | value | |||
) |
Set scalar program uniforms by index Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform [in] value value of scale uniform
void NvGLSLProgram::setUniform1f | ( | const char * | name, | |
float | value | |||
) |
Set scalar program uniform by string name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] name the null-terminated string with the name of the uniform [in] value scalar value of the uniform
void NvGLSLProgram::setUniform1i | ( | GLint | index, | |
int32_t | value | |||
) |
Set scalar program uniforms by index Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform [in] value value of scale uniform
void NvGLSLProgram::setUniform1i | ( | const char * | name, | |
int32_t | value | |||
) |
Set scalar program uniform by string name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] name the null-terminated string with the name of the uniform [in] value scalar value of the uniform
void NvGLSLProgram::setUniform2f | ( | GLint | index, | |
float | x, | |||
float | y | |||
) |
Set 2-vec program uniforms by index Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform [in] x first value of vector uniform [in] y second value of vector uniform
void NvGLSLProgram::setUniform2f | ( | const char * | name, | |
float | x, | |||
float | y | |||
) |
Set 2-vec program uniform by string name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] name the null-terminated string with the name of the uniform [in] x first value of vector uniform [in] y second value of vector uniform
void NvGLSLProgram::setUniform2i | ( | GLint | index, | |
int32_t | x, | |||
int32_t | y | |||
) |
Set 2-vec program uniforms by index Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform [in] x first value of vector uniform [in] y second value of vector uniform
void NvGLSLProgram::setUniform2i | ( | const char * | name, | |
int32_t | x, | |||
int32_t | y | |||
) |
Set 2-vec program uniform by string name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] name the null-terminated string with the name of the uniform [in] x first value of vector uniform [in] y second value of vector uniform
void NvGLSLProgram::setUniform3f | ( | GLint | index, | |
float | x, | |||
float | y, | |||
float | z | |||
) |
Set 3-vec program uniforms by index Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform [in] x first value of vector uniform [in] y second value of vector uniform [in] z third value of vector uniform
void NvGLSLProgram::setUniform3f | ( | const char * | name, | |
float | x, | |||
float | y, | |||
float | z | |||
) |
Set 3-vec program uniform by string name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] name the null-terminated string with the name of the uniform [in] x first value of vector uniform [in] y second value of vector uniform [in] z third value of vector uniform
void NvGLSLProgram::setUniform3fv | ( | GLint | index, | |
const float * | value, | |||
int32_t | count = 1 | |||
) |
Set vector program uniform array by index Assumes that the given shader is bound via enable.
- Parameters:
-
[in] index the index of the uniform [in] value array of values [in] count number of values in array unform
void NvGLSLProgram::setUniform3fv | ( | const char * | name, | |
const float * | value, | |||
int32_t | count = 1 | |||
) |
Set vector program uniform array by string name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] value array of values [in] count number of values in array unform
void NvGLSLProgram::setUniform3i | ( | GLint | index, | |
int32_t | x, | |||
int32_t | y, | |||
int32_t | z | |||
) |
Set 3-vec program uniforms by index Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform [in] x first value of vector uniform [in] y second value of vector uniform [in] z third value of vector uniform
void NvGLSLProgram::setUniform3i | ( | const char * | name, | |
int32_t | x, | |||
int32_t | y, | |||
int32_t | z | |||
) |
Set 3-vec program uniform by string name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] name the null-terminated string with the name of the uniform [in] x first value of vector uniform [in] y second value of vector uniform [in] z third value of vector uniform
void NvGLSLProgram::setUniform4f | ( | GLint | index, | |
float | x, | |||
float | y, | |||
float | z, | |||
float | w | |||
) |
Set 4-vec program uniforms by index Assumes that the given shader is bound via enable
- Parameters:
-
[in] index the index of the uniform [in] x first value of vector uniform [in] y second value of vector uniform [in] z third value of vector uniform [in] w fourth value of vector uniform
void NvGLSLProgram::setUniform4f | ( | const char * | name, | |
float | x, | |||
float | y, | |||
float | z, | |||
float | w | |||
) |
Set 4-vec program uniform by string name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] name the null-terminated string with the name of the uniform [in] x first value of vector uniform [in] y second value of vector uniform [in] z third value of vector uniform [in] w fourth value of vector uniform
void NvGLSLProgram::setUniform4fv | ( | GLint | index, | |
const float * | value, | |||
int32_t | count = 1 | |||
) |
Set vector program uniform array by index Assumes that the given shader is bound via enable.
- Parameters:
-
[in] index the index of the uniform [in] value array of values [in] count number of values in array unform
void NvGLSLProgram::setUniform4fv | ( | const char * | name, | |
const float * | value, | |||
int32_t | count = 1 | |||
) |
Set vector program uniform array by string name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] value array of values [in] count number of values in array unform
void NvGLSLProgram::setUniformMatrix4fv | ( | GLint | index, | |
GLfloat * | m, | |||
int32_t | count = 1 , |
|||
bool | transpose = false | |||
) |
Set matrix array program uniform array by index Assumes that the given shader is bound via enable.
- Parameters:
-
[in] index the index of the uniform [in] m array of matrices [in] count number of values in array unform [in] transpose if true, the matrices are transposed on input
void NvGLSLProgram::setUniformMatrix4fv | ( | const GLchar * | name, | |
GLfloat * | m, | |||
int32_t | count = 1 , |
|||
bool | transpose = false | |||
) |
Set matrix array program uniform array by name Assumes that the given shader is bound via enable.
- Parameters:
-
[in] name the null-terminated string name of the uniform [in] m array of matrices [in] count number of values in array unform [in] transpose if true, the matrices are transposed on input
The documentation for this class was generated from the following file: