Search code examples
c++windowswinapidirectxdxgi

Retrieving ID3D11Texture2D data to be sent over network


I am modifying the desktop duplication api sample kindly provided by Microsoft to capture the screen and send updates over the network to my application. I know how to actually send the data; my problem is getting the data from the ID3D11Texture2D object.

ID3D11Texture2D* m_AcquiredDesktopImage;
IDXGIResource* desktopResource = nullptr;
DXGI_OUTDUPL_FRAME_INFO FrameInfo;

// Get new frame
HRESULT hr = m_DeskDupl->AcquireNextFrame(500, &FrameInfo, &desktopResource);

// QI for IDXGIResource
hr = desktopResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void **>(&m_AcquiredDesktopImage));

At this point, I think the screen updates are in m_AcquiredDesktopImage. I need to transmit this data over the wire (as efficiently as possible).

This answer seems to be on the right track, but I'm new to Windows programming, so I need some additional help.

This is the only solution I can imagine utilizing IDXGIObject::GetPrivateData


Solution

  • Private Data are not what you are looking for at all. They are only here to attach custom values to d3d objects.

    Once you have the ID3D11Texture2D object you need to read back the image from, you need to create a second one in the staging memory pool from the ID3D11Device (get the original description, change the pool, and remove the binding).

    Then, you need to use the ID3D11DeviceContext to copy the texture to your staging one using CopyResource. Then you can use the context Map and Unmap api to read the image.