r/GraphicsProgramming • u/Pxlop • Apr 30 '22
Question Why don't many AAA games clear the screen/viewport/window?
Does it really help that much with performance?
7
Apr 30 '22
Why clearing the screen, when the shader will write everywhere in the screen ?
Only the z buffer needs to be cleared
Even the sky is drawn, in modern games
6
u/msqrt Apr 30 '22
It helps, usually marginally. But I don't really understand the question; why would you do redundant computation on purpose?
6
u/stingoh May 01 '22
Clearing a render target is not a free operation. The hardware has ways of accelerating a clear, but it’s still not free.
Since you’re going to be drawing over the entire surface of the screen anyway, why bother?
2
u/NOISEbyte Apr 30 '22
Most hardware has got metadata associated with targets (e.g. CMASK and DCC for colour targets, HTILE for depth in case if AMD hardware). These are lossless compression schemes that are mostly there to avoid read and/or writes from/to memory. Very simply explained, it keeps track of the data for a block of pixels if all pixels in that block follow the same pattern (e.g. all zero, all one, ...) or if it has unique data.
When a surface is still compressed, clears are very fast since it only clears the metadata, avoiding the writes to memory. However, only the render backends tend to be able to read/write these compressed formats (i.e. the hardware that deals with depth testing and colour blending). As long as the surface keeps on being used as depth or colour targets, these clears will keep on being fast.
However, if you then bind this surface to a texture unit, for instance as an SRV or UAV, the texture units can't read these compressed formats so it has to be decompressed. Not only can this decompress be expensive, all clears from here on out have to actually clear the memory instead of just the metadata, which is significantly slower and should be avoided!
In some cases, the driver might be able to recompress the surface but this is a loose bet and very situational, with the heuristics for this often being quite a black box.
TL;dr: as long as you're careful, they're fast but sometimes the decompress can't be avoided at which point the clears should be avoided at all costs.
1
u/cakeonaut Apr 30 '22
I heard the opposite, that clears were the way to go because resetting the hierarchic depth buffer leads to faster depth culling later. I haven’t measured the difference so try both ways.
8
u/Shadowratenator Apr 30 '22
Lets say you have some variable in your code. Would you set it to null prior to every time you want to assign some new value to it?