Jump to content
  • Advertisement
Sign in to follow this  
chlerub

SharpDX - read R16F into array

This topic is 2069 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

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

 

Edited by chlerub

Share this post


Link to post
Share on other sites
Advertisement

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.

Edited by xoofx

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!