I was building a simple D3D12 app which load and display an image on screen.
As you are aware, D3D12 has no calls like CreateTexture2D which we have in D3D11 so that this process has to be done in the hard way.
So far I've loaded the image in memory using WIC with DXGI_FORMAT_R8G8B8A8_UNORM and got its width and height.
Created a 2D array to store the pixels.
BYTE ** pixels = new BYTE*[height];
for (int i = 0; i < height; i++) {
pixels[i] = new BYTE[width * 4];
}
Use CopyPixels to copy the pixels to the new array
UINT stride = width * 4;
hr = pSource->CopyPixels(nullptr, stride, (sizeof(BYTE)*4*height*width), *pixels);
Note : I'm getting 0x88982f8c: insufficient buffer allocation here!
My aim is to get the texture data so that I can copy it to the upload buffer.
For that I'm extracting the texture dat as follows,
D3D12_SUBRESOURCE_FOOTPRINT srFtprint;
srFtprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
srFtprint.Depth = 1; // 0 or 1?
srFtprint.Width = width;
srFtprint.Height = height;
srFtprint.RowPitch = rowPitch;
D3D12_PLACED_SUBRESOURCE_FOOTPRINT placedTexture2D = { 0 };
placedTexture2D.Offset = 0;
placedTexture2D.Footprint = srFtprint;
UINT8 * pScan;
for (UINT y = 0; y < height; y++)
{
pScan = m_pDataBegin + placedTexture2D.Offset // Now pScan stores the texture data
+ y * srFtprint.RowPitch;
memcpy(pScan, &(pixels[y * width]),
sizeof(BYTE) * width);
}
I'm unable to accomplish this correctly because I think I'm not able to create/copy the exact number of bytes in the same expected alignment.