SpriteRenderer, do not copy vertex array data each flush.

This commit is contained in:
Vapre
2021-07-17 00:03:12 +02:00
committed by abcdefg30
parent 243e2b2a2a
commit 7005da3592
6 changed files with 43 additions and 3 deletions

View File

@@ -84,6 +84,7 @@ namespace OpenRA
public interface IGraphicsContext : IDisposable
{
IVertexBuffer<Vertex> CreateVertexBuffer(int size);
Vertex[] CreateVertices(int size);
ITexture CreateTexture();
IFrameBuffer CreateFrameBuffer(Size s);
IFrameBuffer CreateFrameBuffer(Size s, Color clearColor);
@@ -105,6 +106,11 @@ namespace OpenRA
{
void Bind();
void SetData(T[] vertices, int length);
/// <summary>
/// Upon return `vertices` may reference another array object of at least the same size - containing random values.
/// </summary>
void SetData(ref T[] vertices, int length);
void SetData(T[] vertices, int offset, int start, int length);
}

View File

@@ -23,7 +23,7 @@ namespace OpenRA.Graphics
readonly Renderer renderer;
readonly IShader shader;
readonly Vertex[] vertices;
Vertex[] vertices;
readonly Sheet[] sheets = new Sheet[SheetCount];
BlendMode currentBlend = BlendMode.Alpha;
@@ -34,7 +34,7 @@ namespace OpenRA.Graphics
{
this.renderer = renderer;
this.shader = shader;
vertices = new Vertex[renderer.TempBufferSize];
vertices = renderer.Context.CreateVertices(renderer.TempBufferSize);
}
public void Flush()
@@ -49,7 +49,9 @@ namespace OpenRA.Graphics
renderer.Context.SetBlendMode(currentBlend);
shader.PrepareRender();
renderer.DrawBatch(vertices, nv, PrimitiveType.TriangleList);
// PERF: The renderer may choose to replace vertices with a different temporary buffer.
renderer.DrawBatch(ref vertices, nv, PrimitiveType.TriangleList);
renderer.Context.SetBlendMode(BlendMode.None);
nv = 0;

View File

@@ -333,6 +333,12 @@ namespace OpenRA
DrawBatch(tempBuffer, 0, numVertices, type);
}
public void DrawBatch(ref Vertex[] vertices, int numVertices, PrimitiveType type)
{
tempBuffer.SetData(ref vertices, numVertices);
DrawBatch(tempBuffer, 0, numVertices, type);
}
public void DrawBatch<T>(IVertexBuffer<T> vertices,
int firstVertex, int numVertices, PrimitiveType type)
where T : struct

View File

@@ -67,6 +67,12 @@ namespace OpenRA.Platforms.Default
return new VertexBuffer<Vertex>(size);
}
public Vertex[] CreateVertices(int size)
{
VerifyThreadAffinity();
return new Vertex[size];
}
public ITexture CreateTexture()
{
VerifyThreadAffinity();

View File

@@ -404,6 +404,11 @@ namespace OpenRA.Platforms.Default
return Send(getCreateVertexBuffer, length);
}
public Vertex[] CreateVertices(int size)
{
return GetVertices(size);
}
public void DisableDepthBuffer()
{
Post(doDisableDepthBuffer);
@@ -526,6 +531,16 @@ namespace OpenRA.Platforms.Default
device.Post(setData1, (buffer, length));
}
/// <summary>
/// PERF: The vertices array is passed without copying to the render thread. Upon return `vertices` may reference another
/// array object of at least the same size - containing random values.
/// </summary>
public void SetData(ref Vertex[] vertices, int length)
{
device.Post(setData1, (vertices, length));
vertices = device.GetVertices(vertices.Length);
}
public void SetData(Vertex[] vertices, int offset, int start, int length)
{
if (length <= device.BatchSize)

View File

@@ -60,6 +60,11 @@ namespace OpenRA.Platforms.Default
SetData(data, 0, 0, length);
}
public void SetData(ref T[] data, int length)
{
SetData(data, 0, 0, length);
}
public void SetData(T[] data, int offset, int start, int length)
{
Bind();