Debian Codegraph @

third_party/rust/aa-stroke/src/tri_rasterize.rs

raw
1 /* The rasterization code here is based off of piglit/tests/general/triangle-rasterization.cpp:
2
3 /**************************************************************************
4 *
5 * Copyright 2012 VMware, Inc.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 */
31
32 use std::ops::Index;
33 use crate::Vertex as OutputVertex;
34 #[derive(Debug)]
35 struct Vertex {
36 x: f32,
37 y: f32,
38 coverage: f32
39 }
40 #[derive(Debug)]
41 struct Triangle {
42 v: [Vertex; 3],
43 }
44
45 impl Index<usize> for Triangle {
46 type Output = Vertex;
47
48 fn index(&self, index: usize) -> &Self::Output {
49 &self.v[index]
50 }
51 }
52
53 // D3D11 mandates 8 bit subpixel precision:
54 // https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#CoordinateSnapping
55 const FIXED_SHIFT: i32 = 8;
56 const FIXED_ONE: f32 = (1 << FIXED_SHIFT) as f32;
57
58 /* Proper rounding of float to integer */
59 fn iround(mut v: f32) -> i64 {
60 if v > 0.0 {
61 v += 0.5;
62 }
63 if v < 0.0 {
64 v -= 0.5;
65 }
66 return v as i64
67 }
68
69 /* Based on http://devmaster.net/forums/topic/1145-advanced-rasterization */
70 fn rast_triangle(buffer: &mut [u8], width: usize, height: usize, tri: &Triangle) {
71 let center_offset = -0.5;
72
73 let mut coverage1 = tri[0].coverage;
74 let mut coverage2 = tri[1].coverage;
75 let mut coverage3 = tri[2].coverage;
76
77 /* fixed point coordinates */
78 let mut x1 = iround(FIXED_ONE * (tri[0].x + center_offset));
79 let x2 = iround(FIXED_ONE * (tri[1].x + center_offset));
80 let mut x3 = iround(FIXED_ONE * (tri[2].x + center_offset));
81
82 let mut y1 = iround(FIXED_ONE * (tri[0].y + center_offset));
83 let y2 = iround(FIXED_ONE * (tri[1].y + center_offset));
84 let mut y3 = iround(FIXED_ONE * (tri[2].y + center_offset));
85
86
87 /* Force correct vertex order */
88 let cross = (x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2);
89 if cross > 0 {
90 std::mem::swap(&mut x1, &mut x3);
91 std::mem::swap(&mut y1, &mut y3);
92 // I don't understand why coverage 2 and 3 are swapped instead of 1 and 3
93 std::mem::swap(&mut coverage2, &mut coverage3);
94 } else {
95 std::mem::swap(&mut coverage1, &mut coverage3);
96 }
97
98 /* Deltas */
99 let dx12 = x1 - x2;
100 let dx23 = x2 - x3;
101 let dx31 = x3 - x1;
102
103 let dy12 = y1 - y2;
104 let dy23 = y2 - y3;
105 let dy31 = y3 - y1;
106
107 /* Fixed-point deltas */
108 let fdx12 = dx12 << FIXED_SHIFT;
109 let fdx23 = dx23 << FIXED_SHIFT;
110 let fdx31 = dx31 << FIXED_SHIFT;
111
112 let fdy12 = dy12 << FIXED_SHIFT;
113 let fdy23 = dy23 << FIXED_SHIFT;
114 let fdy31 = dy31 << FIXED_SHIFT;
115
116 /* Bounding rectangle */
117 let mut minx = x1.min(x2).min(x3) >> FIXED_SHIFT;
118 let mut maxx = x1.max(x2).max(x3) >> FIXED_SHIFT;
119
120 let mut miny = y1.min(y2).min(y3) >> FIXED_SHIFT;
121 let mut maxy = y1.max(y2).max(y3) >> FIXED_SHIFT;
122
123 minx = minx.max(0);
124 maxx = maxx.min(width as i64 - 1);
125
126 miny = miny.max(0);
127 maxy = maxy.min(height as i64 - 1);
128
129 /* Half-edge constants */
130 let mut c1 = dy12 * x1 - dx12 * y1;
131 let mut c2 = dy23 * x2 - dx23 * y2;
132 let mut c3 = dy31 * x3 - dx31 * y3;
133
134 /* Correct for top-left filling convention */
135 if dy12 < 0 || (dy12 == 0 && dx12 < 0) { c1 += 1 }
136 if dy23 < 0 || (dy23 == 0 && dx23 < 0) { c2 += 1 }
137 if dy31 < 0 || (dy31 == 0 && dx31 < 0) { c3 += 1 }
138
139 let mut cy1 = c1 + dx12 * (miny << FIXED_SHIFT) - dy12 * (minx << FIXED_SHIFT);
140 let mut cy2 = c2 + dx23 * (miny << FIXED_SHIFT) - dy23 * (minx << FIXED_SHIFT);
141 let mut cy3 = c3 + dx31 * (miny << FIXED_SHIFT) - dy31 * (minx << FIXED_SHIFT);
142 //dbg!(minx, maxx, tri, cross);
143 /* Perform rasterization */
144 for y in miny..=maxy {
145 let mut cx1 = cy1;
146 let mut cx2 = cy2;
147 let mut cx3 = cy3;
148
149 if y < 0 || y >= height as i64 {
150 continue;
151 }
152
153 let buffer = &mut buffer[y as usize * width..];
154
155 for x in minx..=maxx {
156 if x < 0 || x >= width as i64 {
157 continue;
158 }
159 if cx1 > 0 && cx2 > 0 && cx3 > 0 {
160 // cross is equal to 2*area of the triangle.
161 // we can normalize cx by 2*area to get barycentric coords.
162 let area = cross.abs() as f32;
163 let bary = (cx1 as f32 / area, cx2 as f32 / area, cx3 as f32 / area);
164 let coverages = coverage1 * bary.0 + coverage2 * bary.1 + coverage3 * bary.2;
165 let color = (coverages * 255. + 0.5) as u8;
166
167 buffer[x as usize] = color;
168 }
169
170 cx1 -= fdy12;
171 cx2 -= fdy23;
172 cx3 -= fdy31;
173 }
174
175 cy1 += fdx12;
176 cy2 += fdx23;
177 cy3 += fdx31;
178 }
179 }
180
181 pub fn rasterize_to_mask(vertices: &[OutputVertex], width: u32, height: u32) -> Box<[u8]> {
182 let mut mask = vec![0; (width * height) as usize];
183 for n in (0..vertices.len()).step_by(3) {
184 let tri =
185 [&vertices[n], &vertices[n+1], &vertices[n+2]];
186
187 let tri = Triangle { v: [
188 Vertex { x: tri[0].x, y: tri[0].y, coverage: tri[0].coverage},
189 Vertex { x: tri[1].x, y: tri[1].y, coverage: tri[1].coverage},
190 Vertex { x: tri[2].x, y: tri[2].y, coverage: tri[2].coverage}
191 ]
192 };
193 rast_triangle(&mut mask, width as usize, height as usize, &tri);
194 }
195 mask.into_boxed_slice()
196 }