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

WebGPUTextures: Add support for mipmap computation. #20284

Merged
merged 2 commits into from
Sep 8, 2020
Merged

Conversation

Mugen87
Copy link
Collaborator

@Mugen87 Mugen87 commented Sep 7, 2020

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.

@toji
Copy link
Contributor

toji commented Sep 7, 2020

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

@Kangz
Copy link

Kangz commented Sep 7, 2020

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 glGenerateMipmap is problematic because it doesn't do srgb-correct or normal-map correct interpolation.


if ( needsMipmaps === true ) {

usage |= GPUTextureUsage.OUTPUT_ATTACHMENT;
Copy link

@Kangz Kangz Sep 7, 2020

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Contributor

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.

Copy link
Collaborator Author

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

Copy link
Contributor

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?

Copy link

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.

@mrdoob
Copy link
Owner

mrdoob commented Sep 7, 2020

Also glGenerateMipmap is problematic because it doesn't do srgb-correct

TIL

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Sep 8, 2020

Let's try this code in dev.

@Mugen87 Mugen87 merged commit ee3fe43 into mrdoob:dev Sep 8, 2020
@Mugen87 Mugen87 added this to the r121 milestone Sep 8, 2020
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.

4 participants