()
| 97 | } |
| 98 | |
| 99 | fn main() { |
| 100 | const WIDTH: usize = 1280; |
| 101 | const HEIGHT: usize = 720; |
| 102 | |
| 103 | let mut window = Window::new( |
| 104 | "Rust GPU - CPU shader evaluation", |
| 105 | WIDTH, |
| 106 | HEIGHT, |
| 107 | WindowOptions::default(), |
| 108 | ) |
| 109 | .expect("Window creation failed"); |
| 110 | |
| 111 | let push_constants = ShaderConstants { |
| 112 | width: WIDTH as u32, |
| 113 | height: HEIGHT as u32, |
| 114 | time: 0f32, |
| 115 | |
| 116 | // FIXME(eddyb) implement mouse support for the cpu runner. |
| 117 | cursor_x: 0.0, |
| 118 | cursor_y: 0.0, |
| 119 | drag_start_x: 0.0, |
| 120 | drag_start_y: 0.0, |
| 121 | drag_end_x: 0.0, |
| 122 | drag_end_y: 0.0, |
| 123 | mouse_button_pressed: 0, |
| 124 | mouse_button_press_time: [f32::NEG_INFINITY; 3], |
| 125 | }; |
| 126 | |
| 127 | // Limit to max ~60 fps update rate |
| 128 | window.limit_update_rate(Some(std::time::Duration::from_micros(16600))); |
| 129 | |
| 130 | let start_time = Instant::now(); |
| 131 | |
| 132 | let buffer = (0..WIDTH * HEIGHT) |
| 133 | .into_par_iter() |
| 134 | .map(|i| { |
| 135 | let screen_pos = vec2( |
| 136 | (i % WIDTH) as f32 / WIDTH as f32 * 2.0 - 1.0, |
| 137 | -((i / WIDTH) as f32 / HEIGHT as f32 * 2.0 - 1.0), |
| 138 | ); |
| 139 | |
| 140 | let frag_coord = (vec2(screen_pos.x, -screen_pos.y) + Vec2::ONE) / Vec2::splat(2.0) |
| 141 | * vec2(WIDTH as f32, HEIGHT as f32); |
| 142 | |
| 143 | // evaluate the fragment shader for the specific pixel |
| 144 | let color = shader_module::fs(&push_constants, frag_coord, 1); |
| 145 | |
| 146 | color_u32_from_vec4(color) |
| 147 | }) |
| 148 | .collect::<Vec<_>>(); |
| 149 | |
| 150 | println!( |
| 151 | "Evaluating {} pixels took {} ms", |
| 152 | buffer.len(), |
| 153 | start_time.elapsed().as_millis() |
| 154 | ); |
| 155 | |
| 156 | while window.is_open() && !window.is_key_down(Key::Escape) { |
nothing calls this directly
no test coverage detected