Roblox backend: recolored draw lists (same vertex positions, a few vertices' colour / uv changed, e.g. hover) skip HashTiles: CompareDrawList 4 lists them, PatchDrawList gives those primitives their real hashes (PrimHash / FlatTileHash) and redraws just their tiles, leaving the tiles exactly as the full path would; t_recolor_pixels (incremental vs forced, also parallel) in check_all

This commit is contained in:
4DBug committed 2026-10-23 23:19:40 -05:00
1 parent 951d18fda2
commit cef7a1209e
4 files changed
+249 -17

No files matched your search

+17 -7
View File
@@ -170,7 +170,8 @@ function M.DestroyTiles(BackendData)
end
-- Snapshot compared exactly with the next frame's list: 1 = identical (only restack), 2 = moved by whole pixels
-- (only move the layers), 0 = changed. Per command: clip (4 f64), ElemCount, IdxOffset, VtxOffset (u32), tex id
-- (only move the layers), 3 = moved with drifted vertices, 4 = same positions, a few vertices' uv / colour changed
-- (both list those in Drift), 0 = changed. Per command: clip (4 f64), ElemCount, IdxOffset, VtxOffset (u32), tex id
-- (f64), kind (u8), unclipped (u8), bounds of its primitives (4 x i32).
local CMD_STRIDE = 70
@@ -291,17 +292,24 @@ function M.CompareDrawList(set, draw_list, ox, oy, ScreenWidth, ScreenHeight)
-- translation from the first vertex: whole pixels only, and only with DisplayPos at 0 (so it stays exact)
local dx, dy = readf64(vb, 0) - readf64(sv, 0), readf64(vb, 8) - readf64(sv, 8)
if dx ~= dx or dy ~= dy then return 0 end
local nd = 0
if dx == 0 and dy == 0 then
-- per vertex: x y u v as f64s + col, not readstring == (that allocated 2 vertex-buffer strings per list per
-- frame: GC spikes) or 9 u32s (3x slower). Equal f64s have equal bits except +-0: their sign words decide.
-- Same position, other uv / colour (hover, a changed colour): listed ("recolored"), up to MAX_DRIFT.
for v = 0, nv - VTX_STRIDE, VTX_STRIDE do
local x, y, u, w = readf64(vb, v), readf64(vb, v + 8), readf64(vb, v + 16), readf64(vb, v + 24)
if x ~= readf64(sv, v) or y ~= readf64(sv, v + 8) or u ~= readf64(sv, v + 16)
or w ~= readf64(sv, v + 24) or readu32(vb, v + 32) ~= readu32(sv, v + 32) then
if x ~= readf64(sv, v) or y ~= readf64(sv, v + 8) then return 0 end
if (x == 0 or y == 0)
and (readu32(vb, v + 4) ~= readu32(sv, v + 4) or readu32(vb, v + 12) ~= readu32(sv, v + 12)) then
return 0
end
if x == 0 or y == 0 or u == 0 or w == 0 then
for p = v + 4, v + 28, 8 do if readu32(vb, p) ~= readu32(sv, p) then return 0 end end
if u ~= readf64(sv, v + 16) or w ~= readf64(sv, v + 24) or readu32(vb, v + 32) ~= readu32(sv, v + 32)
or ((u == 0 or w == 0)
and (readu32(vb, v + 20) ~= readu32(sv, v + 20) or readu32(vb, v + 28) ~= readu32(sv, v + 28))) then
if nd >= MAX_DRIFT then return 0 end
nd += 1
Drift[nd] = v
end
end
else
@@ -361,7 +369,9 @@ function M.CompareDrawList(set, draw_list, ox, oy, ScreenWidth, ScreenHeight)
or draw_list._ContentClipY0 ~= set.SnapContentY0 or draw_list._ContentClipX1 ~= set.SnapContentX1
or draw_list._ContentClipY1 ~= set.SnapContentY1 or draw_list._ScrollOriginX ~= set.SnapScrollX
or draw_list._ScrollOriginY ~= set.SnapScrollY) then return 0 end
return 1
if nd == 0 then return 1 end
M.DriftCount = nd
return 4, 0, 0
end
if set.SnapHasContent and not (ShiftedExact(set.SnapContentX0, dx, draw_list._ContentClipX0)
and ShiftedExact(set.SnapContentY0, dy, draw_list._ContentClipY0)
@@ -372,7 +382,7 @@ function M.CompareDrawList(set, draw_list, ox, oy, ScreenWidth, ScreenHeight)
-- vertices off the exact shift ("drift": trig-generated ones round differently, often when a coordinate crosses a
-- power of two) are listed. All their floor / ceil / ceil(- 0.5) (CollectPrims' bounds) shifted: 3, only the tiles
-- of their primitives change; else 0 (the full path still gets the list: tile hashes don't see an ulp)
local nd, consistent = 0, true
local consistent = true
for v = 0, nv - VTX_STRIDE, VTX_STRIDE do
if readf64(vb, v + 16) ~= readf64(sv, v + 16) or readf64(vb, v + 24) ~= readf64(sv, v + 24)
or readu32(vb, v + 32) ~= readu32(sv, v + 32) then return 0 end
+86 -10
View File
@@ -75,6 +75,54 @@ end
-- their hashes get DriftMark* (new every frame) added, so their tiles are redrawn even when the hash doesn't see the ulp
local DriftSet, DriftOn, DriftMark1, DriftMark2 = table.create(4096, false), false, 0, 0
-- One primitive's HashTiles entry hash, for PatchDrawList (the same math as HashTiles' inlined copy: keep both equal).
-- Returns h1, h2 and the flat signature (>= 0: a flat rect, whose per-tile hash is FlatTileHash's)
@native
local function PrimHash(p, lx0, ly0, ox, oy)
local vbuf, va, vd, tex_id = PrimVtxBuffer[p], PrimA[p], PrimD[p], PrimTexId[p]
if PrimFlat[p] then
local RectColor, RectUV = readu32(vbuf, va + 32), readf64(vbuf, va + 16) * 8192 + readf64(vbuf, va + 24)
return 0, 0, ((RectColor * 8191 + RectUV * 4096) % P1 * 64 + tex_id) % P1
end
local cx0, cy0, cx1, cy1 = PrimBoundsX0[p] - lx0, PrimBoundsY0[p] - ly0, PrimBoundsX1[p] - lx0,
PrimBoundsY1[p] - ly0
local gk, v2, v3, nv = PrimGradient[p], PrimC[p], PrimB[p], 3
local g = gk or 0
local h1 = (((cx0 * 8191 + cy0) * 8191 + cx1) * 4 + g) % P1
local h2 = (((cy1 * 8191 + cx1) * 8191 + cy0 + tex_id) * 4 + g) % P2
local vax, vay = lx0 + ox, ly0 + oy
if vd then
nv = gk == 3 and 4 or 2
else
v2, v3 = v3, v2
end
local v = va
for k = 1, nv do
if k == 2 then v = v2 elseif k == 3 then v = v3 elseif k == 4 then v = vd end
local x, y = readf64(vbuf, v) - vax, readf64(vbuf, v + 8) - vay
local uv = readf64(vbuf, v + 16) * 8192 + readf64(vbuf, v + 24)
local c = readu32(vbuf, v + 32)
h1 = (h1 * 48271 + (x * 4096 + y) * 31 + uv * 4096 + c) % P1
h2 = (h2 * 16807 + (y * 4093 + x) * 17 + uv * 65521 + c * 3) % P2
end
return h1, h2, -1
end
-- A flat rect's hash in tile (tx, ty) (HashTiles' per-tile flat hash)
@native
local function FlatTileHash(p, lx0, ly0, tx, ty)
local vbuf, va, tex_id = PrimVtxBuffer[p], PrimA[p], PrimTexId[p]
local RectColor, RectUV = readu32(vbuf, va + 32), readf64(vbuf, va + 16) * 8192 + readf64(vbuf, va + 24)
local cx0, cy0, cx1, cy1 = PrimBoundsX0[p] - lx0, PrimBoundsY0[p] - ly0, PrimBoundsX1[p] - lx0,
PrimBoundsY1[p] - ly0
local px0, py0 = tx * TILE, ty * TILE
local ix0, iy0 = max(cx0 - px0, 0), max(cy0 - py0, 0)
local ix1, iy1 = min(cx1 - px0, TILE), min(cy1 - py0, TILE)
local r = ((ix0 * 131 + iy0) * 131 + ix1) * 131 + iy1
return ((r * 48271 + RectColor) % P1 * 48271 + RectUV * 4096 + 7) % P1,
((r * 16807 + RectColor + tex_id) % P2 * 16807 + RectUV * 65521 + 11) % P2
end
-- HashTiles scratch, per touched tile (slot = its index in layer.Touched): tile key -> slot, its list, count, hashes
local KeySlot, SlotCount, SlotHash1, SlotHash2 = {}, {}, {}, {}
local SlotList, SlotListHash1, SlotListHash2, SlotListFlat = {}, {}, {}, {}
@@ -797,16 +845,20 @@ local function LayerOrigin(draw_list, kind, ox, oy)
return floor(draw_list._ScrollOriginX - ox), floor(draw_list._ScrollOriginY - oy)
end
local PatchLayers, PatchKeys, PatchPrim = {}, {}, {}
local PatchLayers, PatchKeys, PatchPrim, PatchH1, PatchH2, PatchSig = {}, {}, {}, {}, {}, {}
-- CompareDrawList 3: moved by whole pixels, but a few vertices drifted off the exact shift (same bounds). CollectPrims
-- ran and gave the primitive sequence the tile lists were built from: the lists stay (indices moved to this frame's)
-- and every tile keeps its pixels, except those with a primitive using a drifted vertex: those entries' hashes change
-- (DriftMark), so the tile redraws just them. Changes nothing when it returns false (the caller takes the full path).
-- recolor (CompareDrawList 4: not moved, a few vertices' uv / colour changed): the same, but those primitives get
-- their real hashes (PrimHash, flat rects too), so the tiles end up exactly as HashTiles + DrawLayer would leave them.
-- Their kind must be the same (so the same bounds and tiles; the other primitives' entries are equal by construction).
@native
local function PatchDrawList(BackendData, set, draw_list, n0, n, dx, dy, ox, oy, LimitX0, LimitY0, LimitX1, LimitY1)
local function PatchDrawList(BackendData, set, draw_list, n0, n, dx, dy, ox, oy, LimitX0, LimitY0, LimitX1, LimitY1,
recolor)
table.clear(PatchPrim)
local SigA, SigD = set.PrimSigA, set.PrimSigD
local SigA, SigD, SigG = set.PrimSigA, set.PrimSigD, set.PrimSigG
if not SigA or set.PrimN ~= n - n0 then return false end
for m = 1, n - n0 do
local p = n0 + m
@@ -839,11 +891,15 @@ local function PatchDrawList(BackendData, set, draw_list, n0, n, dx, dy, ox, oy,
for m = 1, count do
local p = plist[m]
local vd = PrimD[p]
if PrimFlat[p]
if (PrimFlat[p] and not recolor)
or not (DriftSet[PrimA[p] // VTX_STRIDE + 1] or DriftSet[PrimB[p] // VTX_STRIDE + 1]
or DriftSet[PrimC[p] // VTX_STRIDE + 1] or (vd and DriftSet[vd // VTX_STRIDE + 1])) then
continue
end
if recolor then
if PrimGradient[p] ~= SigG[p - n0] then return false end
PatchH1[p], PatchH2[p], PatchSig[p] = PrimHash(p, lx0, ly0, ox, oy)
end
PatchPrim[p] = true
local cx0, cy0, cx1, cy1 = PrimBoundsX0[p] - lx0, PrimBoundsY0[p] - ly0, PrimBoundsX1[p] - lx0,
PrimBoundsY1[p] - ly0
@@ -893,16 +949,28 @@ local function PatchDrawList(BackendData, set, draw_list, n0, n, dx, dy, ox, oy,
table.move(tile.DrawnHash1, 1, count, 1, LH1)
table.move(tile.DrawnHash2, 1, count, 1, LH2)
table.move(tile.DrawnFlat, 1, count, 1, layer.TileListFlat[key])
local lx0, ly0 = LayerOrigin(draw_list, kind, ox, oy)
local LF = layer.TileListFlat[key]
local h1, h2 = 0, 0
for k = 1, count do
if PatchPrim[list[k]] then
local p = list[k]
if not PatchPrim[p] then
-- (unchanged entry)
elseif not recolor then
LH1[k], LH2[k] = (LH1[k] + DriftMark1) % P1, (LH2[k] + DriftMark2) % P2
else
local sig = PatchSig[p]
if sig >= 0 then
LH1[k], LH2[k] = FlatTileHash(p, lx0, ly0, layer.TileX[key], layer.TileY[key])
else
LH1[k], LH2[k] = PatchH1[p], PatchH2[p]
end
LF[k] = sig
end
h1 = (h1 * 48271 + LH1[k]) % P1
h2 = (h2 * 16807 + LH2[k] + k) % P2
end
layer.TileHash1[key], layer.TileHash2[key] = h1, h2
local lx0, ly0 = LayerOrigin(draw_list, kind, ox, oy)
local x0, y0 = lx0 + layer.TileX[key] * TILE, ly0 + layer.TileY[key] * TILE
local vx0, vy0, vx1, vy1 = 0, 0, TILE, TILE
if layer.Root ~= layer.Frame then
@@ -1002,19 +1070,26 @@ function M.RenderDrawData(draw_data)
if nd > 0 then
local Drift = Layers.Drift
for k = 1, nd do DriftSet[Drift[k] // VTX_STRIDE + 1] = true end
DriftOn, DriftMark1, DriftMark2 = true, (FrameNo * 7907 + 12345) % P1, (FrameNo * 7919 + 54321) % P2
-- (recolored vertices, 4: only for PatchDrawList; the full path hashes them like any other)
DriftOn, DriftMark1, DriftMark2 = same == 3, (FrameNo * 7907 + 12345) % P1, (FrameNo * 7919 + 54321) % P2
end
if same == 3 then
if same == 3 or same == 4 then
PB("ImGui drift patch")
if EditableImages.StageTiming then EditableImages.BeginStage() end
local patched = PatchDrawList(BackendData, set, draw_list, n0, n, sdx, sdy, ox, oy, LimitX0, LimitY0,
LimitX1, LimitY1)
LimitX1, LimitY1, same == 4)
if EditableImages.StageTiming then EditableImages.EndStage("Raster") end
PE()
if patched then
for k = 1, nd do DriftSet[Layers.Drift[k] // VTX_STRIDE + 1] = false end
DriftOn = false
z = ReuseDrawList(BackendData, set, draw_list, 2, sdx, sdy, z, ox, oy, ScreenWidth, ScreenHeight)
if same == 4 then
-- the snapshot: only vertex bytes changed
buffer.copy(set.SnapVtx, 0, draw_list.VtxBuffer.Buf, 0, set.SnapVtxBytes)
z = ReuseDrawList(BackendData, set, draw_list, 1, 0, 0, z, ox, oy, ScreenWidth, ScreenHeight)
else
z = ReuseDrawList(BackendData, set, draw_list, 2, sdx, sdy, z, ox, oy, ScreenWidth, ScreenHeight)
end
continue
end
end
@@ -1036,6 +1111,7 @@ function M.RenderDrawData(draw_data)
set.PrimBase, set.PrimN = n0, n - n0
set.PrimSigA = table.move(PrimA, n0 + 1, n, 1, set.PrimSigA or {})
set.PrimSigD = table.move(PrimD, n0 + 1, n, 1, set.PrimSigD or {})
set.PrimSigG = table.move(PrimGradient, n0 + 1, n, 1, set.PrimSigG or {})
end
end
if ListDeferred then AnyDeferred = true end