[SlimDX] Texture class not working?

Started by
7 comments, last by sascoo 14 years, 2 months ago
I have a byte[] i wish to write to a texture. For some reason I cannot get the texture initialized... Texture t = Texture.FromFile(device, "cheese.jpg"); // Works t.FromMemory(device,frame); // Doesnt work? if(t == null) t = new Texture(device, (int)Width, (int)Height, 1, Usage.Dynamic, Format.X8B8G8R8, Pool.Default); // Doesnt work...? I have the latest build from August and I am wondering if this is a bug in the framework. Any suggestions would be appreciated. Thanks, John
Advertisement
The FromMemory methods expect an array of data from an image file, not just an array of raw pixel values.

Also please be specific instead of saying "doesn't work". I assume that some sort of exception is thrown? Which exception? What is the message from that exception?
Quote:t = new Texture(device, (int)Width, (int)Height, 1, Usage.Dynamic, Format.X8B8G8R8, Pool.Default); // Doesnt work...?

Make sure the Format.X8B8G8R8 is supported by checking your device capabilities (my NVidia GT 8500 for instance doesn't). You probably want Format.A8R8G8B8 or Format.X8R8G8B8.

Quote:Original post by unbird
Quote:t = new Texture(device, (int)Width, (int)Height, 1, Usage.Dynamic, Format.X8B8G8R8, Pool.Default); // Doesnt work...?

Make sure the Format.X8B8G8R8 is supported by checking your device capabilities (my NVidia GT 8500 for instance doesn't). You probably want Format.A8R8G8B8 or Format.X8R8G8B8.


Doh... I totally thought I was using X8R8G8B8

Thanks, that fixed it.

One more thing before I go back to my cave...

Is this the correct way to write to texture?

byte[] frame = some data
if (t == null)
t = new Texture(device, (int)Width, (int)Height, 1, Usage.Dynamic, Format.X8R8G8B8, Pool.Default);
DataRectangle r = t.LockRectangle(0,LockFlags.None)
r.Data.Write(frame,0,frame.Length);
t.UnlockRectangle(0);

When I render the texture it only has a single line of pixels that go straight down from the top-left corner. What am I doing incorrectly?
Well, depends on what's in that "some data" array. How do you initialize it and what do you expect ? What size is the byte array and what are Width and Height ?

Probably a differently typed array is more suited, for instance int[] (since you're using XRGB) and then use DataStream.WriteRange<T>(T[] data).
Also, check if you get a non-zero pitch from the DataRectangle (dunno if SlimDX will handle pitch with a byte-array automatically), maybe that's a problem.

PS: By the way:
Quote:t.FromMemory(device,frame); // Doesnt work?

You used that method the wrong way: It's a static method, so the correct call is:
Texture t = Texture.FromMemory(device,frame);


I tried
t = Texture.FromMemory(device, frame);

Do I need it initialize the texture first or should that just work?
No, it creates a new Texture instance, so it's something like a constructor. There are some overloads of this method which allow different initialization (usage, format, etc.), though.

And as MJP pointed out, Texture.FromMemory cannot be fed with raw pixel values. It's similar to Texture.FromFile, only that the data comes from an array.

Only wanted to show you the correct use of static methods, that's why I put the PS (postscript). Please apologize if I confused you.

So again: What is in that frame-array ?

Is it actually raw pixel data ? Then you have to resort to LockRectangle.

Or some DirectX supported image data (see D3DXIMAGE_FILEFORMAT)? Only then, FromMemory should work.
Yeah, I have raw data that I get out of a surface in DirectX that I am trying to write to the Texture. I know I am just missing a trivial step at this point but I cannot figure out where. I think it is probably the pitch from the datarectangle. I will check that out when I get home.

Thanks

This topic is closed to new replies.

Advertisement