我想从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)
这是我的代码,返回的缓冲区充满了'\ 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);
谢谢大家