From cf43581bac813c01bcdebe111d06280e4fa9b37f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sat, 1 Nov 2014 16:13:41 +0100 Subject: [PATCH 1/4] measure sprite font performance --- OpenRA.Game/Graphics/Renderer.cs | 3 ++- OpenRA.Game/Graphics/SpriteFont.cs | 11 +++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/OpenRA.Game/Graphics/Renderer.cs b/OpenRA.Game/Graphics/Renderer.cs index e200b2329d..8a99aef2fb 100644 --- a/OpenRA.Game/Graphics/Renderer.cs +++ b/OpenRA.Game/Graphics/Renderer.cs @@ -60,7 +60,8 @@ namespace OpenRA.Graphics public void InitializeFonts(Manifest m) { - Fonts = m.Fonts.ToDictionary(x => x.Key, x => new SpriteFont(Platform.ResolvePath(x.Value.First), x.Value.Second)); + using (new Support.PerfTimer("SpriteFonts")) + Fonts = m.Fonts.ToDictionary(x => x.Key, x => new SpriteFont(Platform.ResolvePath(x.Value.First), x.Value.Second)); } internal IGraphicsDevice Device { get { return device; } } diff --git a/OpenRA.Game/Graphics/SpriteFont.cs b/OpenRA.Game/Graphics/SpriteFont.cs index b9d50aa94b..7bcce0b0af 100644 --- a/OpenRA.Game/Graphics/SpriteFont.cs +++ b/OpenRA.Game/Graphics/SpriteFont.cs @@ -12,6 +12,7 @@ using System; using System.Drawing; using System.Linq; using OpenRA.Primitives; +using OpenRA.Support; using SharpFont; namespace OpenRA.Graphics @@ -19,10 +20,12 @@ namespace OpenRA.Graphics public class SpriteFont { int size; + string name; public SpriteFont(string name, int size) { this.size = size; + this.name = name; face = library.NewFace(name, 0); face.SetPixelSizes((uint)size, (uint)size); @@ -41,10 +44,10 @@ namespace OpenRA.Graphics void PrecacheColor(Color c) { - // precache glyphs for U+0020 - U+007f - for (var n = (char)0x20; n < (char)0x7f; n++) - if (glyphs[Pair.New(n, c)] == null) - throw new InvalidOperationException(); + using (new PerfTimer("PrecacheColor {0} {1}px {2}".F(name, size, c.Name))) + for (var n = (char)0x20; n < (char)0x7f; n++) + if (glyphs[Pair.New(n, c)] == null) + throw new InvalidOperationException(); } public void DrawText(string text, float2 location, Color c) From c7f5b24593c31162fac44b7e62ac414e8ff64d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sat, 1 Nov 2014 15:55:55 +0100 Subject: [PATCH 2/4] use LoadChar --- OpenRA.Game/Graphics/SpriteFont.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/OpenRA.Game/Graphics/SpriteFont.cs b/OpenRA.Game/Graphics/SpriteFont.cs index 7bcce0b0af..1a01339b2b 100644 --- a/OpenRA.Game/Graphics/SpriteFont.cs +++ b/OpenRA.Game/Graphics/SpriteFont.cs @@ -97,8 +97,7 @@ namespace OpenRA.Graphics GlyphInfo CreateGlyph(Pair c) { - var index = face.GetCharIndex(c.First); - face.LoadGlyph(index, LoadFlags.Default, LoadTarget.Normal); + face.LoadChar(c.First, LoadFlags.Default, LoadTarget.Normal); face.Glyph.RenderGlyph(RenderMode.Normal); var size = new Size((int)face.Glyph.Metrics.Width >> 6, (int)face.Glyph.Metrics.Height >> 6); From 83ed06e015e397e062e4d8ea549c54642e2da651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sat, 1 Nov 2014 16:35:42 +0100 Subject: [PATCH 3/4] C#ify --- OpenRA.Game/Graphics/SpriteFont.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenRA.Game/Graphics/SpriteFont.cs b/OpenRA.Game/Graphics/SpriteFont.cs index 1a01339b2b..e194a17a63 100644 --- a/OpenRA.Game/Graphics/SpriteFont.cs +++ b/OpenRA.Game/Graphics/SpriteFont.cs @@ -27,7 +27,7 @@ namespace OpenRA.Graphics this.size = size; this.name = name; - face = library.NewFace(name, 0); + face = new Face(library, name); face.SetPixelSizes((uint)size, (uint)size); glyphs = new Cache, GlyphInfo>(CreateGlyph, From 44b485606f07cd51061987e8a3a559d85d4c2396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sat, 1 Nov 2014 16:36:51 +0100 Subject: [PATCH 4/4] StyleCop --- OpenRA.Game/Graphics/SpriteFont.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/OpenRA.Game/Graphics/SpriteFont.cs b/OpenRA.Game/Graphics/SpriteFont.cs index e194a17a63..f246fdae64 100644 --- a/OpenRA.Game/Graphics/SpriteFont.cs +++ b/OpenRA.Game/Graphics/SpriteFont.cs @@ -19,6 +19,9 @@ namespace OpenRA.Graphics { public class SpriteFont { + static Library library = new Library(); + static SheetBuilder builder; + int size; string name; @@ -30,8 +33,7 @@ namespace OpenRA.Graphics face = new Face(library, name); face.SetPixelSizes((uint)size, (uint)size); - glyphs = new Cache, GlyphInfo>(CreateGlyph, - Pair.EqualityComparer); + glyphs = new Cache, GlyphInfo>(CreateGlyph, Pair.EqualityComparer); // setup a SheetBuilder for our private use // TODO: SheetBuilder state is leaked between mod switches @@ -92,7 +94,7 @@ namespace OpenRA.Graphics return new int2((int)Math.Ceiling(lines.Max(s => s.Sum(a => glyphs[Pair.New(a, Color.White)].Advance))), lines.Length * size); } - Cache, GlyphInfo> glyphs; + Cache, GlyphInfo> glyphs; Face face; GlyphInfo CreateGlyph(Pair c) @@ -112,6 +114,7 @@ namespace OpenRA.Graphics // A new bitmap is generated each time this property is accessed, so we do need to dispose it. using (var bitmap = face.Glyph.Bitmap) + { unsafe { var p = (byte*)bitmap.Buffer; @@ -133,13 +136,12 @@ namespace OpenRA.Graphics p += bitmap.Pitch; } } + } + s.sheet.CommitData(); return g; } - - static Library library = new Library(); - static SheetBuilder builder; } class GlyphInfo