GLBINDBUFFER(3G)

πŸ’… glBindBuffer β€” bind a named buffer object

okay bestie, welcome to GLBINDBUFFER(3G), the manual page where we learn how to tell OpenGL "hey, this buffer? yeah, it's the main character now." πŸŽ€


🌸 NAME

glBindBuffer β€” bind a named buffer object ✨


πŸ¦‹ C SPECIFICATION

void glBindBuffer(GLenum target, GLuint buffer);

that's it, that's the whole signature, no cap πŸ’…


πŸ’– PARAMETERS

  • target β€” specifies the target to which the buffer object is bound, and it must be one of the buffer binding targets in this list (no improvising, bestie, the spec is watching πŸ‘€):
    • GL_ARRAY_BUFFER β€” vertex attributes
    • GL_ATOMIC_COUNTER_BUFFER β€” atomic counter storage
    • GL_COPY_READ_BUFFER β€” buffer copy source
    • GL_COPY_WRITE_BUFFER β€” buffer copy destination
    • GL_DISPATCH_INDIRECT_BUFFER β€” indirect compute dispatch commands
    • GL_DRAW_INDIRECT_BUFFER β€” indirect command arguments
    • GL_ELEMENT_ARRAY_BUFFER β€” vertex array indices
    • GL_PIXEL_PACK_BUFFER β€” pixel read target
    • GL_PIXEL_UNPACK_BUFFER β€” texture data source
    • GL_QUERY_BUFFER β€” query result buffer
    • GL_SHADER_STORAGE_BUFFER β€” read-write storage for shaders
    • GL_TEXTURE_BUFFER β€” texture data buffer
    • GL_TRANSFORM_FEEDBACK_BUFFER β€” transform feedback buffer
    • GL_UNIFORM_BUFFER β€” uniform block storage
  • buffer β€” specifies the name of a buffer object 🫢

✨ DESCRIPTION

so glBindBuffer binds a buffer object to the specified buffer binding point. literally that's the vibe. when you call glBindBuffer with target set to one of the accepted symbolic constants and buffer set to the name of a buffer object, it binds that buffer object name to the target. and if no buffer object with name buffer exists yet? it just makes one with that name. iconic behaviour, honestly πŸ‘‘

when a buffer object is bound to a target, the previous binding for that target is automatically broken. no messy breakups, it's handled. πŸ’€

buffer object names are unsigned integers. the value zero is reserved, but β€” and this is the plot twist β€” there is no default buffer object for each buffer object target. instead, buffer set to zero effectively unbinds any buffer object previously bound, and restores client memory usage for that buffer object target (if supported for that target). so zero is giving "we're so over" energy.

buffer object names and the corresponding buffer object contents are local to the shared object space of the current GL rendering context πŸŒ€ β€” two rendering contexts share buffer object names only if they explicitly enable sharing between contexts through the appropriate GL windows interfaces functions. iykyk.

glGenBuffers() must be used to generate a set of unused buffer object names. like, that's the assignment, do it. πŸ“

the state of a buffer object immediately after it is first bound is an unmapped zero-sized memory buffer with GL_READ_WRITE access and GL_STATIC_DRAW usage. fresh out of the wrapper, tiny, and full of potential πŸ₯Ί

while a non-zero buffer object name is bound, GL operations on the target to which it is bound affect the bound buffer object, and queries of the target to which it is bound return state from the bound buffer object. while buffer object name zero is bound, as in the initial state, attempts to modify or query state on the target to which it is bound generates an GL_INVALID_OPERATION error. so yeah, zero is not your friend for that πŸ’€


πŸŽ€ the target-by-target lore (yes, there's lore)

GL_ARRAY_BUFFER β€” when a non-zero buffer object is bound to the GL_ARRAY_BUFFER target, the vertex array pointer parameter is interpreted as an offset within the buffer object measured in basic machine units.

GL_DRAW_INDIRECT_BUFFER β€” when a non-zero buffer object is bound to the GL_DRAW_INDIRECT_BUFFER target, parameters for draws issued through glDrawArraysIndirect() and glDrawElementsIndirect() are sourced from the specified offset in that buffer object's data store.

GL_DISPATCH_INDIRECT_BUFFER β€” when a non-zero buffer object is bound to the GL_DISPATCH_INDIRECT_BUFFER target, the parameters for compute dispatches issued through glDispatchComputeIndirect() are sourced from the specified offset in that buffer object's data store.

GL_ELEMENT_ARRAY_BUFFER β€” while a non-zero buffer object is bound to the GL_ELEMENT_ARRAY_BUFFER target, the indices parameter of glDrawElements(), glDrawElementsInstanced(), glDrawElementsBaseVertex(), glDrawRangeElements(), glDrawRangeElementsBaseVertex(), glMultiDrawElements(), or glMultiDrawElementsBaseVertex() is interpreted as an offset within the buffer object measured in basic machine units. that's a lot of functions, bestie, hydrate πŸ’§

GL_PIXEL_PACK_BUFFER β€” while a non-zero buffer object is bound to the GL_PIXEL_PACK_BUFFER target, the following commands are affected: glGetCompressedTexImage(), glGetTexImage(), and glReadPixels(). the pointer parameter is interpreted as an offset within the buffer object measured in basic machine units.

GL_PIXEL_UNPACK_BUFFER β€” while a non-zero buffer object is bound to the GL_PIXEL_UNPACK_BUFFER target, the following commands are affected: glCompressedTexImage1D(), glCompressedTexImage2D(), glCompressedTexImage3D(), glCompressedTexSubImage1D(), glCompressedTexSubImage2D(), glCompressedTexSubImage3D(), glTexImage1D(), glTexImage2D(), glTexImage3D(), glTexSubImage1D(), glTexSubImage2D(), and glTexSubImage3D(). the pointer parameter is interpreted as an offset within the buffer object measured in basic machine units.

GL_COPY_READ_BUFFER and GL_COPY_WRITE_BUFFER β€” these two targets are provided to allow glCopyBufferSubData() to be used without disturbing the state of other bindings πŸ¦‹. however, glCopyBufferSubData() may be used with any pair of buffer binding points. flexible queen behaviour.

GL_TRANSFORM_FEEDBACK_BUFFER β€” this buffer binding point may be passed to glBindBuffer, but it will not directly affect transform feedback state. instead, the indexed GL_TRANSFORM_FEEDBACK_BUFFER bindings must be used through a call to glBindBufferBase() or glBindBufferRange(). this will affect the generic GL_TRANSFORM_FEEDBACK_BUFFER binding. so close, yet so far πŸ™ˆ

likewise, the GL_UNIFORM_BUFFER, GL_ATOMIC_COUNTER_BUFFER and GL_SHADER_STORAGE_BUFFER buffer binding points may be used, but do not directly affect uniform buffer, atomic counter buffer or shader storage buffer state, respectively. glBindBufferBase() or glBindBufferRange() must be used to bind a buffer to an indexed uniform buffer, atomic counter buffer or shader storage buffer binding point. yes, you need the other function, sorry bestie 😭

GL_QUERY_BUFFER β€” this binding point is used to specify a buffer object that is to receive the results of query objects through calls to the glGetQueryObject() family of commands. very "results are in, and they're going in a buffer" πŸ’…


🌈 how long does a binding even last

a buffer object binding created with glBindBuffer remains active until a different buffer object name is bound to the same target, or until the bound buffer object is deleted with glDeleteBuffers(). loyalty periodt πŸ”₯

once created, a named buffer object may be re-bound to any target as often as needed. glow-up, rebrand, whatever. however β€” and this is the fine print β€” the GL implementation may make choices about how to optimize the storage of a buffer object based on its initial binding target. so first impressions lowkey matter πŸ‘€


🧚 NOTES

version check time, bestie β€” not everything is available everywhere:

  • the GL_COPY_READ_BUFFER, GL_UNIFORM_BUFFER and GL_TEXTURE_BUFFER targets are available only if the GL version is 3.1 or greater.
  • the GL_ATOMIC_COUNTER_BUFFER target is available only if the GL version is 4.2 or greater.
  • the GL_DISPATCH_INDIRECT_BUFFER and GL_SHADER_STORAGE_BUFFER targets are available only if the GL version is 4.3 or greater.
  • the GL_QUERY_BUFFER target is available only if the GL version is 4.4 or greater.

πŸ’€ ERRORS

  • GL_INVALID_ENUM is generated if target is not one of the allowable values.
  • GL_INVALID_VALUE is generated if buffer is not a name previously returned from a call to glGenBuffers().
⚠️ ok so real talk, those two errors are not a vibe. pass a bad target or a buffer name you never got from glGenBuffers() and GL will call you out. no cap.

still with me? good, because there's more coming πŸ’–

πŸ’… ASSOCIATED GETS πŸ’…

okay so if you ever wanna peek at what's currently bound, these are the glGet() calls that'll tell you πŸ‘€ β€” just pass the argument and it hands you back the current binding for that target, no cap:

  • glGet() with argument GL_ARRAY_BUFFER_BINDING
  • glGet() with argument GL_ATOMIC_COUNTER_BUFFER_BINDING
  • glGet() with argument GL_COPY_READ_BUFFER_BINDING
  • glGet() with argument GL_COPY_WRITE_BUFFER_BINDING
  • glGet() with argument GL_DRAW_INDIRECT_BUFFER_BINDING
  • glGet() with argument GL_DISPATCH_INDIRECT_BUFFER_BINDING
  • glGet() with argument GL_ELEMENT_ARRAY_BUFFER_BINDING
  • glGet() with argument GL_PIXEL_PACK_BUFFER_BINDING
  • glGet() with argument GL_PIXEL_UNPACK_BUFFER_BINDING
  • glGet() with argument GL_SHADER_STORAGE_BUFFER_BINDING
  • glGet() with argument GL_TRANSFORM_FEEDBACK_BUFFER_BINDING
  • glGet() with argument GL_UNIFORM_BUFFER_BINDING

like, one per target, bestie. collect 'em all πŸŽ€


✨ VERSION SUPPORT ✨

okay this part is literally just the vibe check on when you can use this function, and spoiler: it's everywhere πŸ”₯

  • glBindBuffer β€” supported (βœ”) in OpenGL 2.0, 2.1, 3.0, 3.1, 3.2, 3.3, 4.0, 4.1, 4.2, 4.3, 4.4, and 4.5

that's right. every single one. twelve for twelve. she ate and left zero crumbs πŸ‘‘πŸ’€


πŸ¦‹ SEE ALSO πŸ¦‹

go be nosy, i love that for you β€” these are the related calls worth a scroll:

  • glGenBuffers()
  • glBindBufferBase()
  • glBindBufferRange()
  • glMapBuffer()
  • glUnmapBuffer()
  • glDeleteBuffers()
  • glGet()
  • glIsBuffer()

iykyk, the whole buffer friend group 🌸


Copyright Β© 2005 Addison-Wesley. Copyright Β© 2010-2014 Khronos Group.

This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. http://opencontent.org/openpub/.


Copyright Β© 2005 Sams Publishing

Copyright Β© 2010-2014 Khronos Group


and that's the whole page, bestie 🫢 this lil gem comes from [FIXME: source], dated 08/09/2021, and it's filed under GLBINDBUFFER(3G) β€” go forth and bind responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from