'How to transform a ID3D11Texture2D into any picture format

I have a ID3D11Texture2D and want to write it to disk using literally any picture format (png, bmp, jpeg, ...). I have already tried to read the docs https://docs.microsoft.com/en-us/windows/win32/api/d3d11/nn-d3d11-id3d11texture2d, which are less than helpful, and i have found an NVIDIA tutorial of how to take individual ID3D11Texture2D and convert them into a video: https://github.com/NVIDIA/video-sdk-samples/tree/master/nvEncDXGIOutputDuplicationSample

However, I dont find anything how to simply write it to disk in any format. I'm sure I'm missing something obvious, any hint would be appreciated.

To experiment, I used https://github.com/NVIDIA/video-sdk-samples/tree/master/nvEncDXGIOutputDuplicationSample, set the frames to capture to 1, and try to write the ID3D11Texture2D to file before encoding to video.



Solution 1:[1]

I have a solution for exactly this in the ScreenGrab module which captures a texture (if not already in Map-supporting memory), and then writes it out as a picture using WIC. It handles some edge-cases like "typeless" resources and MSAA as well.

The 'standalone' version is on GitHub here as part of the DirectXTex package:

https://github.com/microsoft/DirectXTex/blob/main/ScreenGrab/ScreenGrab11.h

https://github.com/microsoft/DirectXTex/blob/main/ScreenGrab/ScreenGrab11.cpp

Documentation is here.

ScreenGrab is also included in the DirectX Tool Kit for DX11 and DX12. There's also a basic DX9 version in the DirectXTex package as well.

ScreenGrab can write to any file container supported by WIC. It also has buit-in support for writing DDS files directly without using WIC.

In addition to using ScreenGrab (which is intended as a light-weight screenshot solution), you can also use the DirectTex library to capture a texture and then save it to DDS, HDR, TGA, or any WIC-supported file format.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1