Repository navigation
Description
We've been going back and forth on the need for render/compute/blit passes, and I'd like to systematize the arguments and suggest models we could navigate from.
In native APIs, Vulkan has render passes only, Metal requires all the work to be done in one of the blit/compute/render pass. And D3D12 does not have any passes yet.
Here are the major areas (I can think of) that could take advantage of a pass concept, and the ideas on how they might be relevant to WebGPU:
Resource bindings inheritance - #24
In Metal, resource bindings are only valid with the scope of a pass, where in Vulkan/D3D12 they are scoped to command buffers. This allows Metal runtime/driver to allocate descriptors based on everything used in a pass. This isn't a benefit for WebGPU, given that we have explicit resource bind groups created by the user.
Synchronization and validation - #27, #59
In Vulkan the memory barriers and layout transitions are explicit. In D3D12 the resource state transitions as well as UAV/aliasing barriers are mostly explicit, but there are rules for automatic to/from conversion to the "common" state. In Metal, memory barriers are mostly implicit. They are inserted automatically at the render pass boundaries, around individual dispatch and transfer calls.
For WebGPU, we decided to not expose the memory barriers in the API, and thus we can insert the appropriate synchronization commands between passes. In order for this to be valid, we introduce the "Usage" (mutable) state of a resource and declare a requirement of each resource having a constant usage across a pass. However, we also decided to automatically synchronize individual dispatch (UAV barriers only) and transfer calls, much like Metal. It isn't a long stretch to go from here to a full synchronization (allowing layout changes, for example), in which case the construct of a compute and transfer pass stops being useful (in the context of memory barriers).
Work scheduling - #22
Hardware queues on Vulkan and D3D12 are explicit. In Metal, the MTLCommandQueue is also exposed, but it doesn't have a direct relation to a hardware queue. Metal runtime figures out the dependencies between passes (which is sound, given that all the work is split into passes) and reserves the right to actually execute them on different hardware queues (note: how much this is the case in reality is unknown to me).
In WebGPU we decided to not expose the hardware queues, thus I find the opportunity of automatically taking advantage of them to be quite strong to miss.
Type safety
Each kind of a pass has a restricted set of operations that can be recorded. E.g. one can't dispatch() or bind compute resources in a MTLBlitCommandEncoder pass. This makes the API more usable, assists documentation, and is generally is a good practice for API design.
Activity
Kangz commentedon Jul 25, 2018
Thanks for the investigation.
The resource binding inheritance does help the D3D12 backend in the same way it helps the Metal driver: we might need to rewrite descriptors in heaps per-pass (with caching to avoid doing it if we can obviously), having all descriptors at the ready would help with that.
For synchronization the concept of compute pass helps, because we know no image layout changes are needed inside the pass, only UAV barriers. This both helps make efficient implementations, and convey to the app that the only "hidden" cost inside a compute pass would be because of UAV barriers. It makes WebGPU more efficient because it reduces the granularity at which layout transitions need to be considered and inserted.
We decided to not expose queues in the MVP of WebGPU but we're still interested in potentially having them in v1.
kvark commentedon Jul 25, 2018
Here is a solution that I believe to be the most sound of our alternatives.
Instead of
WebGPUCommandEncoderthat we have now, expose two different types of encoders/passes:WebGPURenderPass- user provides the framebuffer (or render targets) to create one.WebGPUWorkPass- no arguments are needed to create one.Why I think this is a good idea:
kainino0x commentedon Jul 25, 2018
Does
Queue.Submit(orDevice.Submitif there are no queues) then take a sequence of(RenderPass or WorkPass)?Would, e.g., the Vulkan backend construct a
VkCommandBufferwhen the RenderPass/WorkPass is created, or not until they are submitted?How might secondary command buffers work? Would we have secondary RenderPass and secondary WorkPass types? Or ARE the RenderPass/WorkPass objects equivalent to secondary command buffers that are inserted into one big command buffer at
Queue.Submit?kvark commentedon Jul 27, 2018
@kainino0x
Yes. We could also have a base interface for those two (
WebGPUPass).Given that passes have isolated resource scopes, it would make sense for Vulkan to encode them into different
VkCommandBufferobjects. Preferably, not at submission time.This is a hard problem, it depends on many factors, including some ISV feedback on what they want to use the secondary command buffers for. For example, we could expose (1) a D3D12-like bundle that can be encoded and then executed inside of a render pass. Another direction is to expose (2) Metal-like parallel command encoders, i.e. a variant of the render pass start command that would give you a
Sequence<WebGPURenderPass>(of a given length) that you encode independently (but are executed sequentially). Finally, if we introduce the sub-pass concept, we could have a (3)WebGPURenderPassobject returned per sub-pass.There are many options. I think we need (1) + (3) provided in the end, but there is a long way of discussions to it. I don't think that accepting this proposal would harm our future exploration of those options.
Kangz commentedon Jul 30, 2018
@kvark correct me if I'm wrong but my understanding is that your proposal tries to reduce the number of concepts and corner-cases users of WebGPU will need to internalize, as well as improve type-safety (even if it's not a thing in JS). Tweaking your proposal a little bit (which makes it close-ish to Metal), what if we have the following passes:
WebGPURenderPasslike in your proposalWebGPUComputePassthat can only contain setting state for compute work and dispatchesWebGPUOtherWorkPass(though the name sucks ^^) that contains everything but render and compute stuff.In term of cognitive overhead I think it has the following advantages:
WebGPUOtherWorkPassdoesn't have state the developer has to worry about.WebGPUComputePassdoesn't have non-programmable operations intermixed that may or may not cause stalls or other expensive things the developer might not expect.WebGPURenderPassandWebGPUComputePasscontain the execution of programs on the GPU and the state that controls it. That's it. To make it more clear we could make them inherit from aWebGPUProgrammablePassthat have pipeline, bindgroup and pushconstants functions. (+ queries eventually).WebGPURenderPassandWebGPUComputePassare essentially synchronized with caveats for UAVs that are unsynchronized in render passes and unsynchronized inside a single dispatch. The same cognitive burden that is present in your proposal too.The pass object approach you highlighted has advantages compared to putting all commands at the same level in a command buffer: type-safety and less run-time errors because you forgot to begin or end a pass. There's still a bunch of unknowns like Kai said but it looks like an interesting direction.
kvark commentedon Jul 30, 2018
@Kangz I like the concept of
WebGPUProgrammablePassthat both compute and render derive from. Overall, your modification sounds feasible to me.devshgraphicsprogramming commentedon Nov 14, 2018
Not exposing GPU synchronisation and injecting it implicitly is madness.
You will stall every single deferred shading pipeline that has been created since 2016.
devshgraphicsprogramming commentedon Nov 14, 2018
Moreover foregoing queues, completely bricks the following performance saviours:
kvark commentedon Nov 15, 2018
@devshgraphicsprogramming
Can you elaborate?
I agree that proper support for multi-queue/gpu may be hammered by implicit synchronization, but it's still to be figured out, see #95
devshgraphicsprogramming commentedon Nov 15, 2018
Basically you can have mutiple queues so that compute happens asynchronously to graphics work, this is good because as soon as you fill your GBuffer with rasterization you can kick off the deferred shading, postprocessing, SSAO, FXAA, Tonemapping, etc. in compute shaders.
Both Nvidia and AMD support async compute under vulkan, which means that the compute work for the previous frame can overlap with the rasterization of the GBuffer for the next, as long as the ordering is not overconstrained by over-the-top synchronisation.
Furthermore you should expose Vulkan-like sub-passes and the subpass dependencies explicitly so that tiled GPU architectures (all mobile, some Intel + NVidia Maxwell) can avoid resolving the fast local eDRAM to main memory between sub-passes.
This is what actually makes Deferred Rendering feasible on mobile GPUs (you read GBuffer in deferred shading pass straight from tiler scratch memory, not from texture).
devshgraphicsprogramming commentedon Nov 15, 2018
Best example of overlapping compute and graphics for the same frame is compute shader LoD selection and culling while the Z-prepass is running.
Z-prepass usually severely underutilizes the Programmable Shading Cores, so there is some spare power to do compute shader with.
Nvidia and AMD both support running two or more OpenCL kernels/Graphics Shaders on the same GPU core simultaneously (CUDA explicitly says it can "hyperthread" and keep 2 different GPU programs resident on the same SM).
magcius commentedon Nov 16, 2018
As far as my understanding goes, NVIDIA does not support asynchronous queues nor dual-issuing compute and graphics work. In fact, the stall in swapping between compute and graphics is a well-known issue that has only been fixed, I believe, in Volta.
kvark commentedon Nov 16, 2018
@magcius I checked vulkan HW DB, picked a random Geforce MX150 and I'm seeing three different queue families (graphics, compute, transfer): https://vulkan.gpuinfo.org/displayreport.php?id=4560#queuefamilies
What would be the point of exposing those families differently if they get serialized (causing the stall on work switch)?
magcius commentedon Nov 16, 2018
mjp has the best public summary of it that I know of: https://mynameismjp.wordpress.com/2018/09/08/breaking-down-barriers-part-5-back-to-the-real-world/ . Ctrl+F for "As for Nvidia"
And, sorry, it looks like the architectural change is Pascal, not Volta.
7 remaining items
devshgraphicsprogramming commentedon Nov 16, 2018
Why bother, the user can just as well write a poor compute shader that expects work-groups to synchronise between themselves.
You are worrying about making sure accesses of different dispatches stay sequential, when you will never be able to prevent work-groups within the same dispatch stay sequential.
Again that will be a very big DAG, I think you're working under the presumption the DAG will represent a single-frame's worth of work.
This is not the case with higher-end applications where the DAG is never ending (overlapped frame processing and resource memory aliasing) and not very periodic (streaming).
AFAIK no API is portable according to that definition, except for consoles.
Also, no matter how good of a portable front-end API you design you will have to implement it in a back-end API, which is not portable.
With your current design directions you are setting yourself up to create a much more fleshed out API which requires a much more complex implementation leaving more room for non-portable behaviour and be far more at the mercy of the webGPU implementors.
So explicit barriers were agreed upon or what?
kvark commentedon Nov 16, 2018
Yes, that's totally fair, but we need to draw the line somewhere. I'd prefer to draw it at the pass boundary (hence, #59), but the group agreed (for now) to draw it at the dispatch boundary for compute work.
Single frame is not my point. I'm trying to say that most resources are read-only, and there isn't an enormous amount of the rest to synchronize access. Besides, as I mentioned in a neighboring thread, we still have to track this DAG for validation...
I disagree. We, being the clients of native APIs, are responsible for using it in a portable way.
devshgraphicsprogramming commentedon Nov 16, 2018
And for graphics work, where do you draw the boundary?
Because dependencies need to be defined between draws (graphics shaders can write to SSBO and storage images too), not just between renderpasses/subpasses.
You can, with significant effort.
devshgraphicsprogramming commentedon Nov 16, 2018
If by validation you mean making sure the underlying native API object is not deleted before the GPU is done using it, then I don't believe you need a DAG for that.... your standard Garbage Collector enhanced with a references to API fences placed after the last use in each queue (detected by GC) will do.
kvark commentedon Nov 16, 2018
@devshgraphicsprogramming
At the render pass boundary, since within a render pass it's not feasible to insert memory barriers (kills the purpose of having a render pass on tiling hardware).
Yes, the consequence of this decision is that we'll have multiple draw calls racing for the SSBO accesses. However, unlike with compute passes, it's too performance heavy to try to synchronize those.
Not just deletion. We need to guarantee that the same resource isn't accessed at a time from different places, hence we need to track the usage, and hence comes DAG, effectively.
devshgraphicsprogramming commentedon Nov 16, 2018
Memory barrier is not a fine-grained memory dependency, also these memory dependencies will not kill the purpose of a render pass on tiling hardware.
Let me give you an example:
You have a tiled deferred renderer that uses a Z-Prepass.
This is your Graphics workload:
Rasterize Z-Prepass subpass
[Does not actually depend on Z-Prepass finishing, can overlap] Rasterize GBuffer subpass
[Does not require any extra dependencies due to per-pixel hardware ordering guarantees] Rasterize albedo decals, environment probes and emissive lighting with blending
[Depends all GBuffer modifications complete + All the compute workloads] Deferred Lighting (this would actually be done in a fragment shader as opposed to compute to make sure you have subpass inputs available to leverage tilers/caches)
Now These could be your INDEPENDENT compute workloads:
Set 0:
[Depends nothing from graphics] Particle System Simulation
Set 1:
[Depends on Z-Prepass] Min-max Z-Resolve
Set 2:
[Depends on Min-max Z-Resolve] Light-Tile Culling
Set 3:
[Depends on Min-max Z-Resolve] Participating Media Volumetric Lighting
Now you can & should have the memory dependencies between different passes/sets of compute work.
If you cannot achieve this for graphics work, why try it for compute?
Why? (examples)
kvark commentedon Nov 16, 2018
@devshgraphicsprogramming I think we are losing touch here. I'm saying that it's not feasible to insert any memory barriers within a render (sub)pass. Vulkan's support for those is extremely limited, and we weren't able to find a representative use case on why those should be done by us.
To clarify, we need to guarantee that any mutable access to a resource is unique. I.e. you can still read the same resource from different draw calls/stages, but modifying it (without proper synchronization in place) is going to be a source of non-portable behavior that isn't suitable for the Web.
devshgraphicsprogramming commentedon Nov 16, 2018
You are the ones advocating implicit insertion of synchronisation in-place of the user who should know his data dependencies best.
In the above example some dependencies must be inserted to get anything approaching the correct result, some can be skipped.
In the above example just the virtue of read-write access to a Z-Buffer would force your theoretical resource tracker to synchronise every dispatch/draw that touches it.
I do acknowledge that somehow you want to grant FBO attachments some special privilege of being exempt from this implicit ordering, but let me point out to you that there exist rendering techniques that forego using classical attachments (A-Buffer) in favour of using UAV's in their place.
What about all the programmable blending, compute and order-independent-transparency techniques that depend on read-write access to the same resource both within a dispatch/draw-call as well as across draw-calls and dispatches?
The whole algorithm of Knowles on Order Independent Transparency would no longer become interactive if you disallowed non-deterministic and non-repeatable memory modification.
devshgraphicsprogramming commentedon Nov 16, 2018
The prime example of a thing impossible under your implicit synchronisation proposal and portability constraints is a GPU-created Atomic Linked List.
devshgraphicsprogramming commentedon Nov 16, 2018
My overarching point here is that your working group's proposal and concern with keeping mutable access to a resource unique at a renderpass/dispatch(or compute pass) granularity is essentially fighting windmills.
Since it is impossible/impractical to achieve the portable and deterministic behaviour you wish for without stripping the API of much of the features that make the native APIs so enticing.
kvark commentedon Nov 16, 2018
@devshgraphicsprogramming
Sorry, I'm having trouble following your example. Could you explain in a bit more detail what it's demonstrating? If it's to counter my point that we don't want in-render-pass synchronization, where does the example need it?
Which access exactly do you mean?
So these appear to be the cases where unsynchronized UAV access is needed across dispatches, right? If so, can you provide some links? If the group missed those cases and we decide that we need to support the efficiently, we may re-consider the compute pass synchronization scope.
Metal is a native API that is performant, and it does the synchronization (for basic scenarios, Metal provides means to do that manually as well) on behalf of the user. I hear your skepticism, but so far we haven't got substantial evidence that going this path is wrong. Perhaps, we just need you to support the point with more concrete examples (of things we'd do slow with the current model).
devshgraphicsprogramming commentedon Nov 16, 2018
I will be back with concrete examples, diagrams and explanations.
For the time being I link you to Pyarelal Knowles' body of OIT work
https://researchbank.rmit.edu.au/eserv/rmit:161520/Knowles.pdf
http://goanna.cs.rmit.edu.au/~pknowles/bma-preprint.pdf
as well as more relevant to UAV synch:
https://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-SparseVoxelization.pdf (empty/attachmentless FBO and storage image used in place)
https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=2ahUKEwjbgqnP29neAhUBkiwKHX5-DEUQFjAAegQICRAC&url=https%3A%2F%2Fresearch.activision.com%2Feikmo72643%2Fattachments%2Feikmo72643%2Ftech-reports%2F19%2F6%2FPracticalOIT.pdf&usg=AOvVaw066lIeOcyUIXzL9DkVr1ae
(Using CS instead of rasterizer to render particle sprites inbetween transparent OIT geometry)
I have a little issue following your current model, mostly because the propositions are split between 3 issues and so on.
It would help if I could refer to a single formal specification of what are the resources you intend to implicitly synchronise, the scope of the implicit synchronisation and the points at which it would occur.
Also a mention (possible implementation) of the synchronisation primitives you would use in at least one of D3D12 or Vulkan.
Furthermore a clear delineation between synchronisation as execution dependencies and memory dependencies.
Lastly a similar specification of scope of your "mutability uniqueness" (as obviously it cannot be ensured at a granularity less than that of a draw/dispatch)
litherum commentedon Apr 2, 2020
Passes are part of the spec.
Use an async-mutex to run one test at a time in WPT runtime (gpuweb#64)