Bugfix for WebGPU: Call Texture.onUpdate() after mipmap generation instead of before #31752
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.
There is an inconsistency between the WebGL and WebGPU implementations regarding the
Texture.onUpdate()callback.A common(?) use case for this callback is to dispose of the image from RAM once it has been uploaded to the GPU. Currently, in WebGL, one can do:
where
myDisposeFunctiondoes something likedelete texture.source.data;or equivalent, thus allowing the image resource to be garbage collected.With this, the texture can then be added to the scene at leisure, and one can be confident that whenever it get uploaded to the GPU, the dispose function will be called. Since the texture is already on the GPU, one might no longer need it in RAM, and this memory management is necessary for some projects. In WebGL mode, this code currently works fine.
However, in WebGPU mode, this same code causes an error, because Three.js will try to create mipmaps AFTER
texture.onUpdate()has been called. The mipmaps will try to access the deleted image source, throwing an error. The mipmaps might also actually be needed for the texture, so they should probably be generated first.Indeed, when comparing
WebGLTextures.js:uploadTexture()withTextures.js:updateTexture(), we can see that in WebGL, mipmap generation happens before theonUpdate()callback is called. In WebGPU, it's done after.In this pull request, I try to fix this issue, with the motivation of making it conform with how it works in WebGL. This fix definitely solves the issue at least for my project. I welcome feedback or discussion from those more familiar with this part of the code, as I am a new contributor.