Sun shadow rendering optimization using max value from depth buffer

Hi, guys!

I have open area: town with densely located buildings.
Also I have to optimize ‘sunlight shadow rendering’ pass.

The idea is:
If camera surrounded by buildings, than there is no reason render hidden buildings in shadow pass.

So, I mean: If any object located behind of max depth value (excluding depth values of skybox) of rendered scene,
then object and even its shadow is not visible at all.

see screenshot (depth map presents in top right corner):

The main issue is: I need to receive depth value on the CPU side from the GPU.
The question is: How can I do that?

As I know a reading back texture requires a stallen pipeline.
It is too expensive (not acceptable).

It seems I have two basic variants:

  1. write value to SSBO, and read it back.

    • Generate depth texture 1x1 with max depth value excluding skybox values.
    • Render points with GL_PRIMITIVES_GENERATED query using ‘binary search’ (as it do quick sort algorithm).
    • Using viewport: 1x1, depth-test enabled, depth-writes and color-writes disabled.

Maybe I miss something important?

Thank you for any advices.
Have a good day!