0.2.0-dev Download ZIP
79 lines ·3.0 KB Raw
| 1 | using System.Diagnostics; |
| 2 | using EvilEngine.Conversion; |
| 3 | using EvilEngine.Formats.Records; |
| 4 | using EvilEngine.Host; |
| 5 | using EvilEngine.Host.Collision; |
| 6 | using Godot; |
| 7 | |
| 8 | namespace EvilEngine.Shared.World; |
| 9 | |
| 10 | public sealed record RenderWait(string Where, long Count, TimeSpan Took, long PoolCount, TimeSpan PoolTook) |
| 11 | { |
| 12 | public long OwnCount => Count - PoolCount; |
| 13 | |
| 14 | public TimeSpan OwnTook => Took - PoolTook; |
| 15 | } |
| 16 | |
| 17 | public sealed record MapStartTimes( |
| 18 | TimeSpan Load, |
| 19 | TimeSpan Collision, |
| 20 | TimeSpan World, |
| 21 | TimeSpan Total, |
| 22 | IReadOnlyList<RenderWait> Waits) |
| 23 | { |
| 24 | public long WaitCount => Waits.Sum(wait => wait.Count); |
| 25 | |
| 26 | public TimeSpan Waited => TimeSpan.FromTicks(Waits.Sum(wait => wait.Took.Ticks)); |
| 27 | } |
| 28 | |
| 29 | public static class MapStart |
| 30 | { |
| 31 | public static (MapWorld? World, MapStartTimes Times) Build(ConversionPipeline conversion, MapRecord map, |
| 32 | Action<TranscriptTag, string> line, bool collide = true, bool remasteredLiquids = false) |
| 33 | { |
| 34 | var clock = Stopwatch.StartNew(); |
| 35 | RenderingServer.ClearThreadWaits(); |
| 36 | var materialiser = new Materialiser(conversion) { RemasteredLiquids = remasteredLiquids }; |
| 37 | var load = new MapLoader(conversion) { Converted = materialiser.Arrived }.Load(map, line); |
| 38 | var loaded = clock.Elapsed; |
| 39 | if (!load.Loaded) |
| 40 | return (null, new MapStartTimes(loaded, TimeSpan.Zero, TimeSpan.Zero, loaded, [])); |
| 41 | var collision = collide |
| 42 | ? Task.Run(() => |
| 43 | { |
| 44 | var took = Stopwatch.StartNew(); |
| 45 | return (MapCollision.Build(load, conversion.Get, line), took.Elapsed); |
| 46 | }) |
| 47 | : null; |
| 48 | line(TranscriptTag.None, "world: building the map's meshes, materials, textures and sky"); |
| 49 | var world = MapWorld.Build(load, conversion, materialiser, line); |
| 50 | var built = clock.Elapsed; |
| 51 | var collided = TimeSpan.Zero; |
| 52 | if (collision is not null) |
| 53 | { |
| 54 | var (colliders, took) = collision.Result; |
| 55 | world.Collide(colliders); |
| 56 | collided = took; |
| 57 | } |
| 58 | |
| 59 | var waits = Waits(); |
| 60 | var times = new MapStartTimes(loaded, collided, built - loaded, clock.Elapsed, waits); |
| 61 | if (times.WaitCount > 0) |
| 62 | line(TranscriptTag.None, |
| 63 | $"world: waited on the render thread {times.WaitCount:N0} {(times.WaitCount == 1 ? "time" : "times")} for {times.Waited.TotalSeconds:0.00} s, {string.Join(", ", waits.Take(4).Select(wait => $"{wait.Where} {wait.Count:N0}x {wait.Took.TotalMilliseconds:0} ms"))}"); |
| 64 | return (world, times); |
| 65 | } |
| 66 | |
| 67 | public static IReadOnlyList<RenderWait> Waits() |
| 68 | { |
| 69 | var waits = new List<RenderWait>(); |
| 70 | foreach (var (where, total) in RenderingServer.GetThreadWaits()) |
| 71 | { |
| 72 | var counts = total.AsInt64Array(); |
| 73 | waits.Add(new RenderWait(where.AsString(), counts[0], TimeSpan.FromMicroseconds(counts[1]), counts[2], |
| 74 | TimeSpan.FromMicroseconds(counts[3]))); |
| 75 | } |
| 76 | |
| 77 | return waits.OrderByDescending(wait => wait.Took).ToArray(); |
| 78 | } |
| 79 | } |