Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous initialization of Engine #411

Closed
yangfengzzz opened this issue Jul 30, 2021 · 5 comments · Fixed by #1433
Closed

Asynchronous initialization of Engine #411

yangfengzzz opened this issue Jul 30, 2021 · 5 comments · Fixed by #1433
Assignees
Labels
enhancement New feature or request high priority High priority issue
Milestone

Comments

@yangfengzzz
Copy link
Member

yangfengzzz commented Jul 30, 2021

Because WASM loaded is asynchronous,All physical component should be used after WASM binary loaded well.

The initialization of engine and component should be written in the callback function like Laya:

Laya3D.init(0, 0, null, Handler.create(null, () => { xxx }

PR reviewers: @yangfengzzz , @cptbtptpbcptdtptp , @zhuxudong

@yangfengzzz yangfengzzz added enhancement New feature or request high priority High priority issue labels Jul 30, 2021
@yangfengzzz yangfengzzz added this to the 0.6 milestone Jul 30, 2021
@yangfengzzz yangfengzzz mentioned this issue Aug 2, 2021
3 tasks
@GuoLei1990 GuoLei1990 modified the milestones: 0.6, 0.7 Nov 9, 2021
@GuoLei1990 GuoLei1990 added medium priority Medium priority issue and removed high priority High priority issue labels Feb 7, 2022
@GuoLei1990 GuoLei1990 modified the milestones: 0.7, 0.8, 0.9, 1.0 Feb 7, 2022
@GuoLei1990 GuoLei1990 added high priority High priority issue and removed medium priority Medium priority issue labels Nov 14, 2022
@GuoLei1990
Copy link
Member

GuoLei1990 commented Mar 21, 2023

There are several reasons why the engine must consider asynchronous initialization:

  • The initialization of webGPU Device is asynchronous
  • Decoding libraries for some asset loaders, such as ktx2 and mesh_opt
  • The wasm version of the physics engine physX requires asynchronous initialization
  • Other etc.

We don't want the engine's future initialization process to happen then->then->then->.... or awaitl;await;await;...

Here are some initial ideas and designs:

WebGL

Universal version:

const webCanvas = new WebCanvas(htmlCanvas);
const webGLGraphicDevice = new WebGLGraphicDevice({});
const physXPhysics = new PhysXPhysics(PhysXRuntimeMode.Auto);
const configuration = {
  canvas: webCanvas,
  graphicDevice: webGLGraphicDevice,
  physics: physXPhysics,
  ktx2LoaderOptions:<LTX2LoaderOptions>{}
};
const engine = await Engine.create(configuration);

// Destroy physX
physXPhysics.destroy();

Encapsulation version:

const physXPhysics = new PhysXPhysics(PhysXRuntimeMode.Auto);
const configuration = {
  canvas: htmlCanvas,
  physics: physXPhysics,
  graphicDeviceOptions: <WebGLGraphcDeviceOptions>{},
  ktx2LoaderOptions:<LTX2LoaderOptions>{}
};
const engine = await WebGLEngine.create(configuration);

// Destroy physX
physXPhysics.destroy();

WebGPU with multi canvas

Universal version:

const webCanvas1 = new WebGPUCanvas("htmlCanvas1",{...});
const webCanvas2 = new WebGPUCanvas("htmlCanvas2",{...});
const webGPUGraphicDevice = new WebGPUGraphicDevice({...});
const physXPhysics = new PhysXPhysics(PhysXRuntimeMode.Auto);
const configuration = {
  graphicDevice:webGPUGraphicDevice, 
  physics: physXPhysics,
  ktx2LoaderOptions:<LTX2LoaderOptions>{}
};
const engine = await Engine.create(configuration);
......
// Set multi renderTaget to different canvas
camera1.renderTaget = webCanvas1);
camera2.renderTaget = webCanvas2;
// Destroy physX
physXPhysics.destroy();

Encapsulation version:

const webCanvas1 = new WebGPUCanvas("htmlCanvas1",{});
const webCanvas2 = new WebGPUCanvas("htmlCanvas2",{});
const physXPhysics = new PhysXPhysics(PhysXRuntimeMode.Auto);
const configuration = {
  physics: physXPhysics,
  graphicDeviceOptions:<WebGPUGraphcDeviceOptions>{}
  ktx2LoaderOptions:<LTX2LoaderOptions>{}
};
const engine = await WebGPUEngine.create(configuration);
......
// Set multi renderTaget to different canvas
camera1.renderTaget = webCanvas1);
camera2.renderTaget = webCanvas2;
// Destroy physX
physXPhysics.destroy();

PhysX and KTX2Loader implementation:

@resourceLoader(AssetType.Texture2D, ["ktx2"])
class KTX2Loader extends Loader<Texture2D> {
  initialize(): Promise<void> {}
  load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<Texture2D> {}
}

class PhysXPhysics {
  constructor(runtimeMode: PhysXRuntimeMode);
  initialize(): Promise<void> {}
  destroy(): void {}
}

@yangfengzzz @gz65555 Discuss together to see if there are other improvements and potential unmet needs

@luzhuang
Copy link
Contributor

How about letting developers handle asynchrony themselves, like PhysXPhysics now.

Promise.all([KTX2Loader.load(), PhysXPhysics.load()])

When they don't use KTX2 or PhysXPhysic, they can still use the engine in a synchronous way.

@gz65555
Copy link
Collaborator

gz65555 commented Mar 21, 2023

How about letting developers handle asynchrony themselves, like PhysXPhysics now.

Promise.all([KTX2Loader.load(), PhysXPhysics.load()])

When they don't use KTX2 or PhysXPhysic, they can still use the engine in a synchronous way.

@luzhuang Specified configuration can be easily exported by Editor.

@gz65555
Copy link
Collaborator

gz65555 commented Mar 21, 2023

Use ktx2Loader as key will be more precise.

{
  "ktx2Loader": {
    "maxWorker": 4,
    "preCreated": true,
  }
}

How to register a initialized function to be called. @GuoLei1990

@GuoLei1990
Copy link
Member

How about letting developers handle asynchrony themselves, like PhysXPhysics now.

Promise.all([KTX2Loader.load(), PhysXPhysics.load()])

When they don't use KTX2 or PhysXPhysic, they can still use the engine in a synchronous way.

In the foreseeable future, it can be considered that almost 90% of projects will use asynchronous, even WebGPU is forced asynchronous

@GuoLei1990 GuoLei1990 linked a pull request Mar 27, 2023 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request high priority High priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants