Skip to content

Commit

Permalink
introduce first pass at evaluation-tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
tigregalis committed Nov 28, 2024
1 parent 92009ea commit f598f63
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 75 deletions.
84 changes: 45 additions & 39 deletions src/art/strawberries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,51 +38,57 @@ pub fn draw(m: f64, n: f64) -> (u8, u8, u8) {
result
}

#[inline(always)]
pub fn rgb(r: f64, g: f64, b: f64) -> (u8, u8, u8) {
let result = (r.round() as u8, g.round() as u8, b.round() as u8);
result
track! {
#[inline(always)]
pub fn rgb(r: f64, g: f64, b: f64) -> (u8, u8, u8) {
let result = (r.round() as u8, g.round() as u8, b.round() as u8);
result
}
}

pub fn F(x: f64) -> f64 {
let term0 = 255. * e(-e(-HALF_M * x));
let term1 = abs(x).powf(e(-e(HALF_M * (x - 1.))));
let result = term0 * term1;
result
track! {
pub fn F(x: f64) -> f64 {
let term0 = 255. * e(-e(-HALF_M * x));
let term1 = abs(x).powf(e(-e(HALF_M * (x - 1.))));
let result = term0 * term1;
result
}
}

/// H(v,x,y) called with v = [0, 1, 2]
pub fn H(v: usize, x: f64, y: f64) -> f64 {
let v_ = v as f64;
let result = sum(1, 30, |s| {
let term0 = product_with_key("H", 0, s - 1, x, y, |r, x, y| {
let r_ = r as f64;
track! {
/// H(v,x,y) called with v = [0, 1, 2]
pub fn H(v: usize, x: f64, y: f64) -> f64 {
let v_ = v as f64;
let result = sum(1, 30, |s| {
let term0 = product_with_key("H", 0, s - 1, x, y, |r, x, y| {
let r_ = r as f64;

let term010 = 1. - A(HALF_M_INT, r, x, y);
let term0110 = e(-e(-HALF_M * (r_ - 1. / 2.)));
let term0111 = U(r, x, y);
let term011 = 1. - term0110 * term0111;
let term0120 = 5. / 4.;
let term0121 = A(4, r, x, y);
let term012 = 1. - term0120 * term0121;
let term01 = term010 * term011 * term012;
term01
let term010 = 1. - A(HALF_M_INT, r, x, y);
let term0110 = e(-e(-HALF_M * (r_ - 1. / 2.)));
let term0111 = U(r, x, y);
let term011 = 1. - term0110 * term0111;
let term0120 = 5. / 4.;
let term0121 = A(4, r, x, y);
let term012 = 1. - term0120 * term0121;
let term01 = term010 * term011 * term012;
term01
});
let term10000 = v_ - 1.;
let term1000 = 3. * term10000.pow2();
let term1001 = W(x, y);
let term100 = 5. - term1000 + term1001;
let term10 = term100 / 10.;
let term110 = 71. / 10. - 10. * P(s, x, y);
let term11 = e(-e(-abs(term110)));
let term12 = U(s, x, y);
let term13 = A(HALF_M_INT, s, x, y);
let term14 = 1. - U(s, x, y);
let term15 = L(v, s, x, y);
let term1 = term10 * term11 * term12 + term13 * term14 * term15;
term0 * term1
});
let term10000 = v_ - 1.;
let term1000 = 3. * term10000.pow2();
let term1001 = W(x, y);
let term100 = 5. - term1000 + term1001;
let term10 = term100 / 10.;
let term110 = 71. / 10. - 10. * P(s, x, y);
let term11 = e(-e(-abs(term110)));
let term12 = U(s, x, y);
let term13 = A(HALF_M_INT, s, x, y);
let term14 = 1. - U(s, x, y);
let term15 = L(v, s, x, y);
let term1 = term10 * term11 * term12 + term13 * term14 * term15;
term0 * term1
});
result
result
}
}

memo_many! {
Expand Down
74 changes: 41 additions & 33 deletions src/art/sunflower_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,53 @@ pub fn draw(m: f64, n: f64) -> (u8, u8, u8) {
result
}

#[inline(always)]
pub fn rgb(r: f64, g: f64, b: f64) -> (u8, u8, u8) {
let result = (r.round() as u8, g.round() as u8, b.round() as u8);
result
track! {
#[inline(always)]
pub fn rgb(r: f64, g: f64, b: f64) -> (u8, u8, u8) {
let result = (r.round() as u8, g.round() as u8, b.round() as u8);
result
}
}

pub fn F(x: f64) -> f64 {
let term0 = 255. * e(-e(-HALF_M * x));
let term1 = abs(x).powf(e(-e(HALF_M * (x - 1.))));
let result = term0 * term1;
result
track! {
pub fn F(x: f64) -> f64 {
let term0 = 255. * e(-e(-HALF_M * x));
let term1 = abs(x).powf(e(-e(HALF_M * (x - 1.))));
let result = term0 * term1;
result
}
}

/// H(v,x,y) called with v = [0, 1, 2]
pub fn H(v: usize, x: f64, y: f64) -> f64 {
let v_ = v as f64;
let term0 = A(v, x, y); // A is the flower field
let term1 = U(60, x, y); // U is the sky/cloud z-depth
let term20 = B(v, x, y); // B is the clouds
let term21num = 2. - v_; // sky/cloud tint hue: (2 - 0)/40 = 2/40 red
let term21den = 40.;
let term22num = 3. * v_.pow2() - 3. * v_ + 14.; // sky hue: (3*0 - 3*0 + 14)/20 = 14/20 = 70% red
let term22den = 20.;
let term23 = V(20, x, y);
let term240 = -100. * y - 3. * (x - 1. / 2.).pow2() + 14.;
let term24 = e(-e(term240));
let term2 = term20 + term21num / term21den + term22num / term22den * term23 * term24;
let result = term0 + term1 * term2;
result
track! {
/// H(v,x,y) called with v = [0, 1, 2]
pub fn H(v: usize, x: f64, y: f64) -> f64 {
let v_ = v as f64;
let term0 = A(v, x, y); // A is the flower field
let term1 = U(60, x, y); // U is the sky/cloud z-depth
let term20 = B(v, x, y); // B is the clouds
let term21num = 2. - v_; // sky/cloud tint hue: (2 - 0)/40 = 2/40 red
let term21den = 40.;
let term22num = 3. * v_.pow2() - 3. * v_ + 14.; // sky hue: (3*0 - 3*0 + 14)/20 = 14/20 = 70% red
let term22den = 20.;
let term23 = V(20, x, y);
let term240 = -100. * y - 3. * (x - 1. / 2.).pow2() + 14.;
let term24 = e(-e(term240));
let term2 = term20 + term21num / term21den + term22num / term22den * term23 * term24;
let result = term0 + term1 * term2;
result
}
}

/// A(v,x,y) called with v = [0, 1, 2]
pub fn A(v: usize, x: f64, y: f64) -> f64 {
let result = sum(1, 60, |s| {
let term0 = U(s - 1, x, y);
let term1 = W(v, s, x, y);
term0 * term1
});
result
track! {
/// A(v,x,y) called with v = [0, 1, 2]
pub fn A(v: usize, x: f64, y: f64) -> f64 {
let result = sum(1, 60, |s| {
let term0 = U(s - 1, x, y);
let term1 = W(v, s, x, y);
term0 * term1
});
result
}
}

memo_many! {
Expand Down
123 changes: 120 additions & 3 deletions src/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
fmt::Write as _,
num::NonZeroU32,
rc::Rc,
sync::mpsc::{self, Receiver},
Expand All @@ -8,8 +9,9 @@ use std::{
use crate::{utils::*, winit_app, Art};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use softbuffer::Surface;
use track::{set_should_track, with_stack};
use winit::{
event::{Event, KeyEvent, WindowEvent},
event::{ElementState, Event, KeyEvent, MouseButton, WindowEvent},
event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
keyboard::{Key, ModifiersState, NamedKey},
window::Window,
Expand All @@ -24,6 +26,7 @@ struct State {
time_started: Instant,
finished: bool,
drawn: usize,
mouse: Mouse,
}

/// A [`winit::window::Window`] paired with a [`softbuffer::Surface`]
Expand All @@ -49,6 +52,22 @@ struct PixelReady {
pixels: [u32; BATCH_SIZE],
}

struct Mouse {
x: f64,
y: f64,
left_state: ElementState,
}

impl Default for Mouse {
fn default() -> Self {
Self {
x: Default::default(),
y: Default::default(),
left_state: ElementState::Released,
}
}
}

pub fn run<Artwork: Art>() {
let event_loop = EventLoop::new().unwrap();

Expand Down Expand Up @@ -88,6 +107,7 @@ pub fn run<Artwork: Art>() {
time_started: Instant::now(),
finished: false,
drawn: 0,
mouse: Default::default(),
}
})
.with_event_handler(|state, event, elwt| {
Expand All @@ -102,6 +122,7 @@ pub fn run<Artwork: Art>() {
time_started,
finished,
drawn,
mouse,
} = state;

let image_len = image.len();
Expand Down Expand Up @@ -166,8 +187,104 @@ pub fn run<Artwork: Art>() {
}
};
}
WindowEvent::CursorMoved { .. } => {}
WindowEvent::MouseInput { .. } => {}
WindowEvent::CursorMoved {
device_id: _,
position,
} => {
mouse.x = position.x;
mouse.y = position.y;
}
WindowEvent::MouseInput {
device_id: _,
state,
button,
} => {
if button == MouseButton::Left {
let x = mouse.x as usize;
let y = mouse.y as usize;
if (mouse.left_state, state)
== (ElementState::Pressed, ElementState::Released)
&& x < Artwork::FULL_M
&& y < Artwork::FULL_N
{
std::thread::spawn(move || {
set_should_track(true);

let m = (x + 1) as f64;
let n = (y + 1) as f64;
let rgb = Artwork::draw(m, n);
println!("@ m = {m}, n = {n}");

set_should_track(false);

with_stack(|stack| {
let mut result = Vec::with_capacity(stack.len() / 2);
let mut depth = 0usize;
let mut callstack = Vec::with_capacity(stack.len() / 2);
for item in stack.drain(..) {
match item {
track::Item::Start(name) => {
result.push((depth, " ".repeat(depth)));
callstack.push(result.len() - 1);
write!(
&mut result[*callstack.last().unwrap()].1,
"{name}( "
)
.unwrap();
depth += 1;
}
track::Item::ArgUsize(arg, val) => {
write!(
&mut result[*callstack.last().unwrap()].1,
"{arg} = {val}, "
)
.unwrap();
}
track::Item::ArgF64(arg, val) => {
write!(
&mut result[*callstack.last().unwrap()].1,
"{arg} = {val:.3}, "
)
.unwrap();
}
track::Item::FinishArg => {
write!(
&mut result[*callstack.last().unwrap()].1,
")"
)
.unwrap();
}
track::Item::FinishU8U8U8(r, g, b) => {
write!(
&mut result[*callstack.last().unwrap()].1,
" = ({r},{g},{b})"
)
.unwrap();
callstack.pop();
depth -= 1;
}
track::Item::FinishF64(output) => {
write!(
&mut result[*callstack.last().unwrap()].1,
" = {output:.3}"
)
.unwrap();
callstack.pop();
depth -= 1;
}
}
}
for (_, line) in result {
println!(" {line}");
}
});

println!("=> rgb({},{},{})", rgb.0, rgb.1, rgb.2);
});
}
mouse.left_state = state;
}
}
WindowEvent::MouseWheel { .. } => {}
WindowEvent::RedrawRequested => {
let width = surface.window().inner_size().width as usize;
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{

pub mod memo_many;
pub mod memo_once;
pub mod track;
pub mod winit_app;

#[inline(always)]
Expand Down
Loading

0 comments on commit f598f63

Please sign in to comment.