使用DirectXTK获取原始像素数据

时间:2017-05-08 22:10:04

标签: c++ windows directx directxtk

我想从DXGI调用AcquireNextFrame后从Dirty Rect访问像素缓冲区数据。

这是我从D3D11Texture2D获取像素缓冲区数据的代码:

BYTE* DISPLAYMANAGER::GetImageData(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;
    description.Format = DXGI_FORMAT_B8G8R8A8_UNORM;

    ID3D11Texture2D* texTemp = NULL;
    HRESULT hr = m_Device->CreateTexture2D(&description, NULL, &texTemp);
    if (FAILED(hr))
    {
        if (texTemp)
        {
            texTemp->Release();
            texTemp = NULL;
        }
        return NULL;
    }
    m_DeviceContext->CopyResource(texTemp, texture2D);

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

    unsigned char *captureData = new unsigned char[Desc.Width * Desc.Height * 4];
    RtlZeroMemory(captureData, Desc.Width * Desc.Height * 4);
    const int pitch = mapped.RowPitch;
    unsigned char *source = static_cast<unsigned char*>(mapped.pData);
    unsigned char *dest = captureData;
    for (int i = 0; i < Desc.Height; i++) {
        memcpy(captureData, source, Desc.Width * 4);
        source += pitch;
        captureData += Desc.Width * 4;
    }
    for (int i = 0; i < Desc.Width * Desc.Height * 4; i++) {
        //trace(L"Pixel[%d] = %x\n", i, dest[i]);
    }

    m_DeviceContext->Unmap(texTemp, 0);
    return dest;
}
else
    return NULL;
}

dest的输出充满了&#39; 0&#39;值。即使mapped.pData充满了&#39; 0&#39;我不知道为什么。

这是我调用我的函数GetImageData

的地方
DUPL_RETURN DISPLAYMANAGER::ProcessFrame(_In_ FRAME_DATA* Data, _Inout_ ID3D11Texture2D* SharedSurf, INT OffsetX, INT OffsetY, _In_ DXGI_OUTPUT_DESC* DeskDesc)
{
DUPL_RETURN Ret = DUPL_RETURN_SUCCESS;

// Process dirties and moves
if (Data->FrameInfo.TotalMetadataBufferSize)
{
    D3D11_TEXTURE2D_DESC Desc;
    Data->Frame->GetDesc(&Desc);

    if (Data->MoveCount)
    {
        Ret = CopyMove(SharedSurf, reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*>(Data->MetaData), Data->MoveCount, OffsetX, OffsetY, DeskDesc, Desc.Width, Desc.Height);
        if (Ret != DUPL_RETURN_SUCCESS)
        {
            return Ret;
        }
    }

    if (Data->DirtyCount)
    {
        Ret = CopyDirty(Data->Frame, SharedSurf, reinterpret_cast<RECT*>(Data->MetaData + (Data->MoveCount * sizeof(DXGI_OUTDUPL_MOVE_RECT))), Data->DirtyCount, OffsetX, OffsetY, DeskDesc, Desc);
        GetImageData(Data->Frame, Desc); //here I would like to extract the dirty rect buffer pixel (BGRA value).
    }
}

return Ret;
}

有可能吗?我从Microsoft Sample Desktop Duplication API获得了此代码。谢谢!

0 个答案:

没有答案