Game Development Reference
In-Depth Information
float
β32-bit floating-point number
double
β64-bit floating-point number
Note:
Some platforms might not support
int
,
half
, and
double
.If
this is the case, these types are emulated using
float
.
16.3.2
Vector Types
HLSL has the following built-in vector types:
vector
βA 4D vector where each component is of type
float
vector<T, n>
βAn
n
-dimensional vector, where each component
is of
scalar
type
T
. The dimension
n
must be between 1 and 4. Here
is an example of a 2D double vector:
vector<double, 2> vec2;
We can access a component of a vector using an array subscript syntax.
For example, to set the
i
th
component of a vector
vec
, we would write:
vec[i] = 2.0f;
In addition, we can access the components of a vector
vec
as we would
access the members of a structure, using the defined component names
x
,
y
,
z
,
w
,
r
,
g
,
b
, and
a
.
vec.x = vec.r = 1.0f;
vec.y = vec.g = 2.0f;
vec.z = vec.b = 3.0f;
vec.w = vec.a = 4.0f;
The names
r
,
g
,
b
, and
a
refer to exactly the same component as the
names
x
,
y
,
z
, and
w
, respectively. When using vectors to represent col-
ors, the RGBA notation is more desirable since it reinforces the fact
that the vector is representing a color.
Alternatively, we can use these other predefined types that repre-
sent a 2D, 3D, and 4D vector, respectively:
float2 vec2;
float3 vec3;
float4 vec4;
Consider the vector
u
=(
u
x
,
u
y
,
u
z
,
u
w
) and suppose we want to copy
the components of
u
to a vector
v
such that
v
=(
u
x
,
u
y
,
u
y
,
u
w
). The
most immediate solution would be to individually copy each component
of
u
over to
v
as necessary. However, HLSL provides a special syntax
for doing these kinds of out-of-order copies called
swizzles
:
vector u = {1.0f, 2.0f, 3.0f, 4.0f};
vector v = {0.0f, 0.0f, 5.0f, 6.0f};
v = u.xyyw; // v = {1.0f, 2.0f, 2.0f, 4.0f}
Search WWH ::
Custom Search