locked
Alpha Blending

    Question

  • I've downloaded the C++ SpriteBatch sample.  It didn't support animated sprites at first, so I've changed it to support them.  Unfortunately, I can't seem to get alpha blending working now.  I took some graphics logs:

    I can't seem to understand why the pixels behind the grasshopper aren't being blended.
    Here's the blend state:

    Blend State 11

    General Information

    Blend State
    Alpha To Coverage Enable FALSE
    Independent Blend Enable FALSE

    Render Targets

    Render Target Number Blend Enable Blend Source Blend Destination Blend Op Alpha Blend Source Alpha Blend Dest Alpha Blend Op Render Target Write Mask
    0 TRUE D3D11_BLEND_SRC_ALPHA D3D11_BLEND_INV_SRC_ALPHA D3D11_BLEND_OP_ADD D3D11_BLEND_ONE D3D11_BLEND_ONE D3D11_BLEND_OP_ADD 00001111
    1 TRUE D3D11_BLEND_SRC_ALPHA D3D11_BLEND_INV_SRC_ALPHA D3D11_BLEND_OP_ADD D3D11_BLEND_ONE D3D11_BLEND_ONE D3D11_BLEND_OP_ADD 00001111
    2 TRUE D3D11_BLEND_SRC_ALPHA D3D11_BLEND_INV_SRC_ALPHA D3D11_BLEND_OP_ADD D3D11_BLEND_ONE D3D11_BLEND_ONE D3D11_BLEND_OP_ADD 00001111
    3 TRUE D3D11_BLEND_SRC_ALPHA D3D11_BLEND_INV_SRC_ALPHA D3D11_BLEND_OP_ADD D3D11_BLEND_ONE D3D11_BLEND_ONE D3D11_BLEND_OP_ADD 00001111
    4 TRUE D3D11_BLEND_SRC_ALPHA D3D11_BLEND_INV_SRC_ALPHA D3D11_BLEND_OP_ADD D3D11_BLEND_ONE D3D11_BLEND_ONE D3D11_BLEND_OP_ADD 00001111
    5 TRUE D3D11_BLEND_SRC_ALPHA D3D11_BLEND_INV_SRC_ALPHA D3D11_BLEND_OP_ADD D3D11_BLEND_ONE D3D11_BLEND_ONE D3D11_BLEND_OP_ADD 00001111
    6 TRUE D3D11_BLEND_SRC_ALPHA D3D11_BLEND_INV_SRC_ALPHA D3D11_BLEND_OP_ADD D3D11_BLEND_ONE D3D11_BLEND_ONE D3D11_BLEND_OP_ADD 00001111
    7 TRUE D3D11_BLEND_SRC_ALPHA D3D11_BLEND_INV_SRC_ALPHA D3D11_BLEND_OP_ADD D3D11_BLEND_ONE D3D11_BLEND_ONE D3D11_BLEND_OP_ADD 00001111

     

     Here's the output merger state:

    Output Merger State

    Depth Stencil Description

    Depth Stencil State NULL
    Depth Enable TRUE
    Depth Function D3D11_COMPARISON_LESS
    Depth Write Mask D3D11_DEPTH_WRITE_MASK_ALL
    Stencil Enable FALSE
    Stencil Read Mask 11111111
    Stencil Write Mask 11111111
    Stencil Reference Value 0

    Front Face Stencil

    Fail Op D3D11_STENCIL_OP_KEEP
    Depth Fail Op D3D11_STENCIL_OP_KEEP
    Pass Op D3D11_STENCIL_OP_KEEP
    Function D3D11_COMPARISON_ALWAYS

    Back Face Stencil

    Fail Op D3D11_STENCIL_OP_KEEP
    Depth Fail Op D3D11_STENCIL_OP_KEEP
    Pass Op D3D11_STENCIL_OP_KEEP
    Function D3D11_COMPARISON_ALWAYS

     

     Any help?

     

     

     

     

     

    Friday, July 27, 2012 1:58 PM

All replies

  • Hello,

    Would you please provide us the whole codes to reproduce this issue,

    You can upload your project to skydriver
    http://skydrive.live.com/

    It is not necessary that you send out the whole of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business details from it.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 30, 2012 8:41 AM
    Moderator
  • I'd look at the format and data in the grasshopper texture. Does it have an alpha channel? Is it a .DDS file or some WIC-based format?
    Monday, July 30, 2012 8:02 PM
  • I've viewed it with Visual Studio Image viewer and it does have an alpha channel.  It's a .DDS converted from .png.

     

    Here's where I change the blend state:

    // Create the blend states.
        D3D11_BLEND_DESC1 blendDesc;
        ZeroMemory(&blendDesc, sizeof(blendDesc));
        blendDesc.AlphaToCoverageEnable = false;
        blendDesc.IndependentBlendEnable = false;
        blendDesc.RenderTarget[0].BlendEnable = true;
        blendDesc.RenderTarget[0].LogicOpEnable = false;
        blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
        blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
        blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
        blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
        blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
        blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
        blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

    For some reason, after I change AlphaToCoverageEnable to true, alpha blending begins working.  But, there seems to be fuzzy edges on the images.

     I'm sorry, but I'd rather not upload it unless absolutely necessary.
    Tuesday, July 31, 2012 9:29 PM
  • Try:

    // Create the blend states.
        D3D11_BLEND_DESC blendDesc;
        ZeroMemory(&blendDesc, sizeof(blendDesc));
    
        blendDesc.RenderTarget.BlendEnable = true;
        blendDesc.RenderTarget.LogicOpEnable = false;
        blendDesc.RenderTarget.SrcBlend = D3D11_BLEND_ONE;
        blendDesc.RenderTarget.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
        blendDesc.RenderTarget.BlendOp = D3D11_BLEND_OP_ADD;
        blendDesc.RenderTarget.SrcBlendAlpha = D3D11_BLEND_ONE;
        blendDesc.RenderTarget.DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
        blendDesc.RenderTarget.BlendOpAlpha = D3D11_BLEND_OP_ADD;
        blendDesc.RenderTarget.RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
    
        CreateBlendState( &blendDesc, ...);
    

    Tuesday, July 31, 2012 11:50 PM
  • That turned the background behind the grasshopper white.
    Wednesday, August 01, 2012 11:33 AM