Fix/webgpu crash pixel density #8476
Open
+59
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #8456
Description
Fixes a crash when using the WebGPU renderer where calling pixelDensity() immediately after setAttributes() (or any other function that triggers an asynchronous context reset) would result in
TypeError: Cannot read properties of undefined (reading 'createTexture').The Issue
In p5.js 2.0, setAttributes() triggers an asynchronous reset of the rendering context via _resetContext(). Because core functions like pixelDensity() are synchronous, they can trigger a resize() (and subsequently _updateSize()) on a new renderer instance before its WebGPU device has been successfully initialized.
Changes
this.deviceis not yet defined. Since _initContext() calls these methods upon successful initialization, the state is eventually synchronized correctly once the device is ready.awaitthe new renderer'scontextReadypromise before replacing the existing renderer on the p5 instance. This ensures that the sketch's_rendererproperty always points to a valid, initialized renderer.Checklist