From 865f5117ea9a615d65d81c625dfe75b3254d759b Mon Sep 17 00:00:00 2001 From: routersys Date: Thu, 2 Jul 2026 19:04:05 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=AB=E3=83=BC=E3=83=8D=E3=83=AB=E6=8A=BD?= =?UTF-8?q?=E5=87=BA=E3=81=AE=E6=A4=9C=E6=9F=BB=E6=BC=8F=E3=82=8C=E3=81=A8?= =?UTF-8?q?fmod=E3=81=AE=E5=89=B0=E4=BD=99=E5=AE=9F=E8=A3=85=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/KernelCpuEquivalenceTests.cs | 8 ++++++ LuaScript.Tests/KernelSimdEquivalenceTests.cs | 2 ++ LuaScript/Engine/Kernel/CpuKernelCompiler.cs | 2 ++ LuaScript/Engine/Kernel/KernelExtractor.cs | 28 +++++++++++++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/LuaScript.Tests/KernelCpuEquivalenceTests.cs b/LuaScript.Tests/KernelCpuEquivalenceTests.cs index 120e652..fa8bb8e 100644 --- a/LuaScript.Tests/KernelCpuEquivalenceTests.cs +++ b/LuaScript.Tests/KernelCpuEquivalenceTests.cs @@ -21,6 +21,8 @@ namespace LuaScript.Tests [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) local v=(r+g+b)/3 obj.setpixel(x,y,v>128 and 255 or 0,v>128 and 255 or 0,v>128 and 255 or 0,a) end end")] [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,math.min(255,r+50),math.max(0,g-50),math.floor(b/16)*16,a) end end")] [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,r%64,math.abs(g-128),math.sqrt(b*255),a) end end")] + [InlineData("local x=99 local k=3 for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,x/obj.w*255+k,g,b,a) end end")] + [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,math.fmod(r,64),math.fmod(g,7)*36,math.fmod(b-128,60)+128,a) end end")] public void CpuKernelMatchesMoonSharp(string script) { var fields = new Dictionary { ["w"] = Width, ["h"] = Height }; @@ -43,6 +45,7 @@ namespace LuaScript.Tests [InlineData("local pd=obj.getpixeldata() for y=0,obj.h-1 do for x=0,obj.w-1 do local i=(y*obj.w+x)*4 local r=pd:get(i+1) local g=pd:get(i+2) local b=pd:get(i+3) local l=0.298912*r+0.586611*g+0.114478*b pd:set(i+1,l) pd:set(i+2,l) pd:set(i+3,l) end end")] [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for x=0,pd.width-1 do local i=(y*pd.width+x)*4 local r=pd:get(i+1) local g=pd:get(i+2) local b=pd:get(i+3) pd:set(i+1,b) pd:set(i+2,g) pd:set(i+3,r) end end")] [InlineData("local pd=obj.getpixeldata() for y=0,obj.h-1 do for x=0,obj.w-1 do local i=(y*obj.w+x)*4 local r=pd:get(i+1) local a=pd:get(i+4) pd:set(i+1,r*a/255) pd:set(i+2,pd:get(i+2)*obj.track0/100) pd:set(i+3,math.min(255,pd:get(i+3)+50)) end end")] + [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for x=0,pd.width-1 do local i=(y*pd.width+x)*4 local r=pd:get(i+1) local g=pd:get(i+2) local b=pd:get(i+3) pd:set(i+1,r*3) pd:set(i+2,g-300) pd:set(i+3,b*2-255) end end")] public void GetPixelDataKernelMatchesMoonSharp(string script) { var fields = new Dictionary { ["w"] = Width, ["h"] = Height, ["track0"] = 73d }; @@ -54,6 +57,9 @@ namespace LuaScript.Tests [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for x=0,pd.width-1 do local i=(y*pd.width+x)*4 pd:set(i+1,pd:get(i+5)) pd:set(i+2,0) pd:set(i+3,0) end end")] [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for x=0,pd.width-1 do local i=((y+1)*pd.width+x)*4 pd:set(i+1,0) pd:set(i+2,0) pd:set(i+3,0) end end")] [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for x=0,pd.width-1 do local i=(y*pd.width+x)*4 pd:set(i+1,pd:get(i+3)) pd:set(i+2,pd:get(i+2)) pd:set(i+3,pd:get(i+1)) end end")] + [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for x=0,pd.width-1 do local i=(y*pd.width+x)*4 local i=i+4 pd:set(i+1,0) pd:set(i+2,0) pd:set(i+3,0) end end")] + [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for pd=0,pd.width-1 do local i=(y*2+pd)*4 pd:set(i+1,0) pd:set(i+2,0) pd:set(i+3,0) end end")] + [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for x=0,pd.width-1 do local i=(y*pd.width+x)*4 local pd=0 pd:set(i+1,0) end end")] public void GetPixelDataNonKernelScriptsAreRejected(string script) { Assert.Null(KernelExtractor.TryExtract(script)); @@ -128,6 +134,8 @@ namespace LuaScript.Tests private static void RunMoonSharp(string script, byte[] buffer, Dictionary fields, Dictionary globals) { var engine = new Script(CoreModules.Basic | CoreModules.Math | CoreModules.Bit32); + engine.Globals.Get("math").Table["fmod"] = DynValue.NewCallback((_, a) => + DynValue.NewNumber(a[0].Number % a[1].Number)); var obj = new Table(engine); foreach (var (name, value) in fields) obj[name] = value; diff --git a/LuaScript.Tests/KernelSimdEquivalenceTests.cs b/LuaScript.Tests/KernelSimdEquivalenceTests.cs index e1df618..ecf5619 100644 --- a/LuaScript.Tests/KernelSimdEquivalenceTests.cs +++ b/LuaScript.Tests/KernelSimdEquivalenceTests.cs @@ -23,6 +23,7 @@ namespace LuaScript.Tests [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for x=0,pd.width-1 do local i=(y*pd.width+x)*4 local r=pd:get(i+1) local g=pd:get(i+2) local b=pd:get(i+3) pd:set(i+1,255-r) pd:set(i+2,g*g/255) pd:set(i+3,b) end end")] [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,((r>128 and g<64) or (not (b>200))) and 255 or 0,g,b,a) end end")] [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,(r>=200) and 255 or 0,(g<=50) and 255 or 0,(b==0) and 255 or ((b~=0) and 100 or 0),a) end end")] + [InlineData("local pd=obj.getpixeldata() for y=0,pd.height-1 do for x=0,pd.width-1 do local i=(y*pd.width+x)*4 local r=pd:get(i+1) local g=pd:get(i+2) local b=pd:get(i+3) pd:set(i+1,r*3) pd:set(i+2,g-300) pd:set(i+3,b*2-255) end end")] public void SimdBlockMatchesScalarRow(string script) { var program = KernelExtractor.TryExtract(script); @@ -54,6 +55,7 @@ namespace LuaScript.Tests [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,r^2/255,g,b,a) end end")] [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,255*math.sin(r/255),g,b,a) end end")] [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,math.atan2(r,g),b,a,a) end end")] + [InlineData("for y=0,obj.h-1 do for x=0,obj.w-1 do local r,g,b,a=obj.getpixel(x,y) obj.setpixel(x,y,math.fmod(r,64),g,b,a) end end")] public void SimdRejectsNonVectorizableKernels(string script) { var program = KernelExtractor.TryExtract(script); diff --git a/LuaScript/Engine/Kernel/CpuKernelCompiler.cs b/LuaScript/Engine/Kernel/CpuKernelCompiler.cs index 8f6b411..6cdfbd8 100644 --- a/LuaScript/Engine/Kernel/CpuKernelCompiler.cs +++ b/LuaScript/Engine/Kernel/CpuKernelCompiler.cs @@ -155,6 +155,8 @@ namespace LuaScript.Engine.Kernel private static Expression BuildCall(KCall call, Context context) { + if (call.Func == KFunc.Fmod) + return Expression.Modulo(Build(call.Arguments[0], context), Build(call.Arguments[1], context)); var method = Functions[call.Func]; if (call.Arguments.Count == 1) return Expression.Call(method, Build(call.Arguments[0], context)); diff --git a/LuaScript/Engine/Kernel/KernelExtractor.cs b/LuaScript/Engine/Kernel/KernelExtractor.cs index 54c2030..911c36a 100644 --- a/LuaScript/Engine/Kernel/KernelExtractor.cs +++ b/LuaScript/Engine/Kernel/KernelExtractor.cs @@ -80,6 +80,14 @@ namespace LuaScript.Engine.Kernel return null; if (outerAxis == innerAxis) return null; + if (_pixelDataVariable is not null && + (string.Equals(outerVar, _pixelDataVariable, StringComparison.Ordinal) || + string.Equals(innerVar, _pixelDataVariable, StringComparison.Ordinal))) + return null; + if (_scope.ContainsKey(outerVar)) + _scope.Remove(outerVar); + if (_scope.ContainsKey(innerVar)) + _scope.Remove(innerVar); _widthVariable = outerAxis == KAxis.X ? outerVar : innerVar; _heightVariable = outerAxis == KAxis.Y ? outerVar : innerVar; @@ -105,8 +113,8 @@ namespace LuaScript.Engine.Kernel if (body[0] is not LocalStmt { Names.Count: 1, Values.Count: 1 } baseStmt || !IsBaseIndex(baseStmt.Values[0])) return null; + EnsureNotCoordinateName(baseStmt.Names[0]); _baseIndexVariable = baseStmt.Names[0]; - EnsureNotCoordinateName(_baseIndexVariable); _coordsEnabled = true; KExpr? outR = null, outG = null, outB = null; @@ -121,7 +129,7 @@ namespace LuaScript.Engine.Kernel BindAssign(assign); break; case CallStmt { Call: MethodCallExpr set } when IsPixelDataSet(set, out int writeChannel): - var value = Lower(set.Arguments[1]); + var value = ClampChannel(Lower(set.Arguments[1])); if (writeChannel == 0) outR = value; else if (writeChannel == 1) outG = value; else outB = value; @@ -186,6 +194,18 @@ namespace LuaScript.Engine.Kernel return true; } + private KExpr ClampChannel(KExpr value) + { + var bound = new KLocalRef(AddBinding(value)); + return new KSelect( + new KCompare(KCompareOp.Less, bound, new KConst(0d)), + new KConst(0d), + new KSelect( + new KCompare(KCompareOp.Greater, bound, new KConst(255d)), + new KConst(255d), + bound)); + } + private bool TryChannelOffset(LuaExpr expression, int min, int max, out int offset) { offset = 0; @@ -284,6 +304,10 @@ namespace LuaScript.Engine.Kernel if (string.Equals(name, _widthVariable, StringComparison.Ordinal) || string.Equals(name, _heightVariable, StringComparison.Ordinal)) throw new KernelUnsupportedException("Local variable shadows a loop coordinate."); + if (_pixelDataVariable is not null && string.Equals(name, _pixelDataVariable, StringComparison.Ordinal)) + throw new KernelUnsupportedException("Local variable shadows the pixel-data handle."); + if (_baseIndexVariable.Length > 0 && string.Equals(name, _baseIndexVariable, StringComparison.Ordinal)) + throw new KernelUnsupportedException("Local variable shadows the base index."); } private int AddBinding(KExpr expression)