HLSL: initialize variables?

Started by
3 comments, last by dys129 14 years, 8 months ago
Hi, Just a quick question. In HLSL code, I often notice initializations like the following.
VS_OUTPUT VS(float3 InPos  : POSITION, float2 InTex  : TEXCOORD0)
{
    VS_OUTPUT Out = (VS_OUTPUT)0; //Init struct to zero? Special reason?

    // transform the position to the screen
    Out.Pos = float4(InPos,1);
    Out.Tex = InTex;

    return Out;
}
I assume the 1st line clears the whole VS_OUTPUT struct to zero. But most of the time, ALL members of the struct are set afterwards. So, is the 'clear' statement just safe programming or are there other benefits? (I assume it's just safe programming, but I'm asking just in case...) Thanks in advance, Erik.
Advertisement
No, there shouldn't be any special benefits that I'm aware of.
Last time I checked, VS_OUTPUT Out; and VS_OUTPUT Out = (VS_OUTPUT)0; compile to the same thing.

You need to create the VS_OUTPUT object before you can start using it, but you don't need to 0 it. Although it is the "safe" thing to do.
Thank you adt7 and MJP.
Ah, yes, the compiled result, of course!
So the init is even ignored/optimized by the compiler?
'Nough said!
There is no special reason. I assume that it is because of experiance in C programming. All variables of all types are guranteed to be initialized to 0.0f by the compiler and by Microsoft :)

This topic is closed to new replies.

Advertisement