Search code examples
directx-11hlsl

HSLS float1,..., float4 and its fields


I see that HSLS has float and a number after float (1-4) and a lot of fields (r, rr, rrrr, arar,...) (I see these fields with the help of HLSL Tool Thanks to Tim Jones :D - I can't imagine (for now) how can we programming HSLS without this tool).

I try to find references about this data type and its field but I can't find anything about it(why the number 1-4 and the meanings of its fields. In C++ float is just float number, here, what is float)

Below is my progress so far:

According to this article

float - 32-bit floating point value.

For example, here is a 4-component signed-normalized float-variable declaration.

snorm float4 fourComponentIEEEFloat;

It doesn't answer my question: What does this code mean

RWTexture2D<float4> testTexture         : register(u0);

[numthreads(@value( threads_per_group_x ), @value( threads_per_group_y ), @value( threads_per_group_z ))]
void main
(
    uint3 gl_LocalInvocationID : SV_GroupThreadID,
    uint3 gl_GlobalInvocationID : SV_DispatchThreadId
)
{
    testTexture[gl_GlobalInvocationID.xy].xyzw = float4( float2(gl_LocalInvocationID.xy) / 16.0f, 0.0f, 1.0f );
}

I may slightly understand that float2(gl_LocalInvocationID.xy) is 2 float

testTexture[gl_GlobalInvocationID.xy].xyzw = float4( float2(gl_LocalInvocationID.xy) / 16.0f, 0.0f, 1.0f );

is equal

testTexture[gl_GlobalInvocationID.xy].xyzw = float4(gl_LocalInvocationID.x / 16.0f, gl_LocalInvocationID.y / 16.0f, 0.0f, 1.0f);

But I don't know if it's right, beside, how about fields r, rr, rrrr, arar,... I mention earlier.

Thanks for reading.


Solution

  • These fields are no real fields, but Source Register Swizzling (doc).

    A float4 is a four-component float vector, where the components are x, y, z and w. As float4 is often used as a color as well, these components can also be addressed by r, g, b and a (So .xyzw is equivalent to .rgba).

    Source swizzling enables you to swap, reduce or repeat components inline, so if you only need the xy component of your float3 vec you can just write vec.xy instead of something like float2(vec.x, vec.y). If you want to swap color channels of your float4 color for some computation you can easily write color.bgra to swap the red and blue component for example. And finally you can also duplicate channels, such as vec.xxxx to get a float4 with vec.x in all four components. Combining these methods is also allowed, such as vec.yzxx.