Search code examples
c++direct2ddirect3d11dxgichromakey

ChromaKey in DirectX


I need implement some chroma key functionality with Direct2d.

I have some code example from MSDN:

ComPtr<ID2D1Effect> chromakeyEffect;
m_d2dContext->CreateEffect(CLSID_D2D1ChromaKey, &chromakeyEffect);

chromakeyEffect->SetInput(0, bitmap);
chromaKeyEffect->SetValue(D2D1_CHROMAKEY_PROP_COLOR, {0.0f, 1.0f, 0.0f, 0.0f});
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_TOLERANCE, 0.2f);
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_INVERT_ALPHA, false);
chromakeyEffect->SetValue(D2D1_CHROMAKEY_PROP_FEATHER, false);

m_d2dContext->BeginDraw();
m_d2dContext->DrawImage(chromakeyEffect.Get());
m_d2dContext->EndDraw();

How ever, I'm little bit new in WinAPI.
Our platform have function for yield instance of ID3D11Device and I receive DX11Texture as an input.
I have two questions:
1. How can I get m_d2dContext from ID3D11Device?
2. How can I get ID2D1Image from DX11Texture?


Solution

    1. How can I get m_d2dContext from ID3D11Device?

    You cannot, not at least directly.

    You create a D2D factory, and from there you create a render target on top of DXGI resource, which in turn derives from certain D3D11 texture and as such belongs to certain D3D11 device. With all this together you use D2D to draw over specific D3D11 texture.

    1. How can I get ID2D1Image from DX11Texture?

    The entry point into MSDN documentation for your case is this: Direct2D and Direct3D Interoperability Overview. Specififcally for the D3D texture to D2D image scenario the suggested path is this:

    There are two primary ways to use Direct2D and Direct3D together:

    [...]

    By using CreateSharedBitmap to create an ID2D1Bitmap from an IDXGISurface, you can write a Direct3D scene to a bitmap and render it with Direct2D.