Skip to content
Open

Misc #344

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# \___\/ ==\______\/\_____\__\/ ==\______/_____,´ /==\_____\___\/==\_______\/
# \_____\,´

# On nightly only
#cargo-features = ["panic-immediate-abort"]

[package]
name = "retrofire"
description = "90s style software 3D renderer and graphics tools."
Expand Down Expand Up @@ -67,12 +70,21 @@ debug = 1
[profile.bench]
opt-level = 3
codegen-units = 1
lto = "thin"
lto = "fat"

[profile.dev]
opt-level = 1
split-debuginfo = "unpacked"

[profile.min]
inherits = "release"
opt-level = 3
codegen-units = 1
lto = "fat"
debug = 0
strip = true
panic = "abort"
#panic = "immediate-abort" # On nightly only

[[bench]]
name = "fill"
Expand Down
21 changes: 12 additions & 9 deletions benches/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use retrofire_core::{
//#[global_allocator]
//static ALLOC: AllocProfiler = AllocProfiler::system();

#[divan::bench(args = [1, 10, 100, 1000, 10_000])]
#[divan::bench(args = [1, 10, 100, 1000, 10_000], min_time = 1)]
fn clip_mixed(b: Bencher, n: usize) {
let rng = &mut DefaultRng::default();
let pts = pt3(-10.0, -10.0, -10.0)..pt3(10.0, 10.0, 10.0);
let proj = orthographic(pt3(-1.0, -1.0, -1.0), pt3(1.0, 1.0, 1.0));

let mut out = Vec::with_capacity(n);
b.with_inputs(|| {
repeat_with(|| {
let vs = array::from_fn(|_| {
Expand All @@ -32,18 +33,19 @@ fn clip_mixed(b: Bencher, n: usize) {
})
.input_counter(|tris| ItemsCount::of_iter(tris))
.bench_local_values(|tris| {
let mut out = Vec::new();
out.clear();
view_frustum::clip(tris.as_slice(), &mut out);
out
out.len()
})
}

#[divan::bench(args = [1, 10, 100, 1000, 10_000])]
#[divan::bench(args = [1, 10, 100, 1000, 10_000], min_time = 1)]
fn clip_all_inside(b: Bencher, n: usize) {
let rng = &mut DefaultRng::default();
let pts = pt3(-1.0, -1.0, -1.0)..pt3(1.0, 1.0, 1.0);
let proj = orthographic(pt3(-1.0, -1.0, -1.0), pt3(1.0, 1.0, 1.0));

let mut out = Vec::with_capacity(n);
b.with_inputs(|| {
repeat_with(|| {
let vs = array::from_fn(|_| {
Expand All @@ -56,18 +58,19 @@ fn clip_all_inside(b: Bencher, n: usize) {
})
.input_counter(|tris| ItemsCount::of_iter(tris))
.bench_local_values(|tris| {
let mut out = Vec::new();
out.clear();
view_frustum::clip(tris.as_slice(), &mut out);
out
out.len()
})
}

#[divan::bench(args = [1, 10, 100, 1000, 10_000])]
#[divan::bench(args = [1, 10, 100, 1000, 10_000], min_time = 1)]
fn clip_all_outside(b: Bencher, n: usize) {
let mut rng = DEFAULT_RNG;
let pts = pt3(2.0, -10.0, -10.0)..pt3(10.0, 10.0, 10.0);
let proj = orthographic(pt3(-1.0, -1.0, -1.0), pt3(1.0, 1.0, 1.0));

let mut out = Vec::with_capacity(n);
b.with_inputs(|| {
repeat_with(|| {
let vs = ([pts.start; 3]..[pts.end; 3])
Expand All @@ -80,9 +83,9 @@ fn clip_all_outside(b: Bencher, n: usize) {
})
.input_counter(|tris| ItemsCount::of_iter(tris))
.bench_local_values(|tris| {
let mut out = Vec::with_capacity(tris.len());
out.clear();
view_frustum::clip(tris.as_slice(), &mut out);
out
out.len()
})
}

Expand Down
6 changes: 2 additions & 4 deletions benches/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ fn gouraud(b: Bencher, sz: f32) {
});
});

let buf = Buf2::new_from(
(1024, 1024),
buf.data().into_iter().map(|c| c.to_color3()),
);
let buf =
Buf2::new_from((1024, 1024), buf.data().iter().map(|c| c.to_color3()));
save_ppm("benches_fill_color.ppm", buf).unwrap();
}

Expand Down
3 changes: 2 additions & 1 deletion core/examples/hello_tri.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use retrofire_core::prelude::*;
use retrofire_core::render::{Model, render, shader};
use retrofire_core::{prelude::*, util::*};

fn main() {
let verts = [
Expand Down Expand Up @@ -53,6 +53,7 @@ fn main() {
}
#[cfg(feature = "std")]
{
use retrofire_core::util::pnm;
pnm::save_ppm("triangle.ppm", framebuf).unwrap();
}
}
7 changes: 6 additions & 1 deletion core/src/geom/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl<A, B> Mesh<A, B> {
}

/// Returns a mesh with the faces and vertices of both `self` and `other`.
#[must_use]
pub fn merge(mut self, Self { faces, verts }: Self) -> Self {
let n = self.verts.len();
self.verts.extend(verts);
Expand All @@ -107,7 +108,7 @@ fn assert_indices_in_bounds(faces: &[Tri<usize>], len: usize) {
assert!(
vs.iter().all(|&j| j < len),
"vertex index out of bounds at faces[{i}]: {vs:?}"
)
);
}
}

Expand Down Expand Up @@ -164,6 +165,7 @@ impl<A> Builder<A> {
///
/// # Panics
/// If any of the vertex indices in `faces` ≥ `verts.len()`.
#[must_use]
pub fn build(self) -> Mesh<A> {
// Sanity checks done by new()
Mesh::new(self.mesh.faces, self.mesh.verts)
Expand All @@ -175,6 +177,7 @@ impl<A> Builder<A> {
///
/// This is an eager operation, that is, only vertices *currently*
/// added to the builder are transformed.
#[must_use]
pub fn transform(self, tf: &Mat4<Model, Model>) -> Self {
self.warp(|v| vertex(tf.apply(&v.pos), v.attrib))
}
Expand All @@ -184,6 +187,7 @@ impl<A> Builder<A> {
/// This method can be used for various nonlinear transformations such as
/// twisting or dilation. This is an eager operation, that is, only vertices
/// *currently* added to the builder are transformed.
#[must_use]
pub fn warp(mut self, f: impl FnMut(Vertex3<A>) -> Vertex3<A>) -> Self {
self.mesh.verts = self.mesh.verts.into_iter().map(f).collect();
self
Expand All @@ -202,6 +206,7 @@ impl<A> Builder<A> {
/// This is an eager operation, that is, only vertices *currently* added
/// to the builder are transformed. The attribute type of the result is
/// `Normal3`; the vertex type it accepts is changed accordingly.
#[must_use]
pub fn with_vertex_normals(self) -> Builder<Normal3> {
let Mesh { verts, faces } = self.mesh;

Expand Down
17 changes: 9 additions & 8 deletions core/src/geom/prim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ impl<B> Plane3<B> {
///
/// assert_eq!(<Plane3>::new(0.0, 0.0, 1.0, 2.0).project(pt), pt3(1.0, 2.0, 2.0));
/// ```
#[must_use]
pub fn project(&self, pt: Point3<B>) -> Point3<B> {
// The vector that projects pt on the plane is parallel with the plane
// normal and its length is the distance of pt from the plane.
Expand Down Expand Up @@ -672,9 +673,9 @@ impl<B> Line2<B> {
///
/// # Panics
/// If the vector (a, b) is not unit-length.
pub fn new(a: f32, b: f32, c: f32) -> Self {
pub const fn new(a: f32, b: f32, c: f32) -> Self {
// TODO This method can't itself normalize because const
assert!((a * a + b * b - 1.0).abs() < 1e-6, "non-unit normal");
assert!((a * a + b * b - 1.0).abs() < 1e-5, "non-unit normal",);
Self(Vector::new([a, b, -c]))
}

Expand Down Expand Up @@ -720,7 +721,7 @@ impl<B> Line2<B> {

/// Returns the coefficients [a, b, c] of the line equation ax + by = c.
pub const fn coeffs(&self) -> [f32; 3] {
return self.0.0;
self.0.0
}
}

Expand Down Expand Up @@ -1071,21 +1072,21 @@ mod tests {
let mut l: Line2;

l = Line2::new(1.0, 0.0, 0.0); // x = 0
assert_eq!(format!("{:?}", l), "Line(x = 0)");
assert_eq!(format!("{l:?}"), "Line(x = 0)");

l = Line2::from_points(pt2(2.0, 0.0), pt2(2.0, -1.0));
assert_eq!(l.coeffs(), [1.0, 0.0, -2.0]);
assert_eq!(format!("{:?}", l), "Line(x = 2)");
assert_eq!(format!("{l:?}"), "Line(x = 2)");

l = Line2::new(1.0, 0.0, 2.0); // x = 2
assert_eq!(format!("{:?}", l), "Line(x = 2)");
assert_eq!(format!("{l:?}"), "Line(x = 2)");

l = Line2::new(0.0, 1.0, 0.0); // y = 0
assert_eq!(format!("{:?}", l), "Line(y = 0)");
assert_eq!(format!("{l:?}"), "Line(y = 0)");

l = Line2::from_points(pt2(0.0, -3.0), pt2(1.0, -3.0)); // y = -3
assert_eq!(l.slope_intercept(), Some((0.0, -3.0)));
assert_eq!(format!("{:?}", l), "Line(y = -3)");
assert_eq!(format!("{l:?}"), "Line(y = -3)");

l = Line2::new(0.0, 1.0, -3.0); // y = -3
assert_eq!(format!("{:?}", l), "Line(y = -3)");
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
//! # Example
//!
//! ```
#![allow(clippy::needless_doctest_main)]
#![doc = include_str!("../examples/hello_tri.rs")]
//! ```

Expand Down
2 changes: 1 addition & 1 deletion core/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Linear algebra and other useful mathematics.
//!
//! Includes [vectors][self::vec], [matrices][mat], [colors][color],
//! [angles][angle], [Bezier splines][spline] and [pseudo-random numbers][rand],
//! [angles][angle], [splines][spline] and [pseudo-random numbers][rand],
//! as well as support for custom [varying][vary] types and utilities such as
//! approximate equality comparisons.
//!
Expand Down
20 changes: 14 additions & 6 deletions core/src/math/angle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub const fn turns(a: f32) -> Angle {
/// # Panics
/// If `x` is outside the range [-1.0, 1.0].
#[cfg(feature = "fp")]
#[inline]
pub fn asin(x: f32) -> Angle {
assert!(-1.0 <= x && x <= 1.0);
Angle(f32::asin(x))
Expand All @@ -96,6 +97,7 @@ pub fn asin(x: f32) -> Angle {
/// # Panics
/// If `x` is outside the range [-1.0, 1.0].
#[cfg(feature = "fp")]
#[inline]
pub fn acos(x: f32) -> Angle {
Angle(f32::acos(x))
}
Expand All @@ -113,11 +115,13 @@ pub fn acos(x: f32) -> Angle {
/// assert_eq!(atan2(-3.0, 0.0), degs(-90.0));
/// ```
#[cfg(feature = "fp")]
#[inline]
pub fn atan2(y: f32, x: f32) -> Angle {
Angle(f32::atan2(y, x))
}

/// Returns a polar coordinate vector with azimuth `az` and radius `r`.
#[inline]
pub const fn polar<B>(r: f32, az: Angle) -> PolarVec<B> {
Vector::new([r, az.to_rads()])
}
Expand All @@ -126,6 +130,7 @@ pub const fn polar<B>(r: f32, az: Angle) -> PolarVec<B> {
/// altitude `alt`, and radius `r`.
///
/// An altitude of +90° corresponds to straight up and -90° to straight down.
#[inline]
pub const fn spherical<B>(r: f32, az: Angle, alt: Angle) -> SphericalVec<B> {
Vector::new([r, az.to_rads(), alt.to_rads()])
}
Expand Down Expand Up @@ -183,11 +188,13 @@ impl Angle {

/// Returns the minimum of `self` and `other`.
#[inline]
#[must_use]
pub const fn min(self, other: Self) -> Self {
Self(self.0.min(other.0))
}
/// Returns the maximum of `self` and `other`.
#[inline]
#[must_use]
pub const fn max(self, other: Self) -> Self {
Self(self.0.max(other.0))
}
Expand Down Expand Up @@ -413,6 +420,7 @@ impl<B> Vec2<B> {
/// // A negative x and zero y maps to straight angle azimuth
/// assert_approx_eq!(vec2(-1.0, 0.0).to_polar().az(), degs(180.0));
/// ```
#[inline]
pub fn to_polar(&self) -> PolarVec<B> {
polar(self.len(), self.atan())
}
Expand All @@ -426,8 +434,8 @@ impl<B> Vec3<B> {
/// * `r` equals `self.len()`
/// * `az`is the angle between `self` and the xy-plane in the range
/// (-180°, 180°] such that positive `z` maps to *negative* `az`, and
/// * `alt` is the angle between `self` and the xz-plane in the
/// range [-90°, 90°] such that positive `y` maps to positive `alt`.
/// * `alt` is the angle between `self` and the xz-plane in the range
/// [-90°, 90°] such that positive `y` maps to positive `alt`.
///
/// # Examples
/// ```
Expand Down Expand Up @@ -631,7 +639,7 @@ impl DivAssign<f32> for Angle {
impl<B> From<PolarVec<B>> for Vec2<B> {
/// Converts a polar vector into the equivalent Cartesian vector.
///
/// See [PolarVec::to_cart] for more information.
/// See [`PolarVec::to_cart`] for more information.
#[inline]
fn from(p: PolarVec<B>) -> Self {
p.to_cart()
Expand All @@ -642,7 +650,7 @@ impl<B> From<PolarVec<B>> for Vec2<B> {
impl<B> From<Vec2<B>> for PolarVec<B> {
/// Converts a Cartesian 2-vector into the equivalent polar vector.
///
/// See [Vec2::to_polar] for more information.
/// See [`Vec2::to_polar`] for more information.
#[inline]
fn from(v: Vec2<B>) -> Self {
v.to_polar()
Expand All @@ -653,7 +661,7 @@ impl<B> From<Vec2<B>> for PolarVec<B> {
impl<B> From<SphericalVec<B>> for Vec3<B> {
/// Converts a spherical coordinate vector to a Euclidean 3-vector.
///
/// See [SphericalVec::to_cart] for more information.
/// See [`SphericalVec::to_cart`] for more information.
#[inline]
fn from(v: SphericalVec<B>) -> Self {
v.to_cart()
Expand All @@ -664,7 +672,7 @@ impl<B> From<SphericalVec<B>> for Vec3<B> {
impl<B> From<Vec3<B>> for SphericalVec<B> {
/// Converts a Cartesian 3-vector into the equivalent spherical vector.
///
/// See [Vec3::to_spherical] for more information.
/// See [`Vec3::to_spherical`] for more information.
#[inline]
fn from(v: Vec3<B>) -> Self {
v.to_spherical()
Expand Down
Loading
Loading