PK ¹Òø' ' hooks/load.lua-- Shader Aura Cache -- -- engine.Entity:makeMapObject builds each "sdm" shader aura by emptying tiles.repo (so the -- source image is re-read from disk every time) and then calling tex:generateSDM(), a CPU -- heavy image pass with a GPU readback. This runs on every sprite rebuild, once per aura, -- and never caches anything. Here we cache both steps. local Tiles = require "engine.Tiles" local old_get = Tiles.get local src_cache = {} -- ["prefix|image"] = packed results of Tiles:get for the aura source image local src_tex = {} -- [texture userdata] = true, for the textures held in src_cache local sdm_cache = {} -- [texture userdata] = { [double] = packed results of generateSDM } local sdm_hooked = false local stats = {src_hit = 0, sdm_hit = 0} local function pack(...) return {n = select("#", ...), ...} end -- Wrap generateSDM on the texture class (found through the first texture we see) local function hookGenerateSDM(tex) if sdm_hooked then return end local mt = getmetatable(tex) local methods = mt and mt.__index if type(methods) ~= "table" or type(methods.generateSDM) ~= "function" then return end sdm_hooked = true local orig = methods.generateSDM methods.generateSDM = function(t, double, ...) if not src_tex[t] then return orig(t, double, ...) end local per = sdm_cache[t] if not per then per = {} sdm_cache[t] = per end local key = double and true or false local res = per[key] if res then stats.sdm_hit = stats.sdm_hit + 1 if stats.sdm_hit % 1000 == 0 then print("[SDM_CACHE] reused", stats.sdm_hit, "generated SDM textures,", stats.src_hit, "source images") end return unpack(res, 1, res.n) end res = pack(orig(t, double, ...)) per[key] = res print("[SDM_CACHE] generated SDM texture (double=" .. tostring(key) .. ")") return unpack(res, 1, res.n) end print("[SDM_CACHE] generateSDM hooked") end function Tiles:get(char, fr, fg, fb, br, bg, bb, image, alpha, do_outline, allow_tileset, force_texture_repeat) -- The aura source image call from Entity:makeMapObject: plain image, no char, empty repo if char == "" and type(image) == "string" and not alpha and not do_outline and not allow_tileset and self.repo and next(self.repo) == nil then local key = tostring(self.prefix) .. "|" .. image local res = src_cache[key] if res then stats.src_hit = stats.src_hit + 1 return unpack(res, 1, res.n) end res = pack(old_get(self, char, fr, fg, fb, br, bg, bb, image, alpha, do_outline, allow_tileset, force_texture_repeat)) local tex = res[1] if tex then src_cache[key] = res src_tex[tex] = true hookGenerateSDM(tex) print("[SDM_CACHE] cached source image", image) end return unpack(res, 1, res.n) end return old_get(self, char, fr, fg, fb, br, bg, bb, image, alpha, do_outline, allow_tileset, force_texture_repeat) end PK iÏy!ÕÕinit.lualong_name = "Shader Aura Cache" short_name = "sdm-cache" for_module = "tome" version = {1, 7, 6} addon_version = {1, 0, 0} weight = 100 author = { 'CS' } homepage = 'http://te4.org/users/cshigaki' description = [[Caches the textures the game regenerates every time a sprite with shader auras is rebuilt. Fixes freezes and crashes with many auras (e.g. Possessors).]] tags = {'performance', 'shader', 'aura'} overload = false superload = false hooks = true data = false PK ¹Òø' ' hooks/load.luaPK iÏy!ÕÕS init.luaPKrN