Skip to content

Commit

Permalink
Merge pull request #34 from TannerRogalsky/dependency_cleanup
Browse files Browse the repository at this point in the history
Remove vek dependency.
  • Loading branch information
zesterer authored Dec 31, 2024
2 parents 5fe97f3 + 7e61b74 commit e8f7aee
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 166 deletions.
21 changes: 8 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,32 @@
name = "euc"
version = "0.6.0"
description = "A software rendering crate that lets you write shaders with Rust"
authors = ["Joshua Barretto <[email protected]>", "Martin Sandfuchs <[email protected]>"]
authors = [
"Joshua Barretto <[email protected]>",
"Martin Sandfuchs <[email protected]>",
]
license = "Apache-2.0 AND MIT"
repository = "https://github.com/zesterer/euc"
readme = "README.md"
edition = "2021"
keywords = ["renderer", "3D", "graphics", "rasterizer", "shader"]
exclude = [
"/misc",
"/misc/*",
]
exclude = ["/misc", "/misc/*"]

[dependencies]
vek = { version = "0.17", default-features = false, features = [] }
image = { version = "0.25", optional = true }
fxhash = { version = "0.2", optional = true }
micromath = { version = "2", optional = true }
clipline = "0.2"

[features]
default = ["std", "image", "par"]
std = ["vek/std"]
libm = ["vek/libm"]
nightly = []
simd = ["vek/repr_simd", "vek/platform_intrinsics"]
default = ["image", "par"]
image = ["dep:image"]
par = ["std", "fxhash"]
par = ["fxhash"]
micromath = ["dep:micromath"]

[dev-dependencies]
vek = { version = "0.17", default-features = false, features = ["rgba"] }
minifb = "0.20"
minifb = "0.27"
wavefront = "0.2"
criterion = "0.5"
image = "0.25"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

extern crate alloc;

#[cfg(feature = "std")]
#[cfg(any(feature = "par", not(feature = "micromath")))]
extern crate std;

/// N-dimensional buffers that may be used as textures and render targets.
Expand Down
3 changes: 2 additions & 1 deletion src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
buffer::Buffer2d, math::WeightedSum, primitives::PrimitiveKind, rasterizer::Rasterizer,
texture::Target,
};
use alloc::{collections::VecDeque, vec::Vec};
use alloc::collections::VecDeque;
use core::{borrow::Borrow, cmp::Ordering, ops::Range};

#[cfg(feature = "micromath")]
Expand Down Expand Up @@ -314,6 +314,7 @@ fn render_par<'r, Pipe, S, P, D>(
P: Target<Texel = Pipe::Pixel> + Send + Sync,
D: Target<Texel = f32> + Send + Sync,
{
use alloc::vec::Vec;
use core::sync::atomic::{AtomicUsize, Ordering};
use std::thread;

Expand Down
81 changes: 41 additions & 40 deletions src/rasterizer/lines.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use super::*;
use crate::{CoordinateMode, YAxisDirection};
use vek::*;

#[cfg(feature = "micromath")]
use micromath::F32Ext;

/// A rasterizer that produces filled triangles.
#[derive(Copy, Clone, Debug, Default)]
Expand All @@ -30,54 +26,59 @@ impl Rasterizer for Lines {
let tgt_max = blitter.target_max();

let flip = match coords.y_axis_direction {
YAxisDirection::Down => Vec2::new(1.0, 1.0),
YAxisDirection::Up => Vec2::new(1.0, -1.0),
YAxisDirection::Down => [1.0f32, 1.0],
YAxisDirection::Up => [1.0f32, -1.0],
};

let size = Vec2::<usize>::from(tgt_size).map(|e| e as f32);
let size = tgt_size.map(|e| e as f32);

let verts_hom_out =
core::iter::from_fn(move || Some(Vec2::new(vertices.next()?, vertices.next()?)));
let verts_hom_out = core::iter::from_fn(move || Some([vertices.next()?, vertices.next()?]));

verts_hom_out.for_each(|verts_hom_out: Vec2<([f32; 4], V)>| {
verts_hom_out.for_each(|verts_hom_out: [([f32; 4], V); 2]| {
blitter.begin_primitive();

// Calculate vertex shader outputs and vertex homogeneous coordinates
let verts_hom = Vec2::new(verts_hom_out.x.0, verts_hom_out.y.0).map(Vec4::<f32>::from);
let verts_out = verts_hom_out.map(|e| e.1);
let verts_hom = [verts_hom_out[0].0, verts_hom_out[1].0];
let verts_out = verts_hom_out.map(|(_, v)| v);

let verts_hom = verts_hom.map(|v| v * Vec4::new(flip.x, flip.y, 1.0, 1.0));
let verts_hom = verts_hom.map(|[a0, a1, a2, a3]| [a0 * flip[0], a1 * flip[1], a2, a3]);

// Convert homogenous to euclidean coordinates
let verts_euc = verts_hom.map(|v_hom| v_hom.xyz() / v_hom.w.max(0.0001));
let verts_euc = verts_hom.map(|[a0, a1, a2, a3]| {
let w = a3.max(0.0001);
[a0 / w, a1 / w, a2 / w]
});

// Convert vertex coordinates to screen space
let verts_screen = verts_euc.map(|euc| size * (euc.xy() * Vec2::new(0.5, -0.5) + 0.5));
let verts_screen = verts_euc
.map(|[a0, a1, _a2]| [size[0] * (a0 * 0.5 + 0.5), size[1] * (a1 * -0.5 + 0.5)]);

// Calculate the triangle bounds as a bounding box
let screen_min = Vec2::<usize>::from(tgt_min).map(|e| e as f32);
let screen_max = Vec2::<usize>::from(tgt_max).map(|e| e as f32);
let bounds_clamped = Aabr::<usize> {
min: (verts_screen.reduce(Vec2::partial_min) + 0.0)
.clamped(screen_min, screen_max)
.as_(),
max: (verts_screen.reduce(Vec2::partial_max) + 1.0)
.clamped(screen_min, screen_max)
.as_(),
};

let (x1, y1) = verts_screen.x.as_::<isize>().into_tuple();
let (x2, y2) = verts_screen.y.as_::<isize>().into_tuple();

let (wx1, wy1) = bounds_clamped.min.as_::<isize>().into_tuple();
let (wx2, wy2) = bounds_clamped.max.as_::<isize>().into_tuple();
let screen_min = tgt_min.map(|e| e as f32);
let screen_max = tgt_max.map(|e| e as f32);

let [x1, y1] = [verts_screen[0][0] as isize, verts_screen[0][1] as isize];
let [x2, y2] = [verts_screen[1][0] as isize, verts_screen[1][1] as isize];

let [wx1, wy1] = [
(verts_screen[0][0].min(verts_screen[1][0]) + 0.)
.clamp(screen_min[0], screen_max[0]) as isize,
(verts_screen[0][1].min(verts_screen[1][1]) + 0.)
.clamp(screen_min[1], screen_max[1]) as isize,
];
let [wx2, wy2] = [
(verts_screen[0][0].max(verts_screen[1][0]) + 1.)
.clamp(screen_min[0], screen_max[0]) as isize,
(verts_screen[0][1].max(verts_screen[1][1]) + 1.)
.clamp(screen_min[1], screen_max[1]) as isize,
];

let use_x = (x1 - x2).abs() > (y1 - y2).abs();
let norm = 1.0
/ if use_x {
verts_screen.y.x - verts_screen.x.x
verts_screen[1][0] - verts_screen[0][0]
} else {
verts_screen.y.y - verts_screen.x.y
verts_screen[1][1] - verts_screen[0][1]
};

clipline::clipline(
Expand All @@ -87,25 +88,25 @@ impl Rasterizer for Lines {
let (x, y) = (x as usize, y as usize);

let frac = if use_x {
x as f32 - verts_screen.x.x
x as f32 - verts_screen[0][0]
} else {
y as f32 - verts_screen.x.y
y as f32 - verts_screen[0][1]
} * norm;

// Calculate the interpolated z coordinate for the depth target
let z = Lerp::lerp(verts_euc.x.z, verts_euc.y.z, frac);
let z = verts_euc[0][2] + frac * (verts_euc[1][2] - verts_euc[0][2]);

if coords.passes_z_clip(z) && blitter.test_fragment(x, y, z) {
let get_v_data = |x: f32, y: f32| {
let frac = if use_x {
x - verts_screen.x.x
x - verts_screen[0][0]
} else {
y - verts_screen.x.y
y - verts_screen[0][1]
} * norm;

V::weighted_sum2(
verts_out.x.clone(),
verts_out.y.clone(),
verts_out[0].clone(),
verts_out[1].clone(),
1.0 - frac,
frac,
)
Expand Down
Loading

0 comments on commit e8f7aee

Please sign in to comment.