picogl.shaders.manager

Example Usage

Illustrative only (needs an OpenGL context, loaded shader sources, my_mvp_matrix, and set_common_uniforms in scope):

shader_manager = ShaderManager()

for shader_type_value in ShaderType:
    shader_manager.load_shader_source_string(shader_type_value)

if shader_manager.use_shader_type(ShaderType.ATOMS):
    shader_manager.current_shader_program = shader_manager.get(ShaderType.ATOMS)
    set_common_uniforms(
        shader_manager.current_shader_program,
        mvp_matrix=my_mvp_matrix,
        point_size=15.0,
        highlight=True,
        highlight_color=(1.0, 1.0, 0.0),
    )

File naming convention:

Ensure GLSL files follow the naming pattern:

atoms_vert.glsl atoms_frag.glsl bonds_vert.glsl bonds_frag.glsl

Attributes

SILENT_SHADER

Classes

ShaderManager

Manages shader programs and their interaction with the rendering pipeline.

Functions

_progress_iter(pairs, *, desc, total) → Iterable[Tuple[int, picogl.shaders.type.ShaderType]]

Optional tqdm in real terminals only; GUI apps use plain iteration (no monitor thread).

Module Contents

picogl.shaders.manager.SILENT_SHADER = True[source]
picogl.shaders.manager._progress_iter(pairs: Iterable[Tuple[int, picogl.shaders.type.ShaderType]], *, desc: str, total: int) → Iterable[Tuple[int, picogl.shaders.type.ShaderType]][source]

Optional tqdm in real terminals only; GUI apps use plain iteration (no monitor thread).

class picogl.shaders.manager.ShaderManager[source]

Manages shader programs and their interaction with the rendering pipeline.

The ShaderManager class is responsible for managing multiple shader programs, including their initialization, binding, and usage. It keeps track of the current shader being used, ensures shaders are properly loaded, and provides utilities to set shader-specific uniform values. Fallback mechanisms are implemented to handle cases where a desired shader cannot be loaded. The manager also supports default shaders and updates to the model-view-projection (MVP) matrix uniforms to match rendering requirements.

shaders[source]

A dictionary of loaded shader programs mapped by ShaderType.

fallback_shader[source]

An optional fallback shader loaded when a specific shader type cannot be initialized.

default_shader_type[source]

The default shader type used when no specific type is requested.

current_shader_type[source]

The currently active shader type.

current_shader[source]

The currently active shader program.

current_shader_program[source]

The OpenGL Program ID of the current shader program.

shader_directory[source]

The directory path containing shader source files.

fallback_shader_directory[source]

The directory path for fallback shader source files.

shaders: Dict[picogl.shaders.type.ShaderType, picogl.backend.modern.core.shader.program.ShaderProgram][source]
fallback_shader: picogl.backend.modern.core.shader.program.ShaderProgram | None = None[source]
default_shader_type: picogl.shaders.type.ShaderType[source]
current_shader_type: picogl.shaders.type.ShaderType[source]
current_shader: picogl.backend.modern.core.shader.program.ShaderProgram | None = None[source]
current_shader_program: int | None = None[source]
_initialized: bool = False[source]
_initializing: bool = False[source]
shader_directory: str = ''[source]
fallback_shader_directory: str = ''[source]
use_shader_program(shader_program: picogl.backend.modern.core.shader.program.ShaderProgram) → None[source]

use_shader_program

Parameters:

shader_program – PicoGLShader

Returns:

None

Bind the given shader shader_program and update current_shader/shader_program ID

bind(shader_program: picogl.backend.modern.core.shader.program.ShaderProgram) → None[source]

bind

Parameters:

shader_program – ShaderProgram

Returns:

None

unbind() → None[source]

unbind

Returns:

None

get_shader_type(shader_type: picogl.shaders.type.ShaderType) → picogl.backend.modern.core.shader.program.ShaderProgram | picogl.backend.modern.core.shader.program.ShaderProgram | None[source]

Return the shader shader_program for the given ShaderType, loading if necessary.

use_shader_type(shader_type: picogl.shaders.type.ShaderType, mvp_matrix: numpy.ndarray | pyglm.glm.mat4 = None, zoom_scale: int | float = None) → bool[source]

Load (if needed) and bind the shader of the given type.

Returns True when the program is bound in the current gl context.

use(shader_type: picogl.shaders.type.ShaderType) → picogl.backend.modern.core.shader.program.ShaderProgram | None[source]

Bind shader_type without uploading MVP or other draw uniforms.

Returns the bound program, or None if bind failed.

update_mvp_uniform(mvp_matrix: numpy.ndarray | pyglm.glm.mat4) → None[source]

update_mvp_uniform

Parameters:

mvp_matrix – np.ndarray | glm.mat4:

Returns:

None

set_uniform_value(uniform_name: str, uniform_value: float | int | pyglm.glm.vec2 | pyglm.glm.vec3 | pyglm.glm.vec4 | pyglm.glm.mat4 | numpy.ndarray) → None[source]

set_uniform_value

Parameters:
  • uniform_name – str

  • uniform_value – Union[float, int, glm.vec2, glm.vec3, glm.vec4, glm.mat4, np.ndarray]

Returns:

None

use_default_shader(mvp_matrix: numpy.ndarray | pyglm.glm.mat4 = None) → None[source]

use_default_shader

Parameters:

mvp_matrix – np.ndarray | glm.mat4

Returns:

Bind the default shader type.

initialize_shaders(shader_dir: str = None, *, on_shader_loaded: Callable[[int, int, picogl.shaders.type.ShaderType], None] | None = None)[source]

Initialize src and mark gl state as ready.

load_shader(shader_type: str, shader_number: int) → None[source]

load_shader

Parameters:
  • shader_number – Shader Number

  • shader_type – ShaderType

Returns:

None

_fallback_shader_sources() → tuple[str, str][source]

Resolve fallback GLSL from the active shader root, then PicoGL defaults.

_ensure_fallback()[source]

_ensure_fallback

Returns:

None

get(shader_type: picogl.shaders.type.ShaderType) → picogl.backend.modern.core.shader.program.ShaderProgram | picogl.backend.modern.core.shader.program.ShaderProgram | None[source]
release_shaders()[source]

release_shaders

Returns:

None