OpenGL.GL.shaders
Convenience module providing common shader entry points
The point of this module is to allow client code to use
OpenGL Core names to reference shader-related operations
even if the local hardware only supports ARB extension-based
shader rendering.
There are also two utility methods compileProgram and compileShader
which make it easy to create demos which are shader-using.
Functions
compileProgram(
*
shaders
,
**
named
)
Create a new program, attach shaders and validate
- shaders
- arbitrary number of shaders to attach to the generated program.separable (keyword only) -- set the separable flag to allow for partial installation of shader into the pipeline (see glUseProgramStages)retrievable (keyword only) -- set the retrievable flag to allow retrieval of the program binary representation, (see glProgramBinary, glGetProgramBinary)
This convenience function is *not* standard OpenGL,
but it does wind up being fairly useful for demos
and the like. You may wish to copy it to your code
base to guard against PyOpenGL changes.
Usage:
shader = compileProgram(
compileShader( source, GL_VERTEX_SHADER ),
compileShader( source2, GL_FRAGMENT_SHADER ),
)
glUseProgram( shader )
Note:
If (and only if) validation of the linked program
*passes* then the passed-in shader objects will be
deleted from the GL.
returns ShaderProgram() (GLuint) program reference
raises RuntimeError when a link/validation failure occurs
compileShader(
source
,
shaderType
)
Compile shader source of given type
- source
- GLSL source-code for the shader
- shaderType
- GLenum GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, etc,
returns GLuint compiled shader reference
raises RuntimeError when a compilation failure occurs
glGetActiveAttrib (alternate implementations)
glGetActiveAttribARB(
programObj
,
index
,
maxLength
,
length
,
size
,
type
,
name
)
Retrieve the name, size and type of the uniform of the index in the program
glGetActiveUniform (alternate implementations)
glGetActiveUniform(
program
,
index
)
Retrieve the name, size and type of the uniform of the index in the program
glGetActiveUniformARB(
program
,
index
)
Retrieve the name, size and type of the uniform of the index in the program
glGetAttribLocation (alternate implementations)
glGetAttribLocation(
program
,
name
)
-> <class 'ctypes.c_int'>
Check that name is a string with a null byte at the end of it
glGetAttribLocationARB(
program
,
name
)
-> <class 'ctypes.c_int'>
Check that name is a string with a null byte at the end of it
glGetProgramInfoLog (alternate implementations)
glGetProgramInfoLog(
obj
)
Retrieve the shader program's error messages as a Python string
returns string which is '' if no message
glGetInfoLogARB(
obj
)
Retrieve the program/shader's error messages as a Python string
returns string which is '' if no message
glGetProgramiv (alternate implementations)
glGetProgramiv(
program
,
pname
,
params
= None
)
Will automatically allocate params if not provided
glGetShaderInfoLog (alternate implementations)
glGetShaderInfoLog(
obj
)
Retrieve the shader's error messages as a Python string
returns string which is '' if no message
glGetInfoLogARB(
obj
)
Retrieve the program/shader's error messages as a Python string
returns string which is '' if no message
glGetShaderiv (alternate implementations)
glGetShaderiv(
shader
,
pname
,
status
= None
)
Retrieve the integer parameter for the given shader
- shader
- shader ID to query
- pname
- parameter name
- status
- pointer to integer to receive status or None to return the parameter as an integer value
returns
integer if status parameter is None
status if status parameter is not None
glGetShaderSource (alternate implementations)
glGetShaderSource(
obj
)
Retrieve the program/shader's source code as a Python string
returns string which is '' if no source code
glGetShaderSourceARB(
obj
)
Retrieve the program/shader's source code as a Python string
returns string which is '' if no source code
glGetUniformLocation (alternate implementations)
glGetUniformLocation(
program
,
name
)
-> <class 'ctypes.c_int'>
Check that name is a string with a null byte at the end of it
glGetUniformLocationARB(
program
,
name
)
-> <class 'ctypes.c_int'>
Check that name is a string with a null byte at the end of it
glShaderSource (alternate implementations)
glShaderSource(
shaderObj
,
string
)
glShaderSource( GLhandle(shaderObj),[bytes(string),...]) -> None
glShaderSourceARB(
shaderObj
,
string
)
glShaderSourceARB( GLhandleARB(shaderObj), [bytes(string),...] ) -> None
glVertexAttribPointer (alternate implementations)
glVertexAttribPointer(
index
,
size
,
type
,
normalized
,
stride
,
pointer
)
Set an attribute pointer for a given shader (index)
- index
- the index of the generic vertex to bind, see glGetAttribLocation for retrieval of the value, note that index is a global variable, not per-shader
- size
- number of basic elements per record, 1,2,3, or 4
- type
- enum constant for data-type
- normalized
- whether to perform int to float normalization on integer-type values
- stride
- stride in machine units (bytes) between consecutive records, normally used to create "interleaved" arrays
- pointer
- data-pointer which provides the data-values, normally a vertex-buffer-object or offset into the same.
This implementation stores a copy of the data-pointer
in the contextdata structure in order to prevent null-
reference errors in the renderer.
glVertexAttribPointerARB(
index
,
size
,
type
,
normalized
,
stride
,
pointer
)
Set an attribute pointer for a given shader (index)
- index
- the index of the generic vertex to bind, see glGetAttribLocation for retrieval of the value, note that index is a global variable, not per-shader
- size
- number of basic elements per record, 1,2,3, or 4
- type
- enum constant for data-type
- normalized
- whether to perform int to float normalization on integer-type values
- stride
- stride in machine units (bytes) between consecutive records, normally used to create "interleaved" arrays
- pointer
- data-pointer which provides the data-values, normally a vertex-buffer-object or offset into the same.
This implementation stores a copy of the data-pointer
in the contextdata structure in order to prevent null-
reference errors in the renderer.
Classes
class ShaderProgram(
int
):
Integer sub-class with context-manager operation
check_validate(
self
)
Check that the program validates
Validation has to occur *after* linking/loading
raises RuntimeError on failures
load(
self
,
format
,
binary
)
Attempt to load binary-format for a pre-compiled shader
See notes in retrieve
retrieve(
self
)
Attempt to retrieve binary for this compiled shader
Note that binaries for a program are *not* generally portable,
they should be used solely for caching compiled programs for
local use; i.e. to reduce compilation overhead.
returns (format,binaryData) for the shader program