diff --git a/VolumetricLight.Harness/HarnessArguments.cs b/VolumetricLight.Harness/HarnessArguments.cs index 36614dc..0289a80 100644 --- a/VolumetricLight.Harness/HarnessArguments.cs +++ b/VolumetricLight.Harness/HarnessArguments.cs @@ -12,22 +12,23 @@ internal enum HarnessMode Benchmark, } -internal sealed record HarnessArguments(HarnessMode Mode, string? Input, string? OutputDirectory, string? Before, string? After) +internal sealed record HarnessArguments(HarnessMode Mode, string? Input, string? OutputDirectory, string? Before, string? After, string? Case = null) { public const string Usage = """ 使い方: - [<出力先>] [--input <画像>] ケースを描き、PNG とハッシュを出します + [<出力先>] [--input <画像>] [--case <名前>] ケースを描き、PNG とハッシュを出します。--case は名前に含まれるケースだけを描きます --golden [--input <画像>] 基準値を golden.json に書きます --verify [--input <画像>] 基準値と照合します --transition [<出力先>] [--input <画像>] 設定を変えた後とフレームを進めた後の描画を、作り直した描画と照合します --compare <前> <後> 2 つの出力先の PNG を画素ごとに比べます - --benchmark [--input <画像>] 描画を計測します + --benchmark [--input <画像>] [--case <名前>] 描画を計測します """; public static HarnessArguments Parse(string[] arguments) { HarnessMode? mode = null; string? input = null; + string? caseFilter = null; var positionals = new List(); for (var index = 0; index < arguments.Length; index++) { @@ -49,6 +50,11 @@ internal sealed record HarnessArguments(HarnessMode Mode, string? Input, string? case "--benchmark": mode = Select(mode, HarnessMode.Benchmark); break; + case "--case": + if (index + 1 >= arguments.Length) + throw new HarnessException("--case には名前を続けてください。"); + caseFilter = arguments[++index]; + break; case "--input": if (index + 1 >= arguments.Length) throw new HarnessException("--input には画像のパスを続けてください。"); @@ -75,7 +81,7 @@ internal sealed record HarnessArguments(HarnessMode Mode, string? Input, string? case HarnessMode.Transition: if (positionals.Count > 1) throw new HarnessException($"余分な引数です。{positionals[1]}"); - return new HarnessArguments(selected, input, positionals.FirstOrDefault(), null, null); + return new HarnessArguments(selected, input, positionals.FirstOrDefault(), null, null, caseFilter); default: if (positionals.Count > 0) throw new HarnessException($"余分な引数です。{positionals[0]}"); diff --git a/VolumetricLight.Harness/HarnessCases.cs b/VolumetricLight.Harness/HarnessCases.cs index aaeb19e..61bca36 100644 --- a/VolumetricLight.Harness/HarnessCases.cs +++ b/VolumetricLight.Harness/HarnessCases.cs @@ -1,21 +1,102 @@ -using YukkuriMovieMaker.Plugin.Effects; +using System.Globalization; +using System.Numerics; +using YukkuriMovieMaker.Commons; +using YukkuriMovieMaker.Json; namespace VolumetricLight.Harness; internal static class HarnessCases { - public static IEnumerable<(string Name, IVideoEffect Effect, IReadOnlyList Frames)> All() + static readonly HarnessPose RotatedY = HarnessPose.Flat with { Rotation = new Vector3(0f, 35f, 0f) }; + static readonly HarnessPose RotatedX = HarnessPose.Flat with { Rotation = new Vector3(25f, 0f, 0f) }; + static readonly HarnessPose RotatedZ = HarnessPose.Flat with { Rotation = new Vector3(0f, 0f, 20f) }; + static readonly HarnessPose Shrunk = HarnessPose.Flat with { Zoom = new Vector2(0.6f, 0.6f) }; + static readonly HarnessPose Offset = HarnessPose.Flat with { Draw = new Vector3(-180f, 60f, 0f) }; + static readonly HarnessPose Near = HarnessPose.Flat with { Draw = new Vector3(0f, 0f, 300f) }; + static readonly HarnessPose Mirrored = HarnessPose.Flat with { Invert = true }; + static readonly HarnessPose Orbit = HarnessPose.Flat with { Camera = HarnessPose.CameraAt(new Vector3(220f, 0f, 300f), new Vector3(0f, -15f, 0f)) }; + static readonly HarnessPose Telephoto = HarnessPose.Flat with { PerspectiveDistance = 3000f }; + + public static IEnumerable<(string Name, VolumetricLightEffect Effect, IReadOnlyList Frames, HarnessPose Pose)> All() { - yield break; + yield return ("default", Create(), [0], HarnessPose.Flat); + yield return ("direction-animated-frames", Create(effect => effect.Direction.CopyFrom(Linear(-150d, -30d))), [0, 100, 200, 299], HarnessPose.Flat); + yield return ("direction-0", Create(effect => effect.Direction.Values[0].Value = 0), [0], HarnessPose.Flat); + yield return ("direction-180", Create(effect => effect.Direction.Values[0].Value = 180), [0], HarnessPose.Flat); + yield return ("elevation-15", Create(effect => effect.Elevation.Values[0].Value = 15), [0], HarnessPose.Flat); + yield return ("elevation-70", Create(effect => effect.Elevation.Values[0].Value = 70), [0], HarnessPose.Flat); + yield return ("elevation-minus-35", Create(effect => effect.Elevation.Values[0].Value = -35), [0], HarnessPose.Flat); + yield return ("spread-0", Create(effect => effect.Spread.Values[0].Value = 0), [0], HarnessPose.Flat); + yield return ("spread-35", Create(effect => effect.Spread.Values[0].Value = 35), [0], HarnessPose.Flat); + yield return ("spread-minus-25", Create(effect => effect.Spread.Values[0].Value = -25), [0], HarnessPose.Flat); + yield return ("length-400", Create(effect => effect.Length.Values[0].Value = 400), [0], HarnessPose.Flat); + yield return ("length-2800", Create(effect => effect.Length.Values[0].Value = 2800), [0], HarnessPose.Flat); + yield return ("length-0", Create(effect => effect.Length.Values[0].Value = 0), [0], HarnessPose.Flat); + yield return ("intensity-40", Create(effect => effect.Intensity.Values[0].Value = 40), [0], HarnessPose.Flat); + yield return ("intensity-250", Create(effect => effect.Intensity.Values[0].Value = 250), [0], HarnessPose.Flat); + yield return ("threshold-0", Create(effect => effect.Threshold.Values[0].Value = 0), [0], HarnessPose.Flat); + yield return ("threshold-60", Create(effect => effect.Threshold.Values[0].Value = 60), [0], HarnessPose.Flat); + yield return ("tint-amber", Create(effect => effect.Tint = System.Windows.Media.Color.FromRgb(255, 190, 90)), [0], HarnessPose.Flat); + yield return ("falloff-0", Create(effect => effect.Falloff.Values[0].Value = 0), [0], HarnessPose.Flat); + yield return ("falloff-95", Create(effect => effect.Falloff.Values[0].Value = 95), [0], HarnessPose.Flat); + yield return ("lamp-size-0", Create(effect => effect.LampSize.Values[0].Value = 0), [0], HarnessPose.Flat); + yield return ("lamp-size-6", Create(effect => effect.LampSize.Values[0].Value = 6), [0], HarnessPose.Flat); + yield return ("scattering-minus-60", Create(effect => effect.Scattering.Values[0].Value = -60), [0], HarnessPose.Flat); + yield return ("scattering-85", Create(effect => effect.Scattering.Values[0].Value = 85), [0], HarnessPose.Flat); + yield return ("source-hidden", Create(effect => effect.ShowSource = false), [0], HarnessPose.Flat); + yield return ("dust-60", Create(effect => effect.DustAmount.Values[0].Value = 60), [0], HarnessPose.Flat); + yield return ("dust-60-animated-frames", Create(effect => effect.DustAmount.Values[0].Value = 60), [0, 90, 180], HarnessPose.Flat); + yield return ("dust-seed-7", Create(effect => { effect.DustAmount.Values[0].Value = 60; effect.Seed = 7; }), [0], HarnessPose.Flat); + yield return ("quality-high", Create(effect => effect.Quality = VolumetricLightQuality.High), [0], HarnessPose.Flat); + yield return ("quality-ultra", Create(effect => effect.Quality = VolumetricLightQuality.Ultra), [0], HarnessPose.Flat); + yield return ("pose-rotated-y", Create(), [0], RotatedY); + yield return ("pose-rotated-x", Create(), [0], RotatedX); + yield return ("pose-rotated-z", Create(), [0], RotatedZ); + yield return ("pose-shrunk", Create(), [0], Shrunk); + yield return ("pose-offset", Create(), [0], Offset); + yield return ("pose-near", Create(), [0], Near); + yield return ("pose-mirrored", Create(), [0], Mirrored); + yield return ("pose-orbit", Create(), [0], Orbit); + yield return ("pose-telephoto", Create(), [0], Telephoto); + yield return ("pose-rotated-y-converging", Create(effect => effect.Spread.Values[0].Value = -25), [0], RotatedY); } - public static IEnumerable<(string Name, Func Create, Action Change, int Frame)> Transitions() + public static IEnumerable<(string Name, Func Create, Action Change, int Frame, HarnessPose Pose)> Transitions() { - yield break; + yield return ("direction-minus-90-to-0", () => Create(), effect => effect.Direction.Values[0].Value = 0, 0, HarnessPose.Flat); + yield return ("elevation-35-to-60", () => Create(), effect => effect.Elevation.Values[0].Value = 60, 0, HarnessPose.Flat); + yield return ("spread-12-to-minus-20", () => Create(), effect => effect.Spread.Values[0].Value = -20, 0, HarnessPose.Flat); + yield return ("length-1200-to-500", () => Create(), effect => effect.Length.Values[0].Value = 500, 0, HarnessPose.Flat); + yield return ("intensity-100-to-30", () => Create(), effect => effect.Intensity.Values[0].Value = 30, 0, HarnessPose.Flat); + yield return ("threshold-25-to-70", () => Create(), effect => effect.Threshold.Values[0].Value = 70, 0, HarnessPose.Flat); + yield return ("threshold-70-to-10", () => Create(effect => effect.Threshold.Values[0].Value = 70), effect => effect.Threshold.Values[0].Value = 10, 0, HarnessPose.Flat); + yield return ("tint-white-to-blue", () => Create(), effect => effect.Tint = System.Windows.Media.Color.FromRgb(90, 140, 255), 0, HarnessPose.Flat); + yield return ("show-source-on-to-off", () => Create(), effect => effect.ShowSource = false, 0, HarnessPose.Flat); + yield return ("show-source-off-to-on", () => Create(effect => effect.ShowSource = false), effect => effect.ShowSource = true, 0, HarnessPose.Flat); + yield return ("dust-0-to-60", () => Create(), effect => effect.DustAmount.Values[0].Value = 60, 90, HarnessPose.Flat); + yield return ("quality-standard-to-ultra", () => Create(), effect => effect.Quality = VolumetricLightQuality.Ultra, 0, HarnessPose.Flat); + yield return ("falloff-60-to-0", () => Create(), effect => effect.Falloff.Values[0].Value = 0, 0, HarnessPose.Flat); + yield return ("scattering-30-to-minus-50", () => Create(), effect => effect.Scattering.Values[0].Value = -50, 0, HarnessPose.Flat); + yield return ("lamp-size-1.5-to-6", () => Create(), effect => effect.LampSize.Values[0].Value = 6, 0, HarnessPose.Flat); + yield return ("rotated-y-length-1200-to-500", () => Create(), effect => effect.Length.Values[0].Value = 500, 0, RotatedY); } - public static IEnumerable<(string Name, IVideoEffect Effect)> Benchmarks() + public static IEnumerable<(string Name, VolumetricLightEffect Effect, HarnessPose Pose)> Benchmarks() { - yield break; + yield return ("default", Create(), HarnessPose.Flat); + yield return ("quality-ultra", Create(effect => effect.Quality = VolumetricLightQuality.Ultra), HarnessPose.Flat); + yield return ("dust-60", Create(effect => effect.DustAmount.Values[0].Value = 60), HarnessPose.Flat); + yield return ("pose-rotated-y", Create(), RotatedY); + yield return ("direction-animated", Create(effect => effect.Direction.CopyFrom(Linear(-150d, -30d))), HarnessPose.Flat); + } + + static Animation Linear(double from, double to) + => Json.LoadFromText(string.Create(CultureInfo.InvariantCulture, $$"""{"AnimationType":"直線移動","Values":[{"Value":{{from}}},{"Value":{{to}}}]}""")) ?? throw new HarnessException("アニメーションを読み込めません。"); + + public static VolumetricLightEffect Create(Action? configure = null) + { + var effect = new VolumetricLightEffect(); + configure?.Invoke(effect); + return effect; } } diff --git a/VolumetricLight.Harness/HarnessPose.cs b/VolumetricLight.Harness/HarnessPose.cs new file mode 100644 index 0000000..dbd7477 --- /dev/null +++ b/VolumetricLight.Harness/HarnessPose.cs @@ -0,0 +1,20 @@ +using System.Numerics; + +namespace VolumetricLight.Harness; + +internal sealed record HarnessPose(Vector3 Draw, Vector3 Rotation, Vector2 Zoom, Matrix4x4 Camera, bool Invert, float? PerspectiveDistance = null) +{ + const float CameraPivotDepth = 1000f; + + public static HarnessPose Flat { get; } = new(Vector3.Zero, Vector3.Zero, Vector2.One, Matrix4x4.Identity, false); + + public static Matrix4x4 CameraAt(Vector3 position, Vector3 rotationDegrees) + { + var translation = new Vector3(position.X, position.Y, -position.Z); + var pivot = new Vector3(0f, 0f, CameraPivotDepth); + return Matrix4x4.CreateTranslation(-translation) + * Matrix4x4.CreateRotationY(MathF.PI * rotationDegrees.Y / 180f, pivot) + * Matrix4x4.CreateRotationX(MathF.PI * rotationDegrees.X / 180f, pivot) + * Matrix4x4.CreateRotationZ(MathF.PI * rotationDegrees.Z / 180f, pivot); + } +} diff --git a/VolumetricLight.Harness/HarnessRenderer.cs b/VolumetricLight.Harness/HarnessRenderer.cs index 2db978e..20387e0 100644 --- a/VolumetricLight.Harness/HarnessRenderer.cs +++ b/VolumetricLight.Harness/HarnessRenderer.cs @@ -16,10 +16,12 @@ internal sealed class HarnessRenderer : IDisposable { const int Fps = 30; const int Length = 300; - const int WarmupUpdates = 2000; - const int UpdateRepeats = 20; + const int WarmupUpdates = 100; + const int UpdateRepeats = 5; const float Dpi = 96f; + static readonly Color4 Backdrop = new(0f, 0f, 0f, 1f); + static readonly PixelFormat PixelFormat = new(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied); readonly GraphicsDevices devices; @@ -67,7 +69,7 @@ internal sealed class HarnessRenderer : IDisposable public string Driver { get; } - public byte[][] Render(IVideoEffect effect, IReadOnlyList frames) + public byte[][] Render(IVideoEffect effect, IReadOnlyList frames, HarnessPose? pose = null) { if (frames.Count == 0) throw new ArgumentException("フレームを 1 つ以上指定してください。", nameof(frames)); @@ -79,7 +81,7 @@ internal sealed class HarnessRenderer : IDisposable var rendered = new byte[frames.Count][]; for (var index = 0; index < frames.Count; index++) { - processor.Update(Describe(frames[index])); + processor.Update(Describe(frames[index], pose)); rendered[index] = Capture(processor.Output); } @@ -91,22 +93,22 @@ internal sealed class HarnessRenderer : IDisposable } } - public (byte[] Direct, byte[] Transitioned) RenderTransition(Func create, Action change, int frame) + public (byte[] Direct, byte[] Transitioned) RenderTransition(Func create, Action change, int frame, HarnessPose? pose = null) where TEffect : IVideoEffect { var settled = create(); change(settled); - var direct = Render(settled, [frame])[0]; + var direct = Render(settled, [frame], pose)[0]; var live = create(); using var processor = live.CreateVideoEffect(context); processor.SetInput(centered); try { - processor.Update(Describe(frame)); + processor.Update(Describe(frame, pose)); _ = Capture(processor.Output); change(live); - processor.Update(Describe(frame)); + processor.Update(Describe(frame, pose)); return (direct, Capture(processor.Output)); } finally @@ -115,11 +117,11 @@ internal sealed class HarnessRenderer : IDisposable } } - public Measurement Measure(IVideoEffect effect, int frames, bool moving) + public Measurement Measure(IVideoEffect effect, int frames, bool moving, HarnessPose? pose = null) { var descriptions = new EffectDescription[frames]; for (var frame = 0; frame < frames; frame++) - descriptions[frame] = Describe(moving ? frame : 0); + descriptions[frame] = Describe(moving ? frame : 0, pose); using var processor = effect.CreateVideoEffect(context); processor.SetInput(centered); @@ -208,7 +210,7 @@ internal sealed class HarnessRenderer : IDisposable var deviceContext = context.DeviceContext; deviceContext.Target = target; deviceContext.BeginDraw(); - deviceContext.Clear(new Color4(0f, 0f, 0f, 0f)); + deviceContext.Clear(Backdrop); deviceContext.DrawImage(output, new Vector2(CanvasWidth * 0.5f, CanvasHeight * 0.5f), null, InterpolationMode.NearestNeighbor, CompositeMode.SourceOver); deviceContext.EndDraw(); deviceContext.Target = null; @@ -221,7 +223,7 @@ internal sealed class HarnessRenderer : IDisposable sync.Unmap(); } - EffectDescription Describe(int frame) + EffectDescription Describe(int frame, HarnessPose? pose) { var timeline = new TimelineSourceDescription( new System.Drawing.Size(CanvasWidth, CanvasHeight), @@ -232,7 +234,11 @@ internal sealed class HarnessRenderer : IDisposable Guid.Empty, []); var item = new TimelineItemSourceDescription(timeline, frame, Length, 0); - var draw = new DrawDescription(Vector3.Zero, Vector2.Zero, Vector2.One, Vector3.Zero, Matrix4x4.Identity, InterpolationMode.Linear, 1d, false, []); + var placement = pose ?? HarnessPose.Flat; + var draw = new DrawDescription(placement.Draw, Vector2.Zero, placement.Zoom, placement.Rotation, placement.Camera, InterpolationMode.Linear, 1d, placement.Invert, []) + { + PerspectiveDistance = placement.PerspectiveDistance, + }; return new EffectDescription(item, draw, 0, 1, 0, 1); } diff --git a/VolumetricLight.Harness/Program.cs b/VolumetricLight.Harness/Program.cs index 2c5568a..9c703eb 100644 --- a/VolumetricLight.Harness/Program.cs +++ b/VolumetricLight.Harness/Program.cs @@ -6,8 +6,8 @@ using SharpGen.Runtime; const int CanvasWidth = 1280; const int CanvasHeight = 720; -const int ImageWidth = 640; -const int ImageHeight = 480; +const int ImageWidth = 960; +const int ImageHeight = 540; const int FullHdWidth = 1920; const int FullHdHeight = 1080; @@ -35,12 +35,12 @@ try { if (arguments.Input is { } benchmarkInput) { - Benchmark(HarnessImage.Load(benchmarkInput)); + Benchmark(HarnessImage.Load(benchmarkInput), arguments.Case); } else { - Benchmark(HarnessImage.Synthetic(CanvasWidth, CanvasHeight)); - Benchmark(HarnessImage.Synthetic(FullHdWidth, FullHdHeight)); + Benchmark(HarnessImage.Synthetic(CanvasWidth, CanvasHeight), arguments.Case); + Benchmark(HarnessImage.Synthetic(FullHdWidth, FullHdHeight), arguments.Case); } return 0; @@ -53,7 +53,7 @@ try HarnessMode.Golden => WriteGolden(renderer, image), HarnessMode.Verify => Verify(renderer, image), HarnessMode.Transition => Transition(renderer, outputDirectory), - _ => Render(renderer, outputDirectory), + _ => Render(renderer, outputDirectory, arguments.Case), }; } catch (Exception exception) when (exception is HarnessException or SharpGenException) @@ -62,21 +62,21 @@ catch (Exception exception) when (exception is HarnessException or SharpGenExcep return 2; } -static int Render(HarnessRenderer renderer, string outputDirectory) +static int Render(HarnessRenderer renderer, string outputDirectory, string? caseFilter) { Directory.CreateDirectory(outputDirectory); var failures = 0; - var cases = Evaluate(renderer, (golden, files, frames) => + var cases = Evaluate(renderer, caseFilter, (golden, files, frames) => { Console.WriteLine($"{golden.Name}: {golden.Hash}"); for (var index = 0; index < files.Length; index++) { HarnessImage.Save(Path.Combine(outputDirectory, files[index]), frames[index], renderer.CanvasWidth, renderer.CanvasHeight); - var opaque = CountOpaque(frames[index]); - Console.WriteLine($" {files[index]} opaque={opaque}"); - if (opaque == 0) + var lit = CountLit(frames[index]); + Console.WriteLine($" {files[index]} lit={lit}"); + if (lit == 0) { - Console.Error.WriteLine($"{files[index]}: 出力が完全に透明です。"); + Console.Error.WriteLine($"{files[index]}: 出力が真っ黒です。"); failures++; } } @@ -87,7 +87,7 @@ static int Render(HarnessRenderer renderer, string outputDirectory) static int WriteGolden(HarnessRenderer renderer, HarnessImage image) { - var cases = Evaluate(renderer, null); + var cases = Evaluate(renderer, null, null); if (CountDuplicates(cases) != 0) return 1; if (cases.Count == 0) @@ -118,7 +118,7 @@ static int Verify(HarnessRenderer renderer, HarnessImage image) Console.Error.WriteLine($"基準値は {golden.Adapter} (driver {golden.Driver}) で書かれています。今回は {renderer.Adapter} (driver {renderer.Driver}) です。画素の差はこの違いから来ることがあります。"); var failures = 0; - var current = Evaluate(renderer, null).ToDictionary(item => item.Name, StringComparer.Ordinal); + var current = Evaluate(renderer, null, null).ToDictionary(item => item.Name, StringComparer.Ordinal); if (golden.Cases.Count == 0 && current.Count == 0) Console.Error.WriteLine("ケースがありません。HarnessCases に書き足してください。"); foreach (var (name, frames, hash) in golden.Cases) @@ -156,9 +156,9 @@ static int Verify(HarnessRenderer renderer, HarnessImage image) static int Transition(HarnessRenderer renderer, string outputDirectory) { var failures = 0; - foreach (var (name, create, change, frame) in HarnessCases.Transitions()) + foreach (var (name, create, change, frame, pose) in HarnessCases.Transitions()) { - var (direct, transitioned) = renderer.RenderTransition(create, change, frame); + var (direct, transitioned) = renderer.RenderTransition(create, change, frame, pose); var difference = ImageComparison.Of(direct, transitioned, renderer.CanvasWidth, renderer.CanvasHeight); if (difference.IsEmpty) { @@ -176,15 +176,15 @@ static int Transition(HarnessRenderer renderer, string outputDirectory) if (!HarnessCases.Transitions().Any() && !HarnessCases.All().Any(item => item.Frames.Count > 1)) Console.Error.WriteLine("ケースがありません。HarnessCases に書き足してください。"); - foreach (var (name, effect, frames) in HarnessCases.All()) + foreach (var (name, effect, frames, pose) in HarnessCases.All()) { if (frames.Count < 2) continue; - var sequential = renderer.Render(effect, frames); + var sequential = renderer.Render(effect, frames, pose); for (var index = 0; index < frames.Count; index++) { - var fresh = renderer.Render(effect, [frames[index]])[0]; + var fresh = renderer.Render(effect, [frames[index]], pose)[0]; var difference = ImageComparison.Of(fresh, sequential[index], renderer.CanvasWidth, renderer.CanvasHeight); var label = $"{name}-f{frames[index]:D3}"; if (difference.IsEmpty) @@ -248,37 +248,42 @@ static int Compare(string beforeDirectory, string afterDirectory) return failures == 0 ? 0 : 1; } -static void Benchmark(HarnessImage image) +static void Benchmark(HarnessImage image, string? caseFilter) { - const int Frames = 60; + const int Frames = 12; using var renderer = new HarnessRenderer(image.Width, image.Height, image); Console.WriteLine($"adapter: {renderer.Adapter} (driver {renderer.Driver})"); - foreach (var (name, effect) in HarnessCases.Benchmarks()) + foreach (var (name, effect, pose) in HarnessCases.Benchmarks()) { - Report(image, name, "still", renderer.Measure(effect, Frames, moving: false)); - Report(image, name, "moving", renderer.Measure(effect, Frames, moving: true)); + if (caseFilter is not null && !name.Contains(caseFilter, StringComparison.Ordinal)) + continue; + + Report(image, name, "still", renderer.Measure(effect, Frames, moving: false, pose)); + Report(image, name, "moving", renderer.Measure(effect, Frames, moving: true, pose)); } } static void Report(HarnessImage image, string name, string motion, HarnessRenderer.Measurement measurement) => Console.WriteLine( - $"{image.Width}x{image.Height} {name,-15} {motion,-6} " + + $"{image.Width}x{image.Height} {name,-22} {motion,-6} " + $"gpu={(measurement.Gpu is { } gpu ? gpu.TotalMilliseconds.ToString("F3") : "n/a"),7} " + $"cpu={measurement.Cpu.TotalMilliseconds,7:F3} " + $"update={measurement.Update.TotalMilliseconds,6:F3} " + $"alloc={measurement.Allocated,6} gen0={measurement.Gen0}"); -static List Evaluate(HarnessRenderer renderer, Action? report) +static List Evaluate(HarnessRenderer renderer, string? caseFilter, Action? report) { var cases = new List(); var names = new HashSet(StringComparer.Ordinal); - foreach (var (name, effect, frames) in HarnessCases.All()) + foreach (var (name, effect, frames, pose) in HarnessCases.All()) { if (!names.Add(name)) throw new HarnessException($"ケース名が重複しています。{name}"); + if (caseFilter is not null && !name.Contains(caseFilter, StringComparison.Ordinal)) + continue; - var rendered = renderer.Render(effect, frames); + var rendered = renderer.Render(effect, frames, pose); var files = new string[frames.Count]; using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); for (var index = 0; index < frames.Count; index++) @@ -315,12 +320,12 @@ static int CountDuplicates(IReadOnlyList cases) return duplicates; } -static int CountOpaque(byte[] pixels) +static int CountLit(byte[] pixels) { var count = 0; - for (var offset = HarnessImage.BytesPerPixel - 1; offset < pixels.Length; offset += HarnessImage.BytesPerPixel) + for (var offset = 0; offset < pixels.Length; offset += HarnessImage.BytesPerPixel) { - if (pixels[offset] != 0) + if (pixels[offset] != 0 || pixels[offset + 1] != 0 || pixels[offset + 2] != 0) count++; } diff --git a/VolumetricLight.Harness/SyntheticImage.cs b/VolumetricLight.Harness/SyntheticImage.cs index d8a9442..8c9d2e3 100644 --- a/VolumetricLight.Harness/SyntheticImage.cs +++ b/VolumetricLight.Harness/SyntheticImage.cs @@ -2,50 +2,139 @@ namespace VolumetricLight.Harness; internal static class SyntheticImage { - const int GridPitch = 32; + const int Samples = 4; + const float LeadWidthFraction = 0.006f; + const float StoneLevel = 0.03f; + const float LeadLevel = 0.02f; + const int ColumnsPerWindow = 3; + const float RowHeightFraction = 0.095f; + + static readonly (float Red, float Green, float Blue)[] Palette = + [ + (0.10f, 0.22f, 0.95f), + (0.95f, 0.10f, 0.16f), + (1.00f, 0.62f, 0.10f), + (0.10f, 0.72f, 0.32f), + (0.62f, 0.20f, 0.85f), + (0.10f, 0.72f, 0.92f), + (0.97f, 0.32f, 0.62f), + (1.00f, 0.92f, 0.24f), + ]; + + static readonly (float Center, float HalfWidth, float Top)[] Windows = + [ + (0.20f, 0.105f, 0.30f), + (0.50f, 0.130f, 0.10f), + (0.80f, 0.105f, 0.30f), + ]; + + const float WindowBottom = 0.93f; public static byte[] Create(int width, int height) { var pixels = new byte[width * height * HarnessImage.BytesPerPixel]; - var plateLeft = width * 0.10f; - var plateRight = width * 0.56f; - var plateTop = height * 0.14f; - var plateBottom = height * 0.86f; - var discCenterX = width * 0.70f; - var discCenterY = height * 0.50f; - var discRadius = MathF.Min(width, height) * 0.28f; - + var lead = LeadWidthFraction * width; for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { - var positionX = x + 0.5f; - var positionY = y + 0.5f; - var plateInset = MathF.Min(MathF.Min(positionX - plateLeft, plateRight - positionX), MathF.Min(positionY - plateTop, plateBottom - positionY)); - var plateCoverage = Math.Clamp(plateInset + 0.5f, 0f, 1f); - var distance = MathF.Sqrt((positionX - discCenterX) * (positionX - discCenterX) + (positionY - discCenterY) * (positionY - discCenterY)); - var discCoverage = Math.Clamp(discRadius - distance + 0.5f, 0f, 1f); - if (plateCoverage == 0f && discCoverage == 0f) - continue; - - var onGrid = x % GridPitch == 0 || y % GridPitch == 0; - var plateWeight = plateCoverage * (1f - discCoverage); - var shade = 1f - distance / discRadius * 0.5f; - var red = (onGrid ? 0.25f : 0.82f) * plateWeight + 0.90f * shade * discCoverage; - var green = (onGrid ? 0.30f : 0.86f) * plateWeight + 0.45f * shade * discCoverage; - var blue = (onGrid ? 0.40f : 0.90f) * plateWeight + 0.30f * shade * discCoverage; - var alpha = plateWeight + discCoverage; + var red = 0f; + var green = 0f; + var blue = 0f; + for (var sy = 0; sy < Samples; sy++) + { + for (var sx = 0; sx < Samples; sx++) + { + var (r, g, b) = Shade(x + (sx + 0.5f) / Samples, y + (sy + 0.5f) / Samples, width, height, lead); + red += r; + green += g; + blue += b; + } + } + var scale = 1f / (Samples * Samples); var offset = (y * width + x) * HarnessImage.BytesPerPixel; - pixels[offset] = ToByte(blue); - pixels[offset + 1] = ToByte(green); - pixels[offset + 2] = ToByte(red); - pixels[offset + 3] = ToByte(alpha); + pixels[offset] = ToByte(blue * scale); + pixels[offset + 1] = ToByte(green * scale); + pixels[offset + 2] = ToByte(red * scale); + pixels[offset + 3] = byte.MaxValue; } } return pixels; } + static (float Red, float Green, float Blue) Shade(float x, float y, int width, int height, float lead) + { + for (var index = 0; index < Windows.Length; index++) + { + var (centerFraction, halfFraction, topFraction) = Windows[index]; + var centerX = centerFraction * width; + var halfWidth = halfFraction * width; + var top = topFraction * height; + var bottom = WindowBottom * height; + var arcCenterY = top + halfWidth; + if (!Inside(x, y, centerX, halfWidth, arcCenterY, bottom, out var edge)) + continue; + + if (edge < lead) + return (LeadLevel, LeadLevel, LeadLevel); + + var cellWidth = 2f * halfWidth / ColumnsPerWindow; + var cellHeight = RowHeightFraction * height; + var localX = x - (centerX - halfWidth); + var localY = y - top; + var column = (int)MathF.Floor(localX / cellWidth); + var row = (int)MathF.Floor(localY / cellHeight); + var gridX = MathF.Abs(localX - MathF.Round(localX / cellWidth) * cellWidth); + var gridY = MathF.Abs(localY - MathF.Round(localY / cellHeight) * cellHeight); + if (MathF.Min(gridX, gridY) < lead * 0.5f) + return (LeadLevel, LeadLevel, LeadLevel); + + var entry = Palette[(int)(Hash(column + 7 * row + 31 * index) % (uint)Palette.Length)]; + var shade = 1f - 0.35f * (y - top) / (bottom - top); + return (entry.Red * shade, entry.Green * shade, entry.Blue * shade); + } + + return (StoneLevel, StoneLevel, StoneLevel); + } + + static bool Inside(float x, float y, float centerX, float halfWidth, float arcCenterY, float bottom, out float edge) + { + edge = 0f; + if (y > bottom) + return false; + + if (y >= arcCenterY) + { + var side = halfWidth - MathF.Abs(x - centerX); + if (side < 0f) + return false; + + edge = MathF.Min(side, bottom - y); + return true; + } + + var dx = x - centerX; + var dy = y - arcCenterY; + var distance = MathF.Sqrt(dx * dx + dy * dy); + if (distance > halfWidth) + return false; + + edge = halfWidth - distance; + return true; + } + + static uint Hash(int value) + { + var hash = (uint)value; + hash ^= hash >> 16; + hash *= 0x85EBCA6Bu; + hash ^= hash >> 13; + hash *= 0xC2B2AE35u; + hash ^= hash >> 16; + return hash; + } + static byte ToByte(float premultiplied) => (byte)MathF.Round(Math.Clamp(premultiplied, 0f, 1f) * byte.MaxValue); } diff --git a/VolumetricLight.Harness/golden.json b/VolumetricLight.Harness/golden.json new file mode 100644 index 0000000..2f140e4 --- /dev/null +++ b/VolumetricLight.Harness/golden.json @@ -0,0 +1,293 @@ +{ + "adapter": "Intel(R) Iris(R) Xe Graphics", + "driver": "32.0.101.7085", + "input": "synthetic 960x540 6A51E0BB2329D7D8", + "canvas": "1280x720", + "cases": [ + { + "name": "default", + "frames": [ + 0 + ], + "hash": "3656E2C3C62B878C077D8946C9F5ABA54EE3154B10356152FF5B40B3531D7CE8" + }, + { + "name": "direction-animated-frames", + "frames": [ + 0, + 100, + 200, + 299 + ], + "hash": "8C1A53AFFE50FBD597E8C00B82D72C8CB3E593F5E3E402D9CB033FC7AFA69468" + }, + { + "name": "direction-0", + "frames": [ + 0 + ], + "hash": "C093428F8DF1E7519487C2A11FCC859232CDCFDA9E349A50E8448A9123E3FFB3" + }, + { + "name": "direction-180", + "frames": [ + 0 + ], + "hash": "93E910B310C7D982BDC7A63CCC236EE6A396E34551526FFAAEC76DAED4C0ED05" + }, + { + "name": "elevation-15", + "frames": [ + 0 + ], + "hash": "4756D7AB9388BE2DE13B1D2255640ED0D0B20C9FA476E9F86385AE3D48D7CEA3" + }, + { + "name": "elevation-70", + "frames": [ + 0 + ], + "hash": "D44F9F81D1EBC1EA52C7CA8BF26B0419FF3930004CE4F65225B29E38D0CA9748" + }, + { + "name": "elevation-minus-35", + "frames": [ + 0 + ], + "hash": "DB556E772A329EFDC1F183CBD5067EDB6B1953D63E97E6D95E71F8B1A9B6BB74" + }, + { + "name": "spread-0", + "frames": [ + 0 + ], + "hash": "40639AE6D5FD6CA1F386CE6147FFCCEB55095AE922655D2D028BD61F2C335DB2" + }, + { + "name": "spread-35", + "frames": [ + 0 + ], + "hash": "EBACD05575886E1E02E818E57FC1420E6FE73990B5DBE25418D81C757A13DE47" + }, + { + "name": "spread-minus-25", + "frames": [ + 0 + ], + "hash": "47F8BB81DE8DF353E1092E93B1E6567B30453A62A8BB520AA4326C3000F140F1" + }, + { + "name": "length-400", + "frames": [ + 0 + ], + "hash": "55AA462ADDC20228E4092C95E6BADDC259EA632156172A2C51D37257DEE26EDB" + }, + { + "name": "length-2800", + "frames": [ + 0 + ], + "hash": "C451AF89CDBB6C9DA6FE3B93B260C850550FF38988A40930ADB3852DEE8CE728" + }, + { + "name": "length-0", + "frames": [ + 0 + ], + "hash": "1235FE946041792635D34CED5FEF36B0988A14088518D99397D71C92C9241403" + }, + { + "name": "intensity-40", + "frames": [ + 0 + ], + "hash": "7CAF9B0FC93C6F3C2B83A3403E4B266E98E50A1146DAE4C2CF7A533F9830ABB5" + }, + { + "name": "intensity-250", + "frames": [ + 0 + ], + "hash": "6156067CB7C72567462B9352B2A0FCFD0DF6C8D0E11804261170DC90A4DA3F9D" + }, + { + "name": "threshold-0", + "frames": [ + 0 + ], + "hash": "389451AA2417542ACF64D8F1FB72D7AA5D6A15AD98C86E81B8023552D2D65465" + }, + { + "name": "threshold-60", + "frames": [ + 0 + ], + "hash": "BCE8E3F0B0CB732B4504EA292BCD060BD50B56FA5F55D1B87887D9B9FDA81EFA" + }, + { + "name": "tint-amber", + "frames": [ + 0 + ], + "hash": "9B0FB621C5A34D6A51EB11C5E10BC899A934A96781021048CB9706B3008B7431" + }, + { + "name": "falloff-0", + "frames": [ + 0 + ], + "hash": "7BBB77391E84F06B0CB5F6435F29C3ACBDCABEE235E0E3D9E4DA6DA0090BF775" + }, + { + "name": "falloff-95", + "frames": [ + 0 + ], + "hash": "C424C4F07024D1AB4AEC6750D84DC82720F00B6948AD543B50EBC5A2217C7EB3" + }, + { + "name": "lamp-size-0", + "frames": [ + 0 + ], + "hash": "882AB5F9BF2EE8BBD9C8169E77619178A6FF28D9C74B46D825479CA1E91CCD82" + }, + { + "name": "lamp-size-6", + "frames": [ + 0 + ], + "hash": "DD6AA03C3AF3FE7114291E4E399D071D755AA2C779CE74D3BCCE6366D489BB6B" + }, + { + "name": "scattering-minus-60", + "frames": [ + 0 + ], + "hash": "D0FD383568CC3EE2D1B7AC78F8AB753F479908E0781D6CC0FBFC118BA39A76C0" + }, + { + "name": "scattering-85", + "frames": [ + 0 + ], + "hash": "9853FCDF0492A63C51FD8BE4509E3F374F6094BDBF244ADF02C082EB8FE08F2E" + }, + { + "name": "source-hidden", + "frames": [ + 0 + ], + "hash": "06C6B8E6792856984194EDC9413EA497655B0FAC008B7455D004FCBA2DB7F891" + }, + { + "name": "dust-60", + "frames": [ + 0 + ], + "hash": "5CFEE887B02B11B83982C53EC15BC372B9876D360A2325ADD4CF2EC8064AFE27" + }, + { + "name": "dust-60-animated-frames", + "frames": [ + 0, + 90, + 180 + ], + "hash": "EF853C8724E995B1E7028DA336DDD8F06D687484CD9476AA21424165E9B3DFE8" + }, + { + "name": "dust-seed-7", + "frames": [ + 0 + ], + "hash": "50CE845CB4114CF145D1DFDA2CF4FD1C5F2C8161BB1FBB55869AC726E03F0C21" + }, + { + "name": "quality-high", + "frames": [ + 0 + ], + "hash": "551C252D95450796D94CDBF023D03AE1306B3FFBB2383B10255AF55754A1B31D" + }, + { + "name": "quality-ultra", + "frames": [ + 0 + ], + "hash": "64BB68765A4A9BAD849A9660F83B0D8A4F2757777BF325BA4B42D70B03FFC875" + }, + { + "name": "pose-rotated-y", + "frames": [ + 0 + ], + "hash": "283424A554B7A03948CEF7B2F405EB9B1A5707D461FE6965C30C86E0FFD03BA4" + }, + { + "name": "pose-rotated-x", + "frames": [ + 0 + ], + "hash": "31038B97A2F596D9A4C013A1FF23C42312F067A9EAD1FB351CC30E635540C228" + }, + { + "name": "pose-rotated-z", + "frames": [ + 0 + ], + "hash": "E0E0F74D0E72E76C7F68D9836780D03D05CFC5B35D10AABC2BA5587282303F1D" + }, + { + "name": "pose-shrunk", + "frames": [ + 0 + ], + "hash": "2137D38C43C205AEA751E8E7C83C1A11AEB910EF00F15D0F12EB0DD10B1F2D0D" + }, + { + "name": "pose-offset", + "frames": [ + 0 + ], + "hash": "6BA09E49A690297231DA0AB7E3A3DE14CF74B4BE0E7703B21CA2EBB8368C3D9E" + }, + { + "name": "pose-near", + "frames": [ + 0 + ], + "hash": "8A099E111DA6DE8705DA977A2540BCD72683CE23D506DEFC73BC53E46D0DA9A0" + }, + { + "name": "pose-mirrored", + "frames": [ + 0 + ], + "hash": "0243DF70E5695329B661D9D1F7F9ECD71AA147E606F228EA0896BA30581EC54A" + }, + { + "name": "pose-orbit", + "frames": [ + 0 + ], + "hash": "D2A235919D4ADF6EC18338647D4520361B3325D4C73332ED44FB336047F61060" + }, + { + "name": "pose-telephoto", + "frames": [ + 0 + ], + "hash": "1C0632DD227532E91B4E4C7AD3B234700A3DA1B668DA36E62F274FA53A365968" + }, + { + "name": "pose-rotated-y-converging", + "frames": [ + 0 + ], + "hash": "BBC9E3D3D7AE995232EE26039E395E9484755B43A0C8D39A99A2D9334B4E0334" + } + ] +} \ No newline at end of file