In Hanafi reasoning, the qibla pertains to the designated site of the Kaaba and its vertical direction, not only its visible masonry. Hence prayer on the roof can be legally valid.
A reported Shafi‘i argument places greater weight on having building or an appropriate barrier in front. The issue is whether the qibla is limited to the physical structure or includes its designated site and direction.