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

## 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
Pyrogame    106
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
Nik02    4348
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
Nik02    4348
Also, try to fill all the fields of the image info structure (as in mip levels, width and height).

##### Share on other sites
feal87    238
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.

## Create an account

Register a new account

• ### Similar Content

• By isu diss
I'm trying to code Rayleigh part of Nishita's model (Display Method of the Sky Color Taking into Account Multiple Scattering). I get black screen no colors. Can anyone find the issue for me?

• I made my obj parser
and It also calculate tagent space for normalmap.
it seems calculation is wrong..
any good suggestion for this?
https://gamedev.stackexchange.com/questions/147199/how-to-debug-calculating-tangent-space
and I uploaded my code here

• Hi guys,

I dont know if this is the right section, but I did not know where to post this.
I am implementing a day night cycle on my game engine and I was wondering if there was a nice way to interpolate properly between warm colors, such as orange (sunset) and dark blue (night) color. I am using HSL format.
Thank  you.

• I am aiming to learn Windows Forms with the purpose of creating some game-related tools, but since I know absolutely nothing about Windows Forms yet, I wonder:
Is it possible to render a Direct3D 11 viewport inside a Windows Form Application? I see a lot of game editors that have a region of the window reserved for displaying and manipulating a 3D or 2D scene. That's what I am aiming for.
Otherwise, would you suggest another library to create a GUI for game-related tools?

EDIT:
I've found a tutorial here in gamedev that shows a solution:
Though it's for D3D9, I'm not sure if it would work for D3D11?

• in directx I need to know how would I play the animation in directx rendering frames for each part of the model moving parts - like the bones, how would would that work?
I'm new to writing a animation player, for a model parser.
I'm asking so I understand how to do this, I'm new to loading a model and playing\moving the bones to effect the mesh.

• 9
• 15
• 51
• 12
• 11