c++directxdirect3ddirect2ddirect3d9

Direct2D how to open shared texture


I have a code, which uses Direct9Ex like:

res = Device->CreateTexture(1920, 1080, 1, 1, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &Texture, &texture_shared_handle);

How can i open that texture for read in Direct2D using this texture_shared_handle?

I found function ID2D1RenderTarget::CreateSharedBitmap, but can't find any working code that can open shared DXGI resources. Also, texture is valid for sharing, it has only one level and A8R8G8B8 mode. Also, i forced to use direct9ex for texture creation

Another solution i thinking of is to create Direct9Ex surface on same device as above(which creates texture ) with shared handle, like this:

res = Device->CreateOffscreenPlainSurface(1920, 1080, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &MagSurface2, &Surface2_shared_handle);
res = Texture->GetSurfaceLevel(0, &MagSurface1);

...
// copying data from texture level surface to shared surface
res = Device->GetRenderTargetData(MagSurface1, MagSurface2);

But then, how to pass this Surface2_shared_handle to Direct2D?

I also tried to get IDXGISurface to then pass it to Direct2D, but, i always fails:

// Direct9Ex texture
IDXGISurface* pDxgiSurface = NULL;
            res = Texture->QueryInterface(__uuidof(IDXGISurface), (void**)&pDxgiSurface);
res is always E_NOINTERFACE!

Thanks


Solution

  • In order to obtain an IDXGISurface, you need to open the shared texture with your DirectX 11 device (the one you used to initialize Direct2D) using this function. You can call it to get an IDXGIResource which you can QueryInterface for IDXGISurface and use it in your Direct2D. You may also need to implement some synchronization such as the one explained here.