webgpu-native / webgpu-headers

https://webgpu-native.github.io/webgpu-headers/
BSD 3-Clause "New" or "Revised" License
336 stars 37 forks source link

Swapchain / Surface questions #24

Open Kangz opened 4 years ago

Kangz commented 4 years ago

I've started to implement swapchains in Dawn and ran into a bunch of questions.

Q: How do we report surface creation errors? Tentative-A: We return a nullptr surface? But it doesn't allow reporting a string message to explain what went wrong. Alternatively surface creation can never go wrong and errors are always exposed when the swapchain is created on the surface.

Q: Can we have multiple swapchains on the same surface? A: No, there is always a single current swapchain. Creating a new swapchain on the surface invalidates the previous one (this includes destroying the "current texture").

Q: Can we have subsequent swapchains on a surface be on different backends? Tentative-A: No, it is a validation error. (this is to not deal with backend-compatibility stuff)

Q: Do we have the equivalent of GPUCanvasContext.getPrefferredSwapChainFormat? A: Maaaybe? But then is it synchronous or async? Or we could just have a list of formats that we guarantee always work (and do blits)?

Q: How do we handle window resizes and minimization? A: No clue, Vulkan has the "outdated" swapchain concept, but there's no clear way to do this in webgpu.h that would also match the Web where the application sets the size of the canvas directly. Also we should take care on Windows, where Vulkan for example requires the size of the swapchain to match exactly the size of the window.

Q: Do we do things for the user like resize blits, format blits etc?

kainino0x commented 11 months ago

@rajveermalviya there are a few things left in this issue that I think are not answered by #203, you might be interested in trying to resolve them

kainino0x commented 7 months ago

A note from the meeting:

rajveermalviya commented 7 months ago

Most of these have already been answered and implmented but coming back to it today, there may be different answers for some.

How do we report surface creation errors?

Without changes in the header, we can't do more than returning null and log the error, or defer reporting error till surface configure.

But if we do allow changes then:

Can we have multiple swapchains on the same surface?

Current webgpu.h doesn't expose swapchains anymore.

Do we have the equivalent of GPUCanvasContext.getPrefferredSwapChainFormat?

It's already in webgpu.h as wgpuSurfaceGetPreferredFormat. In wgpu it prefers srgb supported formats over linear.

How do we handle window resizes and minimization?

webgpu.h currently contains WGPUSurfaceGetCurrentTextureStatus which has Timeout, Outdated and Lost - but AFAIK only mappable to vulkan. For resizes some rules that [could be]/[are already] enforced:

Do we do things for the user like resize blits, format blits etc?

Need more context here but in general, IMO prefer avoiding implicit behavior, but makes sense from webgpu POV.

kainino0x commented 7 months ago

Nov 16 meeting:

kainino0x commented 7 months ago

How do we report surface creation errors?

Without changes in the header, we can't do more than returning null and log the error, or defer reporting error till surface configure.

I think this is sufficient since it should be possible to write a program that never hits an error condition. Maybe there can be dynamic errors but nullptr is probably enough for an application to know it needs to retry.

Do we have the equivalent of GPUCanvasContext.getPrefferredSwapChainFormat?

It's already in webgpu.h as wgpuSurfaceGetPreferredFormat. In wgpu it prefers srgb supported formats over linear.

One note about this - in the JS API, to avoid breaking content, getPreferredCanvasFormat will only ever choose from rgba8unorm and bgra8unorm. There will likely be some way you can opt into getting other formats, for example navigator.gpu.getPreferredCanvasFormat({ hdr: true }) that could return rgba16float or rgb10a2unorm, or { srgb: true } which could return rgba8unorm-srgb or bgra8unorm-srgb. Or maybe it looks more like getPreferredCanvasFormat(['rgba8unorm-srgb', 'bgra8unorm-srgb']) or getPreferredCanvasFormat(GPUPreferredFormat.SRGB | GPUPreferredFormat.HDR).

Regardless, we are likely to need some configurability here in the future that we don't have right now. If wgpu already wants to return srgb formats here (which differs from the JS API) then maybe we should make it configurable now so the default behavior can match.

Kangz commented 1 month ago

Additional question from #295

Should a WGPUTextureUsage be added to WGPUSurfaceCapabilities with guarantees like RENDER_ATTACHMENT|TEXTURE_BINDING|COPY_SRC|COPY_DST?

Is the following defined to always work without additional waits? At least on Vulkan and D3D12 it will require waiting for the GPU on WGPUSurfaceRelease:

surface = wgpuInstanceCreateSurface(sameParams);
wgpuSurfaceConfigure(surface, ...);
wgpuSurfaceRelease(surface);

surface2 = wgpuInstanceCreateSurface(sameParams);
kainino0x commented 1 month ago

Should a WGPUTextureUsage be added to WGPUSurfaceCapabilities with guarantees like RENDER_ATTACHMENT|TEXTURE_BINDING|COPY_SRC|COPY_DST?

Could the supported usages ever change according to other settings, like which format/alphaMode/presentMode is used? Or should there be no additional restrictions over what the device can do (e.g. whether it has bgra8unorm-storage)?

Kangz commented 1 month ago

I can only see the format causing restrictions. My assumption is that we'd validate the combination for format + usage the same way we do it for regular textures. Note that neither D3D12 nor Metal have a query for what a swapchain can do, and that Vulkan has the query return the allowed usages independently from the formats.

kainino0x commented 3 weeks ago

May 23 meeting (sorry for delay):

kainino0x commented 3 weeks ago

Jun 6 meeting: