1

I have encountered a weird x.wwww component of the float4 x vector in a HLSL code. I haven't seen such a member or member function of float4 structures in HLSL, so please tell me what is the meaning of that. It is in the code of this pixel shader:

float4 PSConstantColor( PS_PARTICLE_INPUT input ) : SV_TARGET
{
   // Sample particle texture
   float4 vColor = g_baseTexture.Sample( g_samLinear, input.Tex ).wwww;

   // Clip fully transparent pixels
   clip( vColor.a - 1.0/255.0 );

   // Return color
   return vColor;
}

At first I thought it should be the w component replicated to all the members of the float4 vColor vector, but it does not make sense for the color value, does it?

Pekov
  • 157
  • 6

1 Answers1

4

From my understanding they are called swizzle operators. In this case they make a float4 of the 4th element in the given float4:

vColor = (w, w, w, w)

Where w is g_baseTexture.Sample( g_samLinear, input.Tex ).w

Here is an example of a swizzle operator in practice:

float3 cross( float3 a, float3 b ) {
    return float3( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.z*b.y - a.y*b.z );
}

and its equivalent swizzle implementation

float3 cross( float3 a, float3 b ) {
    return a.yzx*b.zxy - a.zxy*b.yzx;
}
Saevax
  • 251
  • 1
  • 9
  • I thought so too, but when I searched MSDN for this wwww swizzle operator, as you said, it does not show it as a part of float4 structure. Other operators you mentioned (xyz,zxy etc.) are clearly explained but non of wwww is present. I searched here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb509634(v=vs.85).aspx but found nothing about it. – Pekov Jun 24 '15 at 12:24
  • Yes I got it now, though I still can't understand the meaning of such a color value, except outputing grayscale image of the texture. – Pekov Jun 24 '15 at 12:43
  • 1
    @Pekov - it's not so unusual to use this kind of swizzle, even when you want non-greyscale output. For example, a CoCg_Y-DXT5 decoding shader might use colour = sample.aaaa (or equivalently, sample.wwww) to "broadcast" the luma component (stored in the alpha channel) to all colour channels as a first step, and then add the Co & Cg chroma offsets to each of r, g, and b in subsequent steps, to tint the initial grey to the desired output colour. – DMGregory Jun 24 '15 at 16:33
  • 1
    @Pekov: Those are not actual structure members. They are HLSL language functionality. The compiler generates code for them. – Tara Mar 09 '17 at 17:54