{"version":3,"file":"RenderTexturePool.mjs","sources":["../../src/renderTexture/RenderTexturePool.ts"],"sourcesContent":["import { MSAA_QUALITY } from 'pixijs/constants';\nimport { nextPow2 } from 'pixijs/utils';\nimport { BaseRenderTexture } from './BaseRenderTexture';\nimport { RenderTexture } from './RenderTexture';\n\nimport type { ISize } from 'pixijs/math';\nimport type { IBaseTextureOptions } from '../textures/BaseTexture';\n\n/**\n * Texture pool, used by FilterSystem and plugins.\n *\n * Stores collection of temporary pow2 or screen-sized renderTextures\n *\n * If you use custom RenderTexturePool for your filters, you can use methods\n * `getFilterTexture` and `returnFilterTexture` same as in\n * @memberof PIXI\n */\nexport class RenderTexturePool\n{\n    public textureOptions: IBaseTextureOptions;\n\n    /**\n     * Allow renderTextures of the same size as screen, not just pow2\n     *\n     * Automatically sets to true after `setScreenSize`\n     * @default false\n     */\n    public enableFullScreen: boolean;\n    texturePool: {[x in string | number]: RenderTexture[]};\n    private _pixelsWidth: number;\n    private _pixelsHeight: number;\n\n    /**\n     * @param textureOptions - options that will be passed to BaseRenderTexture constructor\n     * @param {PIXI.SCALE_MODES} [textureOptions.scaleMode] - See {@link PIXI.SCALE_MODES} for possible values.\n     */\n    constructor(textureOptions?: IBaseTextureOptions)\n    {\n        this.texturePool = {};\n        this.textureOptions = textureOptions || {};\n        this.enableFullScreen = false;\n\n        this._pixelsWidth = 0;\n        this._pixelsHeight = 0;\n    }\n\n    /**\n     * Creates texture with params that were specified in pool constructor.\n     * @param realWidth - Width of texture in pixels.\n     * @param realHeight - Height of texture in pixels.\n     * @param multisample - Number of samples of the framebuffer.\n     */\n    createTexture(realWidth: number, realHeight: number, multisample = MSAA_QUALITY.NONE): RenderTexture\n    {\n        const baseRenderTexture = new BaseRenderTexture(Object.assign({\n            width: realWidth,\n            height: realHeight,\n            resolution: 1,\n            multisample,\n        }, this.textureOptions));\n\n        return new RenderTexture(baseRenderTexture);\n    }\n\n    /**\n     * Gets a Power-of-Two render texture or fullScreen texture\n     * @param minWidth - The minimum width of the render texture.\n     * @param minHeight - The minimum height of the render texture.\n     * @param resolution - The resolution of the render texture.\n     * @param multisample - Number of samples of the render texture.\n     * @returns The new render texture.\n     */\n    getOptimalTexture(minWidth: number, minHeight: number, resolution = 1, multisample = MSAA_QUALITY.NONE): RenderTexture\n    {\n        let key;\n\n        minWidth = Math.ceil((minWidth * resolution) - 1e-6);\n        minHeight = Math.ceil((minHeight * resolution) - 1e-6);\n\n        if (!this.enableFullScreen || minWidth !== this._pixelsWidth || minHeight !== this._pixelsHeight)\n        {\n            minWidth = nextPow2(minWidth);\n            minHeight = nextPow2(minHeight);\n            key = (((minWidth & 0xFFFF) << 16) | (minHeight & 0xFFFF)) >>> 0;\n\n            if (multisample > 1)\n            {\n                key += multisample * 0x100000000;\n            }\n        }\n        else\n        {\n            key = multisample > 1 ? -multisample : -1;\n        }\n\n        if (!this.texturePool[key])\n        {\n            this.texturePool[key] = [];\n        }\n\n        let renderTexture = this.texturePool[key].pop();\n\n        if (!renderTexture)\n        {\n            renderTexture = this.createTexture(minWidth, minHeight, multisample);\n        }\n\n        renderTexture.filterPoolKey = key;\n        renderTexture.setResolution(resolution);\n\n        return renderTexture;\n    }\n\n    /**\n     * Gets extra texture of the same size as input renderTexture\n     *\n     * `getFilterTexture(input, 0.5)` or `getFilterTexture(0.5, input)`\n     * @param input - renderTexture from which size and resolution will be copied\n     * @param resolution - override resolution of the renderTexture\n     *  It overrides, it does not multiply\n     * @param multisample - number of samples of the renderTexture\n     */\n    getFilterTexture(input: RenderTexture, resolution?: number, multisample?: MSAA_QUALITY): RenderTexture\n    {\n        const filterTexture = this.getOptimalTexture(input.width, input.height, resolution || input.resolution,\n            multisample || MSAA_QUALITY.NONE);\n\n        filterTexture.filterFrame = input.filterFrame;\n\n        return filterTexture;\n    }\n\n    /**\n     * Place a render texture back into the pool.\n     * @param renderTexture - The renderTexture to free\n     */\n    returnTexture(renderTexture: RenderTexture): void\n    {\n        const key = renderTexture.filterPoolKey;\n\n        renderTexture.filterFrame = null;\n        this.texturePool[key].push(renderTexture);\n    }\n\n    /**\n     * Alias for returnTexture, to be compliant with FilterSystem interface.\n     * @param renderTexture - The renderTexture to free\n     */\n    returnFilterTexture(renderTexture: RenderTexture): void\n    {\n        this.returnTexture(renderTexture);\n    }\n\n    /**\n     * Clears the pool.\n     * @param destroyTextures - Destroy all stored textures.\n     */\n    clear(destroyTextures?: boolean): void\n    {\n        destroyTextures = destroyTextures !== false;\n        if (destroyTextures)\n        {\n            for (const i in this.texturePool)\n            {\n                const textures = this.texturePool[i];\n\n                if (textures)\n                {\n                    for (let j = 0; j < textures.length; j++)\n                    {\n                        textures[j].destroy(true);\n                    }\n                }\n            }\n        }\n\n        this.texturePool = {};\n    }\n\n    /**\n     * If screen size was changed, drops all screen-sized textures,\n     * sets new screen size, sets `enableFullScreen` to true\n     *\n     * Size is measured in pixels, `renderer.view` can be passed here, not `renderer.screen`\n     * @param size - Initial size of screen.\n     */\n    setScreenSize(size: ISize): void\n    {\n        if (size.width === this._pixelsWidth\n            && size.height === this._pixelsHeight)\n        {\n            return;\n        }\n\n        this.enableFullScreen = size.width > 0 && size.height > 0;\n\n        for (const i in this.texturePool)\n        {\n            if (!(Number(i) < 0))\n            {\n                continue;\n            }\n\n            const textures = this.texturePool[i];\n\n            if (textures)\n            {\n                for (let j = 0; j < textures.length; j++)\n                {\n                    textures[j].destroy(true);\n                }\n            }\n\n            this.texturePool[i] = [];\n        }\n\n        this._pixelsWidth = size.width;\n        this._pixelsHeight = size.height;\n    }\n\n    /**\n     * Key that is used to store fullscreen renderTextures in a pool\n     * @readonly\n     */\n    static SCREEN_KEY = -1;\n}\n"],"names":[],"mappings":";;;;;AAiBO,MAAM,iBACb,CAAA;AAAA,EAkBI,YAAY,cACZ,EAAA;AACI,IAAA,IAAA,CAAK,cAAc,EAAC,CAAA;AACpB,IAAK,IAAA,CAAA,cAAA,GAAiB,kBAAkB,EAAC,CAAA;AACzC,IAAA,IAAA,CAAK,gBAAmB,GAAA,K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