Compare commits

...

2 Commits

Author SHA1 Message Date
Jarrod Doyle 8237a46989
Time a bunch more stuff 2024-08-29 19:59:01 +01:00
Jarrod Doyle 72f8666f5d
Sum repeated stage names 2024-08-29 19:27:18 +01:00
2 changed files with 55 additions and 21 deletions

View File

@ -74,7 +74,9 @@ public partial class Mission : Node3D
{ {
if (Build) if (Build)
{ {
Timing.Reset();
Timing.TimeStage("Build", () => RebuildMap()); Timing.TimeStage("Build", () => RebuildMap());
Timing.LogAll();
Build = false; Build = false;
} }
if (Clear) if (Clear)
@ -243,7 +245,7 @@ public partial class Mission : Node3D
var rawRot = brush.angle; var rawRot = brush.angle;
var rot = new Vector3(rawRot.Y, rawRot.Z, rawRot.X) * 360 / ushort.MaxValue; var rot = new Vector3(rawRot.Y, rawRot.Z, rawRot.X) * 360 / ushort.MaxValue;
var scale = scaleProp == null ? Vector3.One : scaleProp.scale.ToGodotVec3(false); var scale = scaleProp == null ? Vector3.One : scaleProp.scale.ToGodotVec3(false);
var model = _modelLoader.Load(_campaignName, modelName); var model = Timing.TimeStage("Load Models", () => { return _modelLoader.Load(_campaignName, modelName); });
if (model != null) if (model != null)
{ {
model.Position = pos; model.Position = pos;
@ -392,12 +394,18 @@ public partial class Mission : Node3D
var layerCount = lightmap.Layers; var layerCount = lightmap.Layers;
var srcRect = new Rect2I(0, 0, width, height); var srcRect = new Rect2I(0, 0, width, height);
var dst = new Vector2I(rect.X, rect.Y); var dst = new Vector2I(rect.X, rect.Y);
Timing.TimeStage("Blit LM Data", () =>
{
for (var i = 0; i < layerCount; i++) for (var i = 0; i < layerCount; i++)
{ {
var cellLm = Image.CreateFromData(width, height, false, lightmapFormat, lightmap.AsBytesRgba(i)); var bytes = Timing.TimeStage("Get LM Data", () => { return lightmap.AsBytesRgba(i); });
var cellLm = Image.CreateFromData(width, height, false, lightmapFormat, bytes);
lmImages[i].BlitRect(cellLm, srcRect, dst); lmImages[i].BlitRect(cellLm, srcRect, dst);
} }
});
Timing.TimeStage("Transform LM UVs", () =>
{
if (!surfaceDataMap.ContainsKey(info.textureId)) GD.Print("Invalid SurfaceDataMap key"); if (!surfaceDataMap.ContainsKey(info.textureId)) GD.Print("Invalid SurfaceDataMap key");
surfaceDataMap[info.textureId].TransformUv2s(info.uvStart, info.uvEnd, (uv) => surfaceDataMap[info.textureId].TransformUv2s(info.uvStart, info.uvEnd, (uv) =>
{ {
@ -415,6 +423,7 @@ public partial class Mission : Node3D
v = (rect.Y + rect.Height * v) / bounds.Height; v = (rect.Y + rect.Height * v) / bounds.Height;
return new Vector2(u, v); return new Vector2(u, v);
}); });
});
} }
var lightmapTexture = new Texture2DArray(); var lightmapTexture = new Texture2DArray();
@ -436,7 +445,7 @@ public partial class Mission : Node3D
// TODO: This is a mess lol // TODO: This is a mess lol
var textureId = renderPoly.TextureId; var textureId = renderPoly.TextureId;
var texture = _textureLoader.Get(textureId); var texture = Timing.TimeStage("Load Textures", () => { return _textureLoader.Get(textureId); });
var texU = renderPoly.TextureVectors.Item1.ToGodotVec3(); var texU = renderPoly.TextureVectors.Item1.ToGodotVec3();
var texV = renderPoly.TextureVectors.Item2.ToGodotVec3(); var texV = renderPoly.TextureVectors.Item2.ToGodotVec3();
var baseU = renderPoly.TextureBases.Item1; var baseU = renderPoly.TextureBases.Item1;

View File

@ -1,16 +1,24 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
namespace KeepersCompound.TMV; namespace KeepersCompound.TMV;
public static class Timing public static class Timing
{ {
static readonly Dictionary<string, TimeSpan> _stages = new();
public static void Reset()
{
_stages.Clear();
}
public static void TimeStage(string stagename, Action action) public static void TimeStage(string stagename, Action action)
{ {
var watch = Stopwatch.StartNew(); var watch = Stopwatch.StartNew();
action(); action();
watch.Stop(); watch.Stop();
Godot.GD.Print($"[{stagename}]: {watch.Elapsed:g}"); AddOrIncrement(stagename, watch.Elapsed);
} }
public static T TimeStage<T>(string stagename, Func<T> action) public static T TimeStage<T>(string stagename, Func<T> action)
@ -18,7 +26,24 @@ public static class Timing
var watch = Stopwatch.StartNew(); var watch = Stopwatch.StartNew();
var value = action(); var value = action();
watch.Stop(); watch.Stop();
Godot.GD.Print($"[{stagename}]: {watch.Elapsed:g}"); AddOrIncrement(stagename, watch.Elapsed);
return value; return value;
} }
public static void LogAll()
{
foreach (var (stagename, time) in _stages)
{
Godot.GD.Print($"[{stagename}]: {time:g}");
}
}
private static void AddOrIncrement(string stagename, TimeSpan elapsed)
{
if (_stages.TryGetValue(stagename, out var time))
{
elapsed += time;
}
_stages[stagename] = elapsed;
}
} }