Repository navigation
Description
Background
On Nov. 1, there was consensus in the group about scheduling resource uploads & downloads at a particular point in the device's queue so that the CPU and GPU wouldn't be accessing the same resource at the same time. This is the best (only?) solution for resources which live across multiple frames.
Let's consider the other side of the issue: when information on the CPU is only necessary on the GPU for a single frame. One example of this is the Model View Projection matrices, which are consistent throughout a single frame, but will change from frame to frame.
There are a few possible models for these kinds of resources:
- Resource Churn The application allocates and destroys a new resource each frame. Obviously (I think we can all agree) we don't want this.
- Scheduled Uploads The application uses a single resource, and schedules an upload at the start of each frame.
- Explicit Recycling Allocate
nresources up front, and recycle them each frame. We can piggyback off the design of the swapchain here, because the swapchain includesnbuffers, and it guarantees that, when you're recording commands to draw into a particular swapchain buffer, that buffer is not being accessed by the GPU. If we make an array of our own resources which parallels thenbuffers in the swapchain, the parallel item in our own resource array is unused by the GPU at recording time, and therefore is free for the CPU to populate. - Implicit recycling Just like above, a collection of resources will be created, but this array is owned by the implementation. All resources exist inside the implementation in a "free pool" or an "in-use pool." When an application asks for a resource, one is pulled from the free pool, or if the pool doesn't contain anything compatible, a new one is created. The application then attaches this resource to their recording commands, and notifies the implementation when they're done recording with this resource. At this point, however, the resource isn't returned to the free pool; instead, the implementation only returns the resource to the free pool when the GPU is finished with the resource. This way, any resource granted to the application is free to be immediately written into by the CPU. (Indeed, the resource acquisition function may even accept an argument to specify the resource's initial contents.) In this option, the same number of resources will be created and recycled as in option 3, but the application doesn't own the array.
Recommendation
Option 4 is most compatible with a Web API, and should be the model for WebGPU. This is for a few reasons.
Option 2 is a good start, but we can do better. In this model, the CPU-side memcpy() will occur on the GPU's timeline, taking time away from the GPU's execution. The other models allow for issuing the memcpy() during command recording, before ownership of the resource has been given to the GPU.
Option 3 improves upon 2, but has the drawback of making application logic dependent on the number of buffers in the swapchain, which is another potential source of non-portability. In particular, both Metal and Vulkan (and maybe Direct3D, I don't know) don't let the application specify exactly how many buffers the swapchain contains. Vulkan allows the application to request a certain number, but the actual implementation may return a different number than requested. Metal never tells you how many buffers are in the swapchain, but just gives you the "next one." We'd like to avoid web authors hardcoding a constant number of resources in their application because that happened to be how many buffers their local machine was using.
Option 4 has all the benefits of option 3, but has additional benefits:
- Portability Application logic is insensitive to the number of buffers in the platform's swapchain.
- Performance Letting the browser automatically recycle resources means that the browser can improve the performance of poorly-written applications. Recycling should be automatic; developers shouldn't have to opt-in to good performance.
- Fingerprinting The nature of the platform swapchains would provide more entropy for fingerprinting.
Activity
Kangz commentedon Nov 2, 2017
Updated the title to "Resource round-robining", let me know if this isn't what you wanted the issue about.
Overall I think letting applications do 3) on their own or with a library is best, with an option for 2) if we have explicit queues.
I'm not sure what's the link with swapchains but having a "get next texture" for them is ok. That's what we currently have in NXT and helps smooth the difference between different environments.
[-]Resource Lifetime Management[/-][+]Resource Round-Robining[/+]litherum commentedon Nov 2, 2017
The facilities in option 2 are needed for changing the contents of a resource which lives across multiple frames. Our API needs to have these facilities. This issue is about providing an additional mechanism for uploads for data which is only relevant for a single frame.
I'll try to be more specific about the distinction between options 3 and 4.
Here is what the WebGPU application's pseudocode would look like for option 3:
And the pseudocode for option 4:
You can see that option 4 is much easier to write, has the same (or better!) performance characteristics, and doesn't expose sensitive information about the underlying platform to the webpage. A developer can't mess up the
setup()function by hardcoding a3instead ofswapChain.getNumberOfBuffers().litherum commentedon Nov 2, 2017
Actually, now that I think about it, the
MVPs[currentBufferIndex].populateWith(data);in the above example doesn't work anyway, because that isn't associated with a particular point in the queue. And, once you associate it with a point in the queue, you've now devolved to option 2.kainino0x commentedon Nov 2, 2017
In Vulkan, and in NXT, there are Push Constants, which seem basically designed for the MVP matrix use case. They're very simple to use, and I think that on APIs which don't support them natively the implementation will look very similar to Option 4.
litherum commentedon Nov 2, 2017
@kainino0x Yep, both Vulkan and D3D12 have this concept, and if this concept was exposed as part of the WebGPU API, the implementation on Metal would look similar to option 4.
I opened this issue to propose a new mechanism for data uploads, and I think an API mechanism like push constants could fit. There are two issues that we would have to work out, though:
kainino0x commentedon Nov 2, 2017
Your argument 2 is quite interesting, and definitely not solved by the push constant/root constant feature. However I'd generally avoid adding in a new feature and making the API more complex.
I'm not necessarily convinced that we need anything more than triple buffering, which I really think is quite simple compared to the rest of the size of any engine. A triple-buffer-but-only-when-you-need-it helper class should be a pretty small chunk of JS(/C/C++) that an app developer could reuse everywhere they need it.
Do you think there's something here that the browser can do better than a helper library can?
litherum commentedon Nov 2, 2017
Yes. The browser knows how many buffers the swapchain holds, and therefore the number of duplicate resources that need to be created if each draw call needs access to an independent resource. The webpage should not know how many buffers the swap chain holds.
As an aside, I think there's a point above that I didn't make clearly: The existing scheduled writes that we all agree on is sufficient (from an expressiveness point-of-view) for all use cases. MVP matrices or scratch buffers can be created and written using scheduled writes (option 2), and the web content doesn't know how many buffers the swapchains hold. I opened this issue to try to come up with a second mechanism which would improve performance in some situations. This second mechanism isn't a requirement, it's just a nice-to-have.
kainino0x commentedon Nov 3, 2017
Okay, it sounds like we can decide post-mvp. Maybe we should start labelling issues once we decide whether they're pre- or post- mvp.
Although this may have some bearing on what kind of information the API provides about memory allocations (part of the other issue).
Kangz commentedon Nov 3, 2017
Agreed that push-constants aren't a good fit because they are very small and read-only.
I'm not sure I follow the link between the number of images of the swapchain and round-robining. Swapchains textures are used to buffer with the display, but the browser is in control of how many frames or rendering are in flight at any given time (through GPU-CPU sync points) so it could make sure there are always at most 2 frames in flight, which makes only 3 resource copies necessary for round-robining.
Application code can be the following:
kvark commentedon Nov 6, 2017
@litherum
I believe it's missing an extra option that works good for stuff like MVP matrices:
5. Update GPU data by in-lining updates in the command buffer
See vkCmdUpdateBuffer for more info. Basically, the user schedules an update of a GPU resource at some point in the command buffer, which means none of recycling/extra copies/scheduling (expressed in points 1-4) are necessary.
This method would be very handy, although it is applicable for small-ish updates (up to 64k in Vulkan). For bigger resources, I believe giving control to the user would be best, since we can't afford allocating more copies than necessary, and the user knows best how many are needed.
devshgraphicsprogramming commentedon Nov 14, 2018
We tackled a similar issue in our engine.
Every update goes through a staging buffer, although you can write to a staging buffer directly (insecure).
The staging bufffer is like an allocator+heap, except that the frees happen like Garbage Collection through a Deferred GPU Event handler.
Every staging buffer memory free gets associated with a GPU fence (in Vulkan parlance, an event) and the free actually gets executed when the event signals.
When the allocator (staging buffer) tries to allocate it will poll the events if it has low free space while allocating.
This is the highest performance way of doing updates, except for when you want to update host-side API buffers.
magcius commentedon Nov 14, 2018
Plenty of resources will change between frames besides modelviewprojection matrices (anything animating, e.g. texture or bone SRTs, color animations, blend shapes, visibility). Push constants aren't a general solution.
I'm using option #2 in my prototype right now, but I would be OK with implementing #3 in my code. Adding more functionality to the WebGPU layer to buffer updates is bad. e.g. Often times, when building velocity buffers, you want to have a read-only copy of the animation or mvp matrices from last frame anyway, so users are going to be buffering them themselves. Having it hidden behind implementation details is just going to have apps waste more memory.
devshgraphicsprogramming commentedon Nov 14, 2018
I agree explicit is best.
litherum commentedon Jan 9, 2021
I believe WebGPU uses option #4. (The evidence is that I don't believe the number of buffers in the swap chain is exposed, which means #3 is impossible.) So I think this issue can be closed.
Add error scope validation tests (gpuweb#38)