1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/* The rasterization code here is based off of piglit/tests/general/triangle-rasterization.cpp:
/**************************************************************************
*
* Copyright 2012 VMware, Inc.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
*/
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use euclid::{default::Transform2D, point2};
use wpf_gpu_raster::{PathBuilder};
use std::ops::Index;
const WIDTH: u32 = 800;
const HEIGHT: u32 = 800;
fn over(src: u32, dst: u32) -> u32 {
let a = src >> 24;
let a = 255 - a;
let mask = 0xff00ff;
let t = (dst & mask) * a + 0x800080;
let mut rb = (t + ((t >> 8) & mask)) >> 8;
rb &= mask;
rb += src & mask;
// saturate
rb |= 0x1000100 - ((rb >> 8) & mask);
rb &= mask;
let t = ((dst >> 8) & mask) * a + 0x800080;
let mut ag = (t + ((t >> 8) & mask)) >> 8;
ag &= mask;
ag += (src >> 8) & mask;
// saturate
ag |= 0x1000100 - ((ag >> 8) & mask);
ag &= mask;
(ag << 8) + rb
}
pub fn alpha_mul(x: u32, a: u32) -> u32 {
let mask = 0xFF00FF;
let src_rb = ((x & mask) * a) >> 8;
let src_ag = ((x >> 8) & mask) * a;
(src_rb & mask) | (src_ag & !mask)
}
fn write_image(data: &[u32], path: &str) {
use std::path::Path;
use std::fs::File;
use std::io::BufWriter;
let mut png_data: Vec<u8> = vec![0; (WIDTH * HEIGHT * 3) as usize];
let mut i = 0;
for pixel in data {
png_data[i] = ((pixel >> 16) & 0xff) as u8;
png_data[i + 1] = ((pixel >> 8) & 0xff) as u8;
png_data[i + 2] = ((pixel >> 0) & 0xff) as u8;
i += 3;
}
let path = Path::new(path);
let file = File::create(path).unwrap();
let w = &mut BufWriter::new(file);
let mut encoder = png::Encoder::new(w, WIDTH, HEIGHT); // Width is 2 pixels and height is 1.
encoder.set_color(png::ColorType::Rgb);
encoder.set_depth(png::BitDepth::Eight);
let mut writer = encoder.write_header().unwrap();
writer.write_image_data(&png_data).unwrap(); // Save
}
#[derive(Debug)]
struct Vertex {
x: f32,
y: f32,
coverage: f32
}
#[derive(Debug)]
struct Triangle {
v: [Vertex; 3],
}
impl Index<usize> for Triangle {
type Output = Vertex;
fn index(&self, index: usize) -> &Self::Output {
&self.v[index]
}
}
// D3D11 mandates 8 bit subpixel precision:
// https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm#CoordinateSnapping
const FIXED_SHIFT: i32 = 8;
const FIXED_ONE: f32 = (1 << FIXED_SHIFT) as f32;
/* Proper rounding of float to integer */
fn iround(mut v: f32) -> i64 {
if v > 0.0 {
v += 0.5;
}
if v < 0.0 {
v -= 0.5;
}
return v as i64
}
/* Based on http://devmaster.net/forums/topic/1145-advanced-rasterization */
fn rast_triangle(buffer: &mut [u32], stride: usize, tri: &Triangle, color: u32) {
let center_offset = -0.5;
let mut coverage1 = tri[0].coverage;
let mut coverage2 = tri[1].coverage;
let mut coverage3 = tri[2].coverage;
/* fixed point coordinates */
let mut x1 = iround(FIXED_ONE * (tri[0].x + center_offset));
let x2 = iround(FIXED_ONE * (tri[1].x + center_offset));
let mut x3 = iround(FIXED_ONE * (tri[2].x + center_offset));
let mut y1 = iround(FIXED_ONE * (tri[0].y + center_offset));
let y2 = iround(FIXED_ONE * (tri[1].y + center_offset));
let mut y3 = iround(FIXED_ONE * (tri[2].y + center_offset));
/* Force correct vertex order */
let cross = (x2 - x1) * (y3 - y2) - (y2 - y1) * (x3 - x2);
if cross > 0 {
std::mem::swap(&mut x1, &mut x3);
std::mem::swap(&mut y1, &mut y3);
// I don't understand why coverage 2 and 3 are swapped instead of 1 and 3
std::mem::swap(&mut coverage2, &mut coverage3);
} else {
std::mem::swap(&mut coverage1, &mut coverage3);
}
/* Deltas */
let dx12 = x1 - x2;
let dx23 = x2 - x3;
let dx31 = x3 - x1;
let dy12 = y1 - y2;
let dy23 = y2 - y3;
let dy31 = y3 - y1;
/* Fixed-point deltas */
let fdx12 = dx12 << FIXED_SHIFT;
let fdx23 = dx23 << FIXED_SHIFT;
let fdx31 = dx31 << FIXED_SHIFT;
let fdy12 = dy12 << FIXED_SHIFT;
let fdy23 = dy23 << FIXED_SHIFT;
let fdy31 = dy31 << FIXED_SHIFT;
/* Bounding rectangle */
let mut minx = x1.min(x2).min(x3) >> FIXED_SHIFT;
let mut maxx = x1.max(x2).max(x3) >> FIXED_SHIFT;
let mut miny = y1.min(y2).min(y3) >> FIXED_SHIFT;
let mut maxy = y1.max(y2).max(y3) >> FIXED_SHIFT;
minx = minx.max(0);
maxx = maxx.min(WIDTH as i64 - 1);
miny = miny.max(0);
maxy = maxy.min(HEIGHT as i64 - 1);
/* Half-edge constants */
let mut c1 = dy12 * x1 - dx12 * y1;
let mut c2 = dy23 * x2 - dx23 * y2;
let mut c3 = dy31 * x3 - dx31 * y3;
/* Correct for top-left filling convention */
if dy12 < 0 || (dy12 == 0 && dx12 < 0) { c1 += 1 }
if dy23 < 0 || (dy23 == 0 && dx23 < 0) { c2 += 1 }
if dy31 < 0 || (dy31 == 0 && dx31 < 0) { c3 += 1 }
let mut cy1 = c1 + dx12 * (miny << FIXED_SHIFT) - dy12 * (minx << FIXED_SHIFT);
let mut cy2 = c2 + dx23 * (miny << FIXED_SHIFT) - dy23 * (minx << FIXED_SHIFT);
let mut cy3 = c3 + dx31 * (miny << FIXED_SHIFT) - dy31 * (minx << FIXED_SHIFT);
/* Perform rasterization */
let mut buffer = &mut buffer[miny as usize * stride..];
for _y in miny..=maxy {
let mut cx1 = cy1;
let mut cx2 = cy2;
let mut cx3 = cy3;
for x in minx..=maxx {
if cx1 > 0 && cx2 > 0 && cx3 > 0 {
// cross is equal to 2*area of the triangle.
// we can normalize cx by 2*area to get barycentric coords.
let area = cross.abs() as f32;
let bary = (cx1 as f32 / area, cx2 as f32/ area, cx3 as f32 / area);
let coverages = coverage1 * bary.0 + coverage2 * bary.1 + coverage3 * bary.2;
let color = alpha_mul(color, (coverages * 256. + 0.5) as u32);
buffer[x as usize] = over(color, buffer[x as usize]);
}
cx1 -= fdy12;
cx2 -= fdy23;
cx3 -= fdy31;
}
cy1 += fdx12;
cy2 += fdx23;
cy3 += fdx31;
buffer = &mut buffer[stride..];
}
}
fn main() {
let opt = usvg::Options::default();
let rtree = usvg::Tree::from_file("tiger.svg", &opt).unwrap();
let mut image = vec![0; (WIDTH * HEIGHT) as usize];
for _ in 0..1 {
let mut total_vertex_count = 0;
let mut total_time = std::time::Duration::default();
for node in rtree.root().descendants() {
use usvg::NodeExt;
let t = node.transform();
let transform = Transform2D::new(
t.a as f32, t.b as f32,
t.c as f32, t.d as f32,
t.e as f32, t.f as f32,
);
let s = 1.;
if let usvg::NodeKind::Path(ref usvg_path) = *node.borrow() {
let color = match usvg_path.fill {
Some(ref fill) => {
match fill.paint {
usvg::Paint::Color(c) => 0xff000000 | (c.red as u32) << 16 | (c.green as u32) << 8 | c.blue as u32,
_ => 0xff00ff00,
}
}
None => {
continue;
}
};
let mut builder = PathBuilder::new();
//dbg!(&usvg_path.segments);
for segment in &usvg_path.segments {
match *segment {
usvg::PathSegment::MoveTo { x, y } => {
let p = transform.transform_point(point2(x as f32, y as f32)) * s;
builder.move_to(p.x, p.y);
}
usvg::PathSegment::LineTo { x, y } => {
let p = transform.transform_point(point2(x as f32, y as f32)) * s;
builder.line_to(p.x, p.y);
}
usvg::PathSegment::CurveTo { x1, y1, x2, y2, x, y, } => {
let c1 = transform.transform_point(point2(x1 as f32, y1 as f32)) * s;
let c2 = transform.transform_point(point2(x2 as f32, y2 as f32)) * s;
let p = transform.transform_point(point2(x as f32, y as f32)) * s;
builder.curve_to(
c1.x, c1.y,
c2.x, c2.y,
p.x, p.y,
);
}
usvg::PathSegment::ClosePath => {
builder.close();
}
}
}
let start = std::time::Instant::now();
let result = builder.rasterize_to_tri_list(0, 0, WIDTH as i32, HEIGHT as i32);
let end = std::time::Instant::now();
total_time += end - start;
println!("vertices {}", result.len());
total_vertex_count += result.len();
if result.len() == 0 {
continue;
}
for n in (0..result.len()).step_by(3) {
let vertices = {
[&result[n], &result[n+1], &result[n+2]]
};
let src = color;
let tri = Triangle { v: [
Vertex { x: vertices[0].x, y: vertices[0].y, coverage: vertices[0].coverage},
Vertex { x: vertices[1].x, y: vertices[1].y, coverage: vertices[1].coverage},
Vertex { x: vertices[2].x, y: vertices[2].y, coverage: vertices[2].coverage}
]
};
rast_triangle(&mut image, WIDTH as usize, &tri, src);
}
}
}
println!("total vertex count {}, took {}ms", total_vertex_count, total_time.as_secs_f32()*1000.);
}
write_image(&image, "out.png");
use std::{hash::{Hash, Hasher}, collections::hash_map::DefaultHasher};
use crate::*;
fn calculate_hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
t.hash(&mut s);
s.finish()
}
assert_eq!(calculate_hash(&image),
if cfg!(debug_assertions) { 0x5973c52a1c0232f3 } else { 0xf15821a5bebc5ecf});
}
|