I use SharpDX and have a problem with PixelFormat
. I want to make it A8_UNorm
and use an OpacityMask
but I get an UnsupportedPixelFormatException
. But according to MSDN everything should be alright.
Code:
PixelFormat PF = new PixelFormat(Format.A8_UNorm, AlphaMode.Straight);
var desc = new Texture2DDescription()
{
Width = Width,
Height = Height,
Format = Format.A8_UNorm,
SampleDescription = new SampleDescription(1, 0),
ArraySize = 1,
CpuAccessFlags = 0,
Usage = ResourceUsage.Default,
MipLevels = 1,
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
};
using (var d = new DataStream(desc.Width * desc.Height, true, true))
{
for (var i = 0; i < desc.Width * desc.Height / 4; i++) d.Write(0);
using (Texture2D tex = new Texture2D(GameControl.Device, desc, new DataRectangle(d.DataPointer, desc.Width)))
{
using (Surface temp = tex.QueryInterface<Surface>())
{
Target = new RenderTarget(Factory2D, temp, new RenderTargetProperties(PF));
OpacityMask = new Bitmap(Target, temp, new BitmapProperties(PF)); //Exception is here
}
}
}
Pixel format support depends on software and hardware. You can check if your platform supports what you want with CheckFormatSupport()
. I use it like this:
var format = Format.A8_UNorm;
var supportRequired = FormatSupport.RenderTarget;
var isSupported = device.CheckFormatSupport(format).HasFlag(supportRequired);
Note that there are many options besides FormatSupport.RenderTarget
, so test all of them that you are trying to use.