1
\$\begingroup\$

I am testing rendering with alpha blending state according to this guide. The aim is to add snow on terrain grass texture. Finally, I got the wrong result -- the blue color fillings up all alpha = 0 pixels.

I checked the instruction step triple time, can't find any mistakes.

Could someone explain to me what's is wrong with my code?

PS and VS:

SnowPSIn SnowVSMain(SnowVSIn Input)
{
    SnowPSIn Output;
    float fY = g_txHeightMap.SampleLevel(g_samLinear, Input.vTexCoord, 0).a * g_fHeightScale;
    float4 vWorldPos = float4(Input.vPos + float3(0.0, fY, 0.0), 1.0);
    Output.vPos = mul(vWorldPos, g_mViewProj);
    Output.vTexCoord.xy = Input.vTexCoord;
    Output.vTexCoord.z = FogValue(length(vWorldPos - g_mInvCamView[3].xyz));
    Output.vTexCoord.w = length(vWorldPos - g_mInvCamView[3].xyz);
    //Output.vShadowPos = mul(vWorldPos, g_mLightViewProj);

    return Output;
}

float4 SnowPSMain(SnowPSIn Input) : SV_Target
{
    float4 vSnowColor = g_txTerrSnow.Sample(g_samLinear, Input.vTexCoord.xy * 64);
    return vSnowColor;
}

BlendState:

BlendState SrcAlphaBlendingAdd
{
    BlendEnable[0] = TRUE;
    SrcBlend = SRC_ALPHA;
    DestBlend = INV_SRC_ALPHA;
    BlendOp = ADD;
    SrcBlendAlpha = ZERO;
    DestBlendAlpha = ZERO;
    BlendOpAlpha = ADD;
    RenderTargetWriteMask[0] = 0x0F;
};

Pass:

pass RenderShowPass
{
    SetVertexShader(CompileShader(vs_5_0, SnowVSMain()));
    SetGeometryShader(NULL);
    SetPixelShader(CompileShader(ps_4_0, SnowPSMain()));
    SetBlendState(SrcAlphaBlendingAdd, float4(0.0f, 0.0f, 0.0f, 0.0f), 0xFFFFFFFF);
}

all sources

Before: initial scene

After: final scene

\$\endgroup\$
6
  • \$\begingroup\$ What format is your snow texture stored in? \$\endgroup\$
    – DMGregory
    Mar 20 '21 at 12:14
  • \$\begingroup\$ It's .dds texture. I created it using GIMP with the following params: BC3 / DXT5 compression and generated mitmaps with default parameters. src here \$\endgroup\$ Mar 20 '21 at 13:16
  • \$\begingroup\$ Are you flattening the image prior to are as part exporting to .dds? \$\endgroup\$
    – Pikalek
    Mar 20 '21 at 15:29
  • \$\begingroup\$ I did not find any flattening option while saving in GIMP. Even more, I attempted NVIDIA Texture Tools with default options and got the same result with blue color instead of alpha channel. I think my texture is OK \$\endgroup\$ Mar 20 '21 at 16:44
  • \$\begingroup\$ @DmitriiKozlov I had read of some issues w/ GIMP do odd things to the channels when exporting .dds files. But if the Nvidia tool texture gives the same result, you're probably correct that the problem is in the code & not the file. \$\endgroup\$
    – Pikalek
    Mar 20 '21 at 20:49

You must log in to answer this question.

Browse other questions tagged .