GLSL samplerBuffer: Accessing Buffer Textures
This article explains the role of samplerBuffer in GLSL,
detailing how it enables shaders to access buffer textures as flat,
one-dimensional linear arrays. You will learn the core purpose of
samplerBuffer, how it differs from traditional texture
samplers, how to retrieve data using integer indexing, and the
advantages it provides for large-scale data storage.
What is samplerBuffer?
In GLSL, samplerBuffer is an opaque uniform type used to
access data from a Buffer Texture (backed by a Texture Buffer Object, or
TBO). Unlike standard 1D, 2D, or 3D textures that feature dimensional
coordinates and image-processing capabilities, a buffer texture is
backed directly by a raw, contiguous OpenGL buffer object. Within the
shader, samplerBuffer acts as a typed handle to read that
continuous block of memory.
Direct Linear Indexing
The primary role of samplerBuffer is to expose memory as
a flat, indexed array rather than an image.
- Integer Indexing: Standard textures use normalized
floating-point coordinates (ranging from
0.0to1.0). In contrast, asamplerBufferuses direct integer indices corresponding to array elements (0,1,2, ...,N-1). - The
texelFetchFunction: You cannot sample a buffer texture using the generictexture()function. Instead, you must usetexelFetch():
uniform samplerBuffer u_DataBuffer;
void main() {
int index = 105; // Points directly to the 106th element
vec4 data = texelFetch(u_DataBuffer, index);
}Because buffer textures do not have mipmaps, texelFetch
takes only the sampler and the integer coordinate, omitting the
level-of-detail (LOD) parameter required by other texture types.
Stripped-Down Sampler Features
To prioritize raw read efficiency, samplerBuffer
bypasses almost all traditional texture hardware logic:
- No Filtering: Buffer textures do not support linear
filtering (
GL_LINEAR). Reads are strictly point-sampled. - No Mipmapping: The data exists solely at base level zero.
- No Texture Wrapping: Operations like
GL_REPEATorGL_CLAMP_TO_EDGEdo not apply. Accessing an index outside the buffer boundaries leads to undefined behavior.
Hardware Format Unpacking
Although the backing memory is an untyped byte array on the CPU side,
samplerBuffer handles automatic data parsing on the GPU.
When the buffer is attached to the texture, an internal format (such as
GL_RGBA32F, GL_R32I, or GL_RGBA8)
is specified.
When texelFetch executes, the hardware reads the
required number of bytes for that index and unpacks them into the
appropriate vector type:
samplerBufferreturns floating-point components (vec4).isamplerBufferreturns signed integer components (ivec4).usamplerBufferreturns unsigned integer components (uvec4).
High-Capacity Array Storage
A critical advantage of using samplerBuffer is memory
capacity. Traditional 1D textures are limited by
GL_MAX_TEXTURE_SIZE (typically 8,192 to 16,384 texels).
Buffer textures are constrained by
GL_MAX_TEXTURE_BUFFER_SIZE, which is often tens or hundreds
of millions of texels (frequently reaching 128 MB or higher). This makes
samplerBuffer a useful mechanism for passing large sets of
uniform data, such as bone transform matrices for skeletal animation,
physical constants for particle engines, or arbitrary lookup tables.