Repository navigation
Description
From our conversation about occlusion queries.
Motivation
One of the natural use cases for using occlusion queries is in conjunction with predicated rendering. One can achieve object culling by rendering the bounding box of some object to an offscreen render target (or with rasterization disabled) using some trivial shader, and then by using occlusion queries to determine whether the object actually ends up hitting the screen. If it does, then the object can be rendered to the real render target like normal, but if it doesn't, then the object can be culled.
The problem is that the CPU shouldn't have to wait for the results of the occlusion query to know whether to issue the appropriate draw calls, because that can cause 2-3 frames of latency, which is too late. Instead, predicated rendering allows the GPU to turn a draw call into a no-op based on whether or not a buffer has a nonzero value in a particular place. The occlusion query can be hooked up to populate this value.
Direct3D 12
There's a command that can be added to the command list: ID3DGraphicsCommandList::SetPredication(). It takes a buffer and an offset into the buffer. After issuing this command, future draw calls will consult with that 32-bit word in the buffer to determine if they should be turned into no-ops. There's a new resource state: D3D12_RESOURCE_STATE_PREDICATION, which the buffer needs to be in (you can transition it to the state using ID3D12GraphicsCommandList::ResourceBarrier()).
But it's not just draw calls. Other operations are predicated too:
- DrawInstanced
- DrawIndexedInstanced
- Dispatch
- CopyTextureRegion
- CopyBufferRegion
- CopyResource
- CopyTiles
- ResolveSubresource
- ClearDepthStencilView
- ClearRenderTargetView
- ClearUnorderedAccessViewUint
- ClearUnorderedAccessViewFloat
- ExecuteIndirect
Vulkan - VK_EXT_conditional_rendering
GPUInfo says 31% Windows, 54% Linux, and 1% Android.
Works very similarly to D3D12. There's a new command you can issue in a command buffer: vkCmdBeginConditionalRenderingEXT(), and it takes a buffer and an offset just like D3D12 does. And, like D3D12, there are new flags values that this buffer needs to be vkCmdPipelineBarrier()-ed to: VK_ACCESS_CONDITIONAL_RENDERING_READ_BIT_EXT, VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT, and VK_PIPELINE_STAGE_CONDITIONAL_RENDERING_BIT_EXT.
These operations are conditional:
- drawing commands
- dispatching commands
vkCmdClearAttachments()
Metal
Metal doesn't have a direct analogue to the other APIs. Instead, the effect can be achieved two different ways:
- The
indirectflavor of draw commands and dispatch commands. ThevertexCountandinstanceCountfields, as present inside the indirect buffer, can be set to 0, which effectively no-ops the draw call. - Using Indirect Command Buffers. These can be recorded on the GPU, which means the draw calls themselves can be omitted from the recording. See the related issue for more discussion.
Both D3D12 and Vulkan have optional support for indirect commands; indeed, they are already part of WebGPU. Both D3D12 and Vulkan also have optional support for Indirect Command Buffers, though in Vulkan, it's only achievable using the vendor-specific VK_NVX_device_generated_commands.
Indirect draws/dispatches are available on all relevant Metal devices. Indirect Command Buffers are only available on some relevant Metal devices. The Metal Feature Set Tables contain additional information. Given that predicated rendering already would be optional because Vulkan support for VK_EXT_conditional_rendering is optional, both of these options seem reasonable.
Activity
litherum commentedon Feb 18, 2020
We have a couple paths we could take in WebGPU:
setPredication()command inside render passes and compute passes. Implement it on Metal by emulating it using indirect draws or indirect command buffers. The emulation would require overhead of an amount of GPU buffer memory proportional to the number of draw calls, which is unfortunate. Also, implementation gets tricky when the application itself issues indirect draw calls - we would have to run a compute shader before the render pass to zero-out the relevant fields inside the indirect buffer.kainino0x commentedon Feb 18, 2020
We did already decide to include indirect draw/dispatch. Although I'm not 100% sure we had the correct info at the time? See this comment and the one above it:
#317 (comment)
It seems(?) like I thought at the time that all iOS supported it. However, the current feature set tables say it becomes available in MTLGPUFamilyApple3 (Apple A9). But based on the comment just above that, it may just be that we decided that pre-A9 doesn't need to be supported. (I know there was discussion of what hardware was the first we needed to support, but I don't remember which it was.)
litherum commentedon Feb 18, 2020
This was misleading. I've updated the OP.
litherum commentedon Feb 18, 2020
Because of our buffer uploading rules, this may actually be tricky for a web author to implement themselves right now. We’ll see how the various new buffer upload proposals fare. If they allow an author to append data to a buffer for the current frame performantly, then I would favor path 1. This is because emulation of path 2 in the browser is tricky / difficult, so its unnecessary if authors can achieve it themselves naturally using other mechanisms. Also,
VK_EXT_conditional_renderingwouldn't be required, so conditional rendering wouldn't (appear to be) only available on only 1% of Android devices.Richard-Yunchao commentedon Feb 24, 2020
@litherum , thanks for your investigation. I am not sure whether I understood your proposal correctly. But it looks to me that path 1 means that web devs need to do the steps below by themselves in their web apps: 1)use occlusion query in a frame, 2) read/resolve the query data into a GPUBuffer, 3) issue an indirect draw/dispatch, and set indirect buffer accordingly (for example, set instanceCount to 0 if occlusion sample in GPUBuffer is zero). For path 2, it means that we add setPredication API to the WebGPU spec, and web devs don't need to reset indirect buffer (and web devs can use direct render/dispatch). And WebGPU implementers do all of these steps under the hood on Metal. For D3D and Vulkan, we can call corresponding API at native side directly.
Another big difference between path 1 and path 2 is the support on devices just as you mentioned.
BTW,
ID3DGraphicsCommandList::SetPredication()in D3D12 says that it can't be supported in direct command buffer, it should be in a bundle (indirect command buffer).litherum commentedon Feb 24, 2020
@Richard-Yunchao Yes, that's correct. If authors can already achieve predicated rendering themselves using existing facilities, then that fact significantly weakens the motivation to add it to the browser. Given that the existing facilities work everywhere, on all relevant devices, and even outside of bundles, then option 1 seems fairly appealing.
damyanp commentedon Feb 25, 2020
The comment in the docs about bundles vs direct command lists is clarifying that a bundle executed with ExecuteBundle will inherit the predication state from the command list it is executed from. You cannot call SetPredication while recording a bundle (but, I would strongly encourage any WebGPU implementation considering using bundles to reconsider their plans).
Explicit predication has some advantages:
magcius commentedon Feb 25, 2020
Predicated rendering tends to be a bigger issue for tilers to support, since could end up synchronizing on the results of other tiles, which can be really expensive.
damyanp commentedon Feb 25, 2020
I think that this would probably also be true of anything that builds up an argument buffer to try and emulate predication using ExecuteIndirect.
alecazam commentedon May 20, 2021
It would be nice in Metal if the boolean/counting occlusion query could set a 4 byte field to 0 (f.e. the primitive count) only if not visible. The boolean query is present with the Indirect Draws, and counting query present with Indirect Command Buffers on A9.
Currently occlusions only write out 8 bytes, but the Indirect Command Buffer block is all composed of 4 byte fields. On the Indirect Draw, the numPrims and numInstances are together nicely in 8 bytes, but I didn't check if they were 8 byte aligned to overwrite with a query.
Predicated rendering currently requires a compute shader just to clobber the numPrimitives field on the indirect draw call. But the query could write over this. But one wouldn't want 1 (boolean) or the count (# pixels) to be copied into the numPrimitives field. This would be a 4-byte only write zero case for occlusion, and the buffer index/offset would reflect the field of the draw structure.
This would also work for these older platforms that can't GPU generate indirect command buffer calls (A11+). But it's all to whether the existing hw could support this change in functionality. Otherwise possibly tack on 8-bytes to the indirect draw and command buffer structs and have the hw test that for non-zero instead of requiring zeroing out the numPrimitives/numInstances.
alecazam commentedon May 21, 2021
Even easier that anything presented here is to assume instancing. Don't try to knock out an ICB or IndirectDraw since that's only a single draw. Use the Metal occlusion query (or geometry shader on other hw), and overwrite some piece of data in the instance MTLBuffer with the occlusion result. Then in the vertex shader vertkill all the data if that written value is zero. One also has a boolean/counting data although so limited material LOD would be possible in the vertex shader. This would work on any iOS device with even boolean occlusion queries (A7+).
An API to handling instanced boxes would be nice where occlusion results are written to offsets for multiple boxes using a single call. The current APIs require submitting a draw for each box(es) associated with a single instance.
alecazam commentedon May 23, 2022
I feel like occlusion queries are quite limiting for occlusion. Sampling from a hierarchical z with an O(1) gather operation is a lot more flexible. This can render all test boxes in one draw call of point primitives, isn't limited by occlusion counts, doesn't lock down depth/raster, supports instancing and indirect draws too. AC/UE5 takes this further with two pass occlusion. Vert kill off the occlusion buffer is needed where predicated rendering isn't available.
Update/cleanup map_oom.spec.ts (gpuweb#551)