Search code examples
c#shaderdirectxdirectx-11directx-9

Some properties are missing in the DirectX 11 compared to the 9th


At the moment, I am porting the old code from the ninth version of DirectX to the newer, eleventh, and I have encountered some difficulties in finding analogues of existing properties and functions.

Display options in the old version (DirectX9):

technique tAccessory
{
    pass p0
    {
        VS_COMPILE_COMMAND VS();
        PS_COMPILE_COMMAND PS(true);
        
        AlphaBlendEnable    = true;
        SrcBlend            = SrcAlpha;
        DestBlend           = InvSrcAlpha;
        ZFunc               = LESSEQUAL;
        AlphaTestEnable     = true;
        AlphaRef            = 1;
        AlphaFunc           = GreaterEqual;
    }

    pass p1
    {
        VS_COMPILE_COMMAND VS();
        PS_COMPILE_COMMAND PSAccessoryAlpha();
        
        AlphaBlendEnable    = true;
        SrcBlend            = SrcAlpha;
        DestBlend           = One;
        ZFunc               = LESSEQUAL;
        AlphaTestEnable     = true;
        AlphaRef            = 1;
        AlphaFunc           = GreaterEqual;
    }
}

The VS, PS, PSAccessoryAlpha functions are self-written, determine the position of textures and alpha. The problem lies in the absence of these parameters in the new version of DirectX 11:

AlphaBlendEnable    = true;
SrcBlend            = SrcAlpha;
DestBlend           = One;
ZFunc               = LESSEQUAL;
AlphaTestEnable     = true;
AlphaRef            = 1;
AlphaFunc           = GreaterEqual;

I managed to find out that the following parameters can be set using the SetBlendState function:

DirectX9:

AlphaBlendEnable    = true;
SrcBlend            = SrcAlpha;
DestBlend           = InvSrcAlpha;

DirectX11:

BlendState BSAlphaBlending
{
    BlendEnable[0] = true;
    SrcBlend  = SRC_ALPHA;
    DestBlend = INV_SRC_ALPHA;
};
SetBlendState( BSAlphaBlending, float4( 0, 0, 0, 0 ), 0xffffffff );

And for the case when DestBlend = One:

DirectX9:

AlphaBlendEnable    = true;
SrcBlend            = SrcAlpha;
DestBlend           = One;

DirectX11:

BlendState BSSrcAlphaDestOne
{
    BlendEnable[0]  = true;
    SrcBlend        = SRC_ALPHA;
    DestBlend       = ONE;
};
SetBlendState( BSSrcAlphaDestOne, float4( 0, 0, 0, 0 ), 0xffffffff );

But I haven't been able to figure out how to port to DirectX11 the rest of the parameters yet, such as:

DirectX9:

ZFunc               = LESSEQUAL;
AlphaTestEnable     = true;
AlphaRef            = 1;
AlphaFunc           = GreaterEqual;

In version 9, there was a D3DRENDERSTATETYPE enum for them and it was possible to specify the necessary parameters using the SetRenderState function, but what is the analogue for this function in version 11?


Solution

  • Firstly, you are using the legacy Effects (FX) system so be sure to look at GitHub for the latest version and limitations.

    For Direct3D 11, you need to figure out your state combination permutations and create state objects for them (you have 5 objects to set, not a hundred little individual switches). ZFunc for example is part of the DepthStencilState object.

    Note that the 'alpha-test' feature of the legacy fixed-function pipeline does not exist in the programmable shader pipeline. You can use the HLSL shader code in AlphaTestEffect.fx to implement it.

    See Microsoft Docs and these C++ Samples. Most of the structural changes you are dealing were true for Direct3D 10 as well.