Merge pull request #8193 from RoosterDragon/line-render-perf

Improve performance of line rendering
This commit is contained in:
Pavel Penev
2015-05-22 17:51:11 +03:00
12 changed files with 138 additions and 100 deletions

View File

@@ -9,6 +9,7 @@
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
namespace OpenRA.Graphics
@@ -69,10 +70,25 @@ namespace OpenRA.Graphics
{
var tr = new float2(br.X, tl.Y);
var bl = new float2(tl.X, br.Y);
DrawLine(tl, tr, c, c);
DrawLine(tl, bl, c, c);
DrawLine(tr, br, c, c);
DrawLine(bl, br, c, c);
DrawLine(tl, tr, c);
DrawLine(tl, bl, c);
DrawLine(tr, br, c);
DrawLine(bl, br, c);
}
public void DrawLine(float2 start, float2 end, Color color)
{
renderer.CurrentBatchRenderer = this;
if (nv + 2 > renderer.TempBufferSize)
Flush();
var r = color.R / 255.0f;
var g = color.G / 255.0f;
var b = color.B / 255.0f;
var a = color.A / 255.0f;
vertices[nv++] = new Vertex(start + Offset, r, g, b, a);
vertices[nv++] = new Vertex(end + Offset, r, g, b, a);
}
public void DrawLine(float2 start, float2 end, Color startColor, Color endColor)
@@ -82,19 +98,52 @@ namespace OpenRA.Graphics
if (nv + 2 > renderer.TempBufferSize)
Flush();
vertices[nv++] = new Vertex(start + Offset,
startColor.R / 255.0f, startColor.G / 255.0f,
startColor.B / 255.0f, startColor.A / 255.0f);
var r = startColor.R / 255.0f;
var g = startColor.G / 255.0f;
var b = startColor.B / 255.0f;
var a = startColor.A / 255.0f;
vertices[nv++] = new Vertex(start + Offset, r, g, b, a);
vertices[nv++] = new Vertex(end + Offset,
endColor.R / 255.0f, endColor.G / 255.0f,
endColor.B / 255.0f, endColor.A / 255.0f);
r = endColor.R / 255.0f;
g = endColor.G / 255.0f;
b = endColor.B / 255.0f;
a = endColor.A / 255.0f;
vertices[nv++] = new Vertex(end + Offset, r, g, b, a);
}
public void DrawLineStrip(IEnumerable<float2> points, Color color)
{
renderer.CurrentBatchRenderer = this;
var r = color.R / 255.0f;
var g = color.G / 255.0f;
var b = color.B / 255.0f;
var a = color.A / 255.0f;
var first = true;
var prev = new Vertex();
foreach (var point in points)
{
if (first)
{
first = false;
prev = new Vertex(point + Offset, r, g, b, a);
continue;
}
if (nv + 2 > renderer.TempBufferSize)
Flush();
vertices[nv++] = prev;
prev = new Vertex(point + Offset, r, g, b, a);
vertices[nv++] = prev;
}
}
public void FillRect(RectangleF r, Color color)
{
for (var y = r.Top; y < r.Bottom; y++)
DrawLine(new float2(r.Left, y), new float2(r.Right, y), color, color);
DrawLine(new float2(r.Left, y), new float2(r.Right, y), color);
}
public void FillEllipse(RectangleF r, Color color)
@@ -106,7 +155,7 @@ namespace OpenRA.Graphics
for (var y = r.Top; y <= r.Bottom; y++)
{
var dx = a * (float)Math.Sqrt(1 - (y - yc) * (y - yc) / b / b);
DrawLine(new float2(xc - dx, y), new float2(xc + dx, y), color, color);
DrawLine(new float2(xc - dx, y), new float2(xc + dx, y), color);
}
}

View File

@@ -65,13 +65,13 @@ namespace OpenRA.Graphics
var z = float2.Lerp(start, end, value);
var wlr = Game.Renderer.WorldLineRenderer;
wlr.DrawLine(start + p, end + p, c, c);
wlr.DrawLine(start + q, end + q, c2, c2);
wlr.DrawLine(start + r, end + r, c, c);
wlr.DrawLine(start + p, end + p, c);
wlr.DrawLine(start + q, end + q, c2);
wlr.DrawLine(start + r, end + r, c);
wlr.DrawLine(start + p, z + p, barColor2, barColor2);
wlr.DrawLine(start + q, z + q, barColor, barColor);
wlr.DrawLine(start + r, z + r, barColor2, barColor2);
wlr.DrawLine(start + p, z + p, barColor2);
wlr.DrawLine(start + q, z + q, barColor);
wlr.DrawLine(start + r, z + r, barColor2);
}
Color GetHealthColor(Health health)
@@ -125,13 +125,13 @@ namespace OpenRA.Graphics
var z = float2.Lerp(start, end, (float)health.HP / health.MaxHP);
var wlr = Game.Renderer.WorldLineRenderer;
wlr.DrawLine(start + p, end + p, c, c);
wlr.DrawLine(start + q, end + q, c2, c2);
wlr.DrawLine(start + r, end + r, c, c);
wlr.DrawLine(start + p, end + p, c);
wlr.DrawLine(start + q, end + q, c2);
wlr.DrawLine(start + r, end + r, c);
wlr.DrawLine(start + p, z + p, healthColor2, healthColor2);
wlr.DrawLine(start + q, z + q, healthColor, healthColor);
wlr.DrawLine(start + r, z + r, healthColor2, healthColor2);
wlr.DrawLine(start + p, z + p, healthColor2);
wlr.DrawLine(start + q, z + q, healthColor);
wlr.DrawLine(start + r, z + r, healthColor2);
if (health.DisplayHp != health.HP)
{
@@ -143,9 +143,9 @@ namespace OpenRA.Graphics
deltaColor.B / 2);
var zz = float2.Lerp(start, end, (float)health.DisplayHp / health.MaxHP);
wlr.DrawLine(z + p, zz + p, deltaColor2, deltaColor2);
wlr.DrawLine(z + q, zz + q, deltaColor, deltaColor);
wlr.DrawLine(z + r, zz + r, deltaColor2, deltaColor2);
wlr.DrawLine(z + p, zz + p, deltaColor2);
wlr.DrawLine(z + q, zz + q, deltaColor);
wlr.DrawLine(z + r, zz + r, deltaColor2);
}
}

View File

@@ -55,15 +55,15 @@ namespace OpenRA.Graphics
var v = new float2(0, 4f / wr.Viewport.Zoom);
var wlr = Game.Renderer.WorldLineRenderer;
wlr.DrawLine(tl + u, tl, color, color);
wlr.DrawLine(tl, tl + v, color, color);
wlr.DrawLine(tr, tr - u, color, color);
wlr.DrawLine(tr, tr + v, color, color);
wlr.DrawLine(tl + u, tl, color);
wlr.DrawLine(tl, tl + v, color);
wlr.DrawLine(tr, tr - u, color);
wlr.DrawLine(tr, tr + v, color);
wlr.DrawLine(bl, bl + u, color, color);
wlr.DrawLine(bl, bl - v, color, color);
wlr.DrawLine(br, br - u, color, color);
wlr.DrawLine(br, br - v, color, color);
wlr.DrawLine(bl, bl + u, color);
wlr.DrawLine(bl, bl - v, color);
wlr.DrawLine(br, br - u, color);
wlr.DrawLine(br, br - v, color);
}
public void RenderDebugGeometry(WorldRenderer wr) { }

View File

@@ -47,7 +47,7 @@ namespace OpenRA.Graphics
var a = first;
foreach (var b in waypoints.Skip(1).Select(pos => wr.ScreenPxPosition(pos)))
{
Game.Renderer.WorldLineRenderer.DrawLine(a, b, color, color);
Game.Renderer.WorldLineRenderer.DrawLine(a, b, color);
wr.DrawTargetMarker(color, b);
a = b;
}

View File

@@ -202,7 +202,7 @@ namespace OpenRA.Graphics
{
var pa = pos + offset.Rotate(WRot.FromFacing(8 * i));
var pb = pos + offset.Rotate(WRot.FromFacing(8 * i + 6));
Game.Renderer.WorldLineRenderer.DrawLine(ScreenPosition(pa), ScreenPosition(pb), c, c);
Game.Renderer.WorldLineRenderer.DrawLine(ScreenPosition(pa), ScreenPosition(pb), c);
}
}
@@ -214,10 +214,10 @@ namespace OpenRA.Graphics
var tr = new float2(br.X, tl.Y);
var wlr = Game.Renderer.WorldLineRenderer;
wlr.DrawLine(location + tl, location + tr, c, c);
wlr.DrawLine(location + tr, location + br, c, c);
wlr.DrawLine(location + br, location + bl, c, c);
wlr.DrawLine(location + bl, location + tl, c, c);
wlr.DrawLine(location + tl, location + tr, c);
wlr.DrawLine(location + tr, location + br, c);
wlr.DrawLine(location + br, location + bl, c);
wlr.DrawLine(location + bl, location + tl, c);
}
public void RefreshPalette()