Just starting out with HLSL, please bare with me :)
There are times I'm a little confused by the syntax for instance in the pixel shader of an example I am following we have the following input and output:
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 Depth : TEXCOORD0;
float3 Normal : TEXCOORD1;
};
struct PixelShaderOutput
{
float4 Normal : COLOR0;
float4 Depth : COLOR1;
};
PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
PixelShaderOutput output;
// Depth is stored as distance from camera / far plane distance
// to get value between 0 and 1
output.Depth = input.Depth.x / input.Depth.y;
// Normal map simply stores X, Y and Z components of normal
// shifted from (-1 to 1) range to (0 to 1) range
output.Normal.xyz = (normalize(input.Normal).xyz / 2) + .5;
// Other components must be initialized to compile
output.Depth.a = 1;
output.Normal.a = 1;
return output;
}
Now one of the parts that confuses me is
output.Depth = input.Depth.x / input.Depth.y;
I'm confused because I thought Depth is a float4, which to my understanding is a 4-component vector. Doesn't input.Depth.x / input.Depth.y return a scalar value? What is happening in this assignment?
Secondly, and similarly
output.Normal.xyz = (normalize(input.Normal).xyz / 2) + .5;
Wouldn't normalize(input.Normal).xyz / 2 return a 3-component vector, with each element divided by 2? What does it mean then to add that 3-component vector to a scalar .5?
I feel like I'm missing something obvious here that I'll probably slap my forehead when someone points it out.
Also one more question - by the time the float4 Position arrives at the pixel shader from the vertex shader output, at this point are the coordinates in clip-space [-1,1] or are they still in camera-coordinates but with the homogeneous w-component (eg before the perspective divide) ? Is the w-component usually equal to the z-component?