@galacean/engine-rhi-webgl 0.9.0 → 1.0.0-beta.0

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.
@@ -192,6 +192,37 @@ function _inherits(subClass, superClass) {
192
192
  if (superClass) _set_prototype_of(subClass, superClass);
193
193
  }
194
194
 
195
+ /**
196
+ * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
197
+ */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
198
+ _inherits(WebGLEngine, Engine);
199
+ function WebGLEngine() {
200
+ return Engine.apply(this, arguments);
201
+ }
202
+ /**
203
+ * Create a WebGL engine.
204
+ * @param configuration - WebGL engine configuration
205
+ * @returns A promise that will resolve when the engine is created
206
+ */ WebGLEngine.create = function create(configuration) {
207
+ var canvas = configuration.canvas;
208
+ var webCanvas = new WebCanvas(typeof canvas === "string" ? engineMiniprogramAdapter.document.getElementById(canvas) : canvas);
209
+ var webGLRenderer = new WebGLGraphicDevice(configuration.graphicDeviceOptions);
210
+ var engine = new WebGLEngine(webCanvas, webGLRenderer, configuration);
211
+ return engine._initialize(configuration);
212
+ };
213
+ _create_class(WebGLEngine, [
214
+ {
215
+ key: "canvas",
216
+ get: /**
217
+ * Web canvas.
218
+ */ function get() {
219
+ return this._canvas;
220
+ }
221
+ }
222
+ ]);
223
+ return WebGLEngine;
224
+ }(miniprogram$1.Engine);
225
+
195
226
  function _extends() {
196
227
  _extends = Object.assign || function assign(target) {
197
228
  for (var i = 1; i < arguments.length; i++) {
@@ -205,6 +236,84 @@ function _extends() {
205
236
  return _extends.apply(this, arguments);
206
237
  }
207
238
 
239
+ var GLBuffer = /*#__PURE__*/ function() {
240
+ function GLBuffer(rhi, type, byteLength, bufferUsage, data) {
241
+ if (bufferUsage === void 0) bufferUsage = miniprogram$1.BufferUsage.Static;
242
+ var gl = rhi.gl;
243
+ var glBuffer = gl.createBuffer();
244
+ var glBufferUsage = this._getGLBufferUsage(gl, bufferUsage);
245
+ var glBindTarget = type === miniprogram$1.BufferBindFlag.VertexBuffer ? gl.ARRAY_BUFFER : gl.ELEMENT_ARRAY_BUFFER;
246
+ this._gl = gl;
247
+ this._glBuffer = glBuffer;
248
+ this._glBufferUsage = glBufferUsage;
249
+ this._glBindTarget = glBindTarget;
250
+ this._isWebGL2 = rhi.isWebGL2;
251
+ this.bind();
252
+ if (data) {
253
+ gl.bufferData(glBindTarget, data, glBufferUsage);
254
+ } else {
255
+ gl.bufferData(glBindTarget, byteLength, glBufferUsage);
256
+ }
257
+ gl.bindBuffer(glBindTarget, null);
258
+ }
259
+ var _proto = GLBuffer.prototype;
260
+ _proto.bind = function bind() {
261
+ this._gl.bindBuffer(this._glBindTarget, this._glBuffer);
262
+ };
263
+ _proto.setData = function setData(byteLength, data, bufferByteOffset, dataOffset, dataLength, options) {
264
+ var gl = this._gl;
265
+ var glBindTarget = this._glBindTarget;
266
+ this.bind();
267
+ if (options === miniprogram$1.SetDataOptions.Discard) {
268
+ gl.bufferData(glBindTarget, byteLength, this._glBufferUsage);
269
+ }
270
+ // TypeArray is BYTES_PER_ELEMENT, unTypeArray is 1
271
+ var byteSize = data.BYTES_PER_ELEMENT || 1;
272
+ var dataByteLength = dataLength ? byteSize * dataLength : data.byteLength;
273
+ if (dataOffset !== 0 || dataByteLength < data.byteLength) {
274
+ var isArrayBufferView = data.byteOffset !== undefined;
275
+ if (this._isWebGL2 && isArrayBufferView) {
276
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data, dataOffset, dataByteLength / byteSize);
277
+ } else {
278
+ var subData = new Uint8Array(isArrayBufferView ? data.buffer : data, dataOffset * byteSize, dataByteLength);
279
+ gl.bufferSubData(glBindTarget, bufferByteOffset, subData);
280
+ }
281
+ } else {
282
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data);
283
+ }
284
+ gl.bindBuffer(glBindTarget, null);
285
+ };
286
+ _proto.getData = function getData(data, bufferByteOffset, dataOffset, dataLength) {
287
+ if (this._isWebGL2) {
288
+ var gl = this._gl;
289
+ this.bind();
290
+ gl.getBufferSubData(this._glBindTarget, bufferByteOffset, data, dataOffset, dataLength);
291
+ } else {
292
+ throw "Buffer is write-only on WebGL1.0 platforms.";
293
+ }
294
+ };
295
+ _proto.resize = function resize(byteLength) {
296
+ this.bind();
297
+ this._gl.bufferData(this._glBindTarget, byteLength, this._glBufferUsage);
298
+ };
299
+ _proto.destroy = function destroy() {
300
+ this._gl.deleteBuffer(this._glBuffer);
301
+ this._gl = null;
302
+ this._glBuffer = null;
303
+ };
304
+ _proto._getGLBufferUsage = function _getGLBufferUsage(gl, bufferUsage) {
305
+ switch(bufferUsage){
306
+ case miniprogram$1.BufferUsage.Static:
307
+ return gl.STATIC_DRAW;
308
+ case miniprogram$1.BufferUsage.Dynamic:
309
+ return gl.DYNAMIC_DRAW;
310
+ case miniprogram$1.BufferUsage.Stream:
311
+ return gl.STREAM_DRAW;
312
+ }
313
+ };
314
+ return GLBuffer;
315
+ }();
316
+
208
317
  /**
209
318
  * GL capability.
210
319
  */ var GLCapability = /*#__PURE__*/ function() {
@@ -474,8 +583,8 @@ function _extends() {
474
583
  if (useVao) {
475
584
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
476
585
  } else {
477
- var _nativeBuffer = _indexBufferBinding.buffer._nativeBuffer;
478
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer);
586
+ var _glBuffer = _indexBufferBinding.buffer._platformBuffer._glBuffer;
587
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer);
479
588
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
480
589
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
481
590
  }
@@ -488,8 +597,8 @@ function _extends() {
488
597
  if (useVao) {
489
598
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
490
599
  } else {
491
- var _nativeBuffer1 = _indexBufferBinding.buffer._nativeBuffer;
492
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer1);
600
+ var _glBuffer1 = _indexBufferBinding.buffer._platformBuffer._glBuffer;
601
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer1);
493
602
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
494
603
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
495
604
  }
@@ -529,7 +638,7 @@ function _extends() {
529
638
  var element = attributes[name];
530
639
  if (element) {
531
640
  var _vertexBufferBindings_element_bindingIndex = vertexBufferBindings[element.bindingIndex], buffer = _vertexBufferBindings_element_bindingIndex.buffer, stride = _vertexBufferBindings_element_bindingIndex.stride;
532
- vbo = buffer._nativeBuffer;
641
+ vbo = buffer._platformBuffer._glBuffer;
533
642
  // prevent binding the vbo which already bound at the last loop, e.g. a buffer with multiple attributes.
534
643
  if (lastBoundVbo !== vbo) {
535
644
  lastBoundVbo = vbo;
@@ -561,7 +670,7 @@ function _extends() {
561
670
  // @ts-ignore
562
671
  var _indexBufferBinding = this._primitive._indexBufferBinding;
563
672
  if (_indexBufferBinding) {
564
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._nativeBuffer);
673
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._platformBuffer._glBuffer);
565
674
  }
566
675
  this._bindBufferAndAttrib(shaderProgram);
567
676
  /** unbind */ gl.bindVertexArray(null);
@@ -1626,20 +1735,19 @@ exports.WebGLMode = void 0;
1626
1735
  WebGLMode[WebGLMode[/** WebGL1.0, */ "WebGL1"] = 2] = "WebGL1";
1627
1736
  })(exports.WebGLMode || (exports.WebGLMode = {}));
1628
1737
  /**
1629
- * WebGL renderer, including WebGL1.0 and WebGL2.0.
1630
- */ var WebGLRenderer = /*#__PURE__*/ function() {
1631
- function WebGLRenderer(initializeOptions) {
1738
+ * WebGL graphic device, including WebGL1.0 and WebGL2.0.
1739
+ */ var WebGLGraphicDevice = /*#__PURE__*/ function() {
1740
+ function WebGLGraphicDevice(initializeOptions) {
1632
1741
  if (initializeOptions === void 0) initializeOptions = {};
1742
+ /** @internal */ this._readFrameBuffer = null;
1633
1743
  /** @internal */ this._enableGlobalDepthBias = false;
1634
1744
  this._activeTextures = new Array(32);
1635
- // cache value
1636
1745
  this._lastViewport = new miniprogram.Vector4(null, null, null, null);
1637
1746
  this._lastScissor = new miniprogram.Vector4(null, null, null, null);
1638
1747
  this._lastClearColor = new miniprogram.Color(null, null, null, null);
1639
1748
  this._scissorEnable = false;
1640
1749
  var options = _extends({
1641
1750
  webGLMode: 0,
1642
- alpha: false,
1643
1751
  stencil: true,
1644
1752
  _forceFlush: false
1645
1753
  }, initializeOptions);
@@ -1654,12 +1762,20 @@ exports.WebGLMode = void 0;
1654
1762
  }
1655
1763
  }
1656
1764
  this._options = options;
1765
+ this._onWebGLContextLost = this._onWebGLContextLost.bind(this);
1766
+ this._onWebGLContextRestored = this._onWebGLContextRestored.bind(this);
1657
1767
  }
1658
- var _proto = WebGLRenderer.prototype;
1659
- _proto.init = function init(canvas) {
1768
+ var _proto = WebGLGraphicDevice.prototype;
1769
+ _proto.init = function init(canvas, onDeviceLost, onDeviceRestored) {
1660
1770
  var options = this._options;
1661
- var webCanvas = this._webCanvas = canvas._webCanvas;
1771
+ var webCanvas = canvas._webCanvas;
1662
1772
  var webGLMode = options.webGLMode;
1773
+ this._onDeviceLost = onDeviceLost;
1774
+ this._onDeviceRestored = onDeviceRestored;
1775
+ webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1776
+ webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1777
+ webCanvas.addEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1778
+ this._webCanvas = webCanvas;
1663
1779
  var gl;
1664
1780
  if (webGLMode == 0 || webGLMode == 1) {
1665
1781
  gl = webCanvas.getContext("webgl2", options);
@@ -1685,16 +1801,7 @@ exports.WebGLMode = void 0;
1685
1801
  throw new Error("Get GL Context FAILED.");
1686
1802
  }
1687
1803
  this._gl = gl;
1688
- this._activeTextureID = gl.TEXTURE0;
1689
- this._renderStates = new GLRenderStates(gl);
1690
- this._extensions = new GLExtensions(this);
1691
- this._capability = new GLCapability(this);
1692
- // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1693
- gl.activeTexture(gl.TEXTURE0);
1694
- var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1695
- if (debugRenderInfo != null) {
1696
- this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
1697
- }
1804
+ this._initGLState(gl);
1698
1805
  };
1699
1806
  _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1700
1807
  return new GLPrimitive(this, primitive);
@@ -1711,6 +1818,10 @@ exports.WebGLMode = void 0;
1711
1818
  _proto.createPlatformRenderTarget = function createPlatformRenderTarget(target) {
1712
1819
  return new GLRenderTarget(this, target);
1713
1820
  };
1821
+ _proto.createPlatformBuffer = function createPlatformBuffer(type, byteLength, bufferUsage, data) {
1822
+ if (bufferUsage === void 0) bufferUsage = miniprogram$1.BufferUsage.Static;
1823
+ return new GLBuffer(this, type, byteLength, bufferUsage, data);
1824
+ };
1714
1825
  _proto.requireExtension = function requireExtension(ext) {
1715
1826
  return this._extensions.requireExtension(ext);
1716
1827
  };
@@ -1838,8 +1949,57 @@ exports.WebGLMode = void 0;
1838
1949
  _proto.flush = function flush() {
1839
1950
  this._gl.flush();
1840
1951
  };
1841
- _proto.destroy = function destroy() {};
1842
- _create_class(WebGLRenderer, [
1952
+ _proto.forceLoseDevice = function forceLoseDevice() {
1953
+ var extension = this.requireExtension(miniprogram$1.GLCapabilityType.WEBGL_lose_context);
1954
+ extension.loseContext();
1955
+ };
1956
+ _proto.forceRestoreDevice = function forceRestoreDevice() {
1957
+ var extension = this.requireExtension(miniprogram$1.GLCapabilityType.WEBGL_lose_context);
1958
+ extension.restoreContext();
1959
+ };
1960
+ _proto.resetState = function resetState() {
1961
+ this._readFrameBuffer = null;
1962
+ this._enableGlobalDepthBias = false;
1963
+ this._currentBindShaderProgram = null;
1964
+ var activeTextures = this._activeTextures;
1965
+ for(var i = 0, n = activeTextures.length; i < n; i++){
1966
+ activeTextures[i] = null;
1967
+ }
1968
+ this._lastViewport.set(null, null, null, null);
1969
+ this._lastScissor.set(null, null, null, null);
1970
+ this._lastClearColor.set(null, null, null, null);
1971
+ this._scissorEnable = false;
1972
+ this._initGLState(this._gl);
1973
+ };
1974
+ _proto._initGLState = function _initGLState(gl) {
1975
+ this._activeTextureID = gl.TEXTURE0;
1976
+ this._renderStates = new GLRenderStates(gl);
1977
+ this._extensions = new GLExtensions(this);
1978
+ this._capability = new GLCapability(this);
1979
+ // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1980
+ gl.activeTexture(gl.TEXTURE0);
1981
+ var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1982
+ if (debugRenderInfo != null) {
1983
+ this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
1984
+ }
1985
+ };
1986
+ _proto.destroy = function destroy() {
1987
+ var webCanvas = this._webCanvas;
1988
+ webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1989
+ webCanvas.removeEventListener("webglcontextlost", this._onWebGLContextLost, false);
1990
+ webCanvas.removeEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1991
+ };
1992
+ _proto._onContextCreationError = function _onContextCreationError(event) {
1993
+ console.error("WebGLRenderer: WebGL context could not be created. Reason: ", event.statusMessage);
1994
+ };
1995
+ _proto._onWebGLContextLost = function _onWebGLContextLost(event) {
1996
+ event.preventDefault();
1997
+ this._onDeviceLost();
1998
+ };
1999
+ _proto._onWebGLContextRestored = function _onWebGLContextRestored(event) {
2000
+ this._onDeviceRestored();
2001
+ };
2002
+ _create_class(WebGLGraphicDevice, [
1843
2003
  {
1844
2004
  key: "isWebGL2",
1845
2005
  get: function get() {
@@ -1880,31 +2040,9 @@ exports.WebGLMode = void 0;
1880
2040
  }
1881
2041
  }
1882
2042
  ]);
1883
- return WebGLRenderer;
2043
+ return WebGLGraphicDevice;
1884
2044
  }();
1885
2045
 
1886
- /**
1887
- * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
1888
- */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
1889
- _inherits(WebGLEngine, Engine);
1890
- function WebGLEngine(canvas, webGLRendererOptions) {
1891
- var webCanvas = new WebCanvas(typeof canvas === "string" ? engineMiniprogramAdapter.document.getElementById(canvas) : canvas);
1892
- var hardwareRenderer = new WebGLRenderer(webGLRendererOptions);
1893
- return Engine.call(this, webCanvas, hardwareRenderer);
1894
- }
1895
- _create_class(WebGLEngine, [
1896
- {
1897
- key: "canvas",
1898
- get: /**
1899
- * Web canvas.
1900
- */ function get() {
1901
- return this._canvas;
1902
- }
1903
- }
1904
- ]);
1905
- return WebGLEngine;
1906
- }(miniprogram$1.Engine);
1907
-
1908
2046
  exports.WebCanvas = WebCanvas;
1909
2047
  exports.WebGLEngine = WebGLEngine;
1910
- exports.WebGLRenderer = WebGLRenderer;
2048
+ exports.WebGLGraphicDevice = WebGLGraphicDevice;
package/dist/module.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Vector2, Vector4, Color } from '@galacean/engine-math';
2
- import { GLCapabilityType, Logger, TextureWrapMode, TextureFormat, RenderBufferDepthFormat, TextureFilterMode, TextureDepthCompareFunction, Texture, TextureCube, CameraClearFlags, ColorWriteMask, SystemInfo, Platform, Engine } from '@galacean/engine-core';
2
+ import { Engine, BufferUsage, BufferBindFlag, SetDataOptions, GLCapabilityType, Logger, TextureWrapMode, TextureFormat, RenderBufferDepthFormat, TextureFilterMode, TextureDepthCompareFunction, Texture, TextureCube, CameraClearFlags, ColorWriteMask, SystemInfo, Platform } from '@galacean/engine-core';
3
3
 
4
4
  /**
5
5
  * Smoothing plug-in.
@@ -187,6 +187,37 @@ function _inherits(subClass, superClass) {
187
187
  if (superClass) _set_prototype_of(subClass, superClass);
188
188
  }
189
189
 
190
+ /**
191
+ * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
192
+ */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
193
+ _inherits(WebGLEngine, Engine);
194
+ function WebGLEngine() {
195
+ return Engine.apply(this, arguments);
196
+ }
197
+ /**
198
+ * Create a WebGL engine.
199
+ * @param configuration - WebGL engine configuration
200
+ * @returns A promise that will resolve when the engine is created
201
+ */ WebGLEngine.create = function create(configuration) {
202
+ var canvas = configuration.canvas;
203
+ var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
204
+ var webGLRenderer = new WebGLGraphicDevice(configuration.graphicDeviceOptions);
205
+ var engine = new WebGLEngine(webCanvas, webGLRenderer, configuration);
206
+ return engine._initialize(configuration);
207
+ };
208
+ _create_class(WebGLEngine, [
209
+ {
210
+ key: "canvas",
211
+ get: /**
212
+ * Web canvas.
213
+ */ function get() {
214
+ return this._canvas;
215
+ }
216
+ }
217
+ ]);
218
+ return WebGLEngine;
219
+ }(Engine);
220
+
190
221
  function _extends() {
191
222
  _extends = Object.assign || function assign(target) {
192
223
  for (var i = 1; i < arguments.length; i++) {
@@ -200,6 +231,84 @@ function _extends() {
200
231
  return _extends.apply(this, arguments);
201
232
  }
202
233
 
234
+ var GLBuffer = /*#__PURE__*/ function() {
235
+ function GLBuffer(rhi, type, byteLength, bufferUsage, data) {
236
+ if (bufferUsage === void 0) bufferUsage = BufferUsage.Static;
237
+ var gl = rhi.gl;
238
+ var glBuffer = gl.createBuffer();
239
+ var glBufferUsage = this._getGLBufferUsage(gl, bufferUsage);
240
+ var glBindTarget = type === BufferBindFlag.VertexBuffer ? gl.ARRAY_BUFFER : gl.ELEMENT_ARRAY_BUFFER;
241
+ this._gl = gl;
242
+ this._glBuffer = glBuffer;
243
+ this._glBufferUsage = glBufferUsage;
244
+ this._glBindTarget = glBindTarget;
245
+ this._isWebGL2 = rhi.isWebGL2;
246
+ this.bind();
247
+ if (data) {
248
+ gl.bufferData(glBindTarget, data, glBufferUsage);
249
+ } else {
250
+ gl.bufferData(glBindTarget, byteLength, glBufferUsage);
251
+ }
252
+ gl.bindBuffer(glBindTarget, null);
253
+ }
254
+ var _proto = GLBuffer.prototype;
255
+ _proto.bind = function bind() {
256
+ this._gl.bindBuffer(this._glBindTarget, this._glBuffer);
257
+ };
258
+ _proto.setData = function setData(byteLength, data, bufferByteOffset, dataOffset, dataLength, options) {
259
+ var gl = this._gl;
260
+ var glBindTarget = this._glBindTarget;
261
+ this.bind();
262
+ if (options === SetDataOptions.Discard) {
263
+ gl.bufferData(glBindTarget, byteLength, this._glBufferUsage);
264
+ }
265
+ // TypeArray is BYTES_PER_ELEMENT, unTypeArray is 1
266
+ var byteSize = data.BYTES_PER_ELEMENT || 1;
267
+ var dataByteLength = dataLength ? byteSize * dataLength : data.byteLength;
268
+ if (dataOffset !== 0 || dataByteLength < data.byteLength) {
269
+ var isArrayBufferView = data.byteOffset !== undefined;
270
+ if (this._isWebGL2 && isArrayBufferView) {
271
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data, dataOffset, dataByteLength / byteSize);
272
+ } else {
273
+ var subData = new Uint8Array(isArrayBufferView ? data.buffer : data, dataOffset * byteSize, dataByteLength);
274
+ gl.bufferSubData(glBindTarget, bufferByteOffset, subData);
275
+ }
276
+ } else {
277
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data);
278
+ }
279
+ gl.bindBuffer(glBindTarget, null);
280
+ };
281
+ _proto.getData = function getData(data, bufferByteOffset, dataOffset, dataLength) {
282
+ if (this._isWebGL2) {
283
+ var gl = this._gl;
284
+ this.bind();
285
+ gl.getBufferSubData(this._glBindTarget, bufferByteOffset, data, dataOffset, dataLength);
286
+ } else {
287
+ throw "Buffer is write-only on WebGL1.0 platforms.";
288
+ }
289
+ };
290
+ _proto.resize = function resize(byteLength) {
291
+ this.bind();
292
+ this._gl.bufferData(this._glBindTarget, byteLength, this._glBufferUsage);
293
+ };
294
+ _proto.destroy = function destroy() {
295
+ this._gl.deleteBuffer(this._glBuffer);
296
+ this._gl = null;
297
+ this._glBuffer = null;
298
+ };
299
+ _proto._getGLBufferUsage = function _getGLBufferUsage(gl, bufferUsage) {
300
+ switch(bufferUsage){
301
+ case BufferUsage.Static:
302
+ return gl.STATIC_DRAW;
303
+ case BufferUsage.Dynamic:
304
+ return gl.DYNAMIC_DRAW;
305
+ case BufferUsage.Stream:
306
+ return gl.STREAM_DRAW;
307
+ }
308
+ };
309
+ return GLBuffer;
310
+ }();
311
+
203
312
  /**
204
313
  * GL capability.
205
314
  */ var GLCapability = /*#__PURE__*/ function() {
@@ -469,8 +578,8 @@ function _extends() {
469
578
  if (useVao) {
470
579
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
471
580
  } else {
472
- var _nativeBuffer = _indexBufferBinding.buffer._nativeBuffer;
473
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer);
581
+ var _glBuffer = _indexBufferBinding.buffer._platformBuffer._glBuffer;
582
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer);
474
583
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
475
584
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
476
585
  }
@@ -483,8 +592,8 @@ function _extends() {
483
592
  if (useVao) {
484
593
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
485
594
  } else {
486
- var _nativeBuffer1 = _indexBufferBinding.buffer._nativeBuffer;
487
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer1);
595
+ var _glBuffer1 = _indexBufferBinding.buffer._platformBuffer._glBuffer;
596
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer1);
488
597
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
489
598
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
490
599
  }
@@ -524,7 +633,7 @@ function _extends() {
524
633
  var element = attributes[name];
525
634
  if (element) {
526
635
  var _vertexBufferBindings_element_bindingIndex = vertexBufferBindings[element.bindingIndex], buffer = _vertexBufferBindings_element_bindingIndex.buffer, stride = _vertexBufferBindings_element_bindingIndex.stride;
527
- vbo = buffer._nativeBuffer;
636
+ vbo = buffer._platformBuffer._glBuffer;
528
637
  // prevent binding the vbo which already bound at the last loop, e.g. a buffer with multiple attributes.
529
638
  if (lastBoundVbo !== vbo) {
530
639
  lastBoundVbo = vbo;
@@ -556,7 +665,7 @@ function _extends() {
556
665
  // @ts-ignore
557
666
  var _indexBufferBinding = this._primitive._indexBufferBinding;
558
667
  if (_indexBufferBinding) {
559
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._nativeBuffer);
668
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._platformBuffer._glBuffer);
560
669
  }
561
670
  this._bindBufferAndAttrib(shaderProgram);
562
671
  /** unbind */ gl.bindVertexArray(null);
@@ -1621,20 +1730,19 @@ var WebGLMode;
1621
1730
  WebGLMode[WebGLMode[/** WebGL1.0, */ "WebGL1"] = 2] = "WebGL1";
1622
1731
  })(WebGLMode || (WebGLMode = {}));
1623
1732
  /**
1624
- * WebGL renderer, including WebGL1.0 and WebGL2.0.
1625
- */ var WebGLRenderer = /*#__PURE__*/ function() {
1626
- function WebGLRenderer(initializeOptions) {
1733
+ * WebGL graphic device, including WebGL1.0 and WebGL2.0.
1734
+ */ var WebGLGraphicDevice = /*#__PURE__*/ function() {
1735
+ function WebGLGraphicDevice(initializeOptions) {
1627
1736
  if (initializeOptions === void 0) initializeOptions = {};
1737
+ /** @internal */ this._readFrameBuffer = null;
1628
1738
  /** @internal */ this._enableGlobalDepthBias = false;
1629
1739
  this._activeTextures = new Array(32);
1630
- // cache value
1631
1740
  this._lastViewport = new Vector4(null, null, null, null);
1632
1741
  this._lastScissor = new Vector4(null, null, null, null);
1633
1742
  this._lastClearColor = new Color(null, null, null, null);
1634
1743
  this._scissorEnable = false;
1635
1744
  var options = _extends({
1636
1745
  webGLMode: 0,
1637
- alpha: false,
1638
1746
  stencil: true,
1639
1747
  _forceFlush: false
1640
1748
  }, initializeOptions);
@@ -1649,12 +1757,20 @@ var WebGLMode;
1649
1757
  }
1650
1758
  }
1651
1759
  this._options = options;
1760
+ this._onWebGLContextLost = this._onWebGLContextLost.bind(this);
1761
+ this._onWebGLContextRestored = this._onWebGLContextRestored.bind(this);
1652
1762
  }
1653
- var _proto = WebGLRenderer.prototype;
1654
- _proto.init = function init(canvas) {
1763
+ var _proto = WebGLGraphicDevice.prototype;
1764
+ _proto.init = function init(canvas, onDeviceLost, onDeviceRestored) {
1655
1765
  var options = this._options;
1656
- var webCanvas = this._webCanvas = canvas._webCanvas;
1766
+ var webCanvas = canvas._webCanvas;
1657
1767
  var webGLMode = options.webGLMode;
1768
+ this._onDeviceLost = onDeviceLost;
1769
+ this._onDeviceRestored = onDeviceRestored;
1770
+ webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1771
+ webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1772
+ webCanvas.addEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1773
+ this._webCanvas = webCanvas;
1658
1774
  var gl;
1659
1775
  if (webGLMode == 0 || webGLMode == 1) {
1660
1776
  gl = webCanvas.getContext("webgl2", options);
@@ -1680,16 +1796,7 @@ var WebGLMode;
1680
1796
  throw new Error("Get GL Context FAILED.");
1681
1797
  }
1682
1798
  this._gl = gl;
1683
- this._activeTextureID = gl.TEXTURE0;
1684
- this._renderStates = new GLRenderStates(gl);
1685
- this._extensions = new GLExtensions(this);
1686
- this._capability = new GLCapability(this);
1687
- // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1688
- gl.activeTexture(gl.TEXTURE0);
1689
- var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1690
- if (debugRenderInfo != null) {
1691
- this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
1692
- }
1799
+ this._initGLState(gl);
1693
1800
  };
1694
1801
  _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1695
1802
  return new GLPrimitive(this, primitive);
@@ -1706,6 +1813,10 @@ var WebGLMode;
1706
1813
  _proto.createPlatformRenderTarget = function createPlatformRenderTarget(target) {
1707
1814
  return new GLRenderTarget(this, target);
1708
1815
  };
1816
+ _proto.createPlatformBuffer = function createPlatformBuffer(type, byteLength, bufferUsage, data) {
1817
+ if (bufferUsage === void 0) bufferUsage = BufferUsage.Static;
1818
+ return new GLBuffer(this, type, byteLength, bufferUsage, data);
1819
+ };
1709
1820
  _proto.requireExtension = function requireExtension(ext) {
1710
1821
  return this._extensions.requireExtension(ext);
1711
1822
  };
@@ -1833,8 +1944,57 @@ var WebGLMode;
1833
1944
  _proto.flush = function flush() {
1834
1945
  this._gl.flush();
1835
1946
  };
1836
- _proto.destroy = function destroy() {};
1837
- _create_class(WebGLRenderer, [
1947
+ _proto.forceLoseDevice = function forceLoseDevice() {
1948
+ var extension = this.requireExtension(GLCapabilityType.WEBGL_lose_context);
1949
+ extension.loseContext();
1950
+ };
1951
+ _proto.forceRestoreDevice = function forceRestoreDevice() {
1952
+ var extension = this.requireExtension(GLCapabilityType.WEBGL_lose_context);
1953
+ extension.restoreContext();
1954
+ };
1955
+ _proto.resetState = function resetState() {
1956
+ this._readFrameBuffer = null;
1957
+ this._enableGlobalDepthBias = false;
1958
+ this._currentBindShaderProgram = null;
1959
+ var activeTextures = this._activeTextures;
1960
+ for(var i = 0, n = activeTextures.length; i < n; i++){
1961
+ activeTextures[i] = null;
1962
+ }
1963
+ this._lastViewport.set(null, null, null, null);
1964
+ this._lastScissor.set(null, null, null, null);
1965
+ this._lastClearColor.set(null, null, null, null);
1966
+ this._scissorEnable = false;
1967
+ this._initGLState(this._gl);
1968
+ };
1969
+ _proto._initGLState = function _initGLState(gl) {
1970
+ this._activeTextureID = gl.TEXTURE0;
1971
+ this._renderStates = new GLRenderStates(gl);
1972
+ this._extensions = new GLExtensions(this);
1973
+ this._capability = new GLCapability(this);
1974
+ // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1975
+ gl.activeTexture(gl.TEXTURE0);
1976
+ var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1977
+ if (debugRenderInfo != null) {
1978
+ this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
1979
+ }
1980
+ };
1981
+ _proto.destroy = function destroy() {
1982
+ var webCanvas = this._webCanvas;
1983
+ webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1984
+ webCanvas.removeEventListener("webglcontextlost", this._onWebGLContextLost, false);
1985
+ webCanvas.removeEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1986
+ };
1987
+ _proto._onContextCreationError = function _onContextCreationError(event) {
1988
+ console.error("WebGLRenderer: WebGL context could not be created. Reason: ", event.statusMessage);
1989
+ };
1990
+ _proto._onWebGLContextLost = function _onWebGLContextLost(event) {
1991
+ event.preventDefault();
1992
+ this._onDeviceLost();
1993
+ };
1994
+ _proto._onWebGLContextRestored = function _onWebGLContextRestored(event) {
1995
+ this._onDeviceRestored();
1996
+ };
1997
+ _create_class(WebGLGraphicDevice, [
1838
1998
  {
1839
1999
  key: "isWebGL2",
1840
2000
  get: function get() {
@@ -1875,30 +2035,8 @@ var WebGLMode;
1875
2035
  }
1876
2036
  }
1877
2037
  ]);
1878
- return WebGLRenderer;
2038
+ return WebGLGraphicDevice;
1879
2039
  }();
1880
2040
 
1881
- /**
1882
- * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
1883
- */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
1884
- _inherits(WebGLEngine, Engine);
1885
- function WebGLEngine(canvas, webGLRendererOptions) {
1886
- var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
1887
- var hardwareRenderer = new WebGLRenderer(webGLRendererOptions);
1888
- return Engine.call(this, webCanvas, hardwareRenderer);
1889
- }
1890
- _create_class(WebGLEngine, [
1891
- {
1892
- key: "canvas",
1893
- get: /**
1894
- * Web canvas.
1895
- */ function get() {
1896
- return this._canvas;
1897
- }
1898
- }
1899
- ]);
1900
- return WebGLEngine;
1901
- }(Engine);
1902
-
1903
- export { GLCompressedTextureInternalFormat, WebCanvas, WebGLEngine, WebGLMode, WebGLRenderer };
2041
+ export { GLCompressedTextureInternalFormat, WebCanvas, WebGLEngine, WebGLGraphicDevice, WebGLMode };
1904
2042
  //# sourceMappingURL=module.js.map