@galacean/engine-rhi-webgl 1.0.0 → 1.0.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
@@ -191,6 +191,39 @@ 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
+ return engine._initialize(configuration);
212
+ };
213
+ _create_class(WebGLEngine, [
214
+ {
215
+ key: "canvas",
216
+ get: /**
217
+ * Web canvas.
218
+ */ function get() {
219
+ // @ts-ignore
220
+ return this._canvas;
221
+ }
222
+ }
223
+ ]);
224
+ return WebGLEngine;
225
+ }(engineCore.Engine);
226
+
194
227
  function _extends() {
195
228
  _extends = Object.assign || function assign(target) {
196
229
  for (var i = 1; i < arguments.length; i++) {
@@ -204,6 +237,84 @@ function _extends() {
204
237
  return _extends.apply(this, arguments);
205
238
  }
206
239
 
240
+ var GLBuffer = /*#__PURE__*/ function() {
241
+ function GLBuffer(rhi, type, byteLength, bufferUsage, data) {
242
+ if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
243
+ var gl = rhi.gl;
244
+ var glBuffer = gl.createBuffer();
245
+ var glBufferUsage = this._getGLBufferUsage(gl, bufferUsage);
246
+ var glBindTarget = type === engineCore.BufferBindFlag.VertexBuffer ? gl.ARRAY_BUFFER : gl.ELEMENT_ARRAY_BUFFER;
247
+ this._gl = gl;
248
+ this._glBuffer = glBuffer;
249
+ this._glBufferUsage = glBufferUsage;
250
+ this._glBindTarget = glBindTarget;
251
+ this._isWebGL2 = rhi.isWebGL2;
252
+ this.bind();
253
+ if (data) {
254
+ gl.bufferData(glBindTarget, data, glBufferUsage);
255
+ } else {
256
+ gl.bufferData(glBindTarget, byteLength, glBufferUsage);
257
+ }
258
+ gl.bindBuffer(glBindTarget, null);
259
+ }
260
+ var _proto = GLBuffer.prototype;
261
+ _proto.bind = function bind() {
262
+ this._gl.bindBuffer(this._glBindTarget, this._glBuffer);
263
+ };
264
+ _proto.setData = function setData(byteLength, data, bufferByteOffset, dataOffset, dataLength, options) {
265
+ var gl = this._gl;
266
+ var glBindTarget = this._glBindTarget;
267
+ this.bind();
268
+ if (options === engineCore.SetDataOptions.Discard) {
269
+ gl.bufferData(glBindTarget, byteLength, this._glBufferUsage);
270
+ }
271
+ // TypeArray is BYTES_PER_ELEMENT, unTypeArray is 1
272
+ var byteSize = data.BYTES_PER_ELEMENT || 1;
273
+ var dataByteLength = dataLength ? byteSize * dataLength : data.byteLength;
274
+ if (dataOffset !== 0 || dataByteLength < data.byteLength) {
275
+ var isArrayBufferView = data.byteOffset !== undefined;
276
+ if (this._isWebGL2 && isArrayBufferView) {
277
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data, dataOffset, dataByteLength / byteSize);
278
+ } else {
279
+ var subData = new Uint8Array(isArrayBufferView ? data.buffer : data, dataOffset * byteSize, dataByteLength);
280
+ gl.bufferSubData(glBindTarget, bufferByteOffset, subData);
281
+ }
282
+ } else {
283
+ gl.bufferSubData(glBindTarget, bufferByteOffset, data);
284
+ }
285
+ gl.bindBuffer(glBindTarget, null);
286
+ };
287
+ _proto.getData = function getData(data, bufferByteOffset, dataOffset, dataLength) {
288
+ if (this._isWebGL2) {
289
+ var gl = this._gl;
290
+ this.bind();
291
+ gl.getBufferSubData(this._glBindTarget, bufferByteOffset, data, dataOffset, dataLength);
292
+ } else {
293
+ throw "Buffer is write-only on WebGL1.0 platforms.";
294
+ }
295
+ };
296
+ _proto.resize = function resize(byteLength) {
297
+ this.bind();
298
+ this._gl.bufferData(this._glBindTarget, byteLength, this._glBufferUsage);
299
+ };
300
+ _proto.destroy = function destroy() {
301
+ this._gl.deleteBuffer(this._glBuffer);
302
+ this._gl = null;
303
+ this._glBuffer = null;
304
+ };
305
+ _proto._getGLBufferUsage = function _getGLBufferUsage(gl, bufferUsage) {
306
+ switch(bufferUsage){
307
+ case engineCore.BufferUsage.Static:
308
+ return gl.STATIC_DRAW;
309
+ case engineCore.BufferUsage.Dynamic:
310
+ return gl.DYNAMIC_DRAW;
311
+ case engineCore.BufferUsage.Stream:
312
+ return gl.STREAM_DRAW;
313
+ }
314
+ };
315
+ return GLBuffer;
316
+ }();
317
+
207
318
  /**
208
319
  * GL capability.
209
320
  */ var GLCapability = /*#__PURE__*/ function() {
@@ -473,8 +584,8 @@ function _extends() {
473
584
  if (useVao) {
474
585
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
475
586
  } else {
476
- var _nativeBuffer = _indexBufferBinding.buffer._nativeBuffer;
477
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer);
587
+ var _glBuffer = _indexBufferBinding.buffer._platformBuffer._glBuffer;
588
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer);
478
589
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
479
590
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
480
591
  }
@@ -487,8 +598,8 @@ function _extends() {
487
598
  if (useVao) {
488
599
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
489
600
  } else {
490
- var _nativeBuffer1 = _indexBufferBinding.buffer._nativeBuffer;
491
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer1);
601
+ var _glBuffer1 = _indexBufferBinding.buffer._platformBuffer._glBuffer;
602
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer1);
492
603
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
493
604
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
494
605
  }
@@ -528,7 +639,7 @@ function _extends() {
528
639
  var element = attributes[name];
529
640
  if (element) {
530
641
  var _vertexBufferBindings_element_bindingIndex = vertexBufferBindings[element.bindingIndex], buffer = _vertexBufferBindings_element_bindingIndex.buffer, stride = _vertexBufferBindings_element_bindingIndex.stride;
531
- vbo = buffer._nativeBuffer;
642
+ vbo = buffer._platformBuffer._glBuffer;
532
643
  // prevent binding the vbo which already bound at the last loop, e.g. a buffer with multiple attributes.
533
644
  if (lastBoundVbo !== vbo) {
534
645
  lastBoundVbo = vbo;
@@ -560,7 +671,7 @@ function _extends() {
560
671
  // @ts-ignore
561
672
  var _indexBufferBinding = this._primitive._indexBufferBinding;
562
673
  if (_indexBufferBinding) {
563
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._nativeBuffer);
674
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._platformBuffer._glBuffer);
564
675
  }
565
676
  this._bindBufferAndAttrib(shaderProgram);
566
677
  /** unbind */ gl.bindVertexArray(null);
@@ -667,9 +778,9 @@ function _extends() {
667
778
  var isWebGL2 = this._isWebGL2;
668
779
  var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
669
780
  // @ts-ignore
670
- var _this__texture = this._texture, mipmapCount = _this__texture.mipmapCount, width = _this__texture.width, height = _this__texture.height, _isDepthTexture = _this__texture._isDepthTexture;
781
+ 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
782
  this._bind();
672
- if (isWebGL2 && !(baseFormat === gl.LUMINANCE_ALPHA || baseFormat === gl.ALPHA)) {
783
+ if (isWebGL2 && !(baseFormat === gl.LUMINANCE_ALPHA || baseFormat === gl.ALPHA) && usage !== engineCore.TextureUsage.Dynamic) {
673
784
  gl.texStorage2D(this._target, mipmapCount, internalFormat, width, height);
674
785
  } else {
675
786
  if (!isCube) {
@@ -1464,11 +1575,15 @@ function _extends() {
1464
1575
  * {@inheritDoc IPlatformTexture2D.setImageSource}
1465
1576
  */ _proto.setImageSource = function setImageSource(imageSource, mipLevel, flipY, premultiplyAlpha, x, y) {
1466
1577
  var gl = this._gl;
1467
- var _this__formatDetail = this._formatDetail, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1578
+ var _this__formatDetail = this._formatDetail, internalFormat = _this__formatDetail.internalFormat, baseFormat = _this__formatDetail.baseFormat, dataType = _this__formatDetail.dataType;
1468
1579
  this._bind();
1469
1580
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, +flipY);
1470
1581
  gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, +premultiplyAlpha);
1471
- gl.texSubImage2D(this._target, mipLevel, x || 0, y || 0, baseFormat, dataType, imageSource);
1582
+ if (this._texture.usage === engineCore.TextureUsage.Dynamic) {
1583
+ gl.texImage2D(this._target, mipLevel, internalFormat, baseFormat, dataType, imageSource);
1584
+ } else {
1585
+ gl.texSubImage2D(this._target, mipLevel, x || 0, y || 0, baseFormat, dataType, imageSource);
1586
+ }
1472
1587
  };
1473
1588
  /**
1474
1589
  * {@inheritDoc IPlatformTexture2D.getPixelBuffer }
@@ -1625,22 +1740,22 @@ 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
- _forceFlush: false
1757
+ _forceFlush: false,
1758
+ _maxAllowSkinUniformVectorCount: 256
1644
1759
  }, initializeOptions);
1645
1760
  if (engineCore.SystemInfo.platform === engineCore.Platform.IPhone || engineCore.SystemInfo.platform === engineCore.Platform.IPad) {
1646
1761
  var version = engineCore.SystemInfo.operatingSystem.match(/(\d+).?(\d+)?.?(\d+)?/);
@@ -1653,12 +1768,20 @@ exports.WebGLMode = void 0;
1653
1768
  }
1654
1769
  }
1655
1770
  this._options = options;
1771
+ this._onWebGLContextLost = this._onWebGLContextLost.bind(this);
1772
+ this._onWebGLContextRestored = this._onWebGLContextRestored.bind(this);
1656
1773
  }
1657
- var _proto = WebGLRenderer.prototype;
1658
- _proto.init = function init(canvas) {
1774
+ var _proto = WebGLGraphicDevice.prototype;
1775
+ _proto.init = function init(canvas, onDeviceLost, onDeviceRestored) {
1659
1776
  var options = this._options;
1660
- var webCanvas = this._webCanvas = canvas._webCanvas;
1777
+ var webCanvas = canvas._webCanvas;
1661
1778
  var webGLMode = options.webGLMode;
1779
+ this._onDeviceLost = onDeviceLost;
1780
+ this._onDeviceRestored = onDeviceRestored;
1781
+ webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1782
+ webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1783
+ webCanvas.addEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1784
+ this._webCanvas = webCanvas;
1662
1785
  var gl;
1663
1786
  if (webGLMode == 0 || webGLMode == 1) {
1664
1787
  gl = webCanvas.getContext("webgl2", options);
@@ -1684,16 +1807,7 @@ exports.WebGLMode = void 0;
1684
1807
  throw new Error("Get GL Context FAILED.");
1685
1808
  }
1686
1809
  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
- }
1810
+ this._initGLState(gl);
1697
1811
  };
1698
1812
  _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1699
1813
  return new GLPrimitive(this, primitive);
@@ -1710,6 +1824,10 @@ exports.WebGLMode = void 0;
1710
1824
  _proto.createPlatformRenderTarget = function createPlatformRenderTarget(target) {
1711
1825
  return new GLRenderTarget(this, target);
1712
1826
  };
1827
+ _proto.createPlatformBuffer = function createPlatformBuffer(type, byteLength, bufferUsage, data) {
1828
+ if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
1829
+ return new GLBuffer(this, type, byteLength, bufferUsage, data);
1830
+ };
1713
1831
  _proto.requireExtension = function requireExtension(ext) {
1714
1832
  return this._extensions.requireExtension(ext);
1715
1833
  };
@@ -1792,23 +1910,23 @@ exports.WebGLMode = void 0;
1792
1910
  };
1793
1911
  _proto.activeRenderTarget = function activeRenderTarget(renderTarget, viewport, mipLevel) {
1794
1912
  var gl = this._gl;
1913
+ var bufferWidth, bufferHeight;
1795
1914
  if (renderTarget) {
1796
1915
  var /** @ts-ignore */ _renderTarget__platformRenderTarget;
1797
1916
  (_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);
1917
+ bufferWidth = renderTarget.width >> mipLevel;
1918
+ bufferHeight = renderTarget.height >> mipLevel;
1802
1919
  } else {
1803
1920
  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);
1921
+ bufferWidth = gl.drawingBufferWidth;
1922
+ bufferHeight = gl.drawingBufferHeight;
1811
1923
  }
1924
+ var width = bufferWidth * viewport.z;
1925
+ var height = bufferHeight * viewport.w;
1926
+ var x = viewport.x * bufferWidth;
1927
+ var y = bufferHeight - viewport.y * bufferHeight - height;
1928
+ this.viewport(x, y, width, height);
1929
+ this.scissor(x, y, width, height);
1812
1930
  };
1813
1931
  _proto.activeTexture = function activeTexture(textureID) {
1814
1932
  if (this._activeTextureID !== textureID) {
@@ -1837,8 +1955,57 @@ exports.WebGLMode = void 0;
1837
1955
  _proto.flush = function flush() {
1838
1956
  this._gl.flush();
1839
1957
  };
1840
- _proto.destroy = function destroy() {};
1841
- _create_class(WebGLRenderer, [
1958
+ _proto.forceLoseDevice = function forceLoseDevice() {
1959
+ var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1960
+ extension.loseContext();
1961
+ };
1962
+ _proto.forceRestoreDevice = function forceRestoreDevice() {
1963
+ var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1964
+ extension.restoreContext();
1965
+ };
1966
+ _proto.resetState = function resetState() {
1967
+ this._readFrameBuffer = null;
1968
+ this._enableGlobalDepthBias = false;
1969
+ this._currentBindShaderProgram = null;
1970
+ var activeTextures = this._activeTextures;
1971
+ for(var i = 0, n = activeTextures.length; i < n; i++){
1972
+ activeTextures[i] = null;
1973
+ }
1974
+ this._lastViewport.set(null, null, null, null);
1975
+ this._lastScissor.set(null, null, null, null);
1976
+ this._lastClearColor.set(null, null, null, null);
1977
+ this._scissorEnable = false;
1978
+ this._initGLState(this._gl);
1979
+ };
1980
+ _proto._initGLState = function _initGLState(gl) {
1981
+ this._activeTextureID = gl.TEXTURE0;
1982
+ this._renderStates = new GLRenderStates(gl);
1983
+ this._extensions = new GLExtensions(this);
1984
+ this._capability = new GLCapability(this);
1985
+ // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1986
+ gl.activeTexture(gl.TEXTURE0);
1987
+ var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1988
+ if (debugRenderInfo != null) {
1989
+ this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
1990
+ }
1991
+ };
1992
+ _proto.destroy = function destroy() {
1993
+ var webCanvas = this._webCanvas;
1994
+ webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1995
+ webCanvas.removeEventListener("webglcontextlost", this._onWebGLContextLost, false);
1996
+ webCanvas.removeEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1997
+ };
1998
+ _proto._onContextCreationError = function _onContextCreationError(event) {
1999
+ console.error("WebGLRenderer: WebGL context could not be created. Reason: ", event.statusMessage);
2000
+ };
2001
+ _proto._onWebGLContextLost = function _onWebGLContextLost(event) {
2002
+ event.preventDefault();
2003
+ this._onDeviceLost();
2004
+ };
2005
+ _proto._onWebGLContextRestored = function _onWebGLContextRestored(event) {
2006
+ this._onDeviceRestored();
2007
+ };
2008
+ _create_class(WebGLGraphicDevice, [
1842
2009
  {
1843
2010
  key: "isWebGL2",
1844
2011
  get: function get() {
@@ -1879,32 +2046,10 @@ exports.WebGLMode = void 0;
1879
2046
  }
1880
2047
  }
1881
2048
  ]);
1882
- return WebGLRenderer;
2049
+ return WebGLGraphicDevice;
1883
2050
  }();
1884
2051
 
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
2052
  exports.WebCanvas = WebCanvas;
1908
2053
  exports.WebGLEngine = WebGLEngine;
1909
- exports.WebGLRenderer = WebGLRenderer;
2054
+ exports.WebGLGraphicDevice = WebGLGraphicDevice;
1910
2055
  //# sourceMappingURL=main.js.map