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