MonoGame/MonoGame

GraphicsDevice.SetVertexBuffers generating garbage every frame due to params option

Open

#6,728 opened on May 4, 2019

View on GitHub
 (6 comments) (0 reactions) (0 assignees)C# (13,886 stars) (3,071 forks)batch import
DesignFeature RequestGood First IssueHelp Wanted

Description

I came across this issue when I decided to profile my (in progress) game and discovered I was generating a ton of VertexBufferBinding[] arrays during the Draw() call.

It appears as though the API GraphicsDevice.SetVertexBuffers(params VertexBufferBinding[] vertexBuffers) is generating garbage every frame due to the params option.

An example of using this API would be instancing viz.

SetVertexBuffers(vertexBinding, instanceBinding);

The params option generates a new array on every call which then needs to be garbage collected.

There is currently a workaround i.e. creating a static array and passing that to the API e.g.

VertexBufferBinding[] bindings = new VertexBufferBinding[2]; bindings[0] = blah; bindings[1] = blah;

SetVertexBuffers(bindings);

However, it's pretty easy to miss this issue, so it would probably be best to clean up the APIs to remove the params option and have a couple different API signatures e.g. one that takes a statically defined array and one that takes in both 1 VertexBufferBinding and 2 VertexBufferBindings, since those would be the most common use cases.

What version of MonoGame does the bug occur on:

  • MonoGame 3.7.1

What operating system are you using:

  • Windows

What MonoGame platform are you using:

  • DesktopGL, WindowsDX

Contributor guide