1

I am in the process of implementing lighting in my DirectX 11 project. The problem I have is that when I try to access a cbuffer value from the Pixel Shader function it's just returning float3(0, 0, 0) meanwhile when I access the same value in the Vertex Shader function it returns the correct value. Here is the shader:

/*********************************************\
                 VERTEX SHADER
\*********************************************/

//Constant buffers
cbuffer Object : register(cb0) {
    float4x4 WorldMatrix;
};
cbuffer Camera : register(cb1) {
    float4x4 ViewMatrix;
    float4x4 ProjectionMatrix;
};
cbuffer LightBuffer : register(cb2) {
    float3 AmbientColor;
}


//IO Structs
struct VS_INPUT {
    float3 Position : POSITION;
    float2 UV : TEXCOORD;
    float3 Normal : NORMAL;
};
struct VS_OUTPUT {
    float4 Position : SV_POSITION;
    float2 UV : TEXCOORD;
    float3 Normal : NORMAL;
};

VS_OUTPUT VS(VS_INPUT input){
    VS_OUTPUT output;

    float4 Position;

    //Multiply position with AmbientColor (should be 1, 1, 1), position unchanged
    Position = mul(ViewMatrix, float4(input.Position * AmbientColor, 1));
    Position = mul(ProjectionMatrix, Position);
    Position = mul(WorldMatrix, Position);

    output.Position = Position;
    output.UV = input.UV;
    output.Normal = mul(WorldMatrix, input.Normal);
    return output;
}

/*********************************************\
                PIXEL SHADER
\*********************************************/

SamplerState TextureState;
Texture2D<float4> Texture;
float4 PS(VS_OUTPUT input) : SV_TARGET {
    float4 MaterialColor = Texture.Sample(TextureState, input.UV);

    //Multiply color with AmbientColor (should be 1, 1, 1), returns black
    float3 FinalColor = MaterialColor.xyz * AmbientColor;

    return float4(FinalColor, MaterialColor.a);
}

Here's is the value I'm sending (c++):

_LightsUniform.AmbientColor = XMFLOAT3(1, 1, 1); 
DeviceContext->UpdateSubresource(_LightBuffer, 0, NULL, &_LightsUniform, 0, 0);
DeviceContext->VSSetConstantBuffers(2, 1, &_LightBuffer);
DeviceContext->PSSetConstantBuffers(2, 1, &_LightBuffer);

Here is the result: http://i.gyazo.com/357f1ed3ea33e6569ad2346b368cd975.png

And result without multiplying color: http://gyazo.com/b60b385daa94d3373e9552a523928e3f

I can't see what is wrong. Anybody else had the same issue?

1 Answer 1

4

I found the problem. Turns out that the registers for my cbuffer(s) were wrong, I used cb# where b# should be used. (I misunderstood what was written here: https://msdn.microsoft.com/en-us/library/windows/desktop/hh447212(v=vs.85).aspx)

Wrong code:

cbuffer LightBuffer : register(cb2) {

Changed to:

cbuffer LightBuffer : register(b2) {
Sign up to request clarification or add additional context in comments.

1 Comment

There is nothing written about this in the docs not understanding why to use the b# syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.