1 | import { SAMPLER_TYPES, TYPES, MIPMAP_MODES, WRAP_MODES, SCALE_MODES } from '@pixi/constants';
|
2 | import { ExtensionType, extensions } from '@pixi/extensions';
|
3 | import { removeItems } from '@pixi/utils';
|
4 | import { BaseTexture } from './BaseTexture.mjs';
|
5 | import { GLTexture } from './GLTexture.mjs';
|
6 | import { mapTypeAndFormatToInternalFormat } from './utils/mapTypeAndFormatToInternalFormat.mjs';
|
7 |
|
8 | class TextureSystem {
|
9 | constructor(renderer) {
|
10 | this.renderer = renderer;
|
11 | this.boundTextures = [];
|
12 | this.currentLocation = -1;
|
13 | this.managedTextures = [];
|
14 | this._unknownBoundTextures = false;
|
15 | this.unknownTexture = new BaseTexture();
|
16 | this.hasIntegerTextures = false;
|
17 | }
|
18 | contextChange() {
|
19 | const gl = this.gl = this.renderer.gl;
|
20 | this.CONTEXT_UID = this.renderer.CONTEXT_UID;
|
21 | this.webGLVersion = this.renderer.context.webGLVersion;
|
22 | this.internalFormats = mapTypeAndFormatToInternalFormat(gl);
|
23 | const maxTextures = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
|
24 | this.boundTextures.length = maxTextures;
|
25 | for (let i = 0; i < maxTextures; i++) {
|
26 | this.boundTextures[i] = null;
|
27 | }
|
28 | this.emptyTextures = {};
|
29 | const emptyTexture2D = new GLTexture(gl.createTexture());
|
30 | gl.bindTexture(gl.TEXTURE_2D, emptyTexture2D.texture);
|
31 | gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(4));
|
32 | this.emptyTextures[gl.TEXTURE_2D] = emptyTexture2D;
|
33 | this.emptyTextures[gl.TEXTURE_CUBE_MAP] = new GLTexture(gl.createTexture());
|
34 | gl.bindTexture(gl.TEXTURE_CUBE_MAP, this.emptyTextures[gl.TEXTURE_CUBE_MAP].texture);
|
35 | for (let i = 0; i < 6; i++) {
|
36 | gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
37 | }
|
38 | gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
39 | gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
40 | for (let i = 0; i < this.boundTextures.length; i++) {
|
41 | this.bind(null, i);
|
42 | }
|
43 | }
|
44 | bind(texture, location = 0) {
|
45 | const { gl } = this;
|
46 | texture = texture?.castToBaseTexture();
|
47 | if (texture?.valid && !texture.parentTextureArray) {
|
48 | texture.touched = this.renderer.textureGC.count;
|
49 | const glTexture = texture._glTextures[this.CONTEXT_UID] || this.initTexture(texture);
|
50 | if (this.boundTextures[location] !== texture) {
|
51 | if (this.currentLocation !== location) {
|
52 | this.currentLocation = location;
|
53 | gl.activeTexture(gl.TEXTURE0 + location);
|
54 | }
|
55 | gl.bindTexture(texture.target, glTexture.texture);
|
56 | }
|
57 | if (glTexture.dirtyId !== texture.dirtyId) {
|
58 | if (this.currentLocation !== location) {
|
59 | this.currentLocation = location;
|
60 | gl.activeTexture(gl.TEXTURE0 + location);
|
61 | }
|
62 | this.updateTexture(texture);
|
63 | } else if (glTexture.dirtyStyleId !== texture.dirtyStyleId) {
|
64 | this.updateTextureStyle(texture);
|
65 | }
|
66 | this.boundTextures[location] = texture;
|
67 | } else {
|
68 | if (this.currentLocation !== location) {
|
69 | this.currentLocation = location;
|
70 | gl.activeTexture(gl.TEXTURE0 + location);
|
71 | }
|
72 | gl.bindTexture(gl.TEXTURE_2D, this.emptyTextures[gl.TEXTURE_2D].texture);
|
73 | this.boundTextures[location] = null;
|
74 | }
|
75 | }
|
76 | reset() {
|
77 | this._unknownBoundTextures = true;
|
78 | this.hasIntegerTextures = false;
|
79 | this.currentLocation = -1;
|
80 | for (let i = 0; i < this.boundTextures.length; i++) {
|
81 | this.boundTextures[i] = this.unknownTexture;
|
82 | }
|
83 | }
|
84 | unbind(texture) {
|
85 | const { gl, boundTextures } = this;
|
86 | if (this._unknownBoundTextures) {
|
87 | this._unknownBoundTextures = false;
|
88 | for (let i = 0; i < boundTextures.length; i++) {
|
89 | if (boundTextures[i] === this.unknownTexture) {
|
90 | this.bind(null, i);
|
91 | }
|
92 | }
|
93 | }
|
94 | for (let i = 0; i < boundTextures.length; i++) {
|
95 | if (boundTextures[i] === texture) {
|
96 | if (this.currentLocation !== i) {
|
97 | gl.activeTexture(gl.TEXTURE0 + i);
|
98 | this.currentLocation = i;
|
99 | }
|
100 | gl.bindTexture(texture.target, this.emptyTextures[texture.target].texture);
|
101 | boundTextures[i] = null;
|
102 | }
|
103 | }
|
104 | }
|
105 | ensureSamplerType(maxTextures) {
|
106 | const { boundTextures, hasIntegerTextures, CONTEXT_UID } = this;
|
107 | if (!hasIntegerTextures) {
|
108 | return;
|
109 | }
|
110 | for (let i = maxTextures - 1; i >= 0; --i) {
|
111 | const tex = boundTextures[i];
|
112 | if (tex) {
|
113 | const glTexture = tex._glTextures[CONTEXT_UID];
|
114 | if (glTexture.samplerType !== SAMPLER_TYPES.FLOAT) {
|
115 | this.renderer.texture.unbind(tex);
|
116 | }
|
117 | }
|
118 | }
|
119 | }
|
120 | initTexture(texture) {
|
121 | const glTexture = new GLTexture(this.gl.createTexture());
|
122 | glTexture.dirtyId = -1;
|
123 | texture._glTextures[this.CONTEXT_UID] = glTexture;
|
124 | this.managedTextures.push(texture);
|
125 | texture.on("dispose", this.destroyTexture, this);
|
126 | return glTexture;
|
127 | }
|
128 | initTextureType(texture, glTexture) {
|
129 | glTexture.internalFormat = this.internalFormats[texture.type]?.[texture.format] ?? texture.format;
|
130 | if (this.webGLVersion === 2 && texture.type === TYPES.HALF_FLOAT) {
|
131 | glTexture.type = this.gl.HALF_FLOAT;
|
132 | } else {
|
133 | glTexture.type = texture.type;
|
134 | }
|
135 | }
|
136 | updateTexture(texture) {
|
137 | const glTexture = texture._glTextures[this.CONTEXT_UID];
|
138 | if (!glTexture) {
|
139 | return;
|
140 | }
|
141 | const renderer = this.renderer;
|
142 | this.initTextureType(texture, glTexture);
|
143 | if (texture.resource?.upload(renderer, texture, glTexture)) {
|
144 | if (glTexture.samplerType !== SAMPLER_TYPES.FLOAT) {
|
145 | this.hasIntegerTextures = true;
|
146 | }
|
147 | } else {
|
148 | const width = texture.realWidth;
|
149 | const height = texture.realHeight;
|
150 | const gl = renderer.gl;
|
151 | if (glTexture.width !== width || glTexture.height !== height || glTexture.dirtyId < 0) {
|
152 | glTexture.width = width;
|
153 | glTexture.height = height;
|
154 | gl.texImage2D(texture.target, 0, glTexture.internalFormat, width, height, 0, texture.format, glTexture.type, null);
|
155 | }
|
156 | }
|
157 | if (texture.dirtyStyleId !== glTexture.dirtyStyleId) {
|
158 | this.updateTextureStyle(texture);
|
159 | }
|
160 | glTexture.dirtyId = texture.dirtyId;
|
161 | }
|
162 | destroyTexture(texture, skipRemove) {
|
163 | const { gl } = this;
|
164 | texture = texture.castToBaseTexture();
|
165 | if (texture._glTextures[this.CONTEXT_UID]) {
|
166 | this.unbind(texture);
|
167 | gl.deleteTexture(texture._glTextures[this.CONTEXT_UID].texture);
|
168 | texture.off("dispose", this.destroyTexture, this);
|
169 | delete texture._glTextures[this.CONTEXT_UID];
|
170 | if (!skipRemove) {
|
171 | const i = this.managedTextures.indexOf(texture);
|
172 | if (i !== -1) {
|
173 | removeItems(this.managedTextures, i, 1);
|
174 | }
|
175 | }
|
176 | }
|
177 | }
|
178 | updateTextureStyle(texture) {
|
179 | const glTexture = texture._glTextures[this.CONTEXT_UID];
|
180 | if (!glTexture) {
|
181 | return;
|
182 | }
|
183 | if ((texture.mipmap === MIPMAP_MODES.POW2 || this.webGLVersion !== 2) && !texture.isPowerOfTwo) {
|
184 | glTexture.mipmap = false;
|
185 | } else {
|
186 | glTexture.mipmap = texture.mipmap >= 1;
|
187 | }
|
188 | if (this.webGLVersion !== 2 && !texture.isPowerOfTwo) {
|
189 | glTexture.wrapMode = WRAP_MODES.CLAMP;
|
190 | } else {
|
191 | glTexture.wrapMode = texture.wrapMode;
|
192 | }
|
193 | if (texture.resource?.style(this.renderer, texture, glTexture)) {
|
194 | } else {
|
195 | this.setStyle(texture, glTexture);
|
196 | }
|
197 | glTexture.dirtyStyleId = texture.dirtyStyleId;
|
198 | }
|
199 | setStyle(texture, glTexture) {
|
200 | const gl = this.gl;
|
201 | if (glTexture.mipmap && texture.mipmap !== MIPMAP_MODES.ON_MANUAL) {
|
202 | gl.generateMipmap(texture.target);
|
203 | }
|
204 | gl.texParameteri(texture.target, gl.TEXTURE_WRAP_S, glTexture.wrapMode);
|
205 | gl.texParameteri(texture.target, gl.TEXTURE_WRAP_T, glTexture.wrapMode);
|
206 | if (glTexture.mipmap) {
|
207 | gl.texParameteri(texture.target, gl.TEXTURE_MIN_FILTER, texture.scaleMode === SCALE_MODES.LINEAR ? gl.LINEAR_MIPMAP_LINEAR : gl.NEAREST_MIPMAP_NEAREST);
|
208 | const anisotropicExt = this.renderer.context.extensions.anisotropicFiltering;
|
209 | if (anisotropicExt && texture.anisotropicLevel > 0 && texture.scaleMode === SCALE_MODES.LINEAR) {
|
210 | const level = Math.min(texture.anisotropicLevel, gl.getParameter(anisotropicExt.MAX_TEXTURE_MAX_ANISOTROPY_EXT));
|
211 | gl.texParameterf(texture.target, anisotropicExt.TEXTURE_MAX_ANISOTROPY_EXT, level);
|
212 | }
|
213 | } else {
|
214 | gl.texParameteri(texture.target, gl.TEXTURE_MIN_FILTER, texture.scaleMode === SCALE_MODES.LINEAR ? gl.LINEAR : gl.NEAREST);
|
215 | }
|
216 | gl.texParameteri(texture.target, gl.TEXTURE_MAG_FILTER, texture.scaleMode === SCALE_MODES.LINEAR ? gl.LINEAR : gl.NEAREST);
|
217 | }
|
218 | destroy() {
|
219 | this.renderer = null;
|
220 | }
|
221 | }
|
222 | TextureSystem.extension = {
|
223 | type: ExtensionType.RendererSystem,
|
224 | name: "texture"
|
225 | };
|
226 | extensions.add(TextureSystem);
|
227 |
|
228 | export { TextureSystem };
|
229 |
|