Skip to content

Instantly share code, notes, and snippets.

@TheSpydog
Created May 2, 2020 19:03
Show Gist options
  • Save TheSpydog/87ad30b72a797151924ded3f2f3c9f74 to your computer and use it in GitHub Desktop.
Save TheSpydog/87ad30b72a797151924ded3f2f3c9f74 to your computer and use it in GitHub Desktop.
/* SpriteBatch Stress Test
* Written by Ethan "flibitijibibo" Lee
* http://www.flibitijibibo.com/
*
* Released under public domain.
* No warranty implied; use at your own risk.
*/
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
class MSAATest : Game
{
GraphicsDeviceManager gdm;
RenderTarget2D rt;
SpriteBatch batch;
VertexBuffer vertexBuffer;
BasicEffect basicEffect;
Matrix world = Matrix.CreateTranslation(0, 0, 0);
Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 3), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.01f, 100f);
// Test different combinations!
private const bool TEST_MSAA_BACKBUFFER = true;
private const bool TEST_MSAA_RENDER_TARGETS = true;
private const int SAMPLE_COUNT = 8;
public MSAATest() : base()
{
gdm = new GraphicsDeviceManager(this);
if (TEST_MSAA_BACKBUFFER)
{
gdm.PreferMultiSampling = true;
}
}
protected override void Initialize()
{
if (TEST_MSAA_BACKBUFFER)
{
GraphicsDevice.PresentationParameters.MultiSampleCount = SAMPLE_COUNT;
gdm.ApplyChanges();
}
basicEffect = new BasicEffect(GraphicsDevice);
VertexPositionColor[] vertices = new VertexPositionColor[3];
vertices[0] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Red);
vertices[1] = new VertexPositionColor(new Vector3(+0.5f, 0, 0), Color.Green);
vertices[2] = new VertexPositionColor(new Vector3(-0.5f, 0, 0), Color.Blue);
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), 3, BufferUsage.WriteOnly);
vertexBuffer.SetData<VertexPositionColor>(vertices);
rt = new RenderTarget2D(GraphicsDevice, 800, 480, false, SurfaceFormat.Color, DepthFormat.None, SAMPLE_COUNT, RenderTargetUsage.DiscardContents);
batch = new SpriteBatch(GraphicsDevice);
base.Initialize();
}
protected override void Draw(GameTime gameTime)
{
if (TEST_MSAA_RENDER_TARGETS)
{
GraphicsDevice.SetRenderTarget(rt);
GraphicsDevice.Clear(Color.Transparent);
}
else if (TEST_MSAA_BACKBUFFER)
{
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
}
basicEffect.World = world;
basicEffect.View = view;
basicEffect.Projection = projection;
basicEffect.VertexColorEnabled = true;
GraphicsDevice.SetVertexBuffer(vertexBuffer);
RasterizerState rasterizerState = new RasterizerState();
rasterizerState.CullMode = CullMode.None;
GraphicsDevice.RasterizerState = rasterizerState;
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
}
if (TEST_MSAA_RENDER_TARGETS)
{
// Draw the render target
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
batch.Begin();
batch.Draw(rt, Vector2.Zero, Color.White);
batch.End();
}
}
public static void Main(string[] args)
{
using (MSAATest game = new MSAATest())
{
game.Run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment