better but still not workable

This commit is contained in:
2025-12-11 00:12:16 +01:00
parent 5e9336c6c7
commit 4930d86e23
10 changed files with 945 additions and 566 deletions

View File

@@ -3,12 +3,12 @@ use std::thread;
use std::time::{Duration, Instant};
use std::sync::{Arc, Mutex};
use rand::Rng;
use teleprof::instrument;
const WIDTH: usize = 800;
const HEIGHT: usize = 600;
const BALL_RADIUS: usize = 20;
// Simple ball state
struct Ball {
x: f32,
y: f32,
@@ -22,9 +22,9 @@ impl Ball {
Self {
x: 400.0,
y: 300.0,
vx: 200.0, // pixels per second
vx: 200.0,
vy: 150.0,
color: 0xFF6464FF, // Red-ish
color: 0xFF6464FF,
}
}
}
@@ -32,10 +32,14 @@ impl Ball {
fn main() {
// Start the telemetry window
teleprof::start();
// Name the main thread
teleprof::set_thread_name("Main");
println!("Bouncing Ball Demo");
println!("The ball window should appear alongside the profiler");
println!("Press Space in profiler window to pause");
println!("Mouse wheel to zoom, drag to pan in profiler");
println!("Press Escape in either window to quit");
println!();
@@ -52,49 +56,14 @@ fn main() {
let ball = Arc::new(Mutex::new(Ball::new()));
let mut framebuffer = vec![0u32; WIDTH * HEIGHT];
// Target 30 FPS
let frame_time = Duration::from_millis(33);
let mut frame_count = 0;
while window.is_open() && !window.is_key_down(Key::Escape) {
let frame_start = Instant::now();
teleprof::span!("main_frame");
// Check if paused
if teleprof::PAUSE.try_lock().is_err() {
println!("Paused!");
while teleprof::PAUSE.try_lock().is_err() {
thread::sleep(Duration::from_millis(100));
}
println!("Resumed!");
}
main_frame(&ball, &mut framebuffer, &mut window, &mut frame_count, frame_time);
// Update physics
let hit_wall = update_physics(&ball, frame_time.as_secs_f32());
// If we hit a wall, spawn a thread to pick a new color
if hit_wall {
let ball_clone = Arc::clone(&ball);
thread::spawn(move || {
pick_new_color(ball_clone);
});
}
// Render
render(&ball, &mut framebuffer);
// Update window
window
.update_with_buffer(&framebuffer, WIDTH, HEIGHT)
.expect("Failed to update window");
frame_count += 1;
if frame_count % 30 == 0 {
print_status(&ball, frame_count);
}
// Sleep to maintain 30fps
let elapsed = frame_start.elapsed();
if elapsed < frame_time {
thread::sleep(frame_time - elapsed);
@@ -102,18 +71,58 @@ fn main() {
}
}
fn update_physics(ball: &Arc<Mutex<Ball>>, dt: f32) -> bool {
teleprof::span!("update_physics");
#[instrument]
fn main_frame(
ball: &Arc<Mutex<Ball>>,
framebuffer: &mut [u32],
window: &mut Window,
frame_count: &mut u32,
frame_time: Duration,
) {
// Check if paused
if teleprof::PAUSE.try_lock().is_err() {
println!("Paused!");
while teleprof::PAUSE.try_lock().is_err() {
thread::sleep(Duration::from_millis(100));
}
println!("Resumed!");
}
// Update physics
let hit_wall = update_physics(ball, frame_time.as_secs_f32());
// If we hit a wall, spawn a thread to pick a new color
if hit_wall {
let ball_clone = Arc::clone(ball);
thread::spawn(move || {
teleprof::set_thread_name("ColorPicker");
pick_new_color(ball_clone);
});
}
// Render
render(ball, framebuffer);
// Update window
window
.update_with_buffer(framebuffer, WIDTH, HEIGHT)
.expect("Failed to update window");
*frame_count += 1;
if *frame_count % 30 == 0 {
print_status(ball, *frame_count);
}
}
#[instrument]
fn update_physics(ball: &Arc<Mutex<Ball>>, dt: f32) -> bool {
let mut ball = ball.lock().unwrap();
// Update position
ball.x += ball.vx * dt;
ball.y += ball.vy * dt;
let mut hit_wall = false;
// Bounce off walls
let radius = BALL_RADIUS as f32;
if ball.x - radius < 0.0 || ball.x + radius > WIDTH as f32 {
ball.vx = -ball.vx;
@@ -127,16 +136,13 @@ fn update_physics(ball: &Arc<Mutex<Ball>>, dt: f32) -> bool {
hit_wall = true;
}
// Simulate some physics computation
thread::sleep(Duration::from_millis(5));
hit_wall
}
#[instrument]
fn pick_new_color(ball: Arc<Mutex<Ball>>) {
teleprof::span!("pick_new_color");
// Simulate some "expensive" color selection
thread::sleep(Duration::from_millis(10));
let mut rng = rand::thread_rng();
@@ -152,50 +158,48 @@ fn pick_new_color(ball: Arc<Mutex<Ball>>) {
println!(" → New color selected: RGB({}, {}, {})", r, g, b);
}
#[instrument]
fn render(ball: &Arc<Mutex<Ball>>, framebuffer: &mut [u32]) {
teleprof::span!("render");
clear_background(framebuffer);
draw_ball(ball, framebuffer);
submit_frame();
}
#[instrument]
fn clear_background(framebuffer: &mut [u32]) {
framebuffer.fill(0x2A2A2AFF);
}
#[instrument]
fn draw_ball(ball: &Arc<Mutex<Ball>>, framebuffer: &mut [u32]) {
let ball = ball.lock().unwrap();
{
teleprof::span!("clear_background");
// Clear to dark gray
framebuffer.fill(0x2A2A2AFF);
}
let cx = ball.x as i32;
let cy = ball.y as i32;
let radius = BALL_RADIUS as i32;
{
teleprof::span!("draw_ball");
let ball = ball.lock().unwrap();
// Draw ball as a filled circle
let cx = ball.x as i32;
let cy = ball.y as i32;
let radius = BALL_RADIUS as i32;
for dy in -radius..=radius {
for dx in -radius..=radius {
// Check if point is inside circle
if dx * dx + dy * dy <= radius * radius {
let x = cx + dx;
let y = cy + dy;
if x >= 0 && x < WIDTH as i32 && y >= 0 && y < HEIGHT as i32 {
let idx = y as usize * WIDTH + x as usize;
framebuffer[idx] = ball.color;
}
for dy in -radius..=radius {
for dx in -radius..=radius {
if dx * dx + dy * dy <= radius * radius {
let x = cx + dx;
let y = cy + dy;
if x >= 0 && x < WIDTH as i32 && y >= 0 && y < HEIGHT as i32 {
let idx = y as usize * WIDTH + x as usize;
framebuffer[idx] = ball.color;
}
}
}
}
{
teleprof::span!("submit_frame");
// Simulate GPU submission
thread::sleep(Duration::from_millis(2));
}
}
#[instrument]
fn submit_frame() {
thread::sleep(Duration::from_millis(2));
}
#[instrument]
fn print_status(ball: &Arc<Mutex<Ball>>, frame: u32) {
teleprof::span!("print_status");
let ball = ball.lock().unwrap();
println!(
"Frame {}: Ball at ({:.1}, {:.1})",

View File

@@ -1,5 +1,6 @@
use std::thread;
use std::time::Duration;
use teleprof::instrument;
fn main() {
// Start the telemetry window
@@ -7,7 +8,9 @@ fn main() {
println!("Teleprof demo running...");
println!("Press Space in the profiler window to pause/unpause");
println!("Mouse wheel to zoom, drag to pan");
println!("Press Escape in the profiler window to quit");
println!();
// Simulate some work
for i in 0..1000 {
@@ -26,9 +29,8 @@ fn main() {
}
}
#[instrument]
fn frame_work(frame: u32) {
teleprof::span!("frame_work");
physics_update();
render();
@@ -37,9 +39,8 @@ fn frame_work(frame: u32) {
}
}
#[instrument]
fn physics_update() {
teleprof::span!("physics_update");
// Spawn some worker threads
let handles: Vec<_> = (0..3).map(|i| {
thread::spawn(move || {
@@ -52,32 +53,30 @@ fn physics_update() {
}
}
#[instrument]
fn physics_worker(id: u32) {
teleprof::span!("physics_worker");
// Simulate work
let work_ms = 5 + (id * 2);
thread::sleep(Duration::from_millis(work_ms as u64));
}
#[instrument]
fn render() {
teleprof::span!("render");
build_command_buffer();
submit_to_gpu();
}
#[instrument]
fn build_command_buffer() {
teleprof::span!("build_command_buffer");
thread::sleep(Duration::from_millis(3));
}
#[instrument]
fn submit_to_gpu() {
teleprof::span!("submit_to_gpu");
thread::sleep(Duration::from_millis(2));
}
#[instrument]
fn occasional_task() {
teleprof::span!("occasional_task");
thread::sleep(Duration::from_millis(10));
}
}