_glutils

This package provides utility functions to handle OpenGL resources.

The gl module provides a wrapper to OpenGL based on PyOpenGL.

_glutils.gl

This module loads PyOpenGL and provides a namespace for OpenGL.

silx.gui._glutils.gl.testGL()[source]

Test if required OpenGL version and extensions are available.

This MUST be run with an active OpenGL context.

silx.gui._glutils.gl.enabled(*args, **kwds)[source]

Context manager enabling an OpenGL capacity.

This is not checking the current state of the capacity.

Parameters:
  • capacity – The OpenGL capacity enum to enable/disable
  • enable (bool) – True (default) to enable during context, False to disable
silx.gui._glutils.gl.disabled(capacity, disable=True)[source]

Context manager disabling an OpenGL capacity.

This is not checking the current state of the capacity.

Parameters:
  • capacity – The OpenGL capacity enum to disable/enable
  • disable (bool) – True (default) to disable during context, False to enable
silx.gui._glutils.gl.glGetActiveAttrib(program, index)[source]

Wrap PyOpenGL glGetActiveAttrib

Utility functions

For OpenGL context management:

silx.gui._glutils.getGLContext()[source]

Returns platform dependent object of current OpenGL context.

This is useful to associate OpenGL resources with the context they are created in.

Returns:Platform specific OpenGL context
Return type:None by default or a platform dependent object
silx.gui._glutils.setGLContextGetter(getter=<function _defaultGLContextGetter>)[source]

Set a platform dependent function to retrieve the current OpenGL context

Parameters:getter (Function with no args returning the current OpenGL context) – Platform dependent GL context getter

For type checking and conversion:

silx.gui._glutils.sizeofGLType(type_)[source]

Returns the size in bytes of an element of type type_

silx.gui._glutils.isSupportedGLType(type_)[source]

Test if a numpy type or dtype can be converted to a GL type.

silx.gui._glutils.numpyToGLType(type_)[source]

Returns the GL type corresponding the provided numpy type or dtype.

Program

class silx.gui._glutils.Program(vertexShader, fragmentShader, attrib0='position')[source]

Wrap OpenGL shader program.

The program is compiled lazily (i.e., at first program use()). When the program is compiled, it stores attributes and uniforms locations. So, attributes and uniforms must be used after use().

This object supports multiple OpenGL contexts.

Parameters:
  • vertexShader (str) – The source of the vertex shader.
  • fragmentShader (str) – The source of the fragment shader.
  • attrib0 (str) – Attribute’s name to bind to position 0 (default: ‘position’). On certain platform, this attribute MUST be active and with an array attached to it in order for the rendering to occur....
attributes

Vertex attributes names and locations as a dict of {str: int}.

WARNING: Read-only usage. To use only with a valid OpenGL context and after use() has been called for this context.

uniforms

Program uniforms names and locations as a dict of {str: int}.

WARNING: Read-only usage. To use only with a valid OpenGL context and after use() has been called for this context.

program

OpenGL id of the program.

WARNING: To use only with a valid OpenGL context and after use() has been called for this context.

use()[source]

Make use of the program, compiling it if necessary

setUniformMatrix(name, value, transpose=True, safe=False)[source]

Wrap glUniformMatrix[2|3|4]fv

Parameters:
  • name (str) – The name of the uniform.
  • value (numpy.ndarray with 2 or 3 dimensions of float32) – The 2D matrix (or the array of matrices, 3D). Matrices are 2x2, 3x3 or 4x4.
  • transpose (bool) – Whether to transpose (True, default) or not.
  • safe (bool) – False: raise an error if no uniform with this name; True: silently ignores it.
Raises:

KeyError – if no uniform corresponds to name.

Texture

class silx.gui._glutils.Texture(internalFormat, data=None, format_=None, shape=None, texUnit=0, minFilter=None, magFilter=None, wrap=None)[source]

Base class to wrap OpenGL 2D and 3D texture

Parameters:
  • internalFormat – OpenGL texture internal format
  • data (numpy.ndarray or None) – The data to copy to the texture or None for an empty texture
  • format – Input data format if different from internalFormat
  • shape (2 or 3-tuple of int (height, width) or (depth, height, width)) – If data is None, shape of the texture
  • texUnit (int) – The texture unit to use
  • minFilter – OpenGL texture minimization filter (default: GL_NEAREST)
  • magFilter – OpenGL texture magnification filter (default: GL_LINEAR)
  • wrap (OpenGL wrap mode or 2 or 3-tuple of wrap mode) – Texture wrap mode for dimensions: (t, s) or (r, t, s) If a single value is provided, it used for all dimensions.
target

OpenGL target type of this texture

ndim

The number of dimensions: 2 or 3

internalFormat

Texture internal format

shape

Shape of the texture: (height, width) or (depth, height, width)

name

OpenGL texture name

minFilter

Minifying function parameter (GL_TEXTURE_MIN_FILTER)

magFilter

Magnification function parameter (GL_TEXTURE_MAG_FILTER)

discard()[source]

Delete associated OpenGL texture

bind(texUnit=None)[source]

Bind the texture to a texture unit.

Parameters:texUnit (int) – The texture unit to use
update(format_, data, offset=(0, 0, 0), texUnit=None)[source]

Update the content of the texture.

Texture is not resized, so data must fit into texture with the given offset.

Parameters:
  • format – The OpenGL format of the data
  • data – The data to use to update the texture
  • offset (2 or 3-tuple of int) – The offset in the texture where to copy the data
  • texUnit (int) – The texture unit to use (default: the one provided at init)

FramebufferTexture

class silx.gui._glutils.FramebufferTexture(internalFormat, shape, stencilFormat=GL_DEPTH24_STENCIL8, depthFormat=GL_DEPTH24_STENCIL8, **kwargs)[source]

Framebuffer with a texture.

Aimed at off-screen rendering to texture.

Parameters:
  • internalFormat – OpenGL texture internal format
  • shape (2-tuple of int) – Shape (height, width) of the framebuffer and texture
  • stencilFormat – Stencil renderbuffer format
  • depthFormat – Depth renderbuffer format
  • kwargs – Extra arguments for Texture constructor
shape

Shape of the framebuffer (height, width)

texture

The texture this framebuffer is rendering to.

The life-cycle of the texture is managed by this object

name

OpenGL name of the framebuffer

bind()[source]

Bind this framebuffer for rendering

discard()[source]

Delete associated OpenGL resources including texture

Vertex Buffer

class silx.gui._glutils.VertexBuffer(data=None, size=None, usage=None, target=None)[source]

Object handling an OpenGL vertex buffer object

Parameters:
  • data (numpy.ndarray or None) – Data used to fill the vertex buffer
  • size (int) – Size in bytes of the buffer or None for data size
  • usage – OpenGL vertex buffer expected usage pattern: GL_STREAM_DRAW, GL_STATIC_DRAW (default) or GL_DYNAMIC_DRAW
  • target – Target buffer: GL_ARRAY_BUFFER (default) or GL_ELEMENT_ARRAY_BUFFER
target

The target buffer of the vertex buffer

usage

The expected usage of the vertex buffer

name

OpenGL Vertex Buffer object name (int)

size

Size in bytes of the Vertex Buffer Object (int)

bind()[source]

Bind the vertex buffer

update(data, offset=0, size=None)[source]

Update vertex buffer content.

Parameters:
  • data (numpy.ndarray) – The data to put in the vertex buffer
  • offset (int) – Offset in bytes in the buffer where to put the data
  • size (int) – If provided, size of data to copy
discard()[source]

Delete the vertex buffer

class silx.gui._glutils.VertexBufferAttrib(vbo, type_, size, dimension=1, offset=0, stride=0, normalization=False)[source]

Describes data stored in a vertex buffer

Convenient class to store info for glVertexAttribPointer calls

Parameters:
  • vbo (VertexBuffer) – The vertex buffer storing the data
  • type (int) – The OpenGL type of the data
  • size (int) – The number of data elements stored in the VBO
  • dimension (int) – The number of type_ element(s) in [1, 4]
  • offset (int) – Start offset of data in the vertex buffer
  • stride (int) – Data stride in the vertex buffer
itemsize

Size in bytes of a vertex buffer element (int)

itemSize

Size in bytes of a vertex buffer element (int)

setVertexAttrib(attribute)[source]

Call glVertexAttribPointer with objects information

silx.gui._glutils.vertexBuffer(arrays, prefix=None, suffix=None, usage=None)[source]

Create a single vertex buffer from multiple 1D or 2D numpy arrays.

It is possible to reserve memory before and after each array in the VBO

Parameters:
  • arrays (Iterable of numpy.ndarray) – Arrays of data to store
  • prefix (Iterable of int or None) – If given, number of elements to reserve before each array
  • suffix (Iterable of int or None) – If given, number of elements to reserve after each array
  • usage (int) – vertex buffer expected usage or None for default
Returns:

List of VertexBufferAttrib objects sharing the same vertex buffer

font

Text rasterisation feature leveraging Qt font and text layout support.

silx.gui._glutils.font.getDefaultFontFamily()[source]

Returns the default font family of the application

silx.gui._glutils.font.ULTRA_LIGHT = 0

Lightest characters: Minimum font weight

silx.gui._glutils.font.LIGHT = 25

Light characters

silx.gui._glutils.font.NORMAL = 50

Normal characters

silx.gui._glutils.font.SEMI_BOLD = 63

Between normal and bold characters

silx.gui._glutils.font.BOLD = 74

Thicker characters

silx.gui._glutils.font.BLACK = 87

Really thick characters

silx.gui._glutils.font.ULTRA_BLACK = 99

Thickest characters: Maximum font weight

silx.gui._glutils.font.rasterText(text, font, size=-1, weight=-1, italic=False, devicePixelRatio=1.0)[source]

Raster text using Qt.

It supports multiple lines.

Parameters:
  • text (str) – The text to raster
  • font (str or QFont) – Font name or QFont to use
  • size (int) – Font size in points Used only if font is given as name.
  • weight (int) – Font weight in [0, 99], see QFont.Weight. Used only if font is given as name.
  • italic (bool) – True for italic font (default: False). Used only if font is given as name.
  • devicePixelRatio (float) – The current ratio between device and device-independent pixel (default: 1.0)
Returns:

Corresponding image in gray scale and baseline offset from top

Return type:

(HxW numpy.ndarray of uint8, int)