Skip to content

Instantly share code, notes, and snippets.

@TheSpydog
Last active May 2, 2020 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheSpydog/c44aca8ac30c9a3a905bbb5a33c94547 to your computer and use it in GitHub Desktop.
Save TheSpydog/c44aca8ac30c9a3a905bbb5a33c94547 to your computer and use it in GitHub Desktop.
/* Mostly ripped off from MonoGame's unit tests. */
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
class GetSetBufferTest : Game
{
VertexPositionTexture[] savedData = new VertexPositionTexture[]
{
new VertexPositionTexture(new Vector3(1,2,3), new Vector2(0.1f,0.2f)),
new VertexPositionTexture(new Vector3(4,5,6), new Vector2(0.3f,0.4f)),
new VertexPositionTexture(new Vector3(7,8,9), new Vector2(0.5f,0.6f)),
new VertexPositionTexture(new Vector3(10,11,12), new Vector2(0.7f,0.8f))
};
VertexPositionTexture vertexZero = new VertexPositionTexture(Vector3.Zero, Vector2.Zero);
GraphicsDeviceManager gdm;
public GetSetBufferTest()
{
gdm = new GraphicsDeviceManager(this);
}
protected override void Initialize()
{
// Run the tests!
ShouldSetAndGetData(true);
ShouldSetAndGetData(false);
ShouldSetAndGetData_elementCount(true);
ShouldSetAndGetData_elementCount(false);
ShouldSetAndGetData_startIndex(true);
ShouldSetAndGetData_startIndex(false);
ShouldSetAndGetData_offsetInBytes(true);
ShouldSetAndGetData_offsetInBytes(false);
IndexShouldSetAndGetData(true);
IndexShouldSetAndGetData(false);
IndexShouldSetAndGetData_elementCount(true);
IndexShouldSetAndGetData_elementCount(false);
IndexShouldSetAndGetData_offsetInBytes(true);
IndexShouldSetAndGetData_offsetInBytes(false);
IndexShouldSetAndGetData_startIndex(true);
IndexShouldSetAndGetData_startIndex(false);
}
public void ShouldSetAndGetData(bool dynamic)
{
var vertexBuffer = (dynamic)
?new DynamicVertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, savedData.Length, BufferUsage.None)
:new VertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, savedData.Length, BufferUsage.None);
vertexBuffer.SetData(savedData);
var readData = new VertexPositionTexture[4];
vertexBuffer.GetData(readData, 0, 4);
bool match = true;
for (int i = 0; i < savedData.Length; i++)
{
if (savedData[i] != readData[i])
{
match = false;
}
}
Console.WriteLine(match);
vertexBuffer.Dispose();
}
public void ShouldSetAndGetData_elementCount(bool dynamic)
{
var vertexBuffer = (dynamic)
? new DynamicVertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, savedData.Length, BufferUsage.None)
: new VertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, savedData.Length, BufferUsage.None);
vertexBuffer.SetData(savedData);
var readData = new VertexPositionTexture[4];
vertexBuffer.GetData(readData, 0, 2);
Console.WriteLine(savedData[0] == readData[0]);
Console.WriteLine(savedData[1] == readData[1]);
Console.WriteLine(vertexZero == readData[2]);
Console.WriteLine(vertexZero == readData[3]);
vertexBuffer.Dispose();
}
public void ShouldSetAndGetData_startIndex(bool dynamic)
{
var vertexBuffer = (dynamic)
? new DynamicVertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, savedData.Length, BufferUsage.None)
: new VertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, savedData.Length, BufferUsage.None);
vertexBuffer.SetData(savedData);
var readData = new VertexPositionTexture[4];
vertexBuffer.GetData(readData, 2, 2);
Console.WriteLine(vertexZero == readData[0]);
Console.WriteLine(vertexZero == readData[1]);
Console.WriteLine(savedData[0] == readData[2]);
Console.WriteLine(savedData[1] == readData[3]);
vertexBuffer.Dispose();
}
public void ShouldSetAndGetData_offsetInBytes(bool dynamic)
{
var vertexBuffer = (dynamic)
? new DynamicVertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, savedData.Length, BufferUsage.None)
: new VertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, savedData.Length, BufferUsage.None);
vertexBuffer.SetData(savedData);
var readData = new VertexPositionTexture[2];
var vertexStride = VertexPositionTexture.VertexDeclaration.VertexStride;
var offsetInBytes = vertexStride * 2;
vertexBuffer.GetData(offsetInBytes, readData, 0, 2, vertexStride);
Console.WriteLine(savedData[2] == readData[0]);
Console.WriteLine(savedData[3] == readData[1]);
vertexBuffer.Dispose();
}
public void IndexShouldSetAndGetData(bool dynamic)
{
var savedData = new short[] { 1, 2, 3, 4 };
var indexBuffer = (dynamic)
? new DynamicIndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, savedData.Length, BufferUsage.None)
: new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, savedData.Length, BufferUsage.None);
indexBuffer.SetData(savedData);
var readData = new short[4];
indexBuffer.GetData(readData, 0, 4);
bool match = true;
for (int i = 0; i < readData.Length; i++)
{
if (savedData[i] != readData[i])
{
match = false;
}
}
Console.WriteLine(match);
indexBuffer.Dispose();
}
public void IndexShouldSetAndGetData_elementCount(bool dynamic)
{
var savedData = new short[] { 1, 2, 3, 4 };
var indexBuffer = (dynamic)
? new DynamicIndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, savedData.Length, BufferUsage.None)
: new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, savedData.Length, BufferUsage.None);
indexBuffer.SetData(savedData);
var readData = new short[4];
indexBuffer.GetData(readData, 0, 2);
Console.WriteLine(1 == readData[0]);
Console.WriteLine(2 == readData[1]);
Console.WriteLine(0 == readData[2]);
Console.WriteLine(0 == readData[3]);
indexBuffer.Dispose();
}
public void IndexShouldSetAndGetData_startIndex(bool dynamic)
{
var savedData = new short[] { 1, 2, 3, 4 };
var indexBuffer = (dynamic)
? new DynamicIndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, savedData.Length, BufferUsage.None)
: new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, savedData.Length, BufferUsage.None);
indexBuffer.SetData(savedData);
var readData = new short[4];
indexBuffer.GetData(readData, 2, 2);
Console.WriteLine(0 == readData[0]);
Console.WriteLine(0 == readData[1]);
Console.WriteLine(1 == readData[2]);
Console.WriteLine(2 == readData[3]);
indexBuffer.Dispose();
}
public void IndexShouldSetAndGetData_offsetInBytes(bool dynamic)
{
var savedData = new short[] { 1, 2, 3, 4 };
var indexBuffer = (dynamic)
? new DynamicIndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, savedData.Length, BufferUsage.None)
: new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, savedData.Length, BufferUsage.None);
indexBuffer.SetData(savedData);
var readData = new short[2];
indexBuffer.GetData(sizeof(short) * 2, readData, 0, 2);
Console.WriteLine(3 == readData[0]);
Console.WriteLine(4 == readData[1]);
indexBuffer.Dispose();
}
public static void Main(string[] args)
{
using (GetSetBufferTest game = new GetSetBufferTest())
{
game.Run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment