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: Add Offscreen Support #27520

Merged
merged 11 commits into from
Jan 8, 2024

Conversation

RenaudRohlinger
Copy link
Collaborator

@RenaudRohlinger RenaudRohlinger commented Jan 7, 2024

This PR fix the use of the WebGPURenderer in a worker context.

A small issue is that the WebGPURenderer is intricately tied to direct imports from 'three' making it complex to create an example without changing the configuration of the project.
Web browsers do not currently support the importmap feature in worker contexts. Alternatives would be to update the rollup configuration or use relatives paths. (Uncaught (in promise) TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".)

I tested on my local with another project on both backends and it works fine.

This contribution is funded by Utsubo

@RenaudRohlinger RenaudRohlinger added this to the r161 milestone Jan 7, 2024
@sunag
Copy link
Collaborator

sunag commented Jan 7, 2024

Web browsers do not currently support the importmap feature in worker contexts. Alternatives would be to update the rollup configuration or use relatives paths. (Uncaught (in promise) TypeError: Failed to resolve module specifier "three". Relative references must start with either "/", "./", or "../".)

Sorry, I didn't quite understand the relationship between the description and the commit.

@RenaudRohlinger
Copy link
Collaborator Author

I wanted to add an example for webgpu_offscreen but it's currently not possible with the WebGPURenderer being in examples/jsm.

The PR by itself simply adds Worker support. 😁

@sunag
Copy link
Collaborator

sunag commented Jan 7, 2024

Maybe you didn't choose other commits by mistake?

image

@RenaudRohlinger
Copy link
Collaborator Author

RenaudRohlinger commented Jan 7, 2024

Indeed that was my bad @sunag. Basically injecting a promise outside of the scope of the module breaks web workers. So I refactored:

// statics

let _staticAdapter = null;

if ( navigator.gpu !== undefined ) {

	_staticAdapter = await navigator.gpu.requestAdapter(); <-- this breaks web workers

}

to:

// statics

let isAvailable = navigator.gpu !== undefined;


if ( typeof window !== 'undefined' && isAvailable ) {

	isAvailable = await navigator.gpu.requestAdapter();

}

Using navigator.gpu in order to check if WebGPU is available in the worker is not a perfect solution but it's very decent and a lot better than nothing!

Alternative solution would have been to make isAvailable or/and the WebGPURenderer an async function which is too much of a breaking change IMO.

@RenaudRohlinger RenaudRohlinger requested a review from sunag January 7, 2024 07:55
@sunag
Copy link
Collaborator

sunag commented Jan 7, 2024

Thanks for the detailed explanation. :)

@sunag sunag merged commit d4e13e5 into mrdoob:dev Jan 8, 2024
11 checks passed
AdaRoseCannon pushed a commit to AdaRoseCannon/three.js that referenced this pull request Jan 15, 2024
* add offscreen support

* refactor statics webgpu

* navigator.gpu should be enough for initial check

* fix webgl context ktx2loader

* use isAvailable in webgpurenderer

* cleanup and should fix pupeeter

* return promise

* feedbacks

* remove unecessary async

* removed oversight

* cleanup

---------
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.

3 participants