Repository navigation
Description
In WebGL the browser handled creating the drawingbuffer for a canvas. The spec says
If the requested width or height cannot be satisfied, either when the drawing buffer is first created or when the width and height attributes of the HTMLCanvasElement or OffscreenCanvas are changed, a drawing buffer with smaller dimensions shall be created. The dimensions actually used are implementation dependent and there is no guarantee that a buffer with the same aspect ratio will be created. The actual drawing buffer size can be obtained from the drawingBufferWidth and drawingBufferHeight attributes.
In WebGPU how would this be implemented?
In WebGL this all happens synchronously so
canvas.width = devicePixelRatio * canvas.clientWidth;
canvas.height = devicePixelRatio * canvas.clientHeight;
// see what size I got
console.log(gl.drawingBufferWidth, gl.drawingBufferHeight);Part of the reason for this is that many apps do the thing above (resize to match the CSS or window size) and a user might have multiple monitors and sizing a window across monitors could easily run out of memory. Ideally you'd like your app not to crash at that point.
Generally in WebGL I have code like that above called just before rendering though I usually check if the size changed before setting canvas.width and canvas.height.
Given I can't check allocation success or failure in WebGPU synchronously then it seems I have a few options
-
Resize asynchronously and only switch buffers when on success, continuous rendering
The problem here is i'll be using the wrong size for the canvas for a few frames and I'll need to keep the old canvas drawingbuffer around so I can continue to render until the new canvas drawingbuffer is ready. That means I need double the memory or more when sizing up. Worse, between the time I allocate the new size and the time I get the error message back the user may have sized the canvas to some other size so I have to throttle the new creation
-
Stop rendering on resize until allocation succeeds
This is, when you notice the canvas has changed size, stop rendering, delete the old textures, create the new ones
wait for success, then start rendering again. This way you don't use double the memory. Curious what the resize experience would be with this method. (update: it's not good ATM) -
Resize asynchronously, switch immediately, barf if out of memory
This is what all current samples I've seen do. They don't check for errors so if the size is too large or if they are out of memory they're going to start generating rendering errors as the GPUTextures they're using are invalid. This is kind of gross. It also means if you're checking for validation errors at all you'll likely get errors you weren't expecting.
-
Resize asynchronously, switch immediately, check for errors
The problem with this solution is you'd get several frames of missing/bad rendering until you tried a smaller size and recovered. Each smaller size requires another round of waiting for the results. And, like the previous example, you'll start getting validation errors. (suppose you could turn off any error checking while resizing)
This is even more perplexing given that if I call someGPUCanvasContext.configure and it fails with out-of-memory, now my canvas is unusable until I recover with a smaller size
Note: all the same problems exist if you're creating textures based on the size of the canvas for things like post processing etc....
What's best or recommended practice here?
Activity
[-]Handling out-of-memory when creating a canvas backing store[/-][+]Handling out-of-memory when resizing canvas backing store[/+]greggman commentedon Jan 29, 2022
I just wanted to add a few more random thoughts
In WebGL, because the browser handled the size of the canvas's drawingbuffer for you it wasn't common to check for the max supported size. Maybe in WebGPU it should be common/best practice to do that (seems like it).
There are 2 problems being solved here. (1) what to do when you ask for a canvas larger than
limits.maxTextureDimension2D(2) what to do when out of memory. (1) is arguably solved by telling devs "hey, this is your responsibility". Maybe that's the common case and running out of memory should crash like JavaScript does. Crash is a harsh word, just passing on that if (1) is common and (2) is uncommon then (1) is basically solved.In support of running out of memory might not be uncommon, if I zoom out (Cmd/Ctrl+Minus) to 25% (chrome's limit) and I have code like
It comes up with 24k (the screen is ~3600. Zoomed out to 25% makes that x4, a dpr = 2 so 24k). So not a small amount of memory. Add in MSAA, and/or post processing passes. Then open a few windows and it's easy to see running out of memory. Add in hi-color to double it again. I don't think people zoom out to 25% often but it'd be nice of the page didn't crash and I have done it by accident before by holding some key combo and scrolling the mouse.
At the same time, one solution here to tell developers they shouldn't blindly do
canvas.clientWidth * devicePixelRatio. Instead they should never ask for more than the actual device pixels via ResizeObserverdevicePixelContentBoxSizebecause arguably there's usually not much point in allocating higher res. FromdevixePixelContentBoxSizeI believe you can compute the zoom level which means you can zoom what you're rendering in math to make it appear the be zooming/out (same as SVG does?)Samples showing how to do this would be great and I'm happy to write one. Unfortunately, neither Firefox nor Safari support
devicePixelContentBoxSizeand further, Safari does not changedevicePixelRatioin response to zoom (would love to know if there is a principle for that or if it's just TBD). In Firefox you can guess thedevicePixelContentBoxSizethough that guess will often be off by a pixel and you'll get moire patterns. In Safari it's not possible IIRC at the moment. I'm stuck allocating the 24k (example above) even though there's only 3.8k pixel across my display since there's no way to figure out the page is zoomed in Safari.Neither of those fully solve the out-of-memory issue but maybe they'd make it less common
greggman commentedon Jan 29, 2022
as one more data point. If I run go to https://austin-eng.com/webgpu-samples/samples/resizeCanvas, make the window fill the screen, and zoom out, the page will crash with these errors (in Chrome)
I only bring it up because it's different than I expected. I expected that once the page starting asking for sizes larger than
limits.maxTextureDimension2Dit would just stop rendering because the textures are invalid, probably spewing errors in the JavaScript console for a while, and that if I then sized the window down it would recover. Instead it gotGPU connection lost. That's probably just a bug in the webgpu implementation.kainino0x commentedon Jan 29, 2022
for the record, editors discussed this a little bit today but didn't have a chance to get into details. Out of memory is a horribly complex problem and I don't have any really good thoughts on solving this yet. (Haven't read your latest comments yet.)
kainino0x commentedon Feb 8, 2022
I was very confused about this until I read the comment about Safari.
devicePixelRatioabsolutely should respond to page zoom, and does in Firefox and Chrome resulting in roughly consistent device pixel size. I definitely think this is a Safari bug, and IMO a major one as it can result in significant memory problems (and under-resolution on zoom in). (Test: https://codepen.io/kainino0x/pen/PoOWEob)Agreed.
greggman commentedon Feb 9, 2022
FYI: Here's A webkit bug filed on devicePixelRatio not responding to zoom from over 8 years ago
https://bugs.webkit.org/show_bug.cgi?id=124862
@litherum
kainino0x commentedon Feb 9, 2022
Spitballing ideas I have about this:
Make the width and height only a "request" like in WebGL, add context.drawingBufferWidth/Height which you have to use when creating your additional render targets.
This does create a footgun though, where people don't know they need to use drawingBufferWidth/Height and their app breaks on resize/zoom. To alleviate that in common cases, maybe we should allow requesting extra resources alongside the single-sampled color texture, instead of requiring users to allocate them?
We would need to be able to provide a depth-stencil texture, and possibly a multisampled color texture. It's rare to need more attachments than that on the final render pass, but unfortunately multi-pass rendering then still runs into the same problem. So I don't really love this solution. Maybe it's okay to have the footgun?
kainino0x commentedon Feb 9, 2022
^ We could do automatic downsizing not just for OOM, but also for max texture size (8192x8192 base limit)
kainino0x commentedon Mar 28, 2022
Editors discussed and we're pretty sure we don't need to do anything here yet. In particular, in practice I don't think WebGL actually automatically downsizes canvases based on real OOM conditions, only on fixed limits.
I think for now we should return invalid GPUTextures when this happens, as is the default behavior in the current spec since getCurrentTexture() calls createTexture() which can OOM.
kainino0x commentedon Mar 28, 2022
Tentatively post-v1
greggman commentedon Mar 29, 2022
This isn't just about OOM, it's also about the limits. I don't know if WebGPU will have the same limits as WebGL but at least in Chrome, there's a limit for WebGL which I assume has a point and that the same reason for applying that limit in WebGL applies to WebGPU?
I'm not sure I understand what an app is supposed to do in WebGPU. The fixed limits work in WebGL because it succeeds (it allocates a drawingbuffer even if smaller than requested). You ask for 8k by 8k and you get back 5k by 5k (success).
In WebGPU you ask for 8k by 8k and it fails and ... your app crashes? You try again with a smaller size? What do you show the user in the meantime since it's async before you can check for success or failure and more async to try again.
Could you please consider writing some "best practice" code for how an app that wants to handle this robustly and provide a good UX is supposed to do it?
Kangz commentedon Mar 30, 2022
I might be missing something, isn't the application in control of the size of the canvas such that they know it should be less than
maxTextureDimension2D(which happens to be 8192)? If they want to draw to a larger canvas, they can easier make it scale from a smaller size, or request a device with larger limits formaxTextureDimension2D? I'm not sure what's the current ideas around GPUCanvasContext sizing, but if we let the application control how many pixels to allocate to the backbuffer, then they know to stay under the limits no?greggman commentedon Mar 30, 2022
No
The value
maxTextureDimensions2Ddoes not tell you you can make a 8192x8192 texture. It tells you, for example, you can probably make 8192x1 or 1x8192.For example rgba32float * 8k * 8k is 1gig of ram. If sample = 4 it would be 4gig of ram. As another example, on WebGL, iOS reports a MAX_TEXTURE_SIZE of 16384 but I certainly can't make an 16k by 16k rgba32float texture as there isn't enough memory.
That's why it would be great if you or some other WebGPU expert could write an example of how to use WebGPU to handle this case in a robust way. I tried. I couldn't do it. Maybe I don't understand the API. I think if you actually try to do it yourself you'll either find the solution (yay!) or you'll understand the problem (also yay!)
Requirements
https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/graphics/gpu/drawing_buffer.cc;l=1415?q=ResizeFramebufferInternal
Kangz commentedon Apr 4, 2022
@kainino0x IIRC we discussed this internally and people in the conversation agreed that it was hard to do anything explicitly in the browser but we should provide a clear OOM signal so that applications can handle it. Do you remember the details?
kainino0x commentedon Apr 6, 2022
I don't remember anymore, I've thought about this topic at too many different times to recall a specific one :P
Given I was wrong about WebGL not doing automatic downsizing, it makes sense to me to just expose OOM from getCurrentTexture. I would really, really like to do it in configure(), but I don't think there's any way we can guarantee resources are allocated at that time.
kainino0x commentedon May 11, 2022
Editors still thinking it's OK for V1 to expose (real) OOMs via out-of-memory in getCurrentTexture just like out-of-memory in createTexture. This error can only be detected asynchronously.
When the size exceeds the device limit you can detect that synchronously (just check if the size is over 8192 or whatever).
We weren't concerned for now about allowing implementations to impose additional limits (like megapixel limits).
kainino0x commentedon May 18, 2022
With #2821 the spec now explicitly mentions that
out-of-memoryerrors can be generated ingetCurrentTexture().Should we close this or is there something specific we still want to investigate (maybe post-v1)?
Kangz commentedon Jun 21, 2022
I think this solve it for now and we can close. If developers run into a lot of issues then we can make some additional improvements potentially, though I'm not sure what.