Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17560

Get Raw Data from D3D11Texture2D C++

$
0
0

Hello everyone,

 

I'm trying to get the pixel raw buffer data from a D3D11Texture2D. I started with this sample code from MSDN : https://code.msdn.microsoft.com/windowsdesktop/Desktop-Duplication-Sample-da4c696a/sourcecode?fileId=42782&pathId=1384140008

 

In this function :

DUPL_RETURN DISPLAYMANAGER::ProcessFrame(_In_ FRAME_DATA* Data, _Inout_ ID3D11Texture2D* SharedSurf, INT OffsetX, INT OffsetY, _In_ DXGI_OUTPUT_DESC* DeskDesc)

I would like to extract the buffer of pixel (BGRA) from Data->Frame so I did this 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;
}

I have a problem, when I print I got 0 in every index... I don't know why.

Please can someone help me ?

Thank you very much


Viewing all articles
Browse latest Browse all 17560

Trending Articles