1 | import { MSAA_QUALITY } from '@pixi/constants';
|
2 | import { nextPow2 } from '@pixi/utils';
|
3 | import { BaseRenderTexture } from './BaseRenderTexture.mjs';
|
4 | import { RenderTexture } from './RenderTexture.mjs';
|
5 |
|
6 | class RenderTexturePool {
|
7 | constructor(textureOptions) {
|
8 | this.texturePool = {};
|
9 | this.textureOptions = textureOptions || {};
|
10 | this.enableFullScreen = false;
|
11 | this._pixelsWidth = 0;
|
12 | this._pixelsHeight = 0;
|
13 | }
|
14 | createTexture(realWidth, realHeight, multisample = MSAA_QUALITY.NONE) {
|
15 | const baseRenderTexture = new BaseRenderTexture(Object.assign({
|
16 | width: realWidth,
|
17 | height: realHeight,
|
18 | resolution: 1,
|
19 | multisample
|
20 | }, this.textureOptions));
|
21 | return new RenderTexture(baseRenderTexture);
|
22 | }
|
23 | getOptimalTexture(minWidth, minHeight, resolution = 1, multisample = MSAA_QUALITY.NONE) {
|
24 | let key;
|
25 | minWidth = Math.ceil(minWidth * resolution - 1e-6);
|
26 | minHeight = Math.ceil(minHeight * resolution - 1e-6);
|
27 | if (!this.enableFullScreen || minWidth !== this._pixelsWidth || minHeight !== this._pixelsHeight) {
|
28 | minWidth = nextPow2(minWidth);
|
29 | minHeight = nextPow2(minHeight);
|
30 | key = ((minWidth & 65535) << 16 | minHeight & 65535) >>> 0;
|
31 | if (multisample > 1) {
|
32 | key += multisample * 4294967296;
|
33 | }
|
34 | } else {
|
35 | key = multisample > 1 ? -multisample : -1;
|
36 | }
|
37 | if (!this.texturePool[key]) {
|
38 | this.texturePool[key] = [];
|
39 | }
|
40 | let renderTexture = this.texturePool[key].pop();
|
41 | if (!renderTexture) {
|
42 | renderTexture = this.createTexture(minWidth, minHeight, multisample);
|
43 | }
|
44 | renderTexture.filterPoolKey = key;
|
45 | renderTexture.setResolution(resolution);
|
46 | return renderTexture;
|
47 | }
|
48 | getFilterTexture(input, resolution, multisample) {
|
49 | const filterTexture = this.getOptimalTexture(input.width, input.height, resolution || input.resolution, multisample || MSAA_QUALITY.NONE);
|
50 | filterTexture.filterFrame = input.filterFrame;
|
51 | return filterTexture;
|
52 | }
|
53 | returnTexture(renderTexture) {
|
54 | const key = renderTexture.filterPoolKey;
|
55 | renderTexture.filterFrame = null;
|
56 | this.texturePool[key].push(renderTexture);
|
57 | }
|
58 | returnFilterTexture(renderTexture) {
|
59 | this.returnTexture(renderTexture);
|
60 | }
|
61 | clear(destroyTextures) {
|
62 | destroyTextures = destroyTextures !== false;
|
63 | if (destroyTextures) {
|
64 | for (const i in this.texturePool) {
|
65 | const textures = this.texturePool[i];
|
66 | if (textures) {
|
67 | for (let j = 0; j < textures.length; j++) {
|
68 | textures[j].destroy(true);
|
69 | }
|
70 | }
|
71 | }
|
72 | }
|
73 | this.texturePool = {};
|
74 | }
|
75 | setScreenSize(size) {
|
76 | if (size.width === this._pixelsWidth && size.height === this._pixelsHeight) {
|
77 | return;
|
78 | }
|
79 | this.enableFullScreen = size.width > 0 && size.height > 0;
|
80 | for (const i in this.texturePool) {
|
81 | if (!(Number(i) < 0)) {
|
82 | continue;
|
83 | }
|
84 | const textures = this.texturePool[i];
|
85 | if (textures) {
|
86 | for (let j = 0; j < textures.length; j++) {
|
87 | textures[j].destroy(true);
|
88 | }
|
89 | }
|
90 | this.texturePool[i] = [];
|
91 | }
|
92 | this._pixelsWidth = size.width;
|
93 | this._pixelsHeight = size.height;
|
94 | }
|
95 | }
|
96 | RenderTexturePool.SCREEN_KEY = -1;
|
97 |
|
98 | export { RenderTexturePool };
|
99 |
|