Uranium
Application Framework
Loading...
Searching...
No Matches
UM.View.GL.ShaderProgram.ShaderProgram Class Reference

An abstract class for dealing with shader programs. More...

Public Member Functions

 __init__ (self)
 
 load (self, file_name, version="")
 Load a shader program file.
 
 setVertexShader (self, shader)
 Set the vertex shader to use.
 
 setFragmentShader (self, shader)
 Set the fragment shader to use.
 
 setGeometryShader (self, shader)
 
 build (self)
 Build the complete shader program out of the separately provided sources.
 
 setUniformValue (self, name, value, **kwargs)
 Set a named uniform variable.
 
 setTexture (self, texture_unit, texture)
 Set a texture that should be bound to a specified texture unit when this shader is bound.
 
 enableAttribute (self, name, type, offset, stride=0)
 Enable a vertex attribute to be used.
 
 disableAttribute (self, name)
 Disable a vertex attribute so it is no longer used.
 
 bind (self)
 Bind the shader to use it for rendering.
 
 release (self)
 Release the shader so it will no longer be used for rendering.
 
 addBinding (self, key, value)
 Add a uniform value binding.
 
 removeBinding (self, key)
 Remove a uniform value binding.
 
 updateBindings (self, **kwargs)
 Update the values of bindings.
 
 addAttributeBinding (self, key, value)
 Add an attribute binding.
 
 removeAttributeBinding (self, key)
 Remove an attribute binding.
 

Protected Member Functions

 _matrixToQMatrix4x4 (self, m)
 
 _setUniformValueDirect (self, uniform, value)
 

Protected Attributes

 _bindings
 
 _attribute_bindings
 
 _shader_program
 
 _uniform_indices
 
 _attribute_indices
 
 _uniform_values
 
 _bound
 
 _textures
 
 _debug_shader
 

Detailed Description

An abstract class for dealing with shader programs.

This class provides an interface an some basic elements for dealing with shader programs. Shader programs are described in a simple text file based on the Python configparser module. These files contain the shaders for the different shader program stages, in addition to defaults that should be used for uniform values and uniform and attribute bindings.

Member Function Documentation

◆ addAttributeBinding()

UM.View.GL.ShaderProgram.ShaderProgram.addAttributeBinding (   self,
  key,
  value 
)

Add an attribute binding.

Attribute bindings are similar to uniform value bindings, except they specify what what attribute name binds to which attribute in the shader.

TODO: Actually use these bindings. However, that kind of depends on a more freeform MeshData object as freeform bindings are rather useless when we only have 5 supported attributes.

Parameters
keyThe identifier used in the shader for the attribute.
valueThe name to bind to this attribute.

◆ addBinding()

UM.View.GL.ShaderProgram.ShaderProgram.addBinding (   self,
  key,
  value 
)

Add a uniform value binding.

Uniform value bindings are used to provide an abstraction between uniforms as set from code and uniforms as used from shaders. Each binding specifies a uniform name as key that should be mapped to a string that can be used to look up the value of the uniform.

Parameters
keyThe name of the uniform to bind.
valueThe string used to look up values for this uniform.

◆ disableAttribute()

UM.View.GL.ShaderProgram.ShaderProgram.disableAttribute (   self,
  name 
)

Disable a vertex attribute so it is no longer used.

Parameters
nameThe name of the attribute to use.

◆ enableAttribute()

UM.View.GL.ShaderProgram.ShaderProgram.enableAttribute (   self,
  name,
  type,
  offset,
  stride = 0 
)

Enable a vertex attribute to be used.

Parameters
nameThe name of the attribute to enable.
typeThe type of the attribute. Should be a python type.
offsetThe offset into a bound buffer where the data for this attribute starts.
strideThe stride of the attribute.
Note
If the shader is not bound, this will bind the shader.

◆ load()

UM.View.GL.ShaderProgram.ShaderProgram.load (   self,
  file_name,
  version = "" 
)

Load a shader program file.

This method loads shaders from a simple text file, using Python's configparser as parser.

Note
When writing shader program files, please note that configparser expects indented lines for multiline values. Since the shaders are provided as a single multiline string, make sure to indent them properly.
Parameters
file_nameThe shader file to load.
versioncan be used for a special version of the shader. it will be appended to the keys [vertex, fragment, geometry] in the shader file
Exceptions

InvalidShaderProgramError} Raised when the file provided does not contain any valid shaders.

◆ removeAttributeBinding()

UM.View.GL.ShaderProgram.ShaderProgram.removeAttributeBinding (   self,
  key 
)

Remove an attribute binding.

Parameters
keyThe name of the attribute binding to remove.

◆ removeBinding()

UM.View.GL.ShaderProgram.ShaderProgram.removeBinding (   self,
  key 
)

Remove a uniform value binding.

Parameters
keyThe uniform to remove.

◆ setFragmentShader()

UM.View.GL.ShaderProgram.ShaderProgram.setFragmentShader (   self,
  shader 
)

Set the fragment shader to use.

Parameters
shaderstring The fragment shader to use.

◆ setTexture()

UM.View.GL.ShaderProgram.ShaderProgram.setTexture (   self,
  texture_unit,
  texture 
)

Set a texture that should be bound to a specified texture unit when this shader is bound.

Parameters
texture_unitint The texture unit to bind the texture to.
textureTexture The texture object to bind to the texture unit.

◆ setUniformValue()

UM.View.GL.ShaderProgram.ShaderProgram.setUniformValue (   self,
  name,
  value,
**  kwargs 
)

Set a named uniform variable.

Unless otherwise specified as argument, the specified value will be cached so that it does not matter whether bind() has already been called. Instead, if the shader is not currently bound, the next call to bind() will update the uniform values.

Parameters
nameThe name of the uniform variable.
valueThe value to set the variable to.
kwargsKeyword arguments. Possible keywords:
  • cache: False when the value should not be cached for later calls to bind().

◆ setVertexShader()

UM.View.GL.ShaderProgram.ShaderProgram.setVertexShader (   self,
  shader 
)

Set the vertex shader to use.

Parameters
shaderstring The vertex shader to use.

◆ updateBindings()

UM.View.GL.ShaderProgram.ShaderProgram.updateBindings (   self,
**  kwargs 
)

Update the values of bindings.

Parameters
kwargsKeyword arguments. Each key should correspond to a binding name, with the value being the value of the uniform.
Note
By default, these values are not cached as they are expected to be continuously updated.

The documentation for this class was generated from the following file: