# DX11 [SlimDX, DX11] Read From texture Into Array

This topic is 3030 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm sure I'm just being stupid, but I really can't figure out how to read the contents of a texture into an array using SlimDX and DirectX 11. In DirectX 9 it was easy, simply call LockRectangle and read from the resulting DataRectangle. I understand that in DirectX 11 I need to call MapSubresource to get a DataBox and then use that. However I can't figure out what I should be passing to the parameters of MapSubresource, and once I've done that I don't know what to do with the resulting DataBox. I'm currently doing the following to load an R32F DDS texture, and the values in my array seem completely wrong. It's as if it's only reading from every 4th row of my texture. Note: The renderSystem.MapSubresource passes the parameters directly to Device.MapSubresource().
ImageLoadInformation info = new ImageLoadInformation()
{
BindFlags = BindFlags.None,
FilterFlags = FilterFlags.None,
Format = SlimDX.DXGI.Format.R32_Float,
MipFilterFlags = FilterFlags.None,
OptionFlags = ResourceOptionFlags.None,
Usage = ResourceUsage.Staging,
};

float[] data = new float[2048 * 2048];

DataBox box = renderSystem.MapSubresource(tex, 0, tex.Description.Width * tex.Description.Height * sizeof(float), MapMode.Read, MapFlags.None);


Am I doing something obviously stupid? (Probably)

##### Share on other sites
Hmm, your parameters seems to be ok. Maybe the "float" is problematic, try "int". Or the texture is somehow compressed (4x4 blocks) and you got the raw data?

##### Share on other sites
The ReadRange method is a templated method that is supposed to take a datatype as the template parameter. Without looking at the source, I would guess that the default type is Byte.

Try:

ReadRange<float>(data, 0, 2048 * 2048);

##### Share on other sites
Also, try to fill all the fields of the image info structure (as in mip levels, width and height).

##### Share on other sites
Another thing :

Check if the pitch is really what you think it is (the drivers sometimes can use some extra data at the end of each scanline)

##### Share on other sites
Quote:
 Original post by PyrogameHmm, your parameters seems to be ok. Maybe the "float" is problematic, try "int". Or the texture is somehow compressed (4x4 blocks) and you got the raw data?

I've not had a chance to try this, but it's a floating point texture, so reading in using sizeof(int) seems completely wrong.

Quote:
 Original post by Nik02The ReadRange method is a templated method that is supposed to take a datatype as the template parameter. Without looking at the source, I would guess that the default type is Byte.Try:ReadRange(data, 0, 2048 * 2048);

It's a generic method (I know, same differencem but there is a distinction) and picks up that it should be outputting floats from the data parameter quite happily.

Quote:
 Original post by Nik02Also, try to fill all the fields of the image info structure (as in mip levels, width and height).

I'll try this later on. Although they seem to be being picked up fine by the Texture2D.FromFile method, the Description structure for the texture is as I would expect after loading.

Quote:
 Original post by feal87Another thing :Check if the pitch is really what you think it is (the drivers sometimes can use some extra data at the end of each scanline)

No matter what I do the pitch comes out as 8192. Which since it's a 2048x2048 texture and sizeof(float) is 4... seems right to me.

---

I'm surprised there is not a sample out there for this somewhere, or no one has done it already.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 13
• 38
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631714
• Total Posts
3001861
×