MoonSharpの画像加工を自動GPU委譲へ統一

このコミットが含まれているのは:
routersys committed 2026-07-05 18:24:07 +09:00
1 親 d6756ad3c8
コミット 7c8776dd05
3ファイル変更
+61 -18

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

+6 -6
ファイルの表示
@@ -511,7 +511,7 @@ namespace LuaScript
private byte[]? _resizeSource;
private byte[]? _resizeBuffer;
public unsafe void FillBuffer(double r, double g, double b, double a, int x, int y, int w, int h, bool accelerate = false)
public unsafe void FillBuffer(double r, double g, double b, double a, int x, int y, int w, int h)
{
EnsurePixelBuffer();
if (_pixelBuffer is null)
@@ -526,7 +526,7 @@ namespace LuaScript
if (x1 <= x0 || y1 <= y0)
return;
if (accelerate && PixelProcessor?.TryFill(_pixelBuffer, cw, ch, r, g, b, a, x0, y0, x1 - x0, y1 - y0) == true)
if (PixelProcessor?.TryFill(_pixelBuffer, cw, ch, r, g, b, a, x0, y0, x1 - x0, y1 - y0) == true)
{
_isPixelsDirty = true;
return;
@@ -621,7 +621,7 @@ namespace LuaScript
}
}
public unsafe void Convolve(double[] kernel, int size, double divisor, double offset, bool accelerate = false)
public unsafe void Convolve(double[] kernel, int size, double divisor, double offset)
{
EnsurePixelBuffer();
if (_pixelBuffer is null)
@@ -629,7 +629,7 @@ namespace LuaScript
int cw = ImageWidth;
int ch = ImageHeight;
if (accelerate && PixelProcessor?.TryConvolve(_pixelBuffer, cw, ch, kernel, size, divisor, offset) == true)
if (PixelProcessor?.TryConvolve(_pixelBuffer, cw, ch, kernel, size, divisor, offset) == true)
{
_isPixelsDirty = true;
return;
@@ -639,7 +639,7 @@ namespace LuaScript
PixelBufferSoftwareProcessor.Convolve(_pixelBuffer, cw, ch, kernel, size, divisor, offset, ref _convolveSource);
}
public unsafe void Resize(int newWidth, int newHeight, bool linear, bool accelerate = false)
public unsafe void Resize(int newWidth, int newHeight, bool linear)
{
EnsurePixelBuffer();
if (_pixelBuffer is null)
@@ -650,7 +650,7 @@ namespace LuaScript
int cw = ImageWidth;
int ch = ImageHeight;
if (accelerate && PixelProcessor?.TryResize(_pixelBuffer, cw, ch, newWidth, newHeight, linear, out var processed) == true && processed is not null)
if (PixelProcessor?.TryResize(_pixelBuffer, cw, ch, newWidth, newHeight, linear, out var processed) == true && processed is not null)
{
ReplaceBuffer(processed, newWidth, newHeight);
return;
+12 -12
ファイルの表示
@@ -187,9 +187,9 @@ namespace LuaScript
private void RegisterFastPrimitives(Script script)
{
script.Globals.Set("__fast_fill", DynValue.NewCallback((_, args) => FillCore(args, true)));
script.Globals.Set("__fast_convolve", DynValue.NewCallback((_, args) => ConvolveCore(args, true)));
script.Globals.Set("__fast_resize", DynValue.NewCallback((_, args) => ResizeCore(args, true)));
script.Globals.Set("__fast_fill", DynValue.NewCallback((_, args) => FillCore(args)));
script.Globals.Set("__fast_convolve", DynValue.NewCallback((_, args) => ConvolveCore(args)));
script.Globals.Set("__fast_resize", DynValue.NewCallback((_, args) => ResizeCore(args)));
}
private void EnsureScriptIntegrity()
@@ -1015,9 +1015,9 @@ namespace LuaScript
}
[LuaFunction("fill")]
private DynValue Fill(CallbackArguments args) => FillCore(args, false);
private DynValue Fill(CallbackArguments args) => FillCore(args);
private DynValue FillCore(CallbackArguments args, bool accelerate)
private DynValue FillCore(CallbackArguments args)
{
_activeCancellation.ThrowIfCancellationRequested();
if (_activeContext is null)
@@ -1033,7 +1033,7 @@ namespace LuaScript
int w = args.Count > 6 ? (int)(args[6].CastToNumber() ?? 0d) : ctx.ImageWidth;
int h = args.Count > 7 ? (int)(args[7].CastToNumber() ?? 0d) : ctx.ImageHeight;
ctx.FillBuffer(r, g, b, a, x, y, w, h, accelerate);
ctx.FillBuffer(r, g, b, a, x, y, w, h);
return DynValue.Void;
}
@@ -1086,9 +1086,9 @@ namespace LuaScript
}
[LuaFunction("convolve")]
private DynValue Convolve(CallbackArguments args) => ConvolveCore(args, false);
private DynValue Convolve(CallbackArguments args) => ConvolveCore(args);
private DynValue ConvolveCore(CallbackArguments args, bool accelerate)
private DynValue ConvolveCore(CallbackArguments args)
{
_activeCancellation.ThrowIfCancellationRequested();
if (_activeContext is null || args.Count < 2 || args[0].Type != DataType.Table)
@@ -1115,14 +1115,14 @@ namespace LuaScript
divisor = 1d;
double offset = args.Count > 3 ? args[3].CastToNumber() ?? 0d : 0d;
_activeContext.Convolve(_kernelScratch, size, divisor, offset, accelerate);
_activeContext.Convolve(_kernelScratch, size, divisor, offset);
return DynValue.Void;
}
[LuaFunction("resize")]
private DynValue Resize(CallbackArguments args) => ResizeCore(args, false);
private DynValue Resize(CallbackArguments args) => ResizeCore(args);
private DynValue ResizeCore(CallbackArguments args, bool accelerate)
private DynValue ResizeCore(CallbackArguments args)
{
_activeCancellation.ThrowIfCancellationRequested();
if (_activeContext is null || args.Count < 2)
@@ -1134,7 +1134,7 @@ namespace LuaScript
return DynValue.Void;
bool linear = !(args.Count > 2 && args[2].Type == DataType.String && args[2].String == "nearest");
_activeContext.Resize(w, h, linear, accelerate);
_activeContext.Resize(w, h, linear);
RefreshObjDimensions();
return DynValue.Void;
}
+43
ファイルの表示
@@ -52,6 +52,49 @@ namespace LuaScript.Tests
return buffer;
}
private sealed class RecordingProcessor : IPixelBufferProcessor
{
public int FillCalls;
public int ConvolveCalls;
public int ResizeCalls;
public bool TryFill(byte[] target, int width, int height, double r, double g, double b, double a, int x, int y, int fillWidth, int fillHeight)
{
FillCalls++;
return false;
}
public bool TryConvolve(byte[] target, int width, int height, double[] kernel, int size, double divisor, double offset)
{
ConvolveCalls++;
return false;
}
public bool TryResize(byte[] source, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight, bool linear, out byte[]? target)
{
ResizeCalls++;
target = null;
return false;
}
}
[Fact]
public void BufferOps_ConsultPixelProcessorAndFallBackToCpu()
{
using var engine = CreateEngine();
var ctx = NewContext(new byte[2 * 2 * 4], 2, 2);
var processor = new RecordingProcessor();
ctx.PixelProcessor = processor;
engine.Execute("obj.fill(200, 100, 50, 255) obj.convolve({1,1,1,1,1,1,1,1,1}, 3) obj.resize(4, 4)", ctx);
Assert.Equal(1, processor.FillCalls);
Assert.Equal(1, processor.ConvolveCalls);
Assert.Equal(1, processor.ResizeCalls);
Assert.Equal(4, ctx.ImageWidth);
Assert.Equal(4, ctx.ImageHeight);
}
[Fact]
public void Fill_WholeBuffer_WritesPremultipliedColor()
{