@galacean/engine-rhi-webgl 1.0.0 → 1.1.0-alpha.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,43 @@ 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 webGLGraphicDevice = new WebGLGraphicDevice(configuration.graphicDeviceOptions);
209
+ var engine = new WebGLEngine(webCanvas, webGLGraphicDevice, configuration);
210
+ // @ts-ignore
211
+ var promise = engine._initialize(configuration);
212
+ return promise.then(function() {
213
+ engine.sceneManager.addScene(new engineCore.Scene(engine, "DefaultScene"));
214
+ return engine;
215
+ });
216
+ };
217
+ _create_class(WebGLEngine, [
218
+ {
219
+ key: "canvas",
220
+ get: /**
221
+ * Web canvas.
222
+ */ function get() {
223
+ // @ts-ignore
224
+ return this._canvas;
225
+ }
226
+ }
227
+ ]);
228
+ return WebGLEngine;
229
+ }(engineCore.Engine);
230
+
194
231
  function _extends() {
195
232
  _extends = Object.assign || function assign(target) {
196
233
  for (var i = 1; i < arguments.length; i++) {
@@ -204,6 +241,84 @@ function _extends() {
204
241
  return _extends.apply(this, arguments);
205
242
  }
206
243
 
244
+ var GLBuffer = /*#__PURE__*/ function() {
245
+ function GLBuffer(rhi, type, byteLength, bufferUsage, data) {
246
+ if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
247
+ var gl = rhi.gl;
248
+ var glBuffer = gl.createBuffer();
249
+ var glBufferUsage = this._getGLBufferUsage(gl, bufferUsage);
250
+ var glBindTarget = type === engineCore.BufferBindFlag.VertexBuffer ? gl.ARRAY_BUFFER : gl.ELEMENT_ARRAY_BUFFER;
251
+ this._gl = gl;
252
+ this._glBuffer = glBuffer;
253
+ this._glBufferUsage = glBufferUsage;
254
+ this._glBindTarget = glBindTarget;
255
+ this._isWebGL2 = rhi.isWebGL2;
256
+ this.bind();
257
+ if (data) {
258
+ gl.bufferData(glBindTarget, data, glBufferUsage);
259
+ } else {
260
+ gl.bufferData(glBindTarget, byteLength, glBufferUsage);
261
+ }
262
+ gl.bindBuffer(glBindTarget, null);
263
+ }
264
+ var _proto = GLBuffer.prototype;
265
+ _proto.bind = function bind() {
266
+ this._gl.bindBuffer(this._glBindTarget, this._glBuffer);
267
+ };
268
+ _proto.setData = function setData(byteLength, data, bufferByteOffset, dataOffset, dataLength, options) {
269
+ var gl = this._gl;
270
+ var glBindTarget = this._glBindTarget;
271
+ this.bind();
272
+ if (options === engineCore.SetDataOptions.Discard) {
273
+ gl.bufferData(glBindTarget, byteLength, this._glBufferUsage);
274
+ }
275
+ // TypeArray is BYTES_PER_ELEMENT, unTypeArray is 1
276
+ var byteSize = data.BYTES_PER_ELEMENT || 1;
277
+ var dataByteLength = dataLength ? byteSize * dataLength : data.byteLength;
278
+ if (dataOffset !== 0 || dataByteLength < data.byteLength) {
279
+ var isArrayBufferView = data.byteOffset !== undefined;
280
+ if (this._isWebGL2 && isArrayBufferView) {
281
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data, dataOffset, dataByteLength / byteSize);
282
+ } else {
283
+ var subData = new Uint8Array(isArrayBufferView ? data.buffer : data, dataOffset * byteSize, dataByteLength);
284
+ gl.bufferSubData(glBindTarget, bufferByteOffset, subData);
285
+ }
286
+ } else {
287
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data);
288
+ }
289
+ gl.bindBuffer(glBindTarget, null);
290
+ };
291
+ _proto.getData = function getData(data, bufferByteOffset, dataOffset, dataLength) {
292
+ if (this._isWebGL2) {
293
+ var gl = this._gl;
294
+ this.bind();
295
+ gl.getBufferSubData(this._glBindTarget, bufferByteOffset, data, dataOffset, dataLength);
296
+ } else {
297
+ throw "Buffer is write-only on WebGL1.0 platforms.";
298
+ }
299
+ };
300
+ _proto.resize = function resize(byteLength) {
301
+ this.bind();
302
+ this._gl.bufferData(this._glBindTarget, byteLength, this._glBufferUsage);
303
+ };
304
+ _proto.destroy = function destroy() {
305
+ this._gl.deleteBuffer(this._glBuffer);
306
+ this._gl = null;
307
+ this._glBuffer = null;
308
+ };
309
+ _proto._getGLBufferUsage = function _getGLBufferUsage(gl, bufferUsage) {
310
+ switch(bufferUsage){
311
+ case engineCore.BufferUsage.Static:
312
+ return gl.STATIC_DRAW;
313
+ case engineCore.BufferUsage.Dynamic:
314
+ return gl.DYNAMIC_DRAW;
315
+ case engineCore.BufferUsage.Stream:
316
+ return gl.STREAM_DRAW;
317
+ }
318
+ };
319
+ return GLBuffer;
320
+ }();
321
+
207
322
  /**
208
323
  * GL capability.
209
324
  */ var GLCapability = /*#__PURE__*/ function() {
@@ -473,8 +588,8 @@ function _extends() {
473
588
  if (useVao) {
474
589
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
475
590
  } else {
476
- var _nativeBuffer = _indexBufferBinding.buffer._nativeBuffer;
477
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer);
591
+ var _glBuffer = _indexBufferBinding.buffer._platformBuffer._glBuffer;
592
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer);
478
593
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
479
594
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
480
595
  }
@@ -487,8 +602,8 @@ function _extends() {
487
602
  if (useVao) {
488
603
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
489
604
  } else {
490
- var _nativeBuffer1 = _indexBufferBinding.buffer._nativeBuffer;
491
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer1);
605
+ var _glBuffer1 = _indexBufferBinding.buffer._platformBuffer._glBuffer;
606
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer1);
492
607
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
493
608
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
494
609
  }
@@ -528,7 +643,7 @@ function _extends() {
528
643
  var element = attributes[name];
529
644
  if (element) {
530
645
  var _vertexBufferBindings_element_bindingIndex = vertexBufferBindings[element.bindingIndex], buffer = _vertexBufferBindings_element_bindingIndex.buffer, stride = _vertexBufferBindings_element_bindingIndex.stride;
531
- vbo = buffer._nativeBuffer;
646
+ vbo = buffer._platformBuffer._glBuffer;
532
647
  // prevent binding the vbo which already bound at the last loop, e.g. a buffer with multiple attributes.
533
648
  if (lastBoundVbo !== vbo) {
534
649
  lastBoundVbo = vbo;
@@ -560,7 +675,7 @@ function _extends() {
560
675
  // @ts-ignore
561
676
  var _indexBufferBinding = this._primitive._indexBufferBinding;
562
677
  if (_indexBufferBinding) {
563
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._nativeBuffer);
678
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._platformBuffer._glBuffer);
564
679
  }
565
680
  this._bindBufferAndAttrib(shaderProgram);
566
681
  /** unbind */ gl.bindVertexArray(null);
@@ -1625,20 +1740,19 @@ exports.WebGLMode = void 0;
1625
1740
  WebGLMode[WebGLMode[/** WebGL1.0, */ "WebGL1"] = 2] = "WebGL1";
1626
1741
  })(exports.WebGLMode || (exports.WebGLMode = {}));
1627
1742
  /**
1628
- * WebGL renderer, including WebGL1.0 and WebGL2.0.
1629
- */ var WebGLRenderer = /*#__PURE__*/ function() {
1630
- function WebGLRenderer(initializeOptions) {
1743
+ * WebGL graphic device, including WebGL1.0 and WebGL2.0.
1744
+ */ var WebGLGraphicDevice = /*#__PURE__*/ function() {
1745
+ function WebGLGraphicDevice(initializeOptions) {
1631
1746
  if (initializeOptions === void 0) initializeOptions = {};
1747
+ /** @internal */ this._readFrameBuffer = null;
1632
1748
  /** @internal */ this._enableGlobalDepthBias = false;
1633
1749
  this._activeTextures = new Array(32);
1634
- // cache value
1635
1750
  this._lastViewport = new engineMath.Vector4(null, null, null, null);
1636
1751
  this._lastScissor = new engineMath.Vector4(null, null, null, null);
1637
1752
  this._lastClearColor = new engineMath.Color(null, null, null, null);
1638
1753
  this._scissorEnable = false;
1639
1754
  var options = _extends({
1640
1755
  webGLMode: 0,
1641
- alpha: false,
1642
1756
  stencil: true,
1643
1757
  _forceFlush: false
1644
1758
  }, initializeOptions);
@@ -1653,12 +1767,20 @@ exports.WebGLMode = void 0;
1653
1767
  }
1654
1768
  }
1655
1769
  this._options = options;
1770
+ this._onWebGLContextLost = this._onWebGLContextLost.bind(this);
1771
+ this._onWebGLContextRestored = this._onWebGLContextRestored.bind(this);
1656
1772
  }
1657
- var _proto = WebGLRenderer.prototype;
1658
- _proto.init = function init(canvas) {
1773
+ var _proto = WebGLGraphicDevice.prototype;
1774
+ _proto.init = function init(canvas, onDeviceLost, onDeviceRestored) {
1659
1775
  var options = this._options;
1660
- var webCanvas = this._webCanvas = canvas._webCanvas;
1776
+ var webCanvas = canvas._webCanvas;
1661
1777
  var webGLMode = options.webGLMode;
1778
+ this._onDeviceLost = onDeviceLost;
1779
+ this._onDeviceRestored = onDeviceRestored;
1780
+ webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1781
+ webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1782
+ webCanvas.addEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1783
+ this._webCanvas = webCanvas;
1662
1784
  var gl;
1663
1785
  if (webGLMode == 0 || webGLMode == 1) {
1664
1786
  gl = webCanvas.getContext("webgl2", options);
@@ -1684,16 +1806,7 @@ exports.WebGLMode = void 0;
1684
1806
  throw new Error("Get GL Context FAILED.");
1685
1807
  }
1686
1808
  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
- }
1809
+ this._initGLState(gl);
1697
1810
  };
1698
1811
  _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1699
1812
  return new GLPrimitive(this, primitive);
@@ -1710,6 +1823,10 @@ exports.WebGLMode = void 0;
1710
1823
  _proto.createPlatformRenderTarget = function createPlatformRenderTarget(target) {
1711
1824
  return new GLRenderTarget(this, target);
1712
1825
  };
1826
+ _proto.createPlatformBuffer = function createPlatformBuffer(type, byteLength, bufferUsage, data) {
1827
+ if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
1828
+ return new GLBuffer(this, type, byteLength, bufferUsage, data);
1829
+ };
1713
1830
  _proto.requireExtension = function requireExtension(ext) {
1714
1831
  return this._extensions.requireExtension(ext);
1715
1832
  };
@@ -1792,23 +1909,23 @@ exports.WebGLMode = void 0;
1792
1909
  };
1793
1910
  _proto.activeRenderTarget = function activeRenderTarget(renderTarget, viewport, mipLevel) {
1794
1911
  var gl = this._gl;
1912
+ var bufferWidth, bufferHeight;
1795
1913
  if (renderTarget) {
1796
1914
  var /** @ts-ignore */ _renderTarget__platformRenderTarget;
1797
1915
  (_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);
1916
+ bufferWidth = renderTarget.width >> mipLevel;
1917
+ bufferHeight = renderTarget.height >> mipLevel;
1802
1918
  } else {
1803
1919
  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);
1920
+ bufferWidth = gl.drawingBufferWidth;
1921
+ bufferHeight = gl.drawingBufferHeight;
1811
1922
  }
1923
+ var width = bufferWidth * viewport.z;
1924
+ var height = bufferHeight * viewport.w;
1925
+ var x = viewport.x * bufferWidth;
1926
+ var y = bufferHeight - viewport.y * bufferHeight - height;
1927
+ this.viewport(x, y, width, height);
1928
+ this.scissor(x, y, width, height);
1812
1929
  };
1813
1930
  _proto.activeTexture = function activeTexture(textureID) {
1814
1931
  if (this._activeTextureID !== textureID) {
@@ -1837,8 +1954,57 @@ exports.WebGLMode = void 0;
1837
1954
  _proto.flush = function flush() {
1838
1955
  this._gl.flush();
1839
1956
  };
1840
- _proto.destroy = function destroy() {};
1841
- _create_class(WebGLRenderer, [
1957
+ _proto.forceLoseDevice = function forceLoseDevice() {
1958
+ var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1959
+ extension.loseContext();
1960
+ };
1961
+ _proto.forceRestoreDevice = function forceRestoreDevice() {
1962
+ var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1963
+ extension.restoreContext();
1964
+ };
1965
+ _proto.resetState = function resetState() {
1966
+ this._readFrameBuffer = null;
1967
+ this._enableGlobalDepthBias = false;
1968
+ this._currentBindShaderProgram = null;
1969
+ var activeTextures = this._activeTextures;
1970
+ for(var i = 0, n = activeTextures.length; i < n; i++){
1971
+ activeTextures[i] = null;
1972
+ }
1973
+ this._lastViewport.set(null, null, null, null);
1974
+ this._lastScissor.set(null, null, null, null);
1975
+ this._lastClearColor.set(null, null, null, null);
1976
+ this._scissorEnable = false;
1977
+ this._initGLState(this._gl);
1978
+ };
1979
+ _proto._initGLState = function _initGLState(gl) {
1980
+ this._activeTextureID = gl.TEXTURE0;
1981
+ this._renderStates = new GLRenderStates(gl);
1982
+ this._extensions = new GLExtensions(this);
1983
+ this._capability = new GLCapability(this);
1984
+ // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1985
+ gl.activeTexture(gl.TEXTURE0);
1986
+ var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1987
+ if (debugRenderInfo != null) {
1988
+ this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
1989
+ }
1990
+ };
1991
+ _proto.destroy = function destroy() {
1992
+ var webCanvas = this._webCanvas;
1993
+ webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1994
+ webCanvas.removeEventListener("webglcontextlost", this._onWebGLContextLost, false);
1995
+ webCanvas.removeEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1996
+ };
1997
+ _proto._onContextCreationError = function _onContextCreationError(event) {
1998
+ console.error("WebGLRenderer: WebGL context could not be created. Reason: ", event.statusMessage);
1999
+ };
2000
+ _proto._onWebGLContextLost = function _onWebGLContextLost(event) {
2001
+ event.preventDefault();
2002
+ this._onDeviceLost();
2003
+ };
2004
+ _proto._onWebGLContextRestored = function _onWebGLContextRestored(event) {
2005
+ this._onDeviceRestored();
2006
+ };
2007
+ _create_class(WebGLGraphicDevice, [
1842
2008
  {
1843
2009
  key: "isWebGL2",
1844
2010
  get: function get() {
@@ -1879,32 +2045,10 @@ exports.WebGLMode = void 0;
1879
2045
  }
1880
2046
  }
1881
2047
  ]);
1882
- return WebGLRenderer;
2048
+ return WebGLGraphicDevice;
1883
2049
  }();
1884
2050
 
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
2051
  exports.WebCanvas = WebCanvas;
1908
2052
  exports.WebGLEngine = WebGLEngine;
1909
- exports.WebGLRenderer = WebGLRenderer;
2053
+ exports.WebGLGraphicDevice = WebGLGraphicDevice;
1910
2054
  //# sourceMappingURL=main.js.map