Skip to content

Commit 2108284

Browse files
committed
Faster MSDF kerning, sky dome allocations
1 parent ce7e510 commit 2108284

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

Other/libs/MSDFData/FieldFont.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ public FieldFont(string name, IReadOnlyCollection<FieldGlyph> glyphs, IReadOnlyC
5656
[ContentSerializerIgnore]
5757
public IEnumerable<char> SupportedCharacters => this.Glyphs.Keys;
5858

59-
private Dictionary<string, KerningPair> StringToPairBackend;
59+
private Dictionary<int, KerningPair> StringToPairBackend;
6060
[ContentSerializerIgnore]
61-
public Dictionary<string, KerningPair> StringToPair {
62-
get {
61+
public Dictionary<int, KerningPair> StringToPair {
62+
get
63+
{
6364
if (StringToPairBackend == null)
6465
{
65-
StringToPairBackend = KerningPairs.ToDictionary(x => new string(new char[] { x.Left, x.Right }));
66+
StringToPairBackend = KerningPairs.ToDictionary(x => x.Left | (x.Right << 16));
6667
}
68+
6769
return StringToPairBackend;
6870
}
6971
}

TSOClient/FSO.UI/Framework/MSDFFont.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void Draw(GraphicsDevice gd, string text, Vector2 pos, Color color, Vecto
201201
if (next.Glyph != null)
202202
{
203203
KerningPair pair;
204-
if (pairs.TryGetValue(new string(new char[] { fglyph.Character, next.Glyph.Character }), out pair))
204+
if (pairs.TryGetValue(fglyph.Character | (next.Glyph.Character << 16), out pair))
205205
{
206206
point.X += pair.Advance * subScale;
207207
}
@@ -284,7 +284,7 @@ public Vector2 MeasureString(string text)
284284
if (next.Glyph != null)
285285
{
286286
KerningPair pair;
287-
if (pairs.TryGetValue(new string(new char[] { glyph.Glyph.Character, next.Glyph.Character }), out pair))
287+
if (pairs.TryGetValue(glyph.Glyph.Character | (next.Glyph.Character << 16), out pair))
288288
{
289289
size.X += pair.Advance * subScale;
290290
}
@@ -367,6 +367,6 @@ public MSDFInfo(FieldFont Font)
367367
public float cutY;
368368

369369
public int atlasWidth;
370-
public Dictionary<string, KerningPair> pairs;
370+
public Dictionary<int, KerningPair> pairs;
371371
}
372372
}

TSOClient/tso.world/Components/AbstractSkyDome.cs

+35-17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class AbstractSkyDome : IDisposable
2121
private int PrimCount;
2222
private float LastSkyPos;
2323

24+
private VertexPositionTexture[] VertexData;
25+
private int[] IndexData;
26+
2427
public AbstractSkyDome(GraphicsDevice GD, float time)
2528
{
2629
float? customSky = DynamicTuning.Global?.GetTuning("city", 0, 2);
@@ -29,7 +32,8 @@ public AbstractSkyDome(GraphicsDevice GD, float time)
2932
{
3033
TryLoadSkyColor(GD, DefaultSkyCol);
3134
}
32-
35+
36+
InitArrays();
3337
BuildSkyDome(GD, time);
3438
}
3539

@@ -94,12 +98,23 @@ public bool Night(float tod)
9498
return night;
9599
}
96100

101+
private void InitArrays()
102+
{
103+
var subdivs = 65;
104+
105+
int vertCount = (subdivs - 1) * (subdivs + 1) + 1;
106+
int indexCount = ((subdivs - 1) * (subdivs * 6 - 3)) - 3;
107+
108+
VertexData = new VertexPositionTexture[vertCount];
109+
IndexData = new int[indexCount];
110+
}
111+
97112
public void BuildSkyDome(GraphicsDevice GD, float time)
98113
{
99114
//generate sky dome geometry
100115
var subdivs = 65;
101-
List<VertexPositionTexture> verts = new List<VertexPositionTexture>();
102-
var indices = new List<int>();
116+
VertexPositionTexture[] verts = VertexData;
117+
int[] indices = IndexData;
103118
var skyCol = new Color(0x00, 0x80, 0xFF, 0xFF);
104119

105120
float skyPos = OutsideSkyP(time);
@@ -112,11 +127,14 @@ public void BuildSkyDome(GraphicsDevice GD, float time)
112127
var topRange = 0.9f * range;
113128
var btmRange = 0.1f * range;
114129

115-
verts.Add(new VertexPositionTexture(new Vector3(0, 1, 0), new Vector2(skyPos, yGap)));
130+
int vi = 0;
131+
int ii = 0;
132+
133+
verts[vi++] = new VertexPositionTexture(new Vector3(0, 1, 0), new Vector2(skyPos, yGap));
116134

117135
for (int y = 1; y < subdivs; y++)
118136
{
119-
int start = verts.Count;
137+
int start = vi;
120138
var angley = (float)Math.PI * y / ((float)subdivs - 1);
121139
var radius = (float)Math.Sin(angley);
122140
var height = Math.Cos(angley);
@@ -129,32 +147,32 @@ public void BuildSkyDome(GraphicsDevice GD, float time)
129147
{
130148
var anglex = (float)Math.PI * x * 2 / (float)subdivs;
131149
var colLerp = Math.Min(1, Math.Abs(((y - 2) / (float)subdivs) - 0.60f) * 4);
132-
verts.Add(new VertexPositionTexture(new Vector3((float)Math.Sin(anglex) * radius, (float)height, (float)Math.Cos(anglex) * radius), new Vector2(skyPos, tpos)));
150+
verts[vi++] = new VertexPositionTexture(new Vector3((float)Math.Sin(anglex) * radius, (float)height, (float)Math.Cos(anglex) * radius), new Vector2(skyPos, tpos));
133151
if (x < subdivs)
134152
{
135153
if (y != 1)
136154
{
137-
indices.Add(vertLastStart + x % vertLastLength);
138-
indices.Add(vertLastStart + (x + 1) % vertLastLength);
139-
indices.Add(verts.Count - 1);
155+
indices[ii++] = vertLastStart + x % vertLastLength;
156+
indices[ii++] = vertLastStart + (x + 1) % vertLastLength;
157+
indices[ii++] = vi - 1;
140158
}
141159

142-
indices.Add(vertLastStart + (x + 1) % vertLastLength);
143-
indices.Add(verts.Count);
144-
indices.Add(verts.Count - 1);
160+
indices[ii++] = vertLastStart + (x + 1) % vertLastLength;
161+
indices[ii++] = vi;
162+
indices[ii++] = vi - 1;
145163
}
146164
}
147165
vertLastStart = start;
148166
vertLastLength = subdivs + 1;
149167
}
150168

151-
if (Verts == null) Verts = new VertexBuffer(GD, typeof(VertexPositionTexture), verts.Count, BufferUsage.None);
152-
Verts.SetData(verts.ToArray());
169+
if (Verts == null) Verts = new VertexBuffer(GD, typeof(VertexPositionTexture), vi, BufferUsage.None);
170+
Verts.SetData(verts);
153171
if (Indices == null)
154172
{
155-
Indices = new IndexBuffer(GD, IndexElementSize.ThirtyTwoBits, indices.Count, BufferUsage.None);
156-
Indices.SetData(indices.ToArray());
157-
PrimCount = indices.Count / 3;
173+
Indices = new IndexBuffer(GD, IndexElementSize.ThirtyTwoBits, ii, BufferUsage.None);
174+
Indices.SetData(indices);
175+
PrimCount = ii / 3;
158176
}
159177
}
160178

0 commit comments

Comments
 (0)