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

WebGPURenderer: Fix global references in Node.js #29919

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from

Conversation

whatisor
Copy link

@whatisor whatisor commented Nov 19, 2024

Fix #29916.

Description
Fix compilation issue on react.

Copy link

github-actions bot commented Nov 19, 2024

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 339.13
79
339.13
79
+0 B
+0 B
WebGPU 478.72
132.72
478.92
132.77
+199 B
+50 B
WebGPU Nodes 478.18
132.6
478.38
132.65
+199 B
+51 B

🌳 Bundle size after tree-shaking

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

Before After Diff
WebGL 464.59
111.96
464.59
111.96
+0 B
+0 B
WebGPU 546.88
148.18
547.08
148.23
+199 B
+52 B
WebGPU Nodes 502.76
137.89
502.96
137.94
+199 B
+53 B

@sunag sunag changed the title Fix compilation in react WebGPURenderer: Fix compilation in react Nov 19, 2024
@sunag sunag added this to the r171 milestone Nov 19, 2024
@donmccurdy donmccurdy changed the title WebGPURenderer: Fix compilation in react WebGPURenderer: Fix global references in Node.js Nov 19, 2024
@donmccurdy
Copy link
Collaborator

Note – I've renamed the issue to "fix global references in Node.js". I think that's the issue here, not React. Next.js is a React-based framework that happens to execute code in both a browser environment and server-side in Node.js.

@@ -64,7 +64,7 @@ class WebGPUBackend extends Backend {
powerPreference: parameters.powerPreference
};

const adapter = await navigator.gpu.requestAdapter( adapterOptions );
const adapter = ( typeof navigator !== 'undefined' ) ? await navigator.gpu.requestAdapter( adapterOptions ) : {};
Copy link
Collaborator

@Mugen87 Mugen87 Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@donmccurdy{} is intended as a workaround for node but I'm not sure {} is ideal here. Do you think this use case is handled appropriately or would you implement it in a different way?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Base on that function code, it should be null, otherwise, next step is exceptional?
"adapter.features"

Copy link
Collaborator

@donmccurdy donmccurdy Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whatisor are you executing this code in the Node.js environment and seeing runtime errors, or is this a compile-time error (like from TypeScript or Webpack)? Not clear to me from #29916.

I'm not sure that backend.init( renderer ) should be making efforts to avoid runtime exceptions, if running in an environment where neither WebGL nor WebGPU exists. I would support fixes that avoid build-time errors, and run-time errors in module scope. But if you are initializing and using a renderer in Node.js, a runtime exception is to be expected.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that backend.init( renderer ) should be making efforts to avoid runtime exceptions, if running in an environment where neither WebGL nor WebGPU exists

I agree, I think we should be throwing an error here, it doesn't make much sense to me to proceed with an empty object as an adapter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@donmccurdy it is compile error.
We also need to fix three\examples\jsm\capabilities\WebGPU.js with global and await.
So, we should return null and (exception is next step as current)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

three\examples\jsm\capabilities\WebGPU.js

Capabilities should be optional, it should be possible to use WebGPURenderer without global await. #29218

Copy link
Author

@whatisor whatisor Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do same thing with webgl to avoid await?
https://developer.mozilla.org/en-US/docs/Web/API/GPUCanvasContext

=> No, we still need to await for real adapter. I guess browser need to improve this part.

Use `null` as fallback when navigator isn't available.
@@ -113,13 +113,21 @@ class WebGPUUtils {
// TODO: Remove this check when Quest 34.5 is out
// https://github.com/mrdoob/three.js/pull/29221/files#r1731833949

if ( navigator.userAgent.includes( 'Quest' ) ) {
if ( typeof navigator !== 'undefined' ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove this? It is inside a function that will never be executed by the adapter return null in this case.

Copy link
Collaborator

@donmccurdy donmccurdy Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the issue is that it's a compiler error, not a runtime error? I wish we knew what in the Next.js stack is doing compile-time checks on dependencies, and what its requirements are. Avoiding all references to browser globals is a pretty tall order for a large library relying heavily on Web APIs. :/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler throwing an error for a global variable inside a function seems strange to say the least, since a variable can be declared at the global level after the function declaration.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not sure why... trying to reproduce the issue:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@verekia
Copy link

verekia commented Nov 21, 2024

I have set up a test suite of various build environments, like Vite, with and without React, Next.js, R3F, and a specific one to check this PR: https://github.com/verekia/three-gpu-ecosystem-tests

The PR does indeed fix Next.js crashing on reference errors 🙌 That being said, I am getting a Multiple instances of Three.js being imported warning, which I don't get when importing WebGPURenderer dynamically. Maybe @CodyJasonBennett would have insights on why that happens.

Regular import (fixed by this PR):
next-pages-vanilla-pr-29919/pages/index.js

Dynamic import workaround:
next-pages-vanilla-dynamic-8ce69e0/pages/index.js

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.

Improve compatibility of three/webgpu with nextjs
5 participants