GPU Types (gpu.types)

class gpu.types.GPUBatch(type, buf, elem=None)

Reusable container for drawable geometry.

Parameters
  • type (str) – One of these primitive types: { POINTS, LINES, TRIS, LINE_STRIP, LINE_LOOP, TRI_STRIP, TRI_FAN, LINES_ADJ, TRIS_ADJ, LINE_STRIP_ADJ }

  • buf (gpu.types.GPUVertBuf) – Vertex buffer containing all or some of the attributes required for drawing.

  • elem (gpu.types.GPUIndexBuf) – An optional index buffer.

draw(program=None)

Run the drawing program with the parameters assigned to the batch.

Parameters

program (gpu.types.GPUShader) – Program that performs the drawing operations. If None is passed, the last program set to this batch will run.

program_set(program)

Assign a shader to this batch that will be used for drawing when not overwritten later. Note: This method has to be called in the draw context that the batch will be drawn in. This function does not need to be called when you always set the shader when calling batch.draw.

Parameters

program (gpu.types.GPUShader) – The program/shader the batch will use in future draw calls.

vertbuf_add(buf)

Add another vertex buffer to the Batch. It is not possible to add more vertices to the batch using this method. Instead it can be used to add more attributes to the existing vertices. A good use case would be when you have a separate vertex buffer for vertex positions and vertex normals. Current a batch can have at most 6 vertex buffers.

Parameters

buf (gpu.types.GPUVertBuf) – The vertex buffer that will be added to the batch.

class gpu.types.GPUIndexBuf(type, seq)

Contains an index buffer.

Parameters
  • type (str) – One of these primitive types: { POINTS, LINES, TRIS, LINE_STRIP_ADJ }

  • seq (1D or 2D sequence) – Indices this index buffer will contain. Whether a 1D or 2D sequence is required depends on the type. Optionally the sequence can support the buffer protocol.

class gpu.types.GPUOffScreen(width, height, samples=0)

This object gives access to off screen buffers.

Parameters
  • width (int) – Horizontal dimension of the buffer.

  • height (int) – Vertical dimension of the buffer.

  • samples (int) – OpenGL samples to use for MSAA or zero to disable.

bind(save=True)

Bind the offscreen object. To make sure that the offscreen gets unbind whether an exception occurs or not, pack it into a with statement.

Parameters

save (bool) – Save the current OpenGL state, so that it can be restored when unbinding.

draw_view3d(scene, view_layer, view3d, region, view_matrix, projection_matrix)

Draw the 3d viewport in the offscreen object.

Parameters
free()

Free the offscreen object. The framebuffer, texture and render objects will no longer be accessible.

unbind(restore=True)

Unbind the offscreen object.

Parameters

restore (bool) – Restore the OpenGL state, can only be used when the state has been saved before.

color_texture

OpenGL bindcode for the color texture.

Type

int

height

Height of the texture.

Type

int

width

Width of the texture.

Type

int

class gpu.types.GPUShader(vertexcode, fragcode, geocode=None, libcode=None, defines=None)

GPUShader combines multiple GLSL shaders into a program used for drawing. It must contain a vertex and fragment shaders, with an optional geometry shader.

The GLSL #version directive is automatically included at the top of shaders, and set to 330. Some preprocessor directives are automatically added according to the Operating System or availability: GPU_ATI, GPU_NVIDIA and GPU_INTEL.

The following extensions are enabled by default if supported by the GPU: GL_ARB_texture_gather and GL_ARB_texture_query_lod.

To debug shaders, use the –debug-gpu-shaders command line option to see full GLSL shader compilation and linking errors.

For drawing user interface elements and gizmos, use fragOutput = blender_srgb_to_framebuffer_space(fragOutput) to transform the output sRGB colors to the framebuffer colorspace. :param vertexcode: Vertex shader code. :type vertexcode: str :param fragcode: Fragment shader code. :type value: str :param geocode: Geometry shader code. :type value: str :param libcode: Code with functions and presets to be shared between shaders. :type value: str :param defines: Preprocessor directives. :type value: str

attr_from_name(name)

Get attribute location by name.

Parameters

name (str) – The name of the attribute variable whose location is to be queried.

Returns

The location of an attribute variable.

Return type

int

bind()

Bind the shader object. Required to be able to change uniforms of this shader.

calc_format()

Build a new format based on the attributes of the shader.

Returns

vertex attribute format for the shader

Return type

GPUVertFormat

uniform_block_from_name(name)

Get uniform block location by name.

Parameters

name (str) – Name of the uniform block variable whose location is to be queried.

Returns

The location of the uniform block variable.

Return type

int

uniform_bool(name, seq)

Specify the value of a uniform variable for the current program object.

Parameters
  • name (str) – Name of the uniform variable whose value is to be changed.

  • seq (sequence of bools) – Value that will be used to update the specified uniform variable.

uniform_float(name, value)

Specify the value of a uniform variable for the current program object.

Parameters
  • name (str) – Name of the uniform variable whose value is to be changed.

  • value (single number or sequence of numbers) – Value that will be used to update the specified uniform variable.

uniform_from_name(name)

Get uniform location by name.

Parameters

name (str) – Name of the uniform variable whose location is to be queried.

Returns

Location of the uniform variable.

Return type

int

uniform_int(name, seq)

Specify the value of a uniform variable for the current program object.

Parameters
  • name (str) – name of the uniform variable whose value is to be changed.

  • seq (sequence of numbers) – Value that will be used to update the specified uniform variable.

uniform_vector_float(location, buffer, length, count)

Set the buffer to fill the uniform.

Parameters
  • location (int) – Location of the uniform variable to be modified.

  • buffer (sequence of floats) – The data that should be set. Can support the buffer protocol.

  • length (int) –

    Size of the uniform data type:

    • 1: float

    • 2: vec2 or float[2]

    • 3: vec3 or float[3]

    • 4: vec4 or float[4]

    • 9: mat3

    • 16: mat4

  • count (int) – Specifies the number of elements, vector or matrices that are to be modified.

uniform_vector_int(location, buffer, length, count)

See GPUShader.uniform_vector_float(…) description.

program

The name of the program object for use by the OpenGL API (read-only).

Type

int

class gpu.types.GPUVertBuf(len, format)

Contains a VBO.

Parameters
  • len – Amount of vertices that will fit into this buffer.

  • format – Vertex format.

attr_fill(id, data)

Insert data into the buffer for a single attribute.

Parameters
  • id (int or str) – Either the name or the id of the attribute.

  • data (sequence of values or tuples) – Sequence of data that should be stored in the buffer

class gpu.types.GPUVertFormat

This object contains information about the structure of a vertex buffer.

attr_add(id, comp_type, len, fetch_mode)

Add a new attribute to the format.

Parameters
  • id (str) – Name the attribute. Often position, normal, …

  • comp_type (str) – The data type that will be used store the value in memory. Possible values are I8, U8, I16, U16, I32, U32, F32 and I10.

  • len (int) – How many individual values the attribute consists of (e.g. 2 for uv coordinates).

  • fetch_mode (str) – How values from memory will be converted when used in the shader. This is mainly useful for memory optimizations when you want to store values with reduced precision. E.g. you can store a float in only 1 byte but it will be converted to a normal 4 byte float when used. Possible values are FLOAT, INT, INT_TO_FLOAT_UNIT and INT_TO_FLOAT.