Sorry, the Forum is closed :(

Unfortunately, the forum is now closed.

Please read the following post for further details.

We apologize for the inconvenience.
The SharpDX team.
Welcome, Guest
Username: Password: Remember me
Welcome to the general discussions!
  • Page:
  • 1

TOPIC: Best way to create a new Texture2D programmatically?

Best way to create a new Texture2D programmatically? 1 year 9 months ago #1214


  • Posts:2
  • joewhite's Avatar
  • joewhite
  • Fresh Boarder
  • OFFLINE
I'm using the SharpDX Toolkit, and I want to create a spritesheet programmatically, specifying all the pixel colors in code. The Toolkit's SpriteBatch.Draw method takes a Texture2D, so that's the class I need to end up with. I didn't see anything in the samples that does this -- the closest seems to be the MiniShape sample, but it's doing vector art and I want pixel art.

My textures are spritesheets made up of lots of individual animation-frame sprites, and will end up being around 5000x1000 pixels in size. So I won't want to do individual SetPixel calls (if any SharpDX classes even have anything resembling SetPixel); I'll want to pass a large array (of bytes? ints?) and construct the whole thing at once. I want to use some flavor of 32-bit RGBA.

My first question is, what's the best approach to take? There seem to be a lot of different classes in SharpDX that represent bitmaps, and there are some ways to convert from one to another (e.g. you can create a SharpDX.Toolkit.Graphics.Texture2D from a SharpDX.Toolkit.Graphics.Image, but I don't know how you get pixels into an Image so that doesn't help me much). What's going to be the best class to work with to build up a texture from individual pixels? I've got some small-scale working code using Texture2D.New and GetData/SetData, but I don't know whether that's going to be the best approach for performance, broad hardware support, etc.

My second question is, what pixel format should I choose? (Perhaps there's a class I could use that doesn't require me to pick a pixel format, but Texture2D.New does require a pixel format.) I don't know any of the reasons why I might choose R8G8B8A8 vs. B8G8R8A8. Is one more performant than the other? Are they both supported on all graphics cards?

And I know almost nothing about all the pixel subformats (UInt, UNorm, Typeless, etc.) beyond the sketchy descriptions on Microsoft's DXGI_FORMAT page. I don't need the sRGB colorspace, and Typeless seems to be for very special purposes. I think I probably want UInt or UNorm since they're both plain old bytes, but it doesn't tell me which to choose. I found by experimenting that UNorm works but UInt ends up not rendering anything to the screen, but I again don't know whether UNorm is the best recommendation, whether it would work on all video cards, etc.

Anyone know more about this than me, or able to point me to some relevant resources (websites/books) that might help?
The administrator has disabled public write access.

Re: Best way to create a new Texture2D programmatically? 1 year 9 months ago #1227


  • Posts:130
  • Toadums's Avatar
  • Toadums
  • Expert Boarder
  • OFFLINE
I might try to use WriteableBitmap. You can specify the size you want the image to be, then programatically set each pixel.

Something like:
WriteableBitmap wmb = new WriteableBitmap(5000,1000);

for(int i = 0; i < wmb.PixelWidth; i++){
   for(int j = 0; j < wmb.PixelHeight; j++){
      wmb.Pixels[i][j] = 0xFFFFF00; //for yellow, or whatever color you want
   }
}

Then you can save that writeable bitmap as an image.

API: Is Here

But this might not be what you want at all.
Last Edit: 1 year 9 months ago by Toadums. Reason: Added link
The administrator has disabled public write access.

Re: Best way to create a new Texture2D programmatically? 1 year 9 months ago #1245


  • Posts:843 Thank you received: 1
  • xoofx's Avatar
  • xoofx
  • Administrator
  • OFFLINE
JoeWhite, I really encourage you to buy a D3D11 book (Luna as stated in "SharpDX where do i start?"), because some of your questions are related to D3D11 usage.

To manipulate image on the CPU in the toolkit, use Image class. You have methods to access the pixel buffer:
var colors = new Color[width * height];
// fill your colors array
...
var image = Image.New2D(width, height, 1, PixelFormat.R8G8B8A8.UNorm);
// Access first mipmap
image.PixelBuffer[0].SetPixels(colors);
// then you can instantiate a texture 2D
var texture2D = Texture2D.New(graphicsDevice, image);

But you could also create directly a texture from a color buffer:
var texture2D = Texture2D.New(graphicsDevice, width, height, PixelFormat.R8G8B8A8.UNorm, colors);
Usually, if texture data doesn't change after creation time, it is better to use these constructors, as texture are created on the GPU as immutable. For PixelFormat, stick with UNorm, this is the standard suffix format (UInt as other usages, and lots of limitations).
Last Edit: 1 year 9 months ago by xoofx.
The administrator has disabled public write access.

Re: Best way to create a new Texture2D programmatically? 1 year 9 months ago #1253


  • Posts:2
  • joewhite's Avatar
  • joewhite
  • Fresh Boarder
  • OFFLINE
@Toadums, that might not be a bad idea; it looks like Texture2D.Load can take a stream, so I wouldn't even need to create a temp file, I could just save the WriteableBitmap to a memory stream and then load the texture from that. I'll see if I can make this work.

@xoofx, thanks for the info; that'll be very useful if I do create the Texture2D directly. Thanks especially for the info about UInt and UNorm.

I'm not sure which of my questions involved D3D11 usage, as I'm not doing anything 3D; but if you think the book will help, I'll see if I can get a copy.

Thanks, guys!
The administrator has disabled public write access.
  • Page:
  • 1
Time to create page: 0.177 seconds