psychopy.tools.gltools.mapBuffer

psychopy.tools.gltools.mapBuffer(vbo, start=0, length=None, read=True, write=True, noSync=False)[source]

Map a vertex buffer object to client memory. This allows you to modify its contents.

If planning to update VBO vertex data, make sure the VBO usage types are GL_DYNAMIC_* or GL_STREAM_* or else serious performance issues may arise.

Warning

Modifying buffer data must be done carefully, or else system stability may be affected. Do not use the returned view ndarray outside of successive mapBuffer() and unmapBuffer() calls. Do not use the mapped buffer for rendering until after unmapBuffer() is called.

Parameters:
  • vbo (VertexBufferInfo) – Vertex buffer to map to client memory.

  • start (int) – Initial index of the sub-range of the buffer to modify.

  • length (int or None) – Number of elements of the sub-array to map from offset. If None, all elements to from offset to the end of the array are mapped.

  • read (bool, optional) – Allow data to be read from the buffer (sets GL_MAP_READ_BIT). This is ignored if noSync is True.

  • write (bool, optional) – Allow data to be written to the buffer (sets GL_MAP_WRITE_BIT).

  • noSync (bool, optional) – If True, GL will not wait until the buffer is free (i.e. not being processed by the GPU) to map it (sets GL_MAP_UNSYNCHRONIZED_BIT). The contents of the previous storage buffer are discarded and the driver returns a new one. This prevents the CPU from stalling until the buffer is available.

Returns:

View of the data. The type of the returned array is one which best matches the data type of the buffer.

Return type:

ndarray

Examples

Map a buffer and edit it:

arr = mapBuffer(vbo)
arr[:, :] += 2.0  # add 2 to all values
unmapBuffer(vbo)  # call when done
# Don't ever modify `arr` after calling `unmapBuffer`. Delete it if
# necessary to prevent it form being used.
del arr

Modify a sub-range of data by specifying start and length, indices correspond to values, not byte offsets:

arr = mapBuffer(vbo, start=12, end=24)
arr[:, :] *= 10.0
unmapBuffer(vbo)

Back to top