Variables

Variables are entry points in the shader that allow to upload CPU data to the GPU. For OpenGL ES 2.0, there are mainly two types: uniforms and attributes. The correspondance betwenn GPU and CPU data types is given in the table below.

GLSL Type GLSL/GL Type # GL elementary type Numpy type
float gl.GL_FLOAT 1 gl.GL_FLOAT np.float32
vec2 gl.GL_FLOAT_VEC2 2 gl.GL_FLOAT np.float32
vec3 gl.GL_FLOAT_VEC3 3 gl.GL_FLOAT np.float32
vec4 gl.GL_FLOAT_VEC4 4 gl.GL_FLOAT np.float32
int gl.GL_INT 1 gl.GL_INT np.int32
ivec2 gl.GL_INT_VEC2 2 gl.GL_INT np.int32
ivec3 gl.GL_INT_VEC3 3 gl.GL_INT np.int32
ivec4 gl.GL_INT_VEC4 4 gl.GL_INT np.int32
bool gl.GL_BOOL 1 gl.GL_BOOL np.bool
bvec2 gl.GL_BOOL_VEC2 2 gl.GL_BOOL np.bool
bvec3 gl.GL_BOOL_VEC3 3 gl.GL_BOOL np.bool
bvec4 gl.GL_BOOL_VEC4 4 gl.GL_BOOL np.bool
mat2 gl.GL_FLOAT_MAT2 4 gl.GL_FLOAT np.float32
mat3 gl.GL_FLOAT_MAT3 9 gl.GL_FLOAT np.float32
mat4 gl.GL_FLOAT_MAT4 16 gl.GL_FLOAT np.float32
sampler1D gl.GL_SAMPLER_1D 1 gl.GL_UNSIGNED_INT np.uint32
sampler2D gl.GL_SAMPLER_2D 1 gl.GL_UNSIGNED_INT np.uint32
samplerCube gl.GL_SAMPLER_CUBE 1 gl.GL_UNSIGNED_INT np.uint32

Note

Most of the time, you don’t need to directly manipulate such variables since they are created automatically when shader code is parsed.

Example usage

vertex = '''
    attribute vec3 position;
    void main (void)
    {
        gl_Position = vec4(position, 1.0);
    } '''
fragment = '''
    uniform vec4 color;
    void main(void)
    {
        gl_FragColor = color;
    } '''
program = gloo.Program(vertex, fragment, count=4)
# program["position"] type is Attribute
# program["color"] type is Uniform

Content

Variable

class glumpy.gloo.Variable(program, name, gtype)

Bases: glumpy.gloo.globject.GLObject

A variable is an interface between a program and data

active

Whether this variable is active in the program

data

CPU data

dtype

Equivalent dtype of the variable

gtype

Type of the underlying variable (as a GL constant)

name

Variable name

program

Program this variable belongs to

Uniform

class glumpy.gloo.Uniform(program, name, gtype)

Bases: glumpy.gloo.variable.Variable

A Uniform represents a program uniform variable.

set_data(data)

Assign new data to the variable (deferred operation)

Attribute

class glumpy.gloo.Attribute(program, name, gtype)

Bases: glumpy.gloo.variable.Variable

An Attribute represents a program attribute variable

set_data(data)

Assign new data to the variable (deferred operation)

size

Size of the underlying vertex buffer

Uniforms

class glumpy.gloo.Uniforms(size, dtype)

Bases: glumpy.gloo.texture.Texture2D

Uniforms data texture holder.

This class is used in conjunction with collections in order to store a number of uniforms in a texture such that each vertices can retrieve a specific group of uniforms. The data type can be structured but must be reduceable to n x np.float32. Note that you don’t need to manipulate directly this function, it is done automagically in collections.

Note

The code needed to retrieve a specific item is given from the code function and is generated specifically for the actual data type.

Parameters:
  • size (int) – Number of items to be stored in the texture
  • dtype (numpy.dtype) – Item data type (must be reduceable to n x np.float32)
code(prefix='u_')

Generate the GLSL code needed to retrieve fake uniform values from a texture. The generated uniform names can be prefixed with the given prefix.