galacean / engine

A typescript interactive engine, support 2D, 3D, animation, physics, built on WebGL and glTF.
https://galacean.antgroup.com/engine
MIT License
4.18k stars 299 forks source link

[Performance] Matrix 用 Float32Array 初始性能比 Array 高, 但量大后则不如Array #579

Open deepkolos opened 2 years ago

deepkolos commented 2 years ago

Describe the bug Matrix 用 Float32Array 初始性能比 Array 高, 但量大后则不如Array

To Reproduce Steps to reproduce the behavior:

复现仓库 https://github.com/deepkolos/matrix-f32-array-performace-issue/

复现地址 https://deepkolos.github.io/matrix-f32-array-performace-issue/

Desktop (please complete the following information):

Additional context https://github.com/toji/gl-matrix/issues/359 https://github.com/akira-cn/babel-plugin-transform-gl-matrix/blob/master/README-CN.md

singlecoder commented 2 years ago

Describe the bug Matrix 用 Float32Array 初始性能比 Array 高, 但量大后则不如Array

To Reproduce Steps to reproduce the behavior:

复现仓库 https://github.com/deepkolos/matrix-f32-array-performace-issue/

复现地址 https://deepkolos.github.io/matrix-f32-array-performace-issue/

Desktop (please complete the following information):

  • OS: Windows
  • Browser Chrome
  • Version 98

Additional context toji/gl-matrix#359 https://github.com/akira-cn/babel-plugin-transform-gl-matrix/blob/master/README-CN.md

先手动为实践点赞,测试很严谨,从 CPU 的角度来说,量大 Array 性能确实会比 Float32Array 快,作为引擎里面的数学库,我们除了从 CPU 的角度考虑外,还需要考虑 CPU 上传数据 (比如:矩阵的 uniform) 到 GPU 的开销,这部分差异很大 (相对来说 CPU 带来的性能差异就没有那么明显了),综合下来看,采用 Float32Array 整体性能会更好~

GuoLei1990 commented 2 years ago

Do a performance test between gl.uniformMatrix4fv( Float32Array) and gl.uniformMatrix4fv( Array<number>)