Repository navigation
Replies: 6 comments 13 replies
|
...sigh..., the compute solution is slower than the render-pass version on NVidia (D3D12). I have no idea, other than benchmarking, or device.info, to know how to decide which path to use for a given device. I wonder if there is a single solution that's fast for all devices. |
|
I feel bad I missed @JolifantoBambla's link to their single pass downsampler implementation. |
|
Thanks for looking in to this. The compute shader approach seems promising, but one question I have is if it is processing blocks of 16x16, what happens with an e.g. 24x24 texture? The top-right block would only have 8x16 of content. For out-of-bounds pixels, will it obtain a default color (e.g. transparent black) which bleeds in to the mipmap? Or will it do something like "clamp" texture mode? Ideally it would ignore the out-of-bounds pixels and ensure they don't contribute to the mipmap. |
|
SPD can't do NPoT texture filtering or adjust for mipmap alpha coverage. We can, please check out this presentation: |
|
Do we have any performance numbers for NVIDIA? Here is a Vulkan implementation that may be a useful benchmark, in case you weren't aware of it. |
|
I’m running into a real dilemma with video textures in WebGPU: If I use importExternalTexture with VideoFrame, I get the benefits of zero‑copy and good performance, but it’s impossible to generate mipmaps, since the texture is read‑only and has no mip levels. if there are recommended patterns (or future directions) for handling video + minification without paying such a large GPU cost. |





Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Mipmap generation in WebGPU is manual. Starting this thread as a way to discuss methods, techniques, and or links to code.
A common method is to use a render passes. You render a quad or full size triangle to each mip level, using a source the previous mip level
This works but it's slow.
AMD has a faster method here. Unfortunately to some degree, it's AMD specific, though I'm sure it can be adapted to other devices.
In anycase, as a first pass, I threw together a compute shader based solution. It generates 4 mip levels per dispatch. It's hard coded to sample a 16x16 block of a texture into an 8x8 block of workgroup memory, at the same time, it writes that 8x8 block to the next mip level.
After that it does it's own bilinear filtering to take that 8x8 block to 4x4, again writing it to both the next-next mip level, and to workgroup memory. Repeat until you get to 1x1.
Note: This means it will get different results than the render pass based. In particular, if you down sample a 7x1 texture you get a 3x1 where the last texel of the 7x1 texture is completely ignored in the generated the 3x1 because each texel is exactly the bilinear filter of the 2x2 texels directly above it. Similarly the 3x1 into the 1x1 only looks at the first 2 texel.
I don't know if this matters in practice. AFAIK, all native APIs that generate mipmaps basically say "the algorithm used is undefined"
In anycase, it appears 3x to 5x faster than the render pass method and at least comparable to WebGL on Mac (haven't tested much elsewhere yet)
#of 4096x4096 textures that can have mipmaps generated at 60fps
code
I will consider adding an article on webgpufundamentals though I'd like to know if there is more low-hanging fruit for more gains before I write it. I can also add it to webgpu-utils where as long as it works it doesn't matter what it does.
Limits
The technique requires
STORAGE_BINDINGusage.Lots of formats do not support this by default.
The ones that do and are relevant to generating mips are:
rgba8unormrgba8snormrgba16floatThe following formats don't support filtering by default though that could be emulated easy enough. This limit is the same in WebGL.
r32floatrg32floatrgba32floattexture-formats-tier1would add inrgb10a2unormrg11b10ufloatrg16floatrg16snormrg16unormr16floatr16unormr16snormrg8unormrg8snormr8unormr8snormAnd there's
bgra8unormwhich requiresbgra8unorm-storageI'm not sure it's all that important to support all of these formats (not that it's hard to add support). AFAIK, 99.9999% of all WebGL apps import
rgba8unormand I'd expect that to stay somewhat the same, maybe withrgba16floatbecoming more common for HDR processing.The technique won't work with cube maps and 2d-arrays in compatibility mode.
You can fallback to the render pass method in compat mode for these.
Ideas for more perf:
expand the workgroup size
At the moment it's hard coded to 8x8 for the first generated mip level (workgroup size 64)
I could make it adjust to the device and do 16x8 or 32x8. I don't know how much perf
that would add (or subtract)
increase the number of mip levels per
dispatchWorkgroupscallIt currently generates 4 levels at a time. This is portable because it reads from one
TEXTURE_BINDINGand writes to 4STORAGE_BINDINGSwhich is the default limit formaxStorageTexturesPerShaderStage. It could adjust tomaxStorageTexturesPerShaderStageand try to do more mip levels at once.
generate more shader variations
The current shader ALWAYS does 16x16->8x8->4x4->2x2->1x1. It gets away with this
because
textureStoreis a no-op if coords are out-of-bounds. Also, it bindsa dummy 1x1 texture for any mip level not used. In other words, if you had a 2x2
texture it would bind mip level 1 to generate the 1x1 and then bind a dummy texture
for levels 2, 3, 4.
It doesn't seem like this optimization would add much. My guess is the overhead
is in actually dispatching the last workgroup and there wouldn't be a measurable
difference.
bound the work
As mentioned above the current shader always does 16x16 and counts on out-of-bounds
behavior to um, bound it. Maybe manual bounding would be better, skipping steps in
code if they are known not to be needed.
use a different access pattern
Making sure each invocation accesses data it's already cached should be a win.
The AMD talk mentioned this.
use subgroups
Subgroups are an optional feature so a solution could not depend on them but the AMD talk
mentioned some possible optimizations there.
My goal was to match WebGL's speed and this first pass seems to have done that or gotten close enough so I'm not sure how much time I'm personally interested in trying these other ideas but maybe someone else would like to try for internet points and fame! 🪙🔥
(*)
on My M1 Mac, when I raised the the number from 110 to 120 the Chrome GPU process locked up and crashed. I'm not sure why. This usually happens when too much work is submitted to the GPU per frame though I'm not sure why 110 runs at 60fps and 120 kills it.GL has the issue that many things can be delayed until later where as WebGPU is more explicit. As an example, in GL, if you call
glGenerateMipmaptwice in a row, GL is free to ignore the 2nd call because it knows the texture has not changed. Further, it's free to do nothing and just record that mipmaps eventually need to be generated.The speed test uploads a single pixel to each 4096x4096 texture, to force the texture to be marked as "dirty" if the GL is trying to skip generating mip levels. It then calls
gl.generateMipmapbut the texture is never used for rendering. This was because I didn't want rendering to affect the timing. Further, there is no output in the test (updating a canvas) because that adds in compositing issues which would further affect the timing.In anycase, it crashed at count = 120 so I'm not sure the test was valid.
All reactions