Skip to content

Conversation

@Eideren
Copy link
Collaborator

@Eideren Eideren commented Sep 12, 2025

PR Details

DrawBatchPerTextureAndPass is called multiple times per frame and per instance,

if (ResourceContext.VertexBufferPosition == 0)
{
if (ResourceContext.VertexBuffer != null)
GraphicsContext.Allocator.ReleaseReference(ResourceContext.VertexBuffer);
ResourceContext.VertexBuffer = GraphicsContext.Allocator.GetTemporaryBuffer(new BufferDescription(ResourceContext.VertexCount * vertexStructSize, BufferFlags.VertexBuffer, GraphicsResourceUsage.Dynamic));
GraphicsContext.CommandList.SetVertexBuffer(0, ResourceContext.VertexBuffer, 0, vertexStructSize);
}

GetTemporaryBuffer() here will retrieve the buffer it just used in a previous call when that buffer may be in use by the GPU causing a stall when calling MapSubresource() below:
var mappedVertices = GraphicsContext.CommandList.MapSubresource(ResourceContext.VertexBuffer, 0, MapMode.WriteNoOverwrite, false, offsetVertexInBytes, vertexCount * vertexStructSize);

This PR cleans up the logic and ensures that temporary buffers are only released on the next Begin of the batcher to make sure that they are not still being used.

Performance difference goes from ~0.083ms to ~0.0013ms per call to MapSubresource. This method is called around 24 times per frame in the scene I ran, leading to a reduction of 1.96ms per frame. I expect more complex UIs would show an even larger difference.

As you may have noticed, someone commented on this performance issue. It is now fixed so I removed the comment they left.

Related Issue

N/A

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My change requires a change to the documentation.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have built and run the editor to try this change out.

@Eideren
Copy link
Collaborator Author

Eideren commented Sep 15, 2025

Replaced the SetData workaround with an actual fix, and updated the description to reflect those changes.

@xen2 xen2 merged commit b3531fe into stride3d:master Sep 18, 2025
7 checks passed
@Eideren Eideren deleted the ui_batch branch September 18, 2025 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants