MJP said:
The pData member of D3D11_MAPPED_SUBRESOURCE is going to be a pointer to the raw texel data, matching the DXGI_FORMAT of the staging texture. So if it's R8G8B8A8_UNORM then there's going 4 bytes per pixel, in RGBA order starting from low to high. You can cast the pData pointer to a uint8_t or a uint32_t or a custom struct you've defined with 1 byte for each channel. Like Aerodactyl55 mentioned though, you need to use the RowPitch when going from one row to the next.
Thank you I have tried to start to implement my function that get a pixel color from screen via DirectX 11:
/// <summary>
/// Get pixel color from screen (24 bit) via DirextX 11
/// </summary>
/// <param name="X"></param>
/// <param name="Y"></param>
/// <param name="swapchainDescription"></param>
/// <param name="pContext"></param>
RGBTRIPLE GetPixelColor(int X, int Y, DXGI_SWAP_CHAIN_DESC* swapchainDescription, ID3D11DeviceContext* pContext)
{
RGBTRIPLE rgb;
D3D11_TEXTURE2D_DESC desc2;
desc2.Width = 1;
desc2.Height = 1;
//desc2.MipLevels = desc.MipLevels; ?? //// How I get this ??
//desc2.ArraySize = desc.ArraySize; ?? //// How I get this ??
desc2.Format = swapchainDescription->BufferDesc.Format;
desc2.SampleDesc = swapchainDescription->SampleDesc;
desc2.Usage = D3D11_USAGE_STAGING;
desc2.BindFlags = 0;
desc2.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc2.MiscFlags = 0;
ID3D11Texture2D* stagingTexture;
ID3D11Texture2D* pSourceTexture; //// How I get this ??
HRESULT hr = pDevice->CreateTexture2D(&desc2, nullptr, &stagingTexture);
if (FAILED(hr)) {
LF::Log_Update("Fail");
}
D3D11_BOX srcBox;
srcBox.left = X;
srcBox.right = srcBox.left + 1;
srcBox.top = Y;
srcBox.bottom = srcBox.top + 1;
srcBox.front = 0;
srcBox.back = 1;
pContext->CopySubresourceRegion(stagingTexture, 0, 0, 0, 0, pSourceTexture, 0, &srcBox);
D3D11_MAPPED_SUBRESOURCE msr;
pContext->Map(stagingTexture, 0, D3D11_MAP_READ, 0, &msr);
uint32_t pixel = reinterpret_cast<uint32_t>(msr.pData); /// I don't think is correct
rgb.rgbtRed = (pixel >> 16) & 0xff;
rgb.rgbtGreen = (pixel >> 8) & 0xff;
rgb.rgbtBlue = pixel & 0xff;
return rgb;
}
but it is incomplete and I don't sure if I'am in the right way becouse there are some parts what I don't think are correct.
Can you please help me with my code ?
Thanks !