Jump to content
  • Advertisement
Sign in to follow this  
adt7

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.

If you intended to correct an error in the post then please contact us.

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,
                CpuAccessFlags = CpuAccessFlags.Read,
                FilterFlags = FilterFlags.None,
                Format = SlimDX.DXGI.Format.R32_Float,
                MipFilterFlags = FilterFlags.None,
                OptionFlags = ResourceOptionFlags.None,
                Usage = ResourceUsage.Staging,
            };

            Texture2D tex = renderSystem.LoadTexture("Terrain.dds", info);

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

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

Am I doing something obviously stupid? (Probably)

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Pyrogame
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?


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 Nik02
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);


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 Nik02
Also, 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 feal87
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)


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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!