UNPKG

9.55 kBSource Map (JSON)View Raw
1{"version":3,"file":"RenderTexturePool.js","sources":["../../src/renderTexture/RenderTexturePool.ts"],"sourcesContent":["import { MSAA_QUALITY } from '@pixi/constants';\nimport { nextPow2 } from '@pixi/utils';\nimport { BaseRenderTexture } from './BaseRenderTexture';\nimport { RenderTexture } from './RenderTexture';\n\nimport type { ISize } from '@pixi/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.max(Math.ceil((minWidth * resolution) - 1e-6), 1);\n minHeight = Math.max(Math.ceil((minHeight * resolution) - 1e-6), 1);\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":["MSAA_QUALITY","BaseRenderTexture","RenderTexture","nextPow2"],"mappings":";;AAiBO,MAAM,kBACb;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBI,YAAY,gBACZ;AACI,SAAK,cAAc,IACnB,KAAK,iBAAiB,kBAAkB,CAAA,GACxC,KAAK,mBAAmB,IAExB,KAAK,eAAe,GACpB,KAAK,gBAAgB;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,cAAc,WAAmB,YAAoB,cAAcA,UAAAA,aAAa,MAChF;AACI,UAAM,oBAAoB,IAAIC,oCAAkB,OAAO,O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