fsleyes.gl.shaders.glsl.program
¶
This module provides the GLSLShader
class, which encapsulates
a GLSL shader program comprising a vertex shader and a fragment shader.
-
fsleyes.gl.shaders.glsl.program.
GLSL_ATTRIBUTE_TYPES
= {'bool': (<MagicMock name='mock.GL.GL_BOOL' id='139845833390848'>, 1), 'float': (<MagicMock name='mock.GL.GL_FLOAT' id='139845834505912'>, 1), 'int': (<MagicMock name='mock.GL.GL_INT' id='139845835825952'>, 1), 'vec2': (<MagicMock name='mock.GL.GL_FLOAT' id='139845834505912'>, 2), 'vec3': (<MagicMock name='mock.GL.GL_FLOAT' id='139845834505912'>, 3), 'vec4': (<MagicMock name='mock.GL.GL_FLOAT' id='139845834505912'>, 4)}¶ This dictionary contains mappings between GLSL data types, and their corresponding GL types and sizes.
-
class
fsleyes.gl.shaders.glsl.program.
GLSLShader
(vertSrc, fragSrc, indexed=False)¶ Bases:
object
The
GLSLShader
class encapsulates information and logic about a GLSL 1.20 shader program, comprising a vertex shader and a fragment shader. It provides methods to set shader attribute and uniform values, to configure attributes, and to load/unload the program. Furthermore, theGLSLShader
makes sure that all uniform and attribute variables are converted to the appropriate type. The following methods are available on aGLSLShader
:load
Loads this GLSLShader
into the GL state.unload
Unloads the GL shader program. destroy
Deletes all GL resources managed by this GLSLShader
.loadAtts
Binds all of the shader program attribute
variables - you must set the data for each attribute viasetAtt()
before calling this method.unloadAtts
Disables all vertex attributes, and unbinds associated vertex buffers. set
setAtt
Sets the value for the specified GLSL attribute
variable.setIndices
If an index array is to be used by this GLSLShader
(see theindexed
argument to__init__()
), the index array may be set via this method.Typical usage of a
GLSLShader
will look something like the following:vertSrc = 'vertex shader source' fragSrc = 'fragment shader source' program = GLSLShader(vertSrc, fragSrc) # Load the program program.load() # Set some uniform values program.set('lighting', True) program.set('lightPos', [0, 0, -1]) # Create and set vertex attributes vertices, normals = createVertices() program.setAtt('vertex', vertices) program.setAtt('normal', normals) # Load the attributes program.loadAtts() # Draw the scene gl.glDrawArrays(gl.GL_TRIANGLES, 0, len(vertices)) # Clear the GL state program.unload() program.unloadAtts() # Delete the program when # we no longer need it program.destroy()
-
__init__
(vertSrc, fragSrc, indexed=False)¶ Create a
GLSLShader
.Parameters: - vertSrc – String containing vertex shader source code.
- fragSrc – String containing fragment shader source code.
- indexed – If
True
, it is assumed that the vertices processed by this shader program will be drawn using an index array. A vertex buffer object is created to store vertex indices - this buffer is expected to be populated via thesetIndices()
method.
-
__del__
()¶ Prints a log message.
-
load
()¶ Loads this
GLSLShader
into the GL state.
-
loadAtts
()¶ Binds all of the shader program
attribute
variables - you must set the data for each attribute viasetAtt()
before calling this method.
-
unloadAtts
()¶ Disables all vertex attributes, and unbinds associated vertex buffers.
-
unload
()¶ Unloads the GL shader program.
-
destroy
()¶ Deletes all GL resources managed by this
GLSLShader
.
-
set
= <MagicMock name='mock.utils.memoize.Instanceify()()' id='139845833549472'>¶
-
setAtt
(name, value, divisor=None)¶ Sets the value for the specified GLSL
attribute
variable.Parameters: divisor – If specified, this value is used as a divisor for this attribute via the glVetexAttribDivisor
function.Note
If a
divisor
is specified, the OpenGLARB_instanced_arrays
extension must be available.
-
setIndices
(indices)¶ If an index array is to be used by this
GLSLShader
(see theindexed
argument to__init__()
), the index array may be set via this method.
-
_attribute_bool
(val)¶
-
_attribute_int
(val)¶
-
_attribute_float
(val)¶
-
_attribute_vec2
(val)¶
-
_attribute_vec3
(val)¶
-
_attribute_vec4
(val)¶
-
_uniform_bool
(pos, val, size)¶
-
_uniform_int
(pos, val, size)¶
-
_uniform_float
(pos, val, size)¶
-
_uniform_vec2
(pos, val, size)¶
-
_uniform_vec3
(pos, val, size)¶
-
_uniform_vec4
(pos, val, size)¶
-
_uniform_mat2
(pos, val, size)¶
-
_uniform_mat3
(pos, val, size)¶
-
_uniform_mat4
(pos, val, size)¶
-
_uniform_sampler1D
(pos, val, size)¶
-
_GLSLShader__compile
(vertShaderSrc, fragShaderSrc)¶ Compiles and links the OpenGL GLSL vertex and fragment shader programs, and returns a reference to the resulting program. Raises an error if compilation/linking fails.
Note
I’m explicitly not using the PyOpenGL
OpenGL.GL.shaders.compileProgram()
function, because it attempts to validate the program after compilation, which fails due to texture data not being bound at the time of validation.
-
_GLSLShader__getPositions
(shaders, vertAtts, vertUniforms, fragUniforms)¶ Gets the position indices for all vertex shader attributes, uniforms, and fragment shader uniforms for the given shader programs.
Parameters: - shaders – Reference to the compiled shader program.
- vertAtts – List of attributes required by the vertex shader.
- vertUniforms – List of uniforms required by the vertex shader.
- fragUniforms – List of uniforms required by the fragment shader.
Returns: A dictionary of
{name : position}
mappings.
-
__dict__
= mappingproxy({'__module__': 'fsleyes.gl.shaders.glsl.program', '__doc__': "The ``GLSLShader`` class encapsulates information and logic about\n a GLSL 1.20 shader program, comprising a vertex shader and a fragment\n shader. It provides methods to set shader attribute and uniform values,\n to configure attributes, and to load/unload the program. Furthermore,\n the ``GLSLShader`` makes sure that all uniform and attribute variables\n are converted to the appropriate type. The following methods are available\n on a ``GLSLShader``:\n\n\n .. autosummary::\n :nosignatures:\n\n load\n unload\n destroy\n loadAtts\n unloadAtts\n set\n setAtt\n setIndices\n\n\n Typical usage of a ``GLSLShader`` will look something like the\n following::\n\n vertSrc = 'vertex shader source'\n fragSrc = 'fragment shader source'\n\n program = GLSLShader(vertSrc, fragSrc)\n\n # Load the program\n program.load()\n\n # Set some uniform values\n program.set('lighting', True)\n program.set('lightPos', [0, 0, -1])\n\n # Create and set vertex attributes\n vertices, normals = createVertices()\n\n program.setAtt('vertex', vertices)\n program.setAtt('normal', normals)\n\n # Load the attributes\n program.loadAtts()\n\n # Draw the scene\n gl.glDrawArrays(gl.GL_TRIANGLES, 0, len(vertices))\n\n # Clear the GL state\n program.unload()\n program.unloadAtts()\n\n\n # Delete the program when\n # we no longer need it\n program.destroy()\n ", '__init__': <function GLSLShader.__init__>, '__del__': <function GLSLShader.__del__>, 'load': <function GLSLShader.load>, 'loadAtts': <function GLSLShader.loadAtts>, 'unloadAtts': <function GLSLShader.unloadAtts>, 'unload': <function GLSLShader.unload>, 'destroy': <function GLSLShader.destroy>, 'set': <MagicMock name='mock.utils.memoize.Instanceify()()' id='139845833549472'>, 'setAtt': <function GLSLShader.setAtt>, 'setIndices': <function GLSLShader.setIndices>, '_GLSLShader__getPositions': <function GLSLShader.__getPositions>, '_GLSLShader__compile': <function GLSLShader.__compile>, '_attribute_bool': <function GLSLShader._attribute_bool>, '_attribute_int': <function GLSLShader._attribute_int>, '_attribute_float': <function GLSLShader._attribute_float>, '_attribute_vec2': <function GLSLShader._attribute_vec2>, '_attribute_vec3': <function GLSLShader._attribute_vec3>, '_attribute_vec4': <function GLSLShader._attribute_vec4>, '_uniform_bool': <function GLSLShader._uniform_bool>, '_uniform_int': <function GLSLShader._uniform_int>, '_uniform_float': <function GLSLShader._uniform_float>, '_uniform_vec2': <function GLSLShader._uniform_vec2>, '_uniform_vec3': <function GLSLShader._uniform_vec3>, '_uniform_vec4': <function GLSLShader._uniform_vec4>, '_uniform_mat2': <function GLSLShader._uniform_mat2>, '_uniform_mat3': <function GLSLShader._uniform_mat3>, '_uniform_mat4': <function GLSLShader._uniform_mat4>, '_uniform_sampler1D': <function GLSLShader._uniform_sampler1D>, '_uniform_sampler2D': <function GLSLShader._uniform_sampler2D>, '_uniform_sampler3D': <function GLSLShader._uniform_sampler3D>, '__dict__': <attribute '__dict__' of 'GLSLShader' objects>, '__weakref__': <attribute '__weakref__' of 'GLSLShader' objects>})¶
-
__module__
= 'fsleyes.gl.shaders.glsl.program'¶
-
__weakref__
¶ list of weak references to the object (if defined)
-
_uniform_sampler2D
(pos, val, size)¶
-
_uniform_sampler3D
(pos, val, size)¶
-