@galacean/engine-rhi-webgl 2.0.0-alpha.6 → 2.0.0-alpha.61

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 CHANGED
@@ -5,9 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var engineCore = require('@galacean/engine-core');
6
6
  var engineMath = require('@galacean/engine-math');
7
7
 
8
- /**
9
- * Smoothing plug-in.
10
- * */ var GLCompressedTextureInternalFormat = /*#__PURE__*/ function(GLCompressedTextureInternalFormat) {
8
+ var GLCompressedTextureInternalFormat = /*#__PURE__*/ function(GLCompressedTextureInternalFormat) {
11
9
  // astc
12
10
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_4X4_KHR"] = 37808] = "RGBA_ASTC_4X4_KHR";
13
11
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_ASTC_5X4_KHR"] = 37809] = "RGBA_ASTC_5X4_KHR";
@@ -73,24 +71,6 @@ var engineMath = require('@galacean/engine-math');
73
71
  return GLCompressedTextureInternalFormat;
74
72
  }({});
75
73
 
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
74
  function _set_prototype_of(o, p) {
95
75
  _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
96
76
  o.__proto__ = p;
@@ -123,69 +103,134 @@ function _instanceof(left, right) {
123
103
  _inherits(WebCanvas, Canvas);
124
104
  function WebCanvas(webCanvas) {
125
105
  var _this;
126
- _this = Canvas.call(this) || this, _this._scale = new engineMath.Vector2();
127
- var width = webCanvas.width;
128
- var height = webCanvas.height;
106
+ _this = Canvas.call(this) || this, _this._autoResolutionScale = 1, _this._resolutionDirty = false, _this._pendingDevicePixelWidth = 0, _this._pendingDevicePixelHeight = 0;
129
107
  _this._webCanvas = webCanvas;
130
- _this.width = width;
131
- _this.height = height;
108
+ _this._setResolution(webCanvas.width, webCanvas.height);
132
109
  return _this;
133
110
  }
134
111
  var _proto = WebCanvas.prototype;
135
112
  /**
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;
113
+ * @inheritdoc
114
+ *
115
+ * @remarks
116
+ * The canvas must have a CSS-constrained display size; otherwise auto-resolution disables itself.
117
+ */ _proto.setAutoResolution = function setAutoResolution(scale) {
118
+ var _this = this;
119
+ if (scale === void 0) scale = 1;
120
+ if (!Number.isFinite(scale) || scale <= 0) {
121
+ throw new Error("WebCanvas.setAutoResolution: invalid scale " + scale);
122
+ }
123
+ // OffscreenCanvas has no display size to follow
124
+ if (this._isOffscreenCanvas()) return;
125
+ this._autoResolutionScale = scale;
126
+ if (this._resizeObserver) {
127
+ this._resolutionDirty = true;
128
+ } else if (typeof ResizeObserver !== "undefined") {
129
+ // observe() fires an initial entry, which sets the pending device pixel size
130
+ this._resizeObserver = new ResizeObserver(function(entries) {
131
+ var _entries__devicePixelContentBoxSize;
132
+ var box = (_entries__devicePixelContentBoxSize = entries[entries.length - 1].devicePixelContentBoxSize) == null ? void 0 : _entries__devicePixelContentBoxSize[0];
133
+ _this._pendingDevicePixelWidth = box ? box.inlineSize : 0;
134
+ _this._pendingDevicePixelHeight = box ? box.blockSize : 0;
135
+ _this._resolutionDirty = true;
136
+ });
137
+ // Observe exact device pixels where supported, otherwise the content-box and size from the client size
138
+ if (WebCanvas._supportsDevicePixelContentBox()) {
139
+ this._resizeObserver.observe(this._webCanvas, {
140
+ box: "device-pixel-content-box"
141
+ });
142
+ } else {
143
+ this._resizeObserver.observe(this._webCanvas);
144
+ }
145
+ } else {
146
+ // ResizeObserver is unavailable (e.g. mini-programs, iOS Safari < 13.4): size once from the client size
147
+ console.warn("ResizeObserver is not supported in this environment. Falling back to one-time clientWidth × devicePixelRatio × scale sizing");
148
+ this._setResolutionByClientSizeFallback(scale);
146
149
  }
147
150
  };
148
151
  /**
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;
152
+ * @internal
153
+ */ _proto._isOffscreenCanvas = function _isOffscreenCanvas() {
154
+ return typeof OffscreenCanvas !== "undefined" && _instanceof(this._webCanvas, OffscreenCanvas);
155
+ };
156
+ /**
157
+ * @internal
158
+ */ _proto._pumpPendingResolution = function _pumpPendingResolution() {
159
+ if (!this._resolutionDirty) return;
160
+ var webCanvas = this._webCanvas;
161
+ if (webCanvas.clientWidth === 0 || webCanvas.clientHeight === 0) return;
162
+ this._resolutionDirty = false;
163
+ var scale = this._autoResolutionScale;
164
+ // Snapshot the display size to detect a layout feedback loop after applying the buffer size below
165
+ var prevClientWidth = webCanvas.clientWidth;
166
+ var prevClientHeight = webCanvas.clientHeight;
167
+ if (this._pendingDevicePixelWidth > 0 && this._pendingDevicePixelHeight > 0) {
168
+ // Device pixels already fold in the device pixel ratio, so only the scale is applied
169
+ this._setResolution(Math.round(this._pendingDevicePixelWidth * scale), Math.round(this._pendingDevicePixelHeight * scale));
170
+ } else {
171
+ this._setResolutionByClientSizeFallback(scale);
172
+ }
173
+ // If setting the buffer moved the display size, the canvas has no CSS size constraint and would grow
174
+ // unboundedly; exit auto-resolution instead
175
+ if (webCanvas.clientWidth !== prevClientWidth || webCanvas.clientHeight !== prevClientHeight) {
176
+ console.warn("Auto-resolution disabled: the canvas has no CSS width/height, so sizing the buffer grows its display size. " + "Set a CSS width/height on the canvas, then call setResolution() or setAutoResolution().");
177
+ this._exitAutoResolution();
178
+ }
155
179
  };
156
- _proto._onWidthChanged = function _onWidthChanged(value) {
157
- this._webCanvas.width = value;
180
+ _proto._setResolutionByClientSizeFallback = function _setResolutionByClientSizeFallback(scale) {
181
+ var webCanvas = this._webCanvas;
182
+ if (webCanvas.clientWidth <= 0 || webCanvas.clientHeight <= 0) return;
183
+ var pixelRatio = window.devicePixelRatio * scale;
184
+ this._setResolution(Math.round(webCanvas.clientWidth * pixelRatio), Math.round(webCanvas.clientHeight * pixelRatio));
158
185
  };
159
- _proto._onHeightChange = function _onHeightChange(value) {
160
- this._webCanvas.height = value;
186
+ /**
187
+ * @internal
188
+ */ _proto._destroy = function _destroy() {
189
+ this._exitAutoResolution();
161
190
  };
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
- }
191
+ _proto._exitAutoResolution = function _exitAutoResolution() {
192
+ if (this._resizeObserver) {
193
+ this._resizeObserver.disconnect();
194
+ this._resizeObserver = undefined;
182
195
  }
183
- ]);
196
+ this._resolutionDirty = false;
197
+ this._pendingDevicePixelWidth = 0;
198
+ this._pendingDevicePixelHeight = 0;
199
+ };
200
+ _proto._onResolutionChanged = function _onResolutionChanged(width, height) {
201
+ var webCanvas = this._webCanvas;
202
+ webCanvas.width = width;
203
+ webCanvas.height = height;
204
+ };
205
+ WebCanvas._supportsDevicePixelContentBox = function _supportsDevicePixelContentBox() {
206
+ var _this__devicePixelContentBoxSupported;
207
+ // Passing { box: "device-pixel-content-box" } to observe() throws on browsers whose
208
+ // ResizeObserverBoxOptions enum lacks that value (Safari, Firefox < 93), so callers must gate on this
209
+ return (_this__devicePixelContentBoxSupported = this._devicePixelContentBoxSupported) != null ? _this__devicePixelContentBoxSupported : this._devicePixelContentBoxSupported = typeof ResizeObserverEntry !== "undefined" && "devicePixelContentBoxSize" in ResizeObserverEntry.prototype;
210
+ };
184
211
  return WebCanvas;
185
212
  }(engineCore.Canvas);
186
213
 
214
+ function _defineProperties(target, props) {
215
+ for (var i = 0; i < props.length; i++) {
216
+ var descriptor = props[i];
217
+ descriptor.enumerable = descriptor.enumerable || false;
218
+ descriptor.configurable = true;
219
+
220
+ if ("value" in descriptor) descriptor.writable = true;
221
+
222
+ Object.defineProperty(target, descriptor.key, descriptor);
223
+ }
224
+ }
225
+ function _create_class(Constructor, protoProps, staticProps) {
226
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
227
+ if (staticProps) _defineProperties(Constructor, staticProps);
228
+
229
+ return Constructor;
230
+ }
231
+
187
232
  /**
188
- * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
233
+ * WebGL platform engine, supports WebGL 2.0.
189
234
  */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
190
235
  _inherits(WebGLEngine, Engine);
191
236
  function WebGLEngine() {
@@ -200,20 +245,15 @@ function _instanceof(left, right) {
200
245
  var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
201
246
  var webGLGraphicDevice = new WebGLGraphicDevice(configuration.graphicDeviceOptions);
202
247
  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
- });
248
+ webCanvas.setAutoResolution();
249
+ return engine._initialize(configuration);
209
250
  };
210
251
  _create_class(WebGLEngine, [
211
252
  {
212
253
  key: "canvas",
213
254
  get: /**
214
- * Web canvas.
255
+ * The web canvas the engine renders to.
215
256
  */ function get() {
216
- // @ts-ignore
217
257
  return this._canvas;
218
258
  }
219
259
  }
@@ -240,12 +280,22 @@ var GLBuffer = /*#__PURE__*/ function() {
240
280
  var gl = rhi.gl;
241
281
  var glBuffer = gl.createBuffer();
242
282
  var glBufferUsage = this._getGLBufferUsage(gl, bufferUsage);
243
- var glBindTarget = type === engineCore.BufferBindFlag.VertexBuffer ? gl.ARRAY_BUFFER : gl.ELEMENT_ARRAY_BUFFER;
283
+ var glBindTarget;
284
+ switch(type){
285
+ case engineCore.BufferBindFlag.VertexBuffer:
286
+ glBindTarget = gl.ARRAY_BUFFER;
287
+ break;
288
+ case engineCore.BufferBindFlag.IndexBuffer:
289
+ glBindTarget = gl.ELEMENT_ARRAY_BUFFER;
290
+ break;
291
+ case engineCore.BufferBindFlag.ConstantBuffer:
292
+ glBindTarget = gl.UNIFORM_BUFFER;
293
+ break;
294
+ }
244
295
  this._gl = gl;
245
296
  this._glBuffer = glBuffer;
246
297
  this._glBufferUsage = glBufferUsage;
247
298
  this._glBindTarget = glBindTarget;
248
- this._isWebGL2 = rhi.isWebGL2;
249
299
  this.bind();
250
300
  if (data) {
251
301
  gl.bufferData(glBindTarget, data, glBufferUsage);
@@ -270,7 +320,7 @@ var GLBuffer = /*#__PURE__*/ function() {
270
320
  var dataByteLength = dataLength ? byteSize * dataLength : data.byteLength;
271
321
  if (dataOffset !== 0 || dataByteLength < data.byteLength) {
272
322
  var isArrayBufferView = data.byteOffset !== undefined;
273
- if (this._isWebGL2 && isArrayBufferView) {
323
+ if (isArrayBufferView) {
274
324
  gl.bufferSubData(glBindTarget, bufferByteOffset, data, dataOffset, dataByteLength / byteSize);
275
325
  } else {
276
326
  var subData = new Uint8Array(isArrayBufferView ? data.buffer : data, dataOffset * byteSize, dataByteLength);
@@ -282,13 +332,16 @@ var GLBuffer = /*#__PURE__*/ function() {
282
332
  gl.bindBuffer(glBindTarget, null);
283
333
  };
284
334
  _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
- }
335
+ this.bind();
336
+ this._gl.getBufferSubData(this._glBindTarget, bufferByteOffset, data, dataOffset, dataLength);
337
+ };
338
+ _proto.copyFromBuffer = function copyFromBuffer(srcBuffer, srcByteOffset, dstByteOffset, byteLength) {
339
+ var gl = this._gl;
340
+ gl.bindBuffer(gl.COPY_READ_BUFFER, srcBuffer._glBuffer);
341
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, this._glBuffer);
342
+ gl.copyBufferSubData(gl.COPY_READ_BUFFER, gl.COPY_WRITE_BUFFER, srcByteOffset, dstByteOffset, byteLength);
343
+ gl.bindBuffer(gl.COPY_READ_BUFFER, null);
344
+ gl.bindBuffer(gl.COPY_WRITE_BUFFER, null);
292
345
  };
293
346
  _proto.destroy = function destroy() {
294
347
  this._gl.deleteBuffer(this._glBuffer);
@@ -315,7 +368,6 @@ var GLBuffer = /*#__PURE__*/ function() {
315
368
  this._rhi = rhi;
316
369
  this.capabilityList = new Map();
317
370
  this._init();
318
- this._compatibleAllInterface();
319
371
  }
320
372
  var _proto = GLCapability.prototype;
321
373
  /**
@@ -331,9 +383,9 @@ var GLBuffer = /*#__PURE__*/ function() {
331
383
  RGB_ETC1_WEBGL = GLCompressedTextureInternalFormat.RGB_ETC1_WEBGL, // etc
332
384
  R11_EAC = GLCompressedTextureInternalFormat.R11_EAC, SRGB8_ALPHA8_ETC2_EAC = GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ETC2_EAC, // pvrtc
333
385
  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
386
+ RGB_S3TC_DXT1_EXT = GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT, RGBA_S3TC_DXT5_EXT = GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT, SRGB_S3TC_DXT1_EXT = GLCompressedTextureInternalFormat.SRGB_S3TC_DXT1_EXT, SRGB_ALPHA_S3TC_DXT5_EXT = GLCompressedTextureInternalFormat.SRGB_ALPHA_S3TC_DXT5_EXT, // bptc
335
387
  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) {
388
+ if (internalType >= RGBA_ASTC_4X4_KHR && internalType <= RGBA_ASTC_12X12_KHR || internalType >= SRGB8_ALPHA8_ASTC_4X4_KHR && internalType <= SRGB8_ALPHA8_ASTC_12X12_KHR) {
337
389
  return this.canIUse(engineCore.GLCapabilityType.astc);
338
390
  } else if (internalType === RGB_ETC1_WEBGL) {
339
391
  return this.canIUse(engineCore.GLCapabilityType.etc1);
@@ -343,36 +395,98 @@ var GLBuffer = /*#__PURE__*/ function() {
343
395
  return this.canIUse(engineCore.GLCapabilityType.pvrtc);
344
396
  } else if (internalType >= RGB_S3TC_DXT1_EXT && internalType <= RGBA_S3TC_DXT5_EXT) {
345
397
  return this.canIUse(engineCore.GLCapabilityType.s3tc);
398
+ } else if (internalType >= SRGB_S3TC_DXT1_EXT && internalType <= SRGB_ALPHA_S3TC_DXT5_EXT) {
399
+ return this.canIUse(engineCore.GLCapabilityType.s3tc_srgb);
346
400
  } else if (internalType >= RGBA_BPTC_UNORM_EXT && internalType <= RGB_BPTC_UNSIGNED_FLOAT_EXT) {
347
401
  return this.canIUse(engineCore.GLCapabilityType.bptc);
348
402
  }
349
403
  return false;
350
404
  };
351
405
  /**
406
+ * Check whether the given texture format is supported.
407
+ */ _proto.supportsTextureFormat = function supportsTextureFormat(format) {
408
+ switch(format){
409
+ case engineCore.TextureFormat.R16_UNorm:
410
+ return this.canIUse(engineCore.GLCapabilityType.textureNorm16);
411
+ case engineCore.TextureFormat.BC1:
412
+ case engineCore.TextureFormat.BC3:
413
+ return this.canIUse(engineCore.GLCapabilityType.s3tc);
414
+ case engineCore.TextureFormat.BC7:
415
+ case engineCore.TextureFormat.BC6H:
416
+ return this.canIUse(engineCore.GLCapabilityType.bptc);
417
+ case engineCore.TextureFormat.ETC1_RGB:
418
+ return this.canIUse(engineCore.GLCapabilityType.etc1);
419
+ case engineCore.TextureFormat.ETC2_RGB:
420
+ case engineCore.TextureFormat.ETC2_RGBA5:
421
+ case engineCore.TextureFormat.ETC2_RGBA8:
422
+ return this.canIUse(engineCore.GLCapabilityType.etc);
423
+ case engineCore.TextureFormat.PVRTC_RGB2:
424
+ case engineCore.TextureFormat.PVRTC_RGBA2:
425
+ case engineCore.TextureFormat.PVRTC_RGB4:
426
+ case engineCore.TextureFormat.PVRTC_RGBA4:
427
+ return this.canIUse(engineCore.GLCapabilityType.pvrtc);
428
+ case engineCore.TextureFormat.ASTC_4x4:
429
+ case engineCore.TextureFormat.ASTC_5x5:
430
+ case engineCore.TextureFormat.ASTC_6x6:
431
+ case engineCore.TextureFormat.ASTC_8x8:
432
+ case engineCore.TextureFormat.ASTC_10x10:
433
+ case engineCore.TextureFormat.ASTC_12x12:
434
+ return this.canIUse(engineCore.GLCapabilityType.astc);
435
+ case engineCore.TextureFormat.R8G8B8_UNorm:
436
+ case engineCore.TextureFormat.R8G8B8A8_UNorm:
437
+ case engineCore.TextureFormat.R4G4B4A4_UNorm:
438
+ case engineCore.TextureFormat.R5G5B5A1_UNorm:
439
+ case engineCore.TextureFormat.R5G6B5_UNorm:
440
+ case engineCore.TextureFormat.R16G16B16A16_SFloat:
441
+ case engineCore.TextureFormat.R32G32B32A32_SFloat:
442
+ case engineCore.TextureFormat.R32G32B32A32_UInt:
443
+ case engineCore.TextureFormat.R11G11B10_UFloat:
444
+ case engineCore.TextureFormat.R16_SFloat:
445
+ case engineCore.TextureFormat.R16G16_SFloat:
446
+ case engineCore.TextureFormat.R32_SFloat:
447
+ case engineCore.TextureFormat.R32G32_SFloat:
448
+ case engineCore.TextureFormat.R8_UNorm:
449
+ case engineCore.TextureFormat.R8G8_UNorm:
450
+ case engineCore.TextureFormat.Depth:
451
+ case engineCore.TextureFormat.DepthStencil:
452
+ case engineCore.TextureFormat.Depth16:
453
+ case engineCore.TextureFormat.Depth24:
454
+ case engineCore.TextureFormat.Depth32:
455
+ case engineCore.TextureFormat.Depth24Stencil8:
456
+ case engineCore.TextureFormat.Depth32Stencil8:
457
+ return true;
458
+ default:
459
+ return false;
460
+ }
461
+ };
462
+ /**
463
+ * Check whether the given texture format can be rendered to as a color attachment.
464
+ */ _proto.supportsRenderTargetColorFormat = function supportsRenderTargetColorFormat(format) {
465
+ switch(format){
466
+ case engineCore.TextureFormat.R16G16B16A16_SFloat:
467
+ return this.canIUse(engineCore.GLCapabilityType.colorBufferHalfFloat);
468
+ case engineCore.TextureFormat.R16_SFloat:
469
+ case engineCore.TextureFormat.R16G16_SFloat:
470
+ case engineCore.TextureFormat.R32_SFloat:
471
+ case engineCore.TextureFormat.R32G32_SFloat:
472
+ case engineCore.TextureFormat.R32G32B32A32_SFloat:
473
+ case engineCore.TextureFormat.R11G11B10_UFloat:
474
+ return this.canIUse(engineCore.GLCapabilityType.colorBufferFloat);
475
+ default:
476
+ return this.supportsTextureFormat(format);
477
+ }
478
+ };
479
+ /**
352
480
  * Init capabilities.
353
481
  */ _proto._init = function _init() {
354
482
  var cap = this.capabilityList;
355
- var isWebGL2 = this.rhi.isWebGL2;
356
483
  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));
484
+ var 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, textureNorm16 = engineCore.GLCapabilityType.textureNorm16, textureFloatLinear = engineCore.GLCapabilityType.textureFloatLinear, colorBufferFloat = engineCore.GLCapabilityType.colorBufferFloat, colorBufferHalfFloat = engineCore.GLCapabilityType.colorBufferHalfFloat, textureFilterAnisotropic = engineCore.GLCapabilityType.textureFilterAnisotropic;
485
+ cap.set(textureNorm16, !!requireExtension(textureNorm16));
370
486
  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));
487
+ cap.set(colorBufferFloat, !!requireExtension(colorBufferFloat));
488
+ cap.set(colorBufferHalfFloat, !!requireExtension(colorBufferFloat) || !!requireExtension(colorBufferHalfFloat));
374
489
  cap.set(textureFilterAnisotropic, !!requireExtension(textureFilterAnisotropic));
375
- cap.set(fragDepth, isWebGL2 || !!requireExtension(fragDepth));
376
490
  cap.set(etc, !!(requireExtension(etc) || requireExtension(etc_webkit)));
377
491
  cap.set(etc1, !!(requireExtension(etc1) || requireExtension(etc1_webkit)));
378
492
  cap.set(pvrtc, !!(requireExtension(pvrtc) || requireExtension(pvrtc_webkit)));
@@ -381,86 +495,7 @@ var GLBuffer = /*#__PURE__*/ function() {
381
495
  cap.set(bptc, !!requireExtension(bptc));
382
496
  var astcExtension = requireExtension(astc) || requireExtension(astc_webkit);
383
497
  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
- });
498
+ cap.set(astc_hdr, typeof (astcExtension == null ? void 0 : astcExtension.getSupportedProfiles) === "function" && astcExtension.getSupportedProfiles().includes("hdr"));
464
499
  };
465
500
  _create_class(GLCapability, [
466
501
  {
@@ -470,28 +505,16 @@ var GLBuffer = /*#__PURE__*/ function() {
470
505
  }
471
506
  },
472
507
  {
473
- key: "canUseFloatTextureBlendShape",
508
+ key: "maxVertexUniformVectors",
474
509
  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;
510
+ return this.rhi.renderStates.getParameter(this.rhi.gl.MAX_VERTEX_UNIFORM_VECTORS);
484
511
  }
485
512
  },
486
513
  {
487
514
  key: "maxDrawBuffers",
488
515
  get: function get() {
489
516
  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
- }
517
+ this._maxDrawBuffers = this._rhi.gl.getParameter(this._rhi.gl.MAX_DRAW_BUFFERS);
495
518
  }
496
519
  return this._maxDrawBuffers;
497
520
  }
@@ -515,19 +538,11 @@ var GLBuffer = /*#__PURE__*/ function() {
515
538
  */ function get() {
516
539
  if (!this._maxAntiAliasing) {
517
540
  var gl = this._rhi.gl;
518
- var canMSAA = this.canIUse(engineCore.GLCapabilityType.multipleSample);
519
- this._maxAntiAliasing = canMSAA ? gl.getParameter(gl.MAX_SAMPLES) : 1;
541
+ this._maxAntiAliasing = gl.getParameter(gl.MAX_SAMPLES);
520
542
  }
521
543
  return this._maxAntiAliasing;
522
544
  }
523
545
  },
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
546
  {
532
547
  key: "rhi",
533
548
  get: function get() {
@@ -559,9 +574,6 @@ var GLBuffer = /*#__PURE__*/ function() {
559
574
  }();
560
575
 
561
576
  /**
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
577
  * @internal
566
578
  * GL platform primitive.
567
579
  */ var GLPrimitive = /*#__PURE__*/ function() {
@@ -569,8 +581,6 @@ var GLBuffer = /*#__PURE__*/ function() {
569
581
  this._attribLocArray = [];
570
582
  this._vaoMap = new Map();
571
583
  this._primitive = primitive;
572
- this._canUseInstancedArrays = rhi.canIUse(engineCore.GLCapabilityType.instancedArrays);
573
- this._isSupportVAO = rhi.canIUse(engineCore.GLCapabilityType.vertexArrayObject);
574
584
  this._gl = rhi.gl;
575
585
  }
576
586
  var _proto = GLPrimitive.prototype;
@@ -579,7 +589,7 @@ var GLBuffer = /*#__PURE__*/ function() {
579
589
  */ _proto.draw = function draw(shaderProgram, subMesh) {
580
590
  var gl = this._gl;
581
591
  var primitive = this._primitive;
582
- var useVao = this._isSupportVAO && primitive.enableVAO;
592
+ var useVao = primitive.enableVAO;
583
593
  if (useVao) {
584
594
  if (primitive._bufferStructChanged) {
585
595
  this._clearVAO();
@@ -608,21 +618,17 @@ var GLBuffer = /*#__PURE__*/ function() {
608
618
  gl.drawArrays(topology, start, count);
609
619
  }
610
620
  } 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
+ if (indexBufferBinding) {
622
+ if (useVao) {
623
+ gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, instanceCount);
621
624
  } else {
622
- gl.drawArraysInstanced(topology, start, count, instanceCount);
625
+ var _glBuffer1 = indexBufferBinding.buffer._platformBuffer._glBuffer;
626
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer1);
627
+ gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, instanceCount);
628
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
623
629
  }
624
630
  } else {
625
- engineCore.Logger.error("ANGLE_instanced_arrays extension is not supported");
631
+ gl.drawArraysInstanced(topology, start, count, instanceCount);
626
632
  }
627
633
  }
628
634
  // Unbind
@@ -633,7 +639,7 @@ var GLBuffer = /*#__PURE__*/ function() {
633
639
  }
634
640
  };
635
641
  _proto.destroy = function destroy() {
636
- this._isSupportVAO && this._clearVAO();
642
+ this._clearVAO();
637
643
  };
638
644
  /**
639
645
  * Bind buffer and attribute.
@@ -661,9 +667,7 @@ var GLBuffer = /*#__PURE__*/ function() {
661
667
  gl.enableVertexAttribArray(loc);
662
668
  var elementInfo = element._formatMetaInfo;
663
669
  gl.vertexAttribPointer(loc, elementInfo.size, elementInfo.type, elementInfo.normalized, stride, element.offset);
664
- if (this._canUseInstancedArrays) {
665
- gl.vertexAttribDivisor(loc, element.instanceStepRate);
666
- }
670
+ gl.vertexAttribDivisor(loc, element.instanceStepRate);
667
671
  this._attribLocArray.push(loc);
668
672
  } else {
669
673
  engineCore.Logger.warn("vertex attribute not found: " + name);
@@ -711,7 +715,6 @@ var GLBuffer = /*#__PURE__*/ function() {
711
715
  /** cache */ this._parameters[gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS] = gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS);
712
716
  this._parameters[gl.MAX_VERTEX_UNIFORM_VECTORS] = gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS);
713
717
  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
718
  this._parameters[gl.MAX_TEXTURE_SIZE] = gl.getParameter(gl.MAX_TEXTURE_SIZE);
716
719
  // init blend state same as BlendState default value.
717
720
  gl.blendFuncSeparate(gl.ONE, gl.ZERO, gl.ONE, gl.ZERO);
@@ -752,7 +755,6 @@ var GLBuffer = /*#__PURE__*/ function() {
752
755
  this._texture = texture;
753
756
  this._rhi = rhi;
754
757
  this._gl = rhi.gl;
755
- this._isWebGL2 = rhi.isWebGL2;
756
758
  this._target = target;
757
759
  this._glTexture = this._gl.createTexture();
758
760
  }
@@ -775,10 +777,14 @@ var GLBuffer = /*#__PURE__*/ function() {
775
777
  * Generate multi-level textures based on the 0th level data.
776
778
  */ _proto.generateMipmaps = function generateMipmaps() {
777
779
  var texture = this._texture;
778
- //@ts-ignore
779
780
  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.");
781
+ if (!engineCore.TextureUtils.supportGenerateMipmaps(texture.format, mipmap, texture.isSRGBColorSpace)) {
782
+ engineCore.Logger.warn("Auto-generating mipmaps for sRGB textures is only supported in [WebGL2 + R8G8B8A8_UNorm], you must generate mipmaps manually.");
783
+ return;
784
+ }
785
+ // WebGL2 generateMipmap requires the format to be both color-renderable and texture-filterable
786
+ if (!this._rhi.supportsRenderTargetColorFormat(texture.format) || this._isFloat32Format(texture.format) && !this._rhi.canIUse(engineCore.GLCapabilityType.textureFloatLinear)) {
787
+ engineCore.Logger.warn("Auto-generating mipmaps is not supported for TextureFormat." + engineCore.TextureFormat[texture.format] + " on this device.");
782
788
  return;
783
789
  }
784
790
  // @todo (1x1).generateMipmap() will flash back in uc.
@@ -794,12 +800,10 @@ var GLBuffer = /*#__PURE__*/ function() {
794
800
  * Pre-development mipmapping GPU memory.
795
801
  */ _proto._init = function _init(isCube) {
796
802
  var gl = this._gl;
797
- var isWebGL2 = this._isWebGL2;
798
803
  var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
799
- // @ts-ignore
800
804
  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
805
  this._bind();
802
- if (isWebGL2 && !(baseFormat === gl.LUMINANCE_ALPHA || baseFormat === gl.ALPHA) && usage !== engineCore.TextureUsage.Dynamic) {
806
+ if (usage !== engineCore.TextureUsage.Dynamic) {
803
807
  gl.texStorage2D(this._target, mipmapCount, internalFormat, width, height);
804
808
  } else {
805
809
  if (!isCube) {
@@ -833,22 +837,26 @@ var GLBuffer = /*#__PURE__*/ function() {
833
837
  * @param mipLevel - Set mip level the data want to get from
834
838
  */ _proto._getPixelBuffer = function _getPixelBuffer(face, x, y, width, height, mipLevel, out) {
835
839
  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;
840
+ var _this__formatDetail = this._formatDetail, dataType = _this__formatDetail.dataType, readType = _this__formatDetail.readType, alignment = _this__formatDetail.alignment;
841
+ var readFormat = this._getReadFormat();
837
842
  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
843
  if (face != null) {
843
844
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, this._glTexture, mipLevel);
844
845
  } else {
845
846
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._glTexture, mipLevel);
846
847
  }
847
848
  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);
849
+ gl.readPixels(x, y, width, height, readFormat, readType != null ? readType : dataType, out);
850
850
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
851
851
  };
852
+ _proto._getReadFormat = function _getReadFormat() {
853
+ var format = this._texture.format;
854
+ if (!this._rhi.supportsRenderTargetColorFormat(format)) {
855
+ throw new Error("Reading pixels from TextureFormat." + engineCore.TextureFormat[format] + " requires color-renderable support");
856
+ }
857
+ var _this__formatDetail = this._formatDetail, readFormat = _this__formatDetail.readFormat, baseFormat = _this__formatDetail.baseFormat;
858
+ return readFormat != null ? readFormat : baseFormat;
859
+ };
852
860
  _proto._getReadFrameBuffer = function _getReadFrameBuffer() {
853
861
  var frameBuffer = this._rhi._readFrameBuffer;
854
862
  if (!frameBuffer) {
@@ -856,8 +864,8 @@ var GLBuffer = /*#__PURE__*/ function() {
856
864
  }
857
865
  return frameBuffer;
858
866
  };
859
- _proto._validate = function _validate(texture, rhi) {
860
- var format = texture.format, width = texture.width, height = texture.height;
867
+ _proto._validate = function _validate(texture) {
868
+ var format = texture.format;
861
869
  // Validate sRGB format
862
870
  // @ts-ignore
863
871
  var isSRGBColorSpace = texture._isSRGBColorSpace;
@@ -866,25 +874,13 @@ var GLBuffer = /*#__PURE__*/ function() {
866
874
  // @ts-ignore
867
875
  texture._isSRGBColorSpace = false;
868
876
  }
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
- }
877
+ };
878
+ _proto._isFloat32Format = function _isFloat32Format(format) {
879
+ return format === engineCore.TextureFormat.R32_SFloat || format === engineCore.TextureFormat.R32G32_SFloat || format === engineCore.TextureFormat.R32G32B32A32_SFloat;
878
880
  };
879
881
  _proto._setWrapMode = function _setWrapMode(value, pname) {
880
882
  var gl = this._gl;
881
- var isWebGL2 = this._isWebGL2;
882
883
  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
884
  switch(value){
889
885
  case engineCore.TextureWrapMode.Clamp:
890
886
  gl.texParameteri(target, pname, gl.CLAMP_TO_EDGE);
@@ -900,92 +896,107 @@ var GLBuffer = /*#__PURE__*/ function() {
900
896
  /**
901
897
  * Get more texture info from TextureFormat.
902
898
  * @internal
903
- */ GLTexture._getFormatDetail = function _getFormatDetail(format, isSRGBColorSpace, gl, isWebGL2) {
899
+ */ GLTexture._getFormatDetail = function _getFormatDetail(format, isSRGBColorSpace, gl) {
904
900
  switch(format){
905
- case engineCore.TextureFormat.R8G8B8:
901
+ case engineCore.TextureFormat.R8G8B8_UNorm:
906
902
  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,
903
+ internalFormat: isSRGBColorSpace ? gl.SRGB8 : gl.RGB8,
904
+ baseFormat: gl.RGB,
905
+ readFormat: gl.RGBA,
910
906
  dataType: gl.UNSIGNED_BYTE,
911
907
  isCompressed: false,
912
908
  alignment: 1
913
909
  };
914
- case engineCore.TextureFormat.R8G8B8A8:
910
+ case engineCore.TextureFormat.R8G8B8A8_UNorm:
915
911
  return {
916
- internalFormat: isSRGBColorSpace ? gl.SRGB8_ALPHA8 : isWebGL2 ? gl.RGBA8 : gl.RGBA,
917
- baseFormat: isSRGBColorSpace ? isWebGL2 ? gl.RGBA : gl.SRGB8_ALPHA8 : gl.RGBA,
912
+ internalFormat: isSRGBColorSpace ? gl.SRGB8_ALPHA8 : gl.RGBA8,
913
+ baseFormat: gl.RGBA,
918
914
  readFormat: gl.RGBA,
919
915
  dataType: gl.UNSIGNED_BYTE,
920
916
  isCompressed: false,
921
917
  alignment: 4
922
918
  };
923
- case engineCore.TextureFormat.R4G4B4A4:
919
+ case engineCore.TextureFormat.R4G4B4A4_UNorm:
924
920
  return {
925
- internalFormat: isWebGL2 ? gl.RGBA4 : gl.RGBA,
921
+ internalFormat: gl.RGBA4,
926
922
  baseFormat: gl.RGBA,
927
923
  dataType: gl.UNSIGNED_SHORT_4_4_4_4,
928
924
  isCompressed: false,
929
925
  alignment: 2
930
926
  };
931
- case engineCore.TextureFormat.R5G5B5A1:
927
+ case engineCore.TextureFormat.R5G5B5A1_UNorm:
932
928
  return {
933
- internalFormat: isWebGL2 ? gl.RGB5_A1 : gl.RGBA,
929
+ internalFormat: gl.RGB5_A1,
934
930
  baseFormat: gl.RGBA,
935
931
  dataType: gl.UNSIGNED_SHORT_5_5_5_1,
936
932
  isCompressed: false,
937
933
  alignment: 2
938
934
  };
939
- case engineCore.TextureFormat.R5G6B5:
935
+ case engineCore.TextureFormat.R5G6B5_UNorm:
940
936
  return {
941
- internalFormat: isWebGL2 ? gl.RGB565 : gl.RGB,
937
+ internalFormat: gl.RGB565,
942
938
  baseFormat: gl.RGB,
943
939
  dataType: gl.UNSIGNED_SHORT_5_6_5,
944
940
  isCompressed: false,
945
941
  alignment: 2
946
942
  };
947
- case engineCore.TextureFormat.Alpha8:
943
+ case engineCore.TextureFormat.R16G16B16A16_SFloat:
948
944
  return {
949
- internalFormat: gl.ALPHA,
950
- baseFormat: gl.ALPHA,
951
- dataType: gl.UNSIGNED_BYTE,
945
+ internalFormat: gl.RGBA16F,
946
+ baseFormat: gl.RGBA,
947
+ readFormat: gl.RGBA,
948
+ readType: gl.FLOAT,
949
+ dataType: gl.HALF_FLOAT,
952
950
  isCompressed: false,
953
- alignment: 1
951
+ alignment: 8
954
952
  };
955
- case engineCore.TextureFormat.LuminanceAlpha:
953
+ case engineCore.TextureFormat.R32G32B32A32_SFloat:
956
954
  return {
957
- internalFormat: gl.LUMINANCE_ALPHA,
958
- baseFormat: gl.LUMINANCE_ALPHA,
959
- dataType: gl.UNSIGNED_BYTE,
955
+ internalFormat: gl.RGBA32F,
956
+ baseFormat: gl.RGBA,
957
+ dataType: gl.FLOAT,
958
+ isCompressed: false,
959
+ alignment: 8
960
+ };
961
+ case engineCore.TextureFormat.R16_UNorm:
962
+ return {
963
+ internalFormat: 0x822a,
964
+ baseFormat: gl.RED,
965
+ readFormat: gl.RGBA,
966
+ dataType: gl.UNSIGNED_SHORT,
960
967
  isCompressed: false,
961
968
  alignment: 2
962
969
  };
963
- case engineCore.TextureFormat.R16G16B16A16:
970
+ case engineCore.TextureFormat.R16_SFloat:
971
+ case engineCore.TextureFormat.R16G16_SFloat:
964
972
  return {
965
- internalFormat: isWebGL2 ? gl.RGBA16F : gl.RGBA,
966
- baseFormat: gl.RGBA,
973
+ internalFormat: format === engineCore.TextureFormat.R16_SFloat ? gl.R16F : gl.RG16F,
974
+ baseFormat: format === engineCore.TextureFormat.R16_SFloat ? gl.RED : gl.RG,
975
+ readFormat: gl.RGBA,
976
+ readType: gl.FLOAT,
967
977
  dataType: gl.HALF_FLOAT,
968
978
  isCompressed: false,
969
- alignment: 8
979
+ alignment: 2
970
980
  };
971
- case engineCore.TextureFormat.R32G32B32A32:
981
+ case engineCore.TextureFormat.R32_SFloat:
982
+ case engineCore.TextureFormat.R32G32_SFloat:
972
983
  return {
973
- internalFormat: isWebGL2 ? gl.RGBA32F : gl.RGBA,
974
- baseFormat: gl.RGBA,
984
+ internalFormat: format === engineCore.TextureFormat.R32_SFloat ? gl.R32F : gl.RG32F,
985
+ baseFormat: format === engineCore.TextureFormat.R32_SFloat ? gl.RED : gl.RG,
986
+ readFormat: gl.RGBA,
975
987
  dataType: gl.FLOAT,
976
988
  isCompressed: false,
977
- alignment: 8
989
+ alignment: 4
978
990
  };
979
- // Only WebGL2 support
980
991
  case engineCore.TextureFormat.R11G11B10_UFloat:
981
992
  return {
982
993
  internalFormat: gl.R11F_G11F_B10F,
983
994
  baseFormat: gl.RGB,
995
+ readFormat: gl.RGBA,
984
996
  dataType: gl.FLOAT,
985
997
  isCompressed: false,
986
998
  alignment: 4
987
999
  };
988
- // Only WebGL2 support
989
1000
  case engineCore.TextureFormat.R32G32B32A32_UInt:
990
1001
  return {
991
1002
  internalFormat: gl.RGBA32UI,
@@ -994,8 +1005,7 @@ var GLBuffer = /*#__PURE__*/ function() {
994
1005
  isCompressed: false,
995
1006
  alignment: 8
996
1007
  };
997
- // Only WebGL2 support
998
- case engineCore.TextureFormat.R8:
1008
+ case engineCore.TextureFormat.R8_UNorm:
999
1009
  return {
1000
1010
  internalFormat: gl.R8,
1001
1011
  baseFormat: gl.RED,
@@ -1003,8 +1013,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1003
1013
  isCompressed: false,
1004
1014
  alignment: 1
1005
1015
  };
1006
- // Only WebGL2 support
1007
- case engineCore.TextureFormat.R8G8:
1016
+ case engineCore.TextureFormat.R8G8_UNorm:
1008
1017
  return {
1009
1018
  internalFormat: gl.RG8,
1010
1019
  baseFormat: gl.RG,
@@ -1044,7 +1053,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1044
1053
  };
1045
1054
  case engineCore.TextureFormat.ETC2_RGBA5:
1046
1055
  return {
1047
- internalFormat: GLCompressedTextureInternalFormat.RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
1056
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 : GLCompressedTextureInternalFormat.RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
1048
1057
  isCompressed: true
1049
1058
  };
1050
1059
  case engineCore.TextureFormat.ETC2_RGBA8:
@@ -1079,48 +1088,48 @@ var GLBuffer = /*#__PURE__*/ function() {
1079
1088
  };
1080
1089
  case engineCore.TextureFormat.ASTC_5x5:
1081
1090
  return {
1082
- internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_5X5_KHR,
1091
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ASTC_5X5_KHR : GLCompressedTextureInternalFormat.RGBA_ASTC_5X5_KHR,
1083
1092
  isCompressed: true
1084
1093
  };
1085
1094
  case engineCore.TextureFormat.ASTC_6x6:
1086
1095
  return {
1087
- internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_6X6_KHR,
1096
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ASTC_6X6_KHR : GLCompressedTextureInternalFormat.RGBA_ASTC_6X6_KHR,
1088
1097
  isCompressed: true
1089
1098
  };
1090
1099
  case engineCore.TextureFormat.ASTC_8x8:
1091
1100
  return {
1092
- internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_8X8_KHR,
1101
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ASTC_8X8_KHR : GLCompressedTextureInternalFormat.RGBA_ASTC_8X8_KHR,
1093
1102
  isCompressed: true
1094
1103
  };
1095
1104
  case engineCore.TextureFormat.ASTC_10x10:
1096
1105
  return {
1097
- internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_10X10_KHR,
1106
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ASTC_10X10_KHR : GLCompressedTextureInternalFormat.RGBA_ASTC_10X10_KHR,
1098
1107
  isCompressed: true
1099
1108
  };
1100
1109
  case engineCore.TextureFormat.ASTC_12x12:
1101
1110
  return {
1102
- internalFormat: GLCompressedTextureInternalFormat.RGBA_ASTC_12X12_KHR,
1111
+ internalFormat: isSRGBColorSpace ? GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ASTC_12X12_KHR : GLCompressedTextureInternalFormat.RGBA_ASTC_12X12_KHR,
1103
1112
  isCompressed: true
1104
1113
  };
1105
1114
  case engineCore.TextureFormat.Depth:
1106
1115
  return {
1107
- internalFormat: isWebGL2 ? gl.DEPTH_COMPONENT32F : gl.DEPTH_COMPONENT,
1116
+ internalFormat: gl.DEPTH_COMPONENT32F,
1108
1117
  baseFormat: gl.DEPTH_COMPONENT,
1109
- dataType: isWebGL2 ? gl.FLOAT : gl.UNSIGNED_SHORT,
1118
+ dataType: gl.FLOAT,
1110
1119
  isCompressed: false,
1111
1120
  attachment: gl.DEPTH_ATTACHMENT
1112
1121
  };
1113
1122
  case engineCore.TextureFormat.DepthStencil:
1114
1123
  return {
1115
- internalFormat: isWebGL2 ? gl.DEPTH32F_STENCIL8 : gl.DEPTH_STENCIL,
1124
+ internalFormat: gl.DEPTH32F_STENCIL8,
1116
1125
  baseFormat: gl.DEPTH_STENCIL,
1117
- dataType: isWebGL2 ? gl.FLOAT_32_UNSIGNED_INT_24_8_REV : gl.UNSIGNED_INT_24_8,
1126
+ dataType: gl.FLOAT_32_UNSIGNED_INT_24_8_REV,
1118
1127
  isCompressed: false,
1119
1128
  attachment: gl.DEPTH_STENCIL_ATTACHMENT
1120
1129
  };
1121
1130
  case engineCore.TextureFormat.Depth16:
1122
1131
  return {
1123
- internalFormat: isWebGL2 ? gl.DEPTH_COMPONENT16 : gl.DEPTH_COMPONENT,
1132
+ internalFormat: gl.DEPTH_COMPONENT16,
1124
1133
  baseFormat: gl.DEPTH_COMPONENT,
1125
1134
  dataType: gl.UNSIGNED_SHORT,
1126
1135
  isCompressed: false,
@@ -1128,13 +1137,12 @@ var GLBuffer = /*#__PURE__*/ function() {
1128
1137
  };
1129
1138
  case engineCore.TextureFormat.Depth24Stencil8:
1130
1139
  return {
1131
- internalFormat: isWebGL2 ? gl.DEPTH24_STENCIL8 : gl.DEPTH_STENCIL,
1140
+ internalFormat: gl.DEPTH24_STENCIL8,
1132
1141
  baseFormat: gl.DEPTH_STENCIL,
1133
1142
  dataType: gl.UNSIGNED_INT_24_8,
1134
1143
  isCompressed: false,
1135
1144
  attachment: gl.DEPTH_STENCIL_ATTACHMENT
1136
1145
  };
1137
- // Only WebGL2 support
1138
1146
  case engineCore.TextureFormat.Depth24:
1139
1147
  return {
1140
1148
  internalFormat: gl.DEPTH_COMPONENT24,
@@ -1143,7 +1151,6 @@ var GLBuffer = /*#__PURE__*/ function() {
1143
1151
  isCompressed: false,
1144
1152
  attachment: gl.DEPTH_ATTACHMENT
1145
1153
  };
1146
- // Only WebGL2 support
1147
1154
  case engineCore.TextureFormat.Depth32:
1148
1155
  return {
1149
1156
  internalFormat: gl.DEPTH_COMPONENT32F,
@@ -1152,7 +1159,6 @@ var GLBuffer = /*#__PURE__*/ function() {
1152
1159
  isCompressed: false,
1153
1160
  attachment: gl.DEPTH_ATTACHMENT
1154
1161
  };
1155
- // Only WebGL2 support
1156
1162
  case engineCore.TextureFormat.Depth32Stencil8:
1157
1163
  return {
1158
1164
  internalFormat: gl.DEPTH32F_STENCIL8,
@@ -1166,23 +1172,22 @@ var GLBuffer = /*#__PURE__*/ function() {
1166
1172
  }
1167
1173
  };
1168
1174
  /**
1169
- * In WebGL 1, internalformat must be the same as baseFormat when call texImage2D.
1170
1175
  * @internal
1171
- */ GLTexture._getRenderBufferDepthFormatDetail = function _getRenderBufferDepthFormatDetail(format, gl, isWebGL2) {
1176
+ */ GLTexture._getRenderBufferDepthFormatDetail = function _getRenderBufferDepthFormatDetail(format, gl) {
1172
1177
  switch(format){
1173
1178
  case engineCore.TextureFormat.Depth:
1174
1179
  return {
1175
- internalFormat: isWebGL2 ? gl.DEPTH_COMPONENT32F : gl.DEPTH_COMPONENT16,
1180
+ internalFormat: gl.DEPTH_COMPONENT32F,
1176
1181
  baseFormat: gl.DEPTH_COMPONENT,
1177
- dataType: isWebGL2 ? gl.FLOAT : gl.UNSIGNED_SHORT,
1182
+ dataType: gl.FLOAT,
1178
1183
  isCompressed: false,
1179
1184
  attachment: gl.DEPTH_ATTACHMENT
1180
1185
  };
1181
1186
  case engineCore.TextureFormat.DepthStencil:
1182
1187
  return {
1183
- internalFormat: isWebGL2 ? gl.DEPTH32F_STENCIL8 : gl.DEPTH_STENCIL,
1188
+ internalFormat: gl.DEPTH32F_STENCIL8,
1184
1189
  baseFormat: gl.DEPTH_STENCIL,
1185
- dataType: isWebGL2 ? gl.FLOAT_32_UNSIGNED_INT_24_8_REV : gl.UNSIGNED_INT_24_8,
1190
+ dataType: gl.FLOAT_32_UNSIGNED_INT_24_8_REV,
1186
1191
  isCompressed: false,
1187
1192
  attachment: gl.DEPTH_STENCIL_ATTACHMENT
1188
1193
  };
@@ -1204,7 +1209,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1204
1209
  };
1205
1210
  case engineCore.TextureFormat.Depth24Stencil8:
1206
1211
  return {
1207
- internalFormat: isWebGL2 ? gl.DEPTH24_STENCIL8 : gl.DEPTH_STENCIL,
1212
+ internalFormat: gl.DEPTH24_STENCIL8,
1208
1213
  baseFormat: gl.DEPTH_STENCIL,
1209
1214
  dataType: gl.UNSIGNED_INT_24_8,
1210
1215
  isCompressed: false,
@@ -1238,46 +1243,6 @@ var GLBuffer = /*#__PURE__*/ function() {
1238
1243
  throw new Error("this TextureFormat is not supported in Galacean Engine: " + format);
1239
1244
  }
1240
1245
  };
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
1246
  _create_class(GLTexture, [
1282
1247
  {
1283
1248
  key: "wrapModeU",
@@ -1304,7 +1269,10 @@ var GLBuffer = /*#__PURE__*/ function() {
1304
1269
  */ function set(value) {
1305
1270
  var gl = this._gl;
1306
1271
  var target = this._target;
1307
- /** @ts-ignore */ var _mipmap = this._texture._mipmap;
1272
+ var _this__texture = this._texture, _mipmap = _this__texture._mipmap, format = _this__texture.format;
1273
+ if (value !== engineCore.TextureFilterMode.Point && this._isFloat32Format(format) && !this._rhi.canIUse(engineCore.GLCapabilityType.textureFloatLinear)) {
1274
+ value = engineCore.TextureFilterMode.Point;
1275
+ }
1308
1276
  this._bind();
1309
1277
  switch(value){
1310
1278
  case engineCore.TextureFilterMode.Point:
@@ -1327,14 +1295,16 @@ var GLBuffer = /*#__PURE__*/ function() {
1327
1295
  set: /**
1328
1296
  * Anisotropic level for texture.
1329
1297
  */ function set(value) {
1330
- var gl = this._gl;
1298
+ var extension = this._rhi.requireExtension(engineCore.GLCapabilityType.textureFilterAnisotropic);
1299
+ if (!extension) return;
1331
1300
  this._bind();
1332
- gl.texParameterf(this._target, gl.TEXTURE_MAX_ANISOTROPY_EXT, value);
1301
+ this._gl.texParameterf(this._target, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(value, this._rhi.capability.maxAnisoLevel));
1333
1302
  }
1334
1303
  },
1335
1304
  {
1336
1305
  key: "depthCompareFunction",
1337
1306
  set: function set(value) {
1307
+ if (value === undefined) return;
1338
1308
  this._bind();
1339
1309
  var gl = this._gl;
1340
1310
  switch(value){
@@ -1377,34 +1347,27 @@ var GLBuffer = /*#__PURE__*/ function() {
1377
1347
  this._curMipLevel = 0;
1378
1348
  this._curFaceIndex = undefined;
1379
1349
  this._gl = rhi.gl;
1380
- this._isWebGL2 = rhi.isWebGL2;
1381
1350
  this._target = target;
1351
+ this._antiAliasing = Math.min(target.antiAliasing, rhi.capability.maxAntiAliasing);
1382
1352
  /** @ts-ignore */ var _colorTextures = target._colorTextures, _depth = target._depth, width = target.width, height = target.height;
1383
- var isDepthTexture = _instanceof(_depth, engineCore.Texture);
1384
1353
  /** todo
1385
1354
  * MRT + Cube + [,MSAA]
1386
1355
  * MRT + MSAA
1387
1356
  */ for(var i = 0, n = _colorTextures.length; i < n; i++){
1388
1357
  var _colorTextures_i = _colorTextures[i], format = _colorTextures_i.format, isSRGBColorSpace = _colorTextures_i.isSRGBColorSpace;
1389
- if (!GLTexture._supportRenderBufferColorFormat(format, rhi)) {
1358
+ if (!rhi.supportsRenderTargetColorFormat(format)) {
1390
1359
  throw new Error("TextureFormat is not supported:" + engineCore.TextureFormat[format] + " in RenderTarget");
1391
1360
  }
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");
1361
+ if (isSRGBColorSpace && format === engineCore.TextureFormat.R8G8B8_UNorm) {
1362
+ throw new Error("If you want to use sRGB color space, only R8G8B8A8_UNorm format is supported in RenderTarget");
1394
1363
  }
1395
1364
  }
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
1365
  if (_colorTextures.some(function(v) {
1403
1366
  return v.width !== width || v.height !== height;
1404
1367
  })) {
1405
1368
  throw new Error("ColorTexture's size must as same as RenderTarget");
1406
1369
  }
1407
- if (isDepthTexture && (_depth.width !== width || _depth.height !== height)) {
1370
+ if (_instanceof(_depth, engineCore.Texture) && (_depth.width !== width || _depth.height !== height)) {
1408
1371
  throw new Error("DepthTexture's size must as same as RenderTarget");
1409
1372
  }
1410
1373
  // todo: necessary to support MRT + Cube + [,MSAA] ?
@@ -1417,7 +1380,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1417
1380
  // bind main FBO
1418
1381
  this._bindMainFBO();
1419
1382
  // bind MSAA FBO
1420
- if (target.antiAliasing > 1) {
1383
+ if (this._antiAliasing > 1) {
1421
1384
  this._MSAAFrameBuffer = this._gl.createFramebuffer();
1422
1385
  this._bindMSAAFBO();
1423
1386
  }
@@ -1437,22 +1400,20 @@ var GLBuffer = /*#__PURE__*/ function() {
1437
1400
  if (colorTexture) {
1438
1401
  var isCube = _instanceof(colorTexture, engineCore.TextureCube);
1439
1402
  if (mipChanged || isCube && faceChanged) {
1440
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, isCube ? gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex : gl.TEXTURE_2D, // @ts-ignore
1441
- colorTexture._platformTexture._glTexture, mipLevel);
1403
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, isCube ? gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex : gl.TEXTURE_2D, colorTexture._platformTexture._glTexture, mipLevel);
1442
1404
  }
1443
1405
  }
1444
1406
  var depthTexture = target.depthTexture;
1445
1407
  if (depthTexture) {
1446
1408
  var isCube1 = _instanceof(depthTexture, engineCore.TextureCube);
1447
1409
  if (mipChanged || isCube1) {
1448
- // @ts-ignore
1449
1410
  var platformTexture = depthTexture._platformTexture;
1450
1411
  gl.framebufferTexture2D(gl.FRAMEBUFFER, platformTexture._formatDetail.attachment, isCube1 ? gl.TEXTURE_CUBE_MAP_POSITIVE_X + faceIndex : gl.TEXTURE_2D, platformTexture._glTexture, mipLevel);
1451
1412
  }
1452
1413
  } else {
1453
1414
  if (mipChanged) {
1454
1415
  // @ts-ignore
1455
- var internalFormat = GLTexture._getRenderBufferDepthFormatDetail(target._depth, gl, this._isWebGL2).internalFormat;
1416
+ var internalFormat = GLTexture._getRenderBufferDepthFormatDetail(target._depth, gl).internalFormat;
1456
1417
  gl.bindRenderbuffer(gl.RENDERBUFFER, this._depthRenderBuffer);
1457
1418
  gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, target.width >> mipLevel, target.height >> mipLevel);
1458
1419
  }
@@ -1501,8 +1462,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1501
1462
  };
1502
1463
  _proto._bindMainFBO = function _bindMainFBO() {
1503
1464
  var gl = this._gl;
1504
- var isWebGL2 = this._isWebGL2;
1505
- /** @ts-ignore */ var _this__target = this._target, _depth = _this__target._depth, colorTextureCount = _this__target.colorTextureCount, width = _this__target.width, height = _this__target.height;
1465
+ var _this__target = this._target, _depth = _this__target._depth, colorTextureCount = _this__target.colorTextureCount; _this__target.width; _this__target.height;
1506
1466
  var drawBuffers = new Array(colorTextureCount);
1507
1467
  gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
1508
1468
  /** color render buffer */ for(var i = 0; i < colorTextureCount; i++){
@@ -1510,7 +1470,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1510
1470
  var attachment = gl.COLOR_ATTACHMENT0 + i;
1511
1471
  drawBuffers[i] = attachment;
1512
1472
  if (!_instanceof(colorTexture, engineCore.TextureCube)) {
1513
- gl.framebufferTexture2D(gl.FRAMEBUFFER, attachment, gl.TEXTURE_2D, /** @ts-ignore */ colorTexture._platformTexture._glTexture, 0);
1473
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, attachment, gl.TEXTURE_2D, colorTexture._platformTexture._glTexture, 0);
1514
1474
  }
1515
1475
  }
1516
1476
  if (colorTextureCount > 1) {
@@ -1518,17 +1478,14 @@ var GLBuffer = /*#__PURE__*/ function() {
1518
1478
  }
1519
1479
  this._oriDrawBuffers = drawBuffers;
1520
1480
  /** depth render buffer */ if (_depth !== null) {
1521
- if (_instanceof(_depth, engineCore.Texture) && !_instanceof(_depth, engineCore.TextureCube)) {
1522
- // @ts-ignore
1523
- var platformTexture = _depth._platformTexture;
1524
- gl.framebufferTexture2D(gl.FRAMEBUFFER, platformTexture._formatDetail.attachment, gl.TEXTURE_2D, platformTexture._glTexture, 0);
1525
- } else if (this._target.antiAliasing <= 1) {
1526
- var _GLTexture__getRenderBufferDepthFormatDetail = GLTexture._getRenderBufferDepthFormatDetail(_depth, gl, isWebGL2), internalFormat = _GLTexture__getRenderBufferDepthFormatDetail.internalFormat, attachment1 = _GLTexture__getRenderBufferDepthFormatDetail.attachment;
1527
- var depthRenderBuffer = gl.createRenderbuffer();
1528
- this._depthRenderBuffer = depthRenderBuffer;
1529
- gl.bindRenderbuffer(gl.RENDERBUFFER, depthRenderBuffer);
1530
- gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, width, height);
1531
- gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment1, gl.RENDERBUFFER, depthRenderBuffer);
1481
+ if (_instanceof(_depth, engineCore.Texture)) {
1482
+ if (!_instanceof(_depth, engineCore.TextureCube)) {
1483
+ var platformTexture = _depth._platformTexture;
1484
+ gl.framebufferTexture2D(gl.FRAMEBUFFER, platformTexture._formatDetail.attachment, gl.TEXTURE_2D, platformTexture._glTexture, 0);
1485
+ }
1486
+ } else if (this._antiAliasing <= 1) {
1487
+ var _GLTexture__getRenderBufferDepthFormatDetail = GLTexture._getRenderBufferDepthFormatDetail(_depth, gl), internalFormat = _GLTexture__getRenderBufferDepthFormatDetail.internalFormat, attachment1 = _GLTexture__getRenderBufferDepthFormatDetail.attachment;
1488
+ this._depthRenderBuffer = this._createRenderBuffer(internalFormat, attachment1);
1532
1489
  }
1533
1490
  }
1534
1491
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
@@ -1536,33 +1493,39 @@ var GLBuffer = /*#__PURE__*/ function() {
1536
1493
  };
1537
1494
  _proto._bindMSAAFBO = function _bindMSAAFBO() {
1538
1495
  var gl = this._gl;
1539
- var isWebGL2 = this._isWebGL2;
1540
- var MSAADepthRenderBuffer = gl.createRenderbuffer();
1541
- /** @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;
1496
+ var _this__target = this._target, _depth = _this__target._depth, colorTextureCount = _this__target.colorTextureCount;
1542
1497
  this._blitDrawBuffers = new Array(colorTextureCount);
1543
- this._MSAADepthRenderBuffer = MSAADepthRenderBuffer;
1544
1498
  gl.bindFramebuffer(gl.FRAMEBUFFER, this._MSAAFrameBuffer);
1545
1499
  // prepare MRT+MSAA color RBOs
1546
1500
  for(var i = 0; i < colorTextureCount; i++){
1547
- var MSAAColorRenderBuffer = gl.createRenderbuffer();
1548
- this._MSAAColorRenderBuffers[i] = MSAAColorRenderBuffer;
1549
1501
  this._blitDrawBuffers[i] = gl.NONE;
1550
- gl.bindRenderbuffer(gl.RENDERBUFFER, MSAAColorRenderBuffer);
1551
- gl.renderbufferStorageMultisample(gl.RENDERBUFFER, antiAliasing, /** @ts-ignore */ this._target.getColorTexture(i)._platformTexture._formatDetail.internalFormat, width, height);
1552
- gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.RENDERBUFFER, MSAAColorRenderBuffer);
1502
+ var internalFormat = this._target.getColorTexture(i)._platformTexture._formatDetail.internalFormat;
1503
+ this._MSAAColorRenderBuffers[i] = this._createRenderBuffer(internalFormat, gl.COLOR_ATTACHMENT0 + i);
1553
1504
  }
1554
1505
  gl.drawBuffers(this._oriDrawBuffers);
1555
1506
  // prepare MSAA depth RBO
1556
1507
  if (_depth !== null) {
1557
- var _ref = _instanceof(_depth, engineCore.Texture) ? /** @ts-ignore */ _depth._platformTexture._formatDetail : GLTexture._getRenderBufferDepthFormatDetail(_depth, gl, isWebGL2), internalFormat = _ref.internalFormat, attachment = _ref.attachment;
1558
- gl.bindRenderbuffer(gl.RENDERBUFFER, MSAADepthRenderBuffer);
1559
- gl.renderbufferStorageMultisample(gl.RENDERBUFFER, antiAliasing, internalFormat, width, height);
1560
- gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, MSAADepthRenderBuffer);
1508
+ var _ref = _instanceof(_depth, engineCore.Texture) ? _depth._platformTexture._formatDetail : GLTexture._getRenderBufferDepthFormatDetail(_depth, gl), internalFormat1 = _ref.internalFormat, attachment = _ref.attachment;
1509
+ this._MSAADepthRenderBuffer = this._createRenderBuffer(internalFormat1, attachment);
1561
1510
  }
1562
1511
  this._checkFrameBuffer();
1563
1512
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1564
1513
  gl.bindRenderbuffer(gl.RENDERBUFFER, null);
1565
1514
  };
1515
+ _proto._createRenderBuffer = function _createRenderBuffer(internalFormat, attachment) {
1516
+ var gl = this._gl;
1517
+ var _this__target = this._target, width = _this__target.width, height = _this__target.height;
1518
+ var antiAliasing = this._antiAliasing;
1519
+ var renderBuffer = gl.createRenderbuffer();
1520
+ gl.bindRenderbuffer(gl.RENDERBUFFER, renderBuffer);
1521
+ if (antiAliasing > 1) {
1522
+ gl.renderbufferStorageMultisample(gl.RENDERBUFFER, antiAliasing, internalFormat, width, height);
1523
+ } else {
1524
+ gl.renderbufferStorage(gl.RENDERBUFFER, internalFormat, width, height);
1525
+ }
1526
+ gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, renderBuffer);
1527
+ return renderBuffer;
1528
+ };
1566
1529
  _proto._checkFrameBuffer = function _checkFrameBuffer() {
1567
1530
  var gl = this._gl;
1568
1531
  var e = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
@@ -1592,12 +1555,11 @@ var GLBuffer = /*#__PURE__*/ function() {
1592
1555
  _inherits(GLTexture2D, GLTexture1);
1593
1556
  function GLTexture2D(rhi, texture2D) {
1594
1557
  var _this;
1595
- _this = GLTexture1.call(this, rhi, texture2D, rhi.gl.TEXTURE_2D) || this, /** Backward compatible with WebGL1.0. */ _this._compressedMipFilled = 0;
1596
- _this._validate(texture2D, rhi);
1558
+ _this = GLTexture1.call(this, rhi, texture2D, rhi.gl.TEXTURE_2D) || this;
1559
+ _this._validate(texture2D);
1597
1560
  var format = texture2D.format, isSRGBColorSpace = texture2D.isSRGBColorSpace;
1598
- var isWebGL2 = _this._isWebGL2;
1599
- _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl, isWebGL2);
1600
- _this._formatDetail.isCompressed && !isWebGL2 || _this._init(false);
1561
+ _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl);
1562
+ _this._init(false);
1601
1563
  return _this;
1602
1564
  }
1603
1565
  var _proto = GLTexture2D.prototype;
@@ -1613,15 +1575,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1613
1575
  height = height || mipHeight - y;
1614
1576
  this._bind();
1615
1577
  if (formatDetail.isCompressed) {
1616
- var isWebGL2 = this._isWebGL2;
1617
- var internalFormat = formatDetail.internalFormat;
1618
- var mipBit = 1 << mipLevel;
1619
- if (isWebGL2 || this._compressedMipFilled & mipBit) {
1620
- gl.compressedTexSubImage2D(this._target, mipLevel, x, y, width, height, internalFormat, colorBuffer);
1621
- } else {
1622
- gl.compressedTexImage2D(this._target, mipLevel, internalFormat, width, height, 0, colorBuffer);
1623
- this._compressedMipFilled |= mipBit;
1624
- }
1578
+ gl.compressedTexSubImage2D(this._target, mipLevel, x, y, width, height, formatDetail.internalFormat, colorBuffer);
1625
1579
  } else {
1626
1580
  var baseFormat = formatDetail.baseFormat, dataType = formatDetail.dataType;
1627
1581
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
@@ -1662,10 +1616,10 @@ var GLBuffer = /*#__PURE__*/ function() {
1662
1616
  function GLTexture2DArray(rhi, texture2DArray) {
1663
1617
  var _this;
1664
1618
  _this = GLTexture1.call(this, rhi, texture2DArray, rhi.gl.TEXTURE_2D_ARRAY) || this;
1665
- _this._validate(texture2DArray, rhi);
1619
+ _this._validate(texture2DArray);
1666
1620
  var format = texture2DArray.format, width = texture2DArray.width, height = texture2DArray.height, length = texture2DArray.length, mipmapCount = texture2DArray.mipmapCount, isSRGBColorSpace = texture2DArray.isSRGBColorSpace;
1667
1621
  _this._bind();
1668
- _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl, true);
1622
+ _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl);
1669
1623
  _this._gl.texStorage3D(_this._target, mipmapCount, _this._formatDetail.internalFormat, width, height, length);
1670
1624
  return _this;
1671
1625
  }
@@ -1680,12 +1634,12 @@ var GLBuffer = /*#__PURE__*/ function() {
1680
1634
  height = height || Math.max(1, this._texture.height >> mipLevel) - y;
1681
1635
  length = length || this._texture.length;
1682
1636
  this._bind();
1683
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1684
- gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1685
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1686
1637
  if (isCompressed) {
1687
1638
  gl.compressedTexSubImage3D(target, mipLevel, x, y, offsetIndex, width, height, length, internalFormat, colorBuffer);
1688
1639
  } else {
1640
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1641
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1642
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1689
1643
  gl.texSubImage3D(target, mipLevel, x, y, offsetIndex, width, height, length, baseFormat, dataType, colorBuffer);
1690
1644
  }
1691
1645
  };
@@ -1707,13 +1661,15 @@ var GLBuffer = /*#__PURE__*/ function() {
1707
1661
  if (formatDetail.isCompressed) {
1708
1662
  throw new Error("Unable to read compressed texture");
1709
1663
  }
1664
+ var readFormat = this._getReadFormat();
1710
1665
  gl.pixelStorei(gl.PACK_ALIGNMENT, formatDetail.alignment);
1711
1666
  gl.bindFramebuffer(gl.FRAMEBUFFER, this._getReadFrameBuffer());
1712
1667
  gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, this._glTexture, mipLevel, elementIndex);
1713
- gl.readPixels(x, y, width, height, formatDetail.baseFormat, formatDetail.dataType, out);
1668
+ var _formatDetail_readType;
1669
+ gl.readPixels(x, y, width, height, readFormat, (_formatDetail_readType = formatDetail.readType) != null ? _formatDetail_readType : formatDetail.dataType, out);
1714
1670
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1715
1671
  };
1716
- _proto._validate = function _validate(texture, rhi) {
1672
+ _proto._validate = function _validate(texture) {
1717
1673
  var format = texture.format;
1718
1674
  // Validate sRGB format
1719
1675
  // @ts-ignore
@@ -1733,19 +1689,11 @@ var GLBuffer = /*#__PURE__*/ function() {
1733
1689
  _inherits(GLTextureCube, GLTexture1);
1734
1690
  function GLTextureCube(rhi, textureCube) {
1735
1691
  var _this;
1736
- _this = GLTexture1.call(this, rhi, textureCube, rhi.gl.TEXTURE_CUBE_MAP) || this, /** Backward compatible with WebGL1.0. */ _this._compressedFaceFilled = [
1737
- 0,
1738
- 0,
1739
- 0,
1740
- 0,
1741
- 0,
1742
- 0
1743
- ];
1744
- _this._validate(textureCube, rhi);
1692
+ _this = GLTexture1.call(this, rhi, textureCube, rhi.gl.TEXTURE_CUBE_MAP) || this;
1693
+ _this._validate(textureCube);
1745
1694
  var format = textureCube.format, isSRGBColorSpace = textureCube.isSRGBColorSpace;
1746
- var isWebGL2 = _this._isWebGL2;
1747
- _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl, isWebGL2);
1748
- _this._formatDetail.isCompressed && !isWebGL2 || _this._init(true);
1695
+ _this._formatDetail = GLTexture._getFormatDetail(format, isSRGBColorSpace, _this._gl);
1696
+ _this._init(true);
1749
1697
  return _this;
1750
1698
  }
1751
1699
  var _proto = GLTextureCube.prototype;
@@ -1753,25 +1701,18 @@ var GLBuffer = /*#__PURE__*/ function() {
1753
1701
  * {@inheritDoc IPlatformTextureCube.setPixelBuffer}
1754
1702
  */ _proto.setPixelBuffer = function setPixelBuffer(face, colorBuffer, mipLevel, x, y, width, height) {
1755
1703
  var gl = this._gl;
1756
- var isWebGL2 = this._isWebGL2;
1757
1704
  var formatDetail = this._formatDetail;
1758
- var internalFormat = formatDetail.internalFormat, baseFormat = formatDetail.baseFormat, dataType = formatDetail.dataType, isCompressed = formatDetail.isCompressed;
1705
+ var baseFormat = formatDetail.baseFormat, dataType = formatDetail.dataType, isCompressed = formatDetail.isCompressed;
1759
1706
  var mipSize = Math.max(1, this._texture.width >> mipLevel);
1760
1707
  width = width || mipSize - x;
1761
1708
  height = height || mipSize - y;
1762
1709
  this._bind();
1763
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1764
- gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1765
- gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1766
1710
  if (isCompressed) {
1767
- var mipBit = 1 << mipLevel;
1768
- if (isWebGL2 || this._compressedFaceFilled[face] & mipBit) {
1769
- gl.compressedTexSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, x, y, width, height, internalFormat, colorBuffer);
1770
- } else {
1771
- gl.compressedTexImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, internalFormat, width, height, 0, colorBuffer);
1772
- this._compressedFaceFilled[face] |= mipBit;
1773
- }
1711
+ gl.compressedTexSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, x, y, width, height, formatDetail.internalFormat, colorBuffer);
1774
1712
  } else {
1713
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
1714
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
1715
+ gl.pixelStorei(gl.UNPACK_ALIGNMENT, formatDetail.alignment);
1775
1716
  gl.texSubImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, mipLevel, x, y, width, height, baseFormat, dataType, colorBuffer);
1776
1717
  }
1777
1718
  };
@@ -1797,18 +1738,150 @@ var GLBuffer = /*#__PURE__*/ function() {
1797
1738
  }(GLTexture);
1798
1739
 
1799
1740
  /**
1800
- * WebGL mode.
1801
- */ var WebGLMode = /*#__PURE__*/ function(WebGLMode) {
1802
- /** Auto, use WebGL2.0 if support, or will fallback to WebGL1.0. */ WebGLMode[WebGLMode["Auto"] = 0] = "Auto";
1803
- /** WebGL2.0. */ WebGLMode[WebGLMode["WebGL2"] = 1] = "WebGL2";
1804
- /** WebGL1.0, */ WebGLMode[WebGLMode["WebGL1"] = 2] = "WebGL1";
1805
- return WebGLMode;
1806
- }({});
1741
+ * @internal
1742
+ * WebGL2 implementation of Transform Feedback.
1743
+ */ var GLTransformFeedback = /*#__PURE__*/ function() {
1744
+ function GLTransformFeedback(rhi) {
1745
+ var gl = rhi.gl;
1746
+ this._gl = gl;
1747
+ this._glTransformFeedback = gl.createTransformFeedback();
1748
+ }
1749
+ var _proto = GLTransformFeedback.prototype;
1750
+ _proto.bindBufferRange = function bindBufferRange(index, buffer, byteOffset, byteSize) {
1751
+ var gl = this._gl;
1752
+ gl.bindBufferRange(gl.TRANSFORM_FEEDBACK_BUFFER, index, buffer._glBuffer, byteOffset, byteSize);
1753
+ };
1754
+ _proto.unbindBuffer = function unbindBuffer(index) {
1755
+ var gl = this._gl;
1756
+ gl.bindBufferBase(gl.TRANSFORM_FEEDBACK_BUFFER, index, null);
1757
+ };
1758
+ _proto.begin = function begin(primitiveMode) {
1759
+ this._gl.beginTransformFeedback(primitiveMode);
1760
+ };
1761
+ _proto.end = function end() {
1762
+ this._gl.endTransformFeedback();
1763
+ };
1764
+ _proto.bind = function bind() {
1765
+ var gl = this._gl;
1766
+ gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, this._glTransformFeedback);
1767
+ };
1768
+ _proto.unbind = function unbind() {
1769
+ var gl = this._gl;
1770
+ gl.bindTransformFeedback(gl.TRANSFORM_FEEDBACK, null);
1771
+ };
1772
+ _proto.destroy = function destroy() {
1773
+ if (this._glTransformFeedback) {
1774
+ this._gl.deleteTransformFeedback(this._glTransformFeedback);
1775
+ this._glTransformFeedback = null;
1776
+ }
1777
+ };
1778
+ return GLTransformFeedback;
1779
+ }();
1780
+
1807
1781
  /**
1808
- * WebGL graphic device, including WebGL1.0 and WebGL2.0.
1782
+ * @internal
1783
+ * WebGL2 implementation of Transform Feedback primitive.
1784
+ * Maintains two VAOs (one per read direction), rebuilding when the program or input bindings change.
1785
+ */ var GLTransformFeedbackPrimitive = /*#__PURE__*/ function() {
1786
+ function GLTransformFeedbackPrimitive(gl) {
1787
+ this._lastProgramId = -1;
1788
+ this._lastInputBindings = [];
1789
+ this._gl = gl;
1790
+ }
1791
+ var _proto = GLTransformFeedbackPrimitive.prototype;
1792
+ _proto.bind = function bind(program, bindingA, bindingB, feedbackElements, inputBindings, inputElements, readIsA) {
1793
+ var layoutChanged = program.id !== this._lastProgramId;
1794
+ var lastInputBindings = this._lastInputBindings;
1795
+ if (!layoutChanged) {
1796
+ if (inputBindings.length !== lastInputBindings.length) {
1797
+ layoutChanged = true;
1798
+ } else {
1799
+ for(var i = 0, n = inputBindings.length; i < n; i++){
1800
+ if (inputBindings[i] !== lastInputBindings[i]) {
1801
+ layoutChanged = true;
1802
+ break;
1803
+ }
1804
+ }
1805
+ }
1806
+ }
1807
+ if (layoutChanged) {
1808
+ this._deleteVAOs();
1809
+ var attribs = program.attributeLocation;
1810
+ this._vaoA = this._createVAO(attribs, bindingA, feedbackElements, inputBindings, inputElements);
1811
+ this._vaoB = this._createVAO(attribs, bindingB, feedbackElements, inputBindings, inputElements);
1812
+ this._lastProgramId = program.id;
1813
+ lastInputBindings.length = inputBindings.length;
1814
+ for(var i1 = 0, n1 = inputBindings.length; i1 < n1; i1++){
1815
+ lastInputBindings[i1] = inputBindings[i1];
1816
+ }
1817
+ this._gl.bindVertexArray(null);
1818
+ }
1819
+ this._gl.bindVertexArray(readIsA ? this._vaoA : this._vaoB);
1820
+ };
1821
+ _proto.unbind = function unbind() {
1822
+ this._gl.bindVertexArray(null);
1823
+ };
1824
+ _proto.draw = function draw(mode, first, count) {
1825
+ this._gl.drawArrays(mode, first, count);
1826
+ };
1827
+ _proto.invalidate = function invalidate() {
1828
+ this._deleteVAOs();
1829
+ };
1830
+ _proto.destroy = function destroy() {
1831
+ this._deleteVAOs();
1832
+ };
1833
+ _proto._deleteVAOs = function _deleteVAOs() {
1834
+ var gl = this._gl;
1835
+ if (this._vaoA) {
1836
+ gl.deleteVertexArray(this._vaoA);
1837
+ this._vaoA = null;
1838
+ }
1839
+ if (this._vaoB) {
1840
+ gl.deleteVertexArray(this._vaoB);
1841
+ this._vaoB = null;
1842
+ }
1843
+ this._lastProgramId = -1;
1844
+ this._lastInputBindings.length = 0;
1845
+ };
1846
+ _proto._createVAO = function _createVAO(attribs, feedbackBinding, feedbackElements, inputBindings, inputElements) {
1847
+ var gl = this._gl;
1848
+ var vao = gl.createVertexArray();
1849
+ gl.bindVertexArray(vao);
1850
+ gl.bindBuffer(gl.ARRAY_BUFFER, feedbackBinding.buffer._platformBuffer._glBuffer);
1851
+ for(var i = 0, n = feedbackElements.length; i < n; i++){
1852
+ this._bindElement(attribs, feedbackElements[i], feedbackBinding.stride);
1853
+ }
1854
+ var lastInputBinding;
1855
+ for(var i1 = 0, n1 = inputElements.length; i1 < n1; i1++){
1856
+ var element = inputElements[i1];
1857
+ var inputBinding = inputBindings[element.bindingIndex];
1858
+ if (inputBinding !== lastInputBinding) {
1859
+ gl.bindBuffer(gl.ARRAY_BUFFER, inputBinding.buffer._platformBuffer._glBuffer);
1860
+ lastInputBinding = inputBinding;
1861
+ }
1862
+ this._bindElement(attribs, element, inputBinding.stride);
1863
+ }
1864
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
1865
+ return vao;
1866
+ };
1867
+ _proto._bindElement = function _bindElement(attribs, element, stride) {
1868
+ var gl = this._gl;
1869
+ var loc = attribs[element.attribute];
1870
+ if (loc !== undefined && loc !== -1) {
1871
+ var info = element._formatMetaInfo;
1872
+ gl.enableVertexAttribArray(loc);
1873
+ gl.vertexAttribPointer(loc, info.size, info.type, info.normalized, stride, element.offset);
1874
+ }
1875
+ };
1876
+ return GLTransformFeedbackPrimitive;
1877
+ }();
1878
+
1879
+ /**
1880
+ * WebGL graphic device, using WebGL2.0.
1809
1881
  */ var WebGLGraphicDevice = /*#__PURE__*/ function() {
1810
1882
  function WebGLGraphicDevice(initializeOptions) {
1811
1883
  if (initializeOptions === void 0) initializeOptions = {};
1884
+ this.supportsRendering = true;
1812
1885
  /** @internal */ this._readFrameBuffer = null;
1813
1886
  /** @internal */ this._mainFrameBuffer = null;
1814
1887
  /** @internal */ this._mainFrameWidth = 0;
@@ -1820,7 +1893,6 @@ var GLBuffer = /*#__PURE__*/ function() {
1820
1893
  this._lastClearColor = new engineMath.Color(null, null, null, null);
1821
1894
  this._scissorEnable = false;
1822
1895
  var options = _extends({
1823
- webGLMode: 0,
1824
1896
  _forceFlush: false,
1825
1897
  _maxAllowSkinUniformVectorCount: 256,
1826
1898
  alpha: true,
@@ -1863,40 +1935,26 @@ var GLBuffer = /*#__PURE__*/ function() {
1863
1935
  var _proto = WebGLGraphicDevice.prototype;
1864
1936
  _proto.init = function init(canvas, onDeviceLost, onDeviceRestored) {
1865
1937
  var webCanvas = canvas._webCanvas;
1866
- var webGLMode = this._options.webGLMode;
1867
- this._onDeviceLost = onDeviceLost;
1868
- this._onDeviceRestored = onDeviceRestored;
1869
- webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1870
- webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1871
1938
  webCanvas.addEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1872
- this._webCanvas = webCanvas;
1873
- var webGLOptions = this._webGLOptions;
1874
- var gl;
1875
- if (webGLMode == 0 || webGLMode == 1) {
1876
- gl = webCanvas.getContext("webgl2", webGLOptions);
1877
- if (!gl && (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas))) {
1939
+ try {
1940
+ var webGLOptions = this._webGLOptions;
1941
+ var gl = webCanvas.getContext("webgl2", webGLOptions);
1942
+ if (!gl && !canvas._isOffscreenCanvas()) {
1878
1943
  gl = webCanvas.getContext("experimental-webgl2", webGLOptions);
1879
1944
  }
1880
- this._isWebGL2 = true;
1881
- // Prevent weird browsers to lie (such as safari!)ƒ
1882
- if (gl && !gl.deleteQuery) {
1883
- this._isWebGL2 = false;
1884
- }
1885
- }
1886
- if (!gl) {
1887
- if (webGLMode == 0 || webGLMode == 2) {
1888
- gl = webCanvas.getContext("webgl", webGLOptions);
1889
- if (!gl && (typeof OffscreenCanvas === "undefined" || !_instanceof(webCanvas, OffscreenCanvas))) {
1890
- gl = webCanvas.getContext("experimental-webgl", webGLOptions);
1891
- }
1892
- this._isWebGL2 = false;
1945
+ if (!gl || !gl.deleteQuery) {
1946
+ throw new Error("WebGL2 is not supported.");
1893
1947
  }
1948
+ this._gl = gl;
1949
+ this._initGLState(gl);
1950
+ } finally{
1951
+ webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1894
1952
  }
1895
- if (!gl) {
1896
- throw new Error("Get GL Context FAILED.");
1897
- }
1898
- this._gl = gl;
1899
- this._initGLState(gl);
1953
+ this._webCanvas = webCanvas;
1954
+ this._onDeviceLost = onDeviceLost;
1955
+ this._onDeviceRestored = onDeviceRestored;
1956
+ webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1957
+ webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1900
1958
  };
1901
1959
  _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1902
1960
  return new GLPrimitive(this, primitive);
@@ -1917,6 +1975,45 @@ var GLBuffer = /*#__PURE__*/ function() {
1917
1975
  if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
1918
1976
  return new GLBuffer(this, type, byteLength, bufferUsage, data);
1919
1977
  };
1978
+ _proto.createPlatformTransformFeedback = function createPlatformTransformFeedback() {
1979
+ return new GLTransformFeedback(this);
1980
+ };
1981
+ _proto.createPlatformTransformFeedbackPrimitive = function createPlatformTransformFeedbackPrimitive() {
1982
+ return new GLTransformFeedbackPrimitive(this._gl);
1983
+ };
1984
+ _proto.bindUniformBufferBase = function bindUniformBufferBase(bindingPoint, buffer) {
1985
+ var gl = this._gl;
1986
+ gl.bindBufferBase(gl.UNIFORM_BUFFER, bindingPoint, buffer._glBuffer);
1987
+ };
1988
+ _proto.bindUniformBlock = function bindUniformBlock(program, blockName, bindingPoint) {
1989
+ var gl = this._gl;
1990
+ var blockIndex = gl.getUniformBlockIndex(program, blockName);
1991
+ if (blockIndex !== gl.INVALID_INDEX) {
1992
+ gl.uniformBlockBinding(program, blockIndex, bindingPoint);
1993
+ }
1994
+ return blockIndex;
1995
+ };
1996
+ /**
1997
+ * Enable GL_RASTERIZER_DISCARD (WebGL2 only).
1998
+ */ _proto.enableRasterizerDiscard = function enableRasterizerDiscard() {
1999
+ var gl = this._gl;
2000
+ gl.enable(gl.RASTERIZER_DISCARD);
2001
+ };
2002
+ /**
2003
+ * Disable GL_RASTERIZER_DISCARD (WebGL2 only).
2004
+ */ _proto.disableRasterizerDiscard = function disableRasterizerDiscard() {
2005
+ var gl = this._gl;
2006
+ gl.disable(gl.RASTERIZER_DISCARD);
2007
+ };
2008
+ /**
2009
+ * Invalidate the cached shader program state.
2010
+ * Call this after using a custom program (e.g., Transform Feedback) outside the engine's pipeline.
2011
+ */ _proto.invalidateShaderProgramState = function invalidateShaderProgramState() {
2012
+ if (this._currentBindShaderProgram) {
2013
+ this._gl.useProgram(null);
2014
+ this._currentBindShaderProgram = null;
2015
+ }
2016
+ };
1920
2017
  _proto.requireExtension = function requireExtension(ext) {
1921
2018
  return this._extensions.requireExtension(ext);
1922
2019
  };
@@ -1926,6 +2023,12 @@ var GLBuffer = /*#__PURE__*/ function() {
1926
2023
  _proto.canIUseCompressedTextureInternalFormat = function canIUseCompressedTextureInternalFormat(type) {
1927
2024
  return this.capability.canIUseCompressedTextureInternalFormat(type);
1928
2025
  };
2026
+ _proto.supportsTextureFormat = function supportsTextureFormat(format) {
2027
+ return this.capability.supportsTextureFormat(format);
2028
+ };
2029
+ _proto.supportsRenderTargetColorFormat = function supportsRenderTargetColorFormat(format) {
2030
+ return this.capability.supportsRenderTargetColorFormat(format);
2031
+ };
1929
2032
  _proto.viewport = function viewport(x, y, width, height) {
1930
2033
  var _this = this, gl = _this._gl, lastViewport = _this._lastViewport;
1931
2034
  if (x !== lastViewport.x || y !== lastViewport.y || width !== lastViewport.z || height !== lastViewport.w) {
@@ -1990,7 +2093,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1990
2093
  };
1991
2094
  _proto.drawPrimitive = function drawPrimitive(primitive, subPrimitive, shaderProgram) {
1992
2095
  // todo: VAO not support morph animation
1993
- if (primitive) {
2096
+ if (primitive && subPrimitive) {
1994
2097
  primitive.draw(shaderProgram, subPrimitive);
1995
2098
  } else {
1996
2099
  engineCore.Logger.error("draw primitive failed.");
@@ -2005,8 +2108,8 @@ var GLBuffer = /*#__PURE__*/ function() {
2005
2108
  _proto.activeRenderTarget = function activeRenderTarget(renderTarget, viewport, isFlipProjection, mipLevel, faceIndex) {
2006
2109
  var bufferWidth, bufferHeight;
2007
2110
  if (renderTarget) {
2008
- /** @ts-ignore */ renderTarget._isContentLost = false;
2009
- /** @ts-ignore */ var platformRenderTarget = renderTarget._platformRenderTarget;
2111
+ renderTarget._isContentLost = false;
2112
+ var platformRenderTarget = renderTarget._platformRenderTarget;
2010
2113
  platformRenderTarget.activeRenderTarget(mipLevel, faceIndex);
2011
2114
  bufferWidth = renderTarget.width >> mipLevel;
2012
2115
  bufferHeight = renderTarget.height >> mipLevel;
@@ -2024,10 +2127,6 @@ var GLBuffer = /*#__PURE__*/ function() {
2024
2127
  this.scissor(x, y, width, height);
2025
2128
  };
2026
2129
  _proto.blitInternalRTByBlitFrameBuffer = function blitInternalRTByBlitFrameBuffer(srcRT, destRT, clearFlags, viewport) {
2027
- if (!this._isWebGL2) {
2028
- engineCore.Logger.warn("WebGL1.0 not support blit frame buffer.");
2029
- return;
2030
- }
2031
2130
  var gl = this._gl;
2032
2131
  // @ts-ignore
2033
2132
  var srcFrameBuffer = srcRT ? srcRT._platformRenderTarget._frameBuffer : null;
@@ -2047,7 +2146,6 @@ var GLBuffer = /*#__PURE__*/ function() {
2047
2146
  gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, destFrameBuffer);
2048
2147
  var blitMask = needBlitColor ? gl.COLOR_BUFFER_BIT : 0;
2049
2148
  if (needBlitDepth || needBlitStencil) {
2050
- // @ts-ignore
2051
2149
  var depthFormat = destRT._depthFormat;
2052
2150
  if (needBlitDepth) {
2053
2151
  if (depthFormat === engineCore.TextureFormat.Depth || depthFormat >= engineCore.TextureFormat.DepthStencil && depthFormat <= engineCore.TextureFormat.Depth32Stencil8) {
@@ -2084,11 +2182,9 @@ var GLBuffer = /*#__PURE__*/ function() {
2084
2182
  var _srcRT__platformRenderTarget__frameBuffer;
2085
2183
  // @ts-ignore
2086
2184
  var frameBuffer = (_srcRT__platformRenderTarget__frameBuffer = srcRT == null ? void 0 : srcRT._platformRenderTarget._frameBuffer) != null ? _srcRT__platformRenderTarget__frameBuffer : this._mainFrameBuffer;
2087
- // @ts-ignore
2088
2185
  gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
2089
- // @ts-ignore
2090
2186
  var glTexture = grabTexture._platformTexture;
2091
- glTexture._bind();
2187
+ this.bindTexture(glTexture);
2092
2188
  gl.copyTexSubImage2D(glTexture._target, 0, 0, 0, xStart, yStart, copyWidth, copyHeight);
2093
2189
  };
2094
2190
  _proto.activeTexture = function activeTexture(textureID) {
@@ -2097,7 +2193,8 @@ var GLBuffer = /*#__PURE__*/ function() {
2097
2193
  this._activeTextureID = textureID;
2098
2194
  }
2099
2195
  };
2100
- _proto.bindTexture = function bindTexture(texture) {
2196
+ _proto.bindTexture = function bindTexture(platformTexture) {
2197
+ var texture = platformTexture;
2101
2198
  var index = this._activeTextureID - this._gl.TEXTURE0;
2102
2199
  if (this._activeTextures[index] !== texture) {
2103
2200
  this._gl.bindTexture(texture._target, texture._glTexture);
@@ -2157,10 +2254,10 @@ var GLBuffer = /*#__PURE__*/ function() {
2157
2254
  if (debugRenderInfo != null) {
2158
2255
  this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
2159
2256
  }
2257
+ this.maxUniformBlockSize = gl.getParameter(gl.MAX_UNIFORM_BLOCK_SIZE);
2160
2258
  };
2161
2259
  _proto.destroy = function destroy() {
2162
2260
  var webCanvas = this._webCanvas;
2163
- webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
2164
2261
  webCanvas.removeEventListener("webglcontextlost", this._onWebGLContextLost, false);
2165
2262
  webCanvas.removeEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
2166
2263
  };
@@ -2175,12 +2272,6 @@ var GLBuffer = /*#__PURE__*/ function() {
2175
2272
  this._onDeviceRestored();
2176
2273
  };
2177
2274
  _create_class(WebGLGraphicDevice, [
2178
- {
2179
- key: "isWebGL2",
2180
- get: function get() {
2181
- return this._isWebGL2;
2182
- }
2183
- },
2184
2275
  {
2185
2276
  key: "renderer",
2186
2277
  get: function get() {
@@ -2191,7 +2282,7 @@ var GLBuffer = /*#__PURE__*/ function() {
2191
2282
  key: "gl",
2192
2283
  get: /**
2193
2284
  * GL Context
2194
- * @member {WebGLRenderingContext}
2285
+ * @member {WebGL2RenderingContext}
2195
2286
  */ function get() {
2196
2287
  return this._gl;
2197
2288
  }
@@ -2207,12 +2298,6 @@ var GLBuffer = /*#__PURE__*/ function() {
2207
2298
  get: function get() {
2208
2299
  return this._capability;
2209
2300
  }
2210
- },
2211
- {
2212
- key: "canIUseMoreJoints",
2213
- get: function get() {
2214
- return this.capability.canIUseMoreJoints;
2215
- }
2216
2301
  }
2217
2302
  ]);
2218
2303
  return WebGLGraphicDevice;
@@ -2222,5 +2307,4 @@ exports.GLCompressedTextureInternalFormat = GLCompressedTextureInternalFormat;
2222
2307
  exports.WebCanvas = WebCanvas;
2223
2308
  exports.WebGLEngine = WebGLEngine;
2224
2309
  exports.WebGLGraphicDevice = WebGLGraphicDevice;
2225
- exports.WebGLMode = WebGLMode;
2226
2310
  //# sourceMappingURL=main.js.map