{"version":3,"file":"BatchSystem.mjs","sources":["../../src/batch/BatchSystem.ts"],"sourcesContent":["import { extensions, ExtensionType } from '@pixi/extensions';\nimport { ObjectRenderer } from './ObjectRenderer';\n\nimport type { ExtensionMetadata } from '@pixi/extensions';\nimport type { Renderer } from '../Renderer';\nimport type { ISystem } from '../system/ISystem';\nimport type { BaseTexture } from '../textures/BaseTexture';\nimport type { BatchTextureArray } from './BatchTextureArray';\n\n/**\n * System plugin to the renderer to manage batching.\n * @memberof PIXI\n */\nexport class BatchSystem implements ISystem\n{\n    /** @ignore */\n    static extension: ExtensionMetadata = {\n        type: ExtensionType.RendererSystem,\n        name: 'batch',\n    };\n\n    /** An empty renderer. */\n    public readonly emptyRenderer: ObjectRenderer;\n\n    /** The currently active ObjectRenderer. */\n    public currentRenderer: ObjectRenderer;\n    private renderer: Renderer;\n\n    /**\n     * @param renderer - The renderer this System works for.\n     */\n    constructor(renderer: Renderer)\n    {\n        this.renderer = renderer;\n        this.emptyRenderer = new ObjectRenderer(renderer);\n        this.currentRenderer = this.emptyRenderer;\n    }\n\n    /**\n     * Changes the current renderer to the one given in parameter\n     * @param objectRenderer - The object renderer to use.\n     */\n    setObjectRenderer(objectRenderer: ObjectRenderer): void\n    {\n        if (this.currentRenderer === objectRenderer)\n        {\n            return;\n        }\n\n        this.currentRenderer.stop();\n        this.currentRenderer = objectRenderer;\n\n        this.currentRenderer.start();\n    }\n\n    /**\n     * This should be called if you wish to do some custom rendering\n     * It will basically render anything that may be batched up such as sprites\n     */\n    flush(): void\n    {\n        this.setObjectRenderer(this.emptyRenderer);\n    }\n\n    /** Reset the system to an empty renderer */\n    reset(): void\n    {\n        this.setObjectRenderer(this.emptyRenderer);\n    }\n\n    /**\n     * Handy function for batch renderers: copies bound textures in first maxTextures locations to array\n     * sets actual _batchLocation for them\n     * @param arr - arr copy destination\n     * @param maxTextures - number of copied elements\n     */\n    copyBoundTextures(arr: BaseTexture[], maxTextures: number): void\n    {\n        const { boundTextures } = this.renderer.texture;\n\n        for (let i = maxTextures - 1; i >= 0; --i)\n        {\n            arr[i] = boundTextures[i] || null;\n            if (arr[i])\n            {\n                arr[i]._batchLocation = i;\n            }\n        }\n    }\n\n    /**\n     * Assigns batch locations to textures in array based on boundTextures state.\n     * All textures in texArray should have `_batchEnabled = _batchId`,\n     * and their count should be less than `maxTextures`.\n     * @param texArray - textures to bound\n     * @param boundTextures - current state of bound textures\n     * @param batchId - marker for _batchEnabled param of textures in texArray\n     * @param maxTextures - number of texture locations to manipulate\n     */\n    boundArray(texArray: BatchTextureArray, boundTextures: Array<BaseTexture>,\n        batchId: number, maxTextures: number): void\n    {\n        const { elements, ids, count } = texArray;\n        let j = 0;\n\n        for (let i = 0; i < count; i++)\n        {\n            const tex = elements[i];\n            const loc = tex._batchLocation;\n\n            if (loc >= 0 && loc < maxTextures\n                && boundTextures[loc] === tex)\n            {\n                ids[i] = loc;\n                continue;\n            }\n\n            while (j < maxTextures)\n            {\n                const bound = boundTextures[j];\n\n                if (bound && bound._batchEnabled === batchId\n                    && bound._batchLocation === j)\n                {\n                    j++;\n                    continue;\n                }\n\n                ids[i] = j;\n                tex._batchLocation = j;\n                boundTextures[j] = tex;\n                break;\n            }\n        }\n    }\n\n    /**\n     * @ignore\n     */\n    destroy(): void\n    {\n        this.renderer = null;\n    }\n}\n\nextensions.add(BatchSystem);\n"],"names":[],"mappings":";;AAaO,MAAM,YACb;AAAA;AAAA;AAAA;AAAA,EAiBI,YAAY,UACZ;AACS,SAAA,WAAW,UAChB,KAAK,gBAAgB,IAAI,eAAe,QAAQ,GAChD,KAAK,kBAAkB,KAAK;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,kBAAkB,gBAClB;AACQ,SAAK,oBAAoB,mBAK7B,KAAK,gBAAgB,KACrB,GAAA,KAAK,kBAAkB,gBAEvB,KAAK,gBAAgB,MAAM;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,QACA;AACS,SAAA,kBAAkB,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