@galacean/engine-rhi-webgl 1.6.8 → 1.6.10

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,2223 @@
1
+ import { Canvas, Scene, Engine, BufferUsage, BufferBindFlag, SetDataOptions, GLCapabilityType, Logger, TextureUtils, TextureUsage, TextureWrapMode, TextureFormat, TextureFilterMode, TextureDepthCompareFunction, Texture, TextureCube, CameraClearFlags, ColorWriteMask, SystemInfo, Platform } from '@galacean/engine-core';
2
+ import { Vector2, MathUtil, Vector4, Color } from '@galacean/engine-math';
3
+
4
+ /**
5
+ * Smoothing plug-in.
6
+ * */ var GLCompressedTextureInternalFormat = /*#__PURE__*/ function(GLCompressedTextureInternalFormat) {
7
+ // astc
8
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_4X4_KHR"] = 37808] = "RGBA_ASTC_4X4_KHR";
9
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_5X4_KHR"] = 37809] = "RGBA_ASTC_5X4_KHR";
10
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_5X5_KHR"] = 37810] = "RGBA_ASTC_5X5_KHR";
11
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_6X5_KHR"] = 37811] = "RGBA_ASTC_6X5_KHR";
12
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_6X6_KHR"] = 37812] = "RGBA_ASTC_6X6_KHR";
13
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_8X5_KHR"] = 37813] = "RGBA_ASTC_8X5_KHR";
14
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_8X6_KHR"] = 37814] = "RGBA_ASTC_8X6_KHR";
15
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_8X8_KHR"] = 37815] = "RGBA_ASTC_8X8_KHR";
16
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_10X5_KHR"] = 37816] = "RGBA_ASTC_10X5_KHR";
17
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_10X6_KHR"] = 37817] = "RGBA_ASTC_10X6_KHR";
18
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_10X8_KHR"] = 37818] = "RGBA_ASTC_10X8_KHR";
19
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_10X10_KHR"] = 37819] = "RGBA_ASTC_10X10_KHR";
20
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_12X10_KHR"] = 37820] = "RGBA_ASTC_12X10_KHR";
21
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_12X12_KHR"] = 37821] = "RGBA_ASTC_12X12_KHR";
22
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_4X4_KHR"] = 37840] = "SRGB8_ALPHA8_ASTC_4X4_KHR";
23
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_5X4_KHR"] = 37841] = "SRGB8_ALPHA8_ASTC_5X4_KHR";
24
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_5X5_KHR"] = 37842] = "SRGB8_ALPHA8_ASTC_5X5_KHR";
25
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_6X5_KHR"] = 37843] = "SRGB8_ALPHA8_ASTC_6X5_KHR";
26
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_6X6_KHR"] = 37844] = "SRGB8_ALPHA8_ASTC_6X6_KHR";
27
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_8X5_KHR"] = 37845] = "SRGB8_ALPHA8_ASTC_8X5_KHR";
28
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_8X6_KHR"] = 37846] = "SRGB8_ALPHA8_ASTC_8X6_KHR";
29
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_8X8_KHR"] = 37847] = "SRGB8_ALPHA8_ASTC_8X8_KHR";
30
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_10X5_KHR"] = 37848] = "SRGB8_ALPHA8_ASTC_10X5_KHR";
31
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_10X6_KHR"] = 37849] = "SRGB8_ALPHA8_ASTC_10X6_KHR";
32
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_10X8_KHR"] = 37850] = "SRGB8_ALPHA8_ASTC_10X8_KHR";
33
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_10X10_KHR"] = 37851] = "SRGB8_ALPHA8_ASTC_10X10_KHR";
34
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_12X10_KHR"] = 37852] = "SRGB8_ALPHA8_ASTC_12X10_KHR";
35
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ASTC_12X12_KHR"] = 37853] = "SRGB8_ALPHA8_ASTC_12X12_KHR";
36
+ // etc1
37
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB_ETC1_WEBGL"] = 36196] = "RGB_ETC1_WEBGL";
38
+ // etc2
39
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["R11_EAC"] = 37488] = "R11_EAC";
40
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SIGNED_R11_EAC"] = 37489] = "SIGNED_R11_EAC";
41
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RG11_EAC"] = 37490] = "RG11_EAC";
42
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SIGNED_RG11_EAC"] = 37491] = "SIGNED_RG11_EAC";
43
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB8_ETC2"] = 37492] = "RGB8_ETC2";
44
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ETC2"] = 37493] = "SRGB8_ETC2";
45
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB8_PUNCHTHROUGH_ALPHA1_ETC2"] = 37494] = "RGB8_PUNCHTHROUGH_ALPHA1_ETC2";
46
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_PUNCHTHROUGH_ALPHA1_ETC2"] = 37495] = "SRGB8_PUNCHTHROUGH_ALPHA1_ETC2";
47
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA8_ETC2_EAC"] = 37496] = "RGBA8_ETC2_EAC";
48
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB8_ALPHA8_ETC2_EAC"] = 37497] = "SRGB8_ALPHA8_ETC2_EAC";
49
+ // pvrtc
50
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB_PVRTC_4BPPV1_IMG"] = 35840] = "RGB_PVRTC_4BPPV1_IMG";
51
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB_PVRTC_2BPPV1_IMG"] = 35841] = "RGB_PVRTC_2BPPV1_IMG";
52
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_PVRTC_4BPPV1_IMG"] = 35842] = "RGBA_PVRTC_4BPPV1_IMG";
53
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_PVRTC_2BPPV1_IMG"] = 35843] = "RGBA_PVRTC_2BPPV1_IMG";
54
+ // s3tc
55
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB_S3TC_DXT1_EXT"] = 33776] = "RGB_S3TC_DXT1_EXT";
56
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT1_EXT"] = 33777] = "RGBA_S3TC_DXT1_EXT";
57
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT3_EXT"] = 33778] = "RGBA_S3TC_DXT3_EXT";
58
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT5_EXT"] = 33779] = "RGBA_S3TC_DXT5_EXT";
59
+ // s3tc sRGB
60
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_S3TC_DXT1_EXT"] = 35916] = "SRGB_S3TC_DXT1_EXT";
61
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_ALPHA_S3TC_DXT1_EXT"] = 35917] = "SRGB_ALPHA_S3TC_DXT1_EXT";
62
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_ALPHA_S3TC_DXT3_EXT"] = 35918] = "SRGB_ALPHA_S3TC_DXT3_EXT";
63
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_ALPHA_S3TC_DXT5_EXT"] = 35919] = "SRGB_ALPHA_S3TC_DXT5_EXT";
64
+ // bptc
65
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_BPTC_UNORM_EXT"] = 36492] = "RGBA_BPTC_UNORM_EXT";
66
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_ALPHA_BPTC_UNORM_EXT"] = 36493] = "SRGB_ALPHA_BPTC_UNORM_EXT";
67
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB_BPTC_SIGNED_FLOAT_EXT"] = 36494] = "RGB_BPTC_SIGNED_FLOAT_EXT";
68
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB_BPTC_UNSIGNED_FLOAT_EXT"] = 36495] = "RGB_BPTC_UNSIGNED_FLOAT_EXT";
69
+ return GLCompressedTextureInternalFormat;
70
+ }({});
71
+
72
+ function _defineProperties(target, props) {
73
+ for (var i = 0; i < props.length; i++) {
74
+ var descriptor = props[i];
75
+ descriptor.enumerable = descriptor.enumerable || false;
76
+ descriptor.configurable = true;
77
+
78
+ if ("value" in descriptor) descriptor.writable = true;
79
+
80
+ Object.defineProperty(target, descriptor.key, descriptor);
81
+ }
82
+ }
83
+ function _create_class(Constructor, protoProps, staticProps) {
84
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
85
+ if (staticProps) _defineProperties(Constructor, staticProps);
86
+
87
+ return Constructor;
88
+ }
89
+
90
+ function _set_prototype_of(o, p) {
91
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
92
+ o.__proto__ = p;
93
+
94
+ return o;
95
+ };
96
+
97
+ return _set_prototype_of(o, p);
98
+ }
99
+
100
+ function _inherits(subClass, superClass) {
101
+ if (typeof superClass !== "function" && superClass !== null) {
102
+ throw new TypeError("Super expression must either be null or a function");
103
+ }
104
+
105
+ subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });
106
+
107
+ if (superClass) _set_prototype_of(subClass, superClass);
108
+ }
109
+
110
+ function _instanceof(left, right) {
111
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
112
+ return !!right[Symbol.hasInstance](left);
113
+ } else return left instanceof right;
114
+ }
115
+
116
+ /**
117
+ * The canvas used on the web, which can support HTMLCanvasElement and OffscreenCanvas.
118
+ */ var WebCanvas = /*#__PURE__*/ function(Canvas) {
119
+ _inherits(WebCanvas, Canvas);
120
+ function WebCanvas(webCanvas) {
121
+ var _this;
122
+ _this = Canvas.call(this) || this, _this._scale = new Vector2();
123
+ var width = webCanvas.width;
124
+ var height = webCanvas.height;
125
+ _this._webCanvas = webCanvas;
126
+ _this.width = width;
127
+ _this.height = height;
128
+ return _this;
129
+ }
130
+ var _proto = WebCanvas.prototype;
131
+ /**
132
+ * Resize the rendering size according to the clientWidth and clientHeight of the canvas.
133
+ * @param pixelRatio - Pixel ratio
134
+ */ _proto.resizeByClientSize = function resizeByClientSize(pixelRatio) {
135
+ if (pixelRatio === void 0) pixelRatio = window.devicePixelRatio;
136
+ var webCanvas = this._webCanvas;
137
+ if (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas)) {
138
+ var exportWidth = webCanvas.clientWidth * pixelRatio;
139
+ var exportHeight = webCanvas.clientHeight * pixelRatio;
140
+ this.width = exportWidth;
141
+ this.height = exportHeight;
142
+ }
143
+ };
144
+ /**
145
+ * Set scale.
146
+ * @param x - Scale along the X axis
147
+ * @param y - Scale along the Y axis
148
+ */ _proto.setScale = function setScale(x, y) {
149
+ this._scale.set(x, y);
150
+ this.scale = this._scale;
151
+ };
152
+ _proto._onWidthChanged = function _onWidthChanged(value) {
153
+ this._webCanvas.width = value;
154
+ };
155
+ _proto._onHeightChange = function _onHeightChange(value) {
156
+ this._webCanvas.height = value;
157
+ };
158
+ _create_class(WebCanvas, [
159
+ {
160
+ key: "scale",
161
+ get: /**
162
+ * The scale of canvas, the value is visible width/height divide the render width/height.
163
+ * @remarks Need to re-assign after modification to ensure that the modification takes effect.
164
+ */ function get() {
165
+ var webCanvas = this._webCanvas;
166
+ if (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas)) {
167
+ this._scale.set(webCanvas.clientWidth * devicePixelRatio / webCanvas.width, webCanvas.clientHeight * devicePixelRatio / webCanvas.height);
168
+ }
169
+ return this._scale;
170
+ },
171
+ set: function set(value) {
172
+ var webCanvas = this._webCanvas;
173
+ if (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas)) {
174
+ webCanvas.style.transformOrigin = "left top";
175
+ webCanvas.style.transform = "scale(" + value.x + ", " + value.y + ")";
176
+ }
177
+ }
178
+ }
179
+ ]);
180
+ return WebCanvas;
181
+ }(Canvas);
182
+
183
+ /**
184
+ * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
185
+ */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
186
+ _inherits(WebGLEngine, Engine);
187
+ function WebGLEngine() {
188
+ return Engine.apply(this, arguments) || this;
189
+ }
190
+ /**
191
+ * Create a WebGL engine.
192
+ * @param configuration - WebGL engine configuration
193
+ * @returns A promise that will resolve when the engine is created
194
+ */ WebGLEngine.create = function create(configuration) {
195
+ var canvas = configuration.canvas;
196
+ var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
197
+ var webGLGraphicDevice = new WebGLGraphicDevice(configuration.graphicDeviceOptions);
198
+ var engine = new WebGLEngine(webCanvas, webGLGraphicDevice, configuration);
199
+ // @ts-ignore
200
+ var promise = engine._initialize(configuration);
201
+ return promise.then(function() {
202
+ engine.sceneManager.addScene(new Scene(engine, "DefaultScene"));
203
+ return engine;
204
+ });
205
+ };
206
+ _create_class(WebGLEngine, [
207
+ {
208
+ key: "canvas",
209
+ get: /**
210
+ * Web canvas.
211
+ */ function get() {
212
+ // @ts-ignore
213
+ return this._canvas;
214
+ }
215
+ }
216
+ ]);
217
+ return WebGLEngine;
218
+ }(Engine);
219
+
220
+ function _extends() {
221
+ _extends = Object.assign || function assign(target) {
222
+ for (var i = 1; i < arguments.length; i++) {
223
+ var source = arguments[i];
224
+ for (var key in source) if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
225
+ }
226
+
227
+ return target;
228
+ };
229
+
230
+ return _extends.apply(this, arguments);
231
+ }
232
+
233
+ var GLBuffer = /*#__PURE__*/ function() {
234
+ function GLBuffer(rhi, type, byteLength, bufferUsage, data) {
235
+ if (bufferUsage === void 0) bufferUsage = BufferUsage.Static;
236
+ var gl = rhi.gl;
237
+ var glBuffer = gl.createBuffer();
238
+ var glBufferUsage = this._getGLBufferUsage(gl, bufferUsage);
239
+ var glBindTarget = type === BufferBindFlag.VertexBuffer ? gl.ARRAY_BUFFER : gl.ELEMENT_ARRAY_BUFFER;
240
+ this._gl = gl;
241
+ this._glBuffer = glBuffer;
242
+ this._glBufferUsage = glBufferUsage;
243
+ this._glBindTarget = glBindTarget;
244
+ this._isWebGL2 = rhi.isWebGL2;
245
+ this.bind();
246
+ if (data) {
247
+ gl.bufferData(glBindTarget, data, glBufferUsage);
248
+ } else {
249
+ gl.bufferData(glBindTarget, byteLength, glBufferUsage);
250
+ }
251
+ gl.bindBuffer(glBindTarget, null);
252
+ }
253
+ var _proto = GLBuffer.prototype;
254
+ _proto.bind = function bind() {
255
+ this._gl.bindBuffer(this._glBindTarget, this._glBuffer);
256
+ };
257
+ _proto.setData = function setData(byteLength, data, bufferByteOffset, dataOffset, dataLength, options) {
258
+ var gl = this._gl;
259
+ var glBindTarget = this._glBindTarget;
260
+ this.bind();
261
+ if (options === SetDataOptions.Discard) {
262
+ gl.bufferData(glBindTarget, byteLength, this._glBufferUsage);
263
+ }
264
+ // TypeArray is BYTES_PER_ELEMENT, unTypeArray is 1
265
+ var byteSize = data.BYTES_PER_ELEMENT || 1;
266
+ var dataByteLength = dataLength ? byteSize * dataLength : data.byteLength;
267
+ if (dataOffset !== 0 || dataByteLength < data.byteLength) {
268
+ var isArrayBufferView = data.byteOffset !== undefined;
269
+ if (this._isWebGL2 && isArrayBufferView) {
270
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data, dataOffset, dataByteLength / byteSize);
271
+ } else {
272
+ var subData = new Uint8Array(isArrayBufferView ? data.buffer : data, dataOffset * byteSize, dataByteLength);
273
+ gl.bufferSubData(glBindTarget, bufferByteOffset, subData);
274
+ }
275
+ } else {
276
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data);
277
+ }
278
+ gl.bindBuffer(glBindTarget, null);
279
+ };
280
+ _proto.getData = function getData(data, bufferByteOffset, dataOffset, dataLength) {
281
+ if (this._isWebGL2) {
282
+ var gl = this._gl;
283
+ this.bind();
284
+ gl.getBufferSubData(this._glBindTarget, bufferByteOffset, data, dataOffset, dataLength);
285
+ } else {
286
+ throw "Buffer is write-only on WebGL1.0 platforms.";
287
+ }
288
+ };
289
+ _proto.destroy = function destroy() {
290
+ this._gl.deleteBuffer(this._glBuffer);
291
+ this._gl = null;
292
+ this._glBuffer = null;
293
+ };
294
+ _proto._getGLBufferUsage = function _getGLBufferUsage(gl, bufferUsage) {
295
+ switch(bufferUsage){
296
+ case BufferUsage.Static:
297
+ return gl.STATIC_DRAW;
298
+ case BufferUsage.Dynamic:
299
+ return gl.DYNAMIC_DRAW;
300
+ case BufferUsage.Stream:
301
+ return gl.STREAM_DRAW;
302
+ }
303
+ };
304
+ return GLBuffer;
305
+ }();
306
+
307
+ /**
308
+ * GL capability.
309
+ */ var GLCapability = /*#__PURE__*/ function() {
310
+ function GLCapability(rhi) {
311
+ this._rhi = rhi;
312
+ this.capabilityList = new Map();
313
+ this._init();
314
+ this._compatibleAllInterface();
315
+ }
316
+ var _proto = GLCapability.prototype;
317
+ /**
318
+ * Check device capabilities.
319
+ */ _proto.canIUse = function canIUse(capabilityType) {
320
+ return this.capabilityList.get(capabilityType);
321
+ };
322
+ /**
323
+ * Check if can use some compressed texture format.
324
+ */ _proto.canIUseCompressedTextureInternalFormat = function canIUseCompressedTextureInternalFormat(internalType) {
325
+ var // astc
326
+ 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
327
+ RGB_ETC1_WEBGL = GLCompressedTextureInternalFormat.RGB_ETC1_WEBGL, // etc
328
+ R11_EAC = GLCompressedTextureInternalFormat.R11_EAC, SRGB8_ALPHA8_ETC2_EAC = GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ETC2_EAC, // pvrtc
329
+ RGB_PVRTC_4BPPV1_IMG = GLCompressedTextureInternalFormat.RGB_PVRTC_4BPPV1_IMG, RGBA_PVRTC_2BPPV1_IMG = GLCompressedTextureInternalFormat.RGBA_PVRTC_2BPPV1_IMG, // s3tc
330
+ RGB_S3TC_DXT1_EXT = GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT, RGBA_S3TC_DXT5_EXT = GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT, // bptc
331
+ RGBA_BPTC_UNORM_EXT = GLCompressedTextureInternalFormat.RGBA_BPTC_UNORM_EXT, RGB_BPTC_UNSIGNED_FLOAT_EXT = GLCompressedTextureInternalFormat.RGB_BPTC_UNSIGNED_FLOAT_EXT;
332
+ 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) {
333
+ return this.canIUse(GLCapabilityType.astc);
334
+ } else if (internalType === RGB_ETC1_WEBGL) {
335
+ return this.canIUse(GLCapabilityType.etc1);
336
+ } else if (internalType >= R11_EAC && internalType <= SRGB8_ALPHA8_ETC2_EAC) {
337
+ return this.canIUse(GLCapabilityType.etc);
338
+ } else if (internalType >= RGB_PVRTC_4BPPV1_IMG && internalType <= RGBA_PVRTC_2BPPV1_IMG) {
339
+ return this.canIUse(GLCapabilityType.pvrtc);
340
+ } else if (internalType >= RGB_S3TC_DXT1_EXT && internalType <= RGBA_S3TC_DXT5_EXT) {
341
+ return this.canIUse(GLCapabilityType.s3tc);
342
+ } else if (internalType >= RGBA_BPTC_UNORM_EXT && internalType <= RGB_BPTC_UNSIGNED_FLOAT_EXT) {
343
+ return this.canIUse(GLCapabilityType.bptc);
344
+ }
345
+ return false;
346
+ };
347
+ /**
348
+ * Init capabilities.
349
+ */ _proto._init = function _init() {
350
+ var cap = this.capabilityList;
351
+ var isWebGL2 = this.rhi.isWebGL2;
352
+ var requireExtension = this.rhi.requireExtension.bind(this.rhi);
353
+ 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, blendMinMax = GLCapabilityType.blendMinMax, astc = GLCapabilityType.astc, astc_webkit = GLCapabilityType.astc_webkit, astc_hdr = GLCapabilityType.astc_hdr, 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, bptc = GLCapabilityType.bptc, s3tc_srgb = GLCapabilityType.s3tc_srgb, 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, fragDepth = GLCapabilityType.fragDepth, sRGB = GLCapabilityType.sRGB;
354
+ cap.set(shaderVertexID, isWebGL2);
355
+ cap.set(standardDerivatives, isWebGL2 || !!requireExtension(standardDerivatives));
356
+ cap.set(shaderTextureLod, isWebGL2 || !!requireExtension(shaderTextureLod));
357
+ cap.set(elementIndexUint, isWebGL2 || !!requireExtension(elementIndexUint));
358
+ cap.set(depthTexture, isWebGL2 || !!requireExtension(depthTexture));
359
+ cap.set(vertexArrayObject, isWebGL2 || !!requireExtension(vertexArrayObject));
360
+ cap.set(instancedArrays, isWebGL2 || !!requireExtension(instancedArrays));
361
+ cap.set(multipleSample, isWebGL2);
362
+ cap.set(drawBuffers, isWebGL2 || !!requireExtension(drawBuffers));
363
+ cap.set(blendMinMax, isWebGL2 || !!requireExtension(blendMinMax));
364
+ cap.set(textureFloat, isWebGL2 || !!requireExtension(textureFloat));
365
+ cap.set(textureHalfFloat, isWebGL2 || !!requireExtension(textureHalfFloat));
366
+ cap.set(textureFloatLinear, !!requireExtension(textureFloatLinear));
367
+ cap.set(textureHalfFloatLinear, isWebGL2 || !!requireExtension(textureHalfFloatLinear));
368
+ cap.set(colorBufferFloat, isWebGL2 && !!requireExtension(colorBufferFloat) || !!requireExtension(WEBGL_colorBufferFloat));
369
+ cap.set(colorBufferHalfFloat, isWebGL2 && !!requireExtension(colorBufferFloat) || !!requireExtension(colorBufferHalfFloat));
370
+ cap.set(textureFilterAnisotropic, !!requireExtension(textureFilterAnisotropic));
371
+ cap.set(fragDepth, isWebGL2 || !!requireExtension(fragDepth));
372
+ cap.set(etc, !!(requireExtension(etc) || requireExtension(etc_webkit)));
373
+ cap.set(etc1, !!(requireExtension(etc1) || requireExtension(etc1_webkit)));
374
+ cap.set(pvrtc, !!(requireExtension(pvrtc) || requireExtension(pvrtc_webkit)));
375
+ cap.set(s3tc, !!(requireExtension(s3tc) || requireExtension(s3tc_webkit)));
376
+ cap.set(s3tc_srgb, !!requireExtension(s3tc_srgb));
377
+ cap.set(bptc, !!requireExtension(bptc));
378
+ var astcExtension = requireExtension(astc) || requireExtension(astc_webkit);
379
+ cap.set(astc, !!astcExtension);
380
+ cap.set(astc_hdr, !!(astcExtension == null ? void 0 : astcExtension.getSupportedProfiles().includes("hdr")));
381
+ cap.set(sRGB, isWebGL2 || !!requireExtension(sRGB));
382
+ };
383
+ /**
384
+ * If there are extensions that can supplement this ability, smooth out the difference.
385
+ * @example
386
+ * compatible(GLCapabilityType.depthTexture,{
387
+ * UNSIGNED_INT_24_8: "UNSIGNED_INT_24_8_WEBGL"
388
+ * })
389
+ * gl.UNSIGNED_INT_24_8 = ext.UNSIGNED_INT_24_8_WEBGL
390
+ */ _proto._compatibleInterface = function _compatibleInterface(capabilityType, flatItem) {
391
+ var rhi = this.rhi;
392
+ var gl = rhi.gl;
393
+ var ext = null;
394
+ if (ext = rhi.requireExtension(capabilityType)) {
395
+ for(var glKey in flatItem){
396
+ var extensionKey = flatItem[glKey];
397
+ var extensionVal = ext[extensionKey];
398
+ // Mini game hack the native function,use “.bind” to smooth out if is “Funcion”.
399
+ if (extensionVal == null ? void 0 : extensionVal.bind) {
400
+ gl[glKey] = extensionVal.bind(ext);
401
+ } else {
402
+ gl[glKey] = extensionVal;
403
+ }
404
+ }
405
+ }
406
+ };
407
+ _proto._compatibleAllInterface = function _compatibleAllInterface() {
408
+ 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, blendMinMax = GLCapabilityType.blendMinMax, sRGB = GLCapabilityType.sRGB;
409
+ var isWebGL2 = this.rhi.isWebGL2;
410
+ if (!isWebGL2) {
411
+ this._compatibleInterface(blendMinMax, {
412
+ MIN: "MIN_EXT",
413
+ MAX: "MAX_EXT"
414
+ });
415
+ this._compatibleInterface(depthTexture, {
416
+ UNSIGNED_INT_24_8: "UNSIGNED_INT_24_8_WEBGL"
417
+ });
418
+ this._compatibleInterface(vertexArrayObject, {
419
+ createVertexArray: "createVertexArrayOES",
420
+ deleteVertexArray: "deleteVertexArrayOES",
421
+ isVertexArray: "isVertexArrayOES",
422
+ bindVertexArray: "bindVertexArrayOES"
423
+ });
424
+ this._compatibleInterface(instancedArrays, {
425
+ drawArraysInstanced: "drawArraysInstancedANGLE",
426
+ drawElementsInstanced: "drawElementsInstancedANGLE",
427
+ vertexAttribDivisor: "vertexAttribDivisorANGLE"
428
+ });
429
+ this._compatibleInterface(drawBuffers, {
430
+ MAX_DRAW_BUFFERS: "MAX_DRAW_BUFFERS_WEBGL"
431
+ });
432
+ var items = {};
433
+ if (this.canIUse(GLCapabilityType.drawBuffers)) {
434
+ var maxDrawBuffers = this.maxDrawBuffers;
435
+ for(var i = 0; i < maxDrawBuffers; i++){
436
+ i != 0 && (items["COLOR_ATTACHMENT" + i] = "COLOR_ATTACHMENT" + i + "_WEBGL");
437
+ items["DRAW_BUFFER" + i] = "DRAW_BUFFER" + i + "_WEBGL";
438
+ }
439
+ this._compatibleInterface(drawBuffers, _extends({
440
+ drawBuffers: "drawBuffersWEBGL"
441
+ }, items));
442
+ }
443
+ this._compatibleInterface(textureHalfFloat, {
444
+ HALF_FLOAT: "HALF_FLOAT_OES"
445
+ });
446
+ this._compatibleInterface(colorBufferHalfFloat, {
447
+ RGBA16F: "RBGA16F_EXT"
448
+ });
449
+ this._compatibleInterface(WEBGL_colorBufferFloat, {
450
+ RGBA32F: "RBGA32F_EXT"
451
+ });
452
+ this._compatibleInterface(sRGB, {
453
+ SRGB8: "SRGB_EXT",
454
+ SRGB8_ALPHA8: "SRGB_ALPHA_EXT"
455
+ });
456
+ }
457
+ this._compatibleInterface(textureFilterAnisotropic, {
458
+ TEXTURE_MAX_ANISOTROPY_EXT: "TEXTURE_MAX_ANISOTROPY_EXT"
459
+ });
460
+ };
461
+ _create_class(GLCapability, [
462
+ {
463
+ key: "maxTextureSize",
464
+ get: function get() {
465
+ return this.rhi.renderStates.getParameter(this.rhi.gl.MAX_TEXTURE_SIZE);
466
+ }
467
+ },
468
+ {
469
+ key: "canUseFloatTextureBlendShape",
470
+ get: function get() {
471
+ return this.canIUse(GLCapabilityType.shaderVertexID) && this.canIUse(GLCapabilityType.textureFloat) && this.rhi.renderStates.getParameter(this.rhi.gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS) > 0;
472
+ }
473
+ },
474
+ {
475
+ key: "canIUseMoreJoints",
476
+ get: /**
477
+ * Whether can use more joints.
478
+ */ function get() {
479
+ return this.canIUse(GLCapabilityType.textureFloat) && this.rhi.renderStates.getParameter(this.rhi.gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS) > 0;
480
+ }
481
+ },
482
+ {
483
+ key: "maxDrawBuffers",
484
+ get: function get() {
485
+ if (!this._maxDrawBuffers) {
486
+ if (this.canIUse(GLCapabilityType.drawBuffers)) {
487
+ this._maxDrawBuffers = this._rhi.gl.getParameter(this._rhi.gl.MAX_DRAW_BUFFERS);
488
+ } else {
489
+ this._maxDrawBuffers = 1;
490
+ }
491
+ }
492
+ return this._maxDrawBuffers;
493
+ }
494
+ },
495
+ {
496
+ key: "maxAnisoLevel",
497
+ get: /**
498
+ * Max anisoLevel.
499
+ */ function get() {
500
+ if (!this._maxAnisoLevel) {
501
+ var ext = this._rhi.requireExtension(GLCapabilityType.textureFilterAnisotropic);
502
+ this._maxAnisoLevel = ext ? this._rhi.gl.getParameter(ext.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 1;
503
+ }
504
+ return this._maxAnisoLevel;
505
+ }
506
+ },
507
+ {
508
+ key: "maxAntiAliasing",
509
+ get: /**
510
+ * Max MSAA count.
511
+ */ function get() {
512
+ if (!this._maxAntiAliasing) {
513
+ var gl = this._rhi.gl;
514
+ var canMSAA = this.canIUse(GLCapabilityType.multipleSample);
515
+ this._maxAntiAliasing = canMSAA ? gl.getParameter(gl.MAX_SAMPLES) : 1;
516
+ }
517
+ return this._maxAntiAliasing;
518
+ }
519
+ },
520
+ {
521
+ key: "isFragmentHighPrecision",
522
+ get: function get() {
523
+ var gl = this._rhi.gl;
524
+ return gl.getShaderPrecisionFormat(gl.FRAGMENT_SHADER, gl.HIGH_FLOAT).precision !== 0;
525
+ }
526
+ },
527
+ {
528
+ key: "rhi",
529
+ get: function get() {
530
+ return this._rhi;
531
+ }
532
+ }
533
+ ]);
534
+ return GLCapability;
535
+ }();
536
+
537
+ /**
538
+ * GLContext extension.
539
+ */ var GLExtensions = /*#__PURE__*/ function() {
540
+ function GLExtensions(rhi) {
541
+ this.rhi = rhi;
542
+ this._requireResult = {};
543
+ }
544
+ var _proto = GLExtensions.prototype;
545
+ /**
546
+ * Require an extension.
547
+ */ _proto.requireExtension = function requireExtension(ext) {
548
+ if (this._requireResult[ext] !== undefined) {
549
+ return this._requireResult[ext];
550
+ }
551
+ this._requireResult[ext] = this.rhi.gl.getExtension(ext);
552
+ return this._requireResult[ext];
553
+ };
554
+ return GLExtensions;
555
+ }();
556
+
557
+ /**
558
+ * Improvement of VAO:
559
+ * 1) WebGL2.0 must support VAO, almost all devices support vao extensions in webgl1.0, we can use PollyFill,only keep VAO mode.
560
+ */ /**
561
+ * @internal
562
+ * GL platform primitive.
563
+ */ var GLPrimitive = /*#__PURE__*/ function() {
564
+ function GLPrimitive(rhi, primitive) {
565
+ this._attribLocArray = [];
566
+ this._vaoMap = new Map();
567
+ this._primitive = primitive;
568
+ this._canUseInstancedArrays = rhi.canIUse(GLCapabilityType.instancedArrays);
569
+ this._isSupportVAO = rhi.canIUse(GLCapabilityType.vertexArrayObject);
570
+ this._gl = rhi.gl;
571
+ }
572
+ var _proto = GLPrimitive.prototype;
573
+ /**
574
+ * Draw the primitive.
575
+ */ _proto.draw = function draw(shaderProgram, subMesh) {
576
+ var gl = this._gl;
577
+ var primitive = this._primitive;
578
+ var useVao = this._isSupportVAO && primitive.enableVAO;
579
+ if (useVao) {
580
+ if (primitive._bufferStructChanged) {
581
+ this._clearVAO();
582
+ }
583
+ if (!this._vaoMap.has(shaderProgram.id)) {
584
+ this._registerVAO(shaderProgram);
585
+ }
586
+ var vao = this._vaoMap.get(shaderProgram.id);
587
+ gl.bindVertexArray(vao);
588
+ } else {
589
+ this._bindBufferAndAttrib(shaderProgram);
590
+ }
591
+ var indexBufferBinding = primitive.indexBufferBinding, instanceCount = primitive.instanceCount, _glIndexType = primitive._glIndexType, _glIndexByteCount = primitive._glIndexByteCount;
592
+ var topology = subMesh.topology, start = subMesh.start, count = subMesh.count;
593
+ if (!instanceCount) {
594
+ if (indexBufferBinding) {
595
+ if (useVao) {
596
+ gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
597
+ } else {
598
+ var _glBuffer = indexBufferBinding.buffer._platformBuffer._glBuffer;
599
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer);
600
+ gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
601
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
602
+ }
603
+ } else {
604
+ gl.drawArrays(topology, start, count);
605
+ }
606
+ } else {
607
+ if (this._canUseInstancedArrays) {
608
+ if (indexBufferBinding) {
609
+ if (useVao) {
610
+ gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, instanceCount);
611
+ } else {
612
+ var _glBuffer1 = indexBufferBinding.buffer._platformBuffer._glBuffer;
613
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer1);
614
+ gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, instanceCount);
615
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
616
+ }
617
+ } else {
618
+ gl.drawArraysInstanced(topology, start, count, instanceCount);
619
+ }
620
+ } else {
621
+ Logger.error("ANGLE_instanced_arrays extension is not supported");
622
+ }
623
+ }
624
+ // Unbind
625
+ if (useVao) {
626
+ gl.bindVertexArray(null);
627
+ } else {
628
+ this._disableAttrib();
629
+ }
630
+ };
631
+ _proto.destroy = function destroy() {
632
+ this._isSupportVAO && this._clearVAO();
633
+ };
634
+ /**
635
+ * Bind buffer and attribute.
636
+ */ _proto._bindBufferAndAttrib = function _bindBufferAndAttrib(shaderProgram) {
637
+ var gl = this._gl;
638
+ var primitive = this._primitive;
639
+ var vertexBufferBindings = primitive.vertexBufferBindings;
640
+ this._attribLocArray.length = 0;
641
+ var attributeLocation = shaderProgram.attributeLocation;
642
+ var attributes = primitive._vertexElementMap;
643
+ var vbo;
644
+ var lastBoundVbo;
645
+ for(var name in attributeLocation){
646
+ var loc = attributeLocation[name];
647
+ if (loc === -1) continue;
648
+ var element = attributes[name];
649
+ if (element) {
650
+ var _vertexBufferBindings_element_bindingIndex = vertexBufferBindings[element.bindingIndex], buffer = _vertexBufferBindings_element_bindingIndex.buffer, stride = _vertexBufferBindings_element_bindingIndex.stride;
651
+ vbo = buffer._platformBuffer._glBuffer;
652
+ // prevent binding the vbo which already bound at the last loop, e.g. a buffer with multiple attributes.
653
+ if (lastBoundVbo !== vbo) {
654
+ lastBoundVbo = vbo;
655
+ gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
656
+ }
657
+ gl.enableVertexAttribArray(loc);
658
+ var elementInfo = element._formatMetaInfo;
659
+ gl.vertexAttribPointer(loc, elementInfo.size, elementInfo.type, elementInfo.normalized, stride, element.offset);
660
+ if (this._canUseInstancedArrays) {
661
+ gl.vertexAttribDivisor(loc, element.instanceStepRate);
662
+ }
663
+ this._attribLocArray.push(loc);
664
+ } else {
665
+ Logger.warn("vertex attribute not found: " + name);
666
+ }
667
+ }
668
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
669
+ };
670
+ _proto._disableAttrib = function _disableAttrib() {
671
+ var gl = this._gl;
672
+ for(var i = 0, l = this._attribLocArray.length; i < l; i++){
673
+ gl.disableVertexAttribArray(this._attribLocArray[i]);
674
+ }
675
+ };
676
+ _proto._registerVAO = function _registerVAO(shaderProgram) {
677
+ var gl = this._gl;
678
+ var vao = gl.createVertexArray();
679
+ /** register VAO */ gl.bindVertexArray(vao);
680
+ var indexBufferBinding = this._primitive.indexBufferBinding;
681
+ if (indexBufferBinding) {
682
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBufferBinding.buffer._platformBuffer._glBuffer);
683
+ }
684
+ this._bindBufferAndAttrib(shaderProgram);
685
+ /** unbind */ gl.bindVertexArray(null);
686
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
687
+ this._disableAttrib();
688
+ this._vaoMap.set(shaderProgram.id, vao);
689
+ };
690
+ _proto._clearVAO = function _clearVAO() {
691
+ var gl = this._gl;
692
+ this._vaoMap.forEach(function(vao) {
693
+ gl.deleteVertexArray(vao);
694
+ });
695
+ this._vaoMap.clear();
696
+ };
697
+ return GLPrimitive;
698
+ }();
699
+
700
+ /**
701
+ * @private
702
+ */ var GLRenderStates = /*#__PURE__*/ function() {
703
+ function GLRenderStates(gl) {
704
+ this._parameters = {};
705
+ this._gl = gl;
706
+ this._parameters = {}; // current gl state parameters
707
+ /** cache */ this._parameters[gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS] = gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS);
708
+ this._parameters[gl.MAX_VERTEX_UNIFORM_VECTORS] = gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS);
709
+ this._parameters[gl.MAX_VERTEX_ATTRIBS] = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
710
+ this._parameters[gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS] = gl.getParameter(gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS);
711
+ this._parameters[gl.MAX_TEXTURE_SIZE] = gl.getParameter(gl.MAX_TEXTURE_SIZE);
712
+ // init blend state same as BlendState default value.
713
+ gl.blendFuncSeparate(gl.ONE, gl.ZERO, gl.ONE, gl.ZERO);
714
+ gl.blendEquationSeparate(gl.FUNC_ADD, gl.FUNC_ADD);
715
+ gl.colorMask(true, true, true, true);
716
+ gl.blendColor(0, 0, 0, 0);
717
+ gl.disable(gl.SAMPLE_ALPHA_TO_COVERAGE);
718
+ // init depth state same as DepthState default value.
719
+ gl.enable(gl.DEPTH_TEST);
720
+ gl.depthFunc(gl.LESS);
721
+ gl.depthMask(true);
722
+ // init stencil state same as StencilState default value.
723
+ gl.disable(gl.STENCIL_TEST);
724
+ gl.stencilFuncSeparate(gl.FRONT, gl.ALWAYS, 0, 0xff);
725
+ gl.stencilFuncSeparate(gl.BACK, gl.ALWAYS, 0, 0xff);
726
+ gl.stencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.KEEP);
727
+ gl.stencilOpSeparate(gl.BACK, gl.KEEP, gl.KEEP, gl.KEEP);
728
+ gl.stencilMask(0xff);
729
+ // init raster state same as RasterState default value.
730
+ gl.enable(gl.CULL_FACE);
731
+ gl.cullFace(gl.BACK);
732
+ gl.disable(gl.POLYGON_OFFSET_FILL);
733
+ gl.polygonOffset(0, 0);
734
+ }
735
+ var _proto = GLRenderStates.prototype;
736
+ /**
737
+ * Get a parameter.
738
+ */ _proto.getParameter = function getParameter(pname) {
739
+ return this._parameters[pname];
740
+ };
741
+ return GLRenderStates;
742
+ }();
743
+
744
+ /**
745
+ * Texture in WebGL platform.
746
+ */ var GLTexture = /*#__PURE__*/ function() {
747
+ function GLTexture(rhi, texture, target) {
748
+ this._texture = texture;
749
+ this._rhi = rhi;
750
+ this._gl = rhi.gl;
751
+ this._isWebGL2 = rhi.isWebGL2;
752
+ this._target = target;
753
+ this._glTexture = this._gl.createTexture();
754
+ }
755
+ var _proto = GLTexture.prototype;
756
+ /**
757
+ * Destroy texture.
758
+ */ _proto.destroy = function destroy() {
759
+ this._gl.deleteTexture(this._glTexture);
760
+ this._texture = null;
761
+ this._glTexture = null;
762
+ this._formatDetail = null;
763
+ };
764
+ /**
765
+ * @internal
766
+ */ _proto.setUseDepthCompareMode = function setUseDepthCompareMode(value) {
767
+ var gl = this._gl;
768
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_MODE, value ? gl.COMPARE_REF_TO_TEXTURE : gl.NONE);
769
+ };
770
+ /**
771
+ * Generate multi-level textures based on the 0th level data.
772
+ */ _proto.generateMipmaps = function generateMipmaps() {
773
+ var texture = this._texture;
774
+ //@ts-ignore
775
+ var mipmap = texture._mipmap;
776
+ if (!TextureUtils.supportGenerateMipmaps(texture.format, mipmap, texture.isSRGBColorSpace, this._isWebGL2)) {
777
+ Logger.warn("Auto-generating mipmaps for sRGB textures is only supported in [WebGL2 + R8G8B8A8], you must generate mipmaps manually.");
778
+ return;
779
+ }
780
+ // @todo (1x1).generateMipmap() will flash back in uc.
781
+ if (texture.width !== 1 || texture.height !== 1) {
782
+ this._bind();
783
+ this._gl.generateMipmap(this._target);
784
+ }
785
+ };
786
+ _proto._bind = function _bind() {
787
+ this._rhi.bindTexture(this);
788
+ };
789
+ /**
790
+ * Pre-development mipmapping GPU memory.
791
+ */ _proto._init = function _init(isCube) {
792
+ var gl = this._gl;
793
+ var isWebGL2 = this._isWebGL2;
794
+ var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
795
+ // @ts-ignore
796
+ var _this__texture = this._texture, mipmapCount = _this__texture.mipmapCount, width = _this__texture.width, height = _this__texture.height, usage = _this__texture.usage, _isDepthTexture = _this__texture._isDepthTexture;
797
+ this._bind();
798
+ if (isWebGL2 && !(baseFormat === gl.LUMINANCE_ALPHA || baseFormat === gl.ALPHA) && usage !== TextureUsage.Dynamic) {
799
+ gl.texStorage2D(this._target, mipmapCount, internalFormat, width, height);
800
+ } else {
801
+ if (!isCube) {
802
+ if (_isDepthTexture) {
803
+ gl.texImage2D(this._target, 0, internalFormat, width, height, 0, baseFormat, dataType, null);
804
+ } else {
805
+ for(var i = 0; i < mipmapCount; i++){
806
+ var mipWidth = Math.max(1, width >> i);
807
+ var mipHeight = Math.max(1, height >> i);
808
+ gl.texImage2D(this._target, i, internalFormat, mipWidth, mipHeight, 0, baseFormat, dataType, null);
809
+ }
810
+ }
811
+ } else {
812
+ for(var i1 = 0; i1 < mipmapCount; i1++){
813
+ var size = Math.max(1, width >> i1);
814
+ for(var faceIndex = 0; faceIndex < 6; faceIndex++){
815
+ gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex, i1, internalFormat, size, size, 0, baseFormat, dataType, null);
816
+ }
817
+ }
818
+ }
819
+ }
820
+ };
821
+ /**
822
+ * Get the pixel color buffer according to the specified cube face and area.
823
+ * @param face - You can choose which cube face to read
824
+ * @param x - X coordinate of area start
825
+ * @param y - Y coordinate of area start
826
+ * @param width - Area width
827
+ * @param height - Area height
828
+ * @param out - Color buffer
829
+ * @param mipLevel - Set mip level the data want to get from
830
+ */ _proto._getPixelBuffer = function _getPixelBuffer(face, x, y, width, height, mipLevel, out) {
831
+ var gl = this._gl;
832
+ var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType, readFormat = _this__formatDetail.readFormat, alignment = _this__formatDetail.alignment;
833
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._getReadFrameBuffer());
834
+ if (mipLevel > 0 && !this._isWebGL2) {
835
+ mipLevel = 0;
836
+ Logger.error("mipLevel only take effect in WebGL2.0");
837
+ }
838
+ if (face != null) {
839
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, this._glTexture, mipLevel);
840
+ } else {
841
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._glTexture, mipLevel);
842
+ }
843
+ gl.pixelStorei(gl.PACK_ALIGNMENT, alignment);
844
+ // Base format is different from read format in webgl1.0 with sRGB
845
+ gl.readPixels(x, y, width, height, readFormat != null ? readFormat : baseFormat, dataType, out);
846
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
847
+ };
848
+ _proto._getReadFrameBuffer = function _getReadFrameBuffer() {
849
+ var frameBuffer = this._rhi._readFrameBuffer;
850
+ if (!frameBuffer) {
851
+ this._rhi._readFrameBuffer = frameBuffer = this._gl.createFramebuffer();
852
+ }
853
+ return frameBuffer;
854
+ };
855
+ _proto._validate = function _validate(texture, rhi) {
856
+ var format = texture.format, width = texture.width, height = texture.height;
857
+ // Validate sRGB format
858
+ // @ts-ignore
859
+ var isSRGBColorSpace = texture._isSRGBColorSpace;
860
+ if (isSRGBColorSpace && !TextureUtils.supportSRGB(format)) {
861
+ Logger.warn("Only support sRGB color space in RGB8 or RGBA8 or some compressed texture format");
862
+ // @ts-ignore
863
+ texture._isSRGBColorSpace = false;
864
+ }
865
+ var isWebGL2 = rhi.isWebGL2;
866
+ // Validate mipmap
867
+ // @ts-ignore
868
+ var mipmap = texture._mipmap;
869
+ if (mipmap && !TextureUtils.supportMipmaps(width, height, isWebGL2)) {
870
+ Logger.warn("Non-power-2 texture is not supported for mipmap in WebGL1, and has automatically downgraded to non-mipmap");
871
+ /** @ts-ignore */ texture._mipmap = false;
872
+ /** @ts-ignore */ texture._mipmapCount = texture._getMipmapCount();
873
+ }
874
+ };
875
+ _proto._setWrapMode = function _setWrapMode(value, pname) {
876
+ var gl = this._gl;
877
+ var isWebGL2 = this._isWebGL2;
878
+ var target = this._target;
879
+ var _this__texture = this._texture, width = _this__texture.width, height = _this__texture.height;
880
+ if (!isWebGL2 && value !== TextureWrapMode.Clamp && (!MathUtil.isPowerOf2(width) || !MathUtil.isPowerOf2(height))) {
881
+ Logger.warn("non-power-2 texture is not supported for REPEAT or MIRRORED_REPEAT in WebGL1,and has automatically downgraded to CLAMP_TO_EDGE");
882
+ value = TextureWrapMode.Clamp;
883
+ }
884
+ switch(value){
885
+ case TextureWrapMode.Clamp:
886
+ gl.texParameteri(target, pname, gl.CLAMP_TO_EDGE);
887
+ break;
888
+ case TextureWrapMode.Repeat:
889
+ gl.texParameteri(target, pname, gl.REPEAT);
890
+ break;
891
+ case TextureWrapMode.Mirror:
892
+ gl.texParameteri(target, pname, gl.MIRRORED_REPEAT);
893
+ break;
894
+ }
895
+ };
896
+ /**
897
+ * Get more texture info from TextureFormat.
898
+ * @internal
899
+ */ GLTexture._getFormatDetail = function _getFormatDetail(format, isSRGBColorSpace, gl, isWebGL2) {
900
+ switch(format){
901
+ case TextureFormat.R8G8B8:
902
+ return {
903
+ internalFormat: isSRGBColorSpace ? gl.SRGB8 : isWebGL2 ? gl.RGB8 : gl.RGB,
904
+ baseFormat: isSRGBColorSpace ? isWebGL2 ? gl.RGB : gl.SRGB8 : gl.RGB,
905
+ readFormat: gl.RGB,
906
+ dataType: gl.UNSIGNED_BYTE,
907
+ isCompressed: false,
908
+ alignment: 1
909
+ };
910
+ case TextureFormat.R8G8B8A8:
911
+ return {
912
+ internalFormat: isSRGBColorSpace ? gl.SRGB8_ALPHA8 : isWebGL2 ? gl.RGBA8 : gl.RGBA,
913
+ baseFormat: isSRGBColorSpace ? isWebGL2 ? gl.RGBA : gl.SRGB8_ALPHA8 : gl.RGBA,
914
+ readFormat: gl.RGBA,
915
+ dataType: gl.UNSIGNED_BYTE,
916
+ isCompressed: false,
917
+ alignment: 4
918
+ };
919
+ case TextureFormat.R4G4B4A4:
920
+ return {
921
+ internalFormat: isWebGL2 ? gl.RGBA4 : gl.RGBA,
922
+ baseFormat: gl.RGBA,
923
+ dataType: gl.UNSIGNED_SHORT_4_4_4_4,
924
+ isCompressed: false,
925
+ alignment: 2
926
+ };
927
+ case TextureFormat.R5G5B5A1:
928
+ return {
929
+ internalFormat: isWebGL2 ? gl.RGB5_A1 : gl.RGBA,
930
+ baseFormat: gl.RGBA,
931
+ dataType: gl.UNSIGNED_SHORT_5_5_5_1,
932
+ isCompressed: false,
933
+ alignment: 2
934
+ };
935
+ case TextureFormat.R5G6B5:
936
+ return {
937
+ internalFormat: isWebGL2 ? gl.RGB565 : gl.RGB,
938
+ baseFormat: gl.RGB,
939
+ dataType: gl.UNSIGNED_SHORT_5_6_5,
940
+ isCompressed: false,
941
+ alignment: 2
942
+ };
943
+ case TextureFormat.Alpha8:
944
+ return {
945
+ internalFormat: gl.ALPHA,
946
+ baseFormat: gl.ALPHA,
947
+ dataType: gl.UNSIGNED_BYTE,
948
+ isCompressed: false,
949
+ alignment: 1
950
+ };
951
+ case TextureFormat.LuminanceAlpha:
952
+ return {
953
+ internalFormat: gl.LUMINANCE_ALPHA,
954
+ baseFormat: gl.LUMINANCE_ALPHA,
955
+ dataType: gl.UNSIGNED_BYTE,
956
+ isCompressed: false,
957
+ alignment: 2
958
+ };
959
+ case TextureFormat.R16G16B16A16:
960
+ return {
961
+ internalFormat: isWebGL2 ? gl.RGBA16F : gl.RGBA,
962
+ baseFormat: gl.RGBA,
963
+ dataType: gl.HALF_FLOAT,
964
+ isCompressed: false,
965
+ alignment: 8
966
+ };
967
+ case TextureFormat.R32G32B32A32:
968
+ return {
969
+ internalFormat: isWebGL2 ? gl.RGBA32F : gl.RGBA,
970
+ baseFormat: gl.RGBA,
971
+ dataType: gl.FLOAT,
972
+ isCompressed: false,
973
+ alignment: 8
974
+ };
975
+ // Only WebGL2 support
976
+ case TextureFormat.R11G11B10_UFloat:
977
+ return {
978
+ internalFormat: gl.R11F_G11F_B10F,
979
+ baseFormat: gl.RGB,
980
+ dataType: gl.FLOAT,
981
+ isCompressed: false,
982
+ alignment: 4
983
+ };
984
+ // Only WebGL2 support
985
+ case TextureFormat.R32G32B32A32_UInt:
986
+ return {
987
+ internalFormat: gl.RGBA32UI,
988
+ baseFormat: gl.RGBA_INTEGER,
989
+ dataType: gl.UNSIGNED_INT,
990
+ isCompressed: false,
991
+ alignment: 8
992
+ };
993
+ // Only WebGL2 support
994
+ case TextureFormat.R8:
995
+ return {
996
+ internalFormat: gl.R8,
997
+ baseFormat: gl.RED,
998
+ dataType: gl.UNSIGNED_BYTE,
999
+ isCompressed: false,
1000
+ alignment: 1
1001
+ };
1002
+ // Only WebGL2 support
1003
+ case TextureFormat.R8G8:
1004
+ return {
1005
+ internalFormat: gl.RG8,
1006
+ baseFormat: gl.RG,
1007
+ dataType: gl.UNSIGNED_BYTE,
1008
+ isCompressed: false,
1009
+ alignment: 2
1010
+ };
1011
+ case TextureFormat.BC1:
1012
+ return {
1013
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB_S3TC_DXT1_EXT : GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT,
1014
+ isCompressed: true
1015
+ };
1016
+ case TextureFormat.BC3:
1017
+ return {
1018
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB_ALPHA_S3TC_DXT5_EXT : GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT,
1019
+ isCompressed: true
1020
+ };
1021
+ case TextureFormat.BC7:
1022
+ return {
1023
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB_ALPHA_BPTC_UNORM_EXT : GLCompressedTextureInternalFormat.RGBA_BPTC_UNORM_EXT,
1024
+ isCompressed: true
1025
+ };
1026
+ case TextureFormat.BC6H:
1027
+ return {
1028
+ internalFormat: GLCompressedTextureInternalFormat.RGB_BPTC_UNSIGNED_FLOAT_EXT,
1029
+ isCompressed: true
1030
+ };
1031
+ case TextureFormat.ETC1_RGB:
1032
+ return {
1033
+ internalFormat: GLCompressedTextureInternalFormat.RGB_ETC1_WEBGL,
1034
+ isCompressed: true
1035
+ };
1036
+ case TextureFormat.ETC2_RGB:
1037
+ return {
1038
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ETC2 : GLCompressedTextureInternalFormat.RGB8_ETC2,
1039
+ isCompressed: true
1040
+ };
1041
+ case TextureFormat.ETC2_RGBA5:
1042
+ return {
1043
+ internalFormat: GLCompressedTextureInternalFormat.RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
1044
+ isCompressed: true
1045
+ };
1046
+ case TextureFormat.ETC2_RGBA8:
1047
+ return {
1048
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ETC2_EAC : GLCompressedTextureInternalFormat.RGBA8_ETC2_EAC,
1049
+ isCompressed: true
1050
+ };
1051
+ case TextureFormat.PVRTC_RGB2:
1052
+ return {
1053
+ internalFormat: GLCompressedTextureInternalFormat.RGB_PVRTC_2BPPV1_IMG,
1054
+ isCompressed: true
1055
+ };
1056
+ case TextureFormat.PVRTC_RGBA2:
1057
+ return {
1058
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_PVRTC_2BPPV1_IMG,
1059
+ isCompressed: true
1060
+ };
1061
+ case TextureFormat.PVRTC_RGB4:
1062
+ return {
1063
+ internalFormat: GLCompressedTextureInternalFormat.RGB_PVRTC_4BPPV1_IMG,
1064
+ isCompressed: true
1065
+ };
1066
+ case TextureFormat.PVRTC_RGBA4:
1067
+ return {
1068
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_PVRTC_4BPPV1_IMG,
1069
+ isCompressed: true
1070
+ };
1071
+ case TextureFormat.ASTC_4x4:
1072
+ return {
1073
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ASTC_4X4_KHR : GLCompressedTextureInternalFormat.RGBA_ASTC_4X4_KHR,
1074
+ isCompressed: true
1075
+ };
1076
+ case TextureFormat.ASTC_5x5:
1077
+ return {
1078
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_5X5_KHR,
1079
+ isCompressed: true
1080
+ };
1081
+ case TextureFormat.ASTC_6x6:
1082
+ return {
1083
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_6X6_KHR,
1084
+ isCompressed: true
1085
+ };
1086
+ case TextureFormat.ASTC_8x8:
1087
+ return {
1088
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_8X8_KHR,
1089
+ isCompressed: true
1090
+ };
1091
+ case TextureFormat.ASTC_10x10:
1092
+ return {
1093
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_10X10_KHR,
1094
+ isCompressed: true
1095
+ };
1096
+ case TextureFormat.ASTC_12x12:
1097
+ return {
1098
+ internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_12X12_KHR,
1099
+ isCompressed: true
1100
+ };
1101
+ case TextureFormat.Depth:
1102
+ return {
1103
+ internalFormat: isWebGL2 ? gl.DEPTH_COMPONENT32F : gl.DEPTH_COMPONENT,
1104
+ baseFormat: gl.DEPTH_COMPONENT,
1105
+ dataType: isWebGL2 ? gl.FLOAT : gl.UNSIGNED_SHORT,
1106
+ isCompressed: false,
1107
+ attachment: gl.DEPTH_ATTACHMENT
1108
+ };
1109
+ case TextureFormat.DepthStencil:
1110
+ return {
1111
+ internalFormat: isWebGL2 ? gl.DEPTH32F_STENCIL8 : gl.DEPTH_STENCIL,
1112
+ baseFormat: gl.DEPTH_STENCIL,
1113
+ dataType: isWebGL2 ? gl.FLOAT_32_UNSIGNED_INT_24_8_REV : gl.UNSIGNED_INT_24_8,
1114
+ isCompressed: false,
1115
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
1116
+ };
1117
+ case TextureFormat.Depth16:
1118
+ return {
1119
+ internalFormat: isWebGL2 ? gl.DEPTH_COMPONENT16 : gl.DEPTH_COMPONENT,
1120
+ baseFormat: gl.DEPTH_COMPONENT,
1121
+ dataType: gl.UNSIGNED_SHORT,
1122
+ isCompressed: false,
1123
+ attachment: gl.DEPTH_ATTACHMENT
1124
+ };
1125
+ case TextureFormat.Depth24Stencil8:
1126
+ return {
1127
+ internalFormat: isWebGL2 ? gl.DEPTH24_STENCIL8 : gl.DEPTH_STENCIL,
1128
+ baseFormat: gl.DEPTH_STENCIL,
1129
+ dataType: gl.UNSIGNED_INT_24_8,
1130
+ isCompressed: false,
1131
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
1132
+ };
1133
+ // Only WebGL2 support
1134
+ case TextureFormat.Depth24:
1135
+ return {
1136
+ internalFormat: gl.DEPTH_COMPONENT24,
1137
+ baseFormat: gl.DEPTH_COMPONENT,
1138
+ dataType: gl.UNSIGNED_INT,
1139
+ isCompressed: false,
1140
+ attachment: gl.DEPTH_ATTACHMENT
1141
+ };
1142
+ // Only WebGL2 support
1143
+ case TextureFormat.Depth32:
1144
+ return {
1145
+ internalFormat: gl.DEPTH_COMPONENT32F,
1146
+ baseFormat: gl.DEPTH_COMPONENT,
1147
+ dataType: gl.FLOAT,
1148
+ isCompressed: false,
1149
+ attachment: gl.DEPTH_ATTACHMENT
1150
+ };
1151
+ // Only WebGL2 support
1152
+ case TextureFormat.Depth32Stencil8:
1153
+ return {
1154
+ internalFormat: gl.DEPTH32F_STENCIL8,
1155
+ baseFormat: gl.DEPTH_STENCIL,
1156
+ dataType: gl.FLOAT_32_UNSIGNED_INT_24_8_REV,
1157
+ isCompressed: false,
1158
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
1159
+ };
1160
+ default:
1161
+ throw new Error("this TextureFormat is not supported in Galacean Engine: " + format);
1162
+ }
1163
+ };
1164
+ /**
1165
+ * In WebGL 1, internalformat must be the same as baseFormat when call texImage2D.
1166
+ * @internal
1167
+ */ GLTexture._getRenderBufferDepthFormatDetail = function _getRenderBufferDepthFormatDetail(format, gl, isWebGL2) {
1168
+ switch(format){
1169
+ case TextureFormat.Depth:
1170
+ return {
1171
+ internalFormat: isWebGL2 ? gl.DEPTH_COMPONENT32F : gl.DEPTH_COMPONENT16,
1172
+ baseFormat: gl.DEPTH_COMPONENT,
1173
+ dataType: isWebGL2 ? gl.FLOAT : gl.UNSIGNED_SHORT,
1174
+ isCompressed: false,
1175
+ attachment: gl.DEPTH_ATTACHMENT
1176
+ };
1177
+ case TextureFormat.DepthStencil:
1178
+ return {
1179
+ internalFormat: isWebGL2 ? gl.DEPTH32F_STENCIL8 : gl.DEPTH_STENCIL,
1180
+ baseFormat: gl.DEPTH_STENCIL,
1181
+ dataType: isWebGL2 ? gl.FLOAT_32_UNSIGNED_INT_24_8_REV : gl.UNSIGNED_INT_24_8,
1182
+ isCompressed: false,
1183
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
1184
+ };
1185
+ case TextureFormat.Stencil:
1186
+ return {
1187
+ internalFormat: gl.STENCIL_INDEX8,
1188
+ baseFormat: gl.STENCIL_ATTACHMENT,
1189
+ dataType: gl.UNSIGNED_BYTE,
1190
+ isCompressed: false,
1191
+ attachment: gl.STENCIL_ATTACHMENT
1192
+ };
1193
+ case TextureFormat.Depth16:
1194
+ return {
1195
+ internalFormat: gl.DEPTH_COMPONENT16,
1196
+ baseFormat: gl.DEPTH_COMPONENT,
1197
+ dataType: gl.UNSIGNED_SHORT,
1198
+ isCompressed: false,
1199
+ attachment: gl.DEPTH_ATTACHMENT
1200
+ };
1201
+ case TextureFormat.Depth24Stencil8:
1202
+ return {
1203
+ internalFormat: isWebGL2 ? gl.DEPTH24_STENCIL8 : gl.DEPTH_STENCIL,
1204
+ baseFormat: gl.DEPTH_STENCIL,
1205
+ dataType: gl.UNSIGNED_INT_24_8,
1206
+ isCompressed: false,
1207
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
1208
+ };
1209
+ case TextureFormat.Depth24:
1210
+ return {
1211
+ internalFormat: gl.DEPTH_COMPONENT24,
1212
+ baseFormat: gl.DEPTH_COMPONENT,
1213
+ dataType: gl.UNSIGNED_INT,
1214
+ isCompressed: false,
1215
+ attachment: gl.DEPTH_ATTACHMENT
1216
+ };
1217
+ case TextureFormat.Depth32:
1218
+ return {
1219
+ internalFormat: gl.DEPTH_COMPONENT32F,
1220
+ baseFormat: gl.DEPTH_COMPONENT,
1221
+ dataType: gl.FLOAT,
1222
+ isCompressed: false,
1223
+ attachment: gl.DEPTH_ATTACHMENT
1224
+ };
1225
+ case TextureFormat.Depth32Stencil8:
1226
+ return {
1227
+ internalFormat: gl.DEPTH32F_STENCIL8,
1228
+ baseFormat: gl.DEPTH_STENCIL,
1229
+ dataType: gl.FLOAT_32_UNSIGNED_INT_24_8_REV,
1230
+ isCompressed: false,
1231
+ attachment: gl.DEPTH_STENCIL_ATTACHMENT
1232
+ };
1233
+ default:
1234
+ throw new Error("this TextureFormat is not supported in Galacean Engine: " + format);
1235
+ }
1236
+ };
1237
+ /**
1238
+ * @internal
1239
+ */ GLTexture._supportRenderBufferColorFormat = function _supportRenderBufferColorFormat(format, rhi) {
1240
+ var isSupported = true;
1241
+ switch(format){
1242
+ case TextureFormat.R16G16B16A16:
1243
+ {
1244
+ if (!rhi.canIUse(GLCapabilityType.colorBufferHalfFloat) || !rhi.canIUse(GLCapabilityType.textureHalfFloat)) {
1245
+ isSupported = false;
1246
+ }
1247
+ }
1248
+ break;
1249
+ case TextureFormat.R32G32B32A32:
1250
+ {
1251
+ if (!rhi.canIUse(GLCapabilityType.colorBufferFloat) || !rhi.canIUse(GLCapabilityType.textureFloat)) {
1252
+ isSupported = false;
1253
+ }
1254
+ }
1255
+ break;
1256
+ case TextureFormat.R11G11B10_UFloat:
1257
+ {
1258
+ isSupported = rhi.isWebGL2;
1259
+ }
1260
+ break;
1261
+ }
1262
+ return isSupported;
1263
+ };
1264
+ /**
1265
+ * @internal
1266
+ */ GLTexture._supportRenderBufferDepthFormat = function _supportRenderBufferDepthFormat(format, rhi) {
1267
+ if (!rhi.isWebGL2) {
1268
+ switch(format){
1269
+ case TextureFormat.Depth24:
1270
+ case TextureFormat.Depth32:
1271
+ case TextureFormat.Depth32Stencil8:
1272
+ return false;
1273
+ }
1274
+ }
1275
+ return true;
1276
+ };
1277
+ _create_class(GLTexture, [
1278
+ {
1279
+ key: "wrapModeU",
1280
+ set: /**
1281
+ * Wrapping mode for texture coordinate S.
1282
+ */ function set(value) {
1283
+ this._bind();
1284
+ this._setWrapMode(value, this._gl.TEXTURE_WRAP_S);
1285
+ }
1286
+ },
1287
+ {
1288
+ key: "wrapModeV",
1289
+ set: /**
1290
+ * Wrapping mode for texture coordinate T.
1291
+ */ function set(value) {
1292
+ this._bind();
1293
+ this._setWrapMode(value, this._gl.TEXTURE_WRAP_T);
1294
+ }
1295
+ },
1296
+ {
1297
+ key: "filterMode",
1298
+ set: /**
1299
+ * Filter mode for texture.
1300
+ */ function set(value) {
1301
+ var gl = this._gl;
1302
+ var target = this._target;
1303
+ /** @ts-ignore */ var _mipmap = this._texture._mipmap;
1304
+ this._bind();
1305
+ switch(value){
1306
+ case TextureFilterMode.Point:
1307
+ gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
1308
+ gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, _mipmap ? gl.NEAREST_MIPMAP_NEAREST : gl.NEAREST);
1309
+ break;
1310
+ case TextureFilterMode.Bilinear:
1311
+ gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
1312
+ gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, _mipmap ? gl.LINEAR_MIPMAP_NEAREST : gl.LINEAR);
1313
+ break;
1314
+ case TextureFilterMode.Trilinear:
1315
+ gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
1316
+ gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, _mipmap ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
1317
+ break;
1318
+ }
1319
+ }
1320
+ },
1321
+ {
1322
+ key: "anisoLevel",
1323
+ set: /**
1324
+ * Anisotropic level for texture.
1325
+ */ function set(value) {
1326
+ var gl = this._gl;
1327
+ this._bind();
1328
+ gl.texParameterf(this._target, gl.TEXTURE_MAX_ANISOTROPY_EXT, value);
1329
+ }
1330
+ },
1331
+ {
1332
+ key: "depthCompareFunction",
1333
+ set: function set(value) {
1334
+ this._bind();
1335
+ var gl = this._gl;
1336
+ switch(value){
1337
+ case TextureDepthCompareFunction.Never:
1338
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.NEVER);
1339
+ break;
1340
+ case TextureDepthCompareFunction.Less:
1341
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.LESS);
1342
+ break;
1343
+ case TextureDepthCompareFunction.Equal:
1344
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.EQUAL);
1345
+ break;
1346
+ case TextureDepthCompareFunction.LessEqual:
1347
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.LEQUAL);
1348
+ break;
1349
+ case TextureDepthCompareFunction.Greater:
1350
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.GREATER);
1351
+ break;
1352
+ case TextureDepthCompareFunction.NotEqual:
1353
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.NOTEQUAL);
1354
+ break;
1355
+ case TextureDepthCompareFunction.GreaterEqual:
1356
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.GEQUAL);
1357
+ break;
1358
+ case TextureDepthCompareFunction.Always:
1359
+ gl.texParameteri(this._target, gl.TEXTURE_COMPARE_FUNC, gl.ALWAYS);
1360
+ break;
1361
+ }
1362
+ }
1363
+ }
1364
+ ]);
1365
+ return GLTexture;
1366
+ }();
1367
+
1368
+ /**
1369
+ * The render target in WebGL platform is used for off-screen rendering.
1370
+ */ var GLRenderTarget = /*#__PURE__*/ function() {
1371
+ function GLRenderTarget(rhi, target) {
1372
+ this._MSAAColorRenderBuffers = [];
1373
+ this._curMipLevel = 0;
1374
+ this._curFaceIndex = undefined;
1375
+ this._gl = rhi.gl;
1376
+ this._isWebGL2 = rhi.isWebGL2;
1377
+ this._target = target;
1378
+ /** @ts-ignore */ var _colorTextures = target._colorTextures, _depth = target._depth, width = target.width, height = target.height;
1379
+ var isDepthTexture = _instanceof(_depth, Texture);
1380
+ /** todo
1381
+ * MRT + Cube + [,MSAA]
1382
+ * MRT + MSAA
1383
+ */ for(var i = 0, n = _colorTextures.length; i < n; i++){
1384
+ var _colorTextures_i = _colorTextures[i], format = _colorTextures_i.format, isSRGBColorSpace = _colorTextures_i.isSRGBColorSpace;
1385
+ if (!GLTexture._supportRenderBufferColorFormat(format, rhi)) {
1386
+ throw new Error("TextureFormat is not supported:" + TextureFormat[format] + " in RenderTarget");
1387
+ }
1388
+ if (isSRGBColorSpace && format === TextureFormat.R8G8B8) {
1389
+ throw new Error("If you want to use sRGB color space, only R8G8B8A8 format is supported in RenderTarget");
1390
+ }
1391
+ }
1392
+ if (!isDepthTexture && !GLTexture._supportRenderBufferDepthFormat(_depth, rhi)) {
1393
+ throw new Error("TextureFormat is not supported:" + TextureFormat[_depth] + " in RenderTarget");
1394
+ }
1395
+ if (_colorTextures.length > 1 && !rhi.canIUse(GLCapabilityType.drawBuffers)) {
1396
+ throw new Error("MRT is not supported");
1397
+ }
1398
+ if (_colorTextures.some(function(v) {
1399
+ return v.width !== width || v.height !== height;
1400
+ })) {
1401
+ throw new Error("ColorTexture's size must as same as RenderTarget");
1402
+ }
1403
+ if (isDepthTexture && (_depth.width !== width || _depth.height !== height)) {
1404
+ throw new Error("DepthTexture's size must as same as RenderTarget");
1405
+ }
1406
+ // todo: necessary to support MRT + Cube + [,MSAA] ?
1407
+ if (_colorTextures.length > 1 && _colorTextures.some(function(v) {
1408
+ return _instanceof(v, TextureCube);
1409
+ })) {
1410
+ throw new Error("MRT+Cube+[,MSAA] is not supported");
1411
+ }
1412
+ var maxAntiAliasing = rhi.capability.maxAntiAliasing;
1413
+ if (target.antiAliasing > maxAntiAliasing) {
1414
+ Logger.warn("MSAA antiAliasing exceeds the limit and is automatically downgraded to:" + maxAntiAliasing);
1415
+ /** @ts-ignore */ target._antiAliasing = maxAntiAliasing;
1416
+ }
1417
+ this._frameBuffer = this._gl.createFramebuffer();
1418
+ // bind main FBO
1419
+ this._bindMainFBO();
1420
+ // bind MSAA FBO
1421
+ if (target.antiAliasing > 1) {
1422
+ this._MSAAFrameBuffer = this._gl.createFramebuffer();
1423
+ this._bindMSAAFBO();
1424
+ }
1425
+ }
1426
+ var _proto = GLRenderTarget.prototype;
1427
+ /**
1428
+ * Set which face and mipLevel of the cube texture to render to.
1429
+ * @param mipLevel - Set mip level the data want to write
1430
+ * @param faceIndex - Cube texture face
1431
+ */ _proto.activeRenderTarget = function activeRenderTarget(mipLevel, faceIndex) {
1432
+ // @todo: support MRT
1433
+ var _this = this, gl = _this._gl, target = _this._target;
1434
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
1435
+ var mipChanged = mipLevel !== this._curMipLevel;
1436
+ var faceChanged = faceIndex !== this._curFaceIndex;
1437
+ var colorTexture = target.getColorTexture(0);
1438
+ if (colorTexture) {
1439
+ var isCube = _instanceof(colorTexture, TextureCube);
1440
+ if (mipChanged || isCube && faceChanged) {
1441
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, isCube ? gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex : gl.TEXTURE_2D, // @ts-ignore
1442
+ colorTexture._platformTexture._glTexture, mipLevel);
1443
+ }
1444
+ }
1445
+ var depthTexture = target.depthTexture;
1446
+ if (depthTexture) {
1447
+ var isCube1 = _instanceof(depthTexture, TextureCube);
1448
+ if (mipChanged || isCube1) {
1449
+ // @ts-ignore
1450
+ var platformTexture = depthTexture._platformTexture;
1451
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, platformTexture._formatDetail.attachment, isCube1 ? gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex : gl.TEXTURE_2D, platformTexture._glTexture, mipLevel);
1452
+ }
1453
+ } else {
1454
+ if (mipChanged) {
1455
+ // @ts-ignore
1456
+ var internalFormat = GLTexture._getRenderBufferDepthFormatDetail(target._depth, gl, this._isWebGL2).internalFormat;
1457
+ gl.bindRenderbuffer(gl.RENDERBUFFER, this._depthRenderBuffer);
1458
+ gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, target.width >> mipLevel, target.height >> mipLevel);
1459
+ }
1460
+ }
1461
+ this._curMipLevel = mipLevel;
1462
+ this._curFaceIndex = faceIndex;
1463
+ if (this._MSAAFrameBuffer) {
1464
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._MSAAFrameBuffer);
1465
+ }
1466
+ };
1467
+ /**
1468
+ * Blit FBO.
1469
+ */ _proto.blitRenderTarget = function blitRenderTarget() {
1470
+ if (!this._MSAAFrameBuffer) return;
1471
+ var gl = this._gl;
1472
+ var mask = gl.COLOR_BUFFER_BIT | (this._target.depthTexture ? gl.DEPTH_BUFFER_BIT : 0);
1473
+ var _this__target = this._target, colorTextureCount = _this__target.colorTextureCount, width = _this__target.width, height = _this__target.height;
1474
+ gl.bindFramebuffer(gl.READ_FRAMEBUFFER, this._MSAAFrameBuffer);
1475
+ gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, this._frameBuffer);
1476
+ for(var textureIndex = 0; textureIndex < colorTextureCount; textureIndex++){
1477
+ var attachment = gl.COLOR_ATTACHMENT0 + textureIndex;
1478
+ this._blitDrawBuffers[textureIndex] = attachment;
1479
+ gl.readBuffer(attachment);
1480
+ gl.drawBuffers(this._blitDrawBuffers);
1481
+ gl.blitFramebuffer(0, 0, width, height, 0, 0, width, height, mask, gl.NEAREST);
1482
+ this._blitDrawBuffers[textureIndex] = gl.NONE;
1483
+ }
1484
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1485
+ };
1486
+ /**
1487
+ * Destroy render target.
1488
+ */ _proto.destroy = function destroy() {
1489
+ var gl = this._gl;
1490
+ this._frameBuffer && gl.deleteFramebuffer(this._frameBuffer);
1491
+ this._depthRenderBuffer && gl.deleteRenderbuffer(this._depthRenderBuffer);
1492
+ this._MSAAFrameBuffer && gl.deleteFramebuffer(this._MSAAFrameBuffer);
1493
+ this._MSAADepthRenderBuffer && gl.deleteRenderbuffer(this._MSAADepthRenderBuffer);
1494
+ for(var i = 0; i < this._MSAAColorRenderBuffers.length; i++){
1495
+ gl.deleteRenderbuffer(this._MSAAColorRenderBuffers[i]);
1496
+ }
1497
+ this._frameBuffer = null;
1498
+ this._depthRenderBuffer = null;
1499
+ this._MSAAFrameBuffer = null;
1500
+ this._MSAAColorRenderBuffers.length = 0;
1501
+ this._MSAADepthRenderBuffer = null;
1502
+ };
1503
+ _proto._bindMainFBO = function _bindMainFBO() {
1504
+ var gl = this._gl;
1505
+ var isWebGL2 = this._isWebGL2;
1506
+ /** @ts-ignore */ var _this__target = this._target, _depth = _this__target._depth, colorTextureCount = _this__target.colorTextureCount, width = _this__target.width, height = _this__target.height;
1507
+ var drawBuffers = new Array(colorTextureCount);
1508
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
1509
+ /** color render buffer */ for(var i = 0; i < colorTextureCount; i++){
1510
+ var colorTexture = this._target.getColorTexture(i);
1511
+ var attachment = gl.COLOR_ATTACHMENT0 + i;
1512
+ drawBuffers[i] = attachment;
1513
+ if (!_instanceof(colorTexture, TextureCube)) {
1514
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, attachment, gl.TEXTURE_2D, /** @ts-ignore */ colorTexture._platformTexture._glTexture, 0);
1515
+ }
1516
+ }
1517
+ if (colorTextureCount > 1) {
1518
+ gl.drawBuffers(drawBuffers);
1519
+ }
1520
+ this._oriDrawBuffers = drawBuffers;
1521
+ /** depth render buffer */ if (_depth !== null) {
1522
+ if (_instanceof(_depth, Texture) && !_instanceof(_depth, TextureCube)) {
1523
+ // @ts-ignore
1524
+ var platformTexture = _depth._platformTexture;
1525
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, platformTexture._formatDetail.attachment, gl.TEXTURE_2D, platformTexture._glTexture, 0);
1526
+ } else if (this._target.antiAliasing <= 1) {
1527
+ var _GLTexture__getRenderBufferDepthFormatDetail = GLTexture._getRenderBufferDepthFormatDetail(_depth, gl, isWebGL2), internalFormat = _GLTexture__getRenderBufferDepthFormatDetail.internalFormat, attachment1 = _GLTexture__getRenderBufferDepthFormatDetail.attachment;
1528
+ var depthRenderBuffer = gl.createRenderbuffer();
1529
+ this._depthRenderBuffer = depthRenderBuffer;
1530
+ gl.bindRenderbuffer(gl.RENDERBUFFER, depthRenderBuffer);
1531
+ gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, width, height);
1532
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment1, gl.RENDERBUFFER, depthRenderBuffer);
1533
+ }
1534
+ }
1535
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1536
+ gl.bindRenderbuffer(gl.RENDERBUFFER, null);
1537
+ };
1538
+ _proto._bindMSAAFBO = function _bindMSAAFBO() {
1539
+ var gl = this._gl;
1540
+ var isWebGL2 = this._isWebGL2;
1541
+ var MSAADepthRenderBuffer = gl.createRenderbuffer();
1542
+ /** @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;
1543
+ this._blitDrawBuffers = new Array(colorTextureCount);
1544
+ this._MSAADepthRenderBuffer = MSAADepthRenderBuffer;
1545
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._MSAAFrameBuffer);
1546
+ // prepare MRT+MSAA color RBOs
1547
+ for(var i = 0; i < colorTextureCount; i++){
1548
+ var MSAAColorRenderBuffer = gl.createRenderbuffer();
1549
+ this._MSAAColorRenderBuffers[i] = MSAAColorRenderBuffer;
1550
+ this._blitDrawBuffers[i] = gl.NONE;
1551
+ gl.bindRenderbuffer(gl.RENDERBUFFER, MSAAColorRenderBuffer);
1552
+ gl.renderbufferStorageMultisample(gl.RENDERBUFFER, antiAliasing, /** @ts-ignore */ this._target.getColorTexture(i)._platformTexture._formatDetail.internalFormat, width, height);
1553
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.RENDERBUFFER, MSAAColorRenderBuffer);
1554
+ }
1555
+ gl.drawBuffers(this._oriDrawBuffers);
1556
+ // prepare MSAA depth RBO
1557
+ if (_depth !== null) {
1558
+ var _ref = _instanceof(_depth, Texture) ? /** @ts-ignore */ _depth._platformTexture._formatDetail : GLTexture._getRenderBufferDepthFormatDetail(_depth, gl, isWebGL2), internalFormat = _ref.internalFormat, attachment = _ref.attachment;
1559
+ gl.bindRenderbuffer(gl.RENDERBUFFER, MSAADepthRenderBuffer);
1560
+ gl.renderbufferStorageMultisample(gl.RENDERBUFFER, antiAliasing, internalFormat, width, height);
1561
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, MSAADepthRenderBuffer);
1562
+ }
1563
+ this._checkFrameBuffer();
1564
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1565
+ gl.bindRenderbuffer(gl.RENDERBUFFER, null);
1566
+ };
1567
+ _proto._checkFrameBuffer = function _checkFrameBuffer() {
1568
+ var gl = this._gl;
1569
+ var e = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
1570
+ switch(e){
1571
+ case gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
1572
+ throw new Error("The attachment types are mismatched or not all framebuffer attachment points are framebuffer attachment complete");
1573
+ case gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
1574
+ throw new Error("There is no attachment");
1575
+ case gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
1576
+ throw new Error(" Height and width of the attachment are not the same.");
1577
+ case gl.FRAMEBUFFER_UNSUPPORTED:
1578
+ // #5.14.3 Event Types in https://registry.khronos.org/webgl/specs/1.0.0/
1579
+ if (!gl.isContextLost()) {
1580
+ throw new Error("The format of the attachment is not supported or if depth and stencil attachments are not the same renderbuffer");
1581
+ }
1582
+ break;
1583
+ case gl.FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
1584
+ 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.");
1585
+ }
1586
+ };
1587
+ return GLRenderTarget;
1588
+ }();
1589
+
1590
+ /**
1591
+ * Texture 2d in WebGL platform.
1592
+ */ var GLTexture2D = /*#__PURE__*/ function(GLTexture1) {
1593
+ _inherits(GLTexture2D, GLTexture1);
1594
+ function GLTexture2D(rhi, texture2D) {
1595
+ var _this;
1596
+ _this = GLTexture1.call(this, rhi, texture2D, rhi.gl.TEXTURE_2D) || this, /** Backward compatible with WebGL1.0. */ _this._compressedMipFilled = 0;
1597
+ _this._validate(texture2D, rhi);
1598
+ var format = texture2D.format, isSRGBColorSpace = texture2D.isSRGBColorSpace;
1599
+ var isWebGL2 = _this._isWebGL2;
1600
+ _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl, isWebGL2);
1601
+ _this._formatDetail.isCompressed && !isWebGL2 || _this._init(false);
1602
+ return _this;
1603
+ }
1604
+ var _proto = GLTexture2D.prototype;
1605
+ /**
1606
+ * {@inheritDoc IPlatformTexture2D.setPixelBuffer}
1607
+ */ _proto.setPixelBuffer = function setPixelBuffer(colorBuffer, mipLevel, x, y, width, height) {
1608
+ if (mipLevel === void 0) mipLevel = 0;
1609
+ var gl = this._gl;
1610
+ var formatDetail = this._formatDetail;
1611
+ var mipWidth = Math.max(1, this._texture.width >> mipLevel);
1612
+ var mipHeight = Math.max(1, this._texture.height >> mipLevel);
1613
+ width = width || mipWidth - x;
1614
+ height = height || mipHeight - y;
1615
+ this._bind();
1616
+ if (formatDetail.isCompressed) {
1617
+ var isWebGL2 = this._isWebGL2;
1618
+ var internalFormat = formatDetail.internalFormat;
1619
+ var mipBit = 1 << mipLevel;
1620
+ if (isWebGL2 || this._compressedMipFilled & mipBit) {
1621
+ gl.compressedTexSubImage2D(this._target, mipLevel, x, y, width, height, internalFormat, colorBuffer);
1622
+ } else {
1623
+ gl.compressedTexImage2D(this._target, mipLevel, internalFormat, width, height, 0, colorBuffer);
1624
+ this._compressedMipFilled |= mipBit;
1625
+ }
1626
+ } else {
1627
+ var baseFormat = formatDetail.baseFormat, dataType = formatDetail.dataType;
1628
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1629
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1630
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1631
+ gl.texSubImage2D(this._target, mipLevel, x, y, width, height, baseFormat, dataType, colorBuffer);
1632
+ }
1633
+ };
1634
+ /**
1635
+ * {@inheritDoc IPlatformTexture2D.setImageSource}
1636
+ */ _proto.setImageSource = function setImageSource(imageSource, mipLevel, flipY, premultiplyAlpha, x, y) {
1637
+ var gl = this._gl;
1638
+ var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1639
+ this._bind();
1640
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, +flipY);
1641
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, +premultiplyAlpha);
1642
+ if (this._texture.usage === TextureUsage.Dynamic) {
1643
+ gl.texImage2D(this._target, mipLevel, internalFormat, baseFormat, dataType, imageSource);
1644
+ } else {
1645
+ gl.texSubImage2D(this._target, mipLevel, x || 0, y || 0, baseFormat, dataType, imageSource);
1646
+ }
1647
+ };
1648
+ /**
1649
+ * {@inheritDoc IPlatformTexture2D.getPixelBuffer }
1650
+ */ _proto.getPixelBuffer = function getPixelBuffer(x, y, width, height, mipLevel, out) {
1651
+ if (this._formatDetail.isCompressed) {
1652
+ throw new Error("Unable to read compressed texture");
1653
+ }
1654
+ GLTexture1.prototype._getPixelBuffer.call(this, null, x, y, width, height, mipLevel, out);
1655
+ };
1656
+ return GLTexture2D;
1657
+ }(GLTexture);
1658
+
1659
+ /**
1660
+ * Texture 2D array in WebGL platform.
1661
+ */ var GLTexture2DArray = /*#__PURE__*/ function(GLTexture1) {
1662
+ _inherits(GLTexture2DArray, GLTexture1);
1663
+ function GLTexture2DArray(rhi, texture2DArray) {
1664
+ var _this;
1665
+ _this = GLTexture1.call(this, rhi, texture2DArray, rhi.gl.TEXTURE_2D_ARRAY) || this;
1666
+ _this._validate(texture2DArray, rhi);
1667
+ var format = texture2DArray.format, width = texture2DArray.width, height = texture2DArray.height, length = texture2DArray.length, mipmapCount = texture2DArray.mipmapCount, isSRGBColorSpace = texture2DArray.isSRGBColorSpace;
1668
+ _this._bind();
1669
+ _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl, true);
1670
+ _this._gl.texStorage3D(_this._target, mipmapCount, _this._formatDetail.internalFormat, width, height, length);
1671
+ return _this;
1672
+ }
1673
+ var _proto = GLTexture2DArray.prototype;
1674
+ /**
1675
+ * {@inheritDoc IPlatformTexture2DArray.setPixelBuffer}
1676
+ */ _proto.setPixelBuffer = function setPixelBuffer(offsetIndex, colorBuffer, mipLevel, x, y, width, height, length) {
1677
+ var _this = this, target = _this._target, gl = _this._gl;
1678
+ var formatDetail = this._formatDetail;
1679
+ var internalFormat = formatDetail.internalFormat, baseFormat = formatDetail.baseFormat, dataType = formatDetail.dataType, isCompressed = formatDetail.isCompressed;
1680
+ width = width || Math.max(1, this._texture.width >> mipLevel) - x;
1681
+ height = height || Math.max(1, this._texture.height >> mipLevel) - y;
1682
+ length = length || this._texture.length;
1683
+ this._bind();
1684
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1685
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1686
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1687
+ if (isCompressed) {
1688
+ gl.compressedTexSubImage3D(target, mipLevel, x, y, offsetIndex, width, height, length, internalFormat, colorBuffer);
1689
+ } else {
1690
+ gl.texSubImage3D(target, mipLevel, x, y, offsetIndex, width, height, length, baseFormat, dataType, colorBuffer);
1691
+ }
1692
+ };
1693
+ /**
1694
+ * {@inheritDoc IPlatformTexture2DArray.setImageSource}
1695
+ */ _proto.setImageSource = function setImageSource(elementIndex, imageSource, mipLevel, flipY, premultiplyAlpha, x, y) {
1696
+ var gl = this._gl;
1697
+ var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1698
+ this._bind();
1699
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, +flipY);
1700
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, +premultiplyAlpha);
1701
+ var _imageSource_width, _imageSource_height;
1702
+ gl.texSubImage3D(this._target, mipLevel, x, y, elementIndex, (_imageSource_width = imageSource.width) != null ? _imageSource_width : imageSource.codedWidth, (_imageSource_height = imageSource.height) != null ? _imageSource_height : imageSource.codedHeight, 1, baseFormat, dataType, imageSource);
1703
+ };
1704
+ /**
1705
+ * {@inheritDoc IPlatformTexture2DArray.getPixelBuffer}
1706
+ */ _proto.getPixelBuffer = function getPixelBuffer(elementIndex, x, y, width, height, mipLevel, out) {
1707
+ var _this = this, gl = _this._gl, formatDetail = _this._formatDetail;
1708
+ if (formatDetail.isCompressed) {
1709
+ throw new Error("Unable to read compressed texture");
1710
+ }
1711
+ gl.pixelStorei(gl.PACK_ALIGNMENT, formatDetail.alignment);
1712
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._getReadFrameBuffer());
1713
+ gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, this._glTexture, mipLevel, elementIndex);
1714
+ gl.readPixels(x, y, width, height, formatDetail.baseFormat, formatDetail.dataType, out);
1715
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1716
+ };
1717
+ _proto._validate = function _validate(texture, rhi) {
1718
+ var format = texture.format;
1719
+ // Validate sRGB format
1720
+ // @ts-ignore
1721
+ var isSRGBColorSpace = texture._isSRGBColorSpace;
1722
+ if (isSRGBColorSpace && !TextureUtils.supportSRGB(format)) {
1723
+ Logger.warn("Only support sRGB color space in RGB8 or RGBA8 or some compressed texture format");
1724
+ // @ts-ignore
1725
+ texture._isSRGBColorSpace = false;
1726
+ }
1727
+ };
1728
+ return GLTexture2DArray;
1729
+ }(GLTexture);
1730
+
1731
+ /**
1732
+ * Cube texture in WebGL platform.
1733
+ */ var GLTextureCube = /*#__PURE__*/ function(GLTexture1) {
1734
+ _inherits(GLTextureCube, GLTexture1);
1735
+ function GLTextureCube(rhi, textureCube) {
1736
+ var _this;
1737
+ _this = GLTexture1.call(this, rhi, textureCube, rhi.gl.TEXTURE_CUBE_MAP) || this, /** Backward compatible with WebGL1.0. */ _this._compressedFaceFilled = [
1738
+ 0,
1739
+ 0,
1740
+ 0,
1741
+ 0,
1742
+ 0,
1743
+ 0
1744
+ ];
1745
+ _this._validate(textureCube, rhi);
1746
+ var format = textureCube.format, isSRGBColorSpace = textureCube.isSRGBColorSpace;
1747
+ var isWebGL2 = _this._isWebGL2;
1748
+ _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl, isWebGL2);
1749
+ _this._formatDetail.isCompressed && !isWebGL2 || _this._init(true);
1750
+ return _this;
1751
+ }
1752
+ var _proto = GLTextureCube.prototype;
1753
+ /**
1754
+ * {@inheritDoc IPlatformTextureCube.setPixelBuffer}
1755
+ */ _proto.setPixelBuffer = function setPixelBuffer(face, colorBuffer, mipLevel, x, y, width, height) {
1756
+ var gl = this._gl;
1757
+ var isWebGL2 = this._isWebGL2;
1758
+ var formatDetail = this._formatDetail;
1759
+ var internalFormat = formatDetail.internalFormat, baseFormat = formatDetail.baseFormat, dataType = formatDetail.dataType, isCompressed = formatDetail.isCompressed;
1760
+ var mipSize = Math.max(1, this._texture.width >> mipLevel);
1761
+ width = width || mipSize - x;
1762
+ height = height || mipSize - y;
1763
+ this._bind();
1764
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1765
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1766
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1767
+ if (isCompressed) {
1768
+ var mipBit = 1 << mipLevel;
1769
+ if (isWebGL2 || this._compressedFaceFilled[face] & mipBit) {
1770
+ gl.compressedTexSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, x, y, width, height, internalFormat, colorBuffer);
1771
+ } else {
1772
+ gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, internalFormat, width, height, 0, colorBuffer);
1773
+ this._compressedFaceFilled[face] |= mipBit;
1774
+ }
1775
+ } else {
1776
+ gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, x, y, width, height, baseFormat, dataType, colorBuffer);
1777
+ }
1778
+ };
1779
+ /**
1780
+ * {@inheritDoc IPlatformTextureCube.setImageSource}
1781
+ */ _proto.setImageSource = function setImageSource(face, imageSource, mipLevel, flipY, premultiplyAlpha, x, y) {
1782
+ var gl = this._gl;
1783
+ var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1784
+ this._bind();
1785
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, +flipY);
1786
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, +premultiplyAlpha);
1787
+ gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, x || 0, y || 0, baseFormat, dataType, imageSource);
1788
+ };
1789
+ /**
1790
+ * {@inheritDoc IPlatformTextureCube.getPixelBuffer}
1791
+ */ _proto.getPixelBuffer = function getPixelBuffer(face, x, y, width, height, mipLevel, out) {
1792
+ if (this._formatDetail.isCompressed) {
1793
+ throw new Error("Unable to read compressed texture");
1794
+ }
1795
+ GLTexture1.prototype._getPixelBuffer.call(this, face, x, y, width, height, mipLevel, out);
1796
+ };
1797
+ return GLTextureCube;
1798
+ }(GLTexture);
1799
+
1800
+ /**
1801
+ * WebGL mode.
1802
+ */ var WebGLMode = /*#__PURE__*/ function(WebGLMode) {
1803
+ /** Auto, use WebGL2.0 if support, or will fallback to WebGL1.0. */ WebGLMode[WebGLMode["Auto"] = 0] = "Auto";
1804
+ /** WebGL2.0. */ WebGLMode[WebGLMode["WebGL2"] = 1] = "WebGL2";
1805
+ /** WebGL1.0, */ WebGLMode[WebGLMode["WebGL1"] = 2] = "WebGL1";
1806
+ return WebGLMode;
1807
+ }({});
1808
+ /**
1809
+ * WebGL graphic device, including WebGL1.0 and WebGL2.0.
1810
+ */ var WebGLGraphicDevice = /*#__PURE__*/ function() {
1811
+ function WebGLGraphicDevice(initializeOptions) {
1812
+ if (initializeOptions === void 0) initializeOptions = {};
1813
+ /** @internal */ this._readFrameBuffer = null;
1814
+ /** @internal */ this._mainFrameBuffer = null;
1815
+ /** @internal */ this._mainFrameWidth = 0;
1816
+ /** @internal */ this._mainFrameHeight = 0;
1817
+ /** @internal */ this._enableGlobalDepthBias = false;
1818
+ this._activeTextures = new Array(32);
1819
+ this._lastViewport = new Vector4(null, null, null, null);
1820
+ this._lastScissor = new Vector4(null, null, null, null);
1821
+ this._lastClearColor = new Color(null, null, null, null);
1822
+ this._scissorEnable = false;
1823
+ var options = _extends({
1824
+ webGLMode: 0,
1825
+ _forceFlush: false,
1826
+ _maxAllowSkinUniformVectorCount: 256,
1827
+ alpha: true,
1828
+ depth: true,
1829
+ stencil: true,
1830
+ failIfMajorPerformanceCaveat: false,
1831
+ powerPreference: "default",
1832
+ premultipliedAlpha: true,
1833
+ preserveDrawingBuffer: false,
1834
+ desynchronized: false,
1835
+ xrCompatible: false
1836
+ }, initializeOptions);
1837
+ if (SystemInfo.platform === Platform.IPhone || SystemInfo.platform === Platform.IPad) {
1838
+ var version = SystemInfo.operatingSystem.match(/(\d+).?(\d+)?.?(\d+)?/);
1839
+ if (version) {
1840
+ var majorVersion = parseInt(version[1]);
1841
+ var minorVersion = parseInt(version[2]);
1842
+ if (majorVersion === 15 && minorVersion >= 0 && minorVersion <= 4) {
1843
+ options._forceFlush = true;
1844
+ }
1845
+ }
1846
+ }
1847
+ this._options = options;
1848
+ // Force disable stencil, antialias and depth, we configure them in internal render target
1849
+ this._webGLOptions = {
1850
+ antialias: false,
1851
+ depth: false,
1852
+ stencil: false,
1853
+ alpha: options.alpha,
1854
+ failIfMajorPerformanceCaveat: options.failIfMajorPerformanceCaveat,
1855
+ powerPreference: options.powerPreference,
1856
+ premultipliedAlpha: options.premultipliedAlpha,
1857
+ preserveDrawingBuffer: options.preserveDrawingBuffer,
1858
+ desynchronized: options.desynchronized,
1859
+ xrCompatible: options.xrCompatible
1860
+ };
1861
+ this._onWebGLContextLost = this._onWebGLContextLost.bind(this);
1862
+ this._onWebGLContextRestored = this._onWebGLContextRestored.bind(this);
1863
+ }
1864
+ var _proto = WebGLGraphicDevice.prototype;
1865
+ _proto.init = function init(canvas, onDeviceLost, onDeviceRestored) {
1866
+ var webCanvas = canvas._webCanvas;
1867
+ var webGLMode = this._options.webGLMode;
1868
+ this._onDeviceLost = onDeviceLost;
1869
+ this._onDeviceRestored = onDeviceRestored;
1870
+ webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1871
+ webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1872
+ webCanvas.addEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1873
+ this._webCanvas = webCanvas;
1874
+ var webGLOptions = this._webGLOptions;
1875
+ var gl;
1876
+ if (webGLMode == 0 || webGLMode == 1) {
1877
+ gl = webCanvas.getContext("webgl2", webGLOptions);
1878
+ if (!gl && (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas))) {
1879
+ gl = webCanvas.getContext("experimental-webgl2", webGLOptions);
1880
+ }
1881
+ this._isWebGL2 = true;
1882
+ // Prevent weird browsers to lie (such as safari!)ƒ
1883
+ if (gl && !gl.deleteQuery) {
1884
+ this._isWebGL2 = false;
1885
+ }
1886
+ }
1887
+ if (!gl) {
1888
+ if (webGLMode == 0 || webGLMode == 2) {
1889
+ gl = webCanvas.getContext("webgl", webGLOptions);
1890
+ if (!gl && (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas))) {
1891
+ gl = webCanvas.getContext("experimental-webgl", webGLOptions);
1892
+ }
1893
+ this._isWebGL2 = false;
1894
+ }
1895
+ }
1896
+ if (!gl) {
1897
+ throw new Error("Get GL Context FAILED.");
1898
+ }
1899
+ this._gl = gl;
1900
+ this._initGLState(gl);
1901
+ };
1902
+ _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1903
+ return new GLPrimitive(this, primitive);
1904
+ };
1905
+ _proto.createPlatformTexture2D = function createPlatformTexture2D(texture2D) {
1906
+ return new GLTexture2D(this, texture2D);
1907
+ };
1908
+ _proto.createPlatformTexture2DArray = function createPlatformTexture2DArray(texture2D) {
1909
+ return new GLTexture2DArray(this, texture2D);
1910
+ };
1911
+ _proto.createPlatformTextureCube = function createPlatformTextureCube(textureCube) {
1912
+ return new GLTextureCube(this, textureCube);
1913
+ };
1914
+ _proto.createPlatformRenderTarget = function createPlatformRenderTarget(target) {
1915
+ return new GLRenderTarget(this, target);
1916
+ };
1917
+ _proto.createPlatformBuffer = function createPlatformBuffer(type, byteLength, bufferUsage, data) {
1918
+ if (bufferUsage === void 0) bufferUsage = BufferUsage.Static;
1919
+ return new GLBuffer(this, type, byteLength, bufferUsage, data);
1920
+ };
1921
+ _proto.requireExtension = function requireExtension(ext) {
1922
+ return this._extensions.requireExtension(ext);
1923
+ };
1924
+ _proto.canIUse = function canIUse(capabilityType) {
1925
+ return this.capability.canIUse(capabilityType);
1926
+ };
1927
+ _proto.canIUseCompressedTextureInternalFormat = function canIUseCompressedTextureInternalFormat(type) {
1928
+ return this.capability.canIUseCompressedTextureInternalFormat(type);
1929
+ };
1930
+ _proto.viewport = function viewport(x, y, width, height) {
1931
+ var _this = this, gl = _this._gl, lastViewport = _this._lastViewport;
1932
+ if (x !== lastViewport.x || y !== lastViewport.y || width !== lastViewport.z || height !== lastViewport.w) {
1933
+ gl.viewport(x, y, width, height);
1934
+ lastViewport.set(x, y, width, height);
1935
+ }
1936
+ };
1937
+ _proto.scissor = function scissor(x, y, width, height) {
1938
+ var _this = this, gl = _this._gl, lastScissor = _this._lastScissor;
1939
+ if (x !== lastScissor.x || y !== lastScissor.y || width !== lastScissor.z || height !== lastScissor.w) {
1940
+ var _this1 = this, webCanvas = _this1._webCanvas;
1941
+ if (x === 0 && y === 0 && width === webCanvas.width && height === webCanvas.height) {
1942
+ if (this._scissorEnable) {
1943
+ gl.disable(gl.SCISSOR_TEST);
1944
+ this._scissorEnable = false;
1945
+ }
1946
+ } else {
1947
+ if (!this._scissorEnable) {
1948
+ gl.enable(gl.SCISSOR_TEST);
1949
+ this._scissorEnable = true;
1950
+ }
1951
+ gl.scissor(x, y, width, height);
1952
+ }
1953
+ lastScissor.set(x, y, width, height);
1954
+ }
1955
+ };
1956
+ _proto.colorMask = function colorMask(r, g, b, a) {
1957
+ this._gl.colorMask(r, g, b, a);
1958
+ };
1959
+ _proto.clearRenderTarget = function clearRenderTarget(engine, clearFlags, clearColor) {
1960
+ var gl = this._gl;
1961
+ var _engine__lastRenderState = engine._lastRenderState, targetBlendState = _engine__lastRenderState.blendState.targetBlendState, depthState = _engine__lastRenderState.depthState, stencilState = _engine__lastRenderState.stencilState;
1962
+ var clearFlag = 0;
1963
+ if (clearFlags & CameraClearFlags.Color && clearColor) {
1964
+ clearFlag |= gl.COLOR_BUFFER_BIT;
1965
+ var lc = this._lastClearColor;
1966
+ var r = clearColor.r, g = clearColor.g, b = clearColor.b, a = clearColor.a;
1967
+ if (clearColor && (r !== lc.r || g !== lc.g || b !== lc.b || a !== lc.a)) {
1968
+ gl.clearColor(r, g, b, a);
1969
+ lc.set(r, g, b, a);
1970
+ }
1971
+ if (targetBlendState.colorWriteMask !== ColorWriteMask.All) {
1972
+ gl.colorMask(true, true, true, true);
1973
+ targetBlendState.colorWriteMask = ColorWriteMask.All;
1974
+ }
1975
+ }
1976
+ if (clearFlags & CameraClearFlags.Depth) {
1977
+ clearFlag |= gl.DEPTH_BUFFER_BIT;
1978
+ if (depthState.writeEnabled !== true) {
1979
+ gl.depthMask(true);
1980
+ depthState.writeEnabled = true;
1981
+ }
1982
+ }
1983
+ if (clearFlags & CameraClearFlags.Stencil) {
1984
+ clearFlag |= gl.STENCIL_BUFFER_BIT;
1985
+ if (stencilState.writeMask !== 0xff) {
1986
+ gl.stencilMask(0xff);
1987
+ stencilState.writeMask = 0xff;
1988
+ }
1989
+ }
1990
+ gl.clear(clearFlag);
1991
+ };
1992
+ _proto.drawPrimitive = function drawPrimitive(primitive, subPrimitive, shaderProgram) {
1993
+ // todo: VAO not support morph animation
1994
+ if (primitive) {
1995
+ primitive.draw(shaderProgram, subPrimitive);
1996
+ } else {
1997
+ Logger.error("draw primitive failed.");
1998
+ }
1999
+ };
2000
+ _proto.getMainFrameBufferWidth = function getMainFrameBufferWidth() {
2001
+ return this._mainFrameWidth || this._gl.drawingBufferWidth;
2002
+ };
2003
+ _proto.getMainFrameBufferHeight = function getMainFrameBufferHeight() {
2004
+ return this._mainFrameHeight || this._gl.drawingBufferHeight;
2005
+ };
2006
+ _proto.activeRenderTarget = function activeRenderTarget(renderTarget, viewport, isFlipProjection, mipLevel, faceIndex) {
2007
+ var bufferWidth, bufferHeight;
2008
+ if (renderTarget) {
2009
+ /** @ts-ignore */ renderTarget._isContentLost = false;
2010
+ /** @ts-ignore */ var platformRenderTarget = renderTarget._platformRenderTarget;
2011
+ platformRenderTarget.activeRenderTarget(mipLevel, faceIndex);
2012
+ bufferWidth = renderTarget.width >> mipLevel;
2013
+ bufferHeight = renderTarget.height >> mipLevel;
2014
+ } else {
2015
+ var gl = this._gl;
2016
+ gl.bindFramebuffer(gl.FRAMEBUFFER, this._mainFrameBuffer);
2017
+ bufferWidth = this.getMainFrameBufferWidth();
2018
+ bufferHeight = this.getMainFrameBufferHeight();
2019
+ }
2020
+ var width = bufferWidth * viewport.z;
2021
+ var height = bufferHeight * viewport.w;
2022
+ var x = viewport.x * bufferWidth;
2023
+ var y = isFlipProjection ? viewport.y * bufferHeight : bufferHeight - viewport.y * bufferHeight - height;
2024
+ this.viewport(x, y, width, height);
2025
+ this.scissor(x, y, width, height);
2026
+ };
2027
+ _proto.blitInternalRTByBlitFrameBuffer = function blitInternalRTByBlitFrameBuffer(srcRT, destRT, clearFlags, viewport) {
2028
+ if (!this._isWebGL2) {
2029
+ Logger.warn("WebGL1.0 not support blit frame buffer.");
2030
+ return;
2031
+ }
2032
+ var gl = this._gl;
2033
+ // @ts-ignore
2034
+ var srcFrameBuffer = srcRT ? srcRT._platformRenderTarget._frameBuffer : null;
2035
+ // @ts-ignore
2036
+ var destFrameBuffer = destRT ? destRT._platformRenderTarget._frameBuffer : null;
2037
+ var bufferWidth = this.getMainFrameBufferWidth();
2038
+ var bufferHeight = this.getMainFrameBufferHeight();
2039
+ var srcWidth = srcRT ? srcRT.width : bufferWidth;
2040
+ var srcHeight = srcRT ? srcRT.height : bufferHeight;
2041
+ var blitWidth = destRT.width;
2042
+ var blitHeight = destRT.height;
2043
+ var needFlipY = !srcRT;
2044
+ var needBlitColor = (clearFlags & CameraClearFlags.Color) === 0;
2045
+ var needBlitDepth = (clearFlags & CameraClearFlags.Depth) === 0;
2046
+ var needBlitStencil = (clearFlags & CameraClearFlags.Stencil) === 0;
2047
+ gl.bindFramebuffer(gl.READ_FRAMEBUFFER, srcFrameBuffer);
2048
+ gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, destFrameBuffer);
2049
+ var blitMask = needBlitColor ? gl.COLOR_BUFFER_BIT : 0;
2050
+ if (needBlitDepth || needBlitStencil) {
2051
+ // @ts-ignore
2052
+ var depthFormat = destRT._depthFormat;
2053
+ if (needBlitDepth) {
2054
+ if (depthFormat === TextureFormat.Depth || depthFormat >= TextureFormat.DepthStencil && depthFormat <= TextureFormat.Depth32Stencil8) {
2055
+ blitMask |= gl.DEPTH_BUFFER_BIT;
2056
+ } else {
2057
+ Logger.warn("Do not clear depth, or set depth format of target which is " + TextureFormat[depthFormat] + " now.");
2058
+ }
2059
+ }
2060
+ if (needBlitStencil) {
2061
+ if (depthFormat === TextureFormat.Stencil || depthFormat === TextureFormat.DepthStencil || depthFormat >= TextureFormat.Depth24Stencil8 || depthFormat >= TextureFormat.Depth32Stencil8) {
2062
+ blitMask |= gl.STENCIL_BUFFER_BIT;
2063
+ } else {
2064
+ Logger.warn("Do not clear stencil, or set stencil format of target which is " + TextureFormat[depthFormat] + " now.");
2065
+ }
2066
+ }
2067
+ }
2068
+ var xStart = viewport.x * srcWidth;
2069
+ var xEnd = xStart + blitWidth;
2070
+ var yStart = needFlipY ? srcHeight - viewport.y * srcHeight : srcHeight - viewport.y * srcHeight - blitHeight;
2071
+ var yEnd = needFlipY ? yStart - blitHeight : yStart + blitHeight;
2072
+ gl.blitFramebuffer(xStart, yStart, xEnd, yEnd, 0, 0, blitWidth, blitHeight, blitMask, gl.NEAREST);
2073
+ };
2074
+ _proto.copyRenderTargetToSubTexture = function copyRenderTargetToSubTexture(srcRT, grabTexture, viewport) {
2075
+ var gl = this._gl;
2076
+ var bufferWidth = this.getMainFrameBufferWidth();
2077
+ var bufferHeight = this.getMainFrameBufferHeight();
2078
+ var srcWidth = srcRT ? srcRT.width : bufferWidth;
2079
+ var srcHeight = srcRT ? srcRT.height : bufferHeight;
2080
+ var copyWidth = grabTexture.width;
2081
+ var copyHeight = grabTexture.height;
2082
+ var flipY = !srcRT;
2083
+ var xStart = viewport.x * srcWidth;
2084
+ var yStart = flipY ? srcHeight - viewport.y * srcHeight - copyHeight : viewport.y * srcHeight;
2085
+ var _srcRT__platformRenderTarget__frameBuffer;
2086
+ // @ts-ignore
2087
+ var frameBuffer = (_srcRT__platformRenderTarget__frameBuffer = srcRT == null ? void 0 : srcRT._platformRenderTarget._frameBuffer) != null ? _srcRT__platformRenderTarget__frameBuffer : this._mainFrameBuffer;
2088
+ // @ts-ignore
2089
+ gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
2090
+ // @ts-ignore
2091
+ var glTexture = grabTexture._platformTexture;
2092
+ glTexture._bind();
2093
+ gl.copyTexSubImage2D(glTexture._target, 0, 0, 0, xStart, yStart, copyWidth, copyHeight);
2094
+ };
2095
+ _proto.activeTexture = function activeTexture(textureID) {
2096
+ if (this._activeTextureID !== textureID) {
2097
+ this._gl.activeTexture(textureID);
2098
+ this._activeTextureID = textureID;
2099
+ }
2100
+ };
2101
+ _proto.bindTexture = function bindTexture(texture) {
2102
+ var index = this._activeTextureID - this._gl.TEXTURE0;
2103
+ if (this._activeTextures[index] !== texture) {
2104
+ this._gl.bindTexture(texture._target, texture._glTexture);
2105
+ this._activeTextures[index] = texture;
2106
+ }
2107
+ };
2108
+ _proto.setGlobalDepthBias = function setGlobalDepthBias(bias, slopeBias) {
2109
+ var gl = this._gl;
2110
+ var enable = bias !== 0 || slopeBias !== 0;
2111
+ if (enable) {
2112
+ gl.enable(gl.POLYGON_OFFSET_FILL);
2113
+ gl.polygonOffset(slopeBias, bias);
2114
+ } else {
2115
+ gl.disable(gl.POLYGON_OFFSET_FILL);
2116
+ }
2117
+ this._enableGlobalDepthBias = enable;
2118
+ };
2119
+ _proto.flush = function flush() {
2120
+ this._gl.flush();
2121
+ };
2122
+ _proto.forceLoseDevice = function forceLoseDevice() {
2123
+ var extension = this.requireExtension(GLCapabilityType.WEBGL_lose_context);
2124
+ extension.loseContext();
2125
+ };
2126
+ _proto.forceRestoreDevice = function forceRestoreDevice() {
2127
+ var extension = this.requireExtension(GLCapabilityType.WEBGL_lose_context);
2128
+ extension.restoreContext();
2129
+ };
2130
+ /**
2131
+ * @remarks
2132
+ * WebGL context loss and restore can happen at any GPU execution point. refs to: https://www.khronos.org/webgl/wiki/HandlingContextLost
2133
+ */ _proto.isContextLost = function isContextLost() {
2134
+ return this.gl.isContextLost();
2135
+ };
2136
+ _proto.resetState = function resetState() {
2137
+ this._readFrameBuffer = null;
2138
+ this._enableGlobalDepthBias = false;
2139
+ this._currentBindShaderProgram = null;
2140
+ var activeTextures = this._activeTextures;
2141
+ for(var i = 0, n = activeTextures.length; i < n; i++){
2142
+ activeTextures[i] = null;
2143
+ }
2144
+ this._lastViewport.set(null, null, null, null);
2145
+ this._lastScissor.set(null, null, null, null);
2146
+ this._lastClearColor.set(null, null, null, null);
2147
+ this._scissorEnable = false;
2148
+ this._initGLState(this._gl);
2149
+ };
2150
+ _proto._initGLState = function _initGLState(gl) {
2151
+ this._activeTextureID = gl.TEXTURE0;
2152
+ this._renderStates = new GLRenderStates(gl);
2153
+ this._extensions = new GLExtensions(this);
2154
+ this._capability = new GLCapability(this);
2155
+ // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
2156
+ gl.activeTexture(gl.TEXTURE0);
2157
+ var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
2158
+ if (debugRenderInfo != null) {
2159
+ this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
2160
+ }
2161
+ };
2162
+ _proto.destroy = function destroy() {
2163
+ var webCanvas = this._webCanvas;
2164
+ webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
2165
+ webCanvas.removeEventListener("webglcontextlost", this._onWebGLContextLost, false);
2166
+ webCanvas.removeEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
2167
+ };
2168
+ _proto._onContextCreationError = function _onContextCreationError(event) {
2169
+ console.error("WebGLRenderer: WebGL context could not be created. Reason: ", event.statusMessage);
2170
+ };
2171
+ _proto._onWebGLContextLost = function _onWebGLContextLost(event) {
2172
+ event.preventDefault();
2173
+ this._onDeviceLost();
2174
+ };
2175
+ _proto._onWebGLContextRestored = function _onWebGLContextRestored(event) {
2176
+ this._onDeviceRestored();
2177
+ };
2178
+ _create_class(WebGLGraphicDevice, [
2179
+ {
2180
+ key: "isWebGL2",
2181
+ get: function get() {
2182
+ return this._isWebGL2;
2183
+ }
2184
+ },
2185
+ {
2186
+ key: "renderer",
2187
+ get: function get() {
2188
+ return this._renderer;
2189
+ }
2190
+ },
2191
+ {
2192
+ key: "gl",
2193
+ get: /**
2194
+ * GL Context
2195
+ * @member {WebGLRenderingContext}
2196
+ */ function get() {
2197
+ return this._gl;
2198
+ }
2199
+ },
2200
+ {
2201
+ key: "renderStates",
2202
+ get: function get() {
2203
+ return this._renderStates;
2204
+ }
2205
+ },
2206
+ {
2207
+ key: "capability",
2208
+ get: function get() {
2209
+ return this._capability;
2210
+ }
2211
+ },
2212
+ {
2213
+ key: "canIUseMoreJoints",
2214
+ get: function get() {
2215
+ return this.capability.canIUseMoreJoints;
2216
+ }
2217
+ }
2218
+ ]);
2219
+ return WebGLGraphicDevice;
2220
+ }();
2221
+
2222
+ export { GLCompressedTextureInternalFormat, WebCanvas, WebGLEngine, WebGLGraphicDevice, WebGLMode };
2223
+ //# sourceMappingURL=module.js.map