Shader Storage Buffer Object: Difference between revisions
(→Shader specification: Describing non-block buffer variables.) |
(Restating.) |
||
Line 13: | Line 13: | ||
The major differences between them are: | The major differences between them are: | ||
# SSBOs can be much larger. The | # SSBOs can be much larger. The OpenGL spec guarantees that UBOs can be up to 16KB in size (implementations can allow them to be bigger). The spec guarantees that SSBOs can be up to 16'''MB'''. Most implementations will let you allocate a size up to the limit of GPU memory. | ||
# SSBOs are writable, even atomically; UBOs are {{code|uniform}}s. SSBOs reads and writes use [[Incoherent Memory Access|incoherent memory accesses]], so they need the appropriate barriers, just as [[Image Load Store]] operations. | # SSBOs are writable, even atomically; UBOs are {{code|uniform}}s. SSBOs reads and writes use [[Incoherent Memory Access|incoherent memory accesses]], so they need the appropriate barriers, just as [[Image Load Store]] operations. | ||
# SSBOs can have variable storage, up to whatever buffer range was bound for that particular buffer; UBOs must have a specific, fixed storage size. This means that you can have an array of arbitrary length in an SSBO (at the end, rather). The actual size of the array, based on the range of the buffer bound, can be queried at runtime in the shader using the {{code|length}} function on the unbounded array variable. | # SSBOs can have variable storage, up to whatever buffer range was bound for that particular buffer; UBOs must have a specific, fixed storage size. This means that you can have an array of arbitrary length in an SSBO (at the end, rather). The actual size of the array, based on the range of the buffer bound, can be queried at runtime in the shader using the {{code|length}} function on the unbounded array variable. |
Revision as of 20:33, 25 October 2015
|
||||||||||||
OpenGL Objects
|
A Shader Storage Buffer Object is a Buffer Object that is used to store and retrieve data from within the OpenGL Shading Language.
SSBOs are a lot like Uniform Buffer Objects. Shader storage blocks are defined by Interface Block (GLSL)s in almost the same way as uniform blocks. Buffer objects that store SSBOs are bound to SSBO binding points, just as buffer objects for uniforms are bound to UBO binding points. And so forth.
The major differences between them are:
- SSBOs can be much larger. The OpenGL spec guarantees that UBOs can be up to 16KB in size (implementations can allow them to be bigger). The spec guarantees that SSBOs can be up to 16MB. Most implementations will let you allocate a size up to the limit of GPU memory.
- SSBOs are writable, even atomically; UBOs are uniforms. SSBOs reads and writes use incoherent memory accesses, so they need the appropriate barriers, just as Image Load Store operations.
- SSBOs can have variable storage, up to whatever buffer range was bound for that particular buffer; UBOs must have a specific, fixed storage size. This means that you can have an array of arbitrary length in an SSBO (at the end, rather). The actual size of the array, based on the range of the buffer bound, can be queried at runtime in the shader using the length function on the unbounded array variable.
- SSBO access, all things being equal, will likely be slower than UBO access. SSBOs generally are accesses like buffer textures, while UBO data is accessed through internal shader-accessible memory reads. At the very least, UBOs will be no slower than SSBOs.
Functionally speaking, SSBOs can be thought of as a much nicer interface to Buffer Textures when accessed via Image Load Store.
Shader specification
There are two ways to declare an SSBO in a shader. The first way is to declare a Buffer Backed Interface Block, using the buffer keyword.
As an alternative, a single global variable can be declared as an SSBO. These are called "buffer variables". Each individual variable declaration is a separate SSBO. They can use any non-Opaque Types, including user-defined arrays and structs.
Usually, buffer variables are declared as arrays. As with interface blocks for SSBOs, buffer variable arrays can have the first index declared as just []. This means that the length of the array will be determined at runtime, based on the amount of storage in the buffer object range bound when rendering.
Atomic operations
There are special atomic functions that can be applied to buffer variables (and also to compute shader shared variables). They only take uint or int types, but these can be members of aggregates (structs/arrays) or vector elements (ie: you can atomically access uvec3.x).
All of the atomic functions return the original value. The term "nint" can be int or uint.
Adds data to mem.
The mem's value is no lower than data.
The mem's value is no greater than data.
mem becomes the bitwise-and between mem and data.
mem becomes the bitwise-or between mem and data.
mem becomes the bitwise-xor between mem and data.
Sets mem's value to data.
If the current value of mem is equal to compare, then mem is set to data. Otherwise it is left unchanged.
OpenGL usage
TODO: This section needs to be filled in. |