~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/ThirdParty/ANGLE/src/libANGLE/renderer/vulkan/doc/OpenGLLineSegmentRasterization.md

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# OpenGL Line Segment Rasterization
 
2
 
 
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.
 
5
 
 
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.
 
9
 
 
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:
 
14
 
 
15
![Vulkan vs OpenGL Line Rasterization][VulkanVsGLLineRaster]
 
16
 
 
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:
 
23
 
 
24
![OpenGL Diamond Rule Example][DiamondRule]
 
25
 
 
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.
 
33
 
 
34
The full code derivation is omitted for brevity. It produces the following
 
35
fragment shader patch implementation:
 
36
 
 
37
```
 
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;
 
41
vec2 p_ = p.yx;
 
42
vec2 d_ = d.yx;
 
43
vec2 f_ = f.yx;
 
44
 
 
45
vec2 i = abs(p - f + (d/d_) * (f_ - p_));
 
46
 
 
47
if (i.x > 0.500001 && i.y  > 0.500001)
 
48
        discard;
 
49
```
 
50
 
 
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`.
 
55
 
 
56
## Limitations
 
57
 
 
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:
 
61
 
 
62
![Holes in the emulated Bresenham line][Holes]
 
63
 
 
64
Therefore for a complete implementation we require the Bresenham line
 
65
rasterization feature from
 
66
[VK_EXT_line_rasterization][VK_EXT_line_rasterization].
 
67
 
 
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