Textures: typed, unrolled RGBA/Alpha8 row conversion (no zero-skip branch): new 512x256 atlas record 0.54 -> 0.30 ms headless, pixels unchanged

This commit is contained in:
4DBug committed 2026-10-20 14:47:54 -05:00
1 parent 1ec760f639
commit dbe961317e
1 file changed
+36 -24
+36 -24
View File
@@ -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 }