Search code examples
c++shaderhlsldirectx-12

dx12) Shader Constants goes wrong


enter image description here

enter image description here

As you can see, in PIX, Pipeline says there's right values on GPU, but when I debug Pixels, it uses wrong values without gZSplits[0].

And I Upload Values to GPU like this, and mZSplits is std::vector:

for (int i = 1; i < mMapCount + 1; ++i)
{
    cmdList->SetGraphicsRoot32BitConstants(7, 1, &mZSplits[i], i - 1);
}

this is my shader codes.

cbuffer ShadowCB : register(b5)
{
    float gZSplits[3] : packoffset(c0);
}

float4 PS(DsOut din) : SV_Target
{
    float4 result = 0.0f;
    
    if(gKeyInput)
    {
        if (din.Tessellation.w <= 5.0f) 
            result = float4(1.0f, 0.0f, 0.0f, 1.0f);
        else if (din.Tessellation.w <= 10.0f) 
            result = float4(0.0f, 1.0f, 0.0f, 1.0f);
        else if (din.Tessellation.w <= 20.0f)
            result = float4(0.0f, 0.0f, 1.0f, 1.0f);
        else if (din.Tessellation.w <= 30.0f)
            result = float4(1.0f, 1.0f, 0.0f, 1.0f);
        else if (din.Tessellation.w <= 40.0f)
            result = float4(1.0f, 0.0f, 1.0f, 1.0f);
        else if (din.Tessellation.w <= 50.0f)
            result = float4(0.0f, 1.0f, 1.0f, 1.0f);
        else if (din.Tessellation.w <= 60.0f)
            result = float4(1.0f, 1.0f, 1.0f, 1.0f);
        else
            result = float4(1.0f, 0.5f, 0.5f, 1.0f);
    }
    else
    {
        int idx = -1;
        float4 PosV = mul(float4(din.PosW, 1.0f), gView);

        for (int j = 2; j >= 0; j--)
        {
            float zSplits = gZSplits[j];
            if (PosV.z < zSplits)
            {
                idx = j;
            }
        }
        float4 PosS = mul(float4(din.PosW, 1.0f), gShadowTransform[idx]);

        float4 diffuse = 0.0f;
        
        float4 baseTexDiffuse = gBaseTexture.Sample(gAnisotropicWrap, din.TexCoord0) * gMat.Diffuse;
        float4 detailedTexDiffuse = gDetailedTexture.Sample(gAnisotropicWrap, din.TexCoord1) * gMat.Diffuse;
        float4 roadTexDiffuse = gRoadTexture.Sample(gAnisotropicWrap, din.TexCoord0) * gMat.Diffuse;
    
        if (roadTexDiffuse.a < 0.4f)
        {
            diffuse = saturate(baseTexDiffuse * 0.6f + detailedTexDiffuse * 0.4f);
        }
        else
        {
            diffuse = roadTexDiffuse;
        }
        
        float3 view = normalize(gCameraPos - din.PosW);
        float4 ambient = gAmbient * float4(gMat.Ambient, 1.0f) * diffuse;

        float shadowFactor[3] = { 1.0f, 1.0f, 1.0f };
        for (int i = 0; i < 3; i++)
            shadowFactor[i] = CalcShadowFactor(PosS, idx);
        
        float4 directLight;
        float shadowFactorOut[3] = { 1.0f, 1.0f, 1.0f };
        if (PosS.x < 0.0f || PosS.x > 1.0f || PosS.z < 0.0f || PosS.z > 1.0f || PosS.y < 0.0f || PosS.y > 1.0f)
            directLight = ComputeLighting(gLights, gMat, normalize(din.NormalW), view, shadowFactorOut);
        else
        {
            directLight = ComputeLighting(gLights, gMat, normalize(din.NormalW), view, shadowFactor);
        }

        result = ambient + directLight;
        result.a = gMat.Diffuse.a;

        float4 debugColor = { 1.0f, 1.0f, 1.0f, 1.0f };
        //for (int k = 2; k >= 0; --k)
        //{
        //    float4 pos = mul(float4(din.PosW, 1.0f), gShadowViewProj[k]);
        //    if (pos.x > 0.0f && pos.x < 1.0f && pos.z > 0.0f && pos.z < 1.0f && pos.y > 0.0f && pos.y < 1.0f)
        //    {
        //        if (k == 2)
        //        {
        //            debugColor = float4(1.0f, 0.0f, 0.0f, 1.0f);
        //        }
        //        if (k == 1)
        //        {
        //            debugColor = float4(0.0f, 1.0f, 0.0f, 1.0f);
        //        }
        //        if (k == 0)
        //        {
        //            debugColor = float4(0.0f, 0.0f, 1.0f, 1.0f);
        //        }
        //    }
        //}
        //result *= debugColor;

        if (idx == 2)
        {
            debugColor = float4(1.0f, 0.0f, 0.0f, 1.0f);
        }
        else if (idx == 1)
        {
            debugColor = float4(0.0f, 1.0f, 0.0f, 1.0f);
        }
        else if (idx == 0)
        {
            debugColor = float4(0.0f, 0.0f, 1.0f, 1.0f);
        }
        result *= debugColor;
    }

    return result;
}

I've checked root signature or any other options, but i can't find anything. maybe shader offset might be the thing?


Solution

  • As per the packing rules for arrays in constant buffers :

    cbuffer ShadowCB : register(b5)
    {
        float gZSplits[3] : packoffset(c0);
    }
    

    Is not 3 floats (size 12 bytes), so the representation is not

    cbuffer ShadowCB : register(b5)
    {
        float gZSplits0;
        float gZSplits1;
        float gZSplits2;
    };
    

    It is actually expanded to:

    cbuffer ShadowCB : register(b5)
    {
        float gZSplits0;
        float3 pad0;
        float gZSplits1;
        float3 pad0;
        float gZSplits2;
    };
    

    So you need to make sure that your cpu side buffer is actually matching that layout.

    You can double check that ShadowCB size is indeed 36 bytes by checking disassembly.