Search code examples
vulkan

Depth biasing seems to be working for lines in Vulkan


I have made a program that draws meshes together with their wireframe.

enter image description here

There is z-fighting between the triangles and the line.

For solving this, I use depth biasing on the pipeline that draws the triangles.

enter image description here

I would have preferred to apply the inverse bias on the lines, since it would fit better my code organization. However, in the Vulkan spec, I only see depth bias referencing "polygons":

25.9.3. Depth Bias

The depth values of all fragments generated by the rasterization of a polygon can be biased (offset) by a single depth bias value that is computed for that polygon.

That made me a bit sad.

But I gave it a chance and... it worked! I was indeed able to apply the depth bias on the lines instead!

So I'm wondering, it this correct behaviour? Do lines also count as polygons? Or is it a bug in my Vulkan driver?

I'm using a Radeon RX6600.

EDIT:

I have found this in the spec:

25.10. Polygons

A polygon results from the decomposition of a triangle strip, triangle fan or a series of independent triangles. Like points and line segments, polygon rasterization is controlled by several variables in the VkPipelineRasterizationStateCreateInfo structure.


Solution

  • It looks like the resolution of this issue added the text:

    Depth bias is applied to triangle topology primitives received by the rasterizer regardless of polygon mode. Depth bias may also be applied to line and point topology primitives received by the rasterizer.

    So that's the answer. The "may" part of it doesn't help you much and suggests that you would have to apply the bias to the polygons instead in order to be portable across devices. I also suspect that most people first think of applying depth bias to polygons anyway, as you did, and so the mushiness of it being applied to lines doesn't hurt that much.

    Why is it this way? I can only guess, but it is possible that some GPUs do not have line rasterization hardware that matches the Vulkan specification very well and the driver may fall back to drawing lines with polygons for certain line rasterization pipeline settings. And so some of the polygon rasterization settings get applied. (I worked on a large application once where we ended up doing all of the line drawing with quads because the support for line rasterization features varied quite a bit across devices.)