@pixi/core
Version:
Core PixiJS
100 lines (97 loc) • 3.29 kB
JavaScript
import { MSAA_QUALITY } from '@pixi/constants';
import { nextPow2 } from '@pixi/utils';
import { BaseRenderTexture } from './BaseRenderTexture.mjs';
import { RenderTexture } from './RenderTexture.mjs';
class RenderTexturePool {
constructor(textureOptions) {
this.texturePool = {};
this.textureOptions = textureOptions || {};
this.enableFullScreen = false;
this._pixelsWidth = 0;
this._pixelsHeight = 0;
}
createTexture(realWidth, realHeight, multisample = MSAA_QUALITY.NONE) {
const baseRenderTexture = new BaseRenderTexture(Object.assign({
width: realWidth,
height: realHeight,
resolution: 1,
multisample
}, this.textureOptions));
return new RenderTexture(baseRenderTexture);
}
getOptimalTexture(minWidth, minHeight, resolution = 1, multisample = MSAA_QUALITY.NONE) {
let key;
minWidth = Math.ceil(minWidth * resolution - 1e-6);
minHeight = Math.ceil(minHeight * resolution - 1e-6);
if (!this.enableFullScreen || minWidth !== this._pixelsWidth || minHeight !== this._pixelsHeight) {
minWidth = nextPow2(minWidth);
minHeight = nextPow2(minHeight);
key = ((minWidth & 65535) << 16 | minHeight & 65535) >>> 0;
if (multisample > 1) {
key += multisample * 4294967296;
}
} else {
key = multisample > 1 ? -multisample : -1;
}
if (!this.texturePool[key]) {
this.texturePool[key] = [];
}
let renderTexture = this.texturePool[key].pop();
if (!renderTexture) {
renderTexture = this.createTexture(minWidth, minHeight, multisample);
}
renderTexture.filterPoolKey = key;
renderTexture.setResolution(resolution);
return renderTexture;
}
getFilterTexture(input, resolution, multisample) {
const filterTexture = this.getOptimalTexture(input.width, input.height, resolution || input.resolution, multisample || MSAA_QUALITY.NONE);
filterTexture.filterFrame = input.filterFrame;
return filterTexture;
}
returnTexture(renderTexture) {
const key = renderTexture.filterPoolKey;
renderTexture.filterFrame = null;
this.texturePool[key].push(renderTexture);
}
returnFilterTexture(renderTexture) {
this.returnTexture(renderTexture);
}
clear(destroyTextures) {
destroyTextures = destroyTextures !== false;
if (destroyTextures) {
for (const i in this.texturePool) {
const textures = this.texturePool[i];
if (textures) {
for (let j = 0; j < textures.length; j++) {
textures[j].destroy(true);
}
}
}
}
this.texturePool = {};
}
setScreenSize(size) {
if (size.width === this._pixelsWidth && size.height === this._pixelsHeight) {
return;
}
this.enableFullScreen = size.width > 0 && size.height > 0;
for (const i in this.texturePool) {
if (!(Number(i) < 0)) {
continue;
}
const textures = this.texturePool[i];
if (textures) {
for (let j = 0; j < textures.length; j++) {
textures[j].destroy(true);
}
}
this.texturePool[i] = [];
}
this._pixelsWidth = size.width;
this._pixelsHeight = size.height;
}
}
RenderTexturePool.SCREEN_KEY = -1;
export { RenderTexturePool };
//# sourceMappingURL=RenderTexturePool.mjs.map