- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
import { RenderTexture } from './RenderTexture';
import { BaseRenderTexture } from './BaseRenderTexture';
import { nextPow2 } from '@pixi/utils';
import { MSAA_QUALITY } from '@pixi/constants';
import type { IBaseTextureOptions } from '../textures/BaseTexture';
import type { ISize } from '@pixi/math';
/**
* Texture pool, used by FilterSystem and plugins.
*
* Stores collection of temporary pow2 or screen-sized renderTextures
*
* If you use custom RenderTexturePool for your filters, you can use methods
* `getFilterTexture` and `returnFilterTexture` same as in
* @memberof PIXI
*/
export class RenderTexturePool
{
public textureOptions: IBaseTextureOptions;
/**
* Allow renderTextures of the same size as screen, not just pow2
*
* Automatically sets to true after `setScreenSize`
* @default false
*/
public enableFullScreen: boolean;
texturePool: {[x in string | number]: RenderTexture[]};
private _pixelsWidth: number;
private _pixelsHeight: number;
/**
* @param textureOptions - options that will be passed to BaseRenderTexture constructor
* @param {PIXI.SCALE_MODES} [textureOptions.scaleMode] - See {@link PIXI.SCALE_MODES} for possible values.
*/
constructor(textureOptions?: IBaseTextureOptions)
{
this.texturePool = {};
this.textureOptions = textureOptions || {};
this.enableFullScreen = false;
this._pixelsWidth = 0;
this._pixelsHeight = 0;
}
/**
* Creates texture with params that were specified in pool constructor.
* @param realWidth - Width of texture in pixels.
* @param realHeight - Height of texture in pixels.
* @param multisample - Number of samples of the framebuffer.
*/
createTexture(realWidth: number, realHeight: number, multisample = MSAA_QUALITY.NONE): RenderTexture
{
const baseRenderTexture = new BaseRenderTexture(Object.assign({
width: realWidth,
height: realHeight,
resolution: 1,
multisample,
}, this.textureOptions));
return new RenderTexture(baseRenderTexture);
}
/**
* Gets a Power-of-Two render texture or fullScreen texture
* @param minWidth - The minimum width of the render texture.
* @param minHeight - The minimum height of the render texture.
* @param resolution - The resolution of the render texture.
* @param multisample - Number of samples of the render texture.
* @returns The new render texture.
*/
getOptimalTexture(minWidth: number, minHeight: number, resolution = 1, multisample = MSAA_QUALITY.NONE): RenderTexture
{
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 & 0xFFFF) << 16) | (minHeight & 0xFFFF)) >>> 0;
if (multisample > 1)
{
key += multisample * 0x100000000;
}
}
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;
}
/**
* Gets extra texture of the same size as input renderTexture
*
* `getFilterTexture(input, 0.5)` or `getFilterTexture(0.5, input)`
* @param input - renderTexture from which size and resolution will be copied
* @param resolution - override resolution of the renderTexture
* It overrides, it does not multiply
* @param multisample - number of samples of the renderTexture
*/
getFilterTexture(input: RenderTexture, resolution?: number, multisample?: MSAA_QUALITY): RenderTexture
{
const filterTexture = this.getOptimalTexture(input.width, input.height, resolution || input.resolution,
multisample || MSAA_QUALITY.NONE);
filterTexture.filterFrame = input.filterFrame;
return filterTexture;
}
/**
* Place a render texture back into the pool.
* @param renderTexture - The renderTexture to free
*/
returnTexture(renderTexture: RenderTexture): void
{
const key = renderTexture.filterPoolKey;
renderTexture.filterFrame = null;
this.texturePool[key].push(renderTexture);
}
/**
* Alias for returnTexture, to be compliant with FilterSystem interface.
* @param renderTexture - The renderTexture to free
*/
returnFilterTexture(renderTexture: RenderTexture): void
{
this.returnTexture(renderTexture);
}
/**
* Clears the pool.
* @param destroyTextures - Destroy all stored textures.
*/
clear(destroyTextures?: boolean): void
{
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 = {};
}
/**
* If screen size was changed, drops all screen-sized textures,
* sets new screen size, sets `enableFullScreen` to true
*
* Size is measured in pixels, `renderer.view` can be passed here, not `renderer.screen`
* @param size - Initial size of screen.
*/
setScreenSize(size: ISize): void
{
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;
}
/**
* Key that is used to store fullscreen renderTextures in a pool
* @constant
*/
static SCREEN_KEY = -1;
}