Skip to content

Commit

Permalink
Optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
zesterer committed Nov 3, 2023
1 parent 09721c8 commit 9ff88fb
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 253 deletions.
9 changes: 6 additions & 3 deletions Cargo.toml
Expand Up @@ -6,7 +6,7 @@ authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>", "Martin Sandfuchs <m
license = "Apache-2.0 AND MIT"
repository = "https://github.com/zesterer/euc"
readme = "README.md"
edition = "2018"
edition = "2021"
keywords = ["renderer", "3D", "graphics", "rasterizer", "shader"]
exclude = [
"/misc",
Expand All @@ -22,7 +22,7 @@ micromath = { version = "1.1", optional = true }
clipline = "0.1.2"

[features]
default = ["std", "image", "par"]
default = ["std", "image"]#, "par"]
std = ["vek/std"]
libm = ["vek/libm"]
nightly = []
Expand All @@ -35,7 +35,7 @@ micromath = ["dep:micromath"]
vek = { version = "0.16", default-features = false, features = ["rgba"] }
minifb = "0.20"
wavefront = "0.2"
criterion = "0.3.3"
criterion = "0.5"
image = "0.24"
derive_more = "0.99"

Expand All @@ -50,3 +50,6 @@ harness = false
# Optimize by default so we don't need to remember to always pass in --release
opt-level = 3
overflow-checks = false

[profile.release]
debug = true
87 changes: 47 additions & 40 deletions benches/teapot.rs
@@ -1,32 +1,38 @@
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use criterion::{black_box, criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
use derive_more::{Add, Mul};
use euc::{
AaMode, Buffer2d, Clamped, CullMode, DepthMode, Empty, Linear, Pipeline, PixelMode, Sampler,
Target, Texture, TriangleList, Unit,
Buffer2d, Clamped, CullMode, DepthMode, Empty, Linear, Pipeline, PixelMode, Sampler, Target,
Texture, TriangleList, Unit,
};
use minifb::{Key, MouseButton, MouseMode, Window, WindowOptions};
use std::{marker::PhantomData, time::Duration};
use std::time::Duration;
use vek::*;

struct TeapotShadow<'a> {
struct TeapotShadow {
mvp: Mat4<f32>,
phantom: PhantomData<&'a ()>,
}

impl<'a> Pipeline for TeapotShadow<'a> {
type Vertex = wavefront::Vertex<'a>;
impl<'r> Pipeline<'r> for TeapotShadow {
type Vertex = wavefront::Vertex<'r>;
type VertexData = f32;
type Primitives = TriangleList;
type Fragment = Unit;
type Pixel = ();

#[inline(always)]
fn pixel_mode(&self) -> PixelMode {
PixelMode::PASS
}

#[inline(always)]
fn depth_mode(&self) -> DepthMode {
DepthMode::LESS_WRITE
}

#[inline(always)]
fn rasterizer_config(&self) -> CullMode {
CullMode::None
}

#[inline(always)]
fn vertex(&self, vertex: &Self::Vertex) -> ([f32; 4], Self::VertexData) {
(
Expand All @@ -41,16 +47,17 @@ impl<'a> Pipeline for TeapotShadow<'a> {
}

#[inline(always)]
fn blend(&self, old: Self::Pixel, new: Self::Fragment) {}
fn blend(&self, _old: Self::Pixel, _new: Self::Fragment) {}
}

struct Teapot<'a> {
struct Teapot<'r> {
m: Mat4<f32>,
v: Mat4<f32>,
p: Mat4<f32>,
light_pos: Vec3<f32>,
shadow: Clamped<Linear<&'a Buffer2d<f32>>>,
shadow: Clamped<Linear<&'r Buffer2d<f32>>>,
light_vp: Mat4<f32>,
cam_pos: Vec3<f32>,
}

#[derive(Add, Mul, Clone)]
Expand All @@ -60,19 +67,17 @@ struct VertexData {
light_view_pos: Vec3<f32>,
}

impl<'a> Pipeline for Teapot<'a> {
type Vertex = wavefront::Vertex<'a>;
impl<'r> Pipeline<'r> for Teapot<'r> {
type Vertex = wavefront::Vertex<'r>;
type VertexData = VertexData;
type Primitives = TriangleList;
type Fragment = Rgba<f32>;
type Pixel = u32;

#[inline(always)]
fn depth_mode(&self) -> DepthMode {
DepthMode::LESS_WRITE
}
fn aa_mode(&self) -> AaMode {
AaMode::Msaa { level: 1 }
}

#[inline(always)]
fn vertex(&self, vertex: &Self::Vertex) -> ([f32; 4], Self::VertexData) {
Expand All @@ -82,7 +87,7 @@ impl<'a> Pipeline for Teapot<'a> {
let light_view_pos = self.light_vp * Vec4::from_point(wpos);
let light_view_pos = light_view_pos.xyz() / light_view_pos.w;
(
(self.p * self.v * wpos).into_array(),
(self.p * (self.v * wpos)).into_array(),
VertexData {
wpos: wpos.xyz(),
wnorm: wnorm.xyz(),
Expand All @@ -101,8 +106,7 @@ impl<'a> Pipeline for Teapot<'a> {
}: Self::VertexData,
) -> Self::Fragment {
let wnorm = wnorm.normalized();
let cam_pos = Vec3::zero();
let cam_dir = (wpos - cam_pos).normalized();
let cam_dir = (self.cam_pos - wpos).normalized();
let light_dir = (wpos - self.light_pos).normalized();
let surf_color = Rgba::new(1.0, 0.8, 0.7, 1.0);

Expand Down Expand Up @@ -130,7 +134,7 @@ impl<'a> Pipeline for Teapot<'a> {

#[inline(always)]
fn blend(&self, _old: Self::Pixel, rgba: Self::Fragment) -> Self::Pixel {
let rgba = rgba.map(|e| e.clamped(0.0, 1.0) * 255.0).as_();
let rgba = rgba.map(|e| e.max(0.0).min(1.0) * 255.0).as_();
// The window's framebuffer uses BGRA format
let bgra = Rgba::new(rgba.b, rgba.g, rgba.r, rgba.a);
u32::from_le_bytes(bgra.into_array())
Expand All @@ -147,8 +151,8 @@ fn teapot_benchmark(b: &mut Bencher, &[width, height]: &[usize; 2]) {
let model =
wavefront::Obj::from_reader(&include_bytes!("../examples/data/teapot.obj")[..]).unwrap();

let mut ori = Vec2::new(0.0, 0.0);
let mut dist = 6.0;
let ori = Vec2::new(-0.55, -0.25);
let dist = 4.5;

// Position of objects in the scene
let teapot_pos = Vec3::new(0.0, 0.0, 0.0);
Expand Down Expand Up @@ -181,11 +185,11 @@ fn teapot_benchmark(b: &mut Bencher, &[width, height]: &[usize; 2]) {
shadow.clear(1.0);

// Shadow pass
TeapotShadow {
mvp: light_vp * m,
phantom: PhantomData,
}
.render(model.vertices(), &mut Empty::default(), &mut shadow);
TeapotShadow { mvp: light_vp * m }.render(
model.vertices(),
&mut Empty::default(),
&mut shadow,
);

// Colour pass
Teapot {
Expand All @@ -195,6 +199,7 @@ fn teapot_benchmark(b: &mut Bencher, &[width, height]: &[usize; 2]) {
light_pos,
shadow: (&shadow).linear().clamped(),
light_vp: light_vp,
cam_pos: v.inverted().mul_point(Vec3::zero()),
}
.render(model.vertices(), &mut color, &mut depth);

Expand All @@ -204,18 +209,20 @@ fn teapot_benchmark(b: &mut Bencher, &[width, height]: &[usize; 2]) {
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function_over_inputs(
"teapot",
|b, &size| teapot_benchmark(b, size),
&[
[1, 1],
[32, 32],
[640, 480],
[1024, 800],
[2048, 1600],
[4096, 3200],
],
);
for size in [
[1, 1],
[32, 32],
[640, 480],
[1024, 800],
[2048, 1600],
[4096, 3200],
] {
c.bench_with_input(
BenchmarkId::new("teapot", format!("{size:?}")),
&size,
|b, size| teapot_benchmark(b, size),
);
}
}

criterion_group! {
Expand Down
2 changes: 1 addition & 1 deletion examples/teapot.rs
Expand Up @@ -133,7 +133,7 @@ impl<'r> Pipeline<'r> for Teapot<'r> {

#[inline(always)]
fn blend(&self, _old: Self::Pixel, rgba: Self::Fragment) -> Self::Pixel {
let rgba = rgba.map(|e| e.clamped(0.0, 1.0) * 255.0).as_();
let rgba = rgba.map(|e| e.max(0.0).min(1.0) * 255.0).as_();
// The window's framebuffer uses BGRA format
let bgra = Rgba::new(rgba.b, rgba.g, rgba.r, rgba.a);
u32::from_le_bytes(bgra.into_array())
Expand Down

0 comments on commit 9ff88fb

Please sign in to comment.