ファイル

1635 行
54 KiB
Lua

local ffi = require("ffi")
ffi.cdef [[
typedef void* HANDLE;
HANDLE OpenFileMappingA(unsigned long, int, const char*);
void* MapViewOfFile(HANDLE, unsigned long, unsigned long, unsigned long, size_t);
int UnmapViewOfFile(const void*);
int CloseHandle(HANDLE);
HANDLE OpenEventA(unsigned long, int, const char*);
unsigned long WaitForSingleObject(HANDLE, unsigned long);
int SetEvent(HANDLE);
unsigned long GetLastError();
int QueryPerformanceCounter(int64_t*);
int QueryPerformanceFrequency(int64_t*);
]]
local k32 = ffi.load("kernel32")
local FILE_MAP_ALL_ACCESS = 0x000F001F
local EVENT_ALL_ACCESS = 0x1F0003
local INFINITE = 0xFFFFFFFF
local mapName = arg[1]
local mapSize = tonumber(arg[2])
local workEventName = arg[3]
local doneEventName = arg[4]
local shimPath = arg[5]
local stringParamsCap = tonumber(arg[6])
local scriptPath = arg[7] or ""
local hMap = k32.OpenFileMappingA(FILE_MAP_ALL_ACCESS, 0, mapName)
assert(hMap ~= nil, "OpenFileMapping failed")
local view = k32.MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, mapSize)
assert(view ~= nil, "MapViewOfFile failed")
local workEvent = k32.OpenEventA(EVENT_ALL_ACCESS, 0, workEventName)
local doneEvent = k32.OpenEventA(EVENT_ALL_ACCESS, 0, doneEventName)
assert(workEvent ~= nil and doneEvent ~= nil, "OpenEvent failed")
local qpcTmp = ffi.new("int64_t[1]")
k32.QueryPerformanceFrequency(qpcTmp)
local qpcFreq = tonumber(qpcTmp[0])
local function qpcNow()
k32.QueryPerformanceCounter(qpcTmp)
return qpcTmp[0]
end
local qpcStart = qpcNow()
local qpcRunStart = qpcStart
local base = ffi.cast("uint8_t*", view)
local i32 = ffi.cast("int32_t*", base)
local f64 = ffi.cast("double*", base + 64)
local OFF_COMMAND = 1
local OFF_STATUS = 2
local OFF_WIDTH = 3
local OFF_HEIGHT = 4
local OFF_SCRIPTLEN = 5
local OFF_PIXELSDIRTY = 6
local OFF_ERRORLEN = 7
local OFF_CB_FRAME = 8
local OFF_CB_TAGLEN = 9
local OFF_CB_FOUND = 10
local OFF_CB_KIND = 11
local OFF_LOAD_RESULT_W = 12
local OFF_LOAD_RESULT_H = 13
local OFF_STRING_PARAMS_LEN = 14
local OFF_SCRIPT_VERSION = 15
local SCRIPT_OFFSET = 64 + 67 * 8
local ERROR_OFFSET = SCRIPT_OFFSET + 128 * 1024
local CB_TAG_OFFSET = ERROR_OFFSET + 4 * 1024
local CB_TAG_MAX = 8192
local CB_RESULT_OFFSET = CB_TAG_OFFSET + CB_TAG_MAX
local STRING_PARAMS_OFFSET = CB_RESULT_OFFSET + 10 * 8
local DRAW_RING_CAPACITY = 4096
local DRAW_ENTRY_DOUBLES = 24
local DRAW_RING_DOUBLES = 1 + DRAW_RING_CAPACITY * DRAW_ENTRY_DOUBLES
local DRAW_RING_BYTES = DRAW_RING_DOUBLES * 8
local DRAW_RING_OFFSET = STRING_PARAMS_OFFSET + stringParamsCap
local PIXEL_OFFSET = DRAW_RING_OFFSET + DRAW_RING_BYTES
local FIRST_WRITABLE = 8
local LAST_WRITABLE = 25
local STATUS_CALLBACK = 3
local CB_KIND_GETOBJECT = 0
local CB_KIND_LOADFIGURE = 1
local CB_KIND_EFFECT = 2
local CB_KIND_DRAW = 3
local CB_KIND_DRAWPOLY = 4
local CB_KIND_LOADTEXT = 5
local CB_KIND_LOADIMAGE = 6
local CB_KIND_LOADMOVIE = 7
local CB_KIND_SETANCHOR = 8
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 CB_KIND_LOADSCENE = 15
local CB_KIND_LOADBRUSH = 16
local CB_KIND_BRUSH = 17
local CB_KIND_GETAUDIO = 18
local CB_KIND_PIXELPROCESS = 19
local PIXEL_PROCESS_FILL = 0
local PIXEL_PROCESS_CONVOLVE = 1
local PIXEL_PROCESS_RESIZE = 2
local PIXEL_PROCESS_FILL_FULL_THRESHOLD = 1048576
local PIXEL_PROCESS_FILL_PARTIAL_THRESHOLD = 1048576
local PIXEL_PROCESS_RESIZE_THRESHOLD = 262144
local PIXEL_PROCESS_CONVOLVE_WORK_THRESHOLD = 262144
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))()
local clamp = function(v, lo, hi)
if v < lo then return lo end
if v > hi then return hi end
return v
end
local width, height = 0, 0
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 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 = {}
local dirty = false
local pixelsValid = false
local drawTarget = "frame"
local pdData, pdCapacity, pdValid, pdDirty = nil, 0, false, false
local convSrcData, convSrcCap = nil, 0
local resizeSrcData, resizeSrcCap = nil, 0
local pixelProcessAvailable = true
local function ensurePixels()
if pixelsValid then return end
i32[OFF_CB_KIND] = CB_KIND_REQUESTPIXELS
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
pixelsValid = true
end
local function flushDraws()
i32[OFF_CB_KIND] = CB_KIND_FLUSHDRAWS
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
end
local function tryPixelProcess(kind, force)
if not pixelProcessAvailable then return false end
cbResult[0] = kind
cbResult[9] = force and 1 or 0
i32[OFF_CB_FOUND] = 0
i32[OFF_CB_KIND] = CB_KIND_PIXELPROCESS
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
local found = i32[OFF_CB_FOUND]
if found < 0 then pixelProcessAvailable = false end
return found == 1
end
local function ringSlot(kind)
local n = ringD[0]
if n >= DRAW_RING_CAPACITY then
flushDraws()
n = 0
end
local eb = 1 + n * DRAW_ENTRY_DOUBLES
ringD[eb] = kind
ringD[0] = n + 1
return eb
end
local function rebuildPixelData()
local count = width * height * 4
if not pdData or pdCapacity < count then
pdData = ffi.new("double[?]", count)
pdCapacity = count
end
for pi = 0, width * height - 1 do
local p = pixels + pi * 4
local di = pi * 4
local a = p[3]
pdData[di + 3] = a
if a <= 0 then
pdData[di] = 0; pdData[di + 1] = 0; pdData[di + 2] = 0
else
local s = 255 / a
pdData[di] = clamp(p[2] * s, 0, 255)
pdData[di + 1] = clamp(p[1] * s, 0, 255)
pdData[di + 2] = clamp(p[0] * s, 0, 255)
end
end
pdValid = true
pdDirty = false
end
local function flushPixelData()
if not pdDirty then return end
for pi = 0, width * height - 1 do
local di = pi * 4
local a = math.floor(clamp(pdData[di + 3], 0, 255))
local aK = a / 255
local p = pixels + pi * 4
p[0] = math.floor(clamp(pdData[di + 2] * aK, 0, 255))
p[1] = math.floor(clamp(pdData[di + 1] * aK, 0, 255))
p[2] = math.floor(clamp(pdData[di] * aK, 0, 255))
p[3] = a
end
dirty = true
pdDirty = false
end
local function sampleSource(src, srcW, srcH, u, v, linear)
if not linear then
local su = clamp(math.floor(u), 0, srcW - 1)
local sv = clamp(math.floor(v), 0, srcH - 1)
local si = (sv * srcW + su) * 4
return src[si], src[si + 1], src[si + 2], src[si + 3]
end
local fx = u - 0.5
local fy = v - 0.5
local x0 = math.floor(fx)
local y0 = math.floor(fy)
local tx = fx - x0
local ty = fy - y0
local b, g, r, a = 0, 0, 0, 0
for j = 0, 1 do
local sy = clamp(y0 + j, 0, srcH - 1)
local wy = (j == 0) and (1 - ty) or ty
for i = 0, 1 do
local sx = clamp(x0 + i, 0, srcW - 1)
local w = wy * ((i == 0) and (1 - tx) or tx)
local si = (sy * srcW + sx) * 4
b = b + src[si] * w
g = g + src[si + 1] * w
r = r + src[si + 2] * w
a = a + src[si + 3] * w
end
end
return b, g, r, a
end
local function compositeOver(dst, di, sb, sg, sr, sa)
local inv = 1 - sa / 255
dst[di] = math.floor(clamp(sb + dst[di] * inv, 0, 255))
dst[di + 1] = math.floor(clamp(sg + dst[di + 1] * inv, 0, 255))
dst[di + 2] = math.floor(clamp(sr + dst[di + 2] * inv, 0, 255))
dst[di + 3] = math.floor(clamp(sa + dst[di + 3] * inv, 0, 255))
end
local function compositeDrawInto(dst, dstW, dstH, src, srcW, srcH, ox, oy, zoom, aspect, alpha, linear)
if srcW <= 0 or srcH <= 0 or dstW <= 0 or dstH <= 0 then return end
local ca = clamp(aspect, -1, 1)
local zx = zoom * (1 + ca)
local zy = zoom * (1 - ca)
if zx == 0 or zy == 0 then return end
local ka = clamp(alpha, 0, 1)
if ka <= 0 then return end
local halfW = srcW * 0.5
local halfH = srcH * 0.5
local spanX = math.abs(halfW * zx)
local spanY = math.abs(halfH * zy)
local x0 = math.max(0, math.floor(ox - spanX))
local x1 = math.min(dstW - 1, math.ceil(ox + spanX))
local y0 = math.max(0, math.floor(oy - spanY))
local y1 = math.min(dstH - 1, math.ceil(oy + spanY))
for py = y0, y1 do
for px = x0, x1 do
local u = (px + 0.5 - ox) / zx + halfW
local v = (py + 0.5 - oy) / zy + halfH
if u >= 0 and u < srcW and v >= 0 and v < srcH then
local sb, sg, sr, sa = sampleSource(src, srcW, srcH, u, v, linear)
compositeOver(dst, (py * dstW + px) * 4, sb * ka, sg * ka, sr * ka, sa * ka)
end
end
end
end
local function compositeDrawPolyInto(dst, dstW, dstH, src, srcW, srcH, poly, alpha, linear)
if srcW <= 0 or srcH <= 0 or dstW <= 0 or dstH <= 0 then return end
local x0p, y0p = poly[0], poly[1]
local x1p, y1p = poly[3], poly[4]
local x3p, y3p = poly[9], poly[10]
local u0, v0 = poly[12], poly[13]
local u1, v1 = poly[14], poly[15]
local u3, v3 = poly[18], poly[19]
local det = u0 * (v1 - v3) - v0 * (u1 - u3) + (u1 * v3 - v1 * u3)
if math.abs(det) < 1e-9 then return end
local inv = 1 / det
local a = (x0p * (v1 - v3) - v0 * (x1p - x3p) + (x1p * v3 - v1 * x3p)) * inv
local b = (u0 * (x1p - x3p) - x0p * (u1 - u3) + (u1 * x3p - x1p * u3)) * inv
local c = (u0 * (v1 * x3p - x1p * v3) - v0 * (u1 * x3p - x1p * u3) + x0p * (u1 * v3 - v1 * u3)) * inv
local d = (y0p * (v1 - v3) - v0 * (y1p - y3p) + (y1p * v3 - v1 * y3p)) * inv
local e = (u0 * (y1p - y3p) - y0p * (u1 - u3) + (u1 * y3p - y1p * u3)) * inv
local f = (u0 * (v1 * y3p - y1p * v3) - v0 * (u1 * y3p - y1p * u3) + y0p * (u1 * v3 - v1 * u3)) * inv
local adet = a * e - b * d
if math.abs(adet) < 1e-9 then return end
local ka = clamp(alpha, 0, 1)
if ka <= 0 then return end
local minX, minY, maxX, maxY = poly[0], poly[1], poly[0], poly[1]
for i = 0, 3 do
local xx = poly[i * 3]
local yy = poly[i * 3 + 1]
if xx < minX then minX = xx end
if xx > maxX then maxX = xx end
if yy < minY then minY = yy end
if yy > maxY then maxY = yy end
end
local bx0 = math.max(0, math.floor(minX))
local bx1 = math.min(dstW - 1, math.ceil(maxX))
local by0 = math.max(0, math.floor(minY))
local by1 = math.min(dstH - 1, math.ceil(maxY))
for py = by0, by1 do
for px = bx0, bx1 do
local dx = (px + 0.5) - c
local dy = (py + 0.5) - f
local u = (e * dx - b * dy) / adet
local v = (-d * dx + a * dy) / adet
if u >= 0 and u < srcW and v >= 0 and v < srcH then
local sb, sg, sr, sa = sampleSource(src, srcW, srcH, u, v, linear)
compositeOver(dst, (py * dstW + px) * 4, sb * ka, sg * ka, sr * ka, sa * ka)
end
end
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
local n = width * height * 4
t = { data = ffi.new("uint8_t[?]", n), w = width, h = height }
buffers["t"] = t
end
return t
end
local function bufferKind(id)
local c = id:match("^%s*(.)")
if not c then return nil end
c = c:lower()
if c == "c" then
local name = id:match(":%s*(.-)%s*$")
return "c", name and ("c:" .. name) or "c"
end
return c, c
end
local sandbox
local obj = {}
local scene = {}
local ymm4 = {}
function obj.getpixel(x, y)
x = math.floor(x); y = math.floor(y)
if x < 0 or y < 0 or x >= width or y >= height then return 0, 0, 0, 0 end
ensurePixels()
flushPixelData()
local p = pixels + (y * width + x) * 4
local a = p[3]
if a <= 0 then return 0, 0, 0, 0 end
local s = 255 / a
return clamp(p[2] * s, 0, 255), clamp(p[1] * s, 0, 255), clamp(p[0] * s, 0, 255), a
end
function obj.setpixel(x, y, r, g, b, a)
x = math.floor(x); y = math.floor(y)
if x < 0 or y < 0 or x >= width or y >= height then return end
ensurePixels()
flushPixelData()
a = a or 255
dirty = true
local aK = clamp(a, 0, 255) / 255
local p = pixels + (y * width + x) * 4
p[0] = math.floor(clamp(b * aK, 0, 255))
p[1] = math.floor(clamp(g * aK, 0, 255))
p[2] = math.floor(clamp(r * aK, 0, 255))
p[3] = math.floor(clamp(a, 0, 255))
pdValid = false
end
function obj.getpixeldata()
ensurePixels()
if not pdValid then rebuildPixelData() end
local data = pdData
local total = width * height * 4
local pd = { width = width, height = height }
function pd:get(index)
local zb = index - 1
if zb < 0 or zb >= total then return 0 end
return data[zb]
end
function pd:set(index, value)
local zb = index - 1
if zb < 0 or zb >= total then return end
data[zb] = clamp(value, 0, 255)
pdDirty = true
end
return pd
end
function obj.putpixeldata() flushPixelData() end
function obj.rand(a, b, seed, frame)
return aviutl_rand(a or 0, b or 0, seed or 0, frame or obj.frame or 0)
end
local font = { family = "", size = 34, bold = false, italic = false, color = 0xFFFFFF }
function obj.setfont(name, size, style, col1)
local nameType = type(name)
if nameType == "string" then
font.family = name
elseif nameType == "number" then
font.family = tostring(name)
end
local sizeNumber = tonumber(size)
if sizeNumber ~= nil then font.size = sizeNumber end
local styleNumber = tonumber(style)
if styleNumber ~= nil then
local flags = math.floor(styleNumber)
font.bold = (flags % 2) == 1
font.italic = (math.floor(flags / 2) % 2) == 1
end
local colorNumber = tonumber(col1)
if colorNumber ~= nil then font.color = colorNumber end
end
local function applyLoadResult()
local newW = i32[OFF_LOAD_RESULT_W]
local newH = i32[OFF_LOAD_RESULT_H]
if newW > 0 and newH > 0 then
width = newW
height = newH
i32[OFF_WIDTH] = newW
i32[OFF_HEIGHT] = newH
dirty = true
pixelsValid = true
pdValid = false
pdDirty = false
obj.w = newW; obj.h = newH
obj.hw = newW / 2; obj.hh = newH / 2
obj.cx = newW / 2; obj.cy = newH / 2
obj.cz = 0
obj.diagonal = math.sqrt(newW * newW + newH * newH)
end
end
local function loadFigure(name, color, size, lineWidth, aspect)
local nameBytes = tostring(name or "")
local len = #nameBytes
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, nameBytes, len)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_KIND] = CB_KIND_LOADFIGURE
cbResult[0] = color or 0
cbResult[1] = size or 100
cbResult[2] = lineWidth or 0
cbResult[3] = aspect or 0
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
applyLoadResult()
end
local function loadText(str)
local payload = font.family .. "\0" .. tostring(str or "")
local len = #payload
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, payload, len)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_KIND] = CB_KIND_LOADTEXT
cbResult[0] = font.size
cbResult[1] = font.bold and 1 or 0
cbResult[2] = font.italic and 1 or 0
cbResult[3] = font.color
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
applyLoadResult()
end
local function loadImage(path)
local payload = tostring(path or "")
local len = #payload
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, payload, len)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_KIND] = CB_KIND_LOADIMAGE
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
applyLoadResult()
end
local function loadMovie(path, time)
local payload = tostring(path or "")
local len = #payload
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, payload, len)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_KIND] = CB_KIND_LOADMOVIE
cbResult[0] = time or obj.time or 0
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
applyLoadResult()
end
local function loadScene(name, time)
local payload = tostring(name or "")
local len = #payload
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, payload, len)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_KIND] = CB_KIND_LOADSCENE
cbResult[0] = time or obj.time or 0
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
applyLoadResult()
end
local function loadBrush(name, w, h)
local payload = tostring(name or "")
local len = #payload
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, payload, len)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_KIND] = CB_KIND_LOADBRUSH
cbResult[0] = w or width
cbResult[1] = h or height
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
applyLoadResult()
end
local function writeNamedArgs(name, args, first, n)
local parts = { name }
local pos = #name + 1
for i = first, n - 1, 2 do
local k = tostring(args[i])
local v = args[i + 1]
local encoded
local vt = type(v)
if vt == "number" then
encoded = "n" .. string.format("%.17g", v)
elseif vt == "boolean" then
encoded = v and "b1" or "b0"
else
encoded = "s" .. tostring(v)
end
local needed = 2 + #k + #encoded
if pos + needed > CB_TAG_MAX then break end
parts[#parts + 1] = k
parts[#parts + 1] = encoded
pos = pos + needed
end
local payload = table.concat(parts, "\0")
local len = #payload
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, payload, len)
return len
end
function obj.load(kind, ...)
if kind == "figure" then
loadFigure(...)
elseif kind == "text" then
loadText(...)
elseif kind == "image" then
loadImage(...)
elseif kind == "movie" then
loadMovie(...)
elseif kind == "scene" then
loadScene(...)
elseif kind == "brush" then
loadBrush(...)
end
end
function obj.effect(name, ...)
if type(name) ~= "string" then return end
local args = { ... }
local n = select("#", ...)
local len = writeNamedArgs(name, args, 1, n)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_KIND] = CB_KIND_EFFECT
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
end
function obj.brush(name, ...)
if type(name) ~= "string" then return end
local args = { ... }
local n = select("#", ...)
local first = 1
local w, h
if n >= 2 and type(args[1]) == "number" and type(args[2]) == "number" then
w = args[1]
h = args[2]
first = 3
end
local len = writeNamedArgs(name, args, first, n)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_KIND] = CB_KIND_BRUSH
cbResult[0] = w or width
cbResult[1] = h or height
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
applyLoadResult()
end
function obj.draw(ox, oy, oz, zoom, alpha, aspect)
if drawTarget == "temp" then
ensurePixels()
flushPixelData()
local t = ensureTemp()
compositeDrawInto(t.data, t.w, t.h, pixels, width, height,
ox or 0, oy or 0, zoom or 1, aspect or 0, alpha or 1, (options.antialias or 1) ~= 0)
return
end
local eb = ringSlot(CB_KIND_DRAW)
ringD[eb + 1] = ox or 0
ringD[eb + 2] = oy or 0
ringD[eb + 3] = oz or 0
ringD[eb + 4] = zoom or 1
ringD[eb + 5] = alpha or 1
ringD[eb + 6] = aspect or 0
ringD[eb + 7] = options.antialias or 1
ringD[eb + 8] = options.blend or 0
end
function obj.drawpoly(...)
local n = select("#", ...)
if n < 12 then return end
local a = { ... }
local p = {}
for i = 0, 11 do p[i] = a[i + 1] or 0 end
if n >= 20 then
for i = 0, 7 do p[12 + i] = a[13 + i] or 0 end
else
p[12] = 0; p[13] = 0
p[14] = width; p[15] = 0
p[16] = width; p[17] = height
p[18] = 0; p[19] = height
end
local alpha = 1
if n == 13 then
alpha = a[13] or 1
elseif n >= 21 then
alpha = a[21] or 1
end
p[20] = alpha
if drawTarget == "temp" then
ensurePixels()
flushPixelData()
local t = ensureTemp()
compositeDrawPolyInto(t.data, t.w, t.h, pixels, width, height, p, alpha, (options.antialias or 1) ~= 0)
return
end
local eb = ringSlot(CB_KIND_DRAWPOLY)
for i = 0, 20 do ringD[eb + 1 + i] = p[i] end
ringD[eb + 22] = options.antialias or 1
ringD[eb + 23] = options.blend or 0
end
function obj.getvalue(target)
if type(target) ~= "string" then return 0 end
local v = obj[target]
if type(v) == "number" then return v end
return 0
end
function obj.getaudio(buf, file, datatype, size)
if type(file) ~= "string" then return 0, 0 end
local payload = file .. "\0" .. tostring(datatype or "pcm")
local len = #payload
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, payload, len)
i32[OFF_CB_TAGLEN] = len
cbResult[0] = tonumber(size) or 0
i32[OFF_CB_KIND] = CB_KIND_GETAUDIO
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
local count = math.floor(cbResult[0])
local rate = math.floor(cbResult[1])
local out = buf
if type(out) ~= "table" then
if buf ~= nil then return count, rate end
out = {}
end
for i = 0, count - 1 do out[i + 1] = cbTagD[i] end
if buf == nil then return count, rate, out end
return count, rate
end
function obj.getinfo(name)
if name == "script_path" then return scriptPath end
if name == "filter" then return true end
if name == "saving" then return ymm4.is_saving end
if name == "image_max" then return scene.width, scene.height end
if name == "bpm" then return f64[63], f64[64], f64[65] end
if name == "clock" then return tonumber(qpcNow() - qpcStart) / qpcFreq end
if name == "script_time" then return tonumber(qpcNow() - qpcRunStart) / qpcFreq * 1000 end
if name == "version" then return f64[66] end
return nil
end
function obj.setoption(name, value, a, b)
if type(name) ~= "string" then return end
if value == nil then value = true end
options[name] = value
if name == "drawtarget" then
if value == "tempbuffer" then
drawTarget = "temp"
if a ~= nil and b ~= nil then
local w = math.max(1, math.floor(a))
local h = math.max(1, math.floor(b))
buffers["t"] = { data = ffi.new("uint8_t[?]", w * h * 4), w = w, h = h }
end
else
drawTarget = "frame"
end
end
end
function obj.getoption(name)
if type(name) ~= "string" then return 0 end
local v = options[name]
if v == nil then return 0 end
return v
end
function obj.pixeloption(name, value)
if type(name) ~= "string" then return end
if value == nil then value = true end
pixeloptions[name] = value
end
function obj.setanchor(name, count, ...)
if type(name) ~= "string" then return 0 end
count = math.floor(count or 0)
if count < 0 then count = 0 elseif count > 32 then count = 32 end
if name == "track" and count == 0 then count = 1 end
local connection, is3d = 0, false
local optCount = select("#", ...)
for i = 1, optCount do
local o = select(i, ...)
if o == "line" then connection = 1
elseif o == "loop" then connection = 2
elseif o == "star" then connection = 3
elseif o == "arm" then connection = 4
elseif o == "xyz" then is3d = true end
end
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
cbResult[0] = count
cbResult[1] = is3d and 1 or 0
cbResult[2] = connection
i32[OFF_CB_KIND] = CB_KIND_SETANCHOR
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
local stride = is3d and 3 or 2
local pos = {}
for i = 0, count - 1 do
pos[i * stride + 1] = cbTagD[i * stride + 0]
pos[i * stride + 2] = cbTagD[i * stride + 1]
if is3d then pos[i * stride + 3] = cbTagD[i * stride + 2] end
end
sandbox[name] = pos
return count
end
function obj.copybuffer(dst, src)
if type(dst) ~= "string" or type(src) ~= "string" then return end
local sk, skey = bufferKind(src)
local data, w, h
if sk == "o" then
ensurePixels()
flushPixelData()
data = pixels
w = width; h = height
elseif sk == "t" or sk == "c" then
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
local dk, dkey = bufferKind(dst)
if dk == "o" then
local need = w * h * 4
if need > pixelCapacity then return end
ffi.copy(pixels, data, need)
width = w; height = h
i32[OFF_WIDTH] = w; i32[OFF_HEIGHT] = h
dirty = true
pixelsValid = true
pdValid = false
pdDirty = false
obj.w = w; obj.h = h
obj.hw = w / 2; obj.hh = h / 2
obj.cx = w / 2; obj.cy = h / 2
obj.cz = 0
obj.diagonal = math.sqrt(w * w + h * h)
elseif dk == "t" or dk == "c" then
local n = w * h * 4
local arr = ffi.new("uint8_t[?]", n)
ffi.copy(arr, data, n)
buffers[dkey] = { data = arr, w = w, h = h }
end
end
local function fillImpl(force, r, g, b, a, x, y, w, h)
ensurePixels()
flushPixelData()
r = r or 0; g = g or 0; b = b or 0; a = a or 255
x = math.floor(x or 0); y = math.floor(y or 0)
w = math.floor(w or width); h = math.floor(h or height)
local x0 = clamp(x, 0, width)
local y0 = clamp(y, 0, height)
local x1 = clamp(x + w, 0, width)
local y1 = clamp(y + h, 0, height)
if x1 <= x0 or y1 <= y0 then return end
local imageArea = width * height
local fillArea = (x1 - x0) * (y1 - y0)
local fullFill = x0 == 0 and y0 == 0 and x1 == width and y1 == height
local usePixelProcess = force
if not usePixelProcess then
if fullFill then
usePixelProcess = imageArea >= PIXEL_PROCESS_FILL_FULL_THRESHOLD
else
usePixelProcess = imageArea >= PIXEL_PROCESS_FILL_PARTIAL_THRESHOLD and fillArea * 4 >= imageArea * 3
end
end
cbResult[1] = r
cbResult[2] = g
cbResult[3] = b
cbResult[4] = a
cbResult[5] = x0
cbResult[6] = y0
cbResult[7] = x1 - x0
cbResult[8] = y1 - y0
if usePixelProcess and tryPixelProcess(PIXEL_PROCESS_FILL, force) then
dirty = true
pdValid = false
pdDirty = false
return
end
local aK = clamp(a, 0, 255) / 255
local pb = math.floor(clamp(b * aK, 0, 255))
local pg = math.floor(clamp(g * aK, 0, 255))
local pr = math.floor(clamp(r * aK, 0, 255))
local pa = math.floor(clamp(a, 0, 255))
for py = y0, y1 - 1 do
for px = x0, x1 - 1 do
local p = pixels + (py * width + px) * 4
p[0] = pb; p[1] = pg; p[2] = pr; p[3] = pa
end
end
dirty = true
pdValid = false
end
function obj.fill(r, g, b, a, x, y, w, h) fillImpl(false, r, g, b, a, x, y, w, h) end
function __fast_fill(r, g, b, a, x, y, w, h) fillImpl(true, r, g, b, a, x, y, w, h) end
function obj.getpixelregion(x, y, w, h)
x = math.floor(x or 0); y = math.floor(y or 0)
w = math.floor(w or 0); h = math.floor(h or 0)
local out = {}
if w <= 0 or h <= 0 then return out end
ensurePixels()
flushPixelData()
for j = 0, h - 1 do
local sy = y + j
for i = 0, w - 1 do
local sx = x + i
local di = (j * w + i) * 4
if sx < 0 or sy < 0 or sx >= width or sy >= height then
out[di + 1] = 0; out[di + 2] = 0; out[di + 3] = 0; out[di + 4] = 0
else
local p = pixels + (sy * width + sx) * 4
local a = p[3]
if a <= 0 then
out[di + 1] = 0; out[di + 2] = 0; out[di + 3] = 0; out[di + 4] = 0
else
local s = 255 / a
out[di + 1] = clamp(p[2] * s, 0, 255)
out[di + 2] = clamp(p[1] * s, 0, 255)
out[di + 3] = clamp(p[0] * s, 0, 255)
out[di + 4] = a
end
end
end
end
return out
end
function obj.putpixelregion(x, y, w, h, data)
if type(data) ~= "table" then return end
x = math.floor(x or 0); y = math.floor(y or 0)
w = math.floor(w or 0); h = math.floor(h or 0)
if w <= 0 or h <= 0 then return end
ensurePixels()
flushPixelData()
for j = 0, h - 1 do
local dy = y + j
if dy >= 0 and dy < height then
for i = 0, w - 1 do
local dx = x + i
if dx >= 0 and dx < width then
local si = (j * w + i) * 4
local a = clamp(data[si + 4] or 0, 0, 255)
local aK = a / 255
local p = pixels + (dy * width + dx) * 4
p[0] = math.floor(clamp((data[si + 3] or 0) * aK, 0, 255))
p[1] = math.floor(clamp((data[si + 2] or 0) * aK, 0, 255))
p[2] = math.floor(clamp((data[si + 1] or 0) * aK, 0, 255))
p[3] = math.floor(a)
end
end
end
end
dirty = true
pdValid = false
end
local function convolveImpl(force, kernel, size, divisor, offset)
if type(kernel) ~= "table" then return end
size = math.floor(size or 0)
if size < 1 or size % 2 == 0 then return end
ensurePixels()
flushPixelData()
local taps = size * size
local k = {}
local sum = 0
for i = 1, taps do
local v = tonumber(kernel[i]) or 0
k[i] = v
sum = sum + v
end
local inv = divisor or sum
if inv == 0 then inv = 1 end
offset = offset or 0
if taps * 8 <= CB_TAG_MAX and (force or width * height * taps >= PIXEL_PROCESS_CONVOLVE_WORK_THRESHOLD) then
cbResult[1] = size
cbResult[2] = inv
cbResult[3] = offset
for i = 1, taps do
cbTagD[i - 1] = k[i]
end
i32[OFF_CB_TAGLEN] = taps * 8
if tryPixelProcess(PIXEL_PROCESS_CONVOLVE, force) then
dirty = true
pdValid = false
pdDirty = false
return
end
end
local total = width * height * 4
if not convSrcData or convSrcCap < total then
convSrcData = ffi.new("double[?]", total)
convSrcCap = total
end
local src = convSrcData
for pi = 0, width * height - 1 do
local p = pixels + pi * 4
local di = pi * 4
local a = p[3]
src[di + 3] = a
if a <= 0 then
src[di] = 0; src[di + 1] = 0; src[di + 2] = 0
else
local s = 255 / a
src[di] = clamp(p[2] * s, 0, 255)
src[di + 1] = clamp(p[1] * s, 0, 255)
src[di + 2] = clamp(p[0] * s, 0, 255)
end
end
local half = math.floor(size / 2)
for y = 0, height - 1 do
for x = 0, width - 1 do
local sr, sg, sb, sa = 0, 0, 0, 0
for ky = 0, size - 1 do
local sy = clamp(y + ky - half, 0, height - 1)
for kx = 0, size - 1 do
local sx = clamp(x + kx - half, 0, width - 1)
local kv = k[ky * size + kx + 1]
local si = (sy * width + sx) * 4
sr = sr + src[si] * kv
sg = sg + src[si + 1] * kv
sb = sb + src[si + 2] * kv
sa = sa + src[si + 3] * kv
end
end
local rr = clamp(sr / inv + offset, 0, 255)
local gg = clamp(sg / inv + offset, 0, 255)
local bb = clamp(sb / inv + offset, 0, 255)
local aa = clamp(sa / inv + offset, 0, 255)
local aK = aa / 255
local p = pixels + (y * width + x) * 4
p[0] = math.floor(clamp(bb * aK, 0, 255))
p[1] = math.floor(clamp(gg * aK, 0, 255))
p[2] = math.floor(clamp(rr * aK, 0, 255))
p[3] = math.floor(aa)
end
end
dirty = true
pdValid = false
end
function obj.convolve(kernel, size, divisor, offset) convolveImpl(false, kernel, size, divisor, offset) end
function __fast_convolve(kernel, size, divisor, offset) convolveImpl(true, kernel, size, divisor, offset) end
local function resizeImpl(force, newW, newH, mode)
newW = math.floor(newW or 0); newH = math.floor(newH or 0)
if newW < 1 then newW = 1 end
if newH < 1 then newH = 1 end
local dstCount = newW * newH * 4
if dstCount > pixelCapacity then return end
ensurePixels()
flushPixelData()
local cw, ch = width, height
local linear = mode ~= "nearest"
cbResult[1] = newW
cbResult[2] = newH
cbResult[3] = linear and 1 or 0
if (force or newW * newH >= PIXEL_PROCESS_RESIZE_THRESHOLD) and tryPixelProcess(PIXEL_PROCESS_RESIZE, force) then
width = newW; height = newH
i32[OFF_WIDTH] = newW; i32[OFF_HEIGHT] = newH
dirty = true
pixelsValid = true
pdValid = false
pdDirty = false
obj.w = newW; obj.h = newH
obj.hw = newW / 2; obj.hh = newH / 2
obj.cx = newW / 2; obj.cy = newH / 2
obj.cz = 0
obj.diagonal = math.sqrt(newW * newW + newH * newH)
return
end
local srcCount = cw * ch * 4
if not resizeSrcData or resizeSrcCap < srcCount then
resizeSrcData = ffi.new("uint8_t[?]", srcCount)
resizeSrcCap = srcCount
end
ffi.copy(resizeSrcData, pixels, srcCount)
local src = resizeSrcData
for y = 0, newH - 1 do
for x = 0, newW - 1 do
local o = pixels + (y * newW + x) * 4
if not linear then
local sx = clamp(math.floor((x + 0.5) * cw / newW), 0, cw - 1)
local sy = clamp(math.floor((y + 0.5) * ch / newH), 0, ch - 1)
local p = src + (sy * cw + sx) * 4
o[0] = p[0]; o[1] = p[1]; o[2] = p[2]; o[3] = p[3]
else
local u = (x + 0.5) * cw / newW - 0.5
local v = (y + 0.5) * ch / newH - 0.5
local x0 = math.floor(u)
local y0 = math.floor(v)
local tx = u - x0
local ty = v - y0
for c = 0, 3 do
local acc = 0
for j = 0, 1 do
local sy = clamp(y0 + j, 0, ch - 1)
local wy = (j == 0) and (1 - ty) or ty
for i = 0, 1 do
local sx = clamp(x0 + i, 0, cw - 1)
local wx = (i == 0) and (1 - tx) or tx
acc = acc + src[(sy * cw + sx) * 4 + c] * wy * wx
end
end
o[c] = math.floor(clamp(acc, 0, 255))
end
end
end
end
width = newW; height = newH
i32[OFF_WIDTH] = newW; i32[OFF_HEIGHT] = newH
dirty = true
pixelsValid = true
pdValid = false
pdDirty = false
obj.w = newW; obj.h = newH
obj.hw = newW / 2; obj.hh = newH / 2
obj.cx = newW / 2; obj.cy = newH / 2
obj.cz = 0
obj.diagonal = math.sqrt(newW * newW + newH * newH)
end
function obj.resize(newW, newH, mode) resizeImpl(false, newW, newH, mode) end
function __fast_resize(newW, newH, mode) resizeImpl(true, newW, newH, mode) end
local SCENE_VALUE_MAX = 4095
local SCENE_NIL = {}
local sceneReads = {}
local sceneWrites = {}
local function sceneCut(s, max)
if #s <= max then return s end
local len = max
while len > 0 do
local b = string.byte(s, len + 1)
if b < 0x80 or b >= 0xC0 then break end
len = len - 1
end
return string.sub(s, 1, len)
end
function scene.get(name)
if type(name) ~= "string" then return nil end
name = sceneCut(name, SCENE_VALUE_MAX)
local written = sceneWrites[name]
if written ~= nil then
if written == SCENE_NIL then return nil end
return written
end
local cached = sceneReads[name]
if cached ~= nil then
if cached == SCENE_NIL then return nil end
return cached
end
local len = #name
ffi.copy(base + CB_TAG_OFFSET, name, len)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_KIND] = CB_KIND_SCENEGET
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
local kind = cbResult[0]
local value
if kind == 1 then
value = cbResult[1]
elseif kind == 2 then
value = ffi.string(base + CB_TAG_OFFSET, i32[OFF_CB_TAGLEN])
elseif kind == 3 then
value = cbResult[1] ~= 0
end
if value == nil then
sceneReads[name] = SCENE_NIL
else
sceneReads[name] = value
end
return value
end
function scene.set(name, value)
if type(name) ~= "string" then return end
name = sceneCut(name, SCENE_VALUE_MAX)
local vt = type(value)
local kind, num
if vt == "number" then
kind, num = 1, value
elseif vt == "string" then
kind, num = 2, 0
value = sceneCut(value, SCENE_VALUE_MAX)
elseif vt == "boolean" then
kind, num = 3, value and 1 or 0
elseif vt == "nil" then
kind, num = 0, 0
else
return
end
if value == nil then
sceneWrites[name] = SCENE_NIL
else
sceneWrites[name] = value
end
local len = #name
ffi.copy(base + CB_TAG_OFFSET, name, len)
if kind == 2 then
base[CB_TAG_OFFSET + len] = 0
local vlen = #value
if vlen > 0 then ffi.copy(base + CB_TAG_OFFSET + len + 1, value, vlen) end
i32[OFF_CB_TAGLEN] = len + 1 + vlen
else
i32[OFF_CB_TAGLEN] = len
end
cbResult[0] = kind
cbResult[1] = num
i32[OFF_CB_KIND] = CB_KIND_SCENESET
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
end
local cacheTag, cacheFrame, cacheFound
local cacheExist, cacheX, cacheY, cacheZ, cacheZoom, cacheRz, cacheAlpha, cacheLayer
local cacheVolume, cacheLength, cacheCharacter, cacheText, cacheKind
local function buildObject()
return {
exist = cacheExist,
x = cacheX, y = cacheY, z = cacheZ,
zoom = cacheZoom, sx = cacheZoom, sy = cacheZoom,
rx = 0, ry = 0, rz = cacheRz,
rxr = 0, ryr = 0, rzr = cacheRz * math.pi / 180,
alpha = cacheAlpha, layer = cacheLayer,
volume = cacheVolume, length = cacheLength,
character = cacheCharacter, text = cacheText, kind = cacheKind,
}
end
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)
if type(tag) ~= "string" then return nil end
if frame then frame = math.floor(frame) else frame = math.floor(obj.timelineframe or 0) end
if tag == cacheTag and frame == cacheFrame then
if cacheFound then return buildObject() end
return nil
end
local len = #tag
if len > CB_TAG_MAX then len = CB_TAG_MAX end
ffi.copy(base + CB_TAG_OFFSET, tag, len)
i32[OFF_CB_TAGLEN] = len
i32[OFF_CB_FRAME] = frame
i32[OFF_CB_KIND] = CB_KIND_GETOBJECT
i32[OFF_STATUS] = STATUS_CALLBACK
k32.SetEvent(doneEvent)
k32.WaitForSingleObject(workEvent, INFINITE)
cacheTag = tag
cacheFrame = frame
if i32[OFF_CB_FOUND] == 0 then
cacheFound = false
return nil
end
cacheFound = true
cacheExist = cbResult[0] ~= 0
cacheX = cbResult[1]; cacheY = cbResult[2]; cacheZ = cbResult[3]
cacheZoom = cbResult[4]; cacheRz = cbResult[5]; cacheAlpha = cbResult[6]; cacheLayer = cbResult[7]
cacheVolume = cbResult[8]; cacheLength = cbResult[9]
local meta = ffi.string(base + CB_TAG_OFFSET, i32[OFF_CB_TAGLEN])
local s1 = meta:find("\0", 1, true)
local s2 = s1 and meta:find("\0", s1 + 1, true)
if s1 and s2 then
cacheCharacter = meta:sub(1, s1 - 1)
cacheText = meta:sub(s1 + 1, s2 - 1)
cacheKind = meta:sub(s2 + 1)
else
cacheCharacter = ""; cacheText = ""; cacheKind = ""
end
return buildObject()
end
sandbox = {
obj = obj, scene = scene, ymm4 = ymm4,
anim = anim, bit32 = bit32, rawlen = rawlen,
RGB = RGB, HSV = HSV, OR = OR, AND = AND, XOR = XOR, SHIFT = SHIFT,
__fast_fill = __fast_fill, __fast_convolve = __fast_convolve, __fast_resize = __fast_resize,
debug_print = debug_print,
math = math, string = string, table = table,
type = type, tostring = tostring, tonumber = tonumber, select = select,
error = error, assert = assert, ipairs = ipairs, pairs = pairs, next = next,
unpack = unpack, setmetatable = setmetatable, getmetatable = getmetatable,
rawget = rawget, rawset = rawset, rawequal = rawequal, rawlen = rawlen,
pcall = pcall, xpcall = xpcall, print = function() end,
}
sandbox._G = sandbox
local function captureKeys(t)
local keys = {}
for k in pairs(t) do keys[k] = true end
return keys
end
local function resetExtraKeys(t, keys)
for k in pairs(t) do
if not keys[k] then t[k] = nil end
end
end
local sandboxKeys = captureKeys(sandbox)
local objKeys = captureKeys(obj)
local sceneKeys = captureKeys(scene)
local ymm4Keys = captureKeys(ymm4)
local compiledCode, compiledChunk, compiledError
local lastScriptVersion
local function loadFields()
width = i32[OFF_WIDTH]
height = i32[OFF_HEIGHT]
obj.w = f64[0]; obj.h = f64[1]; obj.hw = f64[2]; obj.hh = f64[3]
obj.cx = f64[4]; obj.cy = f64[5]; obj.cz = f64[6]; obj.diagonal = f64[7]
obj.x = f64[8]; obj.y = f64[9]; obj.z = f64[10]
obj.ox = f64[11]; obj.oy = f64[12]; obj.oz = f64[13]
obj.sx = f64[14]; obj.sy = f64[15]; obj.sz = f64[16]
obj.zoom = f64[17]; obj.aspect = f64[18]; obj.alpha = f64[19]
obj.rx = f64[20]; obj.ry = f64[21]; obj.rz = f64[22]
obj.rxr = f64[23]; obj.ryr = f64[24]; obj.rzr = f64[25]
obj.track0 = f64[26]; obj.track1 = f64[27]; obj.track2 = f64[28]; obj.track3 = f64[29]
obj.slider0 = f64[58]; obj.slider1 = f64[59]; obj.slider2 = f64[60]; obj.slider3 = f64[61]
obj.time = f64[30]; obj.frame = f64[31]; obj.totalframe = f64[32]; obj.totaltime = f64[33]
obj.t = f64[34]; obj.framerate = f64[35]; obj.layer = f64[36]; obj.index = f64[37]; obj.num = f64[38]
scene.width = f64[39]; scene.height = f64[40]; scene.cx = f64[41]; scene.cy = f64[42]
ymm4.group_index = f64[43]; ymm4.group_count = f64[44]
ymm4.timeline_totalframe = f64[45]; ymm4.timeline_totaltime = f64[46]
ymm4.time_ratio = f64[47]
ymm4.is_saving = f64[48] ~= 0; ymm4.is_playing = f64[49] ~= 0; ymm4.is_paused = f64[50] ~= 0
ymm4.group_ratio = (f64[44] ~= 0) and (f64[43] / f64[44]) or 0
obj.timelineframe = f64[51]; obj.timelinetime = f64[52]
obj.check0 = f64[53] ~= 0; obj.check1 = f64[54] ~= 0; obj.check2 = f64[55] ~= 0; obj.check3 = f64[56] ~= 0
sandbox.color = (f64[57] >= 0) and f64[57] or nil
aviutl_set_time_ratio(f64[33] > 0 and f64[30] / f64[33] or 0)
sandbox.time = f64[30]; sandbox.frame = f64[31]; sandbox.totalframe = f64[32]
sandbox.framerate = f64[35]; sandbox.layer = f64[36]
sandbox.timelineframe = f64[51]; sandbox.timelinetime = f64[52]
end
local function loadStringParams()
if i32[OFF_STRING_PARAMS_LEN] <= 0 then return end
local p = base + STRING_PARAMS_OFFSET
local count = ffi.cast("int32_t*", p)[0]
local pos = 4
for _ = 1, count do
local nameLen = ffi.cast("int32_t*", p + pos)[0]; pos = pos + 4
local name = ffi.string(p + pos, nameLen); pos = pos + nameLen
local valLen = ffi.cast("int32_t*", p + pos)[0]; pos = pos + 4
local val = ffi.string(p + pos, valLen); pos = pos + valLen
obj[name] = val
end
end
local function storeFields()
for i = FIRST_WRITABLE, LAST_WRITABLE do
local key
if i == 8 then key = obj.x elseif i == 9 then key = obj.y elseif i == 10 then key = obj.z
elseif i == 11 then key = obj.ox elseif i == 12 then key = obj.oy elseif i == 13 then key = obj.oz
elseif i == 14 then key = obj.sx elseif i == 15 then key = obj.sy elseif i == 16 then key = obj.sz
elseif i == 17 then key = obj.zoom elseif i == 18 then key = obj.aspect elseif i == 19 then key = obj.alpha
elseif i == 20 then key = obj.rx elseif i == 21 then key = obj.ry elseif i == 22 then key = obj.rz
elseif i == 23 then key = obj.rxr elseif i == 24 then key = obj.ryr elseif i == 25 then key = obj.rzr end
f64[i] = tonumber(key) or f64[i]
end
end
while true do
k32.WaitForSingleObject(workEvent, INFINITE)
if i32[OFF_COMMAND] == 1 then break end
qpcRunStart = qpcNow()
dirty = false
pixelsValid = false
pdValid = false
pdDirty = false
pixelProcessAvailable = i32[OFF_CB_FOUND] > 0
frameBufferValid = false
ringD[0] = 0
local version = i32[OFF_SCRIPT_VERSION]
if version ~= lastScriptVersion then
lastScriptVersion = version
local scriptLen = i32[OFF_SCRIPTLEN]
local code = ffi.string(base + SCRIPT_OFFSET, scriptLen)
if code ~= compiledCode then
local chunk, err = loadstring(code, "LuaScript")
if chunk then
compiledChunk = chunk
compiledCode = code
compiledError = nil
else
compiledChunk = nil
compiledCode = code
compiledError = err or "compile error"
end
end
end
if not compiledChunk then
local msg = compiledError or "compile error"
ffi.copy(base + ERROR_OFFSET, msg, math.min(#msg, 4096))
i32[OFF_ERRORLEN] = math.min(#msg, 4096)
i32[OFF_STATUS] = 2
k32.SetEvent(doneEvent)
else
resetExtraKeys(sandbox, sandboxKeys)
resetExtraKeys(obj, objKeys)
resetExtraKeys(scene, sceneKeys)
resetExtraKeys(ymm4, ymm4Keys)
loadFields()
loadStringParams()
cacheTag = nil
for k in pairs(options) do options[k] = nil end
for k in pairs(pixeloptions) do pixeloptions[k] = nil end
for k in pairs(sceneReads) do sceneReads[k] = nil end
for k in pairs(sceneWrites) do sceneWrites[k] = nil end
drawTarget = "frame"
font.family = ""; font.size = 34; font.bold = false; font.italic = false; font.color = 0xFFFFFF
math.randomseed(f64[31])
setfenv(compiledChunk, sandbox)
local ok, err = pcall(compiledChunk)
if ok then
flushPixelData()
storeFields()
local ds = options.draw_state
if ds == nil then f64[62] = 0 elseif ds == false or ds == 0 then f64[62] = 2 else f64[62] = 1 end
i32[OFF_PIXELSDIRTY] = dirty and 1 or 0
i32[OFF_STATUS] = 1
k32.SetEvent(doneEvent)
else
local msg = tostring(err)
ffi.copy(base + ERROR_OFFSET, msg, math.min(#msg, 4096))
i32[OFF_ERRORLEN] = math.min(#msg, 4096)
i32[OFF_STATUS] = 2
k32.SetEvent(doneEvent)
end
end
end
k32.UnmapViewOfFile(view)
k32.CloseHandle(hMap)