Sign in to follow this  
ErikBongers

HLSL: initialize variables?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this