@pixi/core
Version:
Core PixiJS
230 lines (227 loc) • 8.88 kB
JavaScript
import { SAMPLER_TYPES, TYPES, MIPMAP_MODES, WRAP_MODES, SCALE_MODES } from '@pixi/constants';
import { ExtensionType, extensions } from '@pixi/extensions';
import { removeItems } from '@pixi/utils';
import { BaseTexture } from './BaseTexture.mjs';
import { GLTexture } from './GLTexture.mjs';
import { mapTypeAndFormatToInternalFormat } from './utils/mapTypeAndFormatToInternalFormat.mjs';
class TextureSystem {
constructor(renderer) {
this.renderer = renderer;
this.boundTextures = [];
this.currentLocation = -1;
this.managedTextures = [];
this._unknownBoundTextures = false;
this.unknownTexture = new BaseTexture();
this.hasIntegerTextures = false;
}
contextChange() {
const gl = this.gl = this.renderer.gl;
this.CONTEXT_UID = this.renderer.CONTEXT_UID;
this.webGLVersion = this.renderer.context.webGLVersion;
this.internalFormats = mapTypeAndFormatToInternalFormat(gl);
const maxTextures = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
this.boundTextures.length = maxTextures;
for (let i = 0; i < maxTextures; i++) {
this.boundTextures[i] = null;
}
this.emptyTextures = {};
const emptyTexture2D = new GLTexture(gl.createTexture());
gl.bindTexture(gl.TEXTURE_2D, emptyTexture2D.texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(4));
this.emptyTextures[gl.TEXTURE_2D] = emptyTexture2D;
this.emptyTextures[gl.TEXTURE_CUBE_MAP] = new GLTexture(gl.createTexture());
gl.bindTexture(gl.TEXTURE_CUBE_MAP, this.emptyTextures[gl.TEXTURE_CUBE_MAP].texture);
for (let i = 0; i < 6; i++) {
gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
}
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
for (let i = 0; i < this.boundTextures.length; i++) {
this.bind(null, i);
}
}
bind(texture, location = 0) {
const { gl } = this;
texture = texture?.castToBaseTexture();
if (texture?.valid && !texture.parentTextureArray) {
texture.touched = this.renderer.textureGC.count;
const glTexture = texture._glTextures[this.CONTEXT_UID] || this.initTexture(texture);
if (this.boundTextures[location] !== texture) {
if (this.currentLocation !== location) {
this.currentLocation = location;
gl.activeTexture(gl.TEXTURE0 + location);
}
gl.bindTexture(texture.target, glTexture.texture);
}
if (glTexture.dirtyId !== texture.dirtyId) {
if (this.currentLocation !== location) {
this.currentLocation = location;
gl.activeTexture(gl.TEXTURE0 + location);
}
this.updateTexture(texture);
} else if (glTexture.dirtyStyleId !== texture.dirtyStyleId) {
this.updateTextureStyle(texture);
}
this.boundTextures[location] = texture;
} else {
if (this.currentLocation !== location) {
this.currentLocation = location;
gl.activeTexture(gl.TEXTURE0 + location);
}
gl.bindTexture(gl.TEXTURE_2D, this.emptyTextures[gl.TEXTURE_2D].texture);
this.boundTextures[location] = null;
}
}
reset() {
this._unknownBoundTextures = true;
this.hasIntegerTextures = false;
this.currentLocation = -1;
for (let i = 0; i < this.boundTextures.length; i++) {
this.boundTextures[i] = this.unknownTexture;
}
}
unbind(texture) {
const { gl, boundTextures } = this;
if (this._unknownBoundTextures) {
this._unknownBoundTextures = false;
for (let i = 0; i < boundTextures.length; i++) {
if (boundTextures[i] === this.unknownTexture) {
this.bind(null, i);
}
}
}
for (let i = 0; i < boundTextures.length; i++) {
if (boundTextures[i] === texture) {
if (this.currentLocation !== i) {
gl.activeTexture(gl.TEXTURE0 + i);
this.currentLocation = i;
}
gl.bindTexture(texture.target, this.emptyTextures[texture.target].texture);
boundTextures[i] = null;
}
}
}
ensureSamplerType(maxTextures) {
const { boundTextures, hasIntegerTextures, CONTEXT_UID } = this;
if (!hasIntegerTextures) {
return;
}
for (let i = maxTextures - 1; i >= 0; --i) {
const tex = boundTextures[i];
if (tex) {
const glTexture = tex._glTextures[CONTEXT_UID];
if (glTexture.samplerType !== SAMPLER_TYPES.FLOAT) {
this.renderer.texture.unbind(tex);
}
}
}
}
initTexture(texture) {
const glTexture = new GLTexture(this.gl.createTexture());
glTexture.dirtyId = -1;
texture._glTextures[this.CONTEXT_UID] = glTexture;
this.managedTextures.push(texture);
texture.on("dispose", this.destroyTexture, this);
return glTexture;
}
initTextureType(texture, glTexture) {
glTexture.internalFormat = this.internalFormats[texture.type]?.[texture.format] ?? texture.format;
if (this.webGLVersion === 2 && texture.type === TYPES.HALF_FLOAT) {
glTexture.type = this.gl.HALF_FLOAT;
} else {
glTexture.type = texture.type;
}
}
updateTexture(texture) {
const glTexture = texture._glTextures[this.CONTEXT_UID];
if (!glTexture) {
return;
}
const renderer = this.renderer;
this.initTextureType(texture, glTexture);
if (texture.resource?.upload(renderer, texture, glTexture)) {
if (glTexture.samplerType !== SAMPLER_TYPES.FLOAT) {
this.hasIntegerTextures = true;
}
} else {
const width = texture.realWidth;
const height = texture.realHeight;
const gl = renderer.gl;
if (glTexture.width !== width || glTexture.height !== height || glTexture.dirtyId < 0) {
glTexture.width = width;
glTexture.height = height;
gl.texImage2D(texture.target, 0, glTexture.internalFormat, width, height, 0, texture.format, glTexture.type, null);
}
}
if (texture.dirtyStyleId !== glTexture.dirtyStyleId) {
this.updateTextureStyle(texture);
}
glTexture.dirtyId = texture.dirtyId;
}
destroyTexture(texture, skipRemove) {
const { gl } = this;
texture = texture.castToBaseTexture();
if (texture._glTextures[this.CONTEXT_UID]) {
this.unbind(texture);
gl.deleteTexture(texture._glTextures[this.CONTEXT_UID].texture);
texture.off("dispose", this.destroyTexture, this);
delete texture._glTextures[this.CONTEXT_UID];
if (!skipRemove) {
const i = this.managedTextures.indexOf(texture);
if (i !== -1) {
removeItems(this.managedTextures, i, 1);
}
}
}
}
updateTextureStyle(texture) {
const glTexture = texture._glTextures[this.CONTEXT_UID];
if (!glTexture) {
return;
}
if ((texture.mipmap === MIPMAP_MODES.POW2 || this.webGLVersion !== 2) && !texture.isPowerOfTwo) {
glTexture.mipmap = false;
} else {
glTexture.mipmap = texture.mipmap >= 1;
}
if (this.webGLVersion !== 2 && !texture.isPowerOfTwo) {
glTexture.wrapMode = WRAP_MODES.CLAMP;
} else {
glTexture.wrapMode = texture.wrapMode;
}
if (texture.resource?.style(this.renderer, texture, glTexture)) {
} else {
this.setStyle(texture, glTexture);
}
glTexture.dirtyStyleId = texture.dirtyStyleId;
}
setStyle(texture, glTexture) {
const gl = this.gl;
if (glTexture.mipmap && texture.mipmap !== MIPMAP_MODES.ON_MANUAL) {
gl.generateMipmap(texture.target);
}
gl.texParameteri(texture.target, gl.TEXTURE_WRAP_S, glTexture.wrapMode);
gl.texParameteri(texture.target, gl.TEXTURE_WRAP_T, glTexture.wrapMode);
if (glTexture.mipmap) {
gl.texParameteri(texture.target, gl.TEXTURE_MIN_FILTER, texture.scaleMode === SCALE_MODES.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST);
const anisotropicExt = this.renderer.context.extensions.anisotropicFiltering;
if (anisotropicExt && texture.anisotropicLevel > 0 && texture.scaleMode === SCALE_MODES.LINEAR) {
const level = Math.min(texture.anisotropicLevel, gl.getParameter(anisotropicExt.MAX_TEXTURE_MAX_ANISOTROPY_EXT));
gl.texParameterf(texture.target, anisotropicExt.TEXTURE_MAX_ANISOTROPY_EXT, level);
}
} else {
gl.texParameteri(texture.target, gl.TEXTURE_MIN_FILTER, texture.scaleMode === SCALE_MODES.LINEAR ? gl.LINEAR : gl.NEAREST);
}
gl.texParameteri(texture.target, gl.TEXTURE_MAG_FILTER, texture.scaleMode === SCALE_MODES.LINEAR ? gl.LINEAR : gl.NEAREST);
}
destroy() {
this.renderer = null;
}
}
TextureSystem.extension = {
type: ExtensionType.RendererSystem,
name: "texture"
};
extensions.add(TextureSystem);
export { TextureSystem };
//# sourceMappingURL=TextureSystem.mjs.map