From 729ba355d9cf2cd57efdc3da986401186ff50fa9 Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 27 Jun 2026 09:56:56 +0900 Subject: [PATCH] =?UTF-8?q?--!native=E3=82=92LuaJIT=E3=83=AF=E3=83=BC?= =?UTF-8?q?=E3=82=AB=E3=83=BC=E3=81=B8=E6=8C=AF=E3=82=8A=E5=88=86=E3=81=91?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E9=85=8D=E7=B7=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript/LuaScriptEffectProcessor.cs | 70 +++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/LuaScript/LuaScriptEffectProcessor.cs b/LuaScript/LuaScriptEffectProcessor.cs index 50ec6cd..42d4e6a 100644 --- a/LuaScript/LuaScriptEffectProcessor.cs +++ b/LuaScript/LuaScriptEffectProcessor.cs @@ -1,5 +1,7 @@ +using System.IO; using System.Numerics; using System.Runtime.InteropServices; +using LuaScript.Engine; using Vortice; using Vortice.DCommon; using Vortice.Direct2D1; @@ -42,10 +44,18 @@ namespace LuaScript DrawDescription InputDesc ); + private const int NativeTimeoutMilliseconds = 5000; + + private static string NativeDirectory => + Path.Combine(Path.GetDirectoryName(typeof(LuaScriptEffectProcessor).Assembly.Location) ?? AppContext.BaseDirectory, "native"); + private readonly LuaScriptEngine _engine = new(); private readonly SemaphoreSlim _pixelLoaderSemaphore = new(1, 1); private AviUtlScriptContext _context = new(); + private LuaJitWorker? _nativeWorker; + private double[]? _nativeFields; + private EffectDescription? _frameDesc; private SceneObjectResolver? _frameResolver; private bool _frameResolverBuilt; @@ -168,19 +178,27 @@ namespace LuaScript try { - _engine.Execute(script, ctx); - - if (ctx.IsPixelsDirty) + if (ScriptDirective.Resolve(script) == ScriptEngineKind.Native && + LuaJitWorker.IsAvailable(NativeDirectory)) { - EnsureBitmaps(imgW, imgH); - WritePixelsToOutput(ctx.GetPixelBuffer()!, imgW); - UpdateTransformEffect(bounds); - effectOutput = _transformEffect!.Output; - pixelsModified = true; + pixelsModified = ExecuteNative(script, ctx, bounds, imgW, imgH); } else { - effectOutput = null; + _engine.Execute(script, ctx); + + if (ctx.IsPixelsDirty) + { + EnsureBitmaps(imgW, imgH); + WritePixelsToOutput(ctx.GetPixelBuffer()!, imgW); + UpdateTransformEffect(bounds); + effectOutput = _transformEffect!.Output; + pixelsModified = true; + } + else + { + effectOutput = null; + } } outDesc = BuildOutputDesc(inDesc, ctx); @@ -326,6 +344,39 @@ namespace LuaScript }; } + private bool ExecuteNative(string script, AviUtlScriptContext ctx, RawRectF bounds, int imgW, int imgH) + { + _nativeWorker ??= new LuaJitWorker(NativeDirectory); + _nativeFields ??= new double[NativeProtocol.FieldCount]; + + NativeFieldMap.ToFields(ctx, _nativeFields); + byte[] buffer = LoadInputPixels(bounds, imgW, imgH); + + bool ok = _nativeWorker.Execute( + script, _nativeFields, buffer, imgW, imgH, NativeTimeoutMilliseconds, out bool dirty, out string? error); + + if (!ok) + { + if (error is not null && error.Contains("timed out", StringComparison.Ordinal)) + throw new LuaScriptTimeoutException(error); + throw new LuaScriptRuntimeException(error ?? "Native script execution failed."); + } + + NativeFieldMap.FromFields(_nativeFields, ctx); + + if (dirty) + { + EnsureBitmaps(imgW, imgH); + WritePixelsToOutput(buffer, imgW); + UpdateTransformEffect(bounds); + effectOutput = _transformEffect!.Output; + return true; + } + + effectOutput = null; + return false; + } + private byte[] LoadInputPixels(RawRectF bounds, int width, int height) { _pixelLoaderSemaphore.Wait(); @@ -438,6 +489,7 @@ namespace LuaScript if (disposing) { _engine.Dispose(); + _nativeWorker?.Dispose(); _pixelLoaderSemaphore.Dispose(); _renderTarget?.Dispose(); _stagingBitmap?.Dispose();