Variables are declared with one or more of the following components:
[Storage Class Modifiers] [Type Modifiers] Type VariableName[Array_Index] [: Semantic] [Initializers] [Annotations] [: Register];
Storage class modifiers are optional keywords that give the compiler hints about variable scope and lifetime.
Global variable declarations prefixed with the uniform keyword are uniform inputs to the shader. All non-static global variables are uniform by default.
Global variable declarations can be prefixed with the shared keyword. This a hint to the effect framework that the value of this parameter is intended to be shared between effect objects.
extern | A global variable marked extern is an external input to the shader. To set an extern variable, use any of the set methods ("SetVertexShaderConstantX") or any of the ID3DXConstantTable interface methods. A global variable that is not declared to be either static or extern is assumed to be extern. You cannot declare a global variable with both extern and static. |
---|---|
shared | Global shader variables that can be shared between effects use the shared keyword. |
static | The static keyword is interpreted differently on a global variable and a local variable. For a local variable (declared inside of a function), the static keyword indicates that the value of the variable persists between funtion invocations. Initialization of local static variables happens only once. If they are not explicitly initialized, their initial value is assumed to be zero. For a global variable (declared outside of any function), the static keyword indicates that the shader variable will not be visible to an application. Since it is not visible, the get and set methods ("GetVertexShaderConstantX" or "SetVertexShaderConstantX") will not work, and the ID3DXConstantTable methods will not work. |
uniform | Use the uniform keyword to mark a uniform shader variable. This is a variable that is initialized in the constant registers with the ID3DXConstantTable interface. All the vertices (if you are using a vertex shader) or pixels (if you are using a pixel shader) see the same initial value in a uniform variable. Global variables are treated as if they are declared uniform. |
volatile | On global variables, this is a hint to the effect framework that the value of this parameter changes frequently. |
Storage class modifiers are optional and may be specified in any order, as long as they occur before the variable type.
Type modifiers are optional keywords placed immediately before the variable type, which give the compiler additional information about the data type.
const | The const modifier indicates a variable whose value cannot be changed by a shader. Declaring a variable with const allows the compiler to put the value in a portion of memory that does not need write access. Because the variable cannot be changed, it must be initialized in the declaration. |
---|---|
row_major | Matrix elements are organized in row-major order; that is, each row of the matrix will be stored in a single constant register. |
col_major | Matrix elements are organized in column-major order; that is, each column of the matrix will be stored in a single constant register. |
Any valid type including:
An ASCII string that uniquely identifies the name of a variable within a shader. Each variable can be defined as a single variable or optionally as an array of variables. Array_Index is the optional array size and is specified as a positive integer greater than or equal to 1.
Shader parameters marked with semantics contain varying data. Semantics can mark any parameter, but only the semantics attached to top-level parameters are used at compile time by HLSL. Semantics have no meaning in the language, they are simply associated with the variable and passed down to the back-end. Semantics are not case-sensitive. For a listing of the vertex and pixel shader semantics, see the shader semantic syntax.
An initializer is an optional, default value. For good practice, the number of initializers should match the number of components in the data type.
For global variables marked extern, each expression must be a literal value. For other global variables and local variables marked static, each expression must be a constant.
Annotations are metadata attached to global variables. They are ignored by HLSL, but used by effects. They cannot be referenced from a shader. See Add Information to Effect Parameters with Annotations.
An optional constant register. Use this to assign a variable to a particular constant register. If the variable type will not fit into a single register, consecutive registers are used. See Binding a Shader Parameter to a Particular Register.
Here are a few sample declarations:
float4 color; uniform float4 position : POSITION; const float3 lightDirection = {0,0,1};