【问题标题】:DirectX HLSL - Constant Buffer empty in Pixel Shader but not Vertex ShaderDirectX HLSL - 像素着色器中的常量缓冲区为空,但顶点着色器中没有
【发布时间】:2015-02-16 20:40:54
【问题描述】:

我正在我的 DirectX 11 项目中实施照明。我遇到的问题是,当我尝试从 Pixel Shader 函数访问 cbuffer 值时,它只是返回 float3(0, 0, 0) 同时当我在 Vertex 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);
}

这是我发送的值 (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);

结果如下: http://i.gyazo.com/357f1ed3ea33e6569ad2346b368cd975.png

结果不乘颜色:http://gyazo.com/b60b385daa94d3373e9552a523928e3f

我看不出有什么问题。其他人有同样的问题吗?

【问题讨论】:

    标签: c++ directx hlsl zero pixel-shader


    【解决方案1】:

    我发现了问题。结果证明我的 cbuffer(s) 的寄存器是错误的,我在应该使用 b# 的地方使用了 cb#。 (我误解了这里写的内容:https://msdn.microsoft.com/en-us/library/windows/desktop/hh447212(v=vs.85).aspx

    错误代码:

    cbuffer LightBuffer : register(cb2) {
    

    改为:

    cbuffer LightBuffer : register(b2) {
    

    【讨论】:

    • 文档中没有任何关于此的内容,不理解为什么要使用 b# 语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多