I ran the sample. Yep, the native is slower than web on my iPhone 11. The reason are:
- rendering time is the time of
Root.frameMove()
// Director.mainLoop()
this.emit(Director.EVENT_BEFORE_DRAW);
this._root!.frameMove(this._deltaTime);
this.emit(Director.EVENT_AFTER_DRAW);
// Root.frameMove()
public frameMove(deltaTime: number) {
for (let i = 0; i < scenes.length; i++) {
scenes[i].update(stamp);
}
...
this._pipeline.render(cameraList);
this._device.present();
}
Only this._pipeline.render()
and this._device.present
are implemented in native, other codes are implemented in TS. As want to share memory between TS and c++, engine uses memory pool to record class members, some datas are recored twice, for example,
// Model.ts
public createBoundingShape (minPos?: Vec3, maxPos?: Vec3) {
if (!minPos || !maxPos) { return; }
this._modelBounds = AABB.fromPoints(AABB.create(), minPos, maxPos);
this._worldBounds = AABB.clone(this._modelBounds);
if (this._hWorldBounds === NULL_HANDLE) {
this._hWorldBounds = AABBPool.alloc();
ModelPool.set(this._handle, ModelView.WORLD_BOUNDS, this._hWorldBounds);
}
AABBPool.setVec3(this._hWorldBounds, AABBView.CENTER, this._worldBounds.center);
AABBPool.setVec3(this._hWorldBounds, AABBView.HALF_EXTENSION, this._worldBounds.halfExtents);
}
ModelPool and AABBPool are TypedArrays created by c++. Set/get data in TypedArray is slow.
If i only record the time of this._pipeline.render()
and this._device.present()
, then native is faster than web. So we should do more optimization in future.