Build & Release / build (push) Canceled after 0s
- Less garbage per frame: scalar text/label sizes, scratch rects/points, reused clip-stack ImVec4 and window stack entries (160 -> 56 KB/frame on demo+stress, draw hashes unchanged) - Faster glyph baking: @native rasterizer/rect packer/pixel loops, glyph shape cache, comparator-free edge sort, inline scanline conversion, table.create zero-fill; fix leaked global 'step' - Textures: new atlases convert only the packed corner, flat row conversion skipping zero pixels - Raster: uniform-region tracking, direct opaque glyph texels, inlined BlendPixel with per-texel cache - Raster: textured line quads drawn as one quad, u32 texel reads for constant-colour triangles, column walk for tall narrow triangles (plot bench Raster 4.16 -> 3.21 ms, HashVertex 0.64 -> 0.55 ms, pixels unchanged) - Parallel: send Actors only changed atlas rows / rows above FillRow (full-copy 1283 -> 619 MB on fontbench x4 Actors); dispatch small and text-heavy frames (MinTiles 1, MinPixels 2048, MaxBytesPerPixel 0) - Stage timing: Textures and Flush stages; parallel stats explain serial flushes - extensions/: submodules for imcurveedit-luau, imgradient-luau, imgui-memory-editor-luau, imsequencer-luau; all 9 submodule URLs and README links now on GitHub - new examples: example_love_extensions, example_roblox_extensions
216 lines
8.4 KiB
Lua
216 lines
8.4 KiB
Lua
-- Texture records, the module-wide EditableImage budget, profiling labels and stage timing stats.
|
|
local Raster = IM_INCLUDE("roblox/raster.lua")
|
|
|
|
local AssetService = game:GetService("AssetService")
|
|
local writeu32 = buffer.writeu32
|
|
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).
|
|
local EditableImages = {
|
|
Cap = 64 * 1024 * 1024, Bytes = 0, Count = 0, ImageBytes = setmetatable({}, { __mode = "k" }),
|
|
Pool = {}, PoolMax = 16,
|
|
Backends = setmetatable({}, { __mode = "k" }),
|
|
LastTextureId = 0, KEEP_FRAMES = 30,
|
|
StageTiming = false,
|
|
}
|
|
M.EditableImages = EditableImages
|
|
|
|
local ProfileBegin, ProfileEnd = debug.profilebegin, debug.profileend
|
|
local Profiling = false
|
|
function M.PB(name) if Profiling then ProfileBegin(name) end end
|
|
function M.PE() if Profiling then ProfileEnd() end end
|
|
function M.SetProfiling(enabled) Profiling = enabled and true or false end
|
|
function M.IsProfiling() return Profiling end
|
|
|
|
-- StageTiming on: RenderDrawData accumulates os.clock per stage into StageFrame (GetStats().Frame)
|
|
function EditableImages.BeginStage() EditableImages.StageStart = os.clock() end
|
|
function EditableImages.EndStage(stage)
|
|
local f = EditableImages.StageFrame
|
|
if f then f[stage] += os.clock() - EditableImages.StageStart end
|
|
end
|
|
function EditableImages.AddStat(name, n)
|
|
local f = EditableImages.StageFrame
|
|
if f then f[name] += n end
|
|
end
|
|
|
|
function M.GetBackendData()
|
|
return ImGui.GetCurrentContext() and ImGui.GetIO().BackendPlatformUserData or nil
|
|
end
|
|
|
|
@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 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
|
|
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
|
|
end
|
|
end
|
|
|
|
-- Shared by every context: a font atlas shared between contexts is created under one, drawn under the others
|
|
local Records = {}
|
|
M.Records = Records
|
|
|
|
local function CreateTextureRecord(tex)
|
|
local w, h = tex.Width, tex.Height
|
|
local b = buffer.create(w * h * 4)
|
|
-- The current font atlas texture: pixels outside the packed rects (MaxRectBounds) and the queued uploads (UsedRect)
|
|
-- are still zero (repack's BlockCopy doesn't queue, but every copied rect is packed). Fill them with what zero
|
|
-- converts to, convert only the used corner. A fresh/grown atlas is mostly empty. Any other texture: everything.
|
|
local atlas = ImGui.GetCurrentContext() and ImGui.GetIO().Fonts
|
|
local cw, ch = w, h
|
|
if atlas and atlas.TexData == tex and atlas.Builder then
|
|
local u, m = tex.UsedRect, atlas.Builder.MaxRectBounds
|
|
cw = math.min(w, math.max(u.x + u.w, m.x))
|
|
ch = math.min(h, math.max(u.y + u.h, m.y))
|
|
end
|
|
if cw < w or ch < h then
|
|
if tex.BytesPerPixel ~= 4 then -- Alpha8 zero -> 0x00FFFFFF; RGBA zero -> 0 (buffer.create's fill)
|
|
local n, total = 4, w * h * 4
|
|
writeu32(b, 0, 16777215)
|
|
while n < total do
|
|
local c = math.min(n, total - n)
|
|
buffer.copy(b, n, b, 0, c)
|
|
n += c
|
|
end
|
|
end
|
|
CopyTexturePixels(tex, b, 0, 0, cw, ch, tex.BytesPerPixel == 4)
|
|
else
|
|
CopyTexturePixels(tex, b, 0, 0, w, h, true)
|
|
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 }
|
|
EditableImages.LastTextureId += 1
|
|
Records[EditableImages.LastTextureId] = t
|
|
tex.BackendUserData = EditableImages.LastTextureId
|
|
tex:SetTexID(EditableImages.LastTextureId)
|
|
tex:SetStatus(ImTextureStatus.OK)
|
|
end
|
|
|
|
function M.UpdateTexture(tex)
|
|
if tex.Status == ImTextureStatus.WantCreate then
|
|
CreateTextureRecord(tex)
|
|
elseif tex.Status == ImTextureStatus.WantUpdates then
|
|
local t = Records[tex.BackendUserData]
|
|
if t == nil or t.Width ~= tex.Width or t.Height ~= tex.Height then
|
|
CreateTextureRecord(tex)
|
|
return
|
|
end
|
|
local y0, y1 = math.huge, -1
|
|
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
|
|
end
|
|
-- parallel workers hold copies, patched when this changes: Log[V] = changed rows (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 - 16] = nil
|
|
tex:SetStatus(ImTextureStatus.OK)
|
|
elseif tex.Status == ImTextureStatus.WantDestroy then
|
|
if tex.BackendUserData then
|
|
Records[tex.BackendUserData] = nil
|
|
tex.BackendUserData = nil
|
|
end
|
|
tex:SetTexID(ImTextureID_Invalid)
|
|
tex:SetStatus(ImTextureStatus.Destroyed)
|
|
end
|
|
end
|
|
|
|
-- User texture from RGBA8 pixels. Ids are never reused, so cached tiles can't show a stale image.
|
|
function ImGui_ImplRoblox_CreateTexture(pixels, w, h)
|
|
EditableImages.LastTextureId += 1
|
|
Records[EditableImages.LastTextureId] = { Buffer = pixels, Width = w, Height = h }
|
|
return ImTextureRef(EditableImages.LastTextureId)
|
|
end
|
|
|
|
function ImGui_ImplRoblox_DestroyTexture(tex_ref)
|
|
Records[tex_ref._TexID] = nil
|
|
end
|
|
|
|
-- Every EditableImage goes through Create/Destroy, counted against Cap. Over the cap (or CreateEditableImage failing):
|
|
-- evict idle tiles, retry once; still nothing -> nil, the caller skips the tile this frame.
|
|
function EditableImages.Create(w, h)
|
|
local need = w * h * 4
|
|
if need > EditableImages.Cap then return nil end
|
|
for attempt = 1, 2 do
|
|
if EditableImages.Bytes + need <= EditableImages.Cap then
|
|
local ok, img = pcall(AssetService.CreateEditableImage, AssetService, { Size = Vector2.new(w, h) })
|
|
if ok and img then
|
|
EditableImages.ImageBytes[img] = need
|
|
EditableImages.Bytes += need
|
|
EditableImages.Count += 1
|
|
return img
|
|
end
|
|
if attempt == 1 then EditableImages.Evict(math.huge) end -- engine refused: free everything evictable
|
|
elseif attempt == 1 then
|
|
EditableImages.Evict(EditableImages.Bytes + need - EditableImages.Cap)
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
function EditableImages.Destroy(img)
|
|
local s = EditableImages.ImageBytes[img]
|
|
if s then
|
|
EditableImages.ImageBytes[img] = nil
|
|
EditableImages.Bytes -= s
|
|
EditableImages.Count -= 1
|
|
end
|
|
img:Destroy()
|
|
end
|
|
|
|
-- The free list first, then idle tiles (not shown, not used this frame) of every context
|
|
function EditableImages.Evict(need)
|
|
local freed = 0
|
|
while freed < need and #EditableImages.Pool > 0 do
|
|
local t = table.remove(EditableImages.Pool)
|
|
EditableImages.Destroy(t.Image)
|
|
t.Label:Destroy()
|
|
freed += TILE * TILE * 4
|
|
end
|
|
if freed >= need then return end
|
|
for BackendData in EditableImages.Backends do
|
|
for _, set in pairs(BackendData.Layers) do
|
|
for kind = 1, 3 do
|
|
local l = set[kind]
|
|
if l then
|
|
for k, t in pairs(l.Tiles) do
|
|
if t.Used ~= BackendData.FrameNo and not (t.Visible and l.Visible) then
|
|
EditableImages.FreeTile(l, k)
|
|
freed += TILE * TILE * 4
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
return M
|