Skip to content

Commit

Permalink
Add index buffer SpriteRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
PunkPun committed Aug 28, 2023
1 parent c4b04c2 commit 7895bc5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 67 deletions.
46 changes: 11 additions & 35 deletions OpenRA.Game/Graphics/RgbaColorRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RgbaColorRenderer
static readonly float3 Offset = new(0.5f, 0.5f, 0f);

readonly SpriteRenderer parent;
readonly Vertex[] vertices = new Vertex[6];
readonly Vertex[] vertices = new Vertex[4];

public RgbaColorRenderer(SpriteRenderer parent)
{
Expand All @@ -48,11 +48,9 @@ public void DrawLine(in float3 start, in float3 end, float width, Color startCol
vertices[0] = new Vertex(start - corner + Offset, sr, sg, sb, sa, 0, 0);
vertices[1] = new Vertex(start + corner + Offset, sr, sg, sb, sa, 0, 0);
vertices[2] = new Vertex(end + corner + Offset, er, eg, eb, ea, 0, 0);
vertices[3] = new Vertex(end + corner + Offset, er, eg, eb, ea, 0, 0);
vertices[4] = new Vertex(end - corner + Offset, er, eg, eb, ea, 0, 0);
vertices[5] = new Vertex(start - corner + Offset, sr, sg, sb, sa, 0, 0);
vertices[3] = new Vertex(end - corner + Offset, er, eg, eb, ea, 0, 0);

parent.DrawRGBAVertices(vertices, blendMode);
parent.DrawRGBAQuad(vertices, blendMode);
}

public void DrawLine(in float3 start, in float3 end, float width, Color color, BlendMode blendMode = BlendMode.Alpha)
Expand All @@ -69,10 +67,8 @@ public void DrawLine(in float3 start, in float3 end, float width, Color color, B
vertices[0] = new Vertex(start - corner + Offset, r, g, b, a, 0, 0);
vertices[1] = new Vertex(start + corner + Offset, r, g, b, a, 0, 0);
vertices[2] = new Vertex(end + corner + Offset, r, g, b, a, 0, 0);
vertices[3] = new Vertex(end + corner + Offset, r, g, b, a, 0, 0);
vertices[4] = new Vertex(end - corner + Offset, r, g, b, a, 0, 0);
vertices[5] = new Vertex(start - corner + Offset, r, g, b, a, 0, 0);
parent.DrawRGBAVertices(vertices, blendMode);
vertices[3] = new Vertex(end - corner + Offset, r, g, b, a, 0, 0);
parent.DrawRGBAQuad(vertices, blendMode);
}

/// <summary>
Expand Down Expand Up @@ -160,10 +156,8 @@ void DrawConnectedLine(float3[] points, float width, Color color, bool closed, B
vertices[0] = new Vertex(ca + Offset, r, g, b, a, 0, 0);
vertices[1] = new Vertex(cb + Offset, r, g, b, a, 0, 0);
vertices[2] = new Vertex(cc + Offset, r, g, b, a, 0, 0);
vertices[3] = new Vertex(cc + Offset, r, g, b, a, 0, 0);
vertices[4] = new Vertex(cd + Offset, r, g, b, a, 0, 0);
vertices[5] = new Vertex(ca + Offset, r, g, b, a, 0, 0);
parent.DrawRGBAVertices(vertices, blendMode);
vertices[3] = new Vertex(cd + Offset, r, g, b, a, 0, 0);
parent.DrawRGBAQuad(vertices, blendMode);

// Advance line segment
end = next;
Expand Down Expand Up @@ -200,20 +194,6 @@ public void DrawRect(in float3 tl, in float3 br, float width, Color color, Blend
DrawPolygon(new[] { tl, tr, br, bl }, width, color, blendMode);
}

public void FillTriangle(in float3 a, in float3 b, in float3 c, Color color, BlendMode blendMode = BlendMode.Alpha)
{
color = Util.PremultiplyAlpha(color);
var cr = color.R / 255.0f;
var cg = color.G / 255.0f;
var cb = color.B / 255.0f;
var ca = color.A / 255.0f;

vertices[0] = new Vertex(a + Offset, cr, cg, cb, ca, 0, 0);
vertices[1] = new Vertex(b + Offset, cr, cg, cb, ca, 0, 0);
vertices[2] = new Vertex(c + Offset, cr, cg, cb, ca, 0, 0);
parent.DrawRGBAVertices(vertices, blendMode);
}

public void FillRect(in float3 tl, in float3 br, Color color, BlendMode blendMode = BlendMode.Alpha)
{
var tr = new float3(br.X, tl.Y, tl.Z);
Expand All @@ -232,22 +212,18 @@ public void FillRect(in float3 a, in float3 b, in float3 c, in float3 d, Color c
vertices[0] = new Vertex(a + Offset, cr, cg, cb, ca, 0, 0);
vertices[1] = new Vertex(b + Offset, cr, cg, cb, ca, 0, 0);
vertices[2] = new Vertex(c + Offset, cr, cg, cb, ca, 0, 0);
vertices[3] = new Vertex(c + Offset, cr, cg, cb, ca, 0, 0);
vertices[4] = new Vertex(d + Offset, cr, cg, cb, ca, 0, 0);
vertices[5] = new Vertex(a + Offset, cr, cg, cb, ca, 0, 0);
parent.DrawRGBAVertices(vertices, blendMode);
vertices[3] = new Vertex(d + Offset, cr, cg, cb, ca, 0, 0);
parent.DrawRGBAQuad(vertices, blendMode);
}

public void FillRect(in float3 a, in float3 b, in float3 c, in float3 d, Color topLeftColor, Color topRightColor, Color bottomRightColor, Color bottomLeftColor, BlendMode blendMode = BlendMode.Alpha)
{
vertices[0] = VertexWithColor(a + Offset, topLeftColor);
vertices[1] = VertexWithColor(b + Offset, topRightColor);
vertices[2] = VertexWithColor(c + Offset, bottomRightColor);
vertices[3] = VertexWithColor(c + Offset, bottomRightColor);
vertices[4] = VertexWithColor(d + Offset, bottomLeftColor);
vertices[5] = VertexWithColor(a + Offset, topLeftColor);
vertices[3] = VertexWithColor(d + Offset, bottomLeftColor);

parent.DrawRGBAVertices(vertices, blendMode);
parent.DrawRGBAQuad(vertices, blendMode);
}

static Vertex VertexWithColor(in float3 xyz, Color color)
Expand Down
38 changes: 19 additions & 19 deletions OpenRA.Game/Graphics/SpriteRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class SpriteRenderer : Renderer.IBatchRenderer
readonly Sheet[] sheets = new Sheet[SheetCount];

BlendMode currentBlend = BlendMode.Alpha;
int nv = 0;
int vertexCount = 0;
int ns = 0;

public SpriteRenderer(Renderer renderer, IShader shader)
Expand All @@ -41,7 +41,7 @@ public SpriteRenderer(Renderer renderer, IShader shader)

public void Flush()
{
if (nv > 0)
if (vertexCount > 0)
{
for (var i = 0; i < ns; i++)
{
Expand All @@ -52,11 +52,10 @@ public void Flush()
renderer.Context.SetBlendMode(currentBlend);
shader.PrepareRender();

// PERF: The renderer may choose to replace vertices with a different temporary buffer.
renderer.DrawBatch(ref vertices, nv, PrimitiveType.TriangleList);
renderer.DrawQuadBatch(ref vertices, vertexCount);
renderer.Context.SetBlendMode(BlendMode.None);

nv = 0;
vertexCount = 0;
ns = 0;
}
}
Expand All @@ -65,7 +64,7 @@ int2 SetRenderStateForSprite(Sprite s)
{
renderer.CurrentBatchRenderer = this;

if (s.BlendMode != currentBlend || nv + 6 > renderer.TempVertexBufferSize)
if (s.BlendMode != currentBlend || vertexCount + 4 > renderer.TempVertexBufferSize)
Flush();

currentBlend = s.BlendMode;
Expand Down Expand Up @@ -133,17 +132,17 @@ static float ResolveTextureIndex(Sprite s, PaletteReference pal)
internal void DrawSprite(Sprite s, float paletteTextureIndex, in float3 location, in float3 scale, float rotation = 0f)
{
var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, location + scale * s.Offset, s, samplers, paletteTextureIndex, nv, scale * s.Size, float3.Ones,
Util.FastCreateQuad(vertices, location + scale * s.Offset, s, samplers, paletteTextureIndex, vertexCount, scale * s.Size, float3.Ones,
1f, rotation);
nv += 6;
vertexCount += 4;
}

internal void DrawSprite(Sprite s, float paletteTextureIndex, in float3 location, float scale, float rotation = 0f)
{
var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, location + scale * s.Offset, s, samplers, paletteTextureIndex, nv, scale * s.Size, float3.Ones,
Util.FastCreateQuad(vertices, location + scale * s.Offset, s, samplers, paletteTextureIndex, vertexCount, scale * s.Size, float3.Ones,
1f, rotation);
nv += 6;
vertexCount += 4;
}

public void DrawSprite(Sprite s, PaletteReference pal, in float3 location, float scale = 1f, float rotation = 0f)
Expand All @@ -155,9 +154,9 @@ public void DrawSprite(Sprite s, PaletteReference pal, in float3 location, float
float rotation = 0f)
{
var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, location + scale * s.Offset, s, samplers, paletteTextureIndex, nv, scale * s.Size, tint, alpha,
Util.FastCreateQuad(vertices, location + scale * s.Offset, s, samplers, paletteTextureIndex, vertexCount, scale * s.Size, tint, alpha,
rotation);
nv += 6;
vertexCount += 4;
}

public void DrawSprite(Sprite s, PaletteReference pal, in float3 location, float scale, in float3 tint, float alpha,
Expand All @@ -169,8 +168,8 @@ public void DrawSprite(Sprite s, PaletteReference pal, in float3 location, float
internal void DrawSprite(Sprite s, float paletteTextureIndex, in float3 a, in float3 b, in float3 c, in float3 d, in float3 tint, float alpha)
{
var samplers = SetRenderStateForSprite(s);
Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, paletteTextureIndex, tint, alpha, nv);
nv += 6;
Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, paletteTextureIndex, tint, alpha, vertexCount);
vertexCount += 4;
}

public void DrawVertexBuffer(IVertexBuffer<Vertex> buffer, IIndexBuffer indices, int start, int length, IEnumerable<Sheet> sheets, BlendMode blendMode)
Expand All @@ -187,7 +186,7 @@ public void DrawVertexBuffer(IVertexBuffer<Vertex> buffer, IIndexBuffer indices,

renderer.Context.SetBlendMode(blendMode);
shader.PrepareRender();
renderer.DrawBatch(buffer, indices, length, UintSize * start);
renderer.DrawQuadBatch(buffer, indices, length, UintSize * start);
renderer.Context.SetBlendMode(BlendMode.None);
}

Expand All @@ -198,16 +197,17 @@ static void ThrowSheetOverflow(string paramName)
}

// For RGBAColorRenderer
internal void DrawRGBAVertices(Vertex[] v, BlendMode blendMode)
internal void DrawRGBAQuad(Vertex[] v, BlendMode blendMode)
{
renderer.CurrentBatchRenderer = this;

if (currentBlend != blendMode || nv + v.Length > renderer.TempVertexBufferSize)
if (currentBlend != blendMode || vertexCount + 4 > renderer.TempVertexBufferSize)
Flush();

currentBlend = blendMode;
Array.Copy(v, 0, vertices, nv, v.Length);
nv += v.Length;

Array.Copy(v, 0, vertices, vertexCount, v.Length);
vertexCount += 4;
}

public void SetPalette(ITexture palette, ITexture colorShifts)
Expand Down
20 changes: 7 additions & 13 deletions OpenRA.Game/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,18 +331,6 @@ public void EndFrame(IInputHandler inputHandler)
renderType = RenderType.None;
}

public void DrawBatch(Vertex[] vertices, int numVertices, PrimitiveType type)
{
tempBuffer.SetData(vertices, numVertices);
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
Expand All @@ -352,7 +340,13 @@ public void DrawBatch(ref Vertex[] vertices, int numVertices, PrimitiveType type
PerfHistory.Increment("batches", 1);
}

public void DrawBatch<T>(IVertexBuffer<T> vertices, IIndexBuffer indices, int numIndices, int start)
public void DrawQuadBatch(ref Vertex[] vertices, int numVertices)
{
tempVertexBuffer.SetData(ref vertices, numVertices);
DrawQuadBatch(tempVertexBuffer, quadIndexBuffer, numVertices / 4 * 6, 0);
}

public void DrawQuadBatch<T>(IVertexBuffer<T> vertices, IIndexBuffer indices, int numIndices, int start)
where T : struct
{
vertices.Bind();
Expand Down

0 comments on commit 7895bc5

Please sign in to comment.