xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/vulkan/doc/DeferredClears.md (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1# Deferred Clears
2
3Take the following scenario:
4
51. Application binds and clears FBO1
62. Application binds FBO2 and renders to it
73. Application binds FBO1 again and renders to it
8
9Steps 2 and 3 each require a render pass for rendering. The clear in step 1 can potentially be done
10through `loadOp` of the render pass for step 3, assuming step 2 doesn't use the attachments of FBO1.
11This optimization is achieved in ANGLE by deferring clears.
12
13When a clear is issued, one of the following happens:
14
15- If a render pass is already open, the framebuffer is cleared inline (using
16  `vkCmdClearAttachments`)
17- If the clear is not to the whole attachment (i.e. is scissored, or masked), a draw call is used to
18  perform the clear.
19- Otherwise the clear is deferred.
20
21Deferring a clear is done by staging a `Clear` update in the `vk::ImageHelper` corresponding to the
22attachment being cleared.
23
24There are two possibilities at this point:
25
261. The `vk::ImageHelper` is used in any way other than as a framebuffer attachment (for example it's
27   sampled from), or
282. It's used as a framebuffer attachment and rendering is done.
29
30In scenario 1, the staged updates in the `vk::ImageHelper` are flushed. That includes the `Clear`
31updates which will be done with an out-of-render-pass `vkCmdClear*Image` call.
32
33In scenario 2, `FramebufferVk::syncState` is responsible for extracting the staged `Clear` updates,
34assuming there are no subsequent updates to that subresource of the image, and keep them as
35_deferred clears_. The `FramebufferVk` call that immediately follows must handle these clears one
36way or another. In most cases, this implies starting a new render pass and using `loadOp`s to
37perform the clear before the actual operation in that function is performed. This also implies that
38the front-end must always follow a `syncState` call with a call to the backend (and for example
39cannot decide to no-op the call in between). That way, the backend has a chance to flush any
40deferred clears.
41
42If the subsequent call itself is a clear operation, there are further optimizations possible. In
43particular, the previously deferred clears are overridden by and/or re-deferred along with the new
44clears.
45