From dbe961317ec0a563f5ed2f5d3b9fd3f3ca50b704 Mon Sep 17 00:00:00 2001 From: 4DBug <4DBug@github.com> Date: Tue, 20 Oct 2026 14:06:20 -0500 Subject: [PATCH] Textures: typed, unrolled RGBA/Alpha8 row conversion (no zero-skip branch): new 512x256 atlas record 0.54 -> 0.30 ms headless, pixels unchanged --- backends/roblox/textures.lua | 60 +++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/backends/roblox/textures.lua b/backends/roblox/textures.lua index dbdd1b6..897bd51 100644 --- a/backends/roblox/textures.lua +++ b/backends/roblox/textures.lua @@ -39,33 +39,45 @@ function M.GetBackendData() return ImGui.GetCurrentContext() and ImGui.GetIO().BackendPlatformUserData or nil end +-- n pixels from the byte table src (1-based index s) to dst (byte offset d). Typed + unrolled by 4: the native code +-- skips the generic table/buffer checks (~40% faster than the plain loop). @native -local function CopyTexturePixels(tex, dst, x, y, w, h, dst_zero) - local bpp, tex_w = tex.BytesPerPixel, tex.Width - if x == 0 and w == tex_w and bpp == 4 then -- whole rows: one flat loop; dst_zero: skip writing zero pixels +local function CopyRGBA(src: { number }, s: number, dst: buffer, d: number, n: number) + for _ = 1, n // 4 do + writeu32(dst, d, src[s] + src[s + 1] * 256 + src[s + 2] * 65536 + src[s + 3] * 16777216) + writeu32(dst, d + 4, src[s + 4] + src[s + 5] * 256 + src[s + 6] * 65536 + src[s + 7] * 16777216) + writeu32(dst, d + 8, src[s + 8] + src[s + 9] * 256 + src[s + 10] * 65536 + src[s + 11] * 16777216) + writeu32(dst, d + 12, src[s + 12] + src[s + 13] * 256 + src[s + 14] * 65536 + src[s + 15] * 16777216) + s += 16 + d += 16 + end + for _ = 1, n % 4 do + writeu32(dst, d, src[s] + src[s + 1] * 256 + src[s + 2] * 65536 + src[s + 3] * 16777216) + s += 4 + d += 4 + end +end + +@native +local function CopyAlpha8(src: { number }, s: number, dst: buffer, d: number, n: number) -- white with alpha + for _ = 1, n do + writeu32(dst, d, 16777215 + src[s] * 16777216) + s += 1 + d += 4 + end +end + +local function CopyTexturePixels(tex, dst, x, y, w, h) + local tex_w = tex.Width + local copy = tex.BytesPerPixel == 4 and CopyRGBA or CopyAlpha8 + if x == 0 and w == tex_w then -- whole rows: one flat run local src, s = tex:GetPixelsAt(0, y) - local d = y * tex_w * 4 - for _ = 1, w * h do - local v = src[s] + src[s + 1] * 256 + src[s + 2] * 65536 + src[s + 3] * 16777216 - if v ~= 0 or not dst_zero then writeu32(dst, d, v) end - s += 4 - d += 4 - end + copy(src, s, dst, y * tex_w * 4, w * h) return end for row = 0, h - 1 do - local src, src_base = tex:GetPixelsAt(x, y + row) - local dst_off = ((y + row) * tex_w + x) * 4 - if bpp == 4 then - for i = 0, w - 1 do - local s = src_base + i * 4 - writeu32(dst, dst_off + i * 4, src[s] + src[s + 1] * 256 + src[s + 2] * 65536 + src[s + 3] * 16777216) - end - else -- Alpha8: white with alpha - for i = 0, w - 1 do - writeu32(dst, dst_off + i * 4, 16777215 + src[src_base + i] * 16777216) - end - end + local src, s = tex:GetPixelsAt(x, y + row) + copy(src, s, dst, ((y + row) * tex_w + x) * 4, w) end end @@ -96,9 +108,9 @@ local function CreateTextureRecord(tex) n += c end end - CopyTexturePixels(tex, b, 0, 0, cw, ch, tex.BytesPerPixel == 4) + CopyTexturePixels(tex, b, 0, 0, cw, ch) else - CopyTexturePixels(tex, b, 0, 0, w, h, true) + CopyTexturePixels(tex, b, 0, 0, w, h) end -- rows FillRow.. hold only Fill (what zero converts to): parallel.lua sends Actors the rows above it local t = { Buffer = b, Width = w, Height = h, FillRow = ch, Fill = tex.BytesPerPixel ~= 4 and 16777215 or 0 }