@galacean/engine-rhi-webgl 0.9.0-beta.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.js ADDED
@@ -0,0 +1,1921 @@
1
+ import { Vector2, Vector4, Color } from '@galacean/engine-math';
2
+ import { GLCapabilityType, Logger, TextureWrapMode, TextureFormat, RenderBufferDepthFormat, TextureFilterMode, TextureDepthCompareFunction, Texture, TextureCube, CameraClearFlags, ColorWriteMask, SystemInfo, Platform, Engine } from '@galacean/engine-core';
3
+
4
+ /**
5
+ * Smoothing plug-in.
6
+ * */ var GLCompressedTextureInternalFormat;
7
+ (function(GLCompressedTextureInternalFormat) {
8
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat[// astc
9
+ "RGBA_ASTC_4X4_KHR"] = 0x93b0] = "RGBA_ASTC_4X4_KHR";
10
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_5X4_KHR"] = 0x93b1] = "RGBA_ASTC_5X4_KHR";
11
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_5X5_KHR"] = 0x93b2] = "RGBA_ASTC_5X5_KHR";
12
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_6X5_KHR"] = 0x93b3] = "RGBA_ASTC_6X5_KHR";
13
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_6X6_KHR"] = 0x93b4] = "RGBA_ASTC_6X6_KHR";
14
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_8X5_KHR"] = 0x93b5] = "RGBA_ASTC_8X5_KHR";
15
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_8X6_KHR"] = 0x93b6] = "RGBA_ASTC_8X6_KHR";
16
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_8X8_KHR"] = 0x93b7] = "RGBA_ASTC_8X8_KHR";
17
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_10X5_KHR"] = 0x93b8] = "RGBA_ASTC_10X5_KHR";
18
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_10X6_KHR"] = 0x93b9] = "RGBA_ASTC_10X6_KHR";
19
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_10X8_KHR"] = 0x93ba] = "RGBA_ASTC_10X8_KHR";
20
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_10X10_KHR"] = 0x93bb] = "RGBA_ASTC_10X10_KHR";
21
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_12X10_KHR"] = 0x93bc] = "RGBA_ASTC_12X10_KHR";
22
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_12X12_KHR"] = 0x93bd] = "RGBA_ASTC_12X12_KHR";
23
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_4X4_KHR"] = 0x93d0] = "SRGB8_ALPHA8_ASTC_4X4_KHR";
24
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_5X4_KHR"] = 0x93d1] = "SRGB8_ALPHA8_ASTC_5X4_KHR";
25
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_5X5_KHR"] = 0x93d2] = "SRGB8_ALPHA8_ASTC_5X5_KHR";
26
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_6X5_KHR"] = 0x93d3] = "SRGB8_ALPHA8_ASTC_6X5_KHR";
27
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_6X6_KHR"] = 0x93d4] = "SRGB8_ALPHA8_ASTC_6X6_KHR";
28
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_8X5_KHR"] = 0x93d5] = "SRGB8_ALPHA8_ASTC_8X5_KHR";
29
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_8X6_KHR"] = 0x93d6] = "SRGB8_ALPHA8_ASTC_8X6_KHR";
30
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_8X8_KHR"] = 0x93d7] = "SRGB8_ALPHA8_ASTC_8X8_KHR";
31
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_10X5_KHR"] = 0x93d8] = "SRGB8_ALPHA8_ASTC_10X5_KHR";
32
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_10X6_KHR"] = 0x93d9] = "SRGB8_ALPHA8_ASTC_10X6_KHR";
33
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_10X8_KHR"] = 0x93da] = "SRGB8_ALPHA8_ASTC_10X8_KHR";
34
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_10X10_KHR"] = 0x93db] = "SRGB8_ALPHA8_ASTC_10X10_KHR";
35
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_12X10_KHR"] = 0x93dc] = "SRGB8_ALPHA8_ASTC_12X10_KHR";
36
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_12X12_KHR"] = 0x93dd] = "SRGB8_ALPHA8_ASTC_12X12_KHR";
37
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat[// etc1
38
+ "RGB_ETC1_WEBGL"] = 0x8d64] = "RGB_ETC1_WEBGL";
39
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat[// etc2
40
+ "R11_EAC"] = 0x9270] = "R11_EAC";
41
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SIGNED_R11_EAC"] = 0x9271] = "SIGNED_R11_EAC";
42
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RG11_EAC"] = 0x9272] = "RG11_EAC";
43
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SIGNED_RG11_EAC"] = 0x9273] = "SIGNED_RG11_EAC";
44
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB8_ETC2"] = 0x9274] = "RGB8_ETC2";
45
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ETC2"] = 0x9275] = "SRGB8_ETC2";
46
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB8_PUNCHTHROUGH_ALPHA1_ETC2"] = 0x9276] = "RGB8_PUNCHTHROUGH_ALPHA1_ETC2";
47
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_PUNCHTHROUGH_ALPHA1_ETC2"] = 0x9277] = "SRGB8_PUNCHTHROUGH_ALPHA1_ETC2";
48
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA8_ETC2_EAC"] = 0x9278] = "RGBA8_ETC2_EAC";
49
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ETC2_EAC"] = 0x9279] = "SRGB8_ALPHA8_ETC2_EAC";
50
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat[// pvrtc
51
+ "RGB_PVRTC_4BPPV1_IMG"] = 0x8c00] = "RGB_PVRTC_4BPPV1_IMG";
52
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB_PVRTC_2BPPV1_IMG"] = 0x8c01] = "RGB_PVRTC_2BPPV1_IMG";
53
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_PVRTC_4BPPV1_IMG"] = 0x8c02] = "RGBA_PVRTC_4BPPV1_IMG";
54
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_PVRTC_2BPPV1_IMG"] = 0x8c03] = "RGBA_PVRTC_2BPPV1_IMG";
55
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat[// s3tc
56
+ "RGB_S3TC_DXT1_EXT"] = 0x83f0] = "RGB_S3TC_DXT1_EXT";
57
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT1_EXT"] = 0x83f1] = "RGBA_S3TC_DXT1_EXT";
58
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT3_EXT"] = 0x83f2] = "RGBA_S3TC_DXT3_EXT";
59
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT5_EXT"] = 0x83f3] = "RGBA_S3TC_DXT5_EXT";
60
+ })(GLCompressedTextureInternalFormat || (GLCompressedTextureInternalFormat = {}));
61
+
62
+ function _defineProperties(target, props) {
63
+ for (var i = 0; i < props.length; i++) {
64
+ var descriptor = props[i];
65
+ descriptor.enumerable = descriptor.enumerable || false;
66
+ descriptor.configurable = true;
67
+ if ("value" in descriptor) descriptor.writable = true;
68
+ Object.defineProperty(target, descriptor.key, descriptor);
69
+ }
70
+ }
71
+
72
+ function _createClass(Constructor, protoProps, staticProps) {
73
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
74
+ if (staticProps) _defineProperties(Constructor, staticProps);
75
+ return Constructor;
76
+ }
77
+
78
+ function _instanceof(left, right) {
79
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
80
+ return !!right[Symbol.hasInstance](left);
81
+ } else {
82
+ return left instanceof right;
83
+ }
84
+ }
85
+
86
+ /**
87
+ * The canvas used on the web, which can support HTMLCanvasElement and OffscreenCanvas.
88
+ */ var WebCanvas = /*#__PURE__*/ function() {
89
+ function WebCanvas(webCanvas) {
90
+ this._scale = new Vector2();
91
+ var width = webCanvas.width;
92
+ var height = webCanvas.height;
93
+ this._webCanvas = webCanvas;
94
+ this._width = width;
95
+ this._height = height;
96
+ }
97
+ var _proto = WebCanvas.prototype;
98
+ /**
99
+ * Resize the rendering size according to the clientWidth and clientHeight of the canvas.
100
+ * @param pixelRatio - Pixel ratio
101
+ */ _proto.resizeByClientSize = function resizeByClientSize(pixelRatio) {
102
+ if (pixelRatio === void 0) pixelRatio = window.devicePixelRatio;
103
+ var webCanvas = this._webCanvas;
104
+ if (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas)) {
105
+ this.width = webCanvas.clientWidth * pixelRatio;
106
+ this.height = webCanvas.clientHeight * pixelRatio;
107
+ }
108
+ };
109
+ /**
110
+ * Set scale.
111
+ * @param x - Scale along the X axis
112
+ * @param y - Scale along the Y axis
113
+ */ _proto.setScale = function setScale(x, y) {
114
+ this._scale.set(x, y);
115
+ this.scale = this._scale;
116
+ };
117
+ _createClass(WebCanvas, [
118
+ {
119
+ key: "width",
120
+ get: /**
121
+ * @inheritdoc
122
+ */ function get() {
123
+ return this._width;
124
+ },
125
+ set: function set(value) {
126
+ if (this._width !== value) {
127
+ this._webCanvas.width = value;
128
+ this._width = value;
129
+ }
130
+ }
131
+ },
132
+ {
133
+ key: "height",
134
+ get: /**
135
+ * @inheritdoc
136
+ */ function get() {
137
+ return this._height;
138
+ },
139
+ set: function set(value) {
140
+ if (this._height !== value) {
141
+ this._webCanvas.height = value;
142
+ this._height = value;
143
+ }
144
+ }
145
+ },
146
+ {
147
+ key: "scale",
148
+ get: /**
149
+ * The scale of canvas, the value is visible width/height divide the render width/height.
150
+ * @remarks Need to re-assign after modification to ensure that the modification takes effect.
151
+ */ function get() {
152
+ var webCanvas = this._webCanvas;
153
+ if (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas)) {
154
+ this._scale.set(webCanvas.clientWidth * devicePixelRatio / webCanvas.width, webCanvas.clientHeight * devicePixelRatio / webCanvas.height);
155
+ }
156
+ return this._scale;
157
+ },
158
+ set: function set(value) {
159
+ var webCanvas = this._webCanvas;
160
+ if (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas)) {
161
+ webCanvas.style.transformOrigin = "left top";
162
+ webCanvas.style.transform = "scale(" + value.x + ", " + value.y + ")";
163
+ }
164
+ }
165
+ }
166
+ ]);
167
+ return WebCanvas;
168
+ }();
169
+
170
+ function setPrototypeOf(o, p) {
171
+ setPrototypeOf = Object.setPrototypeOf || function setPrototypeOf(o, p) {
172
+ o.__proto__ = p;
173
+ return o;
174
+ };
175
+
176
+ return setPrototypeOf(o, p);
177
+ }
178
+
179
+ function _setPrototypeOf(o, p) {
180
+ return setPrototypeOf(o, p);
181
+ }
182
+
183
+ function _inherits(subClass, superClass) {
184
+ if (typeof superClass !== "function" && superClass !== null) {
185
+ throw new TypeError("Super expression must either be null or a function");
186
+ }
187
+
188
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
189
+ constructor: {
190
+ value: subClass,
191
+ writable: true,
192
+ configurable: true
193
+ }
194
+ });
195
+ if (superClass) _setPrototypeOf(subClass, superClass);
196
+ }
197
+
198
+ function extends_() {
199
+ extends_ = Object.assign || function (target) {
200
+ for (var i = 1; i < arguments.length; i++) {
201
+ var source = arguments[i];
202
+
203
+ for (var key in source) {
204
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
205
+ target[key] = source[key];
206
+ }
207
+ }
208
+ }
209
+
210
+ return target;
211
+ };
212
+
213
+ return extends_.apply(this, arguments);
214
+ }
215
+
216
+ function _extends() {
217
+ return extends_.apply(this, arguments);
218
+ }
219
+
220
+ /**
221
+ * GL capability.
222
+ */ var GLCapability = /*#__PURE__*/ function() {
223
+ function GLCapability(rhi) {
224
+ this._rhi = rhi;
225
+ this.capabilityList = new Map();
226
+ this._init();
227
+ this._compatibleAllInterface();
228
+ }
229
+ var _proto = GLCapability.prototype;
230
+ /**
231
+ * Check device capabilities.
232
+ */ _proto.canIUse = function canIUse(capabilityType) {
233
+ return this.capabilityList.get(capabilityType);
234
+ };
235
+ /**
236
+ * Check if can use some compressed texture format.
237
+ */ _proto.canIUseCompressedTextureInternalFormat = function canIUseCompressedTextureInternalFormat(internalType) {
238
+ var // astc
239
+ RGBA_ASTC_4X4_KHR = GLCompressedTextureInternalFormat.RGBA_ASTC_4X4_KHR, RGBA_ASTC_12X12_KHR = GLCompressedTextureInternalFormat.RGBA_ASTC_12X12_KHR, SRGB8_ALPHA8_ASTC_4X4_KHR = GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ASTC_4X4_KHR, SRGB8_ALPHA8_ASTC_12X12_KHR = GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ASTC_12X12_KHR, // etc1
240
+ RGB_ETC1_WEBGL = GLCompressedTextureInternalFormat.RGB_ETC1_WEBGL, // etc
241
+ R11_EAC = GLCompressedTextureInternalFormat.R11_EAC, SRGB8_ALPHA8_ETC2_EAC = GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ETC2_EAC, // pvrtc
242
+ RGB_PVRTC_4BPPV1_IMG = GLCompressedTextureInternalFormat.RGB_PVRTC_4BPPV1_IMG, RGBA_PVRTC_2BPPV1_IMG = GLCompressedTextureInternalFormat.RGBA_PVRTC_2BPPV1_IMG, // s3tc
243
+ RGB_S3TC_DXT1_EXT = GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT, RGBA_S3TC_DXT5_EXT = GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT;
244
+ if (internalType >= RGBA_ASTC_4X4_KHR && RGBA_ASTC_12X12_KHR <= RGBA_ASTC_12X12_KHR || internalType >= SRGB8_ALPHA8_ASTC_4X4_KHR && internalType <= SRGB8_ALPHA8_ASTC_12X12_KHR) {
245
+ return this.canIUse(GLCapabilityType.astc);
246
+ } else if (internalType === RGB_ETC1_WEBGL) {
247
+ return this.canIUse(GLCapabilityType.etc1);
248
+ } else if (internalType >= R11_EAC && internalType <= SRGB8_ALPHA8_ETC2_EAC) {
249
+ return this.canIUse(GLCapabilityType.etc);
250
+ } else if (internalType >= RGB_PVRTC_4BPPV1_IMG && internalType <= RGBA_PVRTC_2BPPV1_IMG) {
251
+ return this.canIUse(GLCapabilityType.pvrtc);
252
+ } else if (internalType >= RGB_S3TC_DXT1_EXT && internalType <= RGBA_S3TC_DXT5_EXT) {
253
+ return this.canIUse(GLCapabilityType.s3tc);
254
+ }
255
+ return false;
256
+ };
257
+ /**
258
+ * Init capabilities.
259
+ */ _proto._init = function _init() {
260
+ var cap = this.capabilityList;
261
+ var isWebGL2 = this.rhi.isWebGL2;
262
+ var requireExtension = this.rhi.requireExtension.bind(this.rhi);
263
+ var shaderVertexID = GLCapabilityType.shaderVertexID, standardDerivatives = GLCapabilityType.standardDerivatives, shaderTextureLod = GLCapabilityType.shaderTextureLod, elementIndexUint = GLCapabilityType.elementIndexUint, depthTexture = GLCapabilityType.depthTexture, vertexArrayObject = GLCapabilityType.vertexArrayObject, instancedArrays = GLCapabilityType.instancedArrays, multipleSample = GLCapabilityType.multipleSample, drawBuffers = GLCapabilityType.drawBuffers, astc = GLCapabilityType.astc, astc_webkit = GLCapabilityType.astc_webkit, etc = GLCapabilityType.etc, etc_webkit = GLCapabilityType.etc_webkit, etc1 = GLCapabilityType.etc1, etc1_webkit = GLCapabilityType.etc1_webkit, pvrtc = GLCapabilityType.pvrtc, pvrtc_webkit = GLCapabilityType.pvrtc_webkit, s3tc = GLCapabilityType.s3tc, s3tc_webkit = GLCapabilityType.s3tc_webkit, textureFloat = GLCapabilityType.textureFloat, textureHalfFloat = GLCapabilityType.textureHalfFloat, textureFloatLinear = GLCapabilityType.textureFloatLinear, textureHalfFloatLinear = GLCapabilityType.textureHalfFloatLinear, WEBGL_colorBufferFloat = GLCapabilityType.WEBGL_colorBufferFloat, colorBufferFloat = GLCapabilityType.colorBufferFloat, colorBufferHalfFloat = GLCapabilityType.colorBufferHalfFloat, textureFilterAnisotropic = GLCapabilityType.textureFilterAnisotropic;
264
+ cap.set(shaderVertexID, isWebGL2);
265
+ cap.set(standardDerivatives, isWebGL2 || !!requireExtension(standardDerivatives));
266
+ cap.set(shaderTextureLod, isWebGL2 || !!requireExtension(shaderTextureLod));
267
+ cap.set(elementIndexUint, isWebGL2 || !!requireExtension(elementIndexUint));
268
+ cap.set(depthTexture, isWebGL2 || !!requireExtension(depthTexture));
269
+ cap.set(vertexArrayObject, isWebGL2 || !!requireExtension(vertexArrayObject));
270
+ cap.set(instancedArrays, isWebGL2 || !!requireExtension(instancedArrays));
271
+ cap.set(multipleSample, isWebGL2);
272
+ cap.set(drawBuffers, isWebGL2 || !!requireExtension(drawBuffers));
273
+ cap.set(textureFloat, isWebGL2 || !!requireExtension(textureFloat));
274
+ cap.set(textureHalfFloat, isWebGL2 || !!requireExtension(textureHalfFloat));
275
+ cap.set(textureFloatLinear, !!requireExtension(textureFloatLinear));
276
+ cap.set(textureHalfFloatLinear, isWebGL2 || !!requireExtension(textureHalfFloatLinear));
277
+ cap.set(colorBufferFloat, isWebGL2 && !!requireExtension(colorBufferFloat) || !!requireExtension(WEBGL_colorBufferFloat));
278
+ cap.set(colorBufferHalfFloat, isWebGL2 && !!requireExtension(colorBufferFloat) || !!requireExtension(colorBufferHalfFloat));
279
+ cap.set(textureFilterAnisotropic, !!requireExtension(textureFilterAnisotropic));
280
+ cap.set(astc, !!(requireExtension(astc) || requireExtension(astc_webkit)));
281
+ cap.set(etc, !!(requireExtension(etc) || requireExtension(etc_webkit)));
282
+ cap.set(etc1, !!(requireExtension(etc1) || requireExtension(etc1_webkit)));
283
+ cap.set(pvrtc, !!(requireExtension(pvrtc) || requireExtension(pvrtc_webkit)));
284
+ cap.set(s3tc, !!(requireExtension(s3tc) || requireExtension(s3tc_webkit)));
285
+ };
286
+ /**
287
+ * If there are extensions that can supplement this ability, smooth out the difference.
288
+ * @example
289
+ * compatible(GLCapabilityType.depthTexture,{
290
+ * UNSIGNED_INT_24_8: "UNSIGNED_INT_24_8_WEBGL"
291
+ * })
292
+ * gl.UNSIGNED_INT_24_8 = ext.UNSIGNED_INT_24_8_WEBGL
293
+ */ _proto._compatibleInterface = function _compatibleInterface(capabilityType, flatItem) {
294
+ var rhi = this.rhi;
295
+ var gl = rhi.gl;
296
+ var ext = null;
297
+ if (ext = rhi.requireExtension(capabilityType)) {
298
+ for(var glKey in flatItem){
299
+ var extensionKey = flatItem[glKey];
300
+ var extensionVal = ext[extensionKey];
301
+ // Mini game hack the native function,use “.bind” to smooth out if is “Funcion”.
302
+ if (extensionVal == null ? void 0 : extensionVal.bind) {
303
+ gl[glKey] = extensionVal.bind(ext);
304
+ } else {
305
+ gl[glKey] = extensionVal;
306
+ }
307
+ }
308
+ }
309
+ };
310
+ _proto._compatibleAllInterface = function _compatibleAllInterface() {
311
+ var depthTexture = GLCapabilityType.depthTexture, vertexArrayObject = GLCapabilityType.vertexArrayObject, instancedArrays = GLCapabilityType.instancedArrays, drawBuffers = GLCapabilityType.drawBuffers, textureFilterAnisotropic = GLCapabilityType.textureFilterAnisotropic, textureHalfFloat = GLCapabilityType.textureHalfFloat, colorBufferHalfFloat = GLCapabilityType.colorBufferHalfFloat, WEBGL_colorBufferFloat = GLCapabilityType.WEBGL_colorBufferFloat;
312
+ var isWebGL2 = this.rhi.isWebGL2;
313
+ if (!isWebGL2) {
314
+ this._compatibleInterface(depthTexture, {
315
+ UNSIGNED_INT_24_8: "UNSIGNED_INT_24_8_WEBGL"
316
+ });
317
+ this._compatibleInterface(vertexArrayObject, {
318
+ createVertexArray: "createVertexArrayOES",
319
+ deleteVertexArray: "deleteVertexArrayOES",
320
+ isVertexArray: "isVertexArrayOES",
321
+ bindVertexArray: "bindVertexArrayOES"
322
+ });
323
+ this._compatibleInterface(instancedArrays, {
324
+ drawArraysInstanced: "drawArraysInstancedANGLE",
325
+ drawElementsInstanced: "drawElementsInstancedANGLE",
326
+ vertexAttribDivisor: "vertexAttribDivisorANGLE"
327
+ });
328
+ this._compatibleInterface(drawBuffers, {
329
+ MAX_DRAW_BUFFERS: "MAX_DRAW_BUFFERS_WEBGL"
330
+ });
331
+ var items = {};
332
+ if (this.canIUse(GLCapabilityType.drawBuffers)) {
333
+ var maxDrawBuffers = this.maxDrawBuffers;
334
+ for(var i = 0; i < maxDrawBuffers; i++){
335
+ i != 0 && (items["COLOR_ATTACHMENT" + i] = "COLOR_ATTACHMENT" + i + "_WEBGL");
336
+ items["DRAW_BUFFER" + i] = "DRAW_BUFFER" + i + "_WEBGL";
337
+ }
338
+ this._compatibleInterface(drawBuffers, _extends({
339
+ drawBuffers: "drawBuffersWEBGL"
340
+ }, items));
341
+ }
342
+ this._compatibleInterface(textureHalfFloat, {
343
+ HALF_FLOAT: "HALF_FLOAT_OES"
344
+ });
345
+ this._compatibleInterface(colorBufferHalfFloat, {
346
+ RGBA16F: "RBGA16F_EXT"
347
+ });
348
+ this._compatibleInterface(WEBGL_colorBufferFloat, {
349
+ RGBA32F: "RBGA32F_EXT"
350
+ });
351
+ }
352
+ this._compatibleInterface(textureFilterAnisotropic, {
353
+ TEXTURE_MAX_ANISOTROPY_EXT: "TEXTURE_MAX_ANISOTROPY_EXT"
354
+ });
355
+ };
356
+ _createClass(GLCapability, [
357
+ {
358
+ key: "maxTextureSize",
359
+ get: function get() {
360
+ return this.rhi.renderStates.getParameter(this.rhi.gl.MAX_TEXTURE_SIZE);
361
+ }
362
+ },
363
+ {
364
+ key: "canUseFloatTextureBlendShape",
365
+ get: function get() {
366
+ return this.canIUse(GLCapabilityType.shaderVertexID) && this.canIUse(GLCapabilityType.textureFloat) && this.rhi.renderStates.getParameter(this.rhi.gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS) > 0;
367
+ }
368
+ },
369
+ {
370
+ key: "canIUseMoreJoints",
371
+ get: /**
372
+ * Whether can use more joints.
373
+ */ function get() {
374
+ return this.canIUse(GLCapabilityType.textureFloat) && this.rhi.renderStates.getParameter(this.rhi.gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS) > 0;
375
+ }
376
+ },
377
+ {
378
+ key: "maxDrawBuffers",
379
+ get: function get() {
380
+ if (!this._maxDrawBuffers) {
381
+ if (this.canIUse(GLCapabilityType.drawBuffers)) {
382
+ this._maxDrawBuffers = this._rhi.gl.getParameter(this._rhi.gl.MAX_DRAW_BUFFERS);
383
+ } else {
384
+ this._maxDrawBuffers = 1;
385
+ }
386
+ }
387
+ return this._maxDrawBuffers;
388
+ }
389
+ },
390
+ {
391
+ key: "maxAnisoLevel",
392
+ get: /**
393
+ * Max anisoLevel.
394
+ */ function get() {
395
+ if (!this._maxAnisoLevel) {
396
+ var ext = this._rhi.requireExtension(GLCapabilityType.textureFilterAnisotropic);
397
+ this._maxAnisoLevel = ext ? this._rhi.gl.getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 1;
398
+ }
399
+ return this._maxAnisoLevel;
400
+ }
401
+ },
402
+ {
403
+ key: "maxAntiAliasing",
404
+ get: /**
405
+ * Max MSAA count.
406
+ */ function get() {
407
+ if (!this._maxAntiAliasing) {
408
+ var gl = this._rhi.gl;
409
+ var canMSAA = this.canIUse(GLCapabilityType.multipleSample);
410
+ this._maxAntiAliasing = canMSAA ? gl.getParameter(gl.MAX_SAMPLES) : 1;
411
+ }
412
+ return this._maxAntiAliasing;
413
+ }
414
+ },
415
+ {
416
+ key: "rhi",
417
+ get: function get() {
418
+ return this._rhi;
419
+ }
420
+ }
421
+ ]);
422
+ return GLCapability;
423
+ }();
424
+
425
+ /**
426
+ * GLContext extension.
427
+ */ var GLExtensions = /*#__PURE__*/ function() {
428
+ function GLExtensions(rhi) {
429
+ this.rhi = rhi;
430
+ this._requireResult = {};
431
+ }
432
+ var _proto = GLExtensions.prototype;
433
+ /**
434
+ * Require an extension.
435
+ */ _proto.requireExtension = function requireExtension(ext) {
436
+ if (this._requireResult[ext] !== undefined) {
437
+ return this._requireResult[ext];
438
+ }
439
+ this._requireResult[ext] = this.rhi.gl.getExtension(ext);
440
+ return this._requireResult[ext];
441
+ };
442
+ return GLExtensions;
443
+ }();
444
+
445
+ /**
446
+ * Improvement of VAO:
447
+ * 1) WebGL2.0 must support VAO, almost all devices support vao extensions in webgl1.0, we can use PollyFill,only keep VAO mode.
448
+ */ /**
449
+ * @internal
450
+ * GL platform primitive.
451
+ */ var GLPrimitive = /*#__PURE__*/ function() {
452
+ function GLPrimitive(rhi, primitive) {
453
+ this._attribLocArray = [];
454
+ this._vaoMap = new Map();
455
+ this._primitive = primitive;
456
+ this._canUseInstancedArrays = rhi.canIUse(GLCapabilityType.instancedArrays);
457
+ this._useVao = rhi.canIUse(GLCapabilityType.vertexArrayObject);
458
+ this._gl = rhi.gl;
459
+ }
460
+ var _proto = GLPrimitive.prototype;
461
+ /**
462
+ * Draw the primitive.
463
+ */ _proto.draw = function draw(shaderProgram, subMesh) {
464
+ var gl = this._gl;
465
+ var primitive = this._primitive;
466
+ // @ts-ignore
467
+ var useVao = this._useVao && primitive._enableVAO;
468
+ if (useVao) {
469
+ // @ts-ignore
470
+ if (primitive._bufferStructChanged) {
471
+ this._clearVAO();
472
+ }
473
+ if (!this._vaoMap.has(shaderProgram.id)) {
474
+ this._registerVAO(shaderProgram);
475
+ }
476
+ var vao = this._vaoMap.get(shaderProgram.id);
477
+ gl.bindVertexArray(vao);
478
+ } else {
479
+ this._bindBufferAndAttrib(shaderProgram);
480
+ }
481
+ // @ts-ignore
482
+ var _indexBufferBinding = primitive._indexBufferBinding, _instanceCount = primitive._instanceCount, _glIndexType = primitive._glIndexType, _glIndexByteCount = primitive._glIndexByteCount;
483
+ var topology = subMesh.topology, start = subMesh.start, count = subMesh.count;
484
+ if (!_instanceCount) {
485
+ if (_indexBufferBinding) {
486
+ if (useVao) {
487
+ gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
488
+ } else {
489
+ var _nativeBuffer = _indexBufferBinding.buffer._nativeBuffer;
490
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer);
491
+ gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
492
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
493
+ }
494
+ } else {
495
+ gl.drawArrays(topology, start, count);
496
+ }
497
+ } else {
498
+ if (this._canUseInstancedArrays) {
499
+ if (_indexBufferBinding) {
500
+ if (useVao) {
501
+ gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
502
+ } else {
503
+ var _nativeBuffer1 = _indexBufferBinding.buffer._nativeBuffer;
504
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer1);
505
+ gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
506
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
507
+ }
508
+ } else {
509
+ gl.drawArraysInstanced(topology, start, count, _instanceCount);
510
+ }
511
+ } else {
512
+ Logger.error("ANGLE_instanced_arrays extension is not supported");
513
+ }
514
+ }
515
+ // Unbind
516
+ if (useVao) {
517
+ gl.bindVertexArray(null);
518
+ } else {
519
+ this._disableAttrib();
520
+ }
521
+ };
522
+ _proto.destroy = function destroy() {
523
+ this._useVao && this._clearVAO();
524
+ };
525
+ /**
526
+ * Bind buffer and attribute.
527
+ */ _proto._bindBufferAndAttrib = function _bindBufferAndAttrib(shaderProgram) {
528
+ var gl = this._gl;
529
+ var primitive = this._primitive;
530
+ // @ts-ignore
531
+ var vertexBufferBindings = primitive._vertexBufferBindings;
532
+ this._attribLocArray.length = 0;
533
+ var attributeLocation = shaderProgram.attributeLocation;
534
+ // @ts-ignore
535
+ var attributes = primitive._vertexElementMap;
536
+ var vbo;
537
+ var lastBoundVbo;
538
+ for(var name in attributeLocation){
539
+ var loc = attributeLocation[name];
540
+ if (loc === -1) continue;
541
+ var element = attributes[name];
542
+ if (element) {
543
+ var _vertexBufferBindings_element_bindingIndex = vertexBufferBindings[element.bindingIndex], buffer = _vertexBufferBindings_element_bindingIndex.buffer, stride = _vertexBufferBindings_element_bindingIndex.stride;
544
+ vbo = buffer._nativeBuffer;
545
+ // prevent binding the vbo which already bound at the last loop, e.g. a buffer with multiple attributes.
546
+ if (lastBoundVbo !== vbo) {
547
+ lastBoundVbo = vbo;
548
+ gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
549
+ }
550
+ gl.enableVertexAttribArray(loc);
551
+ var elementInfo = element._glElementInfo;
552
+ gl.vertexAttribPointer(loc, elementInfo.size, elementInfo.type, elementInfo.normalized, stride, element.offset);
553
+ if (this._canUseInstancedArrays) {
554
+ gl.vertexAttribDivisor(loc, element.instanceStepRate);
555
+ }
556
+ this._attribLocArray.push(loc);
557
+ } else {
558
+ Logger.warn("vertex attribute not found: " + name);
559
+ }
560
+ }
561
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
562
+ };
563
+ _proto._disableAttrib = function _disableAttrib() {
564
+ var gl = this._gl;
565
+ for(var i = 0, l = this._attribLocArray.length; i < l; i++){
566
+ gl.disableVertexAttribArray(this._attribLocArray[i]);
567
+ }
568
+ };
569
+ _proto._registerVAO = function _registerVAO(shaderProgram) {
570
+ var gl = this._gl;
571
+ var vao = gl.createVertexArray();
572
+ /** register VAO */ gl.bindVertexArray(vao);
573
+ // @ts-ignore
574
+ var _indexBufferBinding = this._primitive._indexBufferBinding;
575
+ if (_indexBufferBinding) {
576
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._nativeBuffer);
577
+ }
578
+ this._bindBufferAndAttrib(shaderProgram);
579
+ /** unbind */ gl.bindVertexArray(null);
580
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
581
+ this._disableAttrib();
582
+ this._vaoMap.set(shaderProgram.id, vao);
583
+ };
584
+ _proto._clearVAO = function _clearVAO() {
585
+ var gl = this._gl;
586
+ this._vaoMap.forEach(function(vao) {
587
+ gl.deleteVertexArray(vao);
588
+ });
589
+ this._vaoMap.clear();
590
+ };
591
+ return GLPrimitive;
592
+ }();
593
+
594
+ /**
595
+ * @private
596
+ */ var GLRenderStates = /*#__PURE__*/ function() {
597
+ function GLRenderStates(gl) {
598
+ this._parameters = {};
599
+ this._gl = gl;
600
+ this._parameters = {}; // current gl state parameters
601
+ /** cache */ this._parameters[gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS] = gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS);
602
+ this._parameters[gl.MAX_VERTEX_UNIFORM_VECTORS] = gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS);
603
+ this._parameters[gl.MAX_VERTEX_ATTRIBS] = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
604
+ this._parameters[gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS] = gl.getParameter(gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS);
605
+ this._parameters[gl.MAX_TEXTURE_SIZE] = gl.getParameter(gl.MAX_TEXTURE_SIZE);
606
+ // init blend state same as BlendState default value.
607
+ gl.blendFuncSeparate(gl.ONE, gl.ZERO, gl.ONE, gl.ZERO);
608
+ gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
609
+ gl.colorMask(true, true, true, true);
610
+ gl.blendColor(0, 0, 0, 0);
611
+ gl.disable(gl.SAMPLE_ALPHA_TO_COVERAGE);
612
+ // init depth state same as DepthState default value.
613
+ gl.enable(gl.DEPTH_TEST);
614
+ gl.depthFunc(gl.LESS);
615
+ gl.depthMask(true);
616
+ // init stencil state same as StencilState default value.
617
+ gl.disable(gl.STENCIL_TEST);
618
+ gl.stencilFuncSeparate(gl.FRONT, gl.ALWAYS, 0, 0xff);
619
+ gl.stencilFuncSeparate(gl.BACK, gl.ALWAYS, 0, 0xff);
620
+ gl.stencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.KEEP);
621
+ gl.stencilOpSeparate(gl.BACK, gl.KEEP, gl.KEEP, gl.KEEP);
622
+ gl.stencilMask(0xff);
623
+ // init raster state same as RasterState default value.
624
+ gl.enable(gl.CULL_FACE);
625
+ gl.cullFace(gl.BACK);
626
+ gl.disable(gl.POLYGON_OFFSET_FILL);
627
+ gl.polygonOffset(0, 0);
628
+ }
629
+ var _proto = GLRenderStates.prototype;
630
+ /**
631
+ * Get a parameter.
632
+ */ _proto.getParameter = function getParameter(pname) {
633
+ return this._parameters[pname];
634
+ };
635
+ return GLRenderStates;
636
+ }();
637
+
638
+ /**
639
+ * Texture in WebGL platform.
640
+ */ var GLTexture = /*#__PURE__*/ function() {
641
+ function GLTexture(rhi, texture, target) {
642
+ this._texture = texture;
643
+ this._rhi = rhi;
644
+ this._gl = rhi.gl;
645
+ this._isWebGL2 = rhi.isWebGL2;
646
+ this._target = target;
647
+ this._glTexture = this._gl.createTexture();
648
+ }
649
+ var _proto = GLTexture.prototype;
650
+ /**
651
+ * Destroy texture.
652
+ */ _proto.destroy = function destroy() {
653
+ this._gl.deleteTexture(this._glTexture);
654
+ this._texture = null;
655
+ this._glTexture = null;
656
+ this._formatDetail = null;
657
+ };
658
+ /**
659
+ * @internal
660
+ */ _proto.setUseDepthCompareMode = function setUseDepthCompareMode(value) {
661
+ var gl = this._gl;
662
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_MODE, value ? gl.COMPARE_REF_TO_TEXTURE : gl.NONE);
663
+ };
664
+ /**
665
+ * Generate multi-level textures based on the 0th level data.
666
+ */ _proto.generateMipmaps = function generateMipmaps() {
667
+ // @todo (1x1).generateMipmap() will flash back in uc.
668
+ if (this._texture.width !== 1 || this._texture.height !== 1) {
669
+ this._bind();
670
+ this._gl.generateMipmap(this._target);
671
+ }
672
+ };
673
+ _proto._bind = function _bind() {
674
+ this._rhi.bindTexture(this);
675
+ };
676
+ /**
677
+ * Pre-development mipmapping GPU memory.
678
+ */ _proto._init = function _init(isCube) {
679
+ var gl = this._gl;
680
+ var isWebGL2 = this._isWebGL2;
681
+ var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
682
+ // @ts-ignore
683
+ var _this__texture = this._texture, mipmapCount = _this__texture.mipmapCount, width = _this__texture.width, height = _this__texture.height, _isDepthTexture = _this__texture._isDepthTexture;
684
+ this._bind();
685
+ if (isWebGL2 && !(baseFormat === gl.LUMINANCE_ALPHA || baseFormat === gl.ALPHA)) {
686
+ gl.texStorage2D(this._target, mipmapCount, internalFormat, width, height);
687
+ } else {
688
+ if (!isCube) {
689
+ if (_isDepthTexture) {
690
+ gl.texImage2D(this._target, 0, internalFormat, width, height, 0, baseFormat, dataType, null);
691
+ } else {
692
+ for(var i = 0; i < mipmapCount; i++){
693
+ var mipWidth = Math.max(1, width >> i);
694
+ var mipHeight = Math.max(1, height >> i);
695
+ gl.texImage2D(this._target, i, internalFormat, mipWidth, mipHeight, 0, baseFormat, dataType, null);
696
+ }
697
+ }
698
+ } else {
699
+ for(var i1 = 0; i1 < mipmapCount; i1++){
700
+ var size = Math.max(1, width >> i1);
701
+ for(var faceIndex = 0; faceIndex < 6; faceIndex++){
702
+ gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex, i1, internalFormat, size, size, 0, baseFormat, dataType, null);
703
+ }
704
+ }
705
+ }
706
+ }
707
+ };
708
+ /**
709
+ * Get the pixel color buffer according to the specified cube face and area.
710
+ * @param face - You can choose which cube face to read
711
+ * @param x - X coordinate of area start
712
+ * @param y - Y coordinate of area start
713
+ * @param width - Area width
714
+ * @param height - Area height
715
+ * @param out - Color buffer
716
+ * @param mipLevel - Set mip level the data want to get from
717
+ */ _proto._getPixelBuffer = function _getPixelBuffer(face, x, y, width, height, mipLevel, out) {
718
+ var gl = this._gl;
719
+ var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
720
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._getReadFrameBuffer());
721
+ if (mipLevel > 0 && !this._isWebGL2) {
722
+ mipLevel = 0;
723
+ Logger.error("mipLevel only take effect in WebGL2.0");
724
+ }
725
+ if (face != null) {
726
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, this._glTexture, mipLevel);
727
+ } else {
728
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._glTexture, mipLevel);
729
+ }
730
+ gl.readPixels(x, y, width, height, baseFormat, dataType, out);
731
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
732
+ };
733
+ _proto._setWrapMode = function _setWrapMode(value, pname) {
734
+ var gl = this._gl;
735
+ var isWebGL2 = this._isWebGL2;
736
+ var target = this._target;
737
+ var _this__texture = this._texture, width = _this__texture.width, height = _this__texture.height;
738
+ if (!isWebGL2 && value !== TextureWrapMode.Clamp && (!GLTexture._isPowerOf2(width) || !GLTexture._isPowerOf2(height))) {
739
+ Logger.warn("non-power-2 texture is not supported for REPEAT or MIRRORED_REPEAT in WebGL1,and has automatically downgraded to CLAMP_TO_EDGE");
740
+ value = TextureWrapMode.Clamp;
741
+ }
742
+ switch(value){
743
+ case TextureWrapMode.Clamp:
744
+ gl.texParameteri(target, pname, gl.CLAMP_TO_EDGE);
745
+ break;
746
+ case TextureWrapMode.Repeat:
747
+ gl.texParameteri(target, pname, gl.REPEAT);
748
+ break;
749
+ case TextureWrapMode.Mirror:
750
+ gl.texParameteri(target, pname, gl.MIRRORED_REPEAT);
751
+ break;
752
+ }
753
+ };
754
+ _proto._getReadFrameBuffer = function _getReadFrameBuffer() {
755
+ var frameBuffer = this._rhi._readFrameBuffer;
756
+ if (!frameBuffer) {
757
+ this._rhi._readFrameBuffer = frameBuffer = this._gl.createFramebuffer();
758
+ }
759
+ return frameBuffer;
760
+ };
761
+ /** @internal */ GLTexture._isPowerOf2 = function _isPowerOf2(v) {
762
+ return (v & v - 1) === 0;
763
+ };
764
+ /**
765
+ * Get more texture info from TextureFormat.
766
+ * @internal
767
+ */ GLTexture._getFormatDetail = function _getFormatDetail(format, gl, isWebGL2) {
768
+ switch(format){
769
+ case TextureFormat.R8G8B8:
770
+ return {
771
+ internalFormat: isWebGL2 ? gl.RGB8 : gl.RGB,
772
+ baseFormat: gl.RGB,
773
+ dataType: gl.UNSIGNED_BYTE,
774
+ isCompressed: false
775
+ };
776
+ case TextureFormat.R8G8B8A8:
777
+ return {
778
+ internalFormat: isWebGL2 ? gl.RGBA8 : gl.RGBA,
779
+ baseFormat: gl.RGBA,
780
+ dataType: gl.UNSIGNED_BYTE,
781
+ isCompressed: false
782
+ };
783
+ case TextureFormat.R4G4B4A4:
784
+ return {
785
+ internalFormat: isWebGL2 ? gl.RGBA4 : gl.RGBA,
786
+ baseFormat: gl.RGBA,
787
+ dataType: gl.UNSIGNED_SHORT_4_4_4_4,
788
+ isCompressed: false
789
+ };
790
+ case TextureFormat.R5G5B5A1:
791
+ return {
792
+ internalFormat: isWebGL2 ? gl.RGB5_A1 : gl.RGBA,
793
+ baseFormat: gl.RGBA,
794
+ dataType: gl.UNSIGNED_SHORT_5_5_5_1,
795
+ isCompressed: false
796
+ };
797
+ case TextureFormat.R5G6B5:
798
+ return {
799
+ internalFormat: isWebGL2 ? gl.RGB565 : gl.RGB,
800
+ baseFormat: gl.RGB,
801
+ dataType: gl.UNSIGNED_SHORT_5_6_5,
802
+ isCompressed: false
803
+ };
804
+ case TextureFormat.Alpha8:
805
+ return {
806
+ internalFormat: gl.ALPHA,
807
+ baseFormat: gl.ALPHA,
808
+ dataType: gl.UNSIGNED_BYTE,
809
+ isCompressed: false
810
+ };
811
+ case TextureFormat.LuminanceAlpha:
812
+ return {
813
+ internalFormat: gl.LUMINANCE_ALPHA,
814
+ baseFormat: gl.LUMINANCE_ALPHA,
815
+ dataType: gl.UNSIGNED_BYTE,
816
+ isCompressed: false
817
+ };
818
+ case TextureFormat.R16G16B16A16:
819
+ return {
820
+ internalFormat: isWebGL2 ? gl.RGBA16F : gl.RGBA,
821
+ baseFormat: gl.RGBA,
822
+ dataType: gl.HALF_FLOAT,
823
+ isCompressed: false
824
+ };
825
+ case TextureFormat.R32G32B32A32:
826
+ return {
827
+ internalFormat: isWebGL2 ? gl.RGBA32F : gl.RGBA,
828
+ baseFormat: gl.RGBA,
829
+ dataType: gl.FLOAT,
830
+ isCompressed: false
831
+ };
832
+ case TextureFormat.DXT1:
833
+ return {
834
+ internalFormat: GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT,
835
+ isCompressed: true
836
+ };
837
+ case TextureFormat.DXT5:
838
+ return {
839
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT,
840
+ isCompressed: true
841
+ };
842
+ case TextureFormat.ETC1_RGB:
843
+ return {
844
+ internalFormat: GLCompressedTextureInternalFormat.RGB_ETC1_WEBGL,
845
+ isCompressed: true
846
+ };
847
+ case TextureFormat.ETC2_RGB:
848
+ return {
849
+ internalFormat: GLCompressedTextureInternalFormat.RGB8_ETC2,
850
+ isCompressed: true
851
+ };
852
+ case TextureFormat.ETC2_RGBA5:
853
+ return {
854
+ internalFormat: GLCompressedTextureInternalFormat.RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
855
+ isCompressed: true
856
+ };
857
+ case TextureFormat.ETC2_RGBA8:
858
+ return {
859
+ internalFormat: GLCompressedTextureInternalFormat.RGBA8_ETC2_EAC,
860
+ isCompressed: true
861
+ };
862
+ case TextureFormat.PVRTC_RGB2:
863
+ return {
864
+ internalFormat: GLCompressedTextureInternalFormat.RGB_PVRTC_2BPPV1_IMG,
865
+ isCompressed: true
866
+ };
867
+ case TextureFormat.PVRTC_RGBA2:
868
+ return {
869
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_PVRTC_2BPPV1_IMG,
870
+ isCompressed: true
871
+ };
872
+ case TextureFormat.PVRTC_RGB4:
873
+ return {
874
+ internalFormat: GLCompressedTextureInternalFormat.RGB_PVRTC_4BPPV1_IMG,
875
+ isCompressed: true
876
+ };
877
+ case TextureFormat.PVRTC_RGBA4:
878
+ return {
879
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_PVRTC_4BPPV1_IMG,
880
+ isCompressed: true
881
+ };
882
+ case TextureFormat.ASTC_4x4:
883
+ return {
884
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_4X4_KHR,
885
+ isCompressed: true
886
+ };
887
+ case TextureFormat.ASTC_5x5:
888
+ return {
889
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_5X5_KHR,
890
+ isCompressed: true
891
+ };
892
+ case TextureFormat.ASTC_6x6:
893
+ return {
894
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_6X6_KHR,
895
+ isCompressed: true
896
+ };
897
+ case TextureFormat.ASTC_8x8:
898
+ return {
899
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_8X8_KHR,
900
+ isCompressed: true
901
+ };
902
+ case TextureFormat.ASTC_10x10:
903
+ return {
904
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_10X10_KHR,
905
+ isCompressed: true
906
+ };
907
+ case TextureFormat.ASTC_12x12:
908
+ return {
909
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_12X12_KHR,
910
+ isCompressed: true
911
+ };
912
+ case TextureFormat.Depth:
913
+ return {
914
+ internalFormat: isWebGL2 ? gl.DEPTH_COMPONENT32F : gl.DEPTH_COMPONENT,
915
+ baseFormat: gl.DEPTH_COMPONENT,
916
+ dataType: isWebGL2 ? gl.FLOAT : gl.UNSIGNED_SHORT,
917
+ isCompressed: false,
918
+ attachment: gl.DEPTH_ATTACHMENT
919
+ };
920
+ case TextureFormat.DepthStencil:
921
+ return {
922
+ internalFormat: isWebGL2 ? gl.DEPTH32F_STENCIL8 : gl.DEPTH_STENCIL,
923
+ baseFormat: gl.DEPTH_STENCIL,
924
+ dataType: isWebGL2 ? gl.FLOAT_32_UNSIGNED_INT_24_8_REV : gl.UNSIGNED_INT_24_8,
925
+ isCompressed: false,
926
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
927
+ };
928
+ case TextureFormat.Depth16:
929
+ return {
930
+ internalFormat: isWebGL2 ? gl.DEPTH_COMPONENT16 : gl.DEPTH_COMPONENT,
931
+ baseFormat: gl.DEPTH_COMPONENT,
932
+ dataType: gl.UNSIGNED_SHORT,
933
+ isCompressed: false,
934
+ attachment: gl.DEPTH_ATTACHMENT
935
+ };
936
+ case TextureFormat.Depth24Stencil8:
937
+ return {
938
+ internalFormat: isWebGL2 ? gl.DEPTH24_STENCIL8 : gl.DEPTH_STENCIL,
939
+ baseFormat: gl.DEPTH_STENCIL,
940
+ dataType: gl.UNSIGNED_INT_24_8,
941
+ isCompressed: false,
942
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
943
+ };
944
+ case TextureFormat.Depth24:
945
+ return {
946
+ internalFormat: gl.DEPTH_COMPONENT24,
947
+ baseFormat: gl.DEPTH_COMPONENT,
948
+ dataType: gl.UNSIGNED_INT,
949
+ isCompressed: false,
950
+ attachment: gl.DEPTH_ATTACHMENT
951
+ };
952
+ case TextureFormat.Depth32:
953
+ return {
954
+ internalFormat: gl.DEPTH_COMPONENT32F,
955
+ baseFormat: gl.DEPTH_COMPONENT,
956
+ dataType: gl.FLOAT,
957
+ isCompressed: false,
958
+ attachment: gl.DEPTH_ATTACHMENT
959
+ };
960
+ case TextureFormat.Depth32Stencil8:
961
+ return {
962
+ internalFormat: gl.DEPTH32F_STENCIL8,
963
+ baseFormat: gl.DEPTH_STENCIL,
964
+ dataType: gl.FLOAT_32_UNSIGNED_INT_24_8_REV,
965
+ isCompressed: false,
966
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
967
+ };
968
+ default:
969
+ throw new Error("this TextureFormat is not supported in Galacean Engine: " + format);
970
+ }
971
+ };
972
+ /**
973
+ * In WebGL 1, internalformat must be the same as baseFormat when call texImage2D.
974
+ * @internal
975
+ */ GLTexture._getRenderBufferDepthFormatDetail = function _getRenderBufferDepthFormatDetail(format, gl, isWebGL2) {
976
+ switch(format){
977
+ case RenderBufferDepthFormat.Depth:
978
+ return {
979
+ internalFormat: isWebGL2 ? gl.DEPTH_COMPONENT32F : gl.DEPTH_COMPONENT16,
980
+ baseFormat: gl.DEPTH_COMPONENT,
981
+ dataType: isWebGL2 ? gl.FLOAT : gl.UNSIGNED_SHORT,
982
+ isCompressed: false,
983
+ attachment: gl.DEPTH_ATTACHMENT
984
+ };
985
+ case RenderBufferDepthFormat.DepthStencil:
986
+ return {
987
+ internalFormat: isWebGL2 ? gl.DEPTH32F_STENCIL8 : gl.DEPTH_STENCIL,
988
+ baseFormat: gl.DEPTH_STENCIL,
989
+ dataType: isWebGL2 ? gl.FLOAT_32_UNSIGNED_INT_24_8_REV : gl.UNSIGNED_INT_24_8,
990
+ isCompressed: false,
991
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
992
+ };
993
+ case RenderBufferDepthFormat.Stencil:
994
+ return {
995
+ internalFormat: gl.STENCIL_INDEX8,
996
+ baseFormat: gl.STENCIL_ATTACHMENT,
997
+ dataType: gl.UNSIGNED_BYTE,
998
+ isCompressed: false,
999
+ attachment: gl.STENCIL_ATTACHMENT
1000
+ };
1001
+ case RenderBufferDepthFormat.Depth16:
1002
+ return {
1003
+ internalFormat: gl.DEPTH_COMPONENT16,
1004
+ baseFormat: gl.DEPTH_COMPONENT,
1005
+ dataType: gl.UNSIGNED_SHORT,
1006
+ isCompressed: false,
1007
+ attachment: gl.DEPTH_ATTACHMENT
1008
+ };
1009
+ case RenderBufferDepthFormat.Depth24Stencil8:
1010
+ return {
1011
+ internalFormat: isWebGL2 ? gl.DEPTH24_STENCIL8 : gl.DEPTH_STENCIL,
1012
+ baseFormat: gl.DEPTH_STENCIL,
1013
+ dataType: gl.UNSIGNED_INT_24_8,
1014
+ isCompressed: false,
1015
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
1016
+ };
1017
+ case RenderBufferDepthFormat.Depth24:
1018
+ return {
1019
+ internalFormat: gl.DEPTH_COMPONENT24,
1020
+ baseFormat: gl.DEPTH_COMPONENT,
1021
+ dataType: gl.UNSIGNED_INT,
1022
+ isCompressed: false,
1023
+ attachment: gl.DEPTH_ATTACHMENT
1024
+ };
1025
+ case RenderBufferDepthFormat.Depth32:
1026
+ return {
1027
+ internalFormat: gl.DEPTH_COMPONENT32F,
1028
+ baseFormat: gl.DEPTH_COMPONENT,
1029
+ dataType: gl.FLOAT,
1030
+ isCompressed: false,
1031
+ attachment: gl.DEPTH_ATTACHMENT
1032
+ };
1033
+ case RenderBufferDepthFormat.Depth32Stencil8:
1034
+ return {
1035
+ internalFormat: gl.DEPTH32F_STENCIL8,
1036
+ baseFormat: gl.DEPTH_STENCIL,
1037
+ dataType: gl.FLOAT_32_UNSIGNED_INT_24_8_REV,
1038
+ isCompressed: false,
1039
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
1040
+ };
1041
+ default:
1042
+ throw new Error("this TextureFormat is not supported in Galacean Engine: " + format);
1043
+ }
1044
+ };
1045
+ /**
1046
+ * Check whether the corresponding texture format is supported.
1047
+ * @internal
1048
+ */ GLTexture._supportTextureFormat = function _supportTextureFormat(format, rhi) {
1049
+ switch(format){
1050
+ case TextureFormat.R16G16B16A16:
1051
+ if (!rhi.canIUse(GLCapabilityType.textureHalfFloat)) {
1052
+ return false;
1053
+ }
1054
+ break;
1055
+ case TextureFormat.R32G32B32A32:
1056
+ if (!rhi.canIUse(GLCapabilityType.textureFloat)) {
1057
+ return false;
1058
+ }
1059
+ break;
1060
+ case TextureFormat.Depth16:
1061
+ case TextureFormat.Depth24Stencil8:
1062
+ case TextureFormat.Depth:
1063
+ case TextureFormat.DepthStencil:
1064
+ if (!rhi.canIUse(GLCapabilityType.depthTexture)) {
1065
+ return false;
1066
+ }
1067
+ break;
1068
+ case TextureFormat.Depth24:
1069
+ case TextureFormat.Depth32:
1070
+ case TextureFormat.Depth32Stencil8:
1071
+ return rhi.isWebGL2;
1072
+ }
1073
+ return true;
1074
+ };
1075
+ /**
1076
+ * @internal
1077
+ */ GLTexture._supportRenderBufferColorFormat = function _supportRenderBufferColorFormat(format, rhi) {
1078
+ var isSupported = true;
1079
+ switch(format){
1080
+ case TextureFormat.R16G16B16A16:
1081
+ {
1082
+ if (!rhi.canIUse(GLCapabilityType.colorBufferHalfFloat) || !rhi.canIUse(GLCapabilityType.textureHalfFloat)) {
1083
+ isSupported = false;
1084
+ }
1085
+ }
1086
+ break;
1087
+ case TextureFormat.R32G32B32A32:
1088
+ {
1089
+ if (!rhi.canIUse(GLCapabilityType.colorBufferFloat) || !rhi.canIUse(GLCapabilityType.textureFloat)) {
1090
+ isSupported = false;
1091
+ }
1092
+ }
1093
+ break;
1094
+ }
1095
+ return isSupported;
1096
+ };
1097
+ /**
1098
+ * @internal
1099
+ */ GLTexture._supportRenderBufferDepthFormat = function _supportRenderBufferDepthFormat(format, rhi) {
1100
+ if (!rhi.isWebGL2) {
1101
+ switch(format){
1102
+ case RenderBufferDepthFormat.Depth24:
1103
+ case RenderBufferDepthFormat.Depth32:
1104
+ case RenderBufferDepthFormat.Depth32Stencil8:
1105
+ return false;
1106
+ }
1107
+ }
1108
+ return true;
1109
+ };
1110
+ _createClass(GLTexture, [
1111
+ {
1112
+ key: "wrapModeU",
1113
+ set: /**
1114
+ * Wrapping mode for texture coordinate S.
1115
+ */ function set(value) {
1116
+ this._bind();
1117
+ this._setWrapMode(value, this._gl.TEXTURE_WRAP_S);
1118
+ }
1119
+ },
1120
+ {
1121
+ key: "wrapModeV",
1122
+ set: /**
1123
+ * Wrapping mode for texture coordinate T.
1124
+ */ function set(value) {
1125
+ this._bind();
1126
+ this._setWrapMode(value, this._gl.TEXTURE_WRAP_T);
1127
+ }
1128
+ },
1129
+ {
1130
+ key: "filterMode",
1131
+ set: /**
1132
+ * Filter mode for texture.
1133
+ */ function set(value) {
1134
+ var gl = this._gl;
1135
+ var target = this._target;
1136
+ /** @ts-ignore */ var _mipmap = this._texture._mipmap;
1137
+ this._bind();
1138
+ switch(value){
1139
+ case TextureFilterMode.Point:
1140
+ gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
1141
+ gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, _mipmap ? gl.NEAREST_MIPMAP_NEAREST : gl.NEAREST);
1142
+ break;
1143
+ case TextureFilterMode.Bilinear:
1144
+ gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
1145
+ gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, _mipmap ? gl.LINEAR_MIPMAP_NEAREST : gl.LINEAR);
1146
+ break;
1147
+ case TextureFilterMode.Trilinear:
1148
+ gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
1149
+ gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, _mipmap ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
1150
+ break;
1151
+ }
1152
+ }
1153
+ },
1154
+ {
1155
+ key: "anisoLevel",
1156
+ set: /**
1157
+ * Anisotropic level for texture.
1158
+ */ function set(value) {
1159
+ var gl = this._gl;
1160
+ this._bind();
1161
+ gl.texParameterf(this._target, gl.TEXTURE_MAX_ANISOTROPY_EXT, value);
1162
+ }
1163
+ },
1164
+ {
1165
+ key: "depthCompareFunction",
1166
+ set: function set(value) {
1167
+ this._bind();
1168
+ var gl = this._gl;
1169
+ switch(value){
1170
+ case TextureDepthCompareFunction.Never:
1171
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.NEVER);
1172
+ break;
1173
+ case TextureDepthCompareFunction.Less:
1174
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.LESS);
1175
+ break;
1176
+ case TextureDepthCompareFunction.Equal:
1177
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.EQUAL);
1178
+ break;
1179
+ case TextureDepthCompareFunction.LessEqual:
1180
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.LEQUAL);
1181
+ break;
1182
+ case TextureDepthCompareFunction.Greater:
1183
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.GREATER);
1184
+ break;
1185
+ case TextureDepthCompareFunction.NotEqual:
1186
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.NOTEQUAL);
1187
+ break;
1188
+ case TextureDepthCompareFunction.GreaterEqual:
1189
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.GEQUAL);
1190
+ break;
1191
+ case TextureDepthCompareFunction.Always:
1192
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.ALWAYS);
1193
+ break;
1194
+ }
1195
+ }
1196
+ }
1197
+ ]);
1198
+ return GLTexture;
1199
+ }();
1200
+
1201
+ /**
1202
+ * The render target in WebGL platform is used for off-screen rendering.
1203
+ */ var GLRenderTarget = /*#__PURE__*/ function() {
1204
+ function GLRenderTarget(rhi, target) {
1205
+ this._MSAAColorRenderBuffers = [];
1206
+ this._curMipLevel = 0;
1207
+ this._gl = rhi.gl;
1208
+ this._isWebGL2 = rhi.isWebGL2;
1209
+ this._target = target;
1210
+ /** @ts-ignore */ var _colorTextures = target._colorTextures, _depth = target._depth, width = target.width, height = target.height;
1211
+ var isDepthTexture = _instanceof(_depth, Texture);
1212
+ /** todo
1213
+ * MRT + Cube + [,MSAA]
1214
+ * MRT + MSAA
1215
+ */ for(var i = 0, n = _colorTextures.length; i < n; i++){
1216
+ var format = _colorTextures[i]._format;
1217
+ if (!GLTexture._supportRenderBufferColorFormat(format, rhi)) {
1218
+ throw new Error("TextureFormat is not supported:" + TextureFormat[format] + " in RenderTarget");
1219
+ }
1220
+ }
1221
+ if (!isDepthTexture && !GLTexture._supportRenderBufferDepthFormat(_depth, rhi)) {
1222
+ throw new Error("TextureFormat is not supported:" + TextureFormat[_depth] + " in RenderTarget");
1223
+ }
1224
+ if (_colorTextures.length > 1 && !rhi.canIUse(GLCapabilityType.drawBuffers)) {
1225
+ throw new Error("MRT is not supported");
1226
+ }
1227
+ if (_colorTextures.some(function(v) {
1228
+ return v.width !== width || v.height !== height;
1229
+ })) {
1230
+ throw new Error("ColorTexture's size must as same as RenderTarget");
1231
+ }
1232
+ if (isDepthTexture && (_depth.width !== width || _depth.height !== height)) {
1233
+ throw new Error("DepthTexture's size must as same as RenderTarget");
1234
+ }
1235
+ // todo: necessary to support MRT + Cube + [,MSAA] ?
1236
+ if (_colorTextures.length > 1 && _colorTextures.some(function(v) {
1237
+ return _instanceof(v, TextureCube);
1238
+ })) {
1239
+ throw new Error("MRT+Cube+[,MSAA] is not supported");
1240
+ }
1241
+ var maxAntiAliasing = rhi.capability.maxAntiAliasing;
1242
+ if (target.antiAliasing > maxAntiAliasing) {
1243
+ Logger.warn("MSAA antiAliasing exceeds the limit and is automatically downgraded to:" + maxAntiAliasing);
1244
+ /** @ts-ignore */ target._antiAliasing = maxAntiAliasing;
1245
+ }
1246
+ this._frameBuffer = this._gl.createFramebuffer();
1247
+ // bind main FBO
1248
+ this._bindMainFBO();
1249
+ // bind MSAA FBO
1250
+ if (target.antiAliasing > 1) {
1251
+ this._MSAAFrameBuffer = this._gl.createFramebuffer();
1252
+ this._bindMSAAFBO();
1253
+ }
1254
+ }
1255
+ var _proto = GLRenderTarget.prototype;
1256
+ /**
1257
+ * Set which face and mipLevel of the cube texture to render to.
1258
+ * @param faceIndex - Cube texture face
1259
+ * @param mipLevel - Set mip level the data want to write
1260
+ */ _proto.setRenderTargetInfo = function setRenderTargetInfo(faceIndex, mipLevel) {
1261
+ var _this = this, gl = _this._gl, target = _this._target;
1262
+ var depthTexture = target.depthTexture;
1263
+ var colorTexture = target.getColorTexture(0);
1264
+ var mipChanged = mipLevel !== this._curMipLevel;
1265
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
1266
+ if (colorTexture) {
1267
+ var isCube = _instanceof(colorTexture, TextureCube);
1268
+ if (mipChanged || isCube) {
1269
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, isCube ? gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex : gl.TEXTURE_2D, // @ts-ignore
1270
+ colorTexture._platformTexture._glTexture, mipLevel);
1271
+ }
1272
+ }
1273
+ if (depthTexture) {
1274
+ var isCube1 = _instanceof(depthTexture, TextureCube);
1275
+ if (mipChanged || isCube1) {
1276
+ // @ts-ignore
1277
+ var platformTexture = depthTexture._platformTexture;
1278
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, platformTexture._formatDetail.attachment, isCube1 ? gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex : gl.TEXTURE_2D, platformTexture._glTexture, mipLevel);
1279
+ }
1280
+ } else {
1281
+ if (mipChanged) {
1282
+ // @ts-ignore
1283
+ var internalFormat = GLTexture._getRenderBufferDepthFormatDetail(target._depth, gl, this._isWebGL2).internalFormat;
1284
+ gl.bindRenderbuffer(gl.RENDERBUFFER, this._depthRenderBuffer);
1285
+ gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, target.width >> mipLevel, target.height >> mipLevel);
1286
+ }
1287
+ }
1288
+ this._curMipLevel = mipLevel;
1289
+ // revert current activated render target
1290
+ this._activeRenderTarget();
1291
+ };
1292
+ /**
1293
+ * Blit FBO.
1294
+ */ _proto.blitRenderTarget = function blitRenderTarget() {
1295
+ if (!this._MSAAFrameBuffer) return;
1296
+ var gl = this._gl;
1297
+ var mask = gl.COLOR_BUFFER_BIT | (this._target.depthTexture ? gl.DEPTH_BUFFER_BIT : 0);
1298
+ var _this__target = this._target, colorTextureCount = _this__target.colorTextureCount, width = _this__target.width, height = _this__target.height;
1299
+ gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this._MSAAFrameBuffer);
1300
+ gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this._frameBuffer);
1301
+ for(var textureIndex = 0; textureIndex < colorTextureCount; textureIndex++){
1302
+ var attachment = gl.COLOR_ATTACHMENT0 + textureIndex;
1303
+ this._blitDrawBuffers[textureIndex] = attachment;
1304
+ gl.readBuffer(attachment);
1305
+ gl.drawBuffers(this._blitDrawBuffers);
1306
+ gl.blitFramebuffer(0, 0, width, height, 0, 0, width, height, mask, gl.NEAREST);
1307
+ this._blitDrawBuffers[textureIndex] = gl.NONE;
1308
+ }
1309
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1310
+ };
1311
+ /**
1312
+ * Destroy render target.
1313
+ */ _proto.destroy = function destroy() {
1314
+ var gl = this._gl;
1315
+ this._frameBuffer && gl.deleteFramebuffer(this._frameBuffer);
1316
+ this._depthRenderBuffer && gl.deleteRenderbuffer(this._depthRenderBuffer);
1317
+ this._MSAAFrameBuffer && gl.deleteFramebuffer(this._MSAAFrameBuffer);
1318
+ this._MSAADepthRenderBuffer && gl.deleteRenderbuffer(this._MSAADepthRenderBuffer);
1319
+ for(var i = 0; i < this._MSAAColorRenderBuffers.length; i++){
1320
+ gl.deleteRenderbuffer(this._MSAAColorRenderBuffers[i]);
1321
+ }
1322
+ this._frameBuffer = null;
1323
+ this._depthRenderBuffer = null;
1324
+ this._MSAAFrameBuffer = null;
1325
+ this._MSAAColorRenderBuffers.length = 0;
1326
+ this._MSAADepthRenderBuffer = null;
1327
+ };
1328
+ /**
1329
+ * Activate this RenderTarget.
1330
+ * @internal
1331
+ * @remarks
1332
+ * If MSAA is turned on, MSAA FBO is activated, and then this._blitRenderTarget() is performed to exchange FBO.
1333
+ * If MSAA is not turned on, activate the main FBO.
1334
+ */ _proto._activeRenderTarget = function _activeRenderTarget() {
1335
+ var gl = this._gl;
1336
+ if (this._MSAAFrameBuffer) {
1337
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._MSAAFrameBuffer);
1338
+ } else {
1339
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
1340
+ }
1341
+ };
1342
+ _proto._bindMainFBO = function _bindMainFBO() {
1343
+ var gl = this._gl;
1344
+ var isWebGL2 = this._isWebGL2;
1345
+ /** @ts-ignore */ var _this__target = this._target, _depth = _this__target._depth, colorTextureCount = _this__target.colorTextureCount, width = _this__target.width, height = _this__target.height;
1346
+ var drawBuffers = new Array(colorTextureCount);
1347
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
1348
+ /** color render buffer */ for(var i = 0; i < colorTextureCount; i++){
1349
+ var colorTexture = this._target.getColorTexture(i);
1350
+ var attachment = gl.COLOR_ATTACHMENT0 + i;
1351
+ drawBuffers[i] = attachment;
1352
+ if (!_instanceof(colorTexture, TextureCube)) {
1353
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, attachment, gl.TEXTURE_2D, /** @ts-ignore */ colorTexture._platformTexture._glTexture, 0);
1354
+ }
1355
+ }
1356
+ if (colorTextureCount > 1) {
1357
+ gl.drawBuffers(drawBuffers);
1358
+ }
1359
+ this._oriDrawBuffers = drawBuffers;
1360
+ /** depth render buffer */ if (_depth !== null) {
1361
+ if (_instanceof(_depth, Texture) && !_instanceof(_depth, TextureCube)) {
1362
+ // @ts-ignore
1363
+ var platformTexture = _depth._platformTexture;
1364
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, platformTexture._formatDetail.attachment, gl.TEXTURE_2D, platformTexture._glTexture, 0);
1365
+ } else if (this._target.antiAliasing <= 1) {
1366
+ var _GLTexture__getRenderBufferDepthFormatDetail = GLTexture._getRenderBufferDepthFormatDetail(_depth, gl, isWebGL2), internalFormat = _GLTexture__getRenderBufferDepthFormatDetail.internalFormat, attachment1 = _GLTexture__getRenderBufferDepthFormatDetail.attachment;
1367
+ var depthRenderBuffer = gl.createRenderbuffer();
1368
+ this._depthRenderBuffer = depthRenderBuffer;
1369
+ gl.bindRenderbuffer(gl.RENDERBUFFER, depthRenderBuffer);
1370
+ gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, width, height);
1371
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment1, gl.RENDERBUFFER, depthRenderBuffer);
1372
+ }
1373
+ }
1374
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1375
+ gl.bindRenderbuffer(gl.RENDERBUFFER, null);
1376
+ };
1377
+ _proto._bindMSAAFBO = function _bindMSAAFBO() {
1378
+ var gl = this._gl;
1379
+ var isWebGL2 = this._isWebGL2;
1380
+ var MSAADepthRenderBuffer = gl.createRenderbuffer();
1381
+ /** @ts-ignore */ var _this__target = this._target, _depth = _this__target._depth, colorTextureCount = _this__target.colorTextureCount, antiAliasing = _this__target.antiAliasing, width = _this__target.width, height = _this__target.height;
1382
+ this._blitDrawBuffers = new Array(colorTextureCount);
1383
+ this._MSAADepthRenderBuffer = MSAADepthRenderBuffer;
1384
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._MSAAFrameBuffer);
1385
+ // prepare MRT+MSAA color RBOs
1386
+ for(var i = 0; i < colorTextureCount; i++){
1387
+ var MSAAColorRenderBuffer = gl.createRenderbuffer();
1388
+ this._MSAAColorRenderBuffers[i] = MSAAColorRenderBuffer;
1389
+ this._blitDrawBuffers[i] = gl.NONE;
1390
+ gl.bindRenderbuffer(gl.RENDERBUFFER, MSAAColorRenderBuffer);
1391
+ gl.renderbufferStorageMultisample(gl.RENDERBUFFER, antiAliasing, /** @ts-ignore */ this._target.getColorTexture(i)._platformTexture._formatDetail.internalFormat, width, height);
1392
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.RENDERBUFFER, MSAAColorRenderBuffer);
1393
+ }
1394
+ gl.drawBuffers(this._oriDrawBuffers);
1395
+ // prepare MSAA depth RBO
1396
+ if (_depth !== null) {
1397
+ var _ref = _instanceof(_depth, Texture) ? /** @ts-ignore */ _depth._platformTexture._formatDetail : GLTexture._getRenderBufferDepthFormatDetail(_depth, gl, isWebGL2), internalFormat = _ref.internalFormat, attachment = _ref.attachment;
1398
+ gl.bindRenderbuffer(gl.RENDERBUFFER, MSAADepthRenderBuffer);
1399
+ gl.renderbufferStorageMultisample(gl.RENDERBUFFER, antiAliasing, internalFormat, width, height);
1400
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, MSAADepthRenderBuffer);
1401
+ }
1402
+ this._checkFrameBuffer();
1403
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1404
+ gl.bindRenderbuffer(gl.RENDERBUFFER, null);
1405
+ };
1406
+ _proto._checkFrameBuffer = function _checkFrameBuffer() {
1407
+ var gl = this._gl;
1408
+ var isWebGL2 = this._isWebGL2;
1409
+ var e = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
1410
+ switch(e){
1411
+ case gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
1412
+ throw new Error("The attachment types are mismatched or not all framebuffer attachment points are framebuffer attachment complete");
1413
+ case gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
1414
+ throw new Error("There is no attachment");
1415
+ case gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
1416
+ throw new Error(" Height and width of the attachment are not the same.");
1417
+ case gl.FRAMEBUFFER_UNSUPPORTED:
1418
+ throw new Error("The format of the attachment is not supported or if depth and stencil attachments are not the same renderbuffer");
1419
+ }
1420
+ if (isWebGL2 && e === gl.FRAMEBUFFER_INCOMPLETE_MULTISAMPLE) {
1421
+ throw new Error("The values of gl.RENDERBUFFER_SAMPLES are different among attached renderbuffers, or are non-zero if the attached images are a mix of renderbuffers and textures.");
1422
+ }
1423
+ };
1424
+ return GLRenderTarget;
1425
+ }();
1426
+
1427
+ /**
1428
+ * Texture 2d in WebGL platform.
1429
+ */ var GLTexture2D = /*#__PURE__*/ function(GLTexture1) {
1430
+ _inherits(GLTexture2D, GLTexture1);
1431
+ function GLTexture2D(rhi, texture2D) {
1432
+ var _this;
1433
+ _this = GLTexture1.call(this, rhi, texture2D, rhi.gl.TEXTURE_2D) || this;
1434
+ /** Backward compatible with WebGL1.0. */ _this._compressedMipFilled = 0;
1435
+ /** @ts-ignore */ var format = texture2D.format, _mipmap = texture2D._mipmap, width = texture2D.width, height = texture2D.height;
1436
+ var isWebGL2 = _this._isWebGL2;
1437
+ /** @ts-ignore */ if (!GLTexture._supportTextureFormat(format, rhi)) {
1438
+ throw new Error("Texture format is not supported:" + TextureFormat[format]);
1439
+ }
1440
+ if (_mipmap && !isWebGL2 && (!GLTexture._isPowerOf2(width) || !GLTexture._isPowerOf2(height))) {
1441
+ Logger.warn("non-power-2 texture is not supported for mipmap in WebGL1,and has automatically downgraded to non-mipmap");
1442
+ /** @ts-ignore */ texture2D._mipmap = false;
1443
+ /** @ts-ignore */ texture2D._mipmapCount = texture2D._getMipmapCount();
1444
+ }
1445
+ _this._formatDetail = GLTexture._getFormatDetail(format, _this._gl, isWebGL2);
1446
+ _this._formatDetail.isCompressed && !isWebGL2 || _this._init(false);
1447
+ return _this;
1448
+ }
1449
+ var _proto = GLTexture2D.prototype;
1450
+ /**
1451
+ * {@inheritDoc IPlatformTexture2D.setPixelBuffer}
1452
+ */ _proto.setPixelBuffer = function setPixelBuffer(colorBuffer, mipLevel, x, y, width, height) {
1453
+ if (mipLevel === void 0) mipLevel = 0;
1454
+ var gl = this._gl;
1455
+ var isWebGL2 = this._isWebGL2;
1456
+ var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType, isCompressed = _this__formatDetail.isCompressed;
1457
+ var mipWidth = Math.max(1, this._texture.width >> mipLevel);
1458
+ var mipHeight = Math.max(1, this._texture.height >> mipLevel);
1459
+ width = width || mipWidth - x;
1460
+ height = height || mipHeight - y;
1461
+ this._bind();
1462
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1463
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1464
+ if (isCompressed) {
1465
+ var mipBit = 1 << mipLevel;
1466
+ if (isWebGL2 || this._compressedMipFilled & mipBit) {
1467
+ gl.compressedTexSubImage2D(this._target, mipLevel, x, y, width, height, internalFormat, colorBuffer);
1468
+ } else {
1469
+ gl.compressedTexImage2D(this._target, mipLevel, internalFormat, width, height, 0, colorBuffer);
1470
+ this._compressedMipFilled |= mipBit;
1471
+ }
1472
+ } else {
1473
+ gl.texSubImage2D(this._target, mipLevel, x, y, width, height, baseFormat, dataType, colorBuffer);
1474
+ }
1475
+ };
1476
+ /**
1477
+ * {@inheritDoc IPlatformTexture2D.setImageSource}
1478
+ */ _proto.setImageSource = function setImageSource(imageSource, mipLevel, flipY, premultiplyAlpha, x, y) {
1479
+ var gl = this._gl;
1480
+ var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1481
+ this._bind();
1482
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, +flipY);
1483
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, +premultiplyAlpha);
1484
+ gl.texSubImage2D(this._target, mipLevel, x || 0, y || 0, baseFormat, dataType, imageSource);
1485
+ };
1486
+ /**
1487
+ * {@inheritDoc IPlatformTexture2D.getPixelBuffer }
1488
+ */ _proto.getPixelBuffer = function getPixelBuffer(x, y, width, height, mipLevel, out) {
1489
+ if (this._formatDetail.isCompressed) {
1490
+ throw new Error("Unable to read compressed texture");
1491
+ }
1492
+ GLTexture1.prototype._getPixelBuffer.call(this, null, x, y, width, height, mipLevel, out);
1493
+ };
1494
+ return GLTexture2D;
1495
+ }(GLTexture);
1496
+
1497
+ /**
1498
+ * Texture 2D array in WebGL platform.
1499
+ */ var GLTexture2DArray = /*#__PURE__*/ function(GLTexture1) {
1500
+ _inherits(GLTexture2DArray, GLTexture1);
1501
+ function GLTexture2DArray(rhi, texture2DArray) {
1502
+ var _this;
1503
+ _this = GLTexture1.call(this, rhi, texture2DArray, rhi.gl.TEXTURE_2D_ARRAY) || this;
1504
+ var format = texture2DArray.format, width = texture2DArray.width, height = texture2DArray.height, length = texture2DArray.length, mipmapCount = texture2DArray.mipmapCount;
1505
+ if (!_this._isWebGL2) {
1506
+ throw new Error("Texture2D Array is not supported in WebGL1.0");
1507
+ }
1508
+ /** @ts-ignore */ if (!GLTexture._supportTextureFormat(format, rhi)) {
1509
+ throw new Error("Texture format is not supported:" + TextureFormat[format]);
1510
+ }
1511
+ _this._bind();
1512
+ _this._formatDetail = GLTexture._getFormatDetail(format, _this._gl, true);
1513
+ _this._gl.texStorage3D(_this._target, mipmapCount, _this._formatDetail.internalFormat, width, height, length);
1514
+ return _this;
1515
+ }
1516
+ var _proto = GLTexture2DArray.prototype;
1517
+ /**
1518
+ * {@inheritDoc IPlatformTexture2DArray.setPixelBuffer}
1519
+ */ _proto.setPixelBuffer = function setPixelBuffer(offsetIndex, colorBuffer, mipLevel, x, y, width, height, length) {
1520
+ var _this = this, target = _this._target, gl = _this._gl;
1521
+ var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType, isCompressed = _this__formatDetail.isCompressed;
1522
+ width = width || Math.max(1, this._texture.width >> mipLevel) - x;
1523
+ height = height || Math.max(1, this._texture.height >> mipLevel) - y;
1524
+ length = length || this._texture.length;
1525
+ this._bind();
1526
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1527
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1528
+ if (isCompressed) {
1529
+ gl.compressedTexSubImage3D(target, mipLevel, x, y, offsetIndex, width, height, length, internalFormat, colorBuffer);
1530
+ } else {
1531
+ gl.texSubImage3D(target, mipLevel, x, y, offsetIndex, width, height, length, baseFormat, dataType, colorBuffer);
1532
+ }
1533
+ };
1534
+ /**
1535
+ * {@inheritDoc IPlatformTexture2DArray.setImageSource}
1536
+ */ _proto.setImageSource = function setImageSource(elementIndex, imageSource, mipLevel, flipY, premultiplyAlpha, x, y) {
1537
+ var gl = this._gl;
1538
+ var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1539
+ this._bind();
1540
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, +flipY);
1541
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, +premultiplyAlpha);
1542
+ gl.texSubImage3D(this._target, mipLevel, x, y, elementIndex, imageSource.width, imageSource.height, 1, baseFormat, dataType, imageSource);
1543
+ };
1544
+ /**
1545
+ * {@inheritDoc IPlatformTexture2DArray.getPixelBuffer}
1546
+ */ _proto.getPixelBuffer = function getPixelBuffer(elementIndex, x, y, width, height, mipLevel, out) {
1547
+ var _this = this, gl = _this._gl, formatDetail = _this._formatDetail;
1548
+ if (formatDetail.isCompressed) {
1549
+ throw new Error("Unable to read compressed texture");
1550
+ }
1551
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._getReadFrameBuffer());
1552
+ gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, this._glTexture, mipLevel, elementIndex);
1553
+ gl.readPixels(x, y, width, height, formatDetail.baseFormat, formatDetail.dataType, out);
1554
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1555
+ };
1556
+ return GLTexture2DArray;
1557
+ }(GLTexture);
1558
+
1559
+ /**
1560
+ * Cube texture in WebGL platform.
1561
+ */ var GLTextureCube = /*#__PURE__*/ function(GLTexture1) {
1562
+ _inherits(GLTextureCube, GLTexture1);
1563
+ function GLTextureCube(rhi, textureCube) {
1564
+ var _this;
1565
+ _this = GLTexture1.call(this, rhi, textureCube, rhi.gl.TEXTURE_CUBE_MAP) || this;
1566
+ /** Backward compatible with WebGL1.0. */ _this._compressedFaceFilled = [
1567
+ 0,
1568
+ 0,
1569
+ 0,
1570
+ 0,
1571
+ 0,
1572
+ 0
1573
+ ];
1574
+ /** @ts-ignore */ var format = textureCube.format, _mipmap = textureCube._mipmap, size = textureCube.width;
1575
+ var isWebGL2 = _this._isWebGL2;
1576
+ /** @ts-ignore */ if (!GLTexture._supportTextureFormat(format, rhi)) {
1577
+ throw new Error("Texture format is not supported:" + TextureFormat[format]);
1578
+ }
1579
+ if (_mipmap && !isWebGL2 && !GLTexture._isPowerOf2(size)) {
1580
+ Logger.warn("non-power-2 texture is not supported for mipmap in WebGL1,and has automatically downgraded to non-mipmap");
1581
+ /** @ts-ignore */ textureCube._mipmap = false;
1582
+ /** @ts-ignore */ textureCube._mipmapCount = textureCube._getMipmapCount();
1583
+ }
1584
+ _this._formatDetail = GLTexture._getFormatDetail(format, _this._gl, isWebGL2);
1585
+ _this._formatDetail.isCompressed && !isWebGL2 || _this._init(true);
1586
+ return _this;
1587
+ }
1588
+ var _proto = GLTextureCube.prototype;
1589
+ /**
1590
+ * {@inheritDoc IPlatformTextureCube.setPixelBuffer}
1591
+ */ _proto.setPixelBuffer = function setPixelBuffer(face, colorBuffer, mipLevel, x, y, width, height) {
1592
+ var gl = this._gl;
1593
+ var isWebGL2 = this._isWebGL2;
1594
+ var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType, isCompressed = _this__formatDetail.isCompressed;
1595
+ var mipSize = Math.max(1, this._texture.width >> mipLevel);
1596
+ width = width || mipSize - x;
1597
+ height = height || mipSize - y;
1598
+ this._bind();
1599
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1600
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1601
+ if (isCompressed) {
1602
+ var mipBit = 1 << mipLevel;
1603
+ if (isWebGL2 || this._compressedFaceFilled[face] & mipBit) {
1604
+ gl.compressedTexSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, x, y, width, height, internalFormat, colorBuffer);
1605
+ } else {
1606
+ gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, internalFormat, width, height, 0, colorBuffer);
1607
+ this._compressedFaceFilled[face] |= mipBit;
1608
+ }
1609
+ } else {
1610
+ gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, x, y, width, height, baseFormat, dataType, colorBuffer);
1611
+ }
1612
+ };
1613
+ /**
1614
+ * {@inheritDoc IPlatformTextureCube.setImageSource}
1615
+ */ _proto.setImageSource = function setImageSource(face, imageSource, mipLevel, flipY, premultiplyAlpha, x, y) {
1616
+ var gl = this._gl;
1617
+ var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1618
+ this._bind();
1619
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, +flipY);
1620
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, +premultiplyAlpha);
1621
+ gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, x || 0, y || 0, baseFormat, dataType, imageSource);
1622
+ };
1623
+ /**
1624
+ * {@inheritDoc IPlatformTextureCube.getPixelBuffer}
1625
+ */ _proto.getPixelBuffer = function getPixelBuffer(face, x, y, width, height, mipLevel, out) {
1626
+ if (this._formatDetail.isCompressed) {
1627
+ throw new Error("Unable to read compressed texture");
1628
+ }
1629
+ GLTexture1.prototype._getPixelBuffer.call(this, face, x, y, width, height, mipLevel, out);
1630
+ };
1631
+ return GLTextureCube;
1632
+ }(GLTexture);
1633
+
1634
+ var WebGLMode;
1635
+ (function(WebGLMode) {
1636
+ WebGLMode[WebGLMode[/** Auto, use WebGL2.0 if support, or will fallback to WebGL1.0. */ "Auto"] = 0] = "Auto";
1637
+ WebGLMode[WebGLMode[/** WebGL2.0. */ "WebGL2"] = 1] = "WebGL2";
1638
+ WebGLMode[WebGLMode[/** WebGL1.0, */ "WebGL1"] = 2] = "WebGL1";
1639
+ })(WebGLMode || (WebGLMode = {}));
1640
+ /**
1641
+ * WebGL renderer, including WebGL1.0 and WebGL2.0.
1642
+ */ var WebGLRenderer = /*#__PURE__*/ function() {
1643
+ function WebGLRenderer(initializeOptions) {
1644
+ if (initializeOptions === void 0) initializeOptions = {};
1645
+ /** @internal */ this._enableGlobalDepthBias = false;
1646
+ this._activeTextures = new Array(32);
1647
+ // cache value
1648
+ this._lastViewport = new Vector4(null, null, null, null);
1649
+ this._lastScissor = new Vector4(null, null, null, null);
1650
+ this._lastClearColor = new Color(null, null, null, null);
1651
+ this._scissorEnable = false;
1652
+ var options = _extends({
1653
+ webGLMode: 0,
1654
+ alpha: false,
1655
+ stencil: true,
1656
+ _forceFlush: false
1657
+ }, initializeOptions);
1658
+ if (SystemInfo.platform === Platform.IPhone || SystemInfo.platform === Platform.IPad) {
1659
+ var version = SystemInfo.operatingSystem.match(/(\d+).?(\d+)?.?(\d+)?/);
1660
+ if (version) {
1661
+ var majorVersion = parseInt(version[1]);
1662
+ var minorVersion = parseInt(version[2]);
1663
+ if (majorVersion === 15 && minorVersion >= 0 && minorVersion <= 4) {
1664
+ options._forceFlush = true;
1665
+ }
1666
+ }
1667
+ }
1668
+ this._options = options;
1669
+ }
1670
+ var _proto = WebGLRenderer.prototype;
1671
+ _proto.init = function init(canvas) {
1672
+ var options = this._options;
1673
+ var webCanvas = this._webCanvas = canvas._webCanvas;
1674
+ var webGLMode = options.webGLMode;
1675
+ var gl;
1676
+ if (webGLMode == 0 || webGLMode == 1) {
1677
+ gl = webCanvas.getContext("webgl2", options);
1678
+ if (!gl && (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas))) {
1679
+ gl = webCanvas.getContext("experimental-webgl2", options);
1680
+ }
1681
+ this._isWebGL2 = true;
1682
+ // Prevent weird browsers to lie (such as safari!)
1683
+ if (gl && !gl.deleteQuery) {
1684
+ this._isWebGL2 = false;
1685
+ }
1686
+ }
1687
+ if (!gl) {
1688
+ if (webGLMode == 0 || webGLMode == 2) {
1689
+ gl = webCanvas.getContext("webgl", options);
1690
+ if (!gl && (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas))) {
1691
+ gl = webCanvas.getContext("experimental-webgl", options);
1692
+ }
1693
+ this._isWebGL2 = false;
1694
+ }
1695
+ }
1696
+ if (!gl) {
1697
+ throw new Error("Get GL Context FAILED.");
1698
+ }
1699
+ this._gl = gl;
1700
+ this._activeTextureID = gl.TEXTURE0;
1701
+ this._renderStates = new GLRenderStates(gl);
1702
+ this._extensions = new GLExtensions(this);
1703
+ this._capability = new GLCapability(this);
1704
+ // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1705
+ gl.activeTexture(gl.TEXTURE0);
1706
+ var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1707
+ if (debugRenderInfo != null) {
1708
+ this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
1709
+ }
1710
+ };
1711
+ _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1712
+ return new GLPrimitive(this, primitive);
1713
+ };
1714
+ _proto.createPlatformTexture2D = function createPlatformTexture2D(texture2D) {
1715
+ return new GLTexture2D(this, texture2D);
1716
+ };
1717
+ _proto.createPlatformTexture2DArray = function createPlatformTexture2DArray(texture2D) {
1718
+ return new GLTexture2DArray(this, texture2D);
1719
+ };
1720
+ _proto.createPlatformTextureCube = function createPlatformTextureCube(textureCube) {
1721
+ return new GLTextureCube(this, textureCube);
1722
+ };
1723
+ _proto.createPlatformRenderTarget = function createPlatformRenderTarget(target) {
1724
+ return new GLRenderTarget(this, target);
1725
+ };
1726
+ _proto.requireExtension = function requireExtension(ext) {
1727
+ return this._extensions.requireExtension(ext);
1728
+ };
1729
+ _proto.canIUse = function canIUse(capabilityType) {
1730
+ return this.capability.canIUse(capabilityType);
1731
+ };
1732
+ _proto.canIUseCompressedTextureInternalFormat = function canIUseCompressedTextureInternalFormat(type) {
1733
+ return this.capability.canIUseCompressedTextureInternalFormat(type);
1734
+ };
1735
+ _proto.viewport = function viewport(x, y, width, height) {
1736
+ var _this = this, gl = _this._gl, lastViewport = _this._lastViewport;
1737
+ if (x !== lastViewport.x || y !== lastViewport.y || width !== lastViewport.z || height !== lastViewport.w) {
1738
+ gl.viewport(x, y, width, height);
1739
+ lastViewport.set(x, y, width, height);
1740
+ }
1741
+ };
1742
+ _proto.scissor = function scissor(x, y, width, height) {
1743
+ var _this = this, gl = _this._gl, lastScissor = _this._lastScissor;
1744
+ if (x !== lastScissor.x || y !== lastScissor.y || width !== lastScissor.z || height !== lastScissor.w) {
1745
+ var _this1 = this, webCanvas = _this1._webCanvas;
1746
+ if (x === 0 && y === 0 && width === webCanvas.width && height === webCanvas.height) {
1747
+ if (this._scissorEnable) {
1748
+ gl.disable(gl.SCISSOR_TEST);
1749
+ this._scissorEnable = false;
1750
+ }
1751
+ } else {
1752
+ if (!this._scissorEnable) {
1753
+ gl.enable(gl.SCISSOR_TEST);
1754
+ this._scissorEnable = true;
1755
+ }
1756
+ gl.scissor(x, y, width, height);
1757
+ }
1758
+ lastScissor.set(x, y, width, height);
1759
+ }
1760
+ };
1761
+ _proto.colorMask = function colorMask(r, g, b, a) {
1762
+ this._gl.colorMask(r, g, b, a);
1763
+ };
1764
+ _proto.clearRenderTarget = function clearRenderTarget(engine, clearFlags, clearColor) {
1765
+ var gl = this._gl;
1766
+ var _engine__lastRenderState = engine._lastRenderState, targetBlendState = _engine__lastRenderState.blendState.targetBlendState, depthState = _engine__lastRenderState.depthState, stencilState = _engine__lastRenderState.stencilState;
1767
+ var clearFlag = 0;
1768
+ if (clearFlags & CameraClearFlags.Color) {
1769
+ clearFlag |= gl.COLOR_BUFFER_BIT;
1770
+ var lc = this._lastClearColor;
1771
+ var r = clearColor.r, g = clearColor.g, b = clearColor.b, a = clearColor.a;
1772
+ if (clearColor && (r !== lc.r || g !== lc.g || b !== lc.b || a !== lc.a)) {
1773
+ gl.clearColor(r, g, b, a);
1774
+ lc.set(r, g, b, a);
1775
+ }
1776
+ if (targetBlendState.colorWriteMask !== ColorWriteMask.All) {
1777
+ gl.colorMask(true, true, true, true);
1778
+ targetBlendState.colorWriteMask = ColorWriteMask.All;
1779
+ }
1780
+ }
1781
+ if (clearFlags & CameraClearFlags.Depth) {
1782
+ clearFlag |= gl.DEPTH_BUFFER_BIT;
1783
+ if (depthState.writeEnabled !== true) {
1784
+ gl.depthMask(true);
1785
+ depthState.writeEnabled = true;
1786
+ }
1787
+ }
1788
+ if (clearFlags & CameraClearFlags.Stencil) {
1789
+ clearFlag |= gl.STENCIL_BUFFER_BIT;
1790
+ if (stencilState.writeMask !== 0xff) {
1791
+ gl.stencilMask(0xff);
1792
+ stencilState.writeMask = 0xff;
1793
+ }
1794
+ }
1795
+ gl.clear(clearFlag);
1796
+ };
1797
+ _proto.drawPrimitive = function drawPrimitive(primitive, subPrimitive, shaderProgram) {
1798
+ // todo: VAO not support morph animation
1799
+ if (primitive) {
1800
+ //@ts-ignore
1801
+ primitive._draw(shaderProgram, subPrimitive);
1802
+ } else {
1803
+ Logger.error("draw primitive failed.");
1804
+ }
1805
+ };
1806
+ _proto.activeRenderTarget = function activeRenderTarget(renderTarget, viewport, mipLevel) {
1807
+ var gl = this._gl;
1808
+ if (renderTarget) {
1809
+ var /** @ts-ignore */ _renderTarget__platformRenderTarget;
1810
+ (_renderTarget__platformRenderTarget = renderTarget._platformRenderTarget) == null ? void 0 : _renderTarget__platformRenderTarget._activeRenderTarget();
1811
+ var width = renderTarget.width >> mipLevel;
1812
+ var height = renderTarget.height >> mipLevel;
1813
+ this.viewport(0, 0, width, height);
1814
+ this.scissor(0, 0, width, height);
1815
+ } else {
1816
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1817
+ var drawingBufferWidth = gl.drawingBufferWidth, drawingBufferHeight = gl.drawingBufferHeight;
1818
+ var width1 = drawingBufferWidth * viewport.z;
1819
+ var height1 = drawingBufferHeight * viewport.w;
1820
+ var x = viewport.x * drawingBufferWidth;
1821
+ var y = drawingBufferHeight - viewport.y * drawingBufferHeight - height1;
1822
+ this.viewport(x, y, width1, height1);
1823
+ this.scissor(x, y, width1, height1);
1824
+ }
1825
+ };
1826
+ _proto.activeTexture = function activeTexture(textureID) {
1827
+ if (this._activeTextureID !== textureID) {
1828
+ this._gl.activeTexture(textureID);
1829
+ this._activeTextureID = textureID;
1830
+ }
1831
+ };
1832
+ _proto.bindTexture = function bindTexture(texture) {
1833
+ var index = this._activeTextureID - this._gl.TEXTURE0;
1834
+ if (this._activeTextures[index] !== texture) {
1835
+ this._gl.bindTexture(texture._target, texture._glTexture);
1836
+ this._activeTextures[index] = texture;
1837
+ }
1838
+ };
1839
+ _proto.setGlobalDepthBias = function setGlobalDepthBias(bias, slopeBias) {
1840
+ var gl = this._gl;
1841
+ var enable = bias !== 0 || slopeBias !== 0;
1842
+ if (enable) {
1843
+ gl.enable(gl.POLYGON_OFFSET_FILL);
1844
+ gl.polygonOffset(slopeBias, bias);
1845
+ } else {
1846
+ gl.disable(gl.POLYGON_OFFSET_FILL);
1847
+ }
1848
+ this._enableGlobalDepthBias = enable;
1849
+ };
1850
+ _proto.flush = function flush() {
1851
+ this._gl.flush();
1852
+ };
1853
+ _proto.destroy = function destroy() {};
1854
+ _createClass(WebGLRenderer, [
1855
+ {
1856
+ key: "isWebGL2",
1857
+ get: function get() {
1858
+ return this._isWebGL2;
1859
+ }
1860
+ },
1861
+ {
1862
+ key: "renderer",
1863
+ get: function get() {
1864
+ return this._renderer;
1865
+ }
1866
+ },
1867
+ {
1868
+ key: "gl",
1869
+ get: /**
1870
+ * GL Context
1871
+ * @member {WebGLRenderingContext}
1872
+ */ function get() {
1873
+ return this._gl;
1874
+ }
1875
+ },
1876
+ {
1877
+ key: "renderStates",
1878
+ get: function get() {
1879
+ return this._renderStates;
1880
+ }
1881
+ },
1882
+ {
1883
+ key: "capability",
1884
+ get: function get() {
1885
+ return this._capability;
1886
+ }
1887
+ },
1888
+ {
1889
+ key: "canIUseMoreJoints",
1890
+ get: function get() {
1891
+ return this.capability.canIUseMoreJoints;
1892
+ }
1893
+ }
1894
+ ]);
1895
+ return WebGLRenderer;
1896
+ }();
1897
+
1898
+ /**
1899
+ * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
1900
+ */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
1901
+ _inherits(WebGLEngine, Engine);
1902
+ function WebGLEngine(canvas, webGLRendererOptions) {
1903
+ var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
1904
+ var hardwareRenderer = new WebGLRenderer(webGLRendererOptions);
1905
+ return Engine.call(this, webCanvas, hardwareRenderer);
1906
+ }
1907
+ _createClass(WebGLEngine, [
1908
+ {
1909
+ key: "canvas",
1910
+ get: /**
1911
+ * Web canvas.
1912
+ */ function get() {
1913
+ return this._canvas;
1914
+ }
1915
+ }
1916
+ ]);
1917
+ return WebGLEngine;
1918
+ }(Engine);
1919
+
1920
+ export { GLCompressedTextureInternalFormat, WebCanvas, WebGLEngine, WebGLMode, WebGLRenderer };
1921
+ //# sourceMappingURL=module.js.map