Repository navigation
Conversation
|
After a bunch of internal discussion with Chromium canvas owners about our sizing/resizing semantics, this is the design we arrived on. It makes our behavior significantly closer to existing canvas APIs (WebGL/2d canvas) and makes things a bit simpler (except for the fact that we now have to invalidate the current texture on canvas resize). |
3270082 to
460b7a5
Compare
kvark
left a comment
There was a problem hiding this comment.
What issue is this meant to solve? Where can we see the discussion on it?
|
I missed the discussions with Chrome's canvas owners on this topic, sorry. A few questions/concerns:
|
The discussion was about #2300 where I tried to define the natural and intrinsic sizes for WebGPU canvases since the underlying texture (the texture which was current and is now presented) could differ from the canvas size. The opinion of folks on our canvas team was that having the drawing buffer size diverge from the canvas size was undesirable - it's unlike other canvases, kind of confusing (which size is used in different places?), and not necessary (reallocation of the swap chain can be deferred until getCurrentTexture instead of being done eagerly when the configuration or canvas size changes).
It's now synchronous just like you would expect with any other canvas type:
Yes, we could do this. I partially followed WebGL's
This was done to make it match more closely with canvas width/height but this is a fair point. I'd be happy to change it back if we keep it. |
|
Agree that having 2 ways would be unfortunate. |
We don't need this internally in our API for people to do it. You can just set the CSS width/height - that overrides the default layout size of the canvas. Many sites do this anyway to account for the devicePixelRatio so the canvas pixels map to screen pixels instead of CSS pixels. |
|
We discussed this both with editors and with Mozilla (@kdashg in particular). TL;DR: we believe the current API serves well, and this PR shouldn't be merged. Big ArgumentMy understanding is the following now. Canvas in general prefers to be controlled by width and height because it's self-sufficient. I.e. users can set huge values, like 100k by 100k, and browsers are still expected to do something reasonable. They'd scale down the contents. The resulting image will be blurrier, but it's not a failure. This is possible with WebGL, but it's not possible with WebGPU. Reason being that for WebGPU a canvas is just providing a texture to render to. WebGPU user can copy to/from this texture or render to it in a pass that also uses other attachments. These other attachments have to have the same size, and copies have to be within bounds. Typically, there is a depth texture managed by the user, which needs to be resized. In other words, generally Canvas separates the notion of user-visible surface and what is actually rendered by the hardware. For WebGPU the only way reasonable way to separate them is to detach the surface configuration from the canvas size. This is exactly what we have today. Smaller ArgumentsHaving 2 very similar ways to do the same thing (canvas attributes vs If we allow width and height to control the surface size, we'd have a bit of a trouble specifying the validation for it. There needs to be a bit of validation logic to check that surface width and height are within the limits, and a particular place for this to happen. I see |
I don't exactly agree. Canvas size exists to set the drawing buffer size. In 2d canvas there are no bounds on the size. In WebGL the canvas size influences the drawing buffer size but the browser has prerogative to use a different size if the requested size is too big. This WebGPU proposal accounts for this: you can't getCurrentTexture if the canvas is too large. Since getCurrentTexture returns a new GPUTexture object, this logically directly follows from the size validation inside createTexture. This limitation doesn't create problems for users because the only reason you need to set the canvas size is to set its drawing buffer size. For everything else, there's CSS. |
Previously, there was *some* validation inside configure(), but it only covered a subset of the rules that were later covered by createTexture inside getCurrentTexture(). This moves all of the validation to getCurrentTexture() so it's not effectively duplicated. Resolves an issue where configure() attempted to do device-timeline validation but actually needed to do it on on the content timeline. Extracted from gpuweb#2416.
Previously, there was *some* validation inside configure(), but it only covered a subset of the rules that were later covered by createTexture inside getCurrentTexture(). This moves all of the validation to getCurrentTexture() so it's not effectively duplicated. Resolves an issue where configure() attempted to do device-timeline validation but actually needed to do it on on the content timeline. Extracted from gpuweb#2416.
Previously, there was *some* validation inside configure(), but it only covered a subset of the rules that were later covered by createTexture inside getCurrentTexture(). This moves all of the validation to getCurrentTexture() so it's not effectively duplicated. Resolves an issue where configure() attempted to do device-timeline validation but actually needed to do it on on the content timeline. Extracted from gpuweb#2416.
|
A point I brought up on the editors' call today which needed some expansion: DOM APIs including Canvas are, by design, very "lazy" - for example in 2d canvas/webgl, changes like width/height have to be queued up until later to make sure drawing buffers aren't allocated redundantly. But overall, this is the nature of the web for a lot of reasons. Decisions like whether to use an overlay/underlay/etc are very dynamic and can change due to other contents on the page, what part of the page the user is scrolled to, system state including other frames/tabs, etc. This isn't to say that the browser could never early-allocate swap chains (in All that said, I think the biggest objection to WebGPU's current sizing API was basically that canvas already has two sizing states ( |
|
#2536 extracts part of this PR if we decide we want to land it prior to figuring out the canvas width/height stuff. |
|
editors discussed and still would be happy if we completely ignored canvas width/height in our presentation model. (Would even remove the size-defaulting behavior from configure().) So really probably need a discussion with canvas spec people in the room, at least the ones at Google advocating that we shouldn't do that. |
|
Let's touch on this in the F2F. |
|
We should probably also talk about how we deal with canvases getting too big for the device limits (discussed in #2520). I'll see if we can get input from the Chrome canvas folks I talked to. |
|
FWIW, #2416 (comment) matches my understanding. And is also how it works for 2D canvas. The "drawing buffer" is determined by |
|
We talked with @annevk on matrix a bit, and they basically suggest us to remove |
|
One other consideration if we went with a Not terrible, just more shuffling. |
|
Sketched out the |
|
I spent some time investigating the WebGL issue KhronosGroup/WebGL#3360 (comment) WebGL has automatic downsizing, which results in Context:
Options mirroring the ones I wrote in that WebGL comment:
|
|
@kainino0x, @austinEng, and I talked with @junov this morning to get his perspective on the canvas integration. The overview is that Justin feels that there's a good opportunity here to "fix legacy canvas behavior", and as a result we shouldn't feel compelled to match older canvas interfaces simply for the sake of consistency. This is especially true in cases where the current canvas behavior has been observed to cause problems. For example:
(Worth noting that Justin did not express an opinion on using width/height as defaults for These are solid arguments as far as we are concerned, as a result Kai and I are both going to advocate for keeping the canvas configuration behavior as-is in the spec. We may want to add some text, normative or otherwise, drawing parallels with @annevk, given that you advocated for sticking closer to the behavior of other contexts in the past I'd like to get your thoughts on the arguments above? |
Here's the test case: https://codepen.io/kainino0x/pen/popgeKx
|
|
It would help a lot if these arguments came with some sample code to compare the "legacy way" that may or may not be able to address them and the "new way" that would fix it. To me it's not particularly legacy as it's the only way to do 2d and bitmaprenderer and I don't see those disappearing. I trust that the webgl and webgl2 integration were not satisfactory, but it's not clear from the above if that could have been addressed by making different decisions or if there was a more fundamental flaw. As for the above demo, could you not fix that by setting the |
|
(partial response)
Both However I do see that as a workaround, not something fundamentally makes sense. Going back to the comparison with the |
Not sample code, but: 2d/webgl sizing: If using ResizeObserver or devicePixelRatio to match canvas.width/.height with screen pixels, must be careful to override the CSS width/height to modes which cut the layout dependence on canvas.width/.height to break the feedback loop. (Yes, people figure out how to get it right, but why not try to improve?)
I think this is unfair to WebGL. I personally think 2d canvas fits into the platform just as poorly as WebGL, and I think that's fair because both are quite old and predate a lot of design direction that the Web has today. Bitmaprenderer is a small API and chose to match 2d closely since 2d canvas is how you did it before bitmaprenderer existed. And given that all three APIs are wildly different as soon as you get past the canvas setup, I don't see value in artificially trying to make canvas setup look (but not behave) extremely similar. |
|
Regarding WebGL targeting multiple canvases, as of the advent of ImageBitmapRenderers you are able to do this: // WebGL/Canvas2D path
const glCanvas = document.createElement('canvas');
const gl = glCanvas.getContext('webgl', { preserveDrawingBuffer: true });
const outputCanvasA = document.createElement('canvas');
const imgCtxA = outputCanvasA.getContext('bitmaprenderer');
const outputCanvasB = document.createElement('canvas');
const imgCtxB = outputCanvasB.getContext('bitmaprenderer');
async function onFrame() {
// Clear and Draw with gl
const imgA = await createImageBitmap(glCanvas);
// Clear and Draw with gl
const imgB = await createImageBitmap(glCanvas);
imgCtxA.transferFromImageBitmap(imgA);
imgCtxB.transferFromImageBitmap(imgB);
requestAnimationFrame(onFrame);
}Note the requirement for using In comparison, with WebGPU the pattern that we have established is the following: // WebGPU path
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const outputCanvasA = document.createElement('canvas');
const gpuCtxA = outputCanvasA.getContext('webgpu');
gpuCtxA.configure({ device, format: 'bgra8unorm' });
const outputCanvasB = document.createElement('canvas');
const gpuCtxB = outputCanvasB.getContext('webgpu');
gpuCtxB.configure({ device, format: 'bgra8unorm' });
async function onFrame() {
const textureA = gpuCtxA.getCurrentTexture();
// Clear and Draw with device to textureA
const textureB = gpuCtxB.getCurrentTexture();
// Clear and Draw with device to textureB
requestAnimationFrame(onFrame);
}Note that the render loop no longer requires |
|
Regarding reconfiguring contexts after creation: Imagine you are viewing a game that has a checkbox for "Enable/Disable Antialiasing". Because this is a context creation argument in WebGL, checking that box would require destroying the entire context and canvas, losing all the graphics resources you've loaded in the process. Obviously this may be a lengthy, disruptive process. const antialiasCheckbox = document.querySelector('antialiasEnabled');
let glCanvas = document.createElement('canvas');
let gl = glCanvas.getContext('webgl', { antialias: antialiasCheckbox.checked });
loadAllGameResources();
startGame();
antialiasCheckbox.addEventListener('click', () => {
pauseGame();
destroyAllGameResources();
glCanvas = document.createElement('canvas');
gl = glCanvas.getContext('webgl', { antialias: antialiasCheckbox.checked });
loadAllGameResources();
resumeGame();
});WebGPU addresses this in a slightly different way, where the decision about whether to have antialiasing or not doesn't happen at the canvas level, but there are still similar configuration options that may need to be toggled. By having a separate context configuration for the device we can do so less disruptively: const hdrCheckbox = document.querySelector('hdrEnabled');
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
let gpuCanvas = document.createElement('canvas');
let gpuCtx = glCanvas.getContext('webgpu');
gpuCtx.configure({
device,
format: hdrCheckbox .checked ? 'rgba16float' : 'bgra8unorm'
});
loadAllGameResources();
startGame();
hdrCheckbox.addEventListener('click', () => {
gpuCtx.configure({
device,
format: hdrCheckbox .checked ? 'rgba16float' : 'bgra8unorm'
});
}); |
But that is how (Note that for |
|
I don't think I have a big problem with Is there a reason you need to pass device to |
|
I think it would be reasonable to allow developers to pass a Also, given that we have |
|
|
As to the rest of the configuration options - I think they're a lot less fundamental and intricate than the sizing issue, and I have no real opinions on what shape the API takes, just would like the configuration to be mutable. It seems best to come to a conclusion on the sizing question, then make everything else fit with it. |
|
Friendly ping, @annevk. We were discussing this in the WebGPU editor's sync today and wanted to know if you had any thoughts on Kai's response? |
|
I don't really see how In the past |
|
@annevk thanks as always for your input. Apologies for the long reply.
Sorry, I don't quite understand. An
I'm sure I'm missing something, but that seems like just one possible interpretation. The canvas element width/height is one piece of state that's used as input to both the intrinsic size / drawing buffer size and the final layout size. They do influence the intrinsic size, unlike in (As an aside though, I did realize that unfortunately
I definitely get the desire not to go back on that, but I believe 2d canvas is actually the only context where it's strictly true that the canvas element's width/height are equal to the intrinsic dimensions:
|
|
@kainino0x I will grant you that they are separate state, but in terms of influencing the final processing model the As for As for "bitmaprenderer", it doesn't seem to be defined how that actually works. If you're correct that might well invalidate some statements I've made, but it doesn't seem to be defined in sufficient detail at the moment how that actually works. |
I definitely don't have a comprehensive understanding of the spec, but it seems defined to me:
except I'm not 100% sure that the "canvas's bitmap" = the "context's output bitmap". (EDIT: accidentally sent before I finished writing) |
|
(edited previous message to complete the thought)
State is really what I was talking about here, but it's fair to consider the ways the state is used. We're far into territory I'm poorly-versed in, so it's likely I have misstated something. So to try to clarify: what I want (and believe is already true) is for
The spec does say "The canvas element has two attributes to control the size of the element's bitmap: width and height." I'm not sure if this is the sentence you're talking about, but my interpretation of this sentence is that the attributes influence the size of the element's bitmap (in a context-specific way), not that they equal the size of the element's bitmap. The wording is unclear, and it does seem very likely that the original author intended the "equal" interpretation. But there is a lot of language around the In any case, the "equal" interpretation still seems to conflict with "bitmaprenderer" and WebGL, while the "influence" interpretation is compatible with WebGL and not-strictly-conflicting with "bitmaprenderer". |
|
When I play with "bitmaprenderer" I get different results from what the specification describes: https://software.hixie.ch/utilities/js/live-dom-viewer/saved/10227. According to the specification the intrinsic dimensions of the canvas would be that of the image and those would also be used for rendering (according to the Rendering section). However, the I filed whatwg/html#7833 to further discuss that. As I mentioned earlier I don't think state is super meaningful here as there's a lot of redundant state. E.g., the fact that the values of the One way to get where you want to be would be to say that the |
|
Given Anne's strong stance on this - and ongoing discussions about whether bitmaprenderer should even work this way, I'm proposing to remove size entirely from the configure() call and rely directly on canvas.width/height. |
Notably, this means implementations may want to (at least sometimes) defer allocating underlying textures (or swap chains) until the first getCurrentTexture() call, e.g. after the canvas width changes to avoid reallocating before the canvas height is set.
Related: #2300
Preview | Diff