Skip to content

vulkan: Detect pNext chain loops in vk_foreach_struct()

Faith Ekstrand requested to merge gfxstrand/mesa:vulkan/pnext-loop into main

This implements the "tortoise and the hare" algorithm for detecting cycles in graphs. We use the caller's iterator as the hare and our own internal copy as the tortoise. Conveniently, VkBaseOutStructure (and VkBaseInStructure which is identical except the pointer type on pNext) have a pointer we can use for the tortoise and an sType which we can use for a counter to ensure we only increment the tortose every other loop iteration.

There are more efficient algorithms than tortoise and hare but they require allocating memory for something like a hash set of seen nodes. Since this for debug purposes only, it's ok for it to be a bit inefficient in the case where it hits the assert. In the usual case of no loops, it's the same runtime efficiency as the unchecked version except that it does a tiny bit of math and 50% more pointer chases.

Edited by Faith Ekstrand

Merge request reports