Skip to content

Commit

Permalink
[perf] discard VertexBuffer in SpriteBatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
nkast committed May 30, 2024
1 parent 86aaa3c commit d496211
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions MonoGame.Framework/Graphics/.Common/SpriteBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ internal sealed class SpriteBatcher : SpriteBatcherStrategy
private DynamicVertexBuffer _vertexBuffer;
private IndexBuffer _indexBuffer;

SetDataOptions _defaultVertexBufferSetDataMode = SetDataOptions.NoOverwrite;

public override int BatchItemCount { get { return _batchItemCount; } }

public SpriteBatcher(GraphicsDevice device, int capacity = 0)
Expand All @@ -71,6 +73,17 @@ public SpriteBatcher(GraphicsDevice device, int capacity = 0)
_batchItemList[i] = new SpriteBatchItem();

EnsureArrayCapacity(capacity);

switch (_device.Adapter.Backend)
{
case GraphicsBackend.OpenGL:
case GraphicsBackend.GLES:
case GraphicsBackend.WebGL:
// NoOverwrite is not implemented in GL.
_defaultVertexBufferSetDataMode = SetDataOptions.Discard;
break;
}

}

/// <summary>
Expand Down Expand Up @@ -208,8 +221,9 @@ public unsafe override void DrawBatch(Effect effect)
{
VertexPositionColorTexture* vertexArrayPtr = vertexArrayFixedPtr;

SetDataOptions mode = SetDataOptions.NoOverwrite;
if ((_baseQuad + numBatchesToProcess) * 4 > _vertexBuffer.VertexCount)
SetDataOptions mode = _defaultVertexBufferSetDataMode;
if (mode == SetDataOptions.Discard
|| (_baseQuad + numBatchesToProcess) * 4 > _vertexBuffer.VertexCount)
{
mode = SetDataOptions.Discard;
_baseQuad = 0;
Expand Down

0 comments on commit d496211

Please sign in to comment.