From b0114abdb7bf0881048fcc4228342037f2fd0fab Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 2 Jul 2026 13:58:06 +0900 Subject: [PATCH] =?UTF-8?q?GpuKernel=E3=81=B8CpuKernel=E3=81=A8=E3=81=AE?= =?UTF-8?q?=E5=87=BA=E5=8A=9B=E4=B8=80=E8=87=B4=E3=82=92=E5=AE=9F=E6=A9=9F?= =?UTF-8?q?=E3=81=A7=E7=A2=BA=E3=81=8B=E3=82=81=E3=82=8BVerify=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript/Engine/Kernel/GpuKernel.cs | 130 ++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/LuaScript/Engine/Kernel/GpuKernel.cs b/LuaScript/Engine/Kernel/GpuKernel.cs index c804fac..710970b 100644 --- a/LuaScript/Engine/Kernel/GpuKernel.cs +++ b/LuaScript/Engine/Kernel/GpuKernel.cs @@ -1,18 +1,30 @@ using System; +using System.Numerics; +using System.Runtime.InteropServices; +using Vortice.DCommon; using Vortice.Direct2D1; +using Vortice.DXGI; +using Vortice.Mathematics; using YukkuriMovieMaker.Commons; namespace LuaScript.Engine.Kernel { internal sealed class GpuKernel : IDisposable { + private const int VerifySize = 64; + private const int VerifyTolerance = 2; + + private readonly IGraphicsDevicesAndContext _devices; private readonly GpuKernelEffect _effect; private readonly float[] _packed = new float[HlslKernelEmitter.ConstantFloatCount]; private readonly byte[] _bytes = new byte[HlslKernelEmitter.ConstantFloatCount * sizeof(float)]; private ID2D1Image? _output; public GpuKernel(IGraphicsDevicesAndContext devices, KernelProgram program) - => _effect = new GpuKernelEffect(devices, HlslKernelEmitter.Emit(program)); + { + _devices = devices; + _effect = new GpuKernelEffect(devices, HlslKernelEmitter.Emit(program)); + } public bool IsReady => _effect.IsReady; @@ -25,6 +37,122 @@ namespace LuaScript.Engine.Kernel return _output ??= _effect.Output; } + public unsafe bool Verify(CpuKernel reference) + { + if (!IsReady) + return false; + + var uniforms = KernelUniformBinding.Create(); + for (int i = 0; i < uniforms.Length; i++) + uniforms[i] = (i % 7) + 1; + uniforms[(int)KernelUniform.Width] = VerifySize; + uniforms[(int)KernelUniform.Height] = VerifySize; + + byte[] probe = BuildProbe(VerifySize); + byte[] expected = (byte[])probe.Clone(); + reference.Execute(expected, VerifySize, VerifySize, uniforms); + + var dc = (ID2D1DeviceContext)_devices.DeviceContext; + ID2D1Bitmap1? source = null; + ID2D1Bitmap1? target = null; + ID2D1Bitmap1? staging = null; + try + { + source = dc.CreateEmptyBitmap(VerifySize, VerifySize, BitmapOptions.Target); + fixed (byte* ptr = probe) + source.CopyFromMemory(new nint(ptr), VerifySize * 4); + + target = dc.CreateEmptyBitmap(VerifySize, VerifySize, BitmapOptions.Target); + + var stagingProps = new BitmapProperties1( + new PixelFormat(Format.B8G8R8A8_UNorm, Vortice.DCommon.AlphaMode.Premultiplied), + 96f, 96f, + BitmapOptions.CpuRead | BitmapOptions.CannotDraw); + staging = dc.CreateBitmap(new SizeI(VerifySize, VerifySize), nint.Zero, VerifySize * 4, stagingProps); + + var output = Apply(source, uniforms); + + using var savedTarget = dc.Target; + dc.Target = target; + try + { + dc.BeginDraw(); + dc.Clear(null); + dc.DrawImage(output, new Vector2(0f, 0f)); + dc.EndDraw(); + } + finally + { + dc.Target = savedTarget; + } + + staging.CopyFromBitmap(target); + byte[] actual = new byte[VerifySize * VerifySize * 4]; + var mapped = staging.Map(MapOptions.Read); + try + { + if (mapped.Pitch == VerifySize * 4) + { + Marshal.Copy(mapped.Bits, actual, 0, actual.Length); + } + else + { + for (int row = 0; row < VerifySize; row++) + Marshal.Copy(mapped.Bits + mapped.Pitch * row, actual, row * VerifySize * 4, VerifySize * 4); + } + } + finally + { + staging.Unmap(); + } + + return WithinTolerance(expected, actual, VerifyTolerance); + } + catch (Exception ex) + { + Log.Default.Write("LuaScript: GPU kernel verification could not run; using the CPU kernel.", ex); + return false; + } + finally + { + source?.Dispose(); + target?.Dispose(); + staging?.Dispose(); + } + } + + private static byte[] BuildProbe(int size) + { + var buffer = new byte[size * size * 4]; + ReadOnlySpan alphas = [0, 64, 128, 200, 255]; + for (int y = 0; y < size; y++) + { + for (int x = 0; x < size; x++) + { + int alpha = alphas[(x + y) % alphas.Length]; + int r = x * 255 / (size - 1); + int g = y * 255 / (size - 1); + int b = (x + y) * 255 / (2 * (size - 1)); + int p = (y * size + x) * 4; + buffer[p] = (byte)(b * alpha / 255); + buffer[p + 1] = (byte)(g * alpha / 255); + buffer[p + 2] = (byte)(r * alpha / 255); + buffer[p + 3] = (byte)alpha; + } + } + return buffer; + } + + private static bool WithinTolerance(byte[] expected, byte[] actual, int tolerance) + { + if (expected.Length != actual.Length) + return false; + for (int i = 0; i < expected.Length; i++) + if (Math.Abs(expected[i] - actual[i]) > tolerance) + return false; + return true; + } + public void Dispose() { _output?.Dispose();