ハーネスへ構造再計算の内訳計測を統合する

このコミットが含まれているのは:
routersys committed 2026-09-22 12:34:44 +09:00
1 親 66b0f80a36
コミット 9c35c5e265
2ファイル変更
+125

検索条件に一致するファイルはありません

+5
ファイルの表示
@@ -10,6 +10,7 @@ internal enum HarnessMode
Transition,
Compare,
Benchmark,
Structure,
}
internal sealed record HarnessArguments(HarnessMode Mode, string? Input, string? OutputDirectory, string? Before, string? After)
@@ -22,6 +23,7 @@ internal sealed record HarnessArguments(HarnessMode Mode, string? Input, string?
--transition [<出力先>] [--input <画像>] 設定を変えた後とフレームを進めた後の描画を、作り直した描画と照合します
--compare <前> <後> 2 つの出力先の PNG を画素ごとに比べます
--benchmark [--input <画像>] 描画を計測します
--structure 構造の再計算がパラメータごとに掛かる時間を内訳で計測します
""";
public static HarnessArguments Parse(string[] arguments)
@@ -49,6 +51,9 @@ internal sealed record HarnessArguments(HarnessMode Mode, string? Input, string?
case "--benchmark":
mode = Select(mode, HarnessMode.Benchmark);
break;
case "--structure":
mode = Select(mode, HarnessMode.Structure);
break;
case "--input":
if (index + 1 >= arguments.Length)
throw new HarnessException("--input には画像のパスを続けてください。");
+120
ファイルの表示
@@ -1,6 +1,9 @@
using System.Diagnostics;
using System.IO;
using System.IO.Packaging;
using System.Security.Cryptography;
using ComputeWeave;
using InkDispersion;
using InkDispersion.Harness;
using SharpGen.Runtime;
@@ -30,6 +33,12 @@ try
if (arguments.Mode == HarnessMode.Compare)
return Compare(arguments.Before!, arguments.After!);
if (arguments.Mode == HarnessMode.Structure)
{
var structureImage = arguments.Input is { } structureInput ? HarnessImage.Load(structureInput) : HarnessImage.Synthetic(CanvasWidth, CanvasHeight);
return Structure(structureImage);
}
var outputDirectory = arguments.OutputDirectory ?? Path.Combine(AppContext.BaseDirectory, "harness-output");
if (arguments.Mode == HarnessMode.Benchmark)
{
@@ -204,6 +213,117 @@ static int Transition(HarnessRenderer renderer, string outputDirectory)
return failures == 0 ? 0 : 1;
}
static int Structure(HarnessImage image)
{
const int Rounds = 12;
const int Seed = 17;
const int RectFrames = 20;
using var pipeline = InkDispersionPipeline.TryCreate();
if (pipeline is null)
throw new HarnessException("Direct3D 12を利用できません。");
var device = GraphicsDevice.GetDefault();
using var sourceTexture = device.AllocateReadWriteTexture2D<Bgra32, Float4>(image.Width, image.Height);
var pixels = new Bgra32[image.Width * image.Height];
for (var index = 0; index < pixels.Length; index++)
{
var offset = index * HarnessImage.BytesPerPixel;
pixels[index] = new Bgra32(image.Pixels[offset + 2], image.Pixels[offset + 1], image.Pixels[offset], image.Pixels[offset + 3]);
}
sourceTexture.CopyFrom(pixels);
var parameters = new InkDispersionPipeline.Parameters(InkDispersionQuality.High, 1f, 0.6f, 0.5f, 0.3f, 0.3f, 0.4f, 160f, 0.8f, 0.4f, 0.12f, 0.1f, 0.09f, 7);
pipeline.Simulate(sourceTexture, image.Width, image.Height, 0, 0, image.Width, image.Height, in parameters);
pipeline.WaitForCompletion();
var stopwatch = new Stopwatch();
var variants = new (string Name, Func<bool, InkDispersionPipeline.Parameters> Make)[]
{
("cached", _ => parameters),
("water", flip => parameters with { Water = flip ? 0.70f : 0.60f }),
("reach", flip => parameters with { ReachPixels = flip ? 176f : 160f }),
("fiber", flip => parameters with { Fiber = flip ? 0.60f : 0.50f }),
("sizing", flip => parameters with { Sizing = flip ? 0.40f : 0.30f }),
("viscosity", flip => parameters with { Viscosity = flip ? 0.40f : 0.30f }),
("drying", flip => parameters with { Drying = flip ? 0.50f : 0.40f }),
("seed", flip => parameters with { Seed = flip ? 8 : 7 }),
("all", flip => parameters with
{
Water = flip ? 0.70f : 0.60f,
ReachPixels = flip ? 176f : 160f,
Fiber = flip ? 0.60f : 0.50f,
Sizing = flip ? 0.40f : 0.30f,
Viscosity = flip ? 0.40f : 0.30f,
Drying = flip ? 0.50f : 0.40f,
Seed = flip ? 8 : 7,
}),
};
var samples = new List<double>[variants.Length];
for (var index = 0; index < samples.Length; index++)
samples[index] = new List<double>(Rounds);
foreach (var (_, make) in variants)
{
var warmup = make(true);
pipeline.Simulate(sourceTexture, image.Width, image.Height, 0, 0, image.Width, image.Height, in warmup);
pipeline.WaitForCompletion();
}
var order = Enumerable.Range(0, variants.Length).ToArray();
var random = new Random(Seed);
for (var round = 0; round < Rounds; round++)
{
for (var index = order.Length - 1; index > 0; index--)
{
var swap = random.Next(index + 1);
(order[index], order[swap]) = (order[swap], order[index]);
}
foreach (var index in order)
{
var settled = variants[index].Make(false);
pipeline.Simulate(sourceTexture, image.Width, image.Height, 0, 0, image.Width, image.Height, in settled);
pipeline.WaitForCompletion();
var measured = variants[index].Make(true);
stopwatch.Restart();
pipeline.Simulate(sourceTexture, image.Width, image.Height, 0, 0, image.Width, image.Height, in measured);
pipeline.WaitForCompletion();
stopwatch.Stop();
samples[index].Add(stopwatch.Elapsed.TotalMilliseconds);
}
}
Console.WriteLine($"structure recompute over {Rounds} interleaved rounds (ms)");
for (var index = 0; index < variants.Length; index++)
{
var sorted = samples[index].OrderBy(static value => value).ToArray();
var median = sorted[sorted.Length / 2];
Console.WriteLine($" {variants[index].Name,-11} min={sorted[0],7:F2} median={median,7:F2} max={sorted[^1],7:F2}");
}
if (pipeline.TryGetVisibleBounds(image.Width, image.Height, in parameters, out var rect))
{
using var rectOutput = device.AllocateReadWriteTexture2D<Bgra32, Float4>(rect.Width, rect.Height);
pipeline.RenderVisible(rectOutput, image.Width, image.Height, rect, in parameters);
pipeline.WaitForCompletion();
stopwatch.Restart();
for (var frame = 0; frame < RectFrames; frame++)
{
pipeline.Simulate(sourceTexture, image.Width, image.Height, 0, 0, image.Width, image.Height, in parameters);
pipeline.TryGetVisibleBounds(image.Width, image.Height, in parameters, out rect);
pipeline.RenderVisible(rectOutput, image.Width, image.Height, rect, in parameters);
}
pipeline.WaitForCompletion();
stopwatch.Stop();
Console.WriteLine($"cached frame with rect {rect.Width}x{rect.Height} at ({rect.X},{rect.Y}): {stopwatch.Elapsed.TotalMilliseconds / RectFrames:F2} ms/frame");
}
return 0;
}
static int Compare(string beforeDirectory, string afterDirectory)
{
if (!Directory.Exists(beforeDirectory) || !Directory.Exists(afterDirectory))