Skip to content

Commit

Permalink
debug: Add profiler markers to collider allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Feb 7, 2021
1 parent a4f6f08 commit bce1894
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;
using VisualPinball.Engine.VPT;
Expand All @@ -36,11 +37,15 @@ internal struct CircleCollider
public ColliderType Type => _header.Type;
public Entity Entity => _header.Entity;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("CircleCollider.Create");

public static void Create(BlobBuilder builder, HitCircle src, ref BlobPtr<Collider> dest, ColliderType type = ColliderType.Circle)
{
PerfMarker.Begin();
ref var ptr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<CircleCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref ptr);
collider.Init(src, type);
PerfMarker.End();
}

public static CircleCollider Create(HitCircle src, ColliderType type = ColliderType.Circle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Physics;

namespace VisualPinball.Unity
Expand All @@ -31,11 +32,15 @@ internal struct Line3DCollider
private float _zHigh;
private float3x3 _matrix;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("Line3DCollider.Create");

public static void Create(BlobBuilder builder, HitLine3D src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var linePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<Line3DCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref linePtr);
collider.Init(src);
PerfMarker.End();
}

private void Init(HitLine3D src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;
using VisualPinball.Engine.VPT;
Expand All @@ -42,11 +43,15 @@ internal struct LineCollider
public float V1y { set => _v1.y = value; }
public float V2y { set => _v2.y = value; }

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("LineCollider.Create");

public static void Create(BlobBuilder builder, LineSeg src, ref BlobPtr<Collider> dest, ColliderType type = ColliderType.Line)
{
PerfMarker.Begin();
ref var linePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<LineCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref linePtr);
collider.Init(src, type);
PerfMarker.End();
}

public static LineCollider Create(LineSeg src, ColliderType type = ColliderType.Line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Game;
using VisualPinball.Engine.Physics;

Expand All @@ -37,11 +38,15 @@ internal struct LineSlingshotCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("LineSlingshotCollider.Create");

public static void Create(BlobBuilder builder, LineSegSlingshot src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var linePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<LineSlingshotCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref linePtr);
collider.Init(src);
PerfMarker.End();
}

private void Init(LineSegSlingshot src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;

Expand All @@ -33,11 +34,15 @@ internal struct LineZCollider

public float XyY { set => _xy.y = value; }

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("LineZCollider.Create");

public static void Create(BlobBuilder builder, HitLineZ src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var linePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<LineZCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref linePtr);
collider.Init(src);
PerfMarker.End();
}

public static LineZCollider Create(HitLineZ src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;

Expand All @@ -31,11 +32,15 @@ internal struct PlaneCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("PlaneCollider.Create");

public static void Create(BlobBuilder builder, HitPlane src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var ptr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<PlaneCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref ptr);
collider.Init(src);
PerfMarker.End();
}

private void Init(HitPlane src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;

Expand All @@ -31,11 +32,15 @@ internal struct PointCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("PointCollider.Create");

public static void Create(BlobBuilder builder, HitPoint src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var colliderPtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<PointCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref colliderPtr);
collider.Init(src);
PerfMarker.End();
}

private void Init(HitPoint src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;
using VisualPinball.Engine.VPT;
Expand All @@ -37,11 +38,15 @@ internal struct TriangleCollider

public float3 Normal() => _normal;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("TriangleCollider.Create");

public static void Create(BlobBuilder builder, HitTriangle src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var trianglePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<TriangleCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref trianglePtr);
collider.Init(src);
PerfMarker.End();
}

private void Init(HitTriangle src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Linq;
using NLog;
using Unity.Entities;
using Unity.Profiling;
using UnityEngine;
using VisualPinball.Engine.Physics;
using Logger = NLog.Logger;
Expand All @@ -29,17 +30,28 @@ internal static class QuadTreeCreationSystem
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

private static readonly ProfilerMarker PerfMarkerTotal = new ProfilerMarker("QuadTreeCreationSystem");
private static readonly ProfilerMarker PerfMarkerInitItems = new ProfilerMarker("QuadTreeCreationSystem (1 - init items)");
private static readonly ProfilerMarker PerfMarkerGenerateColliders = new ProfilerMarker("QuadTreeCreationSystem (2 - generate colliders)");
private static readonly ProfilerMarker PerfMarkerCreateQuadTree = new ProfilerMarker("QuadTreeCreationSystem (3 - create quad tree)");
private static readonly ProfilerMarker PerfMarkerAllocate = new ProfilerMarker("QuadTreeCreationSystem (4 - allocate)");
private static readonly ProfilerMarker PerfMarkerSaveToEntity = new ProfilerMarker("QuadTreeCreationSystem (5 - save to entity)");

public static void Create(EntityManager entityManager)
{
PerfMarkerTotal.Begin();
var table = Object.FindObjectOfType<TableAuthoring>().Table;
var stopWatch = new Stopwatch();

stopWatch.Start();
PerfMarkerInitItems.Begin();
foreach (var playable in table.Playables) {
playable.Init(table);
}
PerfMarkerInitItems.End();

// index hittables
PerfMarkerGenerateColliders.Begin();
var hittables = table.Hittables.Where(hittable => hittable.IsCollidable).ToArray();
var hitObjects = new List<HitObject>();
var id = 0;
Expand All @@ -59,16 +71,20 @@ public static void Create(EntityManager entityManager)
}
stopWatch.Stop();
Logger.Info("Collider Count:\n" + log + "\nTotal: " + c + " colliders in " + stopWatch.ElapsedMilliseconds + "ms");
PerfMarkerGenerateColliders.End();

// construct quad tree
PerfMarkerCreateQuadTree.Begin();
var quadTree = new Engine.Physics.QuadTree(hitObjects, table.BoundingBox);
var quadTreeBlobAssetRef = QuadTreeBlob.CreateBlobAssetReference(
quadTree,
table.GeneratePlayfieldHit(), // todo use `null` if separate playfield mesh exists
table.GenerateGlassHit()
);
PerfMarkerCreateQuadTree.End();

// playfield and glass need special treatment, since not part of the quad tree
PerfMarkerAllocate.Begin();
var playfieldHitObject = table.GeneratePlayfieldHit();
var glassHitObject = table.GenerateGlassHit();
playfieldHitObject.Id = id++;
Expand All @@ -78,14 +94,18 @@ public static void Create(EntityManager entityManager)

// construct collider blob
var colliderBlob = ColliderBlob.CreateBlobAssetReference(hitObjects, playfieldHitObject.Id, glassHitObject.Id);
PerfMarkerAllocate.End();

// save it to entity
PerfMarkerSaveToEntity.Begin();
var collEntity = entityManager.CreateEntity(ComponentType.ReadOnly<QuadTreeData>(), ComponentType.ReadOnly<ColliderData>());
//DstEntityManager.SetName(collEntity, "Collision Data Holder");
entityManager.SetComponentData(collEntity, new QuadTreeData { Value = quadTreeBlobAssetRef });
entityManager.SetComponentData(collEntity, new ColliderData { Value = colliderBlob });
PerfMarkerSaveToEntity.End();

Logger.Info("Static QuadTree initialized.");
PerfMarkerTotal.End();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Game;
using VisualPinball.Engine.VPT.Flipper;
Expand All @@ -34,11 +35,15 @@ internal struct FlipperCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("FlipperCollider.Create");

public static void Create(BlobBuilder builder, FlipperHit src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var ptr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<FlipperCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref ptr);
collider.Init(src);
PerfMarker.End();
}

private void Init(FlipperHit src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.VPT.Gate;

namespace VisualPinball.Unity
Expand All @@ -31,11 +32,15 @@ internal struct GateCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("GateCollider.Create");

public static void Create(BlobBuilder builder, GateHit src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var ptr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<GateCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref ptr);
collider.Init(src);
PerfMarker.End();
}

private void Init(GateHit src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.VPT.Plunger;

Expand All @@ -32,11 +33,15 @@ internal struct PlungerCollider

public ColliderType Type => ColliderType.Plunger;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("PlungerCollider.Create");

public static void Create(BlobBuilder builder, PlungerHit src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var ptr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<PlungerCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref ptr);
collider.Init(src);
PerfMarker.End();
}

private void Init(PlungerHit src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.VPT.Spinner;

namespace VisualPinball.Unity
Expand All @@ -30,11 +31,15 @@ internal struct SpinnerCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("SpinnerCollider.Create");

public static void Create(BlobBuilder builder, SpinnerHit src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var ptr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<SpinnerCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref ptr);
collider.Init(src);
PerfMarker.End();
}

private void Init(SpinnerHit src)
Expand Down

0 comments on commit bce1894

Please sign in to comment.