1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 14:38:34 +01:00

don't recreate the VertexPositionColorTexture array every time

This commit is contained in:
Ell 2021-10-19 23:12:56 +02:00
parent b271518956
commit 04dc2bf2d0

View file

@ -12,6 +12,10 @@ namespace MLEM.Misc {
/// </summary> /// </summary>
public class StaticSpriteBatch : IDisposable { public class StaticSpriteBatch : IDisposable {
// this maximum is limited by indices being a short
private const int MaxBatchItems = short.MaxValue / 6;
private static readonly VertexPositionColorTexture[] Data = new VertexPositionColorTexture[MaxBatchItems * 4];
/// <summary> /// <summary>
/// The amount of vertices that are currently batched /// The amount of vertices that are currently batched
/// </summary> /// </summary>
@ -63,28 +67,24 @@ namespace MLEM.Misc {
return; return;
this.batchChanged = false; this.batchChanged = false;
// this maximum is limited by indices being a short
const int maxBatchItems = short.MaxValue / 6;
// ensure we have enough vertex buffers // ensure we have enough vertex buffers
var requiredBuffers = (this.vertices.Count / (maxBatchItems * 4F)).Ceil(); var requiredBuffers = (this.vertices.Count / (MaxBatchItems * 4F)).Ceil();
while (this.vertexBuffers.Count < requiredBuffers) while (this.vertexBuffers.Count < requiredBuffers)
this.vertexBuffers.Add(new VertexBuffer(this.graphicsDevice, VertexPositionColorTexture.VertexDeclaration, maxBatchItems * 4, BufferUsage.WriteOnly)); this.vertexBuffers.Add(new VertexBuffer(this.graphicsDevice, VertexPositionColorTexture.VertexDeclaration, MaxBatchItems * 4, BufferUsage.WriteOnly));
// fill vertex buffers // fill vertex buffers
var arrayIndex = 0; var arrayIndex = 0;
var totalIndex = 0; var totalIndex = 0;
var data = new VertexPositionColorTexture[maxBatchItems * 4];
while (totalIndex < this.vertices.Count) { while (totalIndex < this.vertices.Count) {
var now = Math.Min(this.vertices.Count - totalIndex, data.Length); var now = Math.Min(this.vertices.Count - totalIndex, Data.Length);
for (var i = 0; i < now; i++) for (var i = 0; i < now; i++)
data[i] = this.vertices[totalIndex + i]; Data[i] = this.vertices[totalIndex + i];
this.vertexBuffers[arrayIndex++].SetData(data); this.vertexBuffers[arrayIndex++].SetData(Data);
totalIndex += now; totalIndex += now;
} }
// ensure we have enough indices // ensure we have enough indices
var maxItems = Math.Min(this.vertices.Count / 4, maxBatchItems); var maxItems = Math.Min(this.vertices.Count / 4, MaxBatchItems);
// each item has 2 triangles which each have 3 indices // each item has 2 triangles which each have 3 indices
if (this.indices == null || this.indices.IndexCount < 6 * maxItems) { if (this.indices == null || this.indices.IndexCount < 6 * maxItems) {
var newIndices = new short[6 * maxItems]; var newIndices = new short[6 * maxItems];