{"version":3,"file":"TexturePool.mjs","sources":["../../../../../src/rendering/renderers/shared/texture/TexturePool.ts"],"sourcesContent":["import { nextPow2 } from '../../../../maths/misc/pow2';\nimport { GlobalResourceRegistry } from '../../../../utils/pool/GlobalResourceRegistry';\nimport { TextureSource } from './sources/TextureSource';\nimport { Texture } from './Texture';\nimport { TextureStyle } from './TextureStyle';\n\nimport type { TextureSourceOptions } from './sources/TextureSource';\n\nlet count = 0;\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 default pool\n * @category rendering\n * @advanced\n */\nexport class TexturePoolClass\n{\n    /** The default options for texture pool */\n    public textureOptions: TextureSourceOptions;\n\n    /** The default texture style for the pool */\n    public textureStyle: TextureStyle;\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\n    private _texturePool: {[x in string | number]: Texture[]};\n    private _poolKeyHash: Record<number, number> = Object.create(null);\n\n    /**\n     * @param textureOptions - options that will be passed to BaseRenderTexture constructor\n     * @param {SCALE_MODE} [textureOptions.scaleMode] - See {@link SCALE_MODE} for possible values.\n     */\n    constructor(textureOptions?: TextureSourceOptions)\n    {\n        this._texturePool = {};\n        this.textureOptions = textureOptions || {};\n        this.enableFullScreen = false;\n        this.textureStyle = new TextureStyle(this.textureOptions);\n    }\n\n    /**\n     * Creates texture with params that were specified in pool constructor.\n     * @param pixelWidth - Width of texture in pixels.\n     * @param pixelHeight - Height of texture in pixels.\n     * @param antialias\n     * @param autoGenerateMipmaps - Whether to automatically generate mipmaps for this texture\n     */\n    public createTexture(pixelWidth: number, pixelHeight: number, antialias: boolean, autoGenerateMipmaps: boolean): Texture\n    {\n        const textureSource = new TextureSource({\n            ...this.textureOptions,\n\n            width: pixelWidth,\n            height: pixelHeight,\n            resolution: 1,\n            antialias,\n            autoGarbageCollect: false,\n            autoGenerateMipmaps,\n        });\n\n        return new Texture({\n            source: textureSource,\n            label: `texturePool_${count++}`,\n        });\n    }\n\n    /**\n     * Gets a Power-of-Two render texture or fullScreen texture\n     * @param frameWidth - The minimum width of the render texture.\n     * @param frameHeight - The minimum height of the render texture.\n     * @param resolution - The resolution of the render texture.\n     * @param antialias\n     * @param autoGenerateMipmaps - Whether to automatically generate mipmaps. Defaults to false.\n     * @returns The new render texture.\n     */\n    public getOptimalTexture(\n        frameWidth: number,\n        frameHeight: number,\n        resolution = 1,\n        antialias: boolean,\n        autoGenerateMipmaps = false\n    ): Texture\n    {\n        let po2Width = Math.ceil((frameWidth * resolution) - 1e-6);\n        let po2Height = Math.ceil((frameHeight * resolution) - 1e-6);\n\n        po2Width = nextPow2(po2Width);\n        po2Height = nextPow2(po2Height);\n\n        // Pack flags in lower bits, then dimensions in higher bits to avoid collisions\n        // Bit 0: antialias flag\n        // Bit 1: mipmap flag\n        // Bits 2-16: height (15 bits, supports up to 32768)\n        // Bits 17-31: width (15 bits, supports up to 32768)\n        const antialiasFlag = antialias ? 1 : 0;\n        const mipmapFlag = autoGenerateMipmaps ? 1 : 0;\n        const key = (po2Width << 17) + (po2Height << 2) + (mipmapFlag << 1) + antialiasFlag;\n\n        if (!this._texturePool[key])\n        {\n            this._texturePool[key] = [];\n        }\n\n        let texture = this._texturePool[key].pop();\n\n        if (!texture)\n        {\n            texture = this.createTexture(po2Width, po2Height, antialias, autoGenerateMipmaps);\n        }\n\n        texture.source._resolution = resolution;\n        texture.source.width = po2Width / resolution;\n        texture.source.height = po2Height / resolution;\n        texture.source.pixelWidth = po2Width;\n        texture.source.pixelHeight = po2Height;\n\n        // fit the layout to the requested original size\n        texture.frame.x = 0;\n        texture.frame.y = 0;\n        texture.frame.width = frameWidth;\n        texture.frame.height = frameHeight;\n\n        texture.updateUvs();\n\n        this._poolKeyHash[texture.uid] = key;\n\n        return texture;\n    }\n\n    /**\n     * Gets a pooled texture matching the dimensions and resolution of the given texture.\n     *\n     * This is a convenience wrapper around {@link TexturePoolClass#getOptimalTexture|getOptimalTexture}\n     * that copies width, height, and resolution from an existing texture. Useful when a filter needs\n     * a temporary texture the same size as its input (e.g., for multi-pass blur).\n     * @param texture - The texture whose dimensions to match.\n     * @param antialias - Whether to use antialias on the pooled texture. Defaults to `false`.\n     * @returns A pooled texture with power-of-two backing dimensions at the source resolution.\n     */\n    public getSameSizeTexture(texture: Texture, antialias = false)\n    {\n        const source = texture.source;\n\n        return this.getOptimalTexture(texture.width, texture.height, source._resolution, antialias);\n    }\n\n    /**\n     * Returns a texture to the pool so it can be reused by future\n     * {@link TexturePoolClass#getOptimalTexture|getOptimalTexture}\n     * or {@link TexturePoolClass#getSameSizeTexture|getSameSizeTexture} calls.\n     *\n     * If you modified the texture's style after obtaining it (e.g., changed filtering or wrapping),\n     * pass `resetStyle = true` to restore the pool's default {@link TexturePoolClass#textureStyle|textureStyle}.\n     * This prevents style changes from leaking into subsequent consumers of the same pooled texture.\n     * @param renderTexture - The texture to return to the pool.\n     * @param resetStyle - When `true`, replaces the texture source's style with the pool default. Defaults to `false`.\n     */\n    public returnTexture(renderTexture: Texture, resetStyle = false): void\n    {\n        const key = this._poolKeyHash[renderTexture.uid];\n\n        // we can skip the copy if we don't need to reset the style\n        if (resetStyle)\n        {\n            renderTexture.source.style = this.textureStyle;\n        }\n\n        this._texturePool[key].push(renderTexture);\n    }\n\n    /**\n     * Clears the pool.\n     * @param destroyTextures - Destroy all stored textures.\n     */\n    public 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/**\n * The default texture pool instance.\n * @category rendering\n * @advanced\n */\nexport const Textur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