Light | Dark

# docs.GL

OpenGL 4
OpenGL 3
OpenGL 2
OpenGL ES 3
OpenGL ES 2

# glBufferSubData

## Name

glBufferSubData — updates a subset of a buffer object's data store

## C Specification

`void glBufferSubData(` | GLenum | target,
| GLintptr | offset,
| GLsizeiptr | size,
| const GLvoid * | data`)`;

## Parameters

`target`

  Specifies the target buffer object. The symbolic constant must be `GL_ARRAY_BUFFER`, `GL_COPY_READ_BUFFER`, `GL_COPY_WRITE_BUFFER`, `GL_ELEMENT_ARRAY_BUFFER`, `GL_PIXEL_PACK_BUFFER`, `GL_PIXEL_UNPACK_BUFFER`, `GL_TEXTURE_BUFFER`, `GL_TRANSFORM_FEEDBACK_BUFFER`, or `GL_UNIFORM_BUFFER`.

`offset`

  Specifies the offset into the buffer object's data store where data replacement will begin, measured in bytes.

`size`

  Specifies the size in bytes of the data store region being replaced.

`data`

  Specifies a pointer to the new data that will be copied into the data store.

## Description

`glBufferSubData` redefines some or all of the data store for the buffer object currently bound to `target`. Data starting at byte offset `offset` and extending for `size` bytes is copied to the data store from the memory pointed to by `data`. An error is thrown if `offset` and `size` together define a range beyond the bounds of the buffer object's data store.

## Notes

When replacing the entire data store, consider using `glBufferSubData` rather than completely recreating the data store with `glBufferData`. This avoids the cost of reallocating the data store.

Consider using multiple buffer objects to avoid stalling the rendering pipeline during data store updates. If any rendering in the pipeline makes reference to data in the buffer object being updated by `glBufferSubData`, especially from the specific region being updated, that rendering must drain from the pipeline before the data store can be updated.

Clients must align data elements consistent with the requirements of the client platform, with an additional base-level requirement that an offset within a buffer to a datum comprising N bytes be a multiple of N.

## Errors

`GL_INVALID_ENUM` is generated if `target` is not one of the accepted buffer targets.

`GL_INVALID_VALUE` is generated if `offset` or `size` is negative, or if together they define a region of memory that extends beyond the buffer object's allocated data store.

`GL_INVALID_OPERATION` is generated if the reserved buffer object name 0 is bound to `target`.

`GL_INVALID_OPERATION` is generated if the buffer object being updated is mapped.

## Associated Gets

glGetBufferSubData

## Examples

Load a vertex buffer into OpenGL for later rendering.

    // data_size_in_bytes is the size in bytes of your vertex data.
    // data_vertices is your actual vertex data, probably a huge array of floats

    GLuint vertex_buffer; // Save this for later rendering
    glGenBuffers(1, &vertex_buffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
    glBufferData(GL_ARRAY_BUFFER, data_size_in_bytes, 0, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, data_size_in_bytes, data_vertices);

    GLint size = 0;
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
    if(data_size_in_bytes != size)
    {
        glDeleteBuffers(1, &vertex_buffer);
        // Log the error
        return;
    }

    // Success

Load an index buffer into OpenGL for later rendering.

    // data_size_in_bytes is the size in bytes of your vertex data.
    // data_indices is an array of integer offsets into your vertex data.

    GLuint index_buffer; // Save this for later rendering
    glGenBuffers(1, &index_buffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, data_size_in_bytes, 0, GL_STATIC_DRAW);
    glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, data_size_in_bytes, data_indices);

    GLint size = 0;
    glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
    if(data_size_in_bytes != size)
    {
        glDeleteBuffers(1, &index_buffer);
        // Log the error
        return;
    }

    // Success

## Tutorials

Songho - OpenGL Vertex Buffer Object (VBO)
opengl-tutorial.org - Particles / Instancing

## See Also

glBindBuffer, glBufferData, glMapBuffer, glUnmapBuffer

## Copyright

Copyright © 2005 Addison-Wesley. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. https://opencontent.org/openpub/.

Think you can improve this page? Edit this page on GitHub.