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='281473488726336'>, 1), 'float': (<MagicMock name='mock.GL.GL_FLOAT' id='281473488149040'>, 1), 'int': (<MagicMock name='mock.GL.GL_INT' id='281473487487568'>, 1), 'vec2': (<MagicMock name='mock.GL.GL_FLOAT' id='281473488149040'>, 2), 'vec3': (<MagicMock name='mock.GL.GL_FLOAT' id='281473488149040'>, 3), 'vec4': (<MagicMock name='mock.GL.GL_FLOAT' id='281473488149040'>, 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)[source]¶ Bases:
objectThe
GLSLShaderclass 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, theGLSLShadermakes sure that all uniform and attribute variables are converted to the appropriate type. The following methods are available on aGLSLShader:Loads this
GLSLShaderinto the GL state.Unloads the GL shader program.
Deletes all GL resources managed by this
GLSLShader.Binds all of the shader program
attributevariables - you must set the data for each attribute viasetAtt()before calling this method.Disables all vertex attributes, and unbinds associated vertex buffers.
Sets the value for the specified GLSL
attributevariable.If an index array is to be used by this
GLSLShader(see theindexedargument to__init__()), the index array may be set via this method.Typical usage of a
GLSLShaderwill 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)[source]¶ Create a
GLSLShader.The source is passed through
jinja2, replacing any expressions on the basis ofconstants.- 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.constants – Key-value pairs to be used when passing the source through
jinja2.
-
loadAtts()[source]¶ Binds all of the shader program
attributevariables - you must set the data for each attribute viasetAtt()before calling this method.
-
set= <MagicMock name='mock.utils.memoize.Instanceify()()' id='281473487560816'>¶
-
setAtt(name, value, divisor=None)[source]¶ Sets the value for the specified GLSL
attributevariable.- Parameters
divisor – If specified, this value is used as a divisor for this attribute via the
glVetexAttribDivisorfunction.
Note
If a
divisoris specified, the OpenGLARB_instanced_arraysextension must be available.
-
setIndices(indices)[source]¶ If an index array is to be used by this
GLSLShader(see theindexedargument to__init__()), the index array may be set via this method.
-
_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='281473487560816'>, '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)
-