build: rebuild Roblox bundle + worker (span slots, ImPlot rect pairs, greedy dirty-rect diff, per-command primitive data, table natives, trunc helpers)

This commit is contained in:
4DBug committed 2026-10-24 06:09:18 -05:00
1 parent 132660be9a
commit 3bac70663f
3 files changed
+1226 -445

No files matched your search

+1 -1
View File
@@ -1 +1 @@
[[49, "imgui.lua"], [22804, "imgui_demo.lua"], [34664, "imgui_draw.lua"], [43090, "imgui_h.lua"], [46100, "roblox/raster.lua"], [47382, "roblox/textures.lua"], [47633, "roblox/layers.lua"], [48055, "roblox/overlays.lua"], [48136, "roblox/render.lua"], [49295, "roblox/parallel.lua"], [49825, "roblox/patches.lua"], [50110, "imgui_impl_roblox.lua"], [50614, "imgui_impl_roblox_fonts.lua"], [57019, "imgui_internal.lua"], [60994, "imgui_tables.lua"], [66119, "imgui_widgets.lua"], [78507, "imstb_rectpack.lua"], [79184, "imstb_textedit.lua"], [80726, "imstb_truetype.lua"], [83687, "imstd_minstdio.lua"]] [[57, "imgui.lua"], [22812, "imgui_demo.lua"], [34672, "imgui_draw.lua"], [43097, "imgui_h.lua"], [46107, "roblox/raster.lua"], [47714, "roblox/textures.lua"], [47965, "roblox/layers.lua"], [48387, "roblox/overlays.lua"], [48468, "roblox/render.lua"], [49763, "roblox/parallel.lua"], [50295, "roblox/patches.lua"], [50580, "imgui_impl_roblox.lua"], [51084, "imgui_impl_roblox_fonts.lua"], [57489, "imgui_internal.lua"], [61474, "imgui_tables.lua"], [66602, "imgui_widgets.lua"], [78990, "imstb_rectpack.lua"], [79667, "imstb_textedit.lua"], [81209, "imstb_truetype.lua"], [84143, "imstd_minstdio.lua"]]
+741 -285
View File
File diff suppressed because it is too large. Load diff
+484 -159
View File
@@ -18,9 +18,9 @@ local readi32 = buffer.readi32
local M = { local M = {
TILE = 128, TILE = 128,
-- Packed jobs (parallel executor -> worker), per Actor and flush: P = primitive records, T = tile jobs. -- Packed jobs (parallel executor -> worker), per Actor and flush: P = primitive records, T = tile jobs.
-- P record (REC bytes): kind (0 triangle, 1 rect, 2/3 gradient kind 1/2, 4 line quad), tex id (-1: none), -- P record (REC bytes): kind (0 triangle, 1 rect, 2/3 gradient kind 1/2, 4 line quad, 5 triangle pair), tex id
-- clip x0 y0 x1 y1, bounds x0 y0 x1 y1 (screen pixels, i32), then raw vertices (VTX bytes: x y u v f64, -- (-1: none), clip x0 y0 x1 y1, bounds x0 y0 x1 y1 (screen pixels, i32), then raw vertices (VTX bytes: x y u v
-- col u32): a at REC_A, c at REC_C, b at REC_B, d at REC_D. -- f64, col u32): a at REC_A, c at REC_C, b at REC_B, d at REC_D.
-- T job: tox toy (f64), x0 y0 nr count (i32), nr x rect x0 y0 x1 y1 (i32, tile pixels), count x P offset (i32). -- T job: tox toy (f64), x0 y0 nr count (i32), nr x rect x0 y0 x1 y1 (i32, tile pixels), count x P offset (i32).
VTX = 36, REC = 184, REC_A = 40, REC_C = 76, REC_B = 112, REC_D = 148, JOB = 32, VTX = 36, REC = 184, REC_A = 40, REC_C = 76, REC_B = 112, REC_D = 148, JOB = 32,
} }
@@ -189,24 +189,30 @@ end
local function FillSpan(TargetBuffer: buffer, y0: number, y1: number, x0: number, x1: number, r: number, g: number, local function FillSpan(TargetBuffer: buffer, y0: number, y1: number, x0: number, x1: number, r: number, g: number,
b: number, a: number) b: number, a: number)
if a <= 0 then return end if a <= 0 then return end
if PX0 < PX1 then
ApplyDamage(PX0, PY0, PX1, PY1)
PX1 = PX0
end
local w4 = (x1 - x0 - 1) * 4 local w4 = (x1 - x0 - 1) * 4
local span = x1 - x0 local span = x1 - x0
if a >= 255 and span <= 8 then
-- opaque and narrow (vertical lines, histogram bars): writes (a call per row costs more), and the uniform rows
-- just lose the span (pending damage, merged with the neighbours' like any primitive's), no per-row bookkeeping:
-- a narrow span rarely becomes the larger uniform piece of its rows
local packed = floor(r + 0.5) + floor(g + 0.5) * 256 + floor(b + 0.5) * 65536 + 4278190080
local stride = TargetWidth * 4
local first = (y0 * TargetWidth + x0) * 4 + TargetBase
for o = first, first + (y1 - y0 - 1) * stride, stride do
for oo = o, o + w4, 4 do writeu32(TargetBuffer, oo, packed) end
end
Shrink(x0, y0, x1, y1)
return
end
if a >= 255 then if a >= 255 then
if PX0 < PX1 then
ApplyDamage(PX0, PY0, PX1, PY1)
PX1 = PX0
end
local packed = floor(r + 0.5) + floor(g + 0.5) * 256 + floor(b + 0.5) * 65536 + 4278190080 local packed = floor(r + 0.5) + floor(g + 0.5) * 256 + floor(b + 0.5) * 65536 + 4278190080
local first = (y0 * TargetWidth + x0) * 4 + TargetBase local first = (y0 * TargetWidth + x0) * 4 + TargetBase
FillRow(TargetBuffer, first, first + w4, packed, span >= 16 and PairF64(packed)) FillRow(TargetBuffer, first, first + w4, packed, span >= 16 and PairF64(packed))
if span <= 8 then -- narrow (vertical lines): writes, a call per row costs more CopyRows(TargetBuffer, first, w4 + 4, y1 - y0)
local stride = TargetWidth * 4
for o = first + stride, first + (y1 - y0 - 1) * stride, stride do
for oo = o, o + w4, 4 do writeu32(TargetBuffer, oo, packed) end
end
else
CopyRows(TargetBuffer, first, w4 + 4, y1 - y0)
end
-- now uniform: per row keep whichever piece is larger (the same for following rows with the same piece) -- now uniform: per row keep whichever piece is larger (the same for following rows with the same piece)
local y = y0 local y = y0
while y < y1 do while y < y1 do
@@ -236,6 +242,46 @@ local function FillSpan(TargetBuffer: buffer, y0: number, y1: number, x0: number
local rs, gs, bs = r * sa, g * sa, b * sa local rs, gs, bs = r * sa, g * sa, b * sa
local black = rs == 0 and gs == 0 and bs == 0 local black = rs == 0 and gs == 0 and bs == 0
local last_d, last_v = -1, 0 -- UI backgrounds are mostly flat: reuse the previous result for the same destination local last_d, last_v = -1, 0 -- UI backgrounds are mostly flat: reuse the previous result for the same destination
if span <= 4 then
-- narrow (vertical lines): blended per pixel (the uniform-row lookups per row cost more than the blend, which
-- is cached per destination value, the same result as the uniform path's); the rows just lose the span
-- (pending damage, like a narrow opaque span)
local stride = TargetWidth * 4
local first = (y0 * TargetWidth + x0) * 4 + TargetBase
-- rows x pixels; one pixel wide: one loop down the column
local olast, ilen, istep = first + (y1 - y0 - 1) * stride, w4, 4
if span == 1 then olast, ilen, istep = first, olast - first, stride end
for o0 = first, olast, stride do
for o = o0, o0 + ilen, istep do
local d = readu32(TargetBuffer, o)
if d == last_d then
writeu32(TargetBuffer, o, last_v)
elseif d < 16777216 then
writeu32(TargetBuffer, o, packed0)
else
last_d = d
local dr, dg, db = band(d, 255), band(rshift(d, 8), 255), band(rshift(d, 16), 255)
if d >= 4278190080 then
last_v = floor(rs + dr * ns + 0.5) + floor(gs + dg * ns + 0.5) * 256
+ floor(bs + db * ns + 0.5) * 65536 + 4278190080
else
local k = rshift(d, 24) * c1
local oa = sa + k
local inv = 1 / oa
last_v = floor((rs + dr * k) * inv + 0.5) + floor((gs + dg * k) * inv + 0.5) * 256
+ floor((bs + db * k) * inv + 0.5) * 65536 + floor(oa * 255 + 0.5) * 16777216
end
writeu32(TargetBuffer, o, last_v)
end
end
end
Shrink(x0, y0, x1, y1)
return
end
if PX0 < PX1 then
ApplyDamage(PX0, PY0, PX1, PY1)
PX1 = PX0
end
local prev_o, prev_v = 0, -1 -- previous row: filled with prev_v (-1: blended) local prev_o, prev_v = 0, -1 -- previous row: filled with prev_v (-1: blended)
local wide, has_prev = span >= 32 and y1 - y0 > 1, false local wide, has_prev = span >= 32 and y1 - y0 > 1, false
local y = y0 local y = y0
@@ -608,52 +654,78 @@ local function DrawTriangle(dbuf: buffer, vbuf: buffer, va: number, vb: number,
if not textured and (maxx - minx) * 2 < maxy - miny then if not textured and (maxx - minx) * 2 < maxy - miny then
-- tall sliver (vertical AA fringe): walk columns, same per-pixel edge and colour math as the rows below (each -- tall sliver (vertical AA fringe): walk columns, same per-pixel edge and colour math as the rows below (each
-- pixel blended once, so the order does not matter): no empty-row passes -- pixel blended once, so the order does not matter): no empty-row passes
local m0, m1, m2 = B0 ~= 0 and -1 / B0 or 0, B1 ~= 0 and -1 / B1 or 0, B2 ~= 0 and -1 / B2 or 0 -- (edge slots like the rows below, along y: B > 0 bounds a column from above, B < 0 from below; an edge with
-- B = 0 (vertical: w is the same on the whole column, B * py is 0) passes a run of columns: the column range)
local T1A, T1B, T1C, T1M, T1t, T2A, T2B, T2C, T2M, T2t = 0, 0, 1e9, -1, false, 0, 0, 1e9, -1, false
local U1A, U1B, U1C, U1M, U1t, U2A, U2B, U2C, U2M, U2t = 0, 0, 1e9, 1, false, 0, 0, 1e9, 1, false
local VA, VC, Vtl, Vz = 0, 1e9, false, false
local t1, u1 = false, false -- (slot 1 taken)
if B0 > 0 then
T1A, T1B, T1C, T1M, T1t, t1 = A0, B0, C0, -1 / B0, tl0, true
elseif B0 < 0 then
U1A, U1B, U1C, U1M, U1t, u1 = A0, B0, C0, -1 / B0, tl0, true
else
VA, VC, Vtl, Vz = A0, C0, tl0, z0
end
if B1 > 0 then
if not t1 then
T1A, T1B, T1C, T1M, T1t, t1 = A1, B1, C1, -1 / B1, tl1, true
else
T2A, T2B, T2C, T2M, T2t = A1, B1, C1, -1 / B1, tl1
end
elseif B1 < 0 then
if not u1 then
U1A, U1B, U1C, U1M, U1t, u1 = A1, B1, C1, -1 / B1, tl1, true
else
U2A, U2B, U2C, U2M, U2t = A1, B1, C1, -1 / B1, tl1
end
else
VA, VC, Vtl, Vz = A1, C1, tl1, z1
end
if B2 > 0 then
if not t1 then
T1A, T1B, T1C, T1M, T1t, t1 = A2, B2, C2, -1 / B2, tl2, true
else
T2A, T2B, T2C, T2M, T2t = A2, B2, C2, -1 / B2, tl2
end
elseif B2 < 0 then
if not u1 then
U1A, U1B, U1C, U1M, U1t, u1 = A2, B2, C2, -1 / B2, tl2, true
else
U2A, U2B, U2C, U2M, U2t = A2, B2, C2, -1 / B2, tl2
end
else
VA, VC, Vtl, Vz = A2, C2, tl2, z2
end
if VA ~= 0 then
local py = miny + 0.5
while minx <= maxx do
local w = VA * (minx + 0.5) + (0 * py + VC)
if (w > 0 or (w == 0 and Vtl)) and not (w == 0 and Vz) then break end
minx += 1
end
while maxx > minx do
local w = VA * (maxx + 0.5) + (0 * py + VC)
if (w > 0 or (w == 0 and Vtl)) and not (w == 0 and Vz) then break end
maxx -= 1
end
end
local stride = TargetWidth * 4 local stride = TargetWidth * 4
for x = minx, maxx do for x = minx, maxx do
local px = x + 0.5 local px = x + 0.5
local yt, yb = miny, maxy local f1, f2, g1, g2 = T1A * px + T1C, T2A * px + T2C, U1A * px + U1C, U2A * px + U2C
local f0, f1, f2 = A0 * px + C0, A1 * px + C1, A2 * px + C2 local yt = max(miny, floor(f1 * T1M - 0.500001), floor(f2 * T2M - 0.500001))
-- (a vertical edge, B = 0: w is the same on the whole column (B * py is 0), so test it once: an AA fringe's local yb = min(maxy, ceil(g1 * U1M - 0.499999), ceil(g2 * U2M - 0.499999))
-- column on the wrong side would else be scanned row by row)
if B0 > 0 then
yt = max(yt, floor(f0 * m0 - 0.500001))
elseif B0 < 0 then
yb = min(yb, ceil(f0 * m0 - 0.499999))
else
local w = A0 * px + (B0 * (yt + 0.5) + C0)
if not (w > 0 or (w == 0 and tl0)) or (w == 0 and z0) then continue end
end
if B1 > 0 then
yt = max(yt, floor(f1 * m1 - 0.500001))
elseif B1 < 0 then
yb = min(yb, ceil(f1 * m1 - 0.499999))
else
local w = A1 * px + (B1 * (yt + 0.5) + C1)
if not (w > 0 or (w == 0 and tl1)) or (w == 0 and z1) then continue end
end
if B2 > 0 then
yt = max(yt, floor(f2 * m2 - 0.500001))
elseif B2 < 0 then
yb = min(yb, ceil(f2 * m2 - 0.499999))
else
local w = A2 * px + (B2 * (yt + 0.5) + C2)
if not (w > 0 or (w == 0 and tl2)) or (w == 0 and z2) then continue end
end
while yt <= yb do while yt <= yb do
local py = yt + 0.5 local py = yt + 0.5
local w0, w1, w2 = A0 * px + (B0 * py + C0), A1 * px + (B1 * py + C1), A2 * px + (B2 * py + C2) local w1, w2 = T1A * px + (T1B * py + T1C), T2A * px + (T2B * py + T2C)
if (w0 > 0 or (w0 == 0 and tl0)) and (w1 > 0 or (w1 == 0 and tl1)) and (w2 > 0 or (w2 == 0 and tl2)) then if (w1 > 0 or (w1 == 0 and T1t)) and (w2 > 0 or (w2 == 0 and T2t)) then break end
break
end
yt += 1 yt += 1
end end
while yb > yt do while yb >= yt do
local py = yb + 0.5 local py = yb + 0.5
local w0, w1, w2 = A0 * px + (B0 * py + C0), A1 * px + (B1 * py + C1), A2 * px + (B2 * py + C2) local w1, w2 = U1A * px + (U1B * py + U1C), U2A * px + (U2B * py + U2C)
if (w0 > 0 or (w0 == 0 and tl0)) and (w1 > 0 or (w1 == 0 and tl1)) and (w2 > 0 or (w2 == 0 and tl2)) then if (w1 > 0 or (w1 == 0 and U1t)) and (w2 > 0 or (w2 == 0 and U2t)) then break end
break
end
yb -= 1 yb -= 1
end end
if yt > yb then continue end if yt > yb then continue end
@@ -742,56 +814,69 @@ local function DrawTriangle(dbuf: buffer, vbuf: buffer, va: number, vb: number,
return return
end end
-- Row spans. Each edge bounds a row from one side: w is monotone in x (A * px + e rounds monotonically), so the
-- pixels passing an edge are a run open to one side (A > 0: to the right, top-left so w >= 0 passes; A < 0: to
-- the left, w > 0), and the span is [first pixel passing the left edges, last passing the right ones]. Edges go
-- in slots per side (L, R; a spare slot is a dummy edge with w = 1e9, never binding), so a row is an estimate and
-- a short search per side, without per-edge branches. A horizontal edge (A = 0: w is e on the whole row) passes
-- a run of rows instead (e is monotone in y): the row range.
local L1A, L1B, L1C, L1N, L2A, L2B, L2C, L2N = 0, 0, 1e9, -1, 0, 0, 1e9, -1
local R1A, R1B, R1C, R1N, R2A, R2B, R2C, R2N = 0, 0, 1e9, 1, 0, 0, 1e9, 1
local HB, HC, Htl, Hz = 0, 1e9, false, false
local l1, r1 = false, false -- (slot 1 taken)
if A0 > 0 then
L1A, L1B, L1C, L1N, l1 = A0, B0, C0, n0, true
elseif A0 < 0 then
R1A, R1B, R1C, R1N, r1 = A0, B0, C0, n0, true
else
HB, HC, Htl, Hz = B0, C0, tl0, z0
end
if A1 > 0 then
if not l1 then L1A, L1B, L1C, L1N, l1 = A1, B1, C1, n1, true else L2A, L2B, L2C, L2N = A1, B1, C1, n1 end
elseif A1 < 0 then
if not r1 then R1A, R1B, R1C, R1N, r1 = A1, B1, C1, n1, true else R2A, R2B, R2C, R2N = A1, B1, C1, n1 end
else
HB, HC, Htl, Hz = B1, C1, tl1, z1
end
if A2 > 0 then
if not l1 then L1A, L1B, L1C, L1N, l1 = A2, B2, C2, n2, true else L2A, L2B, L2C, L2N = A2, B2, C2, n2 end
elseif A2 < 0 then
if not r1 then R1A, R1B, R1C, R1N, r1 = A2, B2, C2, n2, true else R2A, R2B, R2C, R2N = A2, B2, C2, n2 end
else
HB, HC, Htl, Hz = B2, C2, tl2, z2
end
if HB ~= 0 then
while miny <= maxy do
local e = HB * (miny + 0.5) + HC
if (e > 0 or (e == 0 and Htl)) and not (e == 0 and Hz) then break end
miny += 1
end
while maxy > miny do
local e = HB * (maxy + 0.5) + HC
if (e > 0 or (e == 0 and Htl)) and not (e == 0 and Hz) then break end
maxy -= 1
end
end
for y = miny, maxy do for y = miny, maxy do
local py = y + 0.5 local py = y + 0.5
local xl, xr = minx, maxx
local e0, e1, e2 = B0 * py + C0, B1 * py + C1, B2 * py + C2 local e0, e1, e2 = B0 * py + C0, B1 * py + C1, B2 * py + C2
local f1, f2, g1, g2 = L1B * py + L1C, L2B * py + L2C, R1B * py + R1C, R2B * py + R2C
-- (e * n is -e / A within an ulp; the 1e-6 margin covers that, and the exact tests below decide) -- (e * n is -e / A within an ulp; the 1e-6 margin covers that, and the exact tests below decide)
-- (a horizontal edge, A = 0: w is e on the whole row, tested once, like the columns above) local xl = max(minx, floor(f1 * L1N - 0.500001), floor(f2 * L2N - 0.500001))
if A0 > 0 then local xr = min(maxx, ceil(g1 * R1N - 0.499999), ceil(g2 * R2N - 0.499999))
xl = max(xl, floor(e0 * n0 - 0.500001))
elseif A0 < 0 then
xr = min(xr, ceil(e0 * n0 - 0.499999))
else
local w = A0 * (xl + 0.5) + e0
if not (w > 0 or (w == 0 and tl0)) or (w == 0 and z0) then continue end
end
if A1 > 0 then
xl = max(xl, floor(e1 * n1 - 0.500001))
elseif A1 < 0 then
xr = min(xr, ceil(e1 * n1 - 0.499999))
else
local w = A1 * (xl + 0.5) + e1
if not (w > 0 or (w == 0 and tl1)) or (w == 0 and z1) then continue end
end
if A2 > 0 then
xl = max(xl, floor(e2 * n2 - 0.500001))
elseif A2 < 0 then
xr = min(xr, ceil(e2 * n2 - 0.499999))
else
local w = A2 * (xl + 0.5) + e2
if not (w > 0 or (w == 0 and tl2)) or (w == 0 and z2) then continue end
end
-- An estimate inside (minx, maxx) is never in the span: the pixel centre is >= 1e-6 outside the crossing it -- An estimate inside (minx, maxx) is never in the span: the pixel centre is >= 1e-6 outside the crossing it
-- estimates, and with |A * px| and |e| below 2^17 * |A| the test's rounding error is far below 1e-6 * |A| (the -- estimates, and with |A * px| and |e| below 2^17 * |A| the test's rounding error is far below 1e-6 * |A| (the
-- test reads the same e). Testing from the next pixel gives the same span. -- test reads the same e). Testing from the next pixel gives the same span.
if xl > minx and xl <= xr then xl += 1 end if xl > minx and xl <= xr then xl += 1 end
while xl <= xr do while xl <= xr do
local px = xl + 0.5 local px = xl + 0.5
local w0, w1, w2 = A0 * px + e0, A1 * px + e1, A2 * px + e2 if L1A * px + f1 >= 0 and L2A * px + f2 >= 0 then break end
if (w0 > 0 or (w0 == 0 and tl0)) and (w1 > 0 or (w1 == 0 and tl1)) and (w2 > 0 or (w2 == 0 and tl2)) then
break
end
xl += 1 xl += 1
end end
if xr < maxx and xr > xl then xr -= 1 end if xr < maxx and xr > xl then xr -= 1 end
while xr > xl do while xr >= xl do
local px = xr + 0.5 local px = xr + 0.5
local w0, w1, w2 = A0 * px + e0, A1 * px + e1, A2 * px + e2 if R1A * px + g1 > 0 and R2A * px + g2 > 0 then break end
if (w0 > 0 or (w0 == 0 and tl0)) and (w1 > 0 or (w1 == 0 and tl1)) and (w2 > 0 or (w2 == 0 and tl2)) then
break
end
xr -= 1 xr -= 1
end end
if xl > xr then continue end if xl > xr then continue end
@@ -923,6 +1008,212 @@ local function DrawTriangle(dbuf: buffer, vbuf: buffer, va: number, vb: number,
end end
end end
-- One triangle's row walk as DrawTriangle sets it up (same edges, orientation, edge slots and row range; see its rows):
-- clipped bounds, then the L1 L2 R1 R2 slots (A, B, C, n). For flat triangles only (no alpha-0 edge rows).
@native
local function TriangleSlots(x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, cx0: number,
cy0: number, cx1: number, cy1: number)
local area = (x1 - x0) * (y2 - y0) - (y1 - y0) * (x2 - x0)
if area < 0 then
x1, y1, x2, y2 = x2, y2, x1, y1
end
local minx, maxx = max(floor(min(x0, x1, x2)), cx0), min(ceil(max(x0, x1, x2)), cx1 - 1)
local miny, maxy = max(floor(min(y0, y1, y2)), cy0), min(ceil(max(y0, y1, y2)), cy1 - 1)
local A0, B0 = y1 - y2, x2 - x1
local C0 = -(A0 * x1 + B0 * y1)
local A1, B1 = y2 - y0, x0 - x2
local C1 = -(A1 * x2 + B1 * y2)
local A2, B2 = y0 - y1, x1 - x0
local C2 = -(A2 * x0 + B2 * y0)
local L1A, L1B, L1C, L1N, L2A, L2B, L2C, L2N = 0, 0, 1e9, -1, 0, 0, 1e9, -1
local R1A, R1B, R1C, R1N, R2A, R2B, R2C, R2N = 0, 0, 1e9, 1, 0, 0, 1e9, 1
local HB, HC, Htl = 0, 1e9, false
local l1, r1 = false, false
if A0 > 0 then
L1A, L1B, L1C, L1N, l1 = A0, B0, C0, -1 / A0, true
elseif A0 < 0 then
R1A, R1B, R1C, R1N, r1 = A0, B0, C0, -1 / A0, true
else
HB, HC, Htl = B0, C0, B0 > 0
end
if A1 > 0 then
if not l1 then L1A, L1B, L1C, L1N, l1 = A1, B1, C1, -1 / A1, true else L2A, L2B, L2C, L2N = A1, B1, C1, -1 / A1 end
elseif A1 < 0 then
if not r1 then R1A, R1B, R1C, R1N, r1 = A1, B1, C1, -1 / A1, true else R2A, R2B, R2C, R2N = A1, B1, C1, -1 / A1 end
else
HB, HC, Htl = B1, C1, B1 > 0
end
if A2 > 0 then
if not l1 then L1A, L1B, L1C, L1N, l1 = A2, B2, C2, -1 / A2, true else L2A, L2B, L2C, L2N = A2, B2, C2, -1 / A2 end
elseif A2 < 0 then
if not r1 then R1A, R1B, R1C, R1N, r1 = A2, B2, C2, -1 / A2, true else R2A, R2B, R2C, R2N = A2, B2, C2, -1 / A2 end
else
HB, HC, Htl = B2, C2, B2 > 0
end
if HB ~= 0 and minx <= maxx then
while miny <= maxy do
local e = HB * (miny + 0.5) + HC
if e > 0 or (e == 0 and Htl) then break end
miny += 1
end
while maxy > miny do
local e = HB * (maxy + 0.5) + HC
if e > 0 or (e == 0 and Htl) then break end
maxy -= 1
end
end
return minx, maxx, miny, maxy, L1A, L1B, L1C, L1N, L2A, L2B, L2C, L2N, R1A, R1B, R1C, R1N, R2A, R2B, R2C, R2N
end
-- Flat triangle pair a b c + a b d, one colour, constant uv (ImPlot PrimRectFill: heatmap cells, bars). The same pixels
-- and values as the two DrawTriangle calls: per row each triangle's span exactly as DrawTriangle computes it (a flat
-- triangle's pixels are the same whether walked by rows or columns: the same per-pixel edge tests), then one fill when
-- the spans touch (opaque: or overlap; a pixel inside both gets the same value twice), else each span in order (a
-- translucent pixel inside both is blended twice, first for a b c, as before). Saves the second triangle's setup, a
-- span fill per row, and the column walk of tall bars.
@native
local function DrawTriPair(dbuf: buffer, vbuf: buffer, va: number, vb: number, vc: number, vd: number, t, cx0: number,
cy0: number, cx1: number, cy1: number, ox: number, oy: number)
local ax, ay = readf64(vbuf, va) - ox, readf64(vbuf, va + 8) - oy
local bx, by = readf64(vbuf, vb) - ox, readf64(vbuf, vb + 8) - oy
local cx, cy = readf64(vbuf, vc) - ox, readf64(vbuf, vc + 8) - oy
local dx, dy = readf64(vbuf, vd) - ox, readf64(vbuf, vd + 8) - oy
-- (DrawTriangle walks a tall triangle by columns, whose span estimates may differ from the rows' for an almost
-- vertical edge: then the two calls, like a degenerate triangle)
if (bx - ax) * (cy - ay) - (by - ay) * (cx - ax) == 0 or (bx - ax) * (dy - ay) - (by - ay) * (dx - ax) == 0
or (min(ceil(max(ax, bx, cx)), cx1 - 1) - max(floor(min(ax, bx, cx)), cx0)) * 2
< min(ceil(max(ay, by, cy)), cy1 - 1) - max(floor(min(ay, by, cy)), cy0)
or (min(ceil(max(ax, bx, dx)), cx1 - 1) - max(floor(min(ax, bx, dx)), cx0)) * 2
< min(ceil(max(ay, by, dy)), cy1 - 1) - max(floor(min(ay, by, dy)), cy0) then
DrawTriangle(dbuf, vbuf, va, vb, vc, t, cx0, cy0, cx1, cy1, ox, oy)
DrawTriangle(dbuf, vbuf, va, vb, vd, t, cx0, cy0, cx1, cy1, ox, oy)
return
end
-- DrawTriangle's flat colour (the texel folded in like there)
local col = readu32(vbuf, va + 32)
local ra, ga, ba, aa = band(col, 255), band(rshift(col, 8), 255), band(rshift(col, 16), 255), rshift(col, 24)
if t then
local tw0, th0 = t.Width, t.Height
local tx, ty = floor(readf64(vbuf, va + 16) * tw0), floor(readf64(vbuf, va + 24) * th0)
if tx < 0 then tx = 0 elseif tx >= tw0 then tx = tw0 - 1 end
if ty < 0 then ty = 0 elseif ty >= th0 then ty = th0 - 1 end
local to, tb0 = (ty * tw0 + tx) * 4, t.Buffer
if readu32(tb0, to) ~= 4294967295 then
local tr, tg, tb, ta = readu8(tb0, to), readu8(tb0, to + 1), readu8(tb0, to + 2), readu8(tb0, to + 3)
ra, ga, ba, aa = ra * tr / 255, ga * tg / 255, ba * tb / 255, aa * ta / 255
end
end
if aa <= 0 then return end
local f_opaque = aa >= 255
local f_packed = floor(ra + 0.5) + floor(ga + 0.5) * 256 + floor(ba + 0.5) * 65536
+ (f_opaque and 4278190080 or floor(aa + 0.5) * 16777216)
local f_pair: number | false = f_opaque and PairF64(f_packed)
local f_sa, f_ns, f_c1, f_rs, f_gs, f_bs = 0, 0, 0, 0, 0, 0
if not f_opaque then
f_sa = aa / 255
f_ns = 1 - f_sa
f_c1 = f_ns / 255
f_rs, f_gs, f_bs = ra * f_sa, ga * f_sa, ba * f_sa
end
local minx1, maxx1, miny1, maxy1, P1A, P1B, P1C, P1N, P2A, P2B, P2C, P2N, Q1A, Q1B, Q1C, Q1N, Q2A, Q2B, Q2C, Q2N =
TriangleSlots(ax, ay, bx, by, cx, cy, cx0, cy0, cx1, cy1)
local minx2, maxx2, miny2, maxy2, S1A, S1B, S1C, S1N, S2A, S2B, S2C, S2N, T1A, T1B, T1C, T1N, T2A, T2B, T2C, T2N =
TriangleSlots(ax, ay, bx, by, dx, dy, cx0, cy0, cx1, cy1)
if minx1 > maxx1 then miny1 = maxy1 + 1 end -- (empty)
if minx2 > maxx2 then miny2 = maxy2 + 1 end
local ylo, yhi = miny1, maxy1
if miny1 > maxy1 then
ylo, yhi = miny2, maxy2
elseif miny2 <= maxy2 then
ylo, yhi = min(miny1, miny2), max(maxy1, maxy2)
end
if ylo > yhi then return end
Shrink(min(minx1, minx2), ylo, max(maxx1, maxx2) + 1, yhi + 1)
for y = ylo, yhi do
local py = y + 0.5
local l1, r1, l2, r2 = 0, -1, 0, -1
if y >= miny1 and y <= maxy1 then
local f1, f2, g1, g2 = P1B * py + P1C, P2B * py + P2C, Q1B * py + Q1C, Q2B * py + Q2C
local xl = max(minx1, floor(f1 * P1N - 0.500001), floor(f2 * P2N - 0.500001))
local xr = min(maxx1, ceil(g1 * Q1N - 0.499999), ceil(g2 * Q2N - 0.499999))
if xl > minx1 and xl <= xr then xl += 1 end
while xl <= xr do
local px = xl + 0.5
if P1A * px + f1 >= 0 and P2A * px + f2 >= 0 then break end
xl += 1
end
if xr < maxx1 and xr > xl then xr -= 1 end
while xr >= xl do
local px = xr + 0.5
if Q1A * px + g1 > 0 and Q2A * px + g2 > 0 then break end
xr -= 1
end
l1, r1 = xl, xr
end
if y >= miny2 and y <= maxy2 then
local f1, f2, g1, g2 = S1B * py + S1C, S2B * py + S2C, T1B * py + T1C, T2B * py + T2C
local xl = max(minx2, floor(f1 * S1N - 0.500001), floor(f2 * S2N - 0.500001))
local xr = min(maxx2, ceil(g1 * T1N - 0.499999), ceil(g2 * T2N - 0.499999))
if xl > minx2 and xl <= xr then xl += 1 end
while xl <= xr do
local px = xl + 0.5
if S1A * px + f1 >= 0 and S2A * px + f2 >= 0 then break end
xl += 1
end
if xr < maxx2 and xr > xl then xr -= 1 end
while xr >= xl do
local px = xr + 0.5
if T1A * px + g1 > 0 and T2A * px + g2 > 0 then break end
xr -= 1
end
l2, r2 = xl, xr
end
if l1 > r1 then
if l2 > r2 then continue end
l1, r1, l2, r2 = l2, r2, 0, -1
elseif l2 <= r2 and (l2 == r1 + 1 or l1 == r2 + 1 or (f_opaque and l2 <= r1 and l1 <= r2)) then
l1, r1, l2, r2 = min(l1, l2), max(r1, r2), 0, -1
end
local row = y * TargetWidth * 4 + TargetBase
if f_opaque then
FillRow(dbuf, row + l1 * 4, row + r1 * 4, f_packed, f_pair)
if l2 <= r2 then FillRow(dbuf, row + l2 * 4, row + r2 * 4, f_packed, f_pair) end
continue
end
-- DrawTriangle's flat translucent span (same math, cached per destination value), per span in order
local last_d, last_v = -1, 0
for k = 1, 2 do
if k == 2 then
if l2 > r2 then break end
l1, r1 = l2, r2
end
for oo = row + l1 * 4, row + r1 * 4, 4 do
local d = readu32(dbuf, oo)
if d == last_d then
writeu32(dbuf, oo, last_v)
elseif d < 16777216 then
writeu32(dbuf, oo, f_packed)
else
last_d = d
local dr, dg, db = band(d, 255), band(rshift(d, 8), 255), band(rshift(d, 16), 255)
if d >= 4278190080 then
last_v = floor(f_rs + dr * f_ns + 0.5) + floor(f_gs + dg * f_ns + 0.5) * 256
+ floor(f_bs + db * f_ns + 0.5) * 65536 + 4278190080
else
local kk = rshift(d, 24) * f_c1
local oa = f_sa + kk
local inv = 1 / oa
last_v = floor((f_rs + dr * kk) * inv + 0.5) + floor((f_gs + dg * kk) * inv + 0.5) * 256
+ floor((f_bs + db * kk) * inv + 0.5) * 65536 + floor(oa * 255 + 0.5) * 16777216
end
writeu32(dbuf, oo, last_v)
end
end
end
end
end
-- Textured line quad (triangles a b c + a c d, one colour, uv a = b and c = d: ImPlot PrimLine). Same edge functions as -- Textured line quad (triangles a b c + a c d, one colour, uv a = b and c = d: ImPlot PrimLine). Same edge functions as
-- the two DrawTriangle calls (so the same pixels), uv from triangle a b c's barycentrics over the whole quad. -- the two DrawTriangle calls (so the same pixels), uv from triangle a b c's barycentrics over the whole quad.
@native @native
@@ -985,58 +1276,93 @@ local function DrawLineQuad(dbuf: buffer, vbuf: buffer, va: number, vb: number,
trow = ty * tw * 4 trow = ty * tw * 4
end end
local r255, g255, b255, a255 = ra / 255, ga / 255, ba / 255, aa / 255 local r255, g255, b255, a255 = ra / 255, ga / 255, ba / 255, aa / 255
local last_tv, sa, sr, sg, sb, s_opaque, fa, fn, fk = -1, 0, 0, 0, 0, 0, 0, 0, 0 -- (edge slots and row range like DrawTriangle's rows; up to 3 edges on a side, 2 horizontal)
local L1A, L1B, L1C, L1N, L2A, L2B, L2C, L2N, L3A, L3B, L3C, L3N = 0, 0, 1e9, -1, 0, 0, 1e9, -1, 0, 0, 1e9, -1
local R1A, R1B, R1C, R1N, R2A, R2B, R2C, R2N, R3A, R3B, R3C, R3N = 0, 0, 1e9, 1, 0, 0, 1e9, 1, 0, 0, 1e9, 1
local H1B, H1C, H1t, H2B, H2C, H2t = 0, 1e9, false, 0, 1e9, false
local nl, nr = 0, 0
if A0 > 0 then
if nl == 0 then L1A, L1B, L1C, L1N = A0, B0, C0, n0 elseif nl == 1 then L2A, L2B, L2C, L2N = A0, B0, C0, n0
else L3A, L3B, L3C, L3N = A0, B0, C0, n0 end
nl += 1
elseif A0 < 0 then
if nr == 0 then R1A, R1B, R1C, R1N = A0, B0, C0, n0 elseif nr == 1 then R2A, R2B, R2C, R2N = A0, B0, C0, n0
else R3A, R3B, R3C, R3N = A0, B0, C0, n0 end
nr += 1
elseif H1B == 0 then
H1B, H1C, H1t = B0, C0, tl0
else
H2B, H2C, H2t = B0, C0, tl0
end
if A1 > 0 then
if nl == 0 then L1A, L1B, L1C, L1N = A1, B1, C1, n1 elseif nl == 1 then L2A, L2B, L2C, L2N = A1, B1, C1, n1
else L3A, L3B, L3C, L3N = A1, B1, C1, n1 end
nl += 1
elseif A1 < 0 then
if nr == 0 then R1A, R1B, R1C, R1N = A1, B1, C1, n1 elseif nr == 1 then R2A, R2B, R2C, R2N = A1, B1, C1, n1
else R3A, R3B, R3C, R3N = A1, B1, C1, n1 end
nr += 1
elseif H1B == 0 then
H1B, H1C, H1t = B1, C1, tl1
else
H2B, H2C, H2t = B1, C1, tl1
end
if A2 > 0 then
if nl == 0 then L1A, L1B, L1C, L1N = A2, B2, C2, n2 elseif nl == 1 then L2A, L2B, L2C, L2N = A2, B2, C2, n2
else L3A, L3B, L3C, L3N = A2, B2, C2, n2 end
nl += 1
elseif A2 < 0 then
if nr == 0 then R1A, R1B, R1C, R1N = A2, B2, C2, n2 elseif nr == 1 then R2A, R2B, R2C, R2N = A2, B2, C2, n2
else R3A, R3B, R3C, R3N = A2, B2, C2, n2 end
nr += 1
elseif H1B == 0 then
H1B, H1C, H1t = B2, C2, tl2
else
H2B, H2C, H2t = B2, C2, tl2
end
if A3 > 0 then
if nl == 0 then L1A, L1B, L1C, L1N = A3, B3, C3, n3 elseif nl == 1 then L2A, L2B, L2C, L2N = A3, B3, C3, n3
else L3A, L3B, L3C, L3N = A3, B3, C3, n3 end
nl += 1
elseif A3 < 0 then
if nr == 0 then R1A, R1B, R1C, R1N = A3, B3, C3, n3 elseif nr == 1 then R2A, R2B, R2C, R2N = A3, B3, C3, n3
else R3A, R3B, R3C, R3N = A3, B3, C3, n3 end
nr += 1
elseif H1B == 0 then
H1B, H1C, H1t = B3, C3, tl3
else
H2B, H2C, H2t = B3, C3, tl3
end
if H1B ~= 0 then
while miny <= maxy do
local py = miny + 0.5
local e, e2 = H1B * py + H1C, H2B * py + H2C
if (e > 0 or (e == 0 and H1t)) and (e2 > 0 or (e2 == 0 and H2t)) then break end
miny += 1
end
while maxy > miny do
local py = maxy + 0.5
local e, e2 = H1B * py + H1C, H2B * py + H2C
if (e > 0 or (e == 0 and H1t)) and (e2 > 0 or (e2 == 0 and H2t)) then break end
maxy -= 1
end
end
for y = miny, maxy do for y = miny, maxy do
local py = y + 0.5 local py = y + 0.5
local xl, xr = minx, maxx local f1, f2, f3 = L1B * py + L1C, L2B * py + L2C, L3B * py + L3C
local e0, e1, e2, e3 = B0 * py + C0, B1 * py + C1, B2 * py + C2, B3 * py + C3 local g1, g2, g3 = R1B * py + R1C, R2B * py + R2C, R3B * py + R3C
-- (A = 0: tested once per row, see DrawTriangle) local xl = max(minx, floor(f1 * L1N - 0.500001), floor(f2 * L2N - 0.500001), floor(f3 * L3N - 0.500001))
if A0 > 0 then local xr = min(maxx, ceil(g1 * R1N - 0.499999), ceil(g2 * R2N - 0.499999), ceil(g3 * R3N - 0.499999))
xl = max(xl, floor(e0 * n0 - 0.500001))
elseif A0 < 0 then
xr = min(xr, ceil(e0 * n0 - 0.499999))
elseif not (e0 > 0 or (e0 == 0 and tl0)) then
continue
end
if A1 > 0 then
xl = max(xl, floor(e1 * n1 - 0.500001))
elseif A1 < 0 then
xr = min(xr, ceil(e1 * n1 - 0.499999))
elseif not (e1 > 0 or (e1 == 0 and tl1)) then
continue
end
if A2 > 0 then
xl = max(xl, floor(e2 * n2 - 0.500001))
elseif A2 < 0 then
xr = min(xr, ceil(e2 * n2 - 0.499999))
elseif not (e2 > 0 or (e2 == 0 and tl2)) then
continue
end
if A3 > 0 then
xl = max(xl, floor(e3 * n3 - 0.500001))
elseif A3 < 0 then
xr = min(xr, ceil(e3 * n3 - 0.499999))
elseif not (e3 > 0 or (e3 == 0 and tl3)) then
continue
end
if xl > minx and xl <= xr then xl += 1 end -- (an estimate is never in the span: see DrawTriangle) if xl > minx and xl <= xr then xl += 1 end -- (an estimate is never in the span: see DrawTriangle)
while xl <= xr do while xl <= xr do
local px = xl + 0.5 local px = xl + 0.5
local w0, w1, w2, w3 = A0 * px + e0, A1 * px + e1, A2 * px + e2, A3 * px + e3 if L1A * px + f1 >= 0 and L2A * px + f2 >= 0 and L3A * px + f3 >= 0 then break end
if (w0 > 0 or (w0 == 0 and tl0)) and (w1 > 0 or (w1 == 0 and tl1)) and (w2 > 0 or (w2 == 0 and tl2))
and (w3 > 0 or (w3 == 0 and tl3)) then
break
end
xl += 1 xl += 1
end end
if xr < maxx and xr > xl then xr -= 1 end if xr < maxx and xr > xl then xr -= 1 end
while xr > xl do while xr >= xl do
local px = xr + 0.5 local px = xr + 0.5
local w0, w1, w2, w3 = A0 * px + e0, A1 * px + e1, A2 * px + e2, A3 * px + e3 if R1A * px + g1 > 0 and R2A * px + g2 > 0 and R3A * px + g3 > 0 then break end
if (w0 > 0 or (w0 == 0 and tl0)) and (w1 > 0 or (w1 == 0 and tl1)) and (w2 > 0 or (w2 == 0 and tl2))
and (w3 > 0 or (w3 == 0 and tl3)) then
break
end
xr -= 1 xr -= 1
end end
if xl > xr then continue end if xl > xr then continue end
@@ -1054,35 +1380,30 @@ local function DrawLineQuad(dbuf: buffer, vbuf: buffer, va: number, vb: number,
if ty < 0 then ty = 0 elseif ty >= th then ty = th - 1 end if ty < 0 then ty = 0 elseif ty >= th then ty = th - 1 end
tv = readu32(tbuf, (ty * tw + tx) * 4) tv = readu32(tbuf, (ty * tw + tx) * 4)
end end
if tv ~= last_tv then -- BlendPixel inlined (same math; an opaque texel over a colour gives fa = 1, fn = 0, k = 0: the same values
last_tv = tv -- as its opaque branch, so no branch on it: every unpredictable branch costs more than the math)
sa = rshift(tv, 24) * a255 local ta = rshift(tv, 24)
sr, sg, sb = band(tv, 255) * r255, band(rshift(tv, 8), 255) * g255, band(rshift(tv, 16), 255) * b255 if ta > 0 then
s_opaque = floor(sr + 0.5) + floor(sg + 0.5) * 256 + floor(sb + 0.5) * 65536 local sa = ta * a255
fa = sa / 255 local sr, sg, sb = band(tv, 255) * r255, band(rshift(tv, 8), 255) * g255, band(rshift(tv, 16), 255) * b255
fn = 1 - fa local fa = sa / 255
fk = (1 - fa) / 255
end
-- BlendPixel inlined (same math)
if sa >= 255 then
writeu32(dbuf, o, s_opaque + 4278190080)
elseif sa > 0 then
local d = readu32(dbuf, o) local d = readu32(dbuf, o)
if d < 16777216 then if d >= 4278190080 then
writeu32(dbuf, o, s_opaque + floor(sa + 0.5) * 16777216) local fn = 1 - fa
writeu32(dbuf, o, floor(sr * fa + band(d, 255) * fn + 0.5)
+ floor(sg * fa + band(rshift(d, 8), 255) * fn + 0.5) * 256
+ floor(sb * fa + band(rshift(d, 16), 255) * fn + 0.5) * 65536 + 4278190080)
elseif d < 16777216 then
writeu32(dbuf, o, floor(sr + 0.5) + floor(sg + 0.5) * 256 + floor(sb + 0.5) * 65536
+ floor(sa + 0.5) * 16777216)
else else
local dr, dg, db = band(d, 255), band(rshift(d, 8), 255), band(rshift(d, 16), 255) local dr, dg, db = band(d, 255), band(rshift(d, 8), 255), band(rshift(d, 16), 255)
if d >= 4278190080 then local k = rshift(d, 24) * ((1 - fa) / 255)
writeu32(dbuf, o, floor(sr * fa + dr * fn + 0.5) + floor(sg * fa + dg * fn + 0.5) * 256 local oa = fa + k
+ floor(sb * fa + db * fn + 0.5) * 65536 + 4278190080) local inv2 = 1 / oa
else writeu32(dbuf, o, floor((sr * fa + dr * k) * inv2 + 0.5)
local k = rshift(d, 24) * fk + floor((sg * fa + dg * k) * inv2 + 0.5) * 256
local oa = fa + k + floor((sb * fa + db * k) * inv2 + 0.5) * 65536 + floor(oa * 255 + 0.5) * 16777216)
local inv2 = 1 / oa
writeu32(dbuf, o, floor((sr * fa + dr * k) * inv2 + 0.5)
+ floor((sg * fa + dg * k) * inv2 + 0.5) * 256
+ floor((sb * fa + db * k) * inv2 + 0.5) * 65536 + floor(oa * 255 + 0.5) * 16777216)
end
end end
end end
o += 4 o += 4
@@ -1181,7 +1502,8 @@ function M.ClearRect(rx0: number, ry0: number, rx1: number, ry1: number)
end end
end end
-- kind: 0 triangle (va vb vc), 1 rect (TL va, BR vc), 2/3 gradient kind 1/2, 4 line quad (va vb vc vd) -- kind: 0 triangle (va vb vc), 1 rect (TL va, BR vc), 2/3 gradient kind 1/2, 4 line quad (va vb vc vd), 5 flat
-- triangle pair (va vb vc + va vb vd)
@native @native
local function DrawPrim(kind: number, t, vbuf: buffer, va: number, vb: number, vc: number, vd: number, tox: number, local function DrawPrim(kind: number, t, vbuf: buffer, va: number, vb: number, vc: number, vd: number, tox: number,
toy: number, cx0: number, cy0: number, cx1: number, cy1: number) toy: number, cx0: number, cy0: number, cx1: number, cy1: number)
@@ -1191,6 +1513,9 @@ local function DrawPrim(kind: number, t, vbuf: buffer, va: number, vb: number, v
elseif kind == 4 then elseif kind == 4 then
DrawLineQuad(TargetBuffer, vbuf, va, vb, vc, vd, t, cx0, cy0, cx1, cy1, tox, toy) DrawLineQuad(TargetBuffer, vbuf, va, vb, vc, vd, t, cx0, cy0, cx1, cy1, tox, toy)
return return
elseif kind == 5 then
DrawTriPair(TargetBuffer, vbuf, va, vb, vc, vd, t, cx0, cy0, cx1, cy1, tox, toy)
return
end end
local ax, ay, cx, cy = readf64(vbuf, va) - tox, readf64(vbuf, va + 8) - toy, readf64(vbuf, vc) - tox, local ax, ay, cx, cy = readf64(vbuf, va) - tox, readf64(vbuf, va + 8) - toy, readf64(vbuf, vc) - tox,
readf64(vbuf, vc + 8) - toy readf64(vbuf, vc + 8) - toy