Compare commits

..

3 commits

Author SHA1 Message Date
Joe Ardent
d7e87bdb1e float-y line function 2025-09-05 16:51:48 -07:00
Joe Ardent
c3f2ae9a56 first benchmark, 1.6 seconds 2025-09-05 16:01:17 -07:00
Joe Ardent
635585dd09 line drawing works well, room for optimization 2025-09-05 15:43:03 -07:00
4 changed files with 162 additions and 18 deletions

104
Cargo.lock generated
View file

@ -2,6 +2,24 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "cfg-if"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]] [[package]]
name = "justerror" name = "justerror"
version = "1.1.0" version = "1.1.0"
@ -13,6 +31,21 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "libc"
version = "0.2.175"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.101" version = "1.0.101"
@ -31,6 +64,41 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"
@ -78,6 +146,7 @@ name = "tinyrender"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"justerror", "justerror",
"rand",
"thiserror", "thiserror",
] ]
@ -86,3 +155,38 @@ name = "unicode-ident"
version = "1.0.18" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "wasi"
version = "0.14.3+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a51ae83037bdd272a9e28ce236db8c07016dd0d50c27038b3f407533c030c95"
dependencies = [
"wit-bindgen",
]
[[package]]
name = "wit-bindgen"
version = "0.45.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c573471f125075647d03df72e026074b7203790d41351cd6edc96f46bcccd36"
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.106",
]

View file

@ -5,4 +5,5 @@ edition = "2024"
[dependencies] [dependencies]
justerror = "1.1.0" justerror = "1.1.0"
rand = "0.9.2"
thiserror = "2.0.16" thiserror = "2.0.16"

View file

@ -2,6 +2,7 @@
extern crate justerror; extern crate justerror;
mod tga; mod tga;
use rand::Rng;
use tga::*; use tga::*;
mod point; mod point;
@ -32,6 +33,15 @@ fn main() {
let h = 64; let h = 64;
let mut framebuffer = TGAImage::new(w, h, TGAFormat::RGB); let mut framebuffer = TGAImage::new(w, h, TGAFormat::RGB);
//bench(&mut framebuffer);
baseline(&mut framebuffer);
framebuffer
.write_file("framebuffer.tga", true, true)
.unwrap();
}
fn baseline(framebuffer: &mut TGAImage) {
let ax = 7; let ax = 7;
let ay = 3; let ay = 3;
let bx = 12; let bx = 12;
@ -47,23 +57,52 @@ fn main() {
let b = Point::new(bx, by); let b = Point::new(bx, by);
let c = Point::new(cx, cy); let c = Point::new(cx, cy);
line(a, b, &mut framebuffer, BLUE); line(a, b, framebuffer, BLUE);
line(c, b, &mut framebuffer, GREEN); line(c, b, framebuffer, GREEN);
line(c, a, &mut framebuffer, YELLOW); line(c, a, framebuffer, YELLOW);
line(a, c, &mut framebuffer, RED); line(a, c, framebuffer, RED);
framebuffer
.write_file("framebuffer.tga", true, true)
.unwrap();
} }
fn line(a: Point, b: Point, fb: &mut TGAImage, color: TGAColor) { fn bench(fb: &mut TGAImage) {
let mut t = 0.0; let mut rng = rand::rng();
let step = 0.02;
while t < 1.0 { for _ in 0..1 << 24 {
let x = (a.x as f32 + (b.x - a.x) as f32 * t).round_ties_even() as u32; let ax = rng.random_range(0..fb.width);
let y = (a.y as f32 + (b.y - a.y) as f32 * t).round_ties_even() as u32; let ay = rng.random_range(0..fb.height);
fb.set(x, y, color); let a = Point::new(ax, ay);
t += step;
let bx = rng.random_range(0..fb.width);
let by = rng.random_range(0..fb.height);
let b = Point::new(bx, by);
line(
a,
b,
fb,
[rng.random(), rng.random(), rng.random(), rng.random()].into(),
);
}
}
fn line(mut a: Point, mut b: Point, fb: &mut TGAImage, color: TGAColor) {
let is_steep = (a.x - b.x).abs() < (a.y - b.y).abs();
if is_steep {
std::mem::swap(&mut a.x, &mut a.y);
std::mem::swap(&mut b.x, &mut b.y);
}
if a.x > b.x {
std::mem::swap(&mut a.x, &mut b.x);
std::mem::swap(&mut a.y, &mut b.y);
}
let mut y = a.y as f32;
let step = (b.y - a.y) as f32 / (b.x - a.x) as f32;
for x in (a.x)..b.x {
if is_steep {
fb.set(y as u32, x as u32, color);
} else {
fb.set(x as u32, y as u32, color);
}
y += step;
} }
} }

View file

@ -31,8 +31,8 @@ impl Sub for Point {
fn sub(self, rhs: Self) -> Self::Output { fn sub(self, rhs: Self) -> Self::Output {
Self { Self {
x: self.x.saturating_sub(rhs.x), x: self.x - rhs.x,
y: self.y.saturating_sub(rhs.y), y: self.y - rhs.y,
} }
} }
} }