Skip to content

Commit 0086120

Browse files
[Math] Improve Math.Min/Max and BoundingSphere.CreateFromAABB performance;
[Rendering] Camera uses AABB by default since no meaningful perf improvement was found and it's less accurate; [Rendering] Optimize Renderable iteration performance when clearing culling states;
1 parent 28896c8 commit 0086120

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

Engine/Staple.Core/Math/BoundingSphere.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static BoundingSphere CreateFromAABB(AABB aabb)
4848
temp = aabb.max - center;
4949
distanceSquare = Vector3.Dot(temp, temp);
5050

51-
maxDistanceSquare = Math.Max(maxDistanceSquare, distanceSquare);
51+
maxDistanceSquare = distanceSquare > maxDistanceSquare ? distanceSquare : maxDistanceSquare;
5252

5353
return new(center, Math.Sqrt(maxDistanceSquare));
5454
}

Engine/Staple.Core/Math/Math.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,23 @@ public static class Math
133133
[MethodImpl(MethodImplOptions.AggressiveInlining)]
134134
public static float Max(params float[] f)
135135
{
136-
float t;
137-
138-
if(f.Length > 0)
139-
{
140-
t = f[0];
141-
}
142-
else
136+
if(f.Length == 0)
143137
{
144138
return 0;
145139
}
146140

141+
float t = f[0];
142+
147143
var length = f.Length;
148144

149145
for(var i = 1; i < length; i++)
150146
{
151-
t = System.Math.Max(t, f[i]);
147+
var value = f[i];
148+
149+
if (value > t)
150+
{
151+
t = value;
152+
}
152153
}
153154

154155
return t;
@@ -157,22 +158,23 @@ public static float Max(params float[] f)
157158
[MethodImpl(MethodImplOptions.AggressiveInlining)]
158159
public static float Min(params float[] f)
159160
{
160-
float t;
161-
162-
if (f.Length > 0)
163-
{
164-
t = f[0];
165-
}
166-
else
161+
if (f.Length == 0)
167162
{
168163
return 0;
169164
}
170165

166+
float t = f[0];
167+
171168
var length = f.Length;
172169

173170
for (var i = 1; i < length; i++)
174171
{
175-
t = System.Math.Min(t, f[i]);
172+
var value = f[i];
173+
174+
if (value < t)
175+
{
176+
t = value;
177+
}
176178
}
177179

178180
return t;

Engine/Staple.Core/Rendering/Camera/Camera.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,7 @@ public Vector3[] Corners(Transform cameraTransform)
164164
/// <returns>Whether it's visible</returns>
165165
public bool IsVisible(AABB bounds)
166166
{
167-
#if FRUSTUM_TEST_AABB
168167
return frustumCuller.AABBTest(bounds) != FrustumResult.Invisible;
169-
#else
170-
return frustumCuller.SphereTest(BoundingSphere.CreateFromAABB(bounds)) != FrustumResult.Invisible;
171-
#endif
172168
}
173169

174170
/// <summary>

Engine/Staple.Core/Rendering/RenderSystem/RenderSystem+Internal.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ internal class DrawBucket
8181
/// <summary>
8282
/// All renderables
8383
/// </summary>
84-
private readonly List<Renderable> renderables = [];
84+
private Renderable[] renderables = new Renderable[1024];
85+
86+
/// <summary>
87+
/// Amount of renderables used
88+
/// </summary>
89+
private int renderableCount;
8590

8691
/// <summary>
8792
/// The renderer backend
@@ -251,7 +256,7 @@ public void Update()
251256
/// </summary>
252257
internal void ClearCullingStates()
253258
{
254-
for (var i = 0; i < renderables.Count; i++)
259+
for (var i = 0; i < renderableCount; i++)
255260
{
256261
renderables[i].cullingState = CullingState.None;
257262
}
@@ -282,7 +287,8 @@ public void WorldChanged()
282287
lock (lockObject)
283288
{
284289
renderQueue.Clear();
285-
renderables.Clear();
290+
291+
renderableCount = 0;
286292

287293
foreach (var systemInfo in renderSystems)
288294
{
@@ -295,7 +301,14 @@ public void WorldChanged()
295301
{
296302
if (entityInfo.Item1.TryGetComponent(systemInfo.system.RelatedComponent, out var component))
297303
{
298-
renderables.Add((Renderable)component);
304+
renderableCount++;
305+
306+
if(renderableCount > renderables.Length)
307+
{
308+
Array.Resize(ref renderables, renderables.Length * 2);
309+
}
310+
311+
renderables[renderableCount - 1] = (Renderable)component;
299312
}
300313
}
301314
}

0 commit comments

Comments
 (0)