Sign in to follow this  
jdub

SlimDX/D3D9 Random texture creation problem?

Recommended Posts

Here is my code:


public RandScreen(Device device)
{
rand = new Random();

//create blank texture with specified size and color depth
texture = new Texture(device, 100, 100, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
SurfaceDescription textureDesc = texture.GetLevelDescription(0);

DataRectangle dataStream = texture.LockRectangle(0, LockFlags.Discard);
for (int y = 0; y <= textureDesc.Height; y++)
{
for (int x = 0; x < textureDesc.Width; x++)
{
if (x == dataStream.Pitch)
{
x = 0;
continue;
}

dataStream.Data.Write((byte)randomPix(x,y));
}
}
texture.UnlockRectangle(0);
}






The application runs fine when I only write one byte at a time to the buffer (although the texture doesn't look right at all). However, each of my pixels has a size of 4 bytes. To remedy, I tried replacing the code with this:


byte[] buffer = new byte[4]
{
(byte)randomPix(x,y),
(byte)randomPix(x,y),
(byte)randomPix(x,y),
(byte)randomPix(x,y)
};

dataStream.Data.Write(buffer, 0, 4);






However, when the application runs, I get an EndOfStream Exception. If each pixel I am writing to is 4 bytes large, then why can't I write 4 bytes to it?

Also, how can I generate mipmaps for this texture? I tried to set it so that it would automatically create mipmaps but that makes DirectX throw an exception (realized that this makes sense because there is no texture data to create mipmaps for).

[Edited by - jdub on December 8, 2010 10:31:12 PM]

Share this post


Link to post
Share on other sites
Nope... it happens every time I try and run the application. The random texture is generated once at the beginning of the application after the device and renderer have been created.

The length of the stream is 40,000 bytes so it doesn't make sense that in my first write to it, writing 4 random bytes would cause an EndOfStream exception.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this