Search code examples
arraysgeometryshaderassignhlsl

What am I doing wrong when trying to assign a value in hlsl?


I have this in a geometry shader

cbuffer cbFixed
{
    float2 TexC[4] =
    {
        float2(0.0f, 1.0f),
        float2(0.0f, 0.0f),
        float2(1.0f, 1.0f),
        float2(1.0f, 0.0f)
    };
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float4 PosWorld : POSITION;
    float4 Norm : NORMAL;
    float2 Tex : TEXCOORD;
    uint PrimID: SV_PrimitiveID;
};

and later...

PS_INPUT gout;

for (int i = 0; i < 4; i++)
{
// other stuff
gout.Tex = TexC[i];
// other stuff
}

However, for some reason this does not work as expected, by which I mean that the textures are not applied, whereas this does:

for (int i = 0; i < 4; i++)
{
    if (i == 0) gout.Tex = float2(0.0f, 1.0f);
    if (i == 1) gout.Tex = float2(0.0f, 0.0f);
    if (i == 2) gout.Tex = float2(1.0f, 1.0f);
    if (i == 3) gout.Tex = float2(1.0f, 0.0f);
}

Any idea why? I didn't want to make this a long post so kept the detail to a minimum.


Solution

  • Your TexC values are ignored because variables inside cbuffer have to be set with a call *SetConstantBuffers and default initializers like yours are ignored, and if you don't do that you get undefined behaviour (in most cases you'll be reading zero values). What you want is to write something like this:

    static const float2 TexC[4] =
        {
            float2(0.0f, 1.0f),
            float2(0.0f, 0.0f),
            float2(1.0f, 1.0f),
            float2(1.0f, 0.0f)
        };
    

    instead of cbuffer.