简体   繁体   English

DXGI屏幕捕获扭曲的图像

[英]DXGI Screen capture distorted image

Not only that the fps drops form 60 to 20-21 but the image also looks distorted like this. 不仅fps从60下降到20-21,而且图像看起来也像这样失真。 Second image is what it should look like 第二张图片应该是什么样子

What it looks like What it should look like 它看起来像 什么

if (captureVideo == 1) {
    pNewTexture = NULL;

    // Use the IDXGISwapChain::GetBuffer API to retrieve a swap chain surface ( use the uuid  ID3D11Texture2D for the result type ).
    pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast< void** >( &pSurface ) ); 

    /* The swap chain buffers are not mapable, so I need to copy it to a staging resource. */

    pSurface->GetDesc( &description ); //Use ID3D11Texture2D::GetDesc to retrieve the surface description

    // Patch it with a D3D11_USAGE_STAGING usage and a cpu access flag of D3D11_CPU_ACCESS_READ
    description.BindFlags = 0;
    description.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
    description.Usage = D3D11_USAGE_STAGING;

    // Create a temporary surface ID3D11Device::CreateTexture2D
    HRESULT hr = pDevice->CreateTexture2D( &description, NULL, &pNewTexture );
    if( pNewTexture )
    {
        // Copy to the staging surface ID3D11DeviceContext::CopyResource
        pContext->CopyResource( pNewTexture, pSurface );
        // Now I have a ID3D11Texture2D with the content of your swap chain buffer that allow you to use the ID3D11DeviceContext::Map API to read it on the CPU
        D3D11_MAPPED_SUBRESOURCE resource;
        pContext->Map( pNewTexture, D3D11CalcSubresource( 0, 0, 0), D3D11_MAP_READ, 0, &resource );

        const int pitch = w << 2;
        const unsigned char* source = static_cast< const unsigned char* >( resource.pData );
        unsigned char* dest = static_cast< unsigned char* >(m_lpBits);
        for( int i = 0; i < h; ++i )
        {
            memcpy( dest, source, w * 4 );
            source += pitch;
            dest += pitch;
        }

        AppendNewFrame(w, h, m_lpBits,24);

        pContext->Unmap( pNewTexture, 0);
        pNewTexture->Release();
    }
}

The code snippet even though incomplete shows several potential problems: 即使不完整,该代码段也显示了一些潜在的问题:

  1. Number of 24 in AppendNewFrame line suggests that you are trying to treat the data as 24-bit RGB, and your data is 32-bit RGB. AppendNewFrame行中的24表示您尝试将数据视为24位RGB,而您的数据是32位RGB。 Such mistreatment matches the artifacts exhibited on the attached images; 这种虐待与附件图像上显示的伪像相匹配;
  2. Pitch/stride is taken as assumed default, while you have the effectively used one in D3D11_MAPPED_SUBRESOURCE structure and you should be using it. 当您在D3D11_MAPPED_SUBRESOURCE结构中使用了有效的间距/步幅时,它便被视为默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM