Jump to content
  • Advertisement
Sign in to follow this  
Chase Mosher

SlimDX/SharpDX - Filling Textures

This topic is 2752 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 coming from XNA so I'm sorta lost in the sauce here... What I have is a ushort[] of pixel data and I'm looking for the preferred way of filling a texture with it. I've tried using a DataStream however when I read the data back is it completely wrong. I've search a bit and haven't come up with anything to help me get this right so I'm looking for some help... All the pixel data is 16bit and sorted in indexed files without empty pixels, the images are diamonds so I account for any whitespace when reading then from the file.

This works exactly as it should and fills the ushort[] buffer without any issues in the correct order;
public unsafe void ReadLand(int index)
{
this.Seek(index);

ushort[] buffer = new ushort[44 * 44];

fixed (ushort* pBuffer = buffer)
{
for (int y = 0; y < 22; y++)
{
ushort* position = pBuffer + (y * 44) + (21 - y);

for (int x = 0; x < (2 + (y * 2)); x++)
{
*position++ = (ushort)(this.BinaryReader.ReadUInt16() | 0x8000);
}
}

for (int y = 0; y < 22; y++)
{
ushort* position = pBuffer + ((y + 22) * 44) + y;

for (int x = 0; x < (44 - (y * 2)); x++)
{
*position++ = (ushort)(this.BinaryReader.ReadUInt16() | 0x8000);
}
}
}
}


This is one of the ways I've tried to do it using SharpDX, when I read the data pack it makes no sense at all;
public unsafe void ReadLandDX(int index)
{
this.Seek(index);

DataStream dataStream = new DataStream(44 * 44 * 2, true, true);

for (int y = 0; y < 22; y++)
{
dataStream.Position = ((y * 44) + (21 - y)) * 2;

for (int x = 0; x < (2 + (y * 2)); x++)
{
dataStream.Write<ushort>((ushort)(this.BinaryReader.ReadUInt16() | 0x8000));

dataStream.Position++;
}
}

for (int y = 0; y < 22; y++)
{
dataStream.Position = (((y + 22) * 44) + y) * 2;

for (int x = 0; x < (44 - (y * 2)); x++)
{
dataStream.Write<ushort>((ushort)(this.BinaryReader.ReadUInt16() | 0x8000));

dataStream.Position++;
}
}

ushort[] buffer = new ushort[44 * 44];

for (int i = 0; i < buffer.Length; i++)
{
buffer = dataStream.Read<ushort>();
}
}


Can anyone tell me the proper way to fill a texture? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Thanks, that was a problem as well as that the DataStream needed to be "cleared" first. I finally got it working correctly.

public unsafe void ReadLandDX(Device device, int index)
{
this.Seek(index);

DataStream dataStream = new DataStream(44 * 44 * 2, false, true);

for (int i = 0; i < dataStream.Length; i++)
{
dataStream.WriteByte(0x00);
}

dataStream.Seek(0, SeekOrigin.Begin);

for (int y = 0; y < 22; y++)
{
dataStream.Seek(((y * 44) + (21 - y)) * 2, SeekOrigin.Begin);

for (int x = 0; x < (2 + (y * 2)); x++)
{
dataStream.Write<ushort>((ushort)(this.BinaryReader.ReadUInt16() | 0x8000));
}
}

for (int y = 0; y < 22; y++)
{
dataStream.Seek((((y + 22) * 44) + y) * 2, SeekOrigin.Begin);

for (int x = 0; x < (44 - (y * 2)); x++)
{
dataStream.Write<ushort>((ushort)(this.BinaryReader.ReadUInt16() | 0x8000));
}
}
}


Another question, is this the right way to go about this? Is there a better, more efficient way to do this?

Share this post


Link to post
Share on other sites

public unsafe void ReadLand(int index)
{
this.Seek(index);

ushort[] buffer = new ushort[44 * 44];

fixed (ushort* pBuffer = buffer)
{
for (int y = 0; y < 22; y++)
{
ushort* position = pBuffer + (y * 44) + (21 - y);

for (int x = 0; x < (2 + (y * 2)); x++)
{
*position++ = (ushort)(this.BinaryReader.ReadUInt16() | 0x8000);
}
}

for (int y = 0; y < 22; y++)
{
ushort* position = pBuffer + ((y + 22) * 44) + y;

for (int x = 0; x < (44 - (y * 2)); x++)
{
*position++ = (ushort)(this.BinaryReader.ReadUInt16() | 0x8000);
}
}
}
DataStream dataStream = new DataStream(44 * 44 * 2, false, true);
dataStream.WriteRange<ushort>(buffer);
dataStream.Position = 0;
}


Should be faster, also, its obvious, why you need to change anything at all, XNA uses the same patern - create buffer, then SetData<T>(T[])

Also some things to keep in mind - never hardcode stuff - whats 22, 21, 44, 2, etc? Thats very bad practice, name your things!
Next, about "Another question, is this the right way to go about this? Is there a better, more efficient way to do this?" - no, there is never one right way, there are number of ways to do one thing and the "right one" is that works and does everything in limits you need. Also where did you get intention that makes you ask if there is more efficient way to do this? Did you profile your application, that is supposedly running slowly, and your bottleneck is reading 1936*2 bytes (and i guess this is not the thing you do all the time and in game-time, not loading or such?)? Anyways, if you really have performance problems that makes your question about performance worth to ask, and also have profiled your application to say that bottleneck is this one function- i would suggest to read bigger blocks of data, maybe storing your data so that it is less efficient to write, but you can read it with single read (and maybe without all those | 0x8000).

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!