using System.Globalization; using Annulus; using Azimuth; using Blacklight.Core; using Glass; namespace Konis.Testbed { /// /// The status bar's two lines: what the world holds, and what each pass of a frame costs. /// public class StatusReadout { private const double FieldGap = 20; private const double LineGap = 2; private readonly Label effectField; private readonly Label speedField; private readonly Label emittersField; private readonly Label particlesField; private readonly Label selectedField; private readonly Label allocatedField; private readonly Label collectionsField; private readonly Label simulationField; private readonly Label interfaceField; private readonly Label drawField; private readonly Label submitField; private readonly Label gpuField; private readonly Label syncField; private readonly Label frameField; private readonly string syncName; // Each field is its own control at a fixed position, sized for its widest reading, so a number gaining a digit // doesn't shift the fields after it. public StatusReadout(TestbedSkin skin, VerticalSyncMode verticalSync) { syncName = verticalSync == VerticalSyncMode.Off ? "present" : "vsync"; var content = new Panel { Material = Glass.Material.Invisible, AbsorbsMouse = false }; Scalar lineHeight = skin.StatusText.SingleLineHeight; effectField = NewField(skin, "effect Fountain"); speedField = NewField(skin, "speed 0.25x"); emittersField = NewField(skin, "emitters 9999"); particlesField = NewField(skin, "particles 999999"); selectedField = NewField(skin, SelectedText(LongestEffectName(), 99, 99)); allocatedField = NewField(skin, "alloc 9999.9 KB"); collectionsField = NewField(skin, "gen 0:999 1:999 2:999"); Label[] stateLine = { effectField, speedField, emittersField, particlesField, selectedField, allocatedField, collectionsField, }; simulationField = NewField(skin, "sim 99.99 ms"); interfaceField = NewField(skin, "ui 99.99 ms"); drawField = NewField(skin, "draw 99.99 ms"); submitField = NewField(skin, "submit 99.99 ms"); gpuField = NewField(skin, "gpu 99.99 ms"); syncField = NewField(skin, syncName + " 99.99 ms"); frameField = NewField(skin, "frame 99.9 ms (999 fps)"); Label[] timingLine = { simulationField, interfaceField, drawField, submitField, gpuField, syncField, frameField }; var lines = new RectAggregator(new Vector(0, LineGap)); lines.NewRow(lineHeight); lines.NewRow(lineHeight); var contentSize = new Vector( Scalar.Max(MeasureLine(stateLine).Rect.Width, MeasureLine(timingLine).Rect.Width), lines.Rect.Height); Rect remainder = Rect.FromPositionAndSize(Vector.Zero, contentSize); Rect stateRow = remainder.NewRow(lineHeight, VerticalJustification.Top, LineGap, Shortfall.UseWhatIsLeft); ArrangeLine(content, stateLine, stateRow); ArrangeLine(content, timingLine, remainder); content.Bounds = Rect.FromPositionAndSize(Vector.Zero, contentSize); Bar = content.WrapIn(skin.StatusBar); Bar.DebugName = "status bar"; } /// /// Gets the bar to add to the world. /// public Panel Bar { get; } private static string LongestEffectName() { string longest = ""; foreach (Effect effect in Effects.All) { if (effect.Name.Length > longest.Length) { longest = effect.Name; } } return longest; } private static string SelectedText(string name, int number, int count) { return string.Format(CultureInfo.InvariantCulture, "selected {0} {1} of {2}", name, number, count); } private static Label NewField(TestbedSkin skin, string widestReading) { return new Label(skin.StatusText, widestReading); } private static RectAggregator MeasureLine(Label[] line) { var returnMe = new RectAggregator(new Vector(FieldGap, 0)); foreach (Label label in line) { returnMe.NewCol(label.Material); } return returnMe; } private static void ArrangeLine(Panel content, Label[] line, Rect row) { foreach (Label label in line) { label.Bounds = row.NewCol(label.Material, margin: FieldGap, shortfall: Shortfall.UseWhatIsLeft); content.CanvasChildren.Add(label); } } private static string Milliseconds(string name, double milliseconds) { return string.Format(CultureInfo.InvariantCulture, "{0} {1:0.00} ms", name, milliseconds); } // Bytes alone would round a nearly allocation-free frame to zero, and hide what is left to chase. private static string Allocation(double bytesPerFrame) { if (bytesPerFrame < 2000) { return string.Format(CultureInfo.InvariantCulture, "alloc {0:0} B", bytesPerFrame); } return string.Format(CultureInfo.InvariantCulture, "alloc {0:0.0} KB", bytesPerFrame / 1024); } /// /// Shows the demo's state and the latest averaged frame costs. /// public void Refresh(ParticleDemo demo, FrameStats stats) { effectField.Text = "effect " + Effects.All[demo.EffectIndex].Name; speedField.Text = "speed " + ParticleDemo.SpeedText(demo.SpeedIndex); emittersField.Text = "emitters " + demo.World.Count.ToString(CultureInfo.InvariantCulture); particlesField.Text = "particles " + demo.World.ParticleCount.ToString(CultureInfo.InvariantCulture); selectedField.Text = demo.Selected == null ? "selected none" : SelectedText(Effects.All[demo.Selected.EffectIndex].Name, demo.SelectedIndex + 1, demo.Spawned.Count); allocatedField.Text = Allocation(stats.AllocatedBytes); collectionsField.Text = string.Format(CultureInfo.InvariantCulture, "gen 0:{0} 1:{1} 2:{2}", stats.Gen0Collections, stats.Gen1Collections, stats.Gen2Collections); simulationField.Text = Milliseconds("sim", stats.SimulationMilliseconds); interfaceField.Text = Milliseconds("ui", stats.InterfaceMilliseconds); drawField.Text = Milliseconds("draw", stats.DrawMilliseconds); submitField.Text = Milliseconds("submit", stats.SubmitMilliseconds); gpuField.Text = stats.MeasuresGpuWait ? Milliseconds("gpu", stats.GpuWaitMilliseconds) : "gpu off"; syncField.Text = Milliseconds(syncName, stats.VSyncWaitMilliseconds); frameField.Text = string.Format(CultureInfo.InvariantCulture, "frame {0:0.0} ms ({1:0} fps)", stats.IntervalMilliseconds, 1000 / stats.IntervalMilliseconds); } } }