Skip to content

Instantly share code, notes, and snippets.

@thatcosmonaut
Created June 22, 2020 23:53
Show Gist options
  • Save thatcosmonaut/75d474d8f1ebfd74476233febc70e9be to your computer and use it in GitHub Desktop.
Save thatcosmonaut/75d474d8f1ebfd74476233febc70e9be to your computer and use it in GitHub Desktop.
ReadBackbufferTest
/* 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 System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace MSAATest
{
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 bool CanSaveThisFrame { get; private set; } = true;
public bool SaveThisFrame { get; private set; } = false;
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 Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyUp(Keys.S))
{
CanSaveThisFrame = true;
}
if (CanSaveThisFrame && Keyboard.GetState().IsKeyDown(Keys.S))
{
SaveThisFrame = true;
CanSaveThisFrame = false;
}
}
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();
}
if (SaveThisFrame)
{
int[] backBufferData = new int[GraphicsDevice.PresentationParameters.BackBufferWidth * GraphicsDevice.PresentationParameters.BackBufferHeight];
GraphicsDevice.GetBackBufferData(backBufferData);
Texture2D texture = new Texture2D(GraphicsDevice, GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight);
texture.SetData(backBufferData);
Stream stream = File.OpenWrite("readBackBuffer.png");
texture.SaveAsPng(stream, GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight);
stream.Dispose();
texture.Dispose();
SaveThisFrame = false;
}
}
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