How to choose a pixel format when creating a new Texture2D?

I am using the SharpDX Toolkit and I am trying to create a Texture2D programmatically, so I can manually specify all the pixel values. And I'm not sure what pixel format to create it.

SharpDX doesn't even document the PixelFormat toolkit (they have documentation for another PixelFormat class , but it's for WIC, not toolkit), I found the DirectX enum that it wraps, DXGI_FORMAT , but its documentation does not give any useful recommendations as to how I chose the format.

I'm used to the old 32-bit raster image formats with 8 bits per color channel plus 8-bit alpha, which is good enough for me. Therefore, I assume that the simplest choice would be R8G8B8A8 or B8G8R8A8. Does what I choose matter? Will they both be fully supported on all hardware?

And even after I selected one of them, I need to additionally indicate whether they will be SInt, SNorm, Typeless, UInt, UNorm or UNormSRgb. I do not need the sRGB color space. I don’t understand what should be Typical. UInt seems the simplest - just a plain old unsigned byte - but it turns out that it doesn't work; I am not getting an error, but my texture does not draw anything on the screen. UNorm works, but there is nothing in the documentation that explains why UInt does not. So, now I'm paranoid that UNorm may not work on any other graphics card.

Here is the code that I have if someone wants to see it. Download the full SharpDX package, open the SharpDXToolkitSamples project, go to the SpriteBatchAndFont.WinRTXaml project, open the SpriteBatchAndFontGame class and add the code where it says:

// Add new field to the class: private Texture2D _newTexture; // Add at the end of the LoadContent method: _newTexture = Texture2D.New(GraphicsDevice, 8, 8, PixelFormat.R8G8B8A8.UNorm); var colorData = new Color[_newTexture.Width*_newTexture.Height]; _newTexture.GetData(colorData); for (var i = 0; i < colorData.Length; ++i) colorData[i] = (i%3 == 0) ? Color.Red : Color.Transparent; _newTexture.SetData(colorData); // Add inside the Draw method, just before the call to spriteBatch.End(): spriteBatch.Draw(_newTexture, new Vector2(0, 0), Color.White); 

This draws a small rectangle with diagonal lines in the upper left corner of the screen. It works on a laptop, on which I am testing it, but I have no idea how to find out if this means that it will work everywhere, and I don’t know if it will be the most effective.

What pixel format should I use to make sure that my application will run on all hardware and get maximum performance?

+4
source share
1 answer

Formats in the SharpDX Toolkit are mapped to the basic DirectX / DXGI formats, so you can, as usual, with Microsoft products, get information from MSDN:

DXGI_FORMAT enumeration (Windows)

32-bit textures are a common choice for most texture scenarios and have good performance for older hardware. UNorm means, as the comments already answered, "in the range 0.0 .. 1.0" and, again, is the usual way to access color data in textures.

If you look at Hardware Support for Direct3D 10Level9 Formats (Windows) , you will see that DXGI_FORMAT_R8G8B8A8_UNORM as well as DXGI_FORMAT_B8G8R8A8_UNORM are supported by DirectX 9. You will not encounter compatibility issues with both of them.

Performance depends on how your device is initialized (RGBA / BGRA?) And what hardware (= supported DX level) and the OS on which you run your software. You will need to run your own tests to find out about this (although in the case of these common and similar formats, the difference should be no more than one bit).

+3
source

Source: https://habr.com/ru/post/1447138/


All Articles