Hi.
I’m trying to implement MSAA in my 2D game for quite a while now, but nothing helped so far.
I’ve already followed other posts and by now, my rendering Code looks like that:
internal static GraphicsDeviceManager GraphManager;
private static Matrix Matrix_Scaling;
internal static SamplerState SamplerFilter;
internal static RasterizerState rasterizer;
private static float Scale_Factor;
internal static Vector2 Canvas_Position;
public GameObject()
{
GraphManager = new GraphicsDeviceManager(this)
{
PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8,
GraphicsProfile = GraphicsProfile.HiDef;
IsFullScreen = false,
SynchronizeWithVerticalRetrace = true,
HardwareModeSwitch = false,
PreferMultiSampling = true,
};
GraphManager.PreparingDeviceSettings += SetMultiSampling;
SamplerFilter = new SamplerState()
{
Filter = TextureFilter.Anisotropic,
AddressU = TextureAddressMode.Clamp,
AddressV = TextureAddressMode.Clamp,
AddressW = TextureAddressMode.Clamp,
BorderColor = Color.White,
MaxAnisotropy = 8,
MaxMipLevel = 8,
MipMapLevelOfDetailBias = 0f,
ComparisonFunction = CompareFunction.Never,
FilterMode = TextureFilterMode.Default
};
rasterizer = new RasterizerState()
{
CullMode = CullMode.None,
FillMode = FillMode.Solid,
MultiSampleAntiAlias = true,
};
}
protected override void BeginRun()
{
GraphManager.PreferredBackBufferWidth = (int)WindowSize.X;
GraphManager.PreferredBackBufferHeight = (int)WindowSize.Y;
GraphManager.ApplyChanges();
Canvas = new RenderTarget2D(GraphManager.GraphicsDevice, (int)WindowSize.X, (int)WindowSize.Y
, false, SurfaceFormat.Color, DepthFormat.None, 8, RenderTargetUsage.DiscardContents);
Matrix_Scaling = Matrix.CreateScale(Scale_Factor, Scale_Factor, 1);
}
private static void SetMultiSampling(object sender, PreparingDeviceSettingsEventArgs e)
{
GraphManager.PreferMultiSampling = true;
var pp = e.GraphicsDeviceInformation.PresentationParameters;
pp.MultiSampleCount = 8;
}
protected override void Draw(GameTime gt)
{
GraphManager.GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 1.0f, 0);
GraphManager.GraphicsDevice.SetRenderTarget(Canvas);
GraphManager.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.Stencil, Color.Black, 0, 0);
spriteBatch.Begin(SpriteSortMode.FrontToBack, samplerState: SamplerFilter, rasterizerState: rasterizer, transformMatrix: Matrix_Scaling);
// [...] Drawing all Sprites here
spriteBatch.End();
GraphManager.GraphicsDevice.SetRenderTarget(null);
spriteBatch.Begin(SpriteSortMode.Immediate, samplerState: SamplerFilter, rasterizerState: rasterizer);
spriteBatch.Draw(Canvas, Canvas_Position, Color.White);
spriteBatch.End();
}
On Game start I’m creating a RenderTarget2D to which im drawing all my sprites and that is later drawn onto the screen. It has the size, with that it’s displayed on the screen. A Matrix ensures, that the images are scaled to the correct size.
Currently, when my Gamewindow draws it’s content scaled, the pixels are really jaggy. Moving sprites show wobbly outlines (pixels don’t look like they would all have the same size, but some are bigger than others) and for now I couldn’t activate MSAA to smoothen the scaled images.
Changing the SamplerState did make smaller sprites that were scaled up a bit blurry, but didn’t affect scaling down. Therefore, playing on a smaller window is a bit ugly.
I hope someone can show me what I’m missing.
Thanks