我在将texture2d保存到文件时遇到问题,它总是给我黑色图像。
这是代码:

HRESULT hr = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast< void** >( &g_pSurface ) );
if( g_pSurface )
{
    ID3D11Texture2D* pNewTexture = NULL;

    D3D11_TEXTURE2D_DESC description;
    g_pSurface->GetDesc( &description );
    description.BindFlags = 0;
    description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
    description.Usage = D3D11_USAGE_STAGING;

    HRESULT hr = d3d11Device->CreateTexture2D( &description, NULL, &pNewTexture );
    if( pNewTexture )
    {
        d3d11DevCon->CopyResource( pNewTexture, g_pSurface );

        hr=D3DX11SaveTextureToFileA(d3d11DevCon, pNewTexture, D3DX11_IFF_BMP, "screen.bmp");
        return;
    }
}

我究竟做错了什么?

最佳答案

首先,您需要显式检查返回HRESULTs的所有函数的返回码

HRESULT hr = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ),
    reinterpret_cast< void** >( &g_pSurface ) );
if( SUCCEEDED(hr) )
{
...
    HRESULT hr = d3d11Device->CreateTexture2D( &description, NULL, &pNewTexture );
    if( SUCCEEDED(hr) )

失败的一种可能是CopyResource,它返回void,因此您无法在代码中检测到问题。相反,您需要启用Direct3D DEBUG设备并查找任何ERROR或WARNING消息。

特别是,如果交换链缓冲区是MSAA资源,则将无法获取任何数据。复制之前,您必须明确使用ResolveSubresource。反过来,由于ResolveSubresource返回空值,因此您需要在使用D3D11_FORMAT_SUPPORT_MULTISAMPLE_RESOLVE之前检查格式是否支持D3DX11。这是ScreenGrabDirectX Tool Kit模块中执行此处理的代码:
static HRESULT CaptureTexture( _In_ ID3D11DeviceContext* pContext,
                               _In_ ID3D11Resource* pSource,
                               _Inout_ D3D11_TEXTURE2D_DESC& desc,
                               _Inout_ ComPtr<ID3D11Texture2D>& pStaging )
{
    if ( !pContext || !pSource )
        return E_INVALIDARG;

    D3D11_RESOURCE_DIMENSION resType = D3D11_RESOURCE_DIMENSION_UNKNOWN;
    pSource->GetType( &resType );

    if ( resType != D3D11_RESOURCE_DIMENSION_TEXTURE2D )
        return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );

    ComPtr<ID3D11Texture2D> pTexture;
    HRESULT hr = pSource->QueryInterface( __uuidof(ID3D11Texture2D), reinterpret_cast<void**>( pTexture.GetAddressOf() ) );
    if ( FAILED(hr) )
        return hr;

    assert( pTexture );

    pTexture->GetDesc( &desc );

    ComPtr<ID3D11Device> d3dDevice;
    pContext->GetDevice( d3dDevice.GetAddressOf() );

    if ( desc.SampleDesc.Count > 1 )
    {
        // MSAA content must be resolved before being copied to a staging texture
        desc.SampleDesc.Count = 1;
        desc.SampleDesc.Quality = 0;

        ComPtr<ID3D11Texture2D> pTemp;
        hr = d3dDevice->CreateTexture2D( &desc, 0, pTemp.GetAddressOf() );
        if ( FAILED(hr) )
            return hr;

        assert( pTemp );

        DXGI_FORMAT fmt = EnsureNotTypeless( desc.Format );

        UINT support = 0;
        hr = d3dDevice->CheckFormatSupport( fmt, &support );
        if ( FAILED(hr) )
            return hr;

        if ( !(support & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RESOLVE) )
            return E_FAIL;

        for( UINT item = 0; item < desc.ArraySize; ++item )
        {
            for( UINT level = 0; level < desc.MipLevels; ++level )
            {
                UINT index = D3D11CalcSubresource( level, item, desc.MipLevels );
                pContext->ResolveSubresource( pTemp.Get(), index, pSource, index, fmt );
            }
        }

        desc.BindFlags = 0;
        desc.MiscFlags &= D3D11_RESOURCE_MISC_TEXTURECUBE;
        desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
        desc.Usage = D3D11_USAGE_STAGING;

        hr = d3dDevice->CreateTexture2D( &desc, 0, pStaging.GetAddressOf() );
        if ( FAILED(hr) )
            return hr;

        assert( pStaging );

        pContext->CopyResource( pStaging.Get(), pTemp.Get() );
    }
    else if ( (desc.Usage == D3D11_USAGE_STAGING) && (desc.CPUAccessFlags & D3D11_CPU_ACCESS_READ) )
    {
        // Handle case where the source is already a staging texture we can use directly
        pStaging = pTexture;
    }
    else
    {
        // Otherwise, create a staging texture from the non-MSAA source
        desc.BindFlags = 0;
        desc.MiscFlags &= D3D11_RESOURCE_MISC_TEXTURECUBE;
        desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
        desc.Usage = D3D11_USAGE_STAGING;

        hr = d3dDevice->CreateTexture2D( &desc, 0, pStaging.GetAddressOf() );
        if ( FAILED(hr) )
            return hr;

        assert( pStaging );

        pContext->CopyResource( pStaging.Get(), pSource );
    }



除了MSAA问题外,您可能还会遇到D3DX11选择WIC格式的问题。根据您的渲染目标格式和渲染,它可能会写出全为Alpha channel 的图像,这可能会导致输出图像“空白”。 DirectX工具包的ScreenGrab模块使您能够显式指定输出格式,并且由于这个原因,默认情况下默认尝试使用非alpha输出文件格式。

不使用旧版DXGI_FORMAT_B8G8R8A8_UNORM的另一个原因:从未针对DXGI 1.1格式进行过更新,因此即使基础WIC容器文件格式支持BGI格式的资源,例如DXGI_FORMAT_B8G8R8A8_UNORMDXGI_FORMAT_B8G8R8A8_UNORM,它也不支持写出它们。如果上面代码中的渲染目标是DXGI_FORMAT_R8G8B8A8_UNORM而不是D3DX11,则D3DX11将失败,而ScreenGrab可以正常工作。

我是否提到过ojit_code太老了,自2009年以来没有对其进行任何修复?

这是ScreenGrab的一些用法示例:
ComPtr<ID3D11Texture2D> backBufferTex;
hr = swapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&backBufferTex);
if ( SUCCEEDED(hr) )
{
    // Write out the render target as a PNG
    hr = SaveWICTextureToFile( context.Get(), backBufferTex.Get(), GUID_ContainerFormatPng, L"SCREENSHOT.PNG");

    // Write out the render target as JPG
    hr = SaveWICTextureToFile( context.Get(), backBufferTex.Get(), GUID_ContainerFormatJpeg, L"SCREENSHOT.JPG" );

    // Write out the render target as BMP
    hr = SaveWICTextureToFile( context.Get(), backBufferTex.Get(), GUID_ContainerFormatBmp, L"SCREENSHOT.BMP" );

    // Write out the render target as BMP and explicitly use a 16-bit format
    hr = SaveWICTextureToFile( context.Get(), backBufferTex.Get(), GUID_ContainerFormatBmp, L"SCREENSHOT.BMP", &GUID_WICPixelFormat16bppBGR565 );

    // Write out the render target as a TIF
    hr = SaveWICTextureToFile( context.Get(), backBufferTex.Get(), GUID_ContainerFormatTiff, L"SCREENSHOT.TIF" );

    // Write out the render target as a TIF with explicit WIC codec properties
    hr = SaveWICTextureToFile( context.Get(), backBufferTex.Get(), GUID_ContainerFormatTiff, L"SCREENSHOT.TIF", nullptr,
                                [&](IPropertyBag2* props)
                                {
                                    PROPBAG2 options[2] = { 0, 0 };
                                    options[0].pstrName = L"CompressionQuality";
                                    options[1].pstrName = L"TiffCompressionMethod";

                                    VARIANT varValues[2];
                                    varValues[0].vt = VT_R4;
                                    varValues[0].fltVal = 0.75f;

                                    varValues[1].vt = VT_UI1;
                                    varValues[1].bVal = WICTiffCompressionNone;

                                    (void)props->Write( 2, options, varValues );
                                });

    // Write out the render target as a DDS
    hr = SaveDDSTextureToFile( context.Get(), backBufferTex.Get(), L"SCREENSHOT.DDS" );
}

08-18 04:22