You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

D3D12添加常量缓冲区后worldPos沿X轴无变化问题排查

D3D12常量缓冲区引发worldPos异常问题
  • 添加常量缓冲区后,Output结构体传递出现异常,worldPos沿X轴无变化,导致画面出现光带问题
  • 基于worldPos的像素着色效果不符合预期,worldPos错误依赖对象坐标而非像素坐标(效果对比见附图)
  • 不在C++代码中添加常量缓冲区时,worldPos表现完全正常

顶点着色器代码

cbuffer rot : register(b0)
{
    matrix modelViewProj;
    matrix model;
};

struct Output
{
    float3 worldPos : Position;
    float3 n : Normal;
    float4 pos : SV_Position;
    float2 tc : Texcoord;
};

Output main(float3 pos : Position, float3 n : Normal, float2 tc : Texcoord)
{
    Output vertexOut;

    vertexOut.worldPos = (float3)mul(float4(pos, 1.0f), model);
    vertexOut.pos = mul(float4(pos, 1.0f), modelViewProj);
    vertexOut.n = mul(n, (float3x3) model);
    vertexOut.tc = tc;
    
    return vertexOut;
}

像素着色器代码

struct ObjectCBuf
{
    float3 materialColor;
};

cbuffer LightCBuf : register(b1)
{
    float3 lightPos;
    float3 ambient;
    float3 diffuseColor;
    float diffuseIntensity;
    float attConst;
    float attLin;
    float attQuad;
    ObjectCBuf object;
};

float4 main(float3 worldPos : Position, float3 n : Normal, float4 pos : SV_Position) : SV_TARGET
{
    const float3 vToL = lightPos - worldPos;
    const float distToL = length(vToL);
    const float3 dirToL = vToL / distToL;
    // diffuse attenuation
    const float att = 1.0f / (attConst + attLin * distToL + attQuad * (distToL * distToL));
    // diffuse intensity
    const float3 diffuse = diffuseColor * diffuseIntensity * att * max(0.0f, dot(dirToL, n));
    // final color
    //return float4(object.materialColor, 1.0);
    return float4(saturate((diffuse + ambient) * object.materialColor), 1.0f);
}

内容的提问来源于stack exchange,提问作者voltik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 08:09:57