@galacean/engine-rhi-webgl 1.0.0 → 1.1.0-alpha.1

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
@@ -61,6 +61,11 @@ var engineCore = require('@galacean/engine-core');
61
61
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT1_EXT"] = 0x83f1] = "RGBA_S3TC_DXT1_EXT";
62
62
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT3_EXT"] = 0x83f2] = "RGBA_S3TC_DXT3_EXT";
63
63
  GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGBA_S3TC_DXT5_EXT"] = 0x83f3] = "RGBA_S3TC_DXT5_EXT";
64
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat[// bptc
65
+ "RGBA_BPTC_UNORM_EXT"] = 0x8e8c] = "RGBA_BPTC_UNORM_EXT";
66
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["SRGB_ALPHA_BPTC_UNORM_EXT"] = 0x8e8d] = "SRGB_ALPHA_BPTC_UNORM_EXT";
67
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB_BPTC_SIGNED_FLOAT_EXT"] = 0x8e8e] = "RGB_BPTC_SIGNED_FLOAT_EXT";
68
+ GLCompressedTextureInternalFormat[GLCompressedTextureInternalFormat["RGB_BPTC_UNSIGNED_FLOAT_EXT"] = 0x8e8f] = "RGB_BPTC_UNSIGNED_FLOAT_EXT";
64
69
  })(exports.GLCompressedTextureInternalFormat || (exports.GLCompressedTextureInternalFormat = {}));
65
70
 
66
71
  function _defineProperties(target, props) {
@@ -191,6 +196,43 @@ function _inherits(subClass, superClass) {
191
196
  if (superClass) _set_prototype_of(subClass, superClass);
192
197
  }
193
198
 
199
+ /**
200
+ * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
201
+ */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
202
+ _inherits(WebGLEngine, Engine);
203
+ function WebGLEngine() {
204
+ return Engine.apply(this, arguments);
205
+ }
206
+ /**
207
+ * Create a WebGL engine.
208
+ * @param configuration - WebGL engine configuration
209
+ * @returns A promise that will resolve when the engine is created
210
+ */ WebGLEngine.create = function create(configuration) {
211
+ var canvas = configuration.canvas;
212
+ var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
213
+ var webGLGraphicDevice = new WebGLGraphicDevice(configuration.graphicDeviceOptions);
214
+ var engine = new WebGLEngine(webCanvas, webGLGraphicDevice, configuration);
215
+ // @ts-ignore
216
+ var promise = engine._initialize(configuration);
217
+ return promise.then(function() {
218
+ engine.sceneManager.addScene(new engineCore.Scene(engine, "DefaultScene"));
219
+ return engine;
220
+ });
221
+ };
222
+ _create_class(WebGLEngine, [
223
+ {
224
+ key: "canvas",
225
+ get: /**
226
+ * Web canvas.
227
+ */ function get() {
228
+ // @ts-ignore
229
+ return this._canvas;
230
+ }
231
+ }
232
+ ]);
233
+ return WebGLEngine;
234
+ }(engineCore.Engine);
235
+
194
236
  function _extends() {
195
237
  _extends = Object.assign || function assign(target) {
196
238
  for (var i = 1; i < arguments.length; i++) {
@@ -204,6 +246,80 @@ function _extends() {
204
246
  return _extends.apply(this, arguments);
205
247
  }
206
248
 
249
+ var GLBuffer = /*#__PURE__*/ function() {
250
+ function GLBuffer(rhi, type, byteLength, bufferUsage, data) {
251
+ if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
252
+ var gl = rhi.gl;
253
+ var glBuffer = gl.createBuffer();
254
+ var glBufferUsage = this._getGLBufferUsage(gl, bufferUsage);
255
+ var glBindTarget = type === engineCore.BufferBindFlag.VertexBuffer ? gl.ARRAY_BUFFER : gl.ELEMENT_ARRAY_BUFFER;
256
+ this._gl = gl;
257
+ this._glBuffer = glBuffer;
258
+ this._glBufferUsage = glBufferUsage;
259
+ this._glBindTarget = glBindTarget;
260
+ this._isWebGL2 = rhi.isWebGL2;
261
+ this.bind();
262
+ if (data) {
263
+ gl.bufferData(glBindTarget, data, glBufferUsage);
264
+ } else {
265
+ gl.bufferData(glBindTarget, byteLength, glBufferUsage);
266
+ }
267
+ gl.bindBuffer(glBindTarget, null);
268
+ }
269
+ var _proto = GLBuffer.prototype;
270
+ _proto.bind = function bind() {
271
+ this._gl.bindBuffer(this._glBindTarget, this._glBuffer);
272
+ };
273
+ _proto.setData = function setData(byteLength, data, bufferByteOffset, dataOffset, dataLength, options) {
274
+ var gl = this._gl;
275
+ var glBindTarget = this._glBindTarget;
276
+ this.bind();
277
+ if (options === engineCore.SetDataOptions.Discard) {
278
+ gl.bufferData(glBindTarget, byteLength, this._glBufferUsage);
279
+ }
280
+ // TypeArray is BYTES_PER_ELEMENT, unTypeArray is 1
281
+ var byteSize = data.BYTES_PER_ELEMENT || 1;
282
+ var dataByteLength = dataLength ? byteSize * dataLength : data.byteLength;
283
+ if (dataOffset !== 0 || dataByteLength < data.byteLength) {
284
+ var isArrayBufferView = data.byteOffset !== undefined;
285
+ if (this._isWebGL2 && isArrayBufferView) {
286
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data, dataOffset, dataByteLength / byteSize);
287
+ } else {
288
+ var subData = new Uint8Array(isArrayBufferView ? data.buffer : data, dataOffset * byteSize, dataByteLength);
289
+ gl.bufferSubData(glBindTarget, bufferByteOffset, subData);
290
+ }
291
+ } else {
292
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data);
293
+ }
294
+ gl.bindBuffer(glBindTarget, null);
295
+ };
296
+ _proto.getData = function getData(data, bufferByteOffset, dataOffset, dataLength) {
297
+ if (this._isWebGL2) {
298
+ var gl = this._gl;
299
+ this.bind();
300
+ gl.getBufferSubData(this._glBindTarget, bufferByteOffset, data, dataOffset, dataLength);
301
+ } else {
302
+ throw "Buffer is write-only on WebGL1.0 platforms.";
303
+ }
304
+ };
305
+ _proto.destroy = function destroy() {
306
+ this._gl.deleteBuffer(this._glBuffer);
307
+ this._gl = null;
308
+ this._glBuffer = null;
309
+ };
310
+ _proto._getGLBufferUsage = function _getGLBufferUsage(gl, bufferUsage) {
311
+ switch(bufferUsage){
312
+ case engineCore.BufferUsage.Static:
313
+ return gl.STATIC_DRAW;
314
+ case engineCore.BufferUsage.Dynamic:
315
+ return gl.DYNAMIC_DRAW;
316
+ case engineCore.BufferUsage.Stream:
317
+ return gl.STREAM_DRAW;
318
+ }
319
+ };
320
+ return GLBuffer;
321
+ }();
322
+
207
323
  /**
208
324
  * GL capability.
209
325
  */ var GLCapability = /*#__PURE__*/ function() {
@@ -227,7 +343,8 @@ function _extends() {
227
343
  RGB_ETC1_WEBGL = exports.GLCompressedTextureInternalFormat.RGB_ETC1_WEBGL, // etc
228
344
  R11_EAC = exports.GLCompressedTextureInternalFormat.R11_EAC, SRGB8_ALPHA8_ETC2_EAC = exports.GLCompressedTextureInternalFormat.SRGB8_ALPHA8_ETC2_EAC, // pvrtc
229
345
  RGB_PVRTC_4BPPV1_IMG = exports.GLCompressedTextureInternalFormat.RGB_PVRTC_4BPPV1_IMG, RGBA_PVRTC_2BPPV1_IMG = exports.GLCompressedTextureInternalFormat.RGBA_PVRTC_2BPPV1_IMG, // s3tc
230
- RGB_S3TC_DXT1_EXT = exports.GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT, RGBA_S3TC_DXT5_EXT = exports.GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT;
346
+ RGB_S3TC_DXT1_EXT = exports.GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT, RGBA_S3TC_DXT5_EXT = exports.GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT, // bptc
347
+ RGBA_BPTC_UNORM_EXT = exports.GLCompressedTextureInternalFormat.RGBA_BPTC_UNORM_EXT, RGB_BPTC_UNSIGNED_FLOAT_EXT = exports.GLCompressedTextureInternalFormat.RGB_BPTC_UNSIGNED_FLOAT_EXT;
231
348
  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) {
232
349
  return this.canIUse(engineCore.GLCapabilityType.astc);
233
350
  } else if (internalType === RGB_ETC1_WEBGL) {
@@ -238,6 +355,8 @@ function _extends() {
238
355
  return this.canIUse(engineCore.GLCapabilityType.pvrtc);
239
356
  } else if (internalType >= RGB_S3TC_DXT1_EXT && internalType <= RGBA_S3TC_DXT5_EXT) {
240
357
  return this.canIUse(engineCore.GLCapabilityType.s3tc);
358
+ } else if (internalType >= RGBA_BPTC_UNORM_EXT && internalType <= RGB_BPTC_UNSIGNED_FLOAT_EXT) {
359
+ return this.canIUse(engineCore.GLCapabilityType.bptc);
241
360
  }
242
361
  return false;
243
362
  };
@@ -247,7 +366,7 @@ function _extends() {
247
366
  var cap = this.capabilityList;
248
367
  var isWebGL2 = this.rhi.isWebGL2;
249
368
  var requireExtension = this.rhi.requireExtension.bind(this.rhi);
250
- 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, astc = engineCore.GLCapabilityType.astc, astc_webkit = engineCore.GLCapabilityType.astc_webkit, 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, 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;
369
+ 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, astc = engineCore.GLCapabilityType.astc, astc_webkit = engineCore.GLCapabilityType.astc_webkit, 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, 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;
251
370
  cap.set(shaderVertexID, isWebGL2);
252
371
  cap.set(standardDerivatives, isWebGL2 || !!requireExtension(standardDerivatives));
253
372
  cap.set(shaderTextureLod, isWebGL2 || !!requireExtension(shaderTextureLod));
@@ -269,6 +388,7 @@ function _extends() {
269
388
  cap.set(etc1, !!(requireExtension(etc1) || requireExtension(etc1_webkit)));
270
389
  cap.set(pvrtc, !!(requireExtension(pvrtc) || requireExtension(pvrtc_webkit)));
271
390
  cap.set(s3tc, !!(requireExtension(s3tc) || requireExtension(s3tc_webkit)));
391
+ cap.set(bptc, !!requireExtension(bptc));
272
392
  };
273
393
  /**
274
394
  * If there are extensions that can supplement this ability, smooth out the difference.
@@ -473,8 +593,8 @@ function _extends() {
473
593
  if (useVao) {
474
594
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
475
595
  } else {
476
- var _nativeBuffer = _indexBufferBinding.buffer._nativeBuffer;
477
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer);
596
+ var _glBuffer = _indexBufferBinding.buffer._platformBuffer._glBuffer;
597
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer);
478
598
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
479
599
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
480
600
  }
@@ -487,8 +607,8 @@ function _extends() {
487
607
  if (useVao) {
488
608
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
489
609
  } else {
490
- var _nativeBuffer1 = _indexBufferBinding.buffer._nativeBuffer;
491
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer1);
610
+ var _glBuffer1 = _indexBufferBinding.buffer._platformBuffer._glBuffer;
611
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer1);
492
612
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
493
613
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
494
614
  }
@@ -528,14 +648,14 @@ function _extends() {
528
648
  var element = attributes[name];
529
649
  if (element) {
530
650
  var _vertexBufferBindings_element_bindingIndex = vertexBufferBindings[element.bindingIndex], buffer = _vertexBufferBindings_element_bindingIndex.buffer, stride = _vertexBufferBindings_element_bindingIndex.stride;
531
- vbo = buffer._nativeBuffer;
651
+ vbo = buffer._platformBuffer._glBuffer;
532
652
  // prevent binding the vbo which already bound at the last loop, e.g. a buffer with multiple attributes.
533
653
  if (lastBoundVbo !== vbo) {
534
654
  lastBoundVbo = vbo;
535
655
  gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
536
656
  }
537
657
  gl.enableVertexAttribArray(loc);
538
- var elementInfo = element._glElementInfo;
658
+ var elementInfo = element._formatMetaInfo;
539
659
  gl.vertexAttribPointer(loc, elementInfo.size, elementInfo.type, elementInfo.normalized, stride, element.offset);
540
660
  if (this._canUseInstancedArrays) {
541
661
  gl.vertexAttribDivisor(loc, element.instanceStepRate);
@@ -560,7 +680,7 @@ function _extends() {
560
680
  // @ts-ignore
561
681
  var _indexBufferBinding = this._primitive._indexBufferBinding;
562
682
  if (_indexBufferBinding) {
563
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._nativeBuffer);
683
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._platformBuffer._glBuffer);
564
684
  }
565
685
  this._bindBufferAndAttrib(shaderProgram);
566
686
  /** unbind */ gl.bindVertexArray(null);
@@ -667,9 +787,9 @@ function _extends() {
667
787
  var isWebGL2 = this._isWebGL2;
668
788
  var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
669
789
  // @ts-ignore
670
- var _this__texture = this._texture, mipmapCount = _this__texture.mipmapCount, width = _this__texture.width, height = _this__texture.height, _isDepthTexture = _this__texture._isDepthTexture;
790
+ 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;
671
791
  this._bind();
672
- if (isWebGL2 && !(baseFormat === gl.LUMINANCE_ALPHA || baseFormat === gl.ALPHA)) {
792
+ if (isWebGL2 && !(baseFormat === gl.LUMINANCE_ALPHA || baseFormat === gl.ALPHA) && usage !== engineCore.TextureUsage.Dynamic) {
673
793
  gl.texStorage2D(this._target, mipmapCount, internalFormat, width, height);
674
794
  } else {
675
795
  if (!isCube) {
@@ -816,16 +936,21 @@ function _extends() {
816
936
  dataType: gl.FLOAT,
817
937
  isCompressed: false
818
938
  };
819
- case engineCore.TextureFormat.DXT1:
939
+ case engineCore.TextureFormat.BC1:
820
940
  return {
821
941
  internalFormat: exports.GLCompressedTextureInternalFormat.RGB_S3TC_DXT1_EXT,
822
942
  isCompressed: true
823
943
  };
824
- case engineCore.TextureFormat.DXT5:
944
+ case engineCore.TextureFormat.BC3:
825
945
  return {
826
946
  internalFormat: exports.GLCompressedTextureInternalFormat.RGBA_S3TC_DXT5_EXT,
827
947
  isCompressed: true
828
948
  };
949
+ case engineCore.TextureFormat.BC7:
950
+ return {
951
+ internalFormat: exports.GLCompressedTextureInternalFormat.RGBA_BPTC_UNORM_EXT,
952
+ isCompressed: true
953
+ };
829
954
  case engineCore.TextureFormat.ETC1_RGB:
830
955
  return {
831
956
  internalFormat: exports.GLCompressedTextureInternalFormat.RGB_ETC1_WEBGL,
@@ -1464,11 +1589,15 @@ function _extends() {
1464
1589
  * {@inheritDoc IPlatformTexture2D.setImageSource}
1465
1590
  */ _proto.setImageSource = function setImageSource(imageSource, mipLevel, flipY, premultiplyAlpha, x, y) {
1466
1591
  var gl = this._gl;
1467
- var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1592
+ var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1468
1593
  this._bind();
1469
1594
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, +flipY);
1470
1595
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, +premultiplyAlpha);
1471
- gl.texSubImage2D(this._target, mipLevel, x || 0, y || 0, baseFormat, dataType, imageSource);
1596
+ if (this._texture.usage === engineCore.TextureUsage.Dynamic) {
1597
+ gl.texImage2D(this._target, mipLevel, internalFormat, baseFormat, dataType, imageSource);
1598
+ } else {
1599
+ gl.texSubImage2D(this._target, mipLevel, x || 0, y || 0, baseFormat, dataType, imageSource);
1600
+ }
1472
1601
  };
1473
1602
  /**
1474
1603
  * {@inheritDoc IPlatformTexture2D.getPixelBuffer }
@@ -1526,7 +1655,8 @@ function _extends() {
1526
1655
  this._bind();
1527
1656
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, +flipY);
1528
1657
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, +premultiplyAlpha);
1529
- gl.texSubImage3D(this._target, mipLevel, x, y, elementIndex, imageSource.width, imageSource.height, 1, baseFormat, dataType, imageSource);
1658
+ var _imageSource_width, _imageSource_height;
1659
+ gl.texSubImage3D(this._target, mipLevel, x, y, elementIndex, (_imageSource_width = imageSource.width) != null ? _imageSource_width : imageSource.codedWidth, (_imageSource_height = imageSource.height) != null ? _imageSource_height : imageSource.codedHeight, 1, baseFormat, dataType, imageSource);
1530
1660
  };
1531
1661
  /**
1532
1662
  * {@inheritDoc IPlatformTexture2DArray.getPixelBuffer}
@@ -1625,22 +1755,22 @@ exports.WebGLMode = void 0;
1625
1755
  WebGLMode[WebGLMode[/** WebGL1.0, */ "WebGL1"] = 2] = "WebGL1";
1626
1756
  })(exports.WebGLMode || (exports.WebGLMode = {}));
1627
1757
  /**
1628
- * WebGL renderer, including WebGL1.0 and WebGL2.0.
1629
- */ var WebGLRenderer = /*#__PURE__*/ function() {
1630
- function WebGLRenderer(initializeOptions) {
1758
+ * WebGL graphic device, including WebGL1.0 and WebGL2.0.
1759
+ */ var WebGLGraphicDevice = /*#__PURE__*/ function() {
1760
+ function WebGLGraphicDevice(initializeOptions) {
1631
1761
  if (initializeOptions === void 0) initializeOptions = {};
1762
+ /** @internal */ this._readFrameBuffer = null;
1632
1763
  /** @internal */ this._enableGlobalDepthBias = false;
1633
1764
  this._activeTextures = new Array(32);
1634
- // cache value
1635
1765
  this._lastViewport = new engineMath.Vector4(null, null, null, null);
1636
1766
  this._lastScissor = new engineMath.Vector4(null, null, null, null);
1637
1767
  this._lastClearColor = new engineMath.Color(null, null, null, null);
1638
1768
  this._scissorEnable = false;
1639
1769
  var options = _extends({
1640
1770
  webGLMode: 0,
1641
- alpha: false,
1642
1771
  stencil: true,
1643
- _forceFlush: false
1772
+ _forceFlush: false,
1773
+ _maxAllowSkinUniformVectorCount: 256
1644
1774
  }, initializeOptions);
1645
1775
  if (engineCore.SystemInfo.platform === engineCore.Platform.IPhone || engineCore.SystemInfo.platform === engineCore.Platform.IPad) {
1646
1776
  var version = engineCore.SystemInfo.operatingSystem.match(/(\d+).?(\d+)?.?(\d+)?/);
@@ -1653,12 +1783,20 @@ exports.WebGLMode = void 0;
1653
1783
  }
1654
1784
  }
1655
1785
  this._options = options;
1786
+ this._onWebGLContextLost = this._onWebGLContextLost.bind(this);
1787
+ this._onWebGLContextRestored = this._onWebGLContextRestored.bind(this);
1656
1788
  }
1657
- var _proto = WebGLRenderer.prototype;
1658
- _proto.init = function init(canvas) {
1789
+ var _proto = WebGLGraphicDevice.prototype;
1790
+ _proto.init = function init(canvas, onDeviceLost, onDeviceRestored) {
1659
1791
  var options = this._options;
1660
- var webCanvas = this._webCanvas = canvas._webCanvas;
1792
+ var webCanvas = canvas._webCanvas;
1661
1793
  var webGLMode = options.webGLMode;
1794
+ this._onDeviceLost = onDeviceLost;
1795
+ this._onDeviceRestored = onDeviceRestored;
1796
+ webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1797
+ webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1798
+ webCanvas.addEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1799
+ this._webCanvas = webCanvas;
1662
1800
  var gl;
1663
1801
  if (webGLMode == 0 || webGLMode == 1) {
1664
1802
  gl = webCanvas.getContext("webgl2", options);
@@ -1684,16 +1822,7 @@ exports.WebGLMode = void 0;
1684
1822
  throw new Error("Get GL Context FAILED.");
1685
1823
  }
1686
1824
  this._gl = gl;
1687
- this._activeTextureID = gl.TEXTURE0;
1688
- this._renderStates = new GLRenderStates(gl);
1689
- this._extensions = new GLExtensions(this);
1690
- this._capability = new GLCapability(this);
1691
- // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1692
- gl.activeTexture(gl.TEXTURE0);
1693
- var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1694
- if (debugRenderInfo != null) {
1695
- this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
1696
- }
1825
+ this._initGLState(gl);
1697
1826
  };
1698
1827
  _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1699
1828
  return new GLPrimitive(this, primitive);
@@ -1710,6 +1839,10 @@ exports.WebGLMode = void 0;
1710
1839
  _proto.createPlatformRenderTarget = function createPlatformRenderTarget(target) {
1711
1840
  return new GLRenderTarget(this, target);
1712
1841
  };
1842
+ _proto.createPlatformBuffer = function createPlatformBuffer(type, byteLength, bufferUsage, data) {
1843
+ if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
1844
+ return new GLBuffer(this, type, byteLength, bufferUsage, data);
1845
+ };
1713
1846
  _proto.requireExtension = function requireExtension(ext) {
1714
1847
  return this._extensions.requireExtension(ext);
1715
1848
  };
@@ -1792,23 +1925,23 @@ exports.WebGLMode = void 0;
1792
1925
  };
1793
1926
  _proto.activeRenderTarget = function activeRenderTarget(renderTarget, viewport, mipLevel) {
1794
1927
  var gl = this._gl;
1928
+ var bufferWidth, bufferHeight;
1795
1929
  if (renderTarget) {
1796
1930
  var /** @ts-ignore */ _renderTarget__platformRenderTarget;
1797
1931
  (_renderTarget__platformRenderTarget = renderTarget._platformRenderTarget) == null ? void 0 : _renderTarget__platformRenderTarget._activeRenderTarget();
1798
- var width = renderTarget.width >> mipLevel;
1799
- var height = renderTarget.height >> mipLevel;
1800
- this.viewport(0, 0, width, height);
1801
- this.scissor(0, 0, width, height);
1932
+ bufferWidth = renderTarget.width >> mipLevel;
1933
+ bufferHeight = renderTarget.height >> mipLevel;
1802
1934
  } else {
1803
1935
  gl.bindFramebuffer(gl.FRAMEBUFFER, null);
1804
- var drawingBufferWidth = gl.drawingBufferWidth, drawingBufferHeight = gl.drawingBufferHeight;
1805
- var width1 = drawingBufferWidth * viewport.z;
1806
- var height1 = drawingBufferHeight * viewport.w;
1807
- var x = viewport.x * drawingBufferWidth;
1808
- var y = drawingBufferHeight - viewport.y * drawingBufferHeight - height1;
1809
- this.viewport(x, y, width1, height1);
1810
- this.scissor(x, y, width1, height1);
1936
+ bufferWidth = gl.drawingBufferWidth;
1937
+ bufferHeight = gl.drawingBufferHeight;
1811
1938
  }
1939
+ var width = bufferWidth * viewport.z;
1940
+ var height = bufferHeight * viewport.w;
1941
+ var x = viewport.x * bufferWidth;
1942
+ var y = bufferHeight - viewport.y * bufferHeight - height;
1943
+ this.viewport(x, y, width, height);
1944
+ this.scissor(x, y, width, height);
1812
1945
  };
1813
1946
  _proto.activeTexture = function activeTexture(textureID) {
1814
1947
  if (this._activeTextureID !== textureID) {
@@ -1837,8 +1970,57 @@ exports.WebGLMode = void 0;
1837
1970
  _proto.flush = function flush() {
1838
1971
  this._gl.flush();
1839
1972
  };
1840
- _proto.destroy = function destroy() {};
1841
- _create_class(WebGLRenderer, [
1973
+ _proto.forceLoseDevice = function forceLoseDevice() {
1974
+ var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1975
+ extension.loseContext();
1976
+ };
1977
+ _proto.forceRestoreDevice = function forceRestoreDevice() {
1978
+ var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1979
+ extension.restoreContext();
1980
+ };
1981
+ _proto.resetState = function resetState() {
1982
+ this._readFrameBuffer = null;
1983
+ this._enableGlobalDepthBias = false;
1984
+ this._currentBindShaderProgram = null;
1985
+ var activeTextures = this._activeTextures;
1986
+ for(var i = 0, n = activeTextures.length; i < n; i++){
1987
+ activeTextures[i] = null;
1988
+ }
1989
+ this._lastViewport.set(null, null, null, null);
1990
+ this._lastScissor.set(null, null, null, null);
1991
+ this._lastClearColor.set(null, null, null, null);
1992
+ this._scissorEnable = false;
1993
+ this._initGLState(this._gl);
1994
+ };
1995
+ _proto._initGLState = function _initGLState(gl) {
1996
+ this._activeTextureID = gl.TEXTURE0;
1997
+ this._renderStates = new GLRenderStates(gl);
1998
+ this._extensions = new GLExtensions(this);
1999
+ this._capability = new GLCapability(this);
2000
+ // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
2001
+ gl.activeTexture(gl.TEXTURE0);
2002
+ var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
2003
+ if (debugRenderInfo != null) {
2004
+ this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
2005
+ }
2006
+ };
2007
+ _proto.destroy = function destroy() {
2008
+ var webCanvas = this._webCanvas;
2009
+ webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
2010
+ webCanvas.removeEventListener("webglcontextlost", this._onWebGLContextLost, false);
2011
+ webCanvas.removeEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
2012
+ };
2013
+ _proto._onContextCreationError = function _onContextCreationError(event) {
2014
+ console.error("WebGLRenderer: WebGL context could not be created. Reason: ", event.statusMessage);
2015
+ };
2016
+ _proto._onWebGLContextLost = function _onWebGLContextLost(event) {
2017
+ event.preventDefault();
2018
+ this._onDeviceLost();
2019
+ };
2020
+ _proto._onWebGLContextRestored = function _onWebGLContextRestored(event) {
2021
+ this._onDeviceRestored();
2022
+ };
2023
+ _create_class(WebGLGraphicDevice, [
1842
2024
  {
1843
2025
  key: "isWebGL2",
1844
2026
  get: function get() {
@@ -1879,32 +2061,10 @@ exports.WebGLMode = void 0;
1879
2061
  }
1880
2062
  }
1881
2063
  ]);
1882
- return WebGLRenderer;
2064
+ return WebGLGraphicDevice;
1883
2065
  }();
1884
2066
 
1885
- /**
1886
- * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
1887
- */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
1888
- _inherits(WebGLEngine, Engine);
1889
- function WebGLEngine(canvas, webGLRendererOptions) {
1890
- var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
1891
- var hardwareRenderer = new WebGLRenderer(webGLRendererOptions);
1892
- return Engine.call(this, webCanvas, hardwareRenderer);
1893
- }
1894
- _create_class(WebGLEngine, [
1895
- {
1896
- key: "canvas",
1897
- get: /**
1898
- * Web canvas.
1899
- */ function get() {
1900
- return this._canvas;
1901
- }
1902
- }
1903
- ]);
1904
- return WebGLEngine;
1905
- }(engineCore.Engine);
1906
-
1907
2067
  exports.WebCanvas = WebCanvas;
1908
2068
  exports.WebGLEngine = WebGLEngine;
1909
- exports.WebGLRenderer = WebGLRenderer;
2069
+ exports.WebGLGraphicDevice = WebGLGraphicDevice;
1910
2070
  //# sourceMappingURL=main.js.map