Compute Shader

From OpenGL.org
Revision as of 19:09, 20 October 2012 by Alfonse (Talk | contribs) (Limitations)

Jump to: navigation, search
Compute Shader
Core in version 4.5
Core since version 4.3
Core ARB extension ARB_compute_shader

A Compute Shader is a Shader Stage that is used entirely for computation.

Overview

Compute shaders operate differently from other shader stages. All of the other shader stages have a well-defined set of input values, some built-in and some user-defined. They have a well-defined set of output values, some built-in and some user-defined. The frequency at which a shader stage executes is specified by the nature of that stage; vertex shaders execute once per input vertex, for example (though some executions can be skipped via caching). Fragment shader execution is defined by the fragments generated from the rasterization process.

Compute shaders work very differently. The "space" that a compute shader operates on is largely abstract; it is up to each compute shader to decide what the space means. The number of compute shader executions defined by the function used to execute the compute operation. Most important of all, compute shaders have no user-defined inputs and no outputs at all. The built-in inputs only define where in the "space" of execution a particular compute shader invocation is.

Therefore, if a compute shader wants to take some values as input, it is up to the shader itself to fetch that data, via texture access, arbitrary image load, shader storage blocks, or other forms of interface. Similarly, if a compute shader is to actually compute anything, it must explicitly write to an image or shader storage block.

Compute space

The space that compute shaders operate within is abstract. There is the concept of a work group; this is the smallest amount of compute operations that the user can execute. Or to put it another way, the user can execute some number of work groups.

The number of work groups that a compute operation is executed with is defined by the user when they invoke the compute operation. The space of these groups is three dimensional, so it has a number of "X", "Y", and "Z" groups. Any of these can be 1, so you can perform a two-dimensional or one-dimensional compute operation instead of a 3D one. This is useful for processing image data or linear arrays of a particle system or whatever.

When the system actually computes the work groups, it can do so in any order. So if it is given a work group set of (3, 1, 2), it could execute group (0, 0, 0) first, then skip to group (1, 0, 1), then jump to (2, 0, 0), etc. So your compute shader should not rely on the order in which individual groups are processed.

The work group that a particular compute shader invocation is executing within is passed as an input value.

Do not think that a work group is the same thing as a compute shader invocation; there's a reason why it is called a "group". Within a single work group, there may be many compute shader invocations. How many is defined by the compute shader itself, not by the call that executes it. This is known as the local size of the work group.

Every compute shader has a three-dimensional local size (again, sizes can be 1 to allow 2D or 1D local processing). This defines the number of invocations of the shader that will take place within each work group.

Therefore, if the local size of a compute shader is (128, 1, 1), and you execute it with a work group count of (16, 8, 64), then you will get 1,048,576 separate shader invocations. Each invocation will have a set of inputs that uniquely identify that specific invocation.

This distinction is useful for doing various forms of image compression or decompression; the local size would be the size of a block of image data (8x8, for example), while the group count will be the image size divided by the block size. Each block is processed as a single work group.

The individual work group invocations will be executed "in parallel". The main purpose of the distinction between work group count and local size is that the different compute shader invocations within a work group can inter-communicate through a set of shared​ variables. Invocations between work groups can theoretically inter-communicate, but only through some form of global memory and only with difficulty.

Dispatch

Compute shaders are not part of the regular rendering pipeline. So the usual Vertex Rendering functions do not work on them.

A program object can have a compute shader in it. The compute shader linked with other Shader Stages (whether in a single program object or in a program pipeline) is effectively inert to rendering functions.

There are two functions to initiate compute operations. They will use whichever compute shader is currently active (via glBindProgramPipeline or glUseProgram, following the usual rules for determining the active program for a stage).

 void glDispatchCompute(GLuint num_groups_x​, GLuint num_groups_y​, GLuint num_groups_z​);

The num_groups_*​ parameters define the work group count, in three dimensions. These numbers cannot be zero. There are limitations on the number of work groups that can be dispatched.

It is possible to execute dispatch operations where the work group count comes from information stored in a Buffer Object. This is similar to indirect rendering for vertex data:

 void glDispatchComputeIndirect(GLintptr indirect​);

The indirect​ parameter is the byte-offset to the buffer currently bound to the GL_DISPATCH_INDIRECT_BUFFER​ target. Note that the same limitations on work group counts (see below) still apply; however, indirect dispatch bypasses OpenGL's usual error checking. As such, attempting to dispatch with out-of-bounds work group sizes can cause a crash or even a GPU hard-lock.

Inputs

Compute shaders cannot have any user-defined input variables. It only provides the following built-in input variables:

in uvec3 gl_NumWorkGroups;
in uvec3 gl_WorkGroupID;
in uvec3 gl_LocalInvocationID;
in uvec3 gl_GlobalInvocationID;
in uint  gl_LocalInvocationIndex;
gl_NumWorkGroups​
This variable contains the number of work groups passed to the dispatch function.
gl_WorkGroupID​
This is the current work group for this shader invocation. Each of the XYZ components will be on the half-open range [0, gl_NumWorkGroups.XYZ).
gl_LocalInvocationID​
This is the current invocation of the shader within the work group. Each of the XYZ components will be on the half-open range [0, gl_WorkGroupSize.XYZ).
gl_GlobalInvocationID​
This value uniquely identifies this particular invocation of the compute shader among all invocations of this compute dispatch call. It's a short-hand for the math computation:
gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID;
gl_LocalInvocationIndex​
This is a 1D version of gl_LocalInvocationID​. It identifies this invocation within the work group. It is short-hand for this math computation:
  gl_LocalInvocationIndex =
          gl_LocalInvocationID.z * gl_WorkGroupSize.x * gl_WorkGroupSize.y +
          gl_LocalInvocationID.y * gl_WorkGroupSize.x + 
          gl_LocalInvocationID.x;

Local size

The local size of a compute shader is defined within the shader, using a special layout input declaration:

 layout(local_size_x = X​, local_size_y = Y​, local_size_z = Z​) in;

By default, the local sizes are 1, so if you only want a 1D or 2D work group space, you can specify just the X​ or the X​ and Y​ components. They must be integral constant expressions of value greater than 0.

The local size is available to the shader as a compile-time constant variable, so you don't need to define it yourself:

const uvec3 gl_WorkGroupSize;

Shared variables

Limitations

The number of work groups that can be dispatched in a single dispatch call is defined by GL_MAX_COMPUTE_WORK_GROUP_COUNT. This must be queried with glGetIntegeri_v, with the index being on the closed range [0, 2], representing the X, Y and Z components of the maximum work group count. Attempting to call glDispatchCompute with values that exceed this range is an error. Attempting to call glDispatchComputeIndirect is much worse; it may result in program termination or other badness.

Note that the minimum these values must be is 65535 in all three axes. So you've probably got a lot of room to work with.

There are limits on the local size as well; indeed, there are two sets of limitations. There is a general limitation on the local size dimensions, queried with GL_MAX_COMPUTE_WORK_GROUP_SIZE in the same way as above. Note that the minimum requirements here are much smaller: 1024 for X and Y, and a mere 64 for Z.

There is another limitation: the total number of invocations within a work group. That is, the product of the X, Y and Z components of the local size must be less than GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS (a single value, queried with glGetIntegerv.