From 4cc328560d40a3541f95dcc12dbba0f7ca214eb4 Mon Sep 17 00:00:00 2001 From: 4DBug <115124145+4DBug@users.noreply.github.com> Date: Tue, 20 Oct 2026 14:31:50 -0500 Subject: [PATCH] Parallel: Actors free destroyed textures (each atlas grow/repack left a full copy in every Actor), atlas patches send the updated glyph rects instead of whole row spans and are built once per version for all Actors (fonts tex sends 151 -> 91 KB/frame in Studio, TexBytes 35.6 -> 22.2 MB headless); Actor texture handling moved to Raster.ApplyTextures, which Sync mode now runs on per-Actor copies so t_parallel checks it; t_parallel fonts scenario (new size per frame), check_all runs 620 frames --- backends/roblox/parallel.lua | 94 ++++++++++++++++++++++++++++-- backends/roblox/raster.lua | 42 +++++++++++++ backends/roblox/textures.lua | 19 ++++-- backends/roblox/worker.luau | 28 +-------- tools/test/check_all.sh | 2 +- tools/test/scripts/t_parallel.luau | 10 +++- 6 files changed, 157 insertions(+), 38 deletions(-) diff --git a/backends/roblox/parallel.lua b/backends/roblox/parallel.lua index 8c37c1f..a6b5bdb 100644 --- a/backends/roblox/parallel.lua +++ b/backends/roblox/parallel.lua @@ -68,6 +68,23 @@ local pending, by_id, batch_no = {}, {}, 0 local open = false local actors, ready, conns, folder = {}, false, {}, nil local sent = {} +-- ids of destroyed textures each Actor holds a copy of, sent with its next job (the Actor drops them) +local drops = {} +local NO_DROPS = {} +local sync_tex = {} -- Sync mode: the "Actors'" texture copies +EditableImages.TextureDropped = function(id, rec) + if not rec then + return + end + for a = 1, NA do + local s = sent[a] + if s and s[rec] ~= nil then + s[rec] = nil + local d = drops[a] + d[#d + 1] = id + end + end +end local function grow(b: buffer, need: number): buffer local n = buffer.len(b) @@ -251,6 +268,46 @@ local function fullCopy(rec, v: number): buffer return rec.Head end +-- The pixels changed between versions had and v (rows y0..y1 of rec): the updated rects (rect list -> worker copies +-- each rect's rows), or the whole rows when they are smaller. Built once per (had, v), shared by every Actor. +local function patchCopy(rec, had: number, v: number, y0: number, y1: number): (buffer, any) + if rec.PatchHad == had and rec.PatchV == v then + return rec.Patch, rec.PatchAt + end + local row, src = rec.Width * 4, rec.Buffer + local log, area = rec.Log, 0 + for k = had + 1, v do + local r = log[k][3] + if r then + for i = 1, #r, 4 do area += r[i + 2] * r[i + 3] end + end + end + local patch, at + if area * 4 < (y1 - y0) * row then + patch, at = buffer.create(area * 4), {} + local o, n = 0, 0 + for k = had + 1, v do + local r = log[k][3] + if r then + for i = 1, #r, 4 do + local x, y, w, h = r[i], r[i + 1], r[i + 2], r[i + 3] + at[n + 1], at[n + 2], at[n + 3], at[n + 4] = x, y, w, h + n += 4 + for yy = y, y + h - 1 do + copy(patch, o, src, (yy * rec.Width + x) * 4, w * 4) + o += w * 4 + end + end + end + end + else + patch, at = buffer.create((y1 - y0) * row), y0 * row + copy(patch, 0, src, y0 * row, (y1 - y0) * row) + end + rec.PatchHad, rec.PatchV, rec.Patch, rec.PatchAt = had, v, patch, at + return patch, at +end + function M.Flush() local n = jobs.N if n == 0 then @@ -321,22 +378,45 @@ function M.Flush() tex[#tex + 1] = { id, buf, rec.Width, rec.Height, rec.Fill or 0 } Stats.TexBytes += buffer.len(buf) elseif y1 > y0 then - local row = rec.Width * 4 - local patch = buffer.create((y1 - y0) * row) - copy(patch, 0, rec.Buffer, y0 * row, (y1 - y0) * row) - tex[#tex + 1] = { id, false, rec.Width, rec.Height, y0 * row, patch } + local patch, at = patchCopy(rec, had, v, y0, y1) + tex[#tex + 1] = { id, false, rec.Width, rec.Height, at, patch } Stats.TexBytes += buffer.len(patch) end Stats.TexSends += 1 end end Stats.SendBytes += Pn[a] + tl + local d = drops[a] + if #d > 0 then + drops[a] = {} + else + d = NO_DROPS + end if cfg.Sync then + -- like an Actor: own texture copies, kept by the same messages (SendMessage copies: clone the buffers) + local st = sync_tex[a] + if not st then + st = {} + sync_tex[a] = st + end + local msg = table.create(#tex) + for i, u in tex do + local c = table.clone(u) + for k, x in c do + if type(x) == "buffer" then + local nb = buffer.create(buffer.len(x)) + copy(nb, 0, x) + c[k] = nb + end + end + msg[i] = c + end + Raster.ApplyTextures(st, msg, d) if not (cfg.SyncDrop and a % 2 == 0) then - b.Res[a] = RasterJobs(P, T, tl, Records) + b.Res[a] = RasterJobs(P, T, tl, st) end else - actors[a]:SendMessage("job", batch_no, P, T, tex, tl) + actors[a]:SendMessage("job", batch_no, P, T, tex, tl, d) end end if par then @@ -365,6 +445,8 @@ local function setActors(n) Tb[a] = Tb[a] or buffer.create(1024) used[a] = used[a] or {} sent[a] = setmetatable({}, { __mode = "k" }) + drops[a] = {} + sync_tex[a] = nil end reset() end diff --git a/backends/roblox/raster.lua b/backends/roblox/raster.lua index 1d3acca..70ff305 100644 --- a/backends/roblox/raster.lua +++ b/backends/roblox/raster.lua @@ -795,4 +795,46 @@ function M.RasterJobs(P: buffer, T: buffer, tlen: number, textures): { buffer } return out end +-- An Actor's texture copies (worker.luau; parallel.lua's Sync mode emulates it): drop the destroyed ids, apply the +-- updates parallel.lua's Flush sent. { id, buf, w, h, fill }: whole texture, rows past buf are all fill; +-- { id, false, w, h, offset, patch }: rows; { id, false, w, h, rects, patch }: x, y, w, h quadruples, rows packed. +function M.ApplyTextures(textures, updates, dropped) + for _, id in dropped or {} do + textures[id] = nil + end + for _, u in updates do + if u[2] then + local b, w, h, fill = u[2], u[3], u[4], u[5] + local n, total = buffer.len(b), w * h * 4 + if n < total then + local full = buffer.create(total) + buffer.copy(full, 0, b) + if fill ~= 0 then + buffer.writeu32(full, n, fill) + local k = 4 + while k < total - n do + local c = math.min(k, total - n - k) + buffer.copy(full, n + k, full, n, c) + k += c + end + end + b = full + end + textures[u[1]] = { Buffer = b, Width = w, Height = h } + elseif type(u[5]) == "table" then + local t, rects, patch = textures[u[1]], u[5], u[6] + local dst, w, o = t.Buffer, t.Width, 0 + for i = 1, #rects, 4 do + local x, y, rw, rh = rects[i], rects[i + 1], rects[i + 2], rects[i + 3] + for yy = y, y + rh - 1 do + buffer.copy(dst, (yy * w + x) * 4, patch, o, rw * 4) + o += rw * 4 + end + end + else + buffer.copy(textures[u[1]].Buffer, u[5], u[6]) + end + end +end + return M diff --git a/backends/roblox/textures.lua b/backends/roblox/textures.lua index 897bd51..4a6353e 100644 --- a/backends/roblox/textures.lua +++ b/backends/roblox/textures.lua @@ -7,7 +7,8 @@ local TILE = Raster.TILE local M = {} --- One table, not locals: shared by every context and module. FreeTile is set by layers.lua (Evict needs it). +-- One table, not locals: shared by every context and module. FreeTile is set by layers.lua (Evict needs it), +-- TextureDropped(id, record) by parallel.lua (Actors free their copies). local EditableImages = { Cap = 64 * 1024 * 1024, Bytes = 0, Count = 0, ImageBytes = setmetatable({}, { __mode = "k" }), Pool = {}, PoolMax = 16, @@ -131,22 +132,28 @@ function M.UpdateTexture(tex) return end local y0, y1 = math.huge, -1 + local rects, n = {}, 0 for _, r in tex.Updates:iter() do CopyTexturePixels(tex, t.Buffer, r.x, r.y, r.w, r.h) if r.y < y0 then y0 = r.y end if r.y + r.h > y1 then y1 = r.y + r.h end + rects[n + 1], rects[n + 2], rects[n + 3], rects[n + 4] = r.x, r.y, r.w, r.h + n += 4 end - -- parallel workers hold copies, patched when this changes: Log[V] = changed rows (only the last 16 kept) + -- parallel workers hold copies, patched when this changes: Log[V] = changed rows + the rects (x, y, w, h + -- quadruples) (only the last 16 kept) if t.FillRow and y1 > t.FillRow then t.FillRow = y1 end local v = (t.V or 0) + 1 t.V = v t.Log = t.Log or {} - t.Log[v] = y1 > y0 and { y0, y1 } or { 0, 0 } + t.Log[v] = y1 > y0 and { y0, y1, rects } or { 0, 0 } t.Log[v - 16] = nil tex:SetStatus(ImTextureStatus.OK) elseif tex.Status == ImTextureStatus.WantDestroy then if tex.BackendUserData then - Records[tex.BackendUserData] = nil + local id = tex.BackendUserData + if EditableImages.TextureDropped then EditableImages.TextureDropped(id, Records[id]) end + Records[id] = nil tex.BackendUserData = nil end tex:SetTexID(ImTextureID_Invalid) @@ -162,7 +169,9 @@ function ImGui_ImplRoblox_CreateTexture(pixels, w, h) end function ImGui_ImplRoblox_DestroyTexture(tex_ref) - Records[tex_ref._TexID] = nil + local id = tex_ref._TexID + if EditableImages.TextureDropped then EditableImages.TextureDropped(id, Records[id]) end + Records[id] = nil end -- Every EditableImage goes through Create/Destroy, counted against Cap. Over the cap (or CreateEditableImage failing): diff --git a/backends/roblox/worker.luau b/backends/roblox/worker.luau index 6f1867d..93c2a62 100644 --- a/backends/roblox/worker.luau +++ b/backends/roblox/worker.luau @@ -8,31 +8,9 @@ local Raster = require(script.Parent:WaitForChild("Raster")) local Done = Actor:WaitForChild("Done") local Textures = {} -Actor:BindToMessageParallel("job", function(Id, Prims, Tiles, TexUpdates, TileLength) - for _, Texture in TexUpdates do - if Texture[2] then - local B, W, H, Fill = Texture[2], Texture[3], Texture[4], Texture[5] - local n, total = buffer.len(B), W * H * 4 - if n < total then -- the rows below were left out: all Fill (parallel.lua fullCopy) - local Full = buffer.create(total) - buffer.copy(Full, 0, B) - if Fill ~= 0 then - buffer.writeu32(Full, n, Fill) - local k = 4 - while k < total - n do - local c = math.min(k, total - n - k) - buffer.copy(Full, n + k, Full, n, c) - k += c - end - end - B = Full - end - Textures[Texture[1]] = { Buffer = B, Width = W, Height = H } - else -- rows changed since the copy this Actor has - local T = Textures[Texture[1]] - buffer.copy(T.Buffer, Texture[5], Texture[6]) - end - end +Actor:BindToMessageParallel("job", function(Id, Prims, Tiles, TexUpdates, TileLength, Dropped) + -- this Actor's texture copies: destroyed ids dropped, changed rows/rects patched (parallel.lua Flush) + Raster.ApplyTextures(Textures, TexUpdates, Dropped) local Results = Raster.RasterJobs(Prims, Tiles, TileLength, Textures) task.synchronize() Done:Fire(Id, Results) diff --git a/tools/test/check_all.sh b/tools/test/check_all.sh index f84a76e..bebb450 100755 --- a/tools/test/check_all.sh +++ b/tools/test/check_all.sh @@ -41,7 +41,7 @@ CHECKS=( "pixels|PIXELS SAME|a=\$($R --main tools/test/scripts/t_pixels.luau --frames 600 | grep -P '^PIX\t'); b=\$(RUN_FLAGS=ARG_FORCE=true $R --main tools/test/scripts/t_pixels.luau --frames 600 | grep -P '^PIX\t'); [ -n \"\$a\" ] && [ \"\$a\" = \"\$b\" ] && echo PIXELS SAME" "rect_flip|RECTFLIP OK|$R --main tools/test/scripts/t_rect_flip.luau --frames 30" "tile_bound|TILEBOUND OK|$R --main tools/test/scripts/t_tile_bound.luau --frames 200" -"parallel|PARALLEL OK|$R --main tools/test/scripts/t_parallel.luau --frames 500" +"parallel|PARALLEL OK|$R --main tools/test/scripts/t_parallel.luau --frames 620" "pool_ctx|POOLCTX OK|$R --main tools/test/scripts/t_pool_ctx.luau --frames 60" "assets_scroll|ASSETS OK|$R --main tools/test/scripts/t_assets_scroll.luau --frames 1010" "shot|wrote|python3 tools/test/shot.py /tmp/imgui_shot.png --app" diff --git a/tools/test/scripts/t_parallel.luau b/tools/test/scripts/t_parallel.luau index dd01873..6ba503a 100644 --- a/tools/test/scripts/t_parallel.luau +++ b/tools/test/scripts/t_parallel.luau @@ -4,7 +4,7 @@ -- PreSimulation -> PostSimulation), open with SyncDrop (half the worker results lost -> Finish falls back), and closed -- (late RenderDrawData: rasterized + uploaded at once). The composites of A and B must match at every checkpoint. -- Prints the executor's main-thread pack cost per frame next to A's inline raster + upload cost. --- run: python3 tools/test/run.py --main tools/test/scripts/t_parallel.luau --frames 900 --quiet --roblox +-- run: python3 tools/test/run.py --main tools/test/scripts/t_parallel.luau --frames 900 --quiet --roblox (5 scenarios: >= 600) local RS = game:GetService("ReplicatedStorage") local G = require(RS:WaitForChild("ImGui")) local ImGui, ImVec2 = G.ImGui, G.ImVec2 @@ -85,6 +85,14 @@ local SCEN = { ImGui.Text("frame %d", f) ImGui.End() end }, + { "fonts", function(f) -- a new font size every frame: glyph uploads, atlas grow/repack (Actor texture patches) + ImGui.SetNextWindowPos(ImVec2(40, 30), G.ImGuiCond.Always); ImGui.SetNextWindowSize(ImVec2(620, 600), G.ImGuiCond.Always) + ImGui.Begin("Fonts") + ImGui.PushFont(nil, 12 + (f * 0.25) % 40) + for i = 1, 12 do ImGui.TextWrapped("Line %d: the quick brown fox jumps over the lazy dog, %d times.", i, f) end + ImGui.PopFont() + ImGui.End() + end }, } local ctx, gui = {}, {}