Added display list cache to GL1TextOutputProvider. This change improves peak TextPrinter speed by more than 10x (1.6M glyphs per second as measured on a 1.8GHz Core 2 with a 8400M card). We still need a cache eviction strategy.
Modified ITextOutputProvider interface to pass TextBlocks by reference.
This commit is contained in:
parent
805b722b8c
commit
1daef17177
3 changed files with 98 additions and 73 deletions
|
@ -54,6 +54,11 @@ namespace OpenTK.Graphics.Text
|
|||
Viewport viewport = new Viewport();
|
||||
Matrix4 matrix = new Matrix4();
|
||||
|
||||
// TextBlock - display list cache.
|
||||
// Todo: we need a cache eviction strategy.
|
||||
const int block_cache_capacity = 32;
|
||||
readonly Dictionary<int, int> block_cache = new Dictionary<int, int>(block_cache_capacity);
|
||||
|
||||
bool disposed;
|
||||
|
||||
#endregion
|
||||
|
@ -71,7 +76,7 @@ namespace OpenTK.Graphics.Text
|
|||
|
||||
#region Print
|
||||
|
||||
public void Print(TextBlock block, Color color, IGlyphRasterizer rasterizer)
|
||||
public void Print(ref TextBlock block, Color color, IGlyphRasterizer rasterizer)
|
||||
{
|
||||
GL.PushAttrib(AttribMask.CurrentBit | AttribMask.TextureBit | AttribMask.EnableBit | AttribMask.ColorBufferBit | AttribMask.DepthBufferBit);
|
||||
|
||||
|
@ -83,6 +88,13 @@ namespace OpenTK.Graphics.Text
|
|||
|
||||
RectangleF position;
|
||||
|
||||
int block_hash = block.GetHashCode();
|
||||
if (block_cache.ContainsKey(block_hash))
|
||||
{
|
||||
GL.CallList(block_cache[block_hash]);
|
||||
}
|
||||
else
|
||||
{
|
||||
using (TextExtents extents = rasterizer.MeasureText(ref block))
|
||||
{
|
||||
// Build layout
|
||||
|
@ -139,6 +151,12 @@ namespace OpenTK.Graphics.Text
|
|||
}
|
||||
|
||||
// Render
|
||||
int display_list = 0;
|
||||
if ((block.Options & TextPrinterOptions.NoCache) == 0)
|
||||
{
|
||||
display_list = GL.GenLists(1);
|
||||
GL.NewList(display_list, ListMode.CompileAndExecute);
|
||||
}
|
||||
foreach (Texture2D key in active_lists.Keys)
|
||||
{
|
||||
List<Vector2> list = active_lists[key];
|
||||
|
@ -157,6 +175,11 @@ namespace OpenTK.Graphics.Text
|
|||
|
||||
GL.End();
|
||||
}
|
||||
if ((block.Options & TextPrinterOptions.NoCache) == 0)
|
||||
{
|
||||
GL.EndList();
|
||||
block_cache.Add(block_hash, display_list);
|
||||
}
|
||||
|
||||
// Clean layout
|
||||
foreach (List<Vector2> list in active_lists.Values)
|
||||
|
@ -166,6 +189,7 @@ namespace OpenTK.Graphics.Text
|
|||
}
|
||||
|
||||
active_lists.Clear();
|
||||
}
|
||||
|
||||
GL.PopAttrib();
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@ namespace OpenTK.Graphics.Text
|
|||
{
|
||||
interface ITextOutputProvider : IDisposable
|
||||
{
|
||||
void Print(TextBlock block, Color color, IGlyphRasterizer rasterizer);
|
||||
void Print(ref TextBlock block, Color color, IGlyphRasterizer rasterizer);
|
||||
void Clear();
|
||||
void Begin();
|
||||
void End();
|
||||
|
|
|
@ -134,7 +134,8 @@ namespace OpenTK.Graphics
|
|||
if (!ValidateParameters(text, font, rect))
|
||||
return;
|
||||
|
||||
TextOutput.Print(new TextBlock(text, font, rect, options, alignment, direction), color, Rasterizer);
|
||||
TextBlock block = new TextBlock(text, font, rect, options, alignment, direction);
|
||||
TextOutput.Print(ref block, color, Rasterizer);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
Loading…
Reference in a new issue