Skip to content

Commit 61bd550

Browse files
[Rendering] Refactor Mesh MakeVertexDataBlob to improve allocations;
[Rendering] Reduce TextFont GC cost;
1 parent d974ddf commit 61bd550

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

Engine/Core/Rendering/Mesh/Mesh+Internal.cs

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Bgfx;
2+
using Staple.Internal;
23
using Staple.Utilities;
34
using System;
45
using System.Collections.Generic;
56
using System.Numerics;
7+
using System.Runtime.InteropServices;
68
using System.Text;
79

810
namespace Staple;
@@ -607,17 +609,24 @@ internal byte[] MakeVertexDataBlob(VertexLayout layout)
607609

608610
var buffer = new byte[size];
609611

610-
void Copy(byte[] source, ref int index)
612+
void Copy<T>(T source, ref int index) where T: unmanaged
611613
{
612-
if(index + source.Length > buffer.Length)
614+
var sourceSize = TypeCache.SizeOf(source.GetType().FullName);
615+
616+
if(index + sourceSize > buffer.Length)
613617
{
614-
throw new InvalidOperationException($"[Mesh] Buffer Overrun while generating vertex data blob: {index} -> {index + source.Length} "
618+
throw new InvalidOperationException($"[Mesh] Buffer Overrun while generating vertex data blob: {index} -> {index + sourceSize} "
615619
+ $"is larger than buffer {buffer.Length}");
616620
}
617621

618-
Buffer.BlockCopy(source, 0, buffer, index, source.Length);
622+
unsafe
623+
{
624+
byte* src = (byte*)&source;
625+
626+
Marshal.Copy((nint)src, buffer, index, sourceSize);
627+
}
619628

620-
index += source.Length;
629+
index += sourceSize;
621630
}
622631

623632
for (int i = 0, index = 0; i < vertices.Length; i++)
@@ -627,112 +636,82 @@ void Copy(byte[] source, ref int index)
627636
throw new InvalidOperationException("[Mesh] Exceeded expected byte count while generating vertex data blob");
628637
}
629638

630-
//Copy position
631-
Copy(BitConverter.GetBytes(vertices[i].X), ref index);
632-
Copy(BitConverter.GetBytes(vertices[i].Y), ref index);
633-
Copy(BitConverter.GetBytes(vertices[i].Z), ref index);
639+
Copy(vertices[i], ref index);
634640

635-
//Copy normals
636641
if(HasNormals)
637642
{
638-
Copy(BitConverter.GetBytes(normals[i].X), ref index);
639-
Copy(BitConverter.GetBytes(normals[i].Y), ref index);
640-
Copy(BitConverter.GetBytes(normals[i].Z), ref index);
643+
Copy(normals[i], ref index);
641644
}
642645

643646
if(HasTangents)
644647
{
645-
Copy(BitConverter.GetBytes(tangents[i].X), ref index);
646-
Copy(BitConverter.GetBytes(tangents[i].Y), ref index);
647-
Copy(BitConverter.GetBytes(tangents[i].Z), ref index);
648+
Copy(tangents[i], ref index);
648649
}
649650

650651
if (HasBitangents)
651652
{
652-
Copy(BitConverter.GetBytes(bitangents[i].X), ref index);
653-
Copy(BitConverter.GetBytes(bitangents[i].Y), ref index);
654-
Copy(BitConverter.GetBytes(bitangents[i].Z), ref index);
653+
Copy(bitangents[i], ref index);
655654
}
656655

657656
if(HasColors)
658657
{
659-
Copy(BitConverter.GetBytes(colors[i].r), ref index);
660-
Copy(BitConverter.GetBytes(colors[i].g), ref index);
661-
Copy(BitConverter.GetBytes(colors[i].b), ref index);
662-
Copy(BitConverter.GetBytes(colors[i].a), ref index);
658+
Copy(colors[i], ref index);
663659
}
664660
else if(HasColors32)
665661
{
666662
var c = (Color)colors32[i];
667663

668-
Copy(BitConverter.GetBytes(c.r), ref index);
669-
Copy(BitConverter.GetBytes(c.g), ref index);
670-
Copy(BitConverter.GetBytes(c.b), ref index);
671-
Copy(BitConverter.GetBytes(c.a), ref index);
664+
Copy(c, ref index);
672665
}
673666

674667
if(HasUV)
675668
{
676-
Copy(BitConverter.GetBytes(uv[i].X), ref index);
677-
Copy(BitConverter.GetBytes(uv[i].Y), ref index);
669+
Copy(uv[i], ref index);
678670
}
679671

680672
if (HasUV2)
681673
{
682-
Copy(BitConverter.GetBytes(uv2[i].X), ref index);
683-
Copy(BitConverter.GetBytes(uv2[i].Y), ref index);
674+
Copy(uv2[i], ref index);
684675
}
685676

686677
if (HasUV3)
687678
{
688-
Copy(BitConverter.GetBytes(uv3[i].X), ref index);
689-
Copy(BitConverter.GetBytes(uv3[i].Y), ref index);
679+
Copy(uv3[i], ref index);
690680
}
691681

692682
if (HasUV4)
693683
{
694-
Copy(BitConverter.GetBytes(uv4[i].X), ref index);
695-
Copy(BitConverter.GetBytes(uv4[i].Y), ref index);
684+
Copy(uv4[i], ref index);
696685
}
697686

698687
if (HasUV5)
699688
{
700-
Copy(BitConverter.GetBytes(uv5[i].X), ref index);
701-
Copy(BitConverter.GetBytes(uv5[i].Y), ref index);
689+
Copy(uv5[i], ref index);
702690
}
703691

704692
if (HasUV6)
705693
{
706-
Copy(BitConverter.GetBytes(uv6[i].X), ref index);
707-
Copy(BitConverter.GetBytes(uv6[i].Y), ref index);
694+
Copy(uv6[i], ref index);
708695
}
709696

710697
if (HasUV7)
711698
{
712-
Copy(BitConverter.GetBytes(uv7[i].X), ref index);
713-
Copy(BitConverter.GetBytes(uv7[i].Y), ref index);
699+
Copy(uv7[i], ref index);
714700
}
715701

716702
if (HasUV8)
717703
{
718-
Copy(BitConverter.GetBytes(uv8[i].X), ref index);
719-
Copy(BitConverter.GetBytes(uv8[i].Y), ref index);
704+
Copy(uv8[i], ref index);
720705
}
721706

722707
if(HasBoneIndices)
723708
{
724-
Copy(BitConverter.GetBytes(boneIndices[i].X), ref index);
725-
Copy(BitConverter.GetBytes(boneIndices[i].Y), ref index);
726-
Copy(BitConverter.GetBytes(boneIndices[i].Z), ref index);
727-
Copy(BitConverter.GetBytes(boneIndices[i].W), ref index);
709+
Copy(boneIndices[i], ref index);
728710
}
729711

730712
if(HasBoneWeights)
731713
{
732-
Copy(BitConverter.GetBytes(boneWeights[i].X), ref index);
733-
Copy(BitConverter.GetBytes(boneWeights[i].Y), ref index);
734-
Copy(BitConverter.GetBytes(boneWeights[i].Z), ref index);
735-
Copy(BitConverter.GetBytes(boneWeights[i].W), ref index);
714+
Copy(boneWeights[i], ref index);
736715
}
737716
}
738717

Engine/Core/Rendering/Text/TextFont.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,14 @@ public int FontSize
7070

7171
set
7272
{
73-
if(value <= 0)
73+
if(value <= 0 || fontSource.FontSize == value)
7474
{
7575
return;
7676
}
7777

7878
fontSource.FontSize = value;
7979

8080
changed = true;
81-
82-
GenerateTextureAtlas();
8381
}
8482
}
8583

@@ -91,6 +89,11 @@ public Color TextColor
9189

9290
set
9391
{
92+
if(textColor == value)
93+
{
94+
return;
95+
}
96+
9497
textColor = value;
9598

9699
changed = true;
@@ -106,6 +109,11 @@ public Color SecondaryTextColor
106109

107110
set
108111
{
112+
if (secondaryTextColor == value)
113+
{
114+
return;
115+
}
116+
109117
secondaryTextColor = value;
110118

111119
changed = true;
@@ -120,6 +128,11 @@ public Color BorderColor
120128

121129
set
122130
{
131+
if(borderColor == value)
132+
{
133+
return;
134+
}
135+
123136
borderColor = value;
124137

125138
changed = true;
@@ -135,6 +148,11 @@ public int BorderSize
135148

136149
set
137150
{
151+
if(borderSize == value)
152+
{
153+
return;
154+
}
155+
138156
borderSize = value;
139157

140158
changed = true;
@@ -154,6 +172,8 @@ internal string Key
154172
key = $"{FontSize}:{textureSize}:{TextColor.r},{TextColor.g},{TextColor.b},{TextColor.a}:" +
155173
$"{SecondaryTextColor.r},{SecondaryTextColor.g},{SecondaryTextColor.b},{SecondaryTextColor.a}:" +
156174
$"{BorderSize}:{BorderColor.r},{BorderColor.g},{BorderColor.b},{BorderColor.a}";
175+
176+
GenerateTextureAtlas();
157177
}
158178

159179
return key;
@@ -173,7 +193,7 @@ public bool MakePixelData(int fontSize, int textureSize, out byte[] bitmapData,
173193
return false;
174194
}
175195

176-
glyphs = new();
196+
glyphs = [];
177197

178198
var values = Enum.GetValues<FontCharacterSet>();
179199

0 commit comments

Comments
 (0)