Jump to content
  • Advertisement
Sign in to follow this  

HLSL behaviour when float4 declared inside main function and outside

This topic is 3165 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am seeing a weird issue. I have this shader:

// Vertex shader input structure
struct VS_INPUT
    float4 Position   : POSITION;
    float2 Texture    : TEXCOORD0;
    float4 DiffuseColor : COLOR0;
    float3 Normal       : NORMAL;

// Vertex shader output structure
struct VS_OUTPUT
    float4 Position   : POSITION;
    float2 Texture    : TEXCOORD0;
    float4 DiffuseColor : COLOR0;

// Global variables
float4x4 WorldViewProj;

float4 LightColor = {1.0f, 0.0f, 0.0f, 1.0f};

VS_OUTPUT vs_main( in VS_INPUT In )
    VS_OUTPUT Out;                      //create an output vertex
    float4 colorContibutedbyLight = float4(0.0f, 1.0f, 0.0f, 0.0f);
    colorContibutedbyLight = LightColor * 0.5f;
    Out.Position = mul(In.Position,
                       WorldViewProj);  //apply vertex transformation
    Out.Texture  = In.Texture;          //copy original texcoords
    Out.DiffuseColor = colorContibutedbyLight;

    return Out;                         //return output vertex

Notice that here LightColor is declared just outside vs_main. If i use this shader i always get black diffuse color. It seems that LightColor is not getting initialized properly. But if I just move the line "float4 LightColor = {1.0f, 0.0f, 0.0f, 1.0f};" inside vs_main, then all works as expected. Is this some HLSL thing I dont know ? or is this a driver bug ?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!