Skip to content

iris:crocus: Fix occlusion query without color/depth writes but with alpha test

Sviatoslav Peleshko requested to merge GL/mesa:fix_alpha_oq into main

The "dark screen" problem appears in Source-based games because of the way they implement brightness/tonemapping post-processing. By default (without using mat_tonemapping_occlusion_use_stencil WA) it works by running an occlusion query with a fragment shader that checks each pixel in the framebuffer to know luminescence proportions.

If its luminescence is in the needed luminescence bucket, the shader sets alpha = 1.0, otherwise alpha = 0.0. The draw is ran without color and depth writes, with depth-test disabled, and fixed-function alpha test (>= 1.0) enabled. So, according to OpenGL spec (where legacy fixed-function alpha test stage is ran before depth test), the occlusion query is supposed to return the amount of pixels that pass the alpha test.

They later use those values to make scene brighter/darker to prevent over-/under-exposure, but in our case the occlusion query always returns full screen of pixels in each bucket, so it tries to darken the scene up until the minimal brightness is reached.

The occlusion query results are incorrect because with RT writes disabled, the PS threads are not dispatched, and the alpha test can't kill some pixels before they reach depth test which all of them pass.

We already knew about this, because we're setting ForceThreadDispatchEnable (just like ThreadDispatchEnable on pre-Gen8 hardware), whenever we would've set PixelShaderKillsPixels.

Starting from the Gen8, the HW is smart enough to not require PixelShaderKillsPixels for some fixed-function features that can kill pixels, such as Alpha Test, so they were not included in the SW ForceThreadDispatchEnable calculations. But unfortunately, it turns out that the HW is not smart enough to also force thread dispatch in such cases, so it has to be done by SW, which this MR implements.

The brightness issue was present and confirmed to be fixed by this MR in Left 4 Dead 2 (#4220), CS: GO, Insurgency, and probably many more Source-based games :)

Edited by Sviatoslav Peleshko

Merge request reports