Search code examples
c++buffershaderhlsldirectx-11

C++ HLSL Buffer variable


So hi guys,

right now I'm just trying to pass a value to the hlsl shader. Now heres the constant buffer in the shader:

cbuffer ConstantBuffer
{
    float4x4 final;
    float4x4 rotation;    // the rotation matrix
    float4 lightvec;      // the light's vector
    float4 lightcol;      // the light's color
    float4 ambientcol;    // the ambient light's color
    float3 SPACE;
    float alpha;  // <-- Variable trying to recieve, the first 5 works!
}

And in c++:

struct CBUFFER
{
    public:
        D3DXMATRIX Final;
        D3DXMATRIX Rotation;
        D3DXVECTOR4 LightVector;
        D3DXCOLOR LightColor;
        D3DXCOLOR AmbientColor;
        D3DXVECTOR3 SPACE;
        float Alpha;
};

So I've tried to align the variables to be able to send them in packs of 16 bits. But for some reason, it isn't working, because the variable alpha in the shader isn't set.

So what did i do wrong? (PS. I set the bufferwidth in the initialization of the shader to: 192)

Thank You


Solution

  • check de Dave awnser, is possible that the compiler created a offset in variables of struct to make all more fast to use in CPU fetch. If pragma not works check if you are using the alpha value direct in pixel shader stage and set the correctly constant buffer (constant buffer set in vertex shader is not shared in Pixel shader if you aren't using effects).