@galacean/engine-rhi-webgl 1.0.0-alpha.6 → 1.0.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
@@ -64,27 +64,27 @@ var engineCore = require('@galacean/engine-core');
64
64
  })(exports.GLCompressedTextureInternalFormat || (exports.GLCompressedTextureInternalFormat = {}));
65
65
 
66
66
  function _defineProperties(target, props) {
67
- for (var i = 0; i < props.length; i++) {
68
- var descriptor = props[i];
69
- descriptor.enumerable = descriptor.enumerable || false;
70
- descriptor.configurable = true;
71
- if ("value" in descriptor) descriptor.writable = true;
72
- Object.defineProperty(target, descriptor.key, descriptor);
73
- }
67
+ for (var i = 0; i < props.length; i++) {
68
+ var descriptor = props[i];
69
+ descriptor.enumerable = descriptor.enumerable || false;
70
+ descriptor.configurable = true;
71
+
72
+ if ("value" in descriptor) descriptor.writable = true;
73
+
74
+ Object.defineProperty(target, descriptor.key, descriptor);
75
+ }
74
76
  }
77
+ function _create_class(Constructor, protoProps, staticProps) {
78
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
79
+ if (staticProps) _defineProperties(Constructor, staticProps);
75
80
 
76
- function _createClass(Constructor, protoProps, staticProps) {
77
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
78
- if (staticProps) _defineProperties(Constructor, staticProps);
79
- return Constructor;
81
+ return Constructor;
80
82
  }
81
83
 
82
84
  function _instanceof(left, right) {
83
- if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
84
- return !!right[Symbol.hasInstance](left);
85
- } else {
86
- return left instanceof right;
87
- }
85
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
86
+ return !!right[Symbol.hasInstance](left);
87
+ } else return left instanceof right;
88
88
  }
89
89
 
90
90
  /**
@@ -118,7 +118,7 @@ function _instanceof(left, right) {
118
118
  this._scale.set(x, y);
119
119
  this.scale = this._scale;
120
120
  };
121
- _createClass(WebCanvas, [
121
+ _create_class(WebCanvas, [
122
122
  {
123
123
  key: "width",
124
124
  get: /**
@@ -171,164 +171,38 @@ function _instanceof(left, right) {
171
171
  return WebCanvas;
172
172
  }();
173
173
 
174
- function setPrototypeOf(o, p) {
175
- setPrototypeOf = Object.setPrototypeOf || function setPrototypeOf(o, p) {
176
- o.__proto__ = p;
177
- return o;
178
- };
174
+ function _set_prototype_of(o, p) {
175
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
176
+ o.__proto__ = p;
179
177
 
180
- return setPrototypeOf(o, p);
181
- }
178
+ return o;
179
+ };
182
180
 
183
- function _setPrototypeOf(o, p) {
184
- return setPrototypeOf(o, p);
181
+ return _set_prototype_of(o, p);
185
182
  }
186
183
 
187
184
  function _inherits(subClass, superClass) {
188
- if (typeof superClass !== "function" && superClass !== null) {
189
- throw new TypeError("Super expression must either be null or a function");
190
- }
191
-
192
- subClass.prototype = Object.create(superClass && superClass.prototype, {
193
- constructor: {
194
- value: subClass,
195
- writable: true,
196
- configurable: true
197
- }
198
- });
199
- if (superClass) _setPrototypeOf(subClass, superClass);
200
- }
201
-
202
- /**
203
- * WebGL platform engine,support includes WebGL1.0 and WebGL2.0.
204
- */ var WebGLEngine = /*#__PURE__*/ function(Engine) {
205
- _inherits(WebGLEngine, Engine);
206
- function WebGLEngine() {
207
- return Engine.apply(this, arguments);
185
+ if (typeof superClass !== "function" && superClass !== null) {
186
+ throw new TypeError("Super expression must either be null or a function");
208
187
  }
209
- /**
210
- * Create a WebGL engine.
211
- * @param configuration - WebGL engine configuration
212
- * @returns A promise that will resolve when the engine is created
213
- */ WebGLEngine.create = function create(configuration) {
214
- var canvas = configuration.canvas;
215
- var webCanvas = new WebCanvas(typeof canvas === "string" ? document.getElementById(canvas) : canvas);
216
- var webGLRenderer = new WebGLGraphicDevice(configuration.graphicDeviceOptions);
217
- var engine = new WebGLEngine(webCanvas, webGLRenderer, configuration);
218
- return engine._initialize(configuration);
219
- };
220
- _createClass(WebGLEngine, [
221
- {
222
- key: "canvas",
223
- get: /**
224
- * Web canvas.
225
- */ function get() {
226
- return this._canvas;
227
- }
228
- }
229
- ]);
230
- return WebGLEngine;
231
- }(engineCore.Engine);
232
188
 
233
- function extends_() {
234
- extends_ = Object.assign || function (target) {
235
- for (var i = 1; i < arguments.length; i++) {
236
- var source = arguments[i];
189
+ subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } });
237
190
 
238
- for (var key in source) {
239
- if (Object.prototype.hasOwnProperty.call(source, key)) {
240
- target[key] = source[key];
241
- }
242
- }
243
- }
244
-
245
- return target;
246
- };
247
-
248
- return extends_.apply(this, arguments);
191
+ if (superClass) _set_prototype_of(subClass, superClass);
249
192
  }
250
193
 
251
194
  function _extends() {
252
- return extends_.apply(this, arguments);
253
- }
254
-
255
- var GLBuffer = /*#__PURE__*/ function() {
256
- function GLBuffer(rhi, type, byteLength, bufferUsage, data) {
257
- if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
258
- var gl = rhi.gl;
259
- var glBuffer = gl.createBuffer();
260
- var glBufferUsage = this._getGLBufferUsage(gl, bufferUsage);
261
- var glBindTarget = type === engineCore.BufferBindFlag.VertexBuffer ? gl.ARRAY_BUFFER : gl.ELEMENT_ARRAY_BUFFER;
262
- this._gl = gl;
263
- this._glBuffer = glBuffer;
264
- this._glBufferUsage = glBufferUsage;
265
- this._glBindTarget = glBindTarget;
266
- this._isWebGL2 = rhi.isWebGL2;
267
- this.bind();
268
- if (data) {
269
- gl.bufferData(glBindTarget, data, glBufferUsage);
270
- } else {
271
- gl.bufferData(glBindTarget, byteLength, glBufferUsage);
272
- }
273
- gl.bindBuffer(glBindTarget, null);
274
- }
275
- var _proto = GLBuffer.prototype;
276
- _proto.bind = function bind() {
277
- this._gl.bindBuffer(this._glBindTarget, this._glBuffer);
278
- };
279
- _proto.setData = function setData(byteLength, data, bufferByteOffset, dataOffset, dataLength, options) {
280
- var gl = this._gl;
281
- var glBindTarget = this._glBindTarget;
282
- this.bind();
283
- if (options === engineCore.SetDataOptions.Discard) {
284
- gl.bufferData(glBindTarget, byteLength, this._glBufferUsage);
285
- }
286
- // TypeArray is BYTES_PER_ELEMENT, unTypeArray is 1
287
- var byteSize = data.BYTES_PER_ELEMENT || 1;
288
- var dataByteLength = dataLength ? byteSize * dataLength : data.byteLength;
289
- if (dataOffset !== 0 || dataByteLength < data.byteLength) {
290
- var isArrayBufferView = data.byteOffset !== undefined;
291
- if (this._isWebGL2 && isArrayBufferView) {
292
- gl.bufferSubData(glBindTarget, bufferByteOffset, data, dataOffset, dataByteLength / byteSize);
293
- } else {
294
- var subData = new Uint8Array(isArrayBufferView ? data.buffer : data, dataOffset * byteSize, dataByteLength);
295
- gl.bufferSubData(glBindTarget, bufferByteOffset, subData);
296
- }
297
- } else {
298
- gl.bufferSubData(glBindTarget, bufferByteOffset, data);
299
- }
300
- gl.bindBuffer(glBindTarget, null);
301
- };
302
- _proto.getData = function getData(data, bufferByteOffset, dataOffset, dataLength) {
303
- if (this._isWebGL2) {
304
- var gl = this._gl;
305
- this.bind();
306
- gl.getBufferSubData(this._glBindTarget, bufferByteOffset, data, dataOffset, dataLength);
307
- } else {
308
- throw "Buffer is write-only on WebGL1.0 platforms.";
309
- }
310
- };
311
- _proto.resize = function resize(byteLength) {
312
- this.bind();
313
- this._gl.bufferData(this._glBindTarget, byteLength, this._glBufferUsage);
314
- };
315
- _proto.destroy = function destroy() {
316
- this._gl.deleteBuffer(this._glBuffer);
317
- this._gl = null;
318
- this._glBuffer = null;
319
- };
320
- _proto._getGLBufferUsage = function _getGLBufferUsage(gl, bufferUsage) {
321
- switch(bufferUsage){
322
- case engineCore.BufferUsage.Static:
323
- return gl.STATIC_DRAW;
324
- case engineCore.BufferUsage.Dynamic:
325
- return gl.DYNAMIC_DRAW;
326
- case engineCore.BufferUsage.Stream:
327
- return gl.STREAM_DRAW;
195
+ _extends = Object.assign || function assign(target) {
196
+ for (var i = 1; i < arguments.length; i++) {
197
+ var source = arguments[i];
198
+ for (var key in source) if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
328
199
  }
200
+
201
+ return target;
329
202
  };
330
- return GLBuffer;
331
- }();
203
+
204
+ return _extends.apply(this, arguments);
205
+ }
332
206
 
333
207
  /**
334
208
  * GL capability.
@@ -466,7 +340,7 @@ var GLBuffer = /*#__PURE__*/ function() {
466
340
  TEXTURE_MAX_ANISOTROPY_EXT: "TEXTURE_MAX_ANISOTROPY_EXT"
467
341
  });
468
342
  };
469
- _createClass(GLCapability, [
343
+ _create_class(GLCapability, [
470
344
  {
471
345
  key: "maxTextureSize",
472
346
  get: function get() {
@@ -599,8 +473,8 @@ var GLBuffer = /*#__PURE__*/ function() {
599
473
  if (useVao) {
600
474
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
601
475
  } else {
602
- var _glBuffer = _indexBufferBinding.buffer._platformBuffer._glBuffer;
603
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer);
476
+ var _nativeBuffer = _indexBufferBinding.buffer._nativeBuffer;
477
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer);
604
478
  gl.drawElements(topology, count, _glIndexType, start * _glIndexByteCount);
605
479
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
606
480
  }
@@ -613,8 +487,8 @@ var GLBuffer = /*#__PURE__*/ function() {
613
487
  if (useVao) {
614
488
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
615
489
  } else {
616
- var _glBuffer1 = _indexBufferBinding.buffer._platformBuffer._glBuffer;
617
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _glBuffer1);
490
+ var _nativeBuffer1 = _indexBufferBinding.buffer._nativeBuffer;
491
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _nativeBuffer1);
618
492
  gl.drawElementsInstanced(topology, count, _glIndexType, start * _glIndexByteCount, _instanceCount);
619
493
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
620
494
  }
@@ -654,7 +528,7 @@ var GLBuffer = /*#__PURE__*/ function() {
654
528
  var element = attributes[name];
655
529
  if (element) {
656
530
  var _vertexBufferBindings_element_bindingIndex = vertexBufferBindings[element.bindingIndex], buffer = _vertexBufferBindings_element_bindingIndex.buffer, stride = _vertexBufferBindings_element_bindingIndex.stride;
657
- vbo = buffer._platformBuffer._glBuffer;
531
+ vbo = buffer._nativeBuffer;
658
532
  // prevent binding the vbo which already bound at the last loop, e.g. a buffer with multiple attributes.
659
533
  if (lastBoundVbo !== vbo) {
660
534
  lastBoundVbo = vbo;
@@ -686,7 +560,7 @@ var GLBuffer = /*#__PURE__*/ function() {
686
560
  // @ts-ignore
687
561
  var _indexBufferBinding = this._primitive._indexBufferBinding;
688
562
  if (_indexBufferBinding) {
689
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._platformBuffer._glBuffer);
563
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _indexBufferBinding.buffer._nativeBuffer);
690
564
  }
691
565
  this._bindBufferAndAttrib(shaderProgram);
692
566
  /** unbind */ gl.bindVertexArray(null);
@@ -1220,7 +1094,7 @@ var GLBuffer = /*#__PURE__*/ function() {
1220
1094
  }
1221
1095
  return true;
1222
1096
  };
1223
- _createClass(GLTexture, [
1097
+ _create_class(GLTexture, [
1224
1098
  {
1225
1099
  key: "wrapModeU",
1226
1100
  set: /**
@@ -1751,19 +1625,20 @@ exports.WebGLMode = void 0;
1751
1625
  WebGLMode[WebGLMode[/** WebGL1.0, */ "WebGL1"] = 2] = "WebGL1";
1752
1626
  })(exports.WebGLMode || (exports.WebGLMode = {}));
1753
1627
  /**
1754
- * WebGL graphic device, including WebGL1.0 and WebGL2.0.
1755
- */ var WebGLGraphicDevice = /*#__PURE__*/ function() {
1756
- function WebGLGraphicDevice(initializeOptions) {
1628
+ * WebGL renderer, including WebGL1.0 and WebGL2.0.
1629
+ */ var WebGLRenderer = /*#__PURE__*/ function() {
1630
+ function WebGLRenderer(initializeOptions) {
1757
1631
  if (initializeOptions === void 0) initializeOptions = {};
1758
- /** @internal */ this._readFrameBuffer = null;
1759
1632
  /** @internal */ this._enableGlobalDepthBias = false;
1760
1633
  this._activeTextures = new Array(32);
1634
+ // cache value
1761
1635
  this._lastViewport = new engineMath.Vector4(null, null, null, null);
1762
1636
  this._lastScissor = new engineMath.Vector4(null, null, null, null);
1763
1637
  this._lastClearColor = new engineMath.Color(null, null, null, null);
1764
1638
  this._scissorEnable = false;
1765
1639
  var options = _extends({
1766
1640
  webGLMode: 0,
1641
+ alpha: false,
1767
1642
  stencil: true,
1768
1643
  _forceFlush: false
1769
1644
  }, initializeOptions);
@@ -1778,20 +1653,12 @@ exports.WebGLMode = void 0;
1778
1653
  }
1779
1654
  }
1780
1655
  this._options = options;
1781
- this._onWebGLContextLost = this._onWebGLContextLost.bind(this);
1782
- this._onWebGLContextRestored = this._onWebGLContextRestored.bind(this);
1783
1656
  }
1784
- var _proto = WebGLGraphicDevice.prototype;
1785
- _proto.init = function init(canvas, onDeviceLost, onDeviceRestored) {
1657
+ var _proto = WebGLRenderer.prototype;
1658
+ _proto.init = function init(canvas) {
1786
1659
  var options = this._options;
1787
- var webCanvas = canvas._webCanvas;
1660
+ var webCanvas = this._webCanvas = canvas._webCanvas;
1788
1661
  var webGLMode = options.webGLMode;
1789
- this._onDeviceLost = onDeviceLost;
1790
- this._onDeviceRestored = onDeviceRestored;
1791
- webCanvas.addEventListener("webglcontextlost", this._onWebGLContextLost, false);
1792
- webCanvas.addEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
1793
- webCanvas.addEventListener("webglcontextcreationerror", this._onContextCreationError, false);
1794
- this._webCanvas = webCanvas;
1795
1662
  var gl;
1796
1663
  if (webGLMode == 0 || webGLMode == 1) {
1797
1664
  gl = webCanvas.getContext("webgl2", options);
@@ -1817,7 +1684,16 @@ exports.WebGLMode = void 0;
1817
1684
  throw new Error("Get GL Context FAILED.");
1818
1685
  }
1819
1686
  this._gl = gl;
1820
- this._initGLState(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
+ }
1821
1697
  };
1822
1698
  _proto.createPlatformPrimitive = function createPlatformPrimitive(primitive) {
1823
1699
  return new GLPrimitive(this, primitive);
@@ -1834,10 +1710,6 @@ exports.WebGLMode = void 0;
1834
1710
  _proto.createPlatformRenderTarget = function createPlatformRenderTarget(target) {
1835
1711
  return new GLRenderTarget(this, target);
1836
1712
  };
1837
- _proto.createPlatformBuffer = function createPlatformBuffer(type, byteLength, bufferUsage, data) {
1838
- if (bufferUsage === void 0) bufferUsage = engineCore.BufferUsage.Static;
1839
- return new GLBuffer(this, type, byteLength, bufferUsage, data);
1840
- };
1841
1713
  _proto.requireExtension = function requireExtension(ext) {
1842
1714
  return this._extensions.requireExtension(ext);
1843
1715
  };
@@ -1965,57 +1837,8 @@ exports.WebGLMode = void 0;
1965
1837
  _proto.flush = function flush() {
1966
1838
  this._gl.flush();
1967
1839
  };
1968
- _proto.forceLoseDevice = function forceLoseDevice() {
1969
- var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1970
- extension.loseContext();
1971
- };
1972
- _proto.forceRestoreDevice = function forceRestoreDevice() {
1973
- var extension = this.requireExtension(engineCore.GLCapabilityType.WEBGL_lose_context);
1974
- extension.restoreContext();
1975
- };
1976
- _proto.resetState = function resetState() {
1977
- this._readFrameBuffer = null;
1978
- this._enableGlobalDepthBias = false;
1979
- this._currentBindShaderProgram = null;
1980
- var activeTextures = this._activeTextures;
1981
- for(var i = 0, n = activeTextures.length; i < n; i++){
1982
- activeTextures[i] = null;
1983
- }
1984
- this._lastViewport.set(null, null, null, null);
1985
- this._lastScissor.set(null, null, null, null);
1986
- this._lastClearColor.set(null, null, null, null);
1987
- this._scissorEnable = false;
1988
- this._initGLState(this._gl);
1989
- };
1990
- _proto._initGLState = function _initGLState(gl) {
1991
- this._activeTextureID = gl.TEXTURE0;
1992
- this._renderStates = new GLRenderStates(gl);
1993
- this._extensions = new GLExtensions(this);
1994
- this._capability = new GLCapability(this);
1995
- // Make sure the active texture in gl context is on default, because gl context may be used in other webgl renderer.
1996
- gl.activeTexture(gl.TEXTURE0);
1997
- var debugRenderInfo = gl.getExtension("WEBGL_debug_renderer_info");
1998
- if (debugRenderInfo != null) {
1999
- this._renderer = gl.getParameter(debugRenderInfo.UNMASKED_RENDERER_WEBGL);
2000
- }
2001
- };
2002
- _proto.destroy = function destroy() {
2003
- var webCanvas = this._webCanvas;
2004
- webCanvas.removeEventListener("webglcontextcreationerror", this._onContextCreationError, false);
2005
- webCanvas.removeEventListener("webglcontextlost", this._onWebGLContextLost, false);
2006
- webCanvas.removeEventListener("webglcontextrestored", this._onWebGLContextRestored, false);
2007
- };
2008
- _proto._onContextCreationError = function _onContextCreationError(event) {
2009
- console.error("WebGLRenderer: WebGL context could not be created. Reason: ", event.statusMessage);
2010
- };
2011
- _proto._onWebGLContextLost = function _onWebGLContextLost(event) {
2012
- event.preventDefault();
2013
- this._onDeviceLost();
2014
- };
2015
- _proto._onWebGLContextRestored = function _onWebGLContextRestored(event) {
2016
- this._onDeviceRestored();
2017
- };
2018
- _createClass(WebGLGraphicDevice, [
1840
+ _proto.destroy = function destroy() {};
1841
+ _create_class(WebGLRenderer, [
2019
1842
  {
2020
1843
  key: "isWebGL2",
2021
1844
  get: function get() {
@@ -2056,10 +1879,32 @@ exports.WebGLMode = void 0;
2056
1879
  }
2057
1880
  }
2058
1881
  ]);
2059
- return WebGLGraphicDevice;
1882
+ return WebGLRenderer;
2060
1883
  }();
2061
1884
 
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
+
2062
1907
  exports.WebCanvas = WebCanvas;
2063
1908
  exports.WebGLEngine = WebGLEngine;
2064
- exports.WebGLGraphicDevice = WebGLGraphicDevice;
1909
+ exports.WebGLRenderer = WebGLRenderer;
2065
1910
  //# sourceMappingURL=main.js.map