从ID3D11Texture2D C ++获取像素缓冲区

时间:2017-05-04 08:33:23

标签: c++ windows directx textures

我想从ID3D11Texture2D中提取像素。 我有这个功能:

DUPL_RETURN DISPLAYMANAGER::CopyDirty(_In_ ID3D11Texture2D* SrcSurface, _Inout_ ID3D11Texture2D* SharedSurf, _In_reads_(DirtyCount) RECT* DirtyBuffer, UINT DirtyCount, INT OffsetX, INT OffsetY, _In_ DXGI_OUTPUT_DESC* DeskDesc)

来自桌面复制示例https://code.msdn.microsoft.com/windowsdesktop/Desktop-Duplication-Sample-da4c696a/sourcecode?fileId=42782&pathId=1384140008

这是我的代码,返回的缓冲区充满了'\ 0' ......

     BYTE* DISPLAYMANAGER::GetImageData(ID3D11Device* device, ID3D11DeviceContext* context, ID3D11Texture2D* texture2D, D3D11_TEXTURE2D_DESC Desc)
     {
 if (texture2D != NULL)
 {
    D3D11_TEXTURE2D_DESC description;
    texture2D->GetDesc(&description);
    description.BindFlags = 0;
    description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
    description.Usage = D3D11_USAGE_STAGING;

    ID3D11Texture2D* texTemp = NULL;

    HRESULT hr = device->CreateTexture2D(&description, NULL, &texTemp);
    if (FAILED(hr))
    {
        if (texTemp)
        {
            texTemp->Release();
            texTemp = NULL;
        }
        return NULL;
    }
    context->CopyResource(texTemp, texture2D);

    D3D11_MAPPED_SUBRESOURCE  mapped;
    unsigned int subresource = 0;
    hr = context->Map(texTemp, 0, D3D11_MAP_READ, 0, &mapped);
    if (FAILED(hr))
    {
        texTemp->Release();
        texTemp = NULL;
        return NULL;
    }

    Desc.Width = description.Width;
    Desc.Height = description.Height;
    const int pitch = mapped.RowPitch;
    BYTE* source = (BYTE*)(mapped.pData);
    BYTE* dest = new BYTE[(Desc.Width)*(Desc.Height) * 4];
    BYTE* destTemp = dest;
    for (int i = 0; i < Desc.Height; ++i)
    {
        memcpy(destTemp, source, Desc.Width * 4);
        source += pitch;
        destTemp += Desc.Width * 4;
    }
    context->Unmap(texTemp, 0);
    return dest;
}
else
    return NULL;

}

我在CopyDirty(_In_ ID3D11Texture2D* SrcSurface, _Inout_ ID3D11Texture2D* SharedSurf, _In_reads_(DirtyCount) RECT* DirtyBuffer, UINT DirtyCount, INT OffsetX, INT OffsetY, _In_ DXGI_OUTPUT_DESC* DeskDesc, D3D11_TEXTURE2D_DESC Desc)

中传递了此功能 像这样:

 ...
 m_DeviceContext->Draw(NUMVERTICES * DirtyCount, 0);

BYTE *Bytes = GetImageData(m_Device, m_DeviceContext, SrcSurface, Desc);

谢谢大家

0 个答案:

没有答案