This is based on a previous patch by Marcin Ślusarz addressing the same issue, though it's largely rewritten, simplified and includes additional fixes. Reviewed-by: Caio Oliveira <caio.oliveira@intel.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26606>