1
# OpenGL Line Segment Rasterization
3
OpenGL and Vulkan both render line segments as a series of pixels between two points. They differ in
4
which pixels cover the line.
6
For single sample rendering Vulkan uses an algorithm based on quad coverage. A small shape is
7
extruded around the line segment. Samples covered by the shape then represent the line segment. See
8
[the Vulkan spec][VulkanLineRaster] for more details.
10
OpenGL's algorithm is based on [Bresenham's line algorithm][Bresenham]. Bresenham's algorithm
11
selects pixels on the line between the two segment points. Note Bresenham's does not support
12
multisampling. When compared visually you can see the Vulkan line segment rasterization algorithm
13
always selects a superset of the line segment pixels rasterized in OpenGL. See this example:
15
![Vulkan vs OpenGL Line Rasterization][VulkanVsGLLineRaster]
17
The OpenGL spec defines a "diamond-exit" rule to select fragments on a line. Please refer to the 2.0
18
spec section 3.4.1 "Basic Line Segment Rasterization" spec for more details. To implement this rule
19
we inject a small computation to test if a pixel falls within the diamond in the start of the pixel
20
shader. If the pixel fails the diamond test we discard the fragment. Note that we only perform this
21
test when drawing lines. See the section on [Shader Compilation](ShaderModuleCompilation.md) for
22
more info. See the below diagram for an illustration of the diamond rule:
24
![OpenGL Diamond Rule Example][DiamondRule]
26
The diamond rule can be implemented in the fragment shader by computing the
27
intersection between the line segment and the grid that crosses the pixel
28
center. If the distance between an intersection and the pixel center is less
29
than half a pixel then the line enters and exits the diamond. `f` is the pixel
30
center in the diagram. The green circle indicates a diamond exit and the red
31
circles indicate intersections that do not exit the diamond. We detect
32
non-Bresenham fragments when both intersections are outside the diamond.
34
The full code derivation is omitted for brevity. It produces the following
35
fragment shader patch implementation:
38
vec2 p = (((((ANGLEPosition.xy) * 0.5) + 0.5) * viewport.zw) + viewport.xy);
39
vec2 d = dFdx(p) + dFdy(p);
40
vec2 f = gl_FragCoord.xy;
45
vec2 i = abs(p - f + (d/d_) * (f_ - p_));
47
if (i.x > 0.500001 && i.y > 0.500001)
51
Note that we must also pass the viewport size as an internal uniform. We use a small epsilon value
52
to correct for cases when the line segment is perfectly parallel or perpendicular to the window. For
53
code please see [TranslatorVulkan.cpp][TranslatorVulkan.cpp] under
54
`AddLineSegmentRasterizationEmulation`.
58
Although this emulation passes all current GLES CTS tests it is not guaranteed
59
to produce conformant lines. In particular lines that very nearly intersect
60
the junction of four pixels render with holes. For example:
62
![Holes in the emulated Bresenham line][Holes]
64
Therefore for a complete implementation we require the Bresenham line
65
rasterization feature from
66
[VK_EXT_line_rasterization][VK_EXT_line_rasterization].
68
[Bresenham]: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
69
[DiamondRule]: img/LineRasterPixelExample.png
70
[Holes]: img/LineRasterHoles.jpg
71
[TranslatorVulkan.cpp]: https://chromium.googlesource.com/angle/angle/+/refs/heads/master/src/compiler/translator/TranslatorVulkan.cpp
72
[VK_EXT_line_rasterization]: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VK_EXT_line_rasterization.html
73
[VulkanLineRaster]: https://www.khronos.org/registry/vulkan/specs/1.1/html/chap24.html#primsrast-lines-basic
74
[VulkanVsGLLineRaster]: img/LineRasterComparison.gif