@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.
package/dist/main.js CHANGED
@@ -191,6 +191,37 @@ function _inherits(subClass, superClass) {
191
191
  if (superClass) _set_prototype_of(subClass, superClass);
192
192
  }
193
193
 
194
+ /**
195
+ * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
196
+ */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
197
+ _inherits(WebGLEngine, Engine);
198
+ function WebGLEngine() {
199
+ return Engine.apply(this, arguments);
200
+ }
201
+ /**
202
+ * Create a WebGL engine.
203
+ * @param configuration - WebGL engine configuration
204
+ * @returns A promise that will resolve when the engine is created
205
+ */ WebGLEngine.create = function create(configuration) {
206
+ var canvas = configuration.canvas;
207
+ var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
208
+ var webGLRenderer = new WebGLGraphicDevice(configuration.graphicDeviceOptions);
209
+ var engine = new WebGLEngine(webCanvas, webGLRenderer, configuration);
210
+ return engine._initialize(configuration);
211
+ };
212
+ _create_class(WebGLEngine, [
213
+ {
214
+ key: "canvas",
215
+ get: /**
216
+ * Web canvas.
217
+ */ function get() {
218
+ return this._canvas;
219
+ }
220
+ }
221
+ ]);
222
+ return WebGLEngine;
223
+ }(engineCore.Engine);
224
+
194
225
  function _extends() {
195
226
  _extends = Object.assign || function assign(target) {
196
227
  for (var i = 1; i < arguments.length; i++) {
@@ -204,6 +235,84 @@ function _extends() {
204
235
  return _extends.apply(this, arguments);
205
236
  }
206
237
 
238
+ var GLBuffer = /*#__PURE__*/ function() {
239
+ function GLBuffer(rhi, type, byteLength, bufferUsage, data) {
240
+ if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
241
+ var gl = rhi.gl;
242
+ var glBuffer = gl.createBuffer();
243
+ var glBufferUsage = this._getGLBufferUsage(gl, bufferUsage);
244
+ var glBindTarget = type === engineCore.BufferBindFlag.VertexBuffer ? gl.ARRAY_BUFFER : gl.ELEMENT_ARRAY_BUFFER;
245
+ this._gl = gl;
246
+ this._glBuffer = glBuffer;
247
+ this._glBufferUsage = glBufferUsage;
248
+ this._glBindTarget = glBindTarget;
249
+ this._isWebGL2 = rhi.isWebGL2;
250
+ this.bind();
251
+ if (data) {
252
+ gl.bufferData(glBindTarget, data, glBufferUsage);
253
+ } else {
254
+ gl.bufferData(glBindTarget, byteLength, glBufferUsage);
255
+ }
256
+ gl.bindBuffer(glBindTarget, null);
257
+ }
258
+ var _proto = GLBuffer.prototype;
259
+ _proto.bind = function bind() {
260
+ this._gl.bindBuffer(this._glBindTarget, this._glBuffer);
261
+ };
262
+ _proto.setData = function setData(byteLength, data, bufferByteOffset, dataOffset, dataLength, options) {
263
+ var gl = this._gl;
264
+ var glBindTarget = this._glBindTarget;
265
+ this.bind();
266
+ if (options === engineCore.SetDataOptions.Discard) {
267
+ gl.bufferData(glBindTarget, byteLength, this._glBufferUsage);
268
+ }
269
+ // TypeArray is BYTES_PER_ELEMENT, unTypeArray is 1
270
+ var byteSize = data.BYTES_PER_ELEMENT || 1;
271
+ var dataByteLength = dataLength ? byteSize * dataLength : data.byteLength;
272
+ if (dataOffset !== 0 || dataByteLength < data.byteLength) {
273
+ var isArrayBufferView = data.byteOffset !== undefined;
274
+ if (this._isWebGL2 && isArrayBufferView) {
275
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data, dataOffset, dataByteLength / byteSize);
276
+ } else {
277
+ var subData = new Uint8Array(isArrayBufferView ? data.buffer : data, dataOffset * byteSize, dataByteLength);
278
+ gl.bufferSubData(glBindTarget, bufferByteOffset, subData);
279
+ }
280
+ } else {
281
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data);
282
+ }
283
+ gl.bindBuffer(glBindTarget, null);
284
+ };
285
+ _proto.getData = function getData(data, bufferByteOffset, dataOffset, dataLength) {
286
+ if (this._isWebGL2) {
287
+ var gl = this._gl;
288
+ this.bind();
289
+ gl.getBufferSubData(this._glBindTarget, bufferByteOffset, data, dataOffset, dataLength);
290
+ } else {
291
+ throw "Buffer is write-only on WebGL1.0 platforms.";
292
+ }
293
+ };
294
+ _proto.resize = function resize(byteLength) {
295
+ this.bind();
296
+ this._gl.bufferData(this._glBindTarget, byteLength, this._glBufferUsage);
297
+ };
298
+ _proto.destroy = function destroy() {
299
+ this._gl.deleteBuffer(this._glBuffer);
300
+ this._gl = null;
301
+ this._glBuffer = null;
302
+ };
303
+ _proto._getGLBufferUsage = function _getGLBufferUsage(gl, bufferUsage) {
304
+ switch(bufferUsage){
305
+ case engineCore.BufferUsage.Static:
306
+ return gl.STATIC_DRAW;
307
+ case engineCore.BufferUsage.Dynamic:
308
+ return gl.DYNAMIC_DRAW;
309
+ case engineCore.BufferUsage.Stream:
310
+ return gl.STREAM_DRAW;
311
+ }
312
+ };
313
+ return GLBuffer;
314
+ }();
315
+
207
316
  /**
208
317
  * GL capability.
209
318
  */ var GLCapability = /*#__PURE__*/ function() {
@@ -473,8 +582,8 @@ function _extends() {
473
582
  if (useVao) {
474
583
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
475
584
  } else {
476
- var _nativeBuffer = _indexBufferBinding.buffer._nativeBuffer;
477
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer);
585
+ var _glBuffer = _indexBufferBinding.buffer._platformBuffer._glBuffer;
586
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer);
478
587
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
479
588
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
480
589
  }
@@ -487,8 +596,8 @@ function _extends() {
487
596
  if (useVao) {
488
597
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
489
598
  } else {
490
- var _nativeBuffer1 = _indexBufferBinding.buffer._nativeBuffer;
491
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer1);
599
+ var _glBuffer1 = _indexBufferBinding.buffer._platformBuffer._glBuffer;
600
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer1);
492
601
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
493
602
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
494
603
  }
@@ -528,7 +637,7 @@ function _extends() {
528
637
  var element = attributes[name];
529
638
  if (element) {
530
639
  var _vertexBufferBindings_element_bindingIndex = vertexBufferBindings[element.bindingIndex], buffer = _vertexBufferBindings_element_bindingIndex.buffer, stride = _vertexBufferBindings_element_bindingIndex.stride;
531
- vbo = buffer._nativeBuffer;
640
+ vbo = buffer._platformBuffer._glBuffer;
532
641
  // prevent binding the vbo which already bound at the last loop, e.g. a buffer with multiple attributes.
533
642
  if (lastBoundVbo !== vbo) {
534
643
  lastBoundVbo = vbo;
@@ -560,7 +669,7 @@ function _extends() {
560
669
  // @ts-ignore
561
670
  var _indexBufferBinding = this._primitive._indexBufferBinding;
562
671
  if (_indexBufferBinding) {
563
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._nativeBuffer);
672
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._platformBuffer._glBuffer);
564
673
  }
565
674
  this._bindBufferAndAttrib(shaderProgram);
566
675
  /** unbind */ gl.bindVertexArray(null);
@@ -1625,20 +1734,19 @@ exports.WebGLMode = void 0;
1625
1734
  WebGLMode[WebGLMode[/** WebGL1.0, */ "WebGL1"] = 2] = "WebGL1";
1626
1735
  })(exports.WebGLMode || (exports.WebGLMode = {}));
1627
1736
  /**
1628
- * WebGL renderer, including WebGL1.0 and WebGL2.0.
1629
- */ var WebGLRenderer = /*#__PURE__*/ function() {
1630
- function WebGLRenderer(initializeOptions) {
1737
+ * WebGL graphic device, including WebGL1.0 and WebGL2.0.
1738
+ */ var WebGLGraphicDevice = /*#__PURE__*/ function() {
1739
+ function WebGLGraphicDevice(initializeOptions) {
1631
1740
  if (initializeOptions === void 0) initializeOptions = {};
1741
+ /** @internal */ this._readFrameBuffer = null;
1632
1742
  /** @internal */ this._enableGlobalDepthBias = false;
1633
1743
  this._activeTextures = new Array(32);
1634
- // cache value
1635
1744
  this._lastViewport = new engineMath.Vector4(null, null, null, null);
1636
1745
  this._lastScissor = new engineMath.Vector4(null, null, null, null);
1637
1746
  this._lastClearColor = new engineMath.Color(null, null, null, null);
1638
1747
  this._scissorEnable = false;
1639
1748
  var options = _extends({
1640
1749
  webGLMode: 0,
1641
- alpha: false,
1642
1750
  stencil: true,
1643
1751
  _forceFlush: false
1644
1752
  }, initializeOptions);
@@ -1653,12 +1761,20 @@ exports.WebGLMode = void 0;
1653
1761
  }
1654
1762
  }
1655
1763
  this._options = options;
1764
+ this._onWebGLContextLost = this._onWebGLContextLost.bind(this);
1765
+ this._onWebGLContextRestored = this._onWebGLContextRestored.bind(this);
1656
1766
  }
1657
- var _proto = WebGLRenderer.prototype;
1658
- _proto.init = function init(canvas) {
1767
+ var _proto = WebGLGraphicDevice.prototype;
1768
+ _proto.init = function init(canvas, onDeviceLost, onDeviceRestored) {
1659
1769
  var options = this._options;
1660
- var webCanvas = this._webCanvas = canvas._webCanvas;
1770
+ var webCanvas = canvas._webCanvas;
1661
1771
  var webGLMode = options.webGLMode;
1772
+ this._onDeviceLost = onDeviceLost;
1773
+ this._onDeviceRestored = onDeviceRestored;
1774
+ webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1775
+ webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1776
+ webCanvas.addEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1777
+ this._webCanvas = webCanvas;
1662
1778
  var gl;
1663
1779
  if (webGLMode == 0 || webGLMode == 1) {
1664
1780
  gl = webCanvas.getContext("webgl2", options);
@@ -1684,16 +1800,7 @@ exports.WebGLMode = void 0;
1684
1800
  throw new Error("Get GL Context FAILED.");
1685
1801
  }
1686
1802
  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
- }
1803
+ this._initGLState(gl);
1697
1804
  };
1698
1805
  _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1699
1806
  return new GLPrimitive(this, primitive);
@@ -1710,6 +1817,10 @@ exports.WebGLMode = void 0;
1710
1817
  _proto.createPlatformRenderTarget = function createPlatformRenderTarget(target) {
1711
1818
  return new GLRenderTarget(this, target);
1712
1819
  };
1820
+ _proto.createPlatformBuffer = function createPlatformBuffer(type, byteLength, bufferUsage, data) {
1821
+ if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
1822
+ return new GLBuffer(this, type, byteLength, bufferUsage, data);
1823
+ };
1713
1824
  _proto.requireExtension = function requireExtension(ext) {
1714
1825
  return this._extensions.requireExtension(ext);
1715
1826
  };
@@ -1837,8 +1948,57 @@ exports.WebGLMode = void 0;
1837
1948
  _proto.flush = function flush() {
1838
1949
  this._gl.flush();
1839
1950
  };
1840
- _proto.destroy = function destroy() {};
1841
- _create_class(WebGLRenderer, [
1951
+ _proto.forceLoseDevice = function forceLoseDevice() {
1952
+ var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1953
+ extension.loseContext();
1954
+ };
1955
+ _proto.forceRestoreDevice = function forceRestoreDevice() {
1956
+ var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1957
+ extension.restoreContext();
1958
+ };
1959
+ _proto.resetState = function resetState() {
1960
+ this._readFrameBuffer = null;
1961
+ this._enableGlobalDepthBias = false;
1962
+ this._currentBindShaderProgram = null;
1963
+ var activeTextures = this._activeTextures;
1964
+ for(var i = 0, n = activeTextures.length; i < n; i++){
1965
+ activeTextures[i] = null;
1966
+ }
1967
+ this._lastViewport.set(null, null, null, null);
1968
+ this._lastScissor.set(null, null, null, null);
1969
+ this._lastClearColor.set(null, null, null, null);
1970
+ this._scissorEnable = false;
1971
+ this._initGLState(this._gl);
1972
+ };
1973
+ _proto._initGLState = function _initGLState(gl) {
1974
+ this._activeTextureID = gl.TEXTURE0;
1975
+ this._renderStates = new GLRenderStates(gl);
1976
+ this._extensions = new GLExtensions(this);
1977
+ this._capability = new GLCapability(this);
1978
+ // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1979
+ gl.activeTexture(gl.TEXTURE0);
1980
+ var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1981
+ if (debugRenderInfo != null) {
1982
+ this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
1983
+ }
1984
+ };
1985
+ _proto.destroy = function destroy() {
1986
+ var webCanvas = this._webCanvas;
1987
+ webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1988
+ webCanvas.removeEventListener("webglcontextlost", this._onWebGLContextLost, false);
1989
+ webCanvas.removeEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1990
+ };
1991
+ _proto._onContextCreationError = function _onContextCreationError(event) {
1992
+ console.error("WebGLRenderer: WebGL context could not be created. Reason: ", event.statusMessage);
1993
+ };
1994
+ _proto._onWebGLContextLost = function _onWebGLContextLost(event) {
1995
+ event.preventDefault();
1996
+ this._onDeviceLost();
1997
+ };
1998
+ _proto._onWebGLContextRestored = function _onWebGLContextRestored(event) {
1999
+ this._onDeviceRestored();
2000
+ };
2001
+ _create_class(WebGLGraphicDevice, [
1842
2002
  {
1843
2003
  key: "isWebGL2",
1844
2004
  get: function get() {
@@ -1879,32 +2039,10 @@ exports.WebGLMode = void 0;
1879
2039
  }
1880
2040
  }
1881
2041
  ]);
1882
- return WebGLRenderer;
2042
+ return WebGLGraphicDevice;
1883
2043
  }();
1884
2044
 
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
2045
  exports.WebCanvas = WebCanvas;
1908
2046
  exports.WebGLEngine = WebGLEngine;
1909
- exports.WebGLRenderer = WebGLRenderer;
2047
+ exports.WebGLGraphicDevice = WebGLGraphicDevice;
1910
2048
  //# sourceMappingURL=main.js.map