Is this the correct way of mapping a texture

Started by
2 comments, last by 21st Century Moose 11 years ago

I want to save a complete texture that is in GPU memory in a file.

So i am doing this:


D3D10_MAPPED_TEXTURE2D ddMap;

BallsTexACPU->Map(0, D3D10_MAP_READ, 0, &ddMap);
fwrite (ddMap.pData , 16*res*res , 1 , SaveStructFile );       //(res*res) the number of pixels in the texture, 16 bytes in each pixel
BallsTexACPU->Unmap(0);

BallsTexACPU is a ( ID3D10Texture2D* ) it has D3D10_USAGE_STAGING, D3D10_CPU_ACCESS_READ
I just want to know if

BallsTexACPU->Map(0, D3D10_MAP_READ, 0, &ddMap);
fwrite (ddMap.pData , 16*res*res , 1 , SaveStructFile );
is correct
Advertisement

It's partially correct; you will successfully get access to the texels and you will write them out to file.

What you write out may not be what you want, however, because you're not taking account of the RowPitch member of ddMap. What you want instead would be something like this:


byte *mapped = (byte *) ddMap.pData;

for (int i = 0; i < res; i++)
{
    fwrite (mapped, res * 16, 1, SaveStructFile);
    mapped += ddMap.RowPitch;
}

(This is just code written off the top of my head - you may need to adjust it to get it to work).

(And don't forget to test "if (SUCCEEDED" from your Map call).

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I second the response from mhagain - you have to take into account the row size for the texture. Sometimes the GPU driver can store additional information at the end of each row of texels depending on its implementation. I honestly don't recall seeing any instances of this lately, but for sure back in the D3D9 days it was frequent that there was extra data appended to each row.

I've seen an AMD 6450 do it with a rendertarget recently, so I can confirm that it is still something you need to be on the lookout for (in the rendertarget case saving a screenshot to disk would be an example of where it would matter, which is what I suspect you may be doing here).

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement