Buffer Texture
Core in version | 4.6 | |
---|---|---|
Core since version | 3.0 | |
ARB extension | ARB_texture_buffer_object | |
EXT extension | EXT_texture_buffer_object |
A Buffer Texture is a one-dimensional Texture whose storage comes from a Buffer Object. They are used to allow a shader to access a large table of memory that is managed by a buffer object.
Storage
When creating a regular texture, the user calls glGenTextures, binds the texture object to a particular texture type glBindTexture, and then calls glTexImage* one or more times to create the storage for the data. Creating buffer textures is very similar.
glGenTextures is still used to create the texture object. And glBindTexture is still used to bind the texture. For buffer textures, the texture is bound to the GL_TEXTURE_BUFFER target. Creating the storage for the buffer object is the main difference.
Buffer textures are defined by the fact that the storage for the texels comes directly from a buffer object. Therefore, glTexImage calls are not appropriate for buffer textures; calling any of the gl*Image functions on the GL_TEXTURE_BUFFER will result in an error.
Instead of creating storage for a buffer texture, the user associates a buffer texture with an already existing buffer object. This association is made by binding the buffer texture to the GL_TEXTURE_BUFFER target and then calling this function:
void glTexBuffer(GLenum target, GLenum internalFormat, GLuint buffer);
The target parameter can only be GL_TEXTURE_BUFFER. The internalFormat is the Image Format describing how the pixel data is stored in the buffer object. The buffer is a previously created buffer object. 0 can be used to detach a buffer from a buffer texture.
The buffer object must have been created before this call. The buffer object target GL_TEXTURE_BUFFER can be used as a bind point for creating and modifying such buffer objects, but as stated in the Buffer Object article, this is not required (though it is encouraged).
The storage comes from the beginning of the buffer object. The user cannot allocate a large buffer object and allow multiple texture buffers to use different pieces of them. Unless the user passes an offset into the shader that is applied to the texture coordinate before the fetch. And even then, the limitations still apply to the buffer object as a whole.
Image formats
Normal textures hide the internal binary representation of image data from the user. Since buffer objects are unformatted memory, this is not possible for buffer textures. Therefore, the user is somewhat limited in the Image Formats that are allowed for use with buffer textures.
Only color formats are acceptable in buffer textures; sRGB formats are not allowed. These formats can be 1, 2, and 4 channels (3 channels are available in OpenGL 4.0 or with the ARB_texture_buffer_object_rgb32 extension), so only RED, RG, and RGBA (or RGB with GL 4.0) are acceptable.
Only formats with an explicit size are allowed. GL_RGBA is not sufficient for a buffer texture; the user must use GL_RGBA8 or GL_RGBA16. The sizes can only be 8, 16, or 32; smaller per-component sizes are not allowed. Compressed formats are not allowed.
The format can be any of the various types, like unsigned normalized, floating-point, signed integral, etc. GL_RGBA8UI would be 4-bytes per color, with each byte representing an unsigned integer. When accessing this texture, the user would have to use a proper sampler (usamplerBuffer), which matches the texture type and the unsigned integer nature of its data.
The byte-order for each component is native for the CPU platform in use. So if the user's code is running on a little-endian, then the floats, 32-bit integers, or 16-bit integers are stored in little-endian order. However the order of components is always RGBA in memory, regardless of the endian order.
Texture size
Every texture has a size. For buffer textures, the size of the texture is the size of the buffer object divided by the byte size of a component (based on the internal format. GL_R32F takes up 4 bytes, while GL_RGBA16 takes 8). This is clamped to the implementation-defined limit on the size of all buffer textures (see Limitations below).
Texture parameters
No texture parameters are legal to set on buffer textures. The Sampler Object set onto the same texture unit as a buffer texture is irrelevant.
Access in shaders
In GLSL, buffer textures can only be accessed with the texelFetch function. This function takes pixel offsets into the texture rather than normalized texture coordinates. The sampler types for buffer textures are gsamplerBuffer.
Limitations
Buffer textures have a size limit, separate from the standard one-dimensional texture size. The buffer texture size limit is typically much larger than that of one-dimensional texture. This size, in bytes, is queried with the GL_MAX_TEXTURE_BUFFER_SIZE token. The smallest number this will return is 65536, and the actual value may well be a good deal larger.
Buffer textures are not mip-mapped; they can only store a single image. They also cannot use any filtering. In essence, a buffer texture is simply a way for the shader to directly access a large array of data, generally larger than uniform buffer objects allow.