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

Renderer: Return self in init(). #30556

Merged
merged 1 commit into from
Feb 19, 2025
Merged

Conversation

CodyJasonBennett
Copy link
Contributor

Description

Resolves the renderer prototype instead of returning void for init().

This allows const renderer = await new WebGPURenderer().init() which is the cause of bugs otherwise.

Copy link

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 336.51
78.38
336.51
78.38
+0 B
+0 B
WebGPU 519.2
144.16
519.21
144.16
+4 B
+1 B
WebGPU Nodes 518.67
144.05
518.67
144.05
+4 B
+0 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 465.51
112.23
465.51
112.23
+0 B
+0 B
WebGPU 591.03
160.12
591.04
160.12
+4 B
+1 B
WebGPU Nodes 546.34
149.59
546.35
149.59
+4 B
+0 B

@Mugen87 Mugen87 added this to the r174 milestone Feb 18, 2025
@sunag sunag merged commit 91964e8 into mrdoob:dev Feb 19, 2025
12 checks passed
@WORMSS
Copy link
Contributor

WORMSS commented Feb 27, 2025

Hmm, just wondering the reason why init() is async in the first place? Legacy maybe?
I can see it returns a promise, but no actual awaiting happens inside init();

I could understand if it was

await this._initPromise;
return this;

but with it being (below) I fail to understand the reasoning behind it. Any insight would be grateful.

return this._initPromise;

@CodyJasonBennett CodyJasonBennett deleted the Renderer-init-type branch February 28, 2025 16:17
@CodyJasonBennett
Copy link
Contributor Author

Awaiting does happen. WebGPU API is asynchronous to request a device and adapter. The code you shared does the same except when an exception is encountered. In both cases, init() is deduplicated and can be awaited at all calling sites.

this._initPromise = new Promise( async ( resolve, reject ) => {
let backend = this.backend;
try {
await backend.init( this );
} catch ( error ) {
if ( this._getFallback !== null ) {
// try the fallback
try {
this.backend = backend = this._getFallback( error );
await backend.init( this );

@WORMSS
Copy link
Contributor

WORMSS commented Feb 28, 2025

@CodyJasonBennett that awaits is within the promise resolver callback.
Different scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants