Hello everyone,
I'm trying to extract buffer of pixel BGRA from a D3D11Texture2D.
Here is the code I started with : https://code.msdn.microsoft.com/windowsdesktop/Desktop-Duplication-Sample-da4c696a/sourcecode?fileId=42782&pathId=1384140008
In this function I would like to extract the raw data from Data->Frame :
DUPL_RETURN DISPLAYMANAGER::ProcessFrame(_In_ FRAME_DATA* Data, _Inout_ ID3D11Texture2D* SharedSurf, INT OffsetX, INT OffsetY, _In_ DXGI_OUTPUT_DESC* DeskDesc)
Here is my extract code :
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 *m_CaptureData = new unsigned char[Desc.Width * Desc.Height * 4]; RtlZeroMemory(m_CaptureData, Desc.Width * Desc.Height * 4); const int pitch = Desc.Width; unsigned char* source = static_cast<unsigned char*>(mapped.pData); unsigned char* dest = m_CaptureData; for (int i = 0; i < Desc.Width; i++) { memcpy(dest, source, Desc.Height * 4); source += pitch; dest += pitch; } for (int i = 0; i < 1200; i++) { trace(L"Pixel[%d] = %x\n", i, source[i]); } m_DeviceContext->Unmap(texTemp, 0); return dest; } else return NULL; }
On the print I got value '0' in every index. I don't know why ..
Please can you help me ? Thank you !