Shader Subroutine: Difference between revisions

From OpenGL Wiki
Jump to navigation Jump to search
General outline and categorization.
Line 17: Line 17:


== Definition ==
== Definition ==
The first step in defining shader subroutines is to define a '''subroutine type'''. A subroutine type consists of a named function signature. It represents a specific set of subroutines that can be called.
A subroutine type is declared as follows:
<source lang="glsl">
subroutine FuncReturnType SubroutineTypeName(Type0 param0, Type1 param1, ...);
</source>
The {{code|SubroutineTypeName}} is the name of the subroutine type defined by this statement. Multiple different named types can use the same function signature.
A specific function definition (and ''only'' a function definition. If you use this syntax, the function must have a body) can be associated with one or more subroutine types as follows:
<source lang="glsl">
subroutine(SubroutineTypeName) FuncReturnType FunctionName(Type0 param0, Type1 param1, ...);
</source>
{{code|FunctionName}}'s function signature must exactly match the subroutine type's signature.
GLSL normally allows [[Core_Language_(GLSL)#Functions|function overloading]]. However, subroutine functions cannot be overloaded; you cannot declare a second {{code|FunctionName}} with a different set of parameters. Even if the second function is not a subroutine.
A function can be associated with multiple subroutine types by listing multiple {{code|SubroutineTypeName}}s, separated by commas. Obviously, all of those subroutine types must use the same signature.
The user can call {{code|FunctionName}} exactly as it used to, and it will have the usual behavior. To use it as a subroutine requires calling it in a different way.


== Subroutine uniforms ==
== Subroutine uniforms ==

Revision as of 04:21, 29 August 2013

Shader Subroutine
Core in version 4.6
Core since version 4.0
Core ARB extension ARB_shader_subroutine

Shader Subroutines are special GLSL functions which can have variations. The specific variation that will be called is selected by the OpenGL code.

The general idea is as follows. The shader defines some number of special functions, each of which uses the same function signature. Each such function is called a "subroutine". Each subroutine is associated with one or more "subroutine types". The type represents a specific aggregation of subroutines that could be called, a specific list of variations.

Once a subroutine type is defined, the shader can declare one or more global "variables" of that type. Each variable represents . Because these are set from OpenGL code before rendering type, these are called "uniform"s (though they do not work like regular uniforms in almost any way). Each variable represents a specific, user-defined selection from among the possible subroutines in a subroutine type. The shader code can call these subroutine uniforms as though they were functions.

The user can query indices and locations for subroutines, types, and subroutine uniforms. These queries allow the user to specify which subroutines are used in which subroutine uniforms.

Note: This specification must happen every time the current program or program pipeline object binding is changed. This is not program object state, and the context will destroy this state after each new binding.

Definition

The first step in defining shader subroutines is to define a subroutine type. A subroutine type consists of a named function signature. It represents a specific set of subroutines that can be called.

A subroutine type is declared as follows:

subroutine FuncReturnType SubroutineTypeName(Type0 param0, Type1 param1, ...);

The SubroutineTypeName is the name of the subroutine type defined by this statement. Multiple different named types can use the same function signature.

A specific function definition (and only a function definition. If you use this syntax, the function must have a body) can be associated with one or more subroutine types as follows:

subroutine(SubroutineTypeName) FuncReturnType FunctionName(Type0 param0, Type1 param1, ...);

FunctionName's function signature must exactly match the subroutine type's signature.

GLSL normally allows function overloading. However, subroutine functions cannot be overloaded; you cannot declare a second FunctionName with a different set of parameters. Even if the second function is not a subroutine.

A function can be associated with multiple subroutine types by listing multiple SubroutineTypeNames, separated by commas. Obviously, all of those subroutine types must use the same signature.

The user can call FunctionName exactly as it used to, and it will have the usual behavior. To use it as a subroutine requires calling it in a different way.

Subroutine uniforms

Calling

Runtime selection

Limitations