I am currently creating a game using Direct2D. For rendering, I want to use D2D's device context because it allows for more control and effects etc.
I have created the HWND swapchain as usual and it works. I have already tried Alpha Blending using semi-transparent rectangles partly overlapping, and it seems to work. However, when I create the swapchain, I specify DXGI_ALPHA_MODE_UNSPECIFIED
as its AlphaMode, but D2D1_ALPHA_MODE_PREMULTIPLIED
when creating the Direct2D render-target (a bitmap in this case). Changing DXGI's alpha mode does not seem to change anything apart from failing the function when specifying DXGI_ALPHA_MODE_PREMULTILPLIED
.
What does the AlphaMode of DXGI do when using a DXGI swapchain for Direct2D (and only Direct2D) rendering? Can I expect it to work without problems even though the AlphaModes for DXGI and D2D are not exactly the same?
As I said, it does work so far. But I am afraid it may introduce problems later as I get into more complex things such as per-pixel alpha bitmaps, and effects.
Here are the structs used to initialize the swapchain and D2D's device context (some, for this question, irrelevant code has been omitted):
static D3D_FEATURE_LEVEL const eaD3DFeatureLevels[] = {
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
DXGI_SWAP_CHAIN_DESC1 sDXGISwapChainDesc = {
.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED,
.Width = 0,
.Height = 0,
.Flags = 0,
.BufferCount = 2,
.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT,
.Format = DXGI_FORMAT_B8G8R8A8_UNORM,
.Scaling = DXGI_SCALING_STRETCH,
.Stereo = FALSE,
.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL,
.SampleDesc = {
.Count = 1,
.Quality = 0
}
};
D2D1_BITMAP_PROPERTIES1 sD2DBitmapProps = {
.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
.pixelFormat = {
.format = DXGI_FORMAT_B8G8R8A8_UNORM,
.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED
}
};
[...]
gl_sApplication.sRenderer->sD2DRenderer.sDXGIFactory->lpVtbl->CreateSwapChainForHwnd(
gl_sApplication.sRenderer->sD2DRenderer.sDXGIFactory,
(IUnknown *)gl_sApplication.sRenderer->sD2DRenderer.sD3D11Device,
hwRenderWindow,
&sDXGISwapChainDesc,
NULL,
NULL,
&gl_sApplication.sRenderer->sD2DRenderer.sDXGISwapchain
);
D2DWr_DeviceContext_CreateBitmapFromDxgiSurface( /* wrapper function to expose Direct2D's C++ interface to C */
gl_sApplication.sRenderer->sD2DRenderer.sD2DDevContext,
gl_sApplication.sRenderer->sD2DRenderer.sDXGIBackbuffer,
&sD2DBitmapProps,
&gl_sApplication.sRenderer->sD2DRenderer.sBitmap
);