-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
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
WebGPUTextures: Add support for mipmap computation. #20284
Conversation
FWIW I've got a newer version of the same code here: https://github.com/toji/web-texture-tool/blob/master/src/webgpu-mipmap-generator.js There's been several fixes applied to it since the original comments you linked, like handling texture formats other than |
Yeah that general approach is the recommended way. In the future there might be more efficient libraries that do it with compute but that's not available yet. Also |
|
||
if ( needsMipmaps === true ) { | ||
|
||
usage |= GPUTextureUsage.OUTPUT_ATTACHMENT; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's unfortunate because OUTPUT_ATTACHMENT can make the texture consume more memory. I think @toji's utility does a copy when the texture isn't OUTPUT_ATTACHMENT so that it doesn't pessimize things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My older code always did the copy, my newer code always does the render between texture layers. The ideal scenario would be to have both paths and choose based on whether OUTPUT_ATTACHMENT was used at creation. It's on my to-do list, at least.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code is definitely a good start for WebGPUTextureUtils
😊 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This thread gave me the prodding I needed to make the change anyway. :) https://github.com/toji/web-texture-tool/blob/master/src/webgpu-mipmap-generator.js will now handle mipmapping for texture creation for textures created without OUTPUT_ATTACHMENT usage. It's still not clear to me which path is preferrable, and I suspect it depends on the hardware?
The OUTPUT_ATTACHMENT path is going to be faster to generate mipmaps for, but may be slower to access afterward. The copy path will be slower during mipmap gen and use more memory but may allow for faster sampling later? Maybe @Kangz has some better insight into the pros/cons?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OUTPUT_ATTACHMENT
allows in-place mipmap generation and avoids the allocation and release of temporary texture memory, but using it when not necessary will consume more memory (due to additional compression planes and stricter alignment requirements) and could prevent some faster HW paths in some cases.
TIL |
Let's try this code in |
WebGPU does not support automatic computation of mipmaps like WebGL does (see gpuweb/gpuweb#386). So there is no pendant to
WebGLRenderingContext.generateMipmap()
.TBH, this is a bit unfortunate since probably all 3D engines require this feature and need to implement it with custom code. Most
three.js
users use JPG and PNG as their texture format so asking them to use texture formats with precomputed/embedded mipmaps is inappropriate.For now I have copied @toji's code from gpuweb/gpuweb#386 (comment) into a new class called
WebGPUTextureUtils
and applied some minor refactoring to it.@Kangz Do you think the current approach in
WebGPUTextureUtils
is still valid/recommended? The code from the gist is half a year old and there might be probably a better way of computing the mipmaps now.