1 | "use strict";
|
2 | var constants = require("@pixi/constants"), extensions = require("@pixi/extensions"), utils = require("@pixi/utils"), BaseTexture = require("./BaseTexture.js"), GLTexture = require("./GLTexture.js"), mapInternalFormatToSamplerType = require("./utils/mapInternalFormatToSamplerType.js"), mapTypeAndFormatToInternalFormat = require("./utils/mapTypeAndFormatToInternalFormat.js");
|
3 | class TextureSystem {
|
4 | |
5 |
|
6 |
|
7 | constructor(renderer) {
|
8 | this.renderer = renderer, this.boundTextures = [], this.currentLocation = -1, this.managedTextures = [], this._unknownBoundTextures = !1, this.unknownTexture = new BaseTexture.BaseTexture(), this.hasIntegerTextures = !1;
|
9 | }
|
10 |
|
11 | contextChange() {
|
12 | const gl = this.gl = this.renderer.gl;
|
13 | this.CONTEXT_UID = this.renderer.CONTEXT_UID, this.webGLVersion = this.renderer.context.webGLVersion, this.internalFormats = mapTypeAndFormatToInternalFormat.mapTypeAndFormatToInternalFormat(gl), this.samplerTypes = mapInternalFormatToSamplerType.mapInternalFormatToSamplerType(gl);
|
14 | const maxTextures = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
|
15 | this.boundTextures.length = maxTextures;
|
16 | for (let i = 0; i < maxTextures; i++)
|
17 | this.boundTextures[i] = null;
|
18 | this.emptyTextures = {};
|
19 | const emptyTexture2D = new GLTexture.GLTexture(gl.createTexture());
|
20 | 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.GLTexture(gl.createTexture()), gl.bindTexture(gl.TEXTURE_CUBE_MAP, this.emptyTextures[gl.TEXTURE_CUBE_MAP].texture);
|
21 | for (let i = 0; i < 6; i++)
|
22 | gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
23 | gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR), gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
24 | for (let i = 0; i < this.boundTextures.length; i++)
|
25 | this.bind(null, i);
|
26 | }
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | bind(texture, location = 0) {
|
35 | const { gl } = this;
|
36 | if (texture = texture?.castToBaseTexture(), texture?.valid && !texture.parentTextureArray) {
|
37 | texture.touched = this.renderer.textureGC.count;
|
38 | const glTexture = texture._glTextures[this.CONTEXT_UID] || this.initTexture(texture);
|
39 | this.boundTextures[location] !== texture && (this.currentLocation !== location && (this.currentLocation = location, gl.activeTexture(gl.TEXTURE0 + location)), gl.bindTexture(texture.target, glTexture.texture)), glTexture.dirtyId !== texture.dirtyId ? (this.currentLocation !== location && (this.currentLocation = location, gl.activeTexture(gl.TEXTURE0 + location)), this.updateTexture(texture)) : glTexture.dirtyStyleId !== texture.dirtyStyleId && this.updateTextureStyle(texture), this.boundTextures[location] = texture;
|
40 | } else
|
41 | 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;
|
42 | }
|
43 |
|
44 | reset() {
|
45 | this._unknownBoundTextures = !0, this.hasIntegerTextures = !1, this.currentLocation = -1;
|
46 | for (let i = 0; i < this.boundTextures.length; i++)
|
47 | this.boundTextures[i] = this.unknownTexture;
|
48 | }
|
49 | |
50 |
|
51 |
|
52 |
|
53 | unbind(texture) {
|
54 | const { gl, boundTextures } = this;
|
55 | if (this._unknownBoundTextures) {
|
56 | this._unknownBoundTextures = !1;
|
57 | for (let i = 0; i < boundTextures.length; i++)
|
58 | boundTextures[i] === this.unknownTexture && this.bind(null, i);
|
59 | }
|
60 | for (let i = 0; i < boundTextures.length; i++)
|
61 | boundTextures[i] === texture && (this.currentLocation !== i && (gl.activeTexture(gl.TEXTURE0 + i), this.currentLocation = i), gl.bindTexture(texture.target, this.emptyTextures[texture.target].texture), boundTextures[i] = null);
|
62 | }
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | ensureSamplerType(maxTextures) {
|
69 | const { boundTextures, hasIntegerTextures, CONTEXT_UID } = this;
|
70 | if (hasIntegerTextures)
|
71 | for (let i = maxTextures - 1; i >= 0; --i) {
|
72 | const tex = boundTextures[i];
|
73 | tex && tex._glTextures[CONTEXT_UID].samplerType !== constants.SAMPLER_TYPES.FLOAT && this.renderer.texture.unbind(tex);
|
74 | }
|
75 | }
|
76 | |
77 |
|
78 |
|
79 |
|
80 |
|
81 | initTexture(texture) {
|
82 | const glTexture = new GLTexture.GLTexture(this.gl.createTexture());
|
83 | return glTexture.dirtyId = -1, texture._glTextures[this.CONTEXT_UID] = glTexture, this.managedTextures.push(texture), texture.on("dispose", this.destroyTexture, this), glTexture;
|
84 | }
|
85 | initTextureType(texture, glTexture) {
|
86 | glTexture.internalFormat = this.internalFormats[texture.type]?.[texture.format] ?? texture.format, glTexture.samplerType = this.samplerTypes[glTexture.internalFormat] ?? constants.SAMPLER_TYPES.FLOAT, this.webGLVersion === 2 && texture.type === constants.TYPES.HALF_FLOAT ? glTexture.type = this.gl.HALF_FLOAT : glTexture.type = texture.type;
|
87 | }
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 | updateTexture(texture) {
|
94 | const glTexture = texture._glTextures[this.CONTEXT_UID];
|
95 | if (!glTexture)
|
96 | return;
|
97 | const renderer = this.renderer;
|
98 | if (this.initTextureType(texture, glTexture), texture.resource?.upload(renderer, texture, glTexture))
|
99 | glTexture.samplerType !== constants.SAMPLER_TYPES.FLOAT && (this.hasIntegerTextures = !0);
|
100 | else {
|
101 | const width = texture.realWidth, height = texture.realHeight, gl = renderer.gl;
|
102 | (glTexture.width !== width || glTexture.height !== height || glTexture.dirtyId < 0) && (glTexture.width = width, glTexture.height = height, gl.texImage2D(
|
103 | texture.target,
|
104 | 0,
|
105 | glTexture.internalFormat,
|
106 | width,
|
107 | height,
|
108 | 0,
|
109 | texture.format,
|
110 | glTexture.type,
|
111 | null
|
112 | ));
|
113 | }
|
114 | texture.dirtyStyleId !== glTexture.dirtyStyleId && this.updateTextureStyle(texture), glTexture.dirtyId = texture.dirtyId;
|
115 | }
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | destroyTexture(texture, skipRemove) {
|
123 | const { gl } = this;
|
124 | if (texture = texture.castToBaseTexture(), 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], !skipRemove)) {
|
125 | const i = this.managedTextures.indexOf(texture);
|
126 | i !== -1 && utils.removeItems(this.managedTextures, i, 1);
|
127 | }
|
128 | }
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 | updateTextureStyle(texture) {
|
135 | const glTexture = texture._glTextures[this.CONTEXT_UID];
|
136 | glTexture && ((texture.mipmap === constants.MIPMAP_MODES.POW2 || this.webGLVersion !== 2) && !texture.isPowerOfTwo ? glTexture.mipmap = !1 : glTexture.mipmap = texture.mipmap >= 1, this.webGLVersion !== 2 && !texture.isPowerOfTwo ? glTexture.wrapMode = constants.WRAP_MODES.CLAMP : glTexture.wrapMode = texture.wrapMode, texture.resource?.style(this.renderer, texture, glTexture) || this.setStyle(texture, glTexture), glTexture.dirtyStyleId = texture.dirtyStyleId);
|
137 | }
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | setStyle(texture, glTexture) {
|
145 | const gl = this.gl;
|
146 | if (glTexture.mipmap && texture.mipmap !== constants.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), glTexture.mipmap) {
|
147 | gl.texParameteri(texture.target, gl.TEXTURE_MIN_FILTER, texture.scaleMode === constants.SCALE_MODES.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST);
|
148 | const anisotropicExt = this.renderer.context.extensions.anisotropicFiltering;
|
149 | if (anisotropicExt && texture.anisotropicLevel > 0 && texture.scaleMode === constants.SCALE_MODES.LINEAR) {
|
150 | const level = Math.min(texture.anisotropicLevel, gl.getParameter(anisotropicExt.MAX_TEXTURE_MAX_ANISOTROPY_EXT));
|
151 | gl.texParameterf(texture.target, anisotropicExt.TEXTURE_MAX_ANISOTROPY_EXT, level);
|
152 | }
|
153 | } else
|
154 | gl.texParameteri(texture.target, gl.TEXTURE_MIN_FILTER, texture.scaleMode === constants.SCALE_MODES.LINEAR ? gl.LINEAR : gl.NEAREST);
|
155 | gl.texParameteri(texture.target, gl.TEXTURE_MAG_FILTER, texture.scaleMode === constants.SCALE_MODES.LINEAR ? gl.LINEAR : gl.NEAREST);
|
156 | }
|
157 | destroy() {
|
158 | this.renderer = null;
|
159 | }
|
160 | }
|
161 | TextureSystem.extension = {
|
162 | type: extensions.ExtensionType.RendererSystem,
|
163 | name: "texture"
|
164 | };
|
165 | extensions.extensions.add(TextureSystem);
|
166 | exports.TextureSystem = TextureSystem;
|
167 |
|