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='140736137065296'>, 1), 'float': (<MagicMock name='mock.GL.GL_FLOAT' id='140736138809232'>, 1), 'int': (<MagicMock name='mock.GL.GL_INT' id='140736137702736'>, 1), 'vec2': (<MagicMock name='mock.GL.GL_FLOAT' id='140736138809232'>, 2), 'vec3': (<MagicMock name='mock.GL.GL_FLOAT' id='140736138809232'>, 3), 'vec4': (<MagicMock name='mock.GL.GL_FLOAT' id='140736138809232'>, 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, constants=None)

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, the GLSLShader makes sure that all uniform and attribute variables are converted to the appropriate type. The following methods are available on a GLSLShader:

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 via setAtt() 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 the indexed 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, constants=None)

Create a GLSLShader.

The source is passed through jinja2, replacing any expressions on the basis of constants.

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 the setIndices() method.

  • constants – Key-value pairs to be used when passing the source through jinja2.

__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 via setAtt() 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='140736137784080'>
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 OpenGL ARB_instanced_arrays extension must be available.

setIndices(indices)

If an index array is to be used by this GLSLShader (see the indexed 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='140736137784080'>, '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)