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';
/**
* Frame buffer used by the BaseRenderTexture
*
* @class
* @memberof PIXI
*/
export class Framebuffer {
/**
* @param {number} width - Width of the frame buffer
* @param {number} height - Height of the frame buffer
*/
constructor(width, height) {
/**
* Width of framebuffer in pixels
* @member {number}
*/
this.width = Math.ceil(width || 100);
/**
* Height of framebuffer in pixels
* @member {number}
*/
this.height = Math.ceil(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');
/**
* 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(renderTexture, myContainer);
* renderer.framebuffer.blit(); // copies data from MSAA framebuffer to texture
* ```
*
* @member {PIXI.MSAA_QUALITY}
* @default PIXI.MSAA_QUALITY.NONE
*/
this.multisample = MSAA_QUALITY.NONE;
}
/**
* Reference to the colorTexture.
*
* @member {PIXI.BaseTexture[]}
* @readonly
*/
get colorTexture() {
return this.colorTextures[0];
}
/**
* Add texture to the colorTexture array
*
* @param {number} [index=0] - Index of the array to add the texture to
* @param {PIXI.BaseTexture} [texture] - Texture to add to the array
*/
addColorTexture(index = 0, texture) {
// 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 {PIXI.BaseTexture} [texture] - Texture to add
*/
addDepthTexture(texture) {
/* 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.depth = true;
this.dirtyId++;
this.dirtyFormat++;
return this;
}
/**
* Enable stencil on the frame buffer
*/
enableStencil() {
this.stencil = true;
this.dirtyId++;
this.dirtyFormat++;
return this;
}
/**
* Resize the frame buffer
*
* @param {number} width - Width of the frame buffer to resize to
* @param {number} height - Height of the frame buffer to resize to
*/
resize(width, height) {
width = Math.ceil(width);
height = Math.ceil(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 acount 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() {
this.disposeRunner.emit(this, false);
}
/**
* Destroys and removes the depth texture added to this framebuffer.
*/
destroyDepthTexture() {
if (this.depthTexture) {
this.depthTexture.destroy();
this.depthTexture = null;
++this.dirtyId;
++this.dirtyFormat;
}
}
}