Search code examples
c++direct3ddirect2ddxgi

what are the correct arguments for D2D1Factory::CreateDxgiSurfaceRenderTarget()


i want create direct2D render target that will render on screen with direct3D, so i try to call the D2D1Factory::CreateDxgiSurfaceRenderTarget() funtion to make a direct2D render target, but it keep fails. The error i get from HRESULT is invalid argument-s passed. I tried these 3 codes

FLOAT dpiX;
FLOAT dpiY;
factory->GetDesktopDpi(&dpiX, &dpiY);

D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
    D2D1_RENDER_TARGET_TYPE_DEFAULT,
    D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED),
    dpiX, dpiY
);

hr = factory->CreateDxgiSurfaceRenderTarget(dxgiBackbuffer, &props, &d2dRenderTarget);

if (FAILED(hr)) { //i get the error here }
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
    D2D1_RENDER_TARGET_TYPE_DEFAULT,
    D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED)
);

hr = factory->CreateDxgiSurfaceRenderTarget(dxgiBackbuffer, &props, &d2dRenderTarget);

if (FAILED(hr)) { //i get the error here }
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties();

hr = factory->CreateDxgiSurfaceRenderTarget(dxgiBackbuffer, &props, &d2dRenderTarget);

if (FAILED(hr)) { //i get the error here }

i assume the error coming from D2D1_RENDER_TARGET_PROPERTIES, but what are the correct arguments to make it work?

here is how i get the dxgiBackbuffer

IDXGISurface* dxgiBackbuffer;
hr = swapchain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackbuffer));

i get the swapchain from the main application that running direct3D, so i will inject a DLL that will run direct2D

In general this code is correct, i tested it on a direct3D project i made and it worked fine, but with this application that i try to inject the DLL it looks like it has something special, like some custom rendering properties? So in this case how can i get the correct properties?


Solution

  • After some research and as i haven't access to main application and also wasn't able to attach debugger, i finally found the issue. According to microsoft, when you call D3D11CreateDeviceAndSwapChain to create the D3D device, the flag D3D11_CREATE_DEVICE_BGRA_SUPPORT required for Direct2D interoperability with Direct3D resources.

    https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_create_device_flag

    The application i try to hook into create its D3D device with flag 0.