{"version":3,"file":"RenderableGCSystem.mjs","sources":["../../../../../src/rendering/renderers/shared/texture/RenderableGCSystem.ts"],"sourcesContent":["import { ExtensionType } from '../../../../extensions/Extensions';\n\nimport type { Renderer } from '../../types';\nimport type { InstructionSet } from '../instructions/InstructionSet';\nimport type { RenderPipe } from '../instructions/RenderPipe';\nimport type { Renderable } from '../Renderable';\nimport type { System } from '../system/System';\n\n/**\n * Options for the {@link RenderableGCSystem}.\n * @memberof rendering\n * @property {boolean} [renderableGCActive=true] - If set to true, this will enable the garbage collector on the renderables.\n * @property {number} [renderableGCAMaxIdle=60000] -\n * The maximum idle frames before a texture is destroyed by garbage collection.\n * @property {number} [renderableGCCheckCountMax=60000] - time between two garbage collections.\n */\nexport interface RenderableGCSystemOptions\n{\n    /**\n     * If set to true, this will enable the garbage collector on the GPU.\n     * @default true\n     * @memberof rendering.SharedRendererOptions\n     */\n    renderableGCActive: boolean;\n    /**\n     * The maximum idle frames before a texture is destroyed by garbage collection.\n     * @default 60 * 60\n     * @memberof rendering.SharedRendererOptions\n     */\n    renderableGCMaxUnusedTime: number;\n    /**\n     * Frames between two garbage collections.\n     * @default 600\n     * @memberof rendering.SharedRendererOptions\n     */\n    renderableGCFrequency: number;\n}\n/**\n * System plugin to the renderer to manage renderable garbage collection. When rendering\n * stuff with the renderer will assign resources to each renderable. This could be for example\n * a batchable Sprite, or a text texture. If the renderable is not used for a certain amount of time\n * its resources will be tided up by its render pipe.\n * @memberof rendering\n */\nexport class RenderableGCSystem implements System<RenderableGCSystemOptions>\n{\n    /** @ignore */\n    public static extension = {\n        type: [\n            ExtensionType.WebGLSystem,\n            ExtensionType.WebGPUSystem,\n        ],\n        name: 'renderableGC',\n    } as const;\n\n    /** default options for the renderableGCSystem */\n    public static defaultOptions: RenderableGCSystemOptions = {\n        /**\n         * If set to true, this will enable the garbage collector on the GPU.\n         * @default true\n         */\n        renderableGCActive: true,\n        /**\n         * The maximum idle frames before a texture is destroyed by garbage collection.\n         * @default 60 * 60\n         */\n        renderableGCMaxUnusedTime: 60000,\n        /**\n         * Frames between two garbage collections.\n         * @default 600\n         */\n        renderableGCFrequency: 30000,\n    };\n\n    /**\n     * Maximum idle frames before a texture is destroyed by garbage collection.\n     * @see renderableGCSystem.defaultMaxIdle\n     */\n    public maxUnusedTime: number;\n\n    private _renderer: Renderer;\n\n    private readonly _managedRenderables: Renderable[] = [];\n    private _handler: number;\n    private _frequency: number;\n    private _now: number;\n\n    /** @param renderer - The renderer this System works for. */\n    constructor(renderer: Renderer)\n    {\n        this._renderer = renderer;\n    }\n\n    public init(options: RenderableGCSystemOptions): void\n    {\n        options = { ...RenderableGCSystem.defaultOptions, ...options };\n\n        this.maxUnusedTime = options.renderableGCMaxUnusedTime;\n        this._frequency = options.renderableGCFrequency;\n\n        this.enabled = options.renderableGCActive;\n    }\n\n    get enabled(): boolean\n    {\n        return !!this._handler;\n    }\n\n    set enabled(value: boolean)\n    {\n        if (this.enabled === value) return;\n\n        if (value)\n        {\n            this._handler = this._renderer.scheduler.repeat(\n                () => this.run(),\n                this._frequency\n            );\n        }\n        else\n        {\n            this._renderer.scheduler.cancel(this._handler);\n        }\n    }\n\n    public prerender(): void\n    {\n        this._now = performance.now();\n    }\n\n    public addRenderable(renderable: Renderable, instructionSet: InstructionSet): void\n    {\n        renderable._lastUsed = this._now;\n\n        if (renderable._lastInstructionTick === -1)\n        {\n            // TODO manage index...\n            this._managedRenderables.push(renderable);\n        }\n\n        renderable._lastInstructionTick = instructionSet.tick;\n    }\n\n    /** Runs the scheduled garbage collection */\n    public run(): void\n    {\n        const now = performance.now();\n\n        const managedRenderables = this._managedRenderables;\n\n        const renderPipes = this._renderer.renderPipes;\n\n        let offset = 0;\n\n        for (let i = 0; i < managedRenderables.length; i++)\n        {\n            const renderable = managedRenderables[i];\n\n            const renderGroup = renderable.renderGroup ?? renderable.parentRenderGroup;\n            const currentIndex = renderGroup?.instructionSet?.tick ?? -1;\n\n            if (renderable._lastInstructionTick !== currentIndex && now - renderable._lastUsed > this.maxUnusedTime)\n            {\n                if (!renderable.destroyed)\n                {\n                    const rp = renderPipes as unknown as Record<string, RenderPipe>;\n\n                    rp[renderable.renderPipeId].destroyRenderable(renderable);\n                }\n\n                // remove from the array as this has been destroyed..\n                renderable._lastInstructionTick = -1;\n                offset++;\n            }\n            else\n            {\n                managedRenderables[i - (offset)] = renderable;\n            }\n        }\n\n        managedRenderables.length = managedRenderables.length - offset;\n    }\n\n    public destroy(): void\n    {\n        this.enabled = false;\n        this._renderer = null as any as Renderer;\n        this._managedRenderables.length = 0;\n    }\n}\n"],"names":[],"mappings":";;;AA4CO,MAAM,mBAAA,GAAN,MAAM,mBACb,CAAA;AAAA;AAAA,EA2CI,YAAY,QACZ,EAAA;AAPA,IAAA,IAAA,CAAiB,sBAAoC,EAAC,CAAA;AAQlD,IAAA,IAAA,CAAK,SAAY,GAAA,QAAA,CAAA;AAAA,GACrB;AAAA,EAEO,KAAK,OACZ,EAAA;AACI,IAAA,OAAA,GAAU,EAAE,GAAG,mBAAmB,CAAA,cAAA,EAAgB,GAAG,OAAQ,EAAA,CAAA;AAE7D,IAAA,IAAA,CAAK,gBAAgB,OAAQ,CAAA,yBAAA,CAAA;AAC7B,IAAA,IAAA,CAAK,aAAa,OAAQ,CAAA,qBAAA,CAAA;AAE1B,IAAA,IAAA,CAAK,UAAU,OAAQ,CAAA,kBAAA,CAAA;AAAA,GAC3B;AAAA,EAEA,IAAI,OACJ,GAAA;AACI,IAAO,OAAA,CAAC,CAAC,IAAK,CAAA,QAAA,CAAA;AAAA,GAClB;AAAA,EAEA,IAAI,QAAQ,KACZ,EAAA;AACI,IAAA,IAAI,KAAK,OAAY,KAAA,KAAA;AAAO,MAAA,OAAA;AAE5B,IAAA,IAAI,KACJ,EAAA;AACI,MAAK,IAAA,CAAA,QAAA,GAAW,IAAK,CAAA,SAAA,CAAU,SAAU,CAAA,MAAA;AAAA,QACrC,MAAM,KAAK,GAAI,EAAA;AAAA,QACf,IAAK,CAAA,UAA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