Graphics Reference
In-Depth Information
As in C or C++, these types can be used to declare both scalar and array values. Scalar
variables support a standard set of mathematical operators, including addition, subtraction,
negation, multiplication, division, and modulus. Scalars also support a standard set of logi-
cal and comparison operators, using the same syntax as C/C++. Array variables must have
a fixed, statically declared size determined at the time of compilation, because HLSL does
not support dynamic memory allocation.
6.3.2 Vectors
HLSL also supports declaring vector and matrix variables. Vector types support 1-4 com-
ponents, with each component using the storage format specified by the primitive type.
Vector variables are declared with the syntax shown in Listing 6.2.
vector<float, 4>
floatVector;
// 4-component vector with float components
vector<int, 2> intVector;
// 2-component vector with int components
Listing 6.2. Vector declaration syntax.
For convenience, HLSL predefines shorthand-type definitions for all combinations of
primitive types and components. Listing 6.3 demonstrates the declaration of vector vari-
ables using the shorthand notation.
float4
floatVector;
// 4-component vector with float components
int2
intVector;
// 2-component vector with int components
Listing 6.3. Vector shorthand declaration syntax.
Vector types can be initialized when declared using array initializer syntax, with the
number of values matching the number of components. Vector types also support construc-
tor syntax, which allows passing scalar values or even other vectors. In addition, vectors
can be initialized using a single scalar value, in which case the scalar value is replicated to
all components. Listing 6.4 demonstrates these initialization patterns.
float2 vec0 = { 0.0f, 1.0f };
float3 vec1 = float3( 0.0f, 0.1f, 0.2f );
float4 vec2 = float4( vecl, 1.0f );
float4 vec3 = 1.0f;
Listing 6.4. Vector initialization syntax.
Search WWH ::




Custom Search