Skip to main content
GameDev.net gamedev.net
🔒 Locked

SharpDX - read R16F into array

Started by chlerub Feb 22, 2014 at 10:12 PM 1 replies 2.2k views
Original Post
chlerub
chlerub

Hey there,

i'm trying to read a 16b floating point texture into a float array.

However, no matter what i do it comes out as complete garbage.

In order to test this i create a new texture from the data and save it.
See the attached screenshots, one is the source image, the other is the resulting garbage.

If anyone knows what's going on here, every bit of help is greatly appreciated!

Here's how i load the heightmap and read the data. The image was saved as R16 DDS using the Nvidia Photoshop plugin.


            var desc = new ImageLoadInformation
            {
                Format = SharpDX.DXGI.Format.R16_Float,
                Filter = FilterFlags.None,
                Width = 4097,
                Height = 4097,
                MipLevels = 1,
                CpuAccessFlags = CpuAccessFlags.Read,
                OptionFlags = ResourceOptionFlags.None,
                Usage = ResourceUsage.Staging,
                BindFlags = BindFlags.None,
                MipFilter = FilterFlags.None,
            };

            var texture = Texture2D.FromFile<Texture2D>(Engine.Device, "height16b.dds", desc);
            var buffer = new Half[4097 * 4097];
            
            var box = Engine.Device.ImmediateContext.MapSubresource(texture, 0, MapMode.Read, MapFlags.None);
            Utilities.Read<Half>(box.DataPointer, buff, 0, buff.Length);
            Engine.Device.ImmediateContext.UnmapSubresource(texture, 0);

Here's how i create a new texture from the array and save it:


            int byteSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(Half));

            DataStream stream = new DataStream(buffer.Length * byteSize, true, true);
            stream.WriteRange(buffer);

            int pitch = size * byteSize;
            DataRectangle rect = new DataRectangle(stream.DataPointer, pitch);

            Texture2D result = new Texture2D(Engine.Device, new Texture2DDescription
            {
                ArraySize = 1,
                BindFlags = BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = SharpDX.DXGI.Format.R16_Float,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                Height = size,
                Width = size,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                Usage = ResourceUsage.Default

            }, rect);

            Texture2D.ToFile(Engine.Device.ImmediateContext, result , ImageFileFormat.Dds, "G:\\test16bit.dds");
AlexandreMutel
AlexandreMutel

You can't back read the whole texture in a float array as the number of bytes per row is more likely to be different when mapping the resource. Check the box.RowPitch that will give you exactly the number of bytes per row (that will not equal to sizeof(float) * 4097). Internally, the texture on the GPU is not having a size of 4097 but will align it to the need of its architecture.

You need to iterate on each row and copy them one at a time when the box.RowPitch doesn't match your target rowpitch.

chlerub
chlerub

Awesome, thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.