From eb0199131dd83a3ab685bf4a5149b5790307c694 Mon Sep 17 00:00:00 2001 From: routersys Date: Sat, 4 Jul 2026 11:58:07 +0900 Subject: [PATCH] =?UTF-8?q?worker=E3=81=B8obj.pixelshader=E3=81=A8?= =?UTF-8?q?=E4=BB=AE=E6=83=B3=E3=83=95=E3=83=AC=E3=83=BC=E3=83=A0=E3=83=90?= =?UTF-8?q?=E3=83=83=E3=83=95=E3=82=A1=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LuaScript.Tests/NativeWorkerRoundTripTests.cs | 151 ++++++++++++- native/worker.lua | 212 +++++++++++++++++- 2 files changed, 357 insertions(+), 6 deletions(-) diff --git a/LuaScript.Tests/NativeWorkerRoundTripTests.cs b/LuaScript.Tests/NativeWorkerRoundTripTests.cs index 95af86b..c41b0ef 100644 --- a/LuaScript.Tests/NativeWorkerRoundTripTests.cs +++ b/LuaScript.Tests/NativeWorkerRoundTripTests.cs @@ -1434,16 +1434,161 @@ namespace LuaScript.Tests } [Fact] - public void PixelShader_ReportsClearError() + public void PixelShader_RoundTrip_StagesResourcesAndWritesTarget() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + var pixels = new byte[16]; + for (int i = 0; i < pixels.Length; i++) + pixels[i] = (byte)(i + 1); + var original = (byte[])pixels.Clone(); + var fields = Fields(2, 2, 0d); + + string? observedName = null; + PixelShaderInput[] observedResources = []; + float[] observedConstants = []; + PixelShaderBlend observedBlend = default; + PixelShaderSampler observedSampler = default; + int observedWidth = 0; + int observedHeight = 0; + _runPixelShader = (string name, ReadOnlySpan resources, ReadOnlySpan constants, PixelShaderBlend blend, PixelShaderSampler sampler, byte[] target, int targetWidth, int targetHeight, out string? shaderError) => + { + observedName = name; + observedResources = resources.ToArray(); + observedConstants = constants.ToArray(); + observedBlend = blend; + observedSampler = sampler; + observedWidth = targetWidth; + observedHeight = targetHeight; + for (int i = 0; i < targetWidth * targetHeight * 4; i++) + target[i] = 0x5A; + shaderError = null; + return PixelShaderRunStatus.Success; + }; + + bool ok = RunWorker( + "obj.pixelshader(\"ps\", \"object\", {\"object\", \"random\"}, {1, 2.5}, 25, \"clamp\")", + fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out bool dirty, out _, out byte[]? resultPixels, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.True(dirty); + Assert.Equal("ps", observedName); + Assert.Equal(2, observedResources.Length); + Assert.False(observedResources[0].IsRandom); + Assert.Equal(2, observedResources[0].Width); + Assert.Equal(2, observedResources[0].Height); + Assert.Equal(original, observedResources[0].Data!.AsSpan(0, 16).ToArray()); + Assert.True(observedResources[1].IsRandom); + Assert.Equal([1f, 2.5f], observedConstants); + Assert.Equal(PixelShaderBlend.Composite(25d), observedBlend); + Assert.Equal(PixelShaderSampler.Clamp, observedSampler); + Assert.Equal(2, observedWidth); + Assert.Equal(2, observedHeight); + Assert.NotNull(resultPixels); + Assert.All(resultPixels.AsSpan(0, 16).ToArray(), static b => Assert.Equal(0x5A, b)); + } + + [Fact] + public void PixelShader_TempBufferTarget_RoundTripsThroughCopyBuffer() { Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); var pixels = new byte[16]; var fields = Fields(2, 2, 0d); - bool ok = RunWorker("obj.pixelshader(\"ps\", \"object\", \"object\")", fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out _, out _, out _, out _, out _, out string? error); + _runPixelShader = (string name, ReadOnlySpan resources, ReadOnlySpan constants, PixelShaderBlend blend, PixelShaderSampler sampler, byte[] target, int targetWidth, int targetHeight, out string? shaderError) => + { + for (int i = 0; i < targetWidth * targetHeight * 4; i++) + target[i] = 0x33; + shaderError = null; + return PixelShaderRunStatus.Success; + }; + + bool ok = RunWorker( + "obj.pixelshader(\"ps\", \"tempbuffer\", \"object\")\nobj.copybuffer(\"obj\", \"tmp\")", + fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out bool dirty, out _, out byte[]? resultPixels, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.True(dirty); + Assert.NotNull(resultPixels); + Assert.All(resultPixels.AsSpan(0, 16).ToArray(), static b => Assert.Equal(0x33, b)); + } + + [Fact] + public void PixelShader_FramebufferTarget_ReplacesObjectWithSceneSize() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + var pixels = new byte[16]; + var fields = Fields(2, 2, 0d); + fields[NativeProtocol.SceneWidth] = 4; + fields[NativeProtocol.SceneHeight] = 3; + + int observedWidth = 0; + int observedHeight = 0; + _runPixelShader = (string name, ReadOnlySpan resources, ReadOnlySpan constants, PixelShaderBlend blend, PixelShaderSampler sampler, byte[] target, int targetWidth, int targetHeight, out string? shaderError) => + { + observedWidth = targetWidth; + observedHeight = targetHeight; + for (int i = 0; i < targetWidth * targetHeight * 4; i++) + target[i] = 0x77; + shaderError = null; + return PixelShaderRunStatus.Success; + }; + + bool ok = RunWorker( + "obj.pixelshader(\"ps\", \"framebuffer\", \"object\")", + fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out bool dirty, out bool replaced, out byte[]? resultPixels, out int resultW, out int resultH, out string? error); + + Assert.True(ok, error); + Assert.True(dirty); + Assert.True(replaced); + Assert.Equal(4, observedWidth); + Assert.Equal(3, observedHeight); + Assert.Equal(4, resultW); + Assert.Equal(3, resultH); + Assert.NotNull(resultPixels); + Assert.All(resultPixels.AsSpan(0, 48).ToArray(), static b => Assert.Equal(0x77, b)); + } + + [Fact] + public void PixelShader_CompileError_IsReportedToScript() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + var pixels = new byte[16]; + var fields = Fields(2, 2, 0d); + + _runPixelShader = (string name, ReadOnlySpan resources, ReadOnlySpan constants, PixelShaderBlend blend, PixelShaderSampler sampler, byte[] target, int targetWidth, int targetHeight, out string? shaderError) => + { + shaderError = "obj.pixelshader: shader 'ps' failed to compile.\nerror X3000: syntax error"; + return PixelShaderRunStatus.CompileError; + }; + + bool ok = RunWorker( + "obj.pixelshader(\"ps\", \"object\", \"object\")", + fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out _, out _, out _, out _, out _, out string? error); + Assert.False(ok); - Assert.Contains("pixelshader", error); + Assert.Contains("X3000", error); + } + + [Fact] + public void PixelShader_WithoutHost_IsSilentlySkipped() + { + Assert.True(LuaJitWorker.IsAvailable(NativeDir), "native/luajit.exe must be present"); + + var pixels = new byte[16]; + var fields = Fields(2, 2, 0d); + _runPixelShader = null; + + bool ok = RunWorker( + "obj.pixelshader(\"ps\", \"object\", \"object\")\nobj.x = 12", + fields, NoStringParams, () => pixels, 2, 2, 5000, NoResolver, NoLoadFigure, NoLoadText, NoLoadImage, NoLoadMovie, NoAddEffect, NoAddDraw, NoSetAnchor, out bool dirty, out _, out _, out _, out _, out string? error); + + Assert.True(ok, error); + Assert.False(dirty); + Assert.Equal(12d, fields[NativeProtocol.X]); } [Fact] diff --git a/native/worker.lua b/native/worker.lua index 05408b3..6103635 100644 --- a/native/worker.lua +++ b/native/worker.lua @@ -93,6 +93,16 @@ local CB_KIND_REQUESTPIXELS = 9 local CB_KIND_FLUSHDRAWS = 10 local CB_KIND_SCENEGET = 11 local CB_KIND_SCENESET = 12 +local CB_KIND_PIXELSHADER_STAGE = 13 +local CB_KIND_PIXELSHADER_RUN = 14 +local MAX_PIXEL_BUFFER = 3840 * 2160 * 4 +local SHADER_META_DOUBLES = 20 +local SHADER_CONSTANTS_MAX = 1024 +local SHADER_RESOURCES_MAX = 8 +local SHADER_REGION_BYTES = SHADER_META_DOUBLES * 8 + SHADER_CONSTANTS_MAX * 4 + MAX_PIXEL_BUFFER +local SHADER_META_OFFSET = mapSize - SHADER_REGION_BYTES +local SHADER_CONST_OFFSET = SHADER_META_OFFSET + SHADER_META_DOUBLES * 8 +local SHADER_IMAGE_OFFSET = SHADER_CONST_OFFSET + SHADER_CONSTANTS_MAX * 4 assert(loadfile(shimPath))() @@ -107,7 +117,10 @@ local pixels = ffi.cast("uint8_t*", base + PIXEL_OFFSET) local ringD = ffi.cast("double*", base + DRAW_RING_OFFSET) local cbResult = ffi.cast("double*", base + CB_RESULT_OFFSET) local cbTagD = ffi.cast("double*", base + CB_TAG_OFFSET) -local pixelCapacity = mapSize - PIXEL_OFFSET +local shaderMeta = ffi.cast("double*", base + SHADER_META_OFFSET) +local shaderConst = ffi.cast("float*", base + SHADER_CONST_OFFSET) +local shaderImage = base + SHADER_IMAGE_OFFSET +local pixelCapacity = SHADER_META_OFFSET - PIXEL_OFFSET local buffers = {} local options = {} local pixeloptions = {} @@ -298,6 +311,32 @@ local function compositeDrawPolyInto(dst, dstW, dstH, src, srcW, srcH, poly, alp end end +local frameBufferData, frameBufferCapacity = nil, 0 +local frameBufferW, frameBufferH = 0, 0 +local frameBufferValid = false + +local function ensureFrameBuffer() + local sw = math.max(1, math.floor(f64[39])) + local sh = math.max(1, math.floor(f64[40])) + local n = sw * sh * 4 + if not frameBufferData or frameBufferCapacity < n then + frameBufferData = ffi.new("uint8_t[?]", n) + frameBufferCapacity = n + frameBufferValid = false + end + if not frameBufferValid or frameBufferW ~= sw or frameBufferH ~= sh then + ffi.fill(frameBufferData, n) + ensurePixels() + flushPixelData() + frameBufferW = sw + frameBufferH = sh + compositeDrawInto(frameBufferData, sw, sh, pixels, width, height, + sw * 0.5 + f64[8], sh * 0.5 + f64[9], f64[17], f64[18], f64[19] / 255, true) + frameBufferValid = true + end + return frameBufferData, frameBufferW, frameBufferH +end + local function ensureTemp() local t = buffers["t"] if not t then @@ -684,6 +723,8 @@ function obj.copybuffer(dst, src) local b = buffers[skey] if not b then return end data, w, h = b.data, b.w, b.h + elseif sk == "f" then + data, w, h = ensureFrameBuffer() else return end @@ -818,8 +859,172 @@ local function buildObject() } end -function obj.pixelshader() - error("obj.pixelshader is not supported on the native engine. Remove the --!native directive.", 2) +local SHADER_BLEND_MODES = { copy = 0, mask = 1, draw = 2, add = 3 } +local SHADER_SAMPLERS = { clip = 1, clamp = 2, loop = 3, mirror = 4, dot = 5 } + +local function stageShaderImage(slot, data, w, h) + if w * h * 4 > MAX_PIXEL_BUFFER then + error("obj.pixelshader: resource is too large for the native engine.", 4) + end + ffi.copy(shaderImage, data, w * h * 4) + shaderMeta[5] = w + shaderMeta[6] = h + shaderMeta[7] = slot + i32[OFF_CB_KIND] = CB_KIND_PIXELSHADER_STAGE + i32[OFF_STATUS] = STATUS_CALLBACK + k32.SetEvent(doneEvent) + k32.WaitForSingleObject(workEvent, INFINITE) +end + +local function resolveShaderResource(slot, id) + if type(id) ~= "string" then return 2 end + if id == "random" then return 1 end + local bk, bkey = bufferKind(id) + if bk == "o" then + ensurePixels() + flushPixelData() + return 3 + end + if bk == "f" then + local data, w, h = ensureFrameBuffer() + stageShaderImage(slot, data, w, h) + return 0 + end + if bk == "t" or bk == "c" then + local b = buffers[bkey] + if b then + stageShaderImage(slot, b.data, b.w, b.h) + return 0 + end + end + return 2 +end + +function obj.pixelshader(name, target, resources, constants, blend, sampler) + if type(name) ~= "string" or type(target) ~= "string" then return end + + local blendMode, compositeBlend = 0, 0 + local blendType = type(blend) + if blendType == "number" then + blendMode = 4 + compositeBlend = blend + elseif blendType == "string" then + blendMode = SHADER_BLEND_MODES[blend] + if not blendMode then + error("obj.pixelshader: unknown blend '" .. blend .. "'. Use copy, mask, draw, add or a blend number.", 2) + end + end + + local samplerMode = 0 + if type(sampler) == "string" then + samplerMode = SHADER_SAMPLERS[sampler] + if not samplerMode then + error("obj.pixelshader: unknown sampler '" .. sampler .. "'. Use clip, clamp, loop, mirror or dot.", 2) + end + end + + local constantCount = 0 + if type(constants) == "table" then + constantCount = #constants + if constantCount > SHADER_CONSTANTS_MAX then + error("obj.pixelshader: too many constants (" .. constantCount .. "). Up to " .. SHADER_CONSTANTS_MAX .. " are supported.", 2) + end + for i = 1, constantCount do + shaderConst[i - 1] = tonumber(constants[i]) or 0 + end + end + + local resourceCount = 0 + if type(resources) == "string" then + resourceCount = 1 + shaderMeta[9] = resolveShaderResource(0, resources) + elseif type(resources) == "table" then + resourceCount = #resources + if resourceCount > SHADER_RESOURCES_MAX then + error("obj.pixelshader: too many resources (" .. resourceCount .. "). Up to " .. SHADER_RESOURCES_MAX .. " are supported.", 2) + end + for i = 1, resourceCount do + shaderMeta[9 + (i - 1)] = resolveShaderResource(i - 1, resources[i]) + end + end + + local tk, tkey = bufferKind(target) + local targetKind, targetW, targetH, targetData + if tk == "o" then + ensurePixels() + flushPixelData() + targetKind, targetW, targetH = 1, width, height + elseif tk == "t" or tk == "c" then + local b = buffers[tkey] + if not b then + b = { data = ffi.new("uint8_t[?]", width * height * 4), w = width, h = height } + buffers[tkey] = b + end + targetKind, targetW, targetH, targetData = 0, b.w, b.h, b.data + elseif tk == "f" then + local data, w, h = ensureFrameBuffer() + targetKind, targetW, targetH, targetData = 0, w, h, data + else + return + end + + if targetData then + if targetW * targetH * 4 > MAX_PIXEL_BUFFER then + error("obj.pixelshader: target is too large for the native engine.", 2) + end + ffi.copy(shaderImage, targetData, targetW * targetH * 4) + end + + shaderMeta[0] = resourceCount + shaderMeta[1] = blendMode + shaderMeta[2] = compositeBlend + shaderMeta[3] = samplerMode + shaderMeta[4] = constantCount + shaderMeta[5] = targetW + shaderMeta[6] = targetH + shaderMeta[7] = targetKind + + local len = #name + if len > CB_TAG_MAX then len = CB_TAG_MAX end + ffi.copy(base + CB_TAG_OFFSET, name, len) + i32[OFF_CB_TAGLEN] = len + i32[OFF_CB_KIND] = CB_KIND_PIXELSHADER_RUN + i32[OFF_STATUS] = STATUS_CALLBACK + k32.SetEvent(doneEvent) + k32.WaitForSingleObject(workEvent, INFINITE) + + local status = shaderMeta[8] + if status == 2 then + error(ffi.string(base + CB_TAG_OFFSET, i32[OFF_CB_TAGLEN]), 2) + end + if status ~= 0 then return end + + if tk == "o" then + dirty = true + pixelsValid = true + pdValid = false + pdDirty = false + return + end + + ffi.copy(targetData, shaderImage, targetW * targetH * 4) + if tk == "f" then + local need = targetW * targetH * 4 + if need <= pixelCapacity then + ffi.copy(pixels, targetData, need) + width = targetW; height = targetH + i32[OFF_WIDTH] = targetW; i32[OFF_HEIGHT] = targetH + dirty = true + pixelsValid = true + pdValid = false + pdDirty = false + obj.w = targetW; obj.h = targetH + obj.hw = targetW / 2; obj.hh = targetH / 2 + obj.cx = targetW / 2; obj.cy = targetH / 2 + obj.cz = 0 + obj.diagonal = math.sqrt(targetW * targetW + targetH * targetH) + end + end end function obj.getobject(tag, frame) @@ -951,6 +1156,7 @@ while true do pixelsValid = false pdValid = false pdDirty = false + frameBufferValid = false ringD[0] = 0 local version = i32[OFF_SCRIPT_VERSION] if version ~= lastScriptVersion then