- 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
import { Runner } from '@pixi/runner';
import { BaseTexture } from '../textures/BaseTexture';
import { DepthResource } from '../textures/resources/DepthResource';
import { FORMATS, MIPMAP_MODES, SCALE_MODES, TYPES, MSAA_QUALITY } from '@pixi/constants';
import type { GLFramebuffer } from './GLFramebuffer';
/**
* A framebuffer can be used to render contents off of the screen. {@link PIXI.BaseRenderTexture} uses
* one internally to render into itself. You can attach a depth or stencil buffer to a framebuffer.
*
* On WebGL 2 machines, shaders can output to multiple textures simultaneously with GLSL 300 ES.
* @memberof PIXI
*/
export class Framebuffer
{
/** Width of framebuffer in pixels. */
public width: number;
/** Height of framebuffer in pixels. */
public height: number;
/**
* Desired number of samples for antialiasing. 0 means AA should not be used.
*
* Experimental WebGL2 feature, allows to use antialiasing in individual renderTextures.
* Antialiasing is the same as for main buffer with renderer `antialias:true` options.
* Seriously affects GPU memory consumption and GPU performance.
*
*```js
* renderTexture.framebuffer.multisample = PIXI.MSAA_QUALITY.HIGH;
* //...
* renderer.render(myContainer, {renderTexture});
* renderer.framebuffer.blit(); // copies data from MSAA framebuffer to texture
* ```
* @default PIXI.MSAA_QUALITY.NONE
*/
public multisample: MSAA_QUALITY;
stencil: boolean;
depth: boolean;
dirtyId: number;
dirtyFormat: number;
dirtySize: number;
depthTexture: BaseTexture;
colorTextures: Array<BaseTexture>;
glFramebuffers: {[key: string]: GLFramebuffer};
disposeRunner: Runner;
/**
* @param width - Width of the frame buffer
* @param height - Height of the frame buffer
*/
constructor(width: number, height: number)
{
this.width = Math.round(width || 100);
this.height = Math.round(height || 100);
this.stencil = false;
this.depth = false;
this.dirtyId = 0;
this.dirtyFormat = 0;
this.dirtySize = 0;
this.depthTexture = null;
this.colorTextures = [];
this.glFramebuffers = {};
this.disposeRunner = new Runner('disposeFramebuffer');
this.multisample = MSAA_QUALITY.NONE;
}
/**
* Reference to the colorTexture.
* @readonly
*/
get colorTexture(): BaseTexture
{
return this.colorTextures[0];
}
/**
* Add texture to the colorTexture array.
* @param index - Index of the array to add the texture to
* @param texture - Texture to add to the array
*/
addColorTexture(index = 0, texture?: BaseTexture): this
{
// TODO add some validation to the texture - same width / height etc?
this.colorTextures[index] = texture || new BaseTexture(null, {
scaleMode: SCALE_MODES.NEAREST,
resolution: 1,
mipmap: MIPMAP_MODES.OFF,
width: this.width,
height: this.height,
});
this.dirtyId++;
this.dirtyFormat++;
return this;
}
/**
* Add a depth texture to the frame buffer.
* @param texture - Texture to add.
*/
addDepthTexture(texture?: BaseTexture): this
{
/* eslint-disable max-len */
this.depthTexture = texture || new BaseTexture(new DepthResource(null, { width: this.width, height: this.height }), {
scaleMode: SCALE_MODES.NEAREST,
resolution: 1,
width: this.width,
height: this.height,
mipmap: MIPMAP_MODES.OFF,
format: FORMATS.DEPTH_COMPONENT,
type: TYPES.UNSIGNED_SHORT,
});
this.dirtyId++;
this.dirtyFormat++;
return this;
}
/** Enable depth on the frame buffer. */
enableDepth(): this
{
this.depth = true;
this.dirtyId++;
this.dirtyFormat++;
return this;
}
/** Enable stencil on the frame buffer. */
enableStencil(): this
{
this.stencil = true;
this.dirtyId++;
this.dirtyFormat++;
return this;
}
/**
* Resize the frame buffer
* @param width - Width of the frame buffer to resize to
* @param height - Height of the frame buffer to resize to
*/
resize(width: number, height: number): void
{
width = Math.round(width);
height = Math.round(height);
if (width === this.width && height === this.height) return;
this.width = width;
this.height = height;
this.dirtyId++;
this.dirtySize++;
for (let i = 0; i < this.colorTextures.length; i++)
{
const texture = this.colorTextures[i];
const resolution = texture.resolution;
// take into account the fact the texture may have a different resolution..
texture.setSize(width / resolution, height / resolution);
}
if (this.depthTexture)
{
const resolution = this.depthTexture.resolution;
this.depthTexture.setSize(width / resolution, height / resolution);
}
}
/** Disposes WebGL resources that are connected to this geometry. */
dispose(): void
{
this.disposeRunner.emit(this, false);
}
/** Destroys and removes the depth texture added to this framebuffer. */
destroyDepthTexture(): void
{
if (this.depthTexture)
{
this.depthTexture.destroy();
this.depthTexture = null;
++this.dirtyId;
++this.dirtyFormat;
}
}
}