2023-03-05 18:56:35 +00:00
|
|
|
use crate::{
|
|
|
|
fractal_compute_pipeline::FractalComputePipeline, place_over_frame::RenderPassPlaceOverFrame,
|
|
|
|
};
|
2024-02-21 08:30:15 +00:00
|
|
|
use glam::f32::Vec2;
|
2023-03-05 18:56:35 +00:00
|
|
|
use std::{sync::Arc, time::Instant};
|
|
|
|
use vulkano::{
|
2023-10-25 09:01:30 +00:00
|
|
|
command_buffer::allocator::{
|
|
|
|
StandardCommandBufferAllocator, StandardCommandBufferAllocatorCreateInfo,
|
|
|
|
},
|
|
|
|
descriptor_set::allocator::StandardDescriptorSetAllocator,
|
|
|
|
device::Queue,
|
|
|
|
image::view::ImageView,
|
|
|
|
memory::allocator::StandardMemoryAllocator,
|
|
|
|
sync::GpuFuture,
|
2023-03-05 18:56:35 +00:00
|
|
|
};
|
2023-07-03 20:37:29 +00:00
|
|
|
use vulkano_util::{renderer::VulkanoWindowRenderer, window::WindowDescriptor};
|
2022-03-06 19:30:49 +00:00
|
|
|
use winit::{
|
|
|
|
dpi::PhysicalPosition,
|
2023-10-31 17:47:17 +00:00
|
|
|
event::{ElementState, Event, KeyEvent, MouseButton, MouseScrollDelta, WindowEvent},
|
|
|
|
keyboard::{Key, NamedKey},
|
2023-03-05 18:56:35 +00:00
|
|
|
window::Fullscreen,
|
2021-09-13 15:14:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const MAX_ITERS_INIT: u32 = 200;
|
|
|
|
const MOVE_SPEED: f32 = 0.5;
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// App for exploring Julia and Mandelbrot fractals.
|
2021-09-13 15:14:54 +00:00
|
|
|
pub struct FractalApp {
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Pipeline that computes Mandelbrot & Julia fractals and writes them to an image.
|
2021-09-13 15:14:54 +00:00
|
|
|
fractal_pipeline: FractalComputePipeline,
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Our render pipeline (pass).
|
2022-06-24 15:27:33 +00:00
|
|
|
pub place_over_frame: RenderPassPlaceOverFrame,
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Toggle that flips between Julia and Mandelbrot.
|
2021-09-13 15:14:54 +00:00
|
|
|
pub is_julia: bool,
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Toggle that stops the movement on Julia.
|
2021-09-13 15:14:54 +00:00
|
|
|
is_c_paused: bool,
|
|
|
|
/// C is a constant input to Julia escape time algorithm (mouse position).
|
2024-02-21 08:30:15 +00:00
|
|
|
c: Vec2,
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Our zoom level.
|
2024-02-21 08:30:15 +00:00
|
|
|
scale: Vec2,
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Our translation on the complex plane.
|
2024-02-21 08:30:15 +00:00
|
|
|
translation: Vec2,
|
2023-03-05 18:56:35 +00:00
|
|
|
/// How long the escape time algorithm should run (higher = less performance, more accurate
|
|
|
|
/// image).
|
2021-09-13 15:14:54 +00:00
|
|
|
pub max_iters: u32,
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Time tracking, useful for frame independent movement.
|
2021-09-13 15:14:54 +00:00
|
|
|
time: Instant,
|
|
|
|
dt: f32,
|
|
|
|
dt_sum: f32,
|
|
|
|
frame_count: f32,
|
|
|
|
avg_fps: f32,
|
|
|
|
/// Input state to handle mouse positions, continuous movement etc.
|
|
|
|
input_state: InputState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FractalApp {
|
2024-02-21 08:08:50 +00:00
|
|
|
pub fn new(
|
|
|
|
gfx_queue: Arc<Queue>,
|
|
|
|
image_format: vulkano::format::Format,
|
|
|
|
swapchain_image_views: &[Arc<ImageView>],
|
|
|
|
) -> FractalApp {
|
2022-10-26 14:25:01 +00:00
|
|
|
let memory_allocator = Arc::new(StandardMemoryAllocator::new_default(
|
|
|
|
gfx_queue.device().clone(),
|
|
|
|
));
|
2022-10-27 18:59:47 +00:00
|
|
|
let command_buffer_allocator = Arc::new(StandardCommandBufferAllocator::new(
|
2022-10-05 09:09:26 +00:00
|
|
|
gfx_queue.device().clone(),
|
2023-10-25 09:01:30 +00:00
|
|
|
StandardCommandBufferAllocatorCreateInfo {
|
|
|
|
secondary_buffer_count: 32,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-10-05 09:09:26 +00:00
|
|
|
));
|
2022-10-27 18:59:47 +00:00
|
|
|
let descriptor_set_allocator = Arc::new(StandardDescriptorSetAllocator::new(
|
2022-10-05 09:09:26 +00:00
|
|
|
gfx_queue.device().clone(),
|
2023-10-24 14:23:17 +00:00
|
|
|
Default::default(),
|
2022-10-05 09:09:26 +00:00
|
|
|
));
|
|
|
|
|
2021-09-13 15:14:54 +00:00
|
|
|
FractalApp {
|
2022-10-05 09:09:26 +00:00
|
|
|
fractal_pipeline: FractalComputePipeline::new(
|
|
|
|
gfx_queue.clone(),
|
2022-10-26 14:25:01 +00:00
|
|
|
memory_allocator.clone(),
|
2022-10-05 09:09:26 +00:00
|
|
|
command_buffer_allocator.clone(),
|
|
|
|
descriptor_set_allocator.clone(),
|
|
|
|
),
|
|
|
|
place_over_frame: RenderPassPlaceOverFrame::new(
|
|
|
|
gfx_queue,
|
|
|
|
command_buffer_allocator,
|
|
|
|
descriptor_set_allocator,
|
|
|
|
image_format,
|
2024-02-21 08:08:50 +00:00
|
|
|
swapchain_image_views,
|
2022-10-05 09:09:26 +00:00
|
|
|
),
|
2021-09-13 15:14:54 +00:00
|
|
|
is_julia: false,
|
|
|
|
is_c_paused: false,
|
2024-02-21 08:30:15 +00:00
|
|
|
c: Vec2::new(0.0, 0.0),
|
|
|
|
scale: Vec2::new(4.0, 4.0),
|
|
|
|
translation: Vec2::new(0.0, 0.0),
|
2021-09-13 15:14:54 +00:00
|
|
|
max_iters: MAX_ITERS_INIT,
|
|
|
|
time: Instant::now(),
|
|
|
|
dt: 0.0,
|
|
|
|
dt_sum: 0.0,
|
|
|
|
frame_count: 0.0,
|
|
|
|
avg_fps: 0.0,
|
|
|
|
input_state: InputState::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_guide(&self) {
|
|
|
|
println!(
|
|
|
|
"\
|
|
|
|
Usage:
|
|
|
|
WASD: Pan view
|
|
|
|
Scroll: Zoom in/out
|
|
|
|
Space: Toggle between Mandelbrot and Julia
|
|
|
|
Enter: Randomize color palette
|
|
|
|
Equals/Minus: Increase/Decrease max iterations
|
2022-02-20 09:55:34 +00:00
|
|
|
F: Toggle full-screen
|
2021-09-13 15:14:54 +00:00
|
|
|
Right mouse: Stop movement in Julia (mouse position determines c)
|
|
|
|
Esc: Quit\
|
2023-03-05 18:56:35 +00:00
|
|
|
",
|
2021-09-13 15:14:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Runs our compute pipeline and return a future of when the compute is finished.
|
2023-07-03 20:37:29 +00:00
|
|
|
pub fn compute(&self, image_target: Arc<ImageView>) -> Box<dyn GpuFuture> {
|
2021-09-13 15:14:54 +00:00
|
|
|
self.fractal_pipeline.compute(
|
|
|
|
image_target,
|
|
|
|
self.c,
|
|
|
|
self.scale,
|
|
|
|
self.translation,
|
|
|
|
self.max_iters,
|
|
|
|
self.is_julia,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Returns whether the app should quit. (Happens on when pressing ESC.)
|
2021-09-13 15:14:54 +00:00
|
|
|
pub fn is_running(&self) -> bool {
|
|
|
|
!self.input_state.should_quit
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Returns the average FPS.
|
2021-09-13 15:14:54 +00:00
|
|
|
pub fn avg_fps(&self) -> f32 {
|
|
|
|
self.avg_fps
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Returns the delta time in milliseconds.
|
2021-09-13 15:14:54 +00:00
|
|
|
pub fn dt(&self) -> f32 {
|
|
|
|
self.dt * 1000.0
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Updates times and dt at the end of each frame.
|
2021-09-13 15:14:54 +00:00
|
|
|
pub fn update_time(&mut self) {
|
2023-03-05 18:56:35 +00:00
|
|
|
// Each second, update average fps & reset frame count & dt sum.
|
2021-09-13 15:14:54 +00:00
|
|
|
if self.dt_sum > 1.0 {
|
|
|
|
self.avg_fps = self.frame_count / self.dt_sum;
|
|
|
|
self.frame_count = 0.0;
|
|
|
|
self.dt_sum = 0.0;
|
|
|
|
}
|
2022-03-06 19:30:49 +00:00
|
|
|
self.dt = self.time.elapsed().as_secs_f32();
|
2021-09-13 15:14:54 +00:00
|
|
|
self.dt_sum += self.dt;
|
|
|
|
self.frame_count += 1.0;
|
|
|
|
self.time = Instant::now();
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Updates app state based on input state.
|
2022-06-24 15:27:33 +00:00
|
|
|
pub fn update_state_after_inputs(&mut self, renderer: &mut VulkanoWindowRenderer) {
|
2023-03-05 18:56:35 +00:00
|
|
|
// Zoom in or out.
|
2021-09-13 15:14:54 +00:00
|
|
|
if self.input_state.scroll_delta > 0. {
|
|
|
|
self.scale /= 1.05;
|
|
|
|
} else if self.input_state.scroll_delta < 0. {
|
|
|
|
self.scale *= 1.05;
|
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Move speed scaled by zoom level.
|
2021-09-13 15:14:54 +00:00
|
|
|
let move_speed = MOVE_SPEED * self.dt * self.scale.x;
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Panning.
|
2021-09-13 15:14:54 +00:00
|
|
|
if self.input_state.pan_up {
|
2024-02-21 08:30:15 +00:00
|
|
|
self.translation += Vec2::new(0.0, move_speed);
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
|
|
|
if self.input_state.pan_down {
|
2024-02-21 08:30:15 +00:00
|
|
|
self.translation += Vec2::new(0.0, -move_speed);
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
|
|
|
if self.input_state.pan_right {
|
2024-02-21 08:30:15 +00:00
|
|
|
self.translation += Vec2::new(move_speed, 0.0);
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
|
|
|
if self.input_state.pan_left {
|
2024-02-21 08:30:15 +00:00
|
|
|
self.translation += Vec2::new(-move_speed, 0.0);
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Toggle between Julia and Mandelbrot.
|
2021-09-13 15:14:54 +00:00
|
|
|
if self.input_state.toggle_julia {
|
|
|
|
self.is_julia = !self.is_julia;
|
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Toggle c.
|
2021-09-13 15:14:54 +00:00
|
|
|
if self.input_state.toggle_c {
|
|
|
|
self.is_c_paused = !self.is_c_paused;
|
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Update c.
|
2021-09-13 15:14:54 +00:00
|
|
|
if !self.is_c_paused {
|
2023-03-05 18:56:35 +00:00
|
|
|
// Scale normalized mouse pos between -1.0 and 1.0.
|
2024-02-21 08:30:15 +00:00
|
|
|
let mouse_pos = self.input_state.normalized_mouse_pos() * 2.0 - Vec2::new(1.0, 1.0);
|
2023-03-05 18:56:35 +00:00
|
|
|
// Scale by our zoom (scale) level so when zooming in the movement on Julia is not so
|
|
|
|
// drastic.
|
2021-09-13 15:14:54 +00:00
|
|
|
self.c = mouse_pos * self.scale.x;
|
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Update how many iterations we have.
|
2021-09-13 15:14:54 +00:00
|
|
|
if self.input_state.increase_iterations {
|
|
|
|
self.max_iters += 1;
|
|
|
|
}
|
|
|
|
if self.input_state.decrease_iterations {
|
|
|
|
if self.max_iters as i32 - 1 <= 0 {
|
|
|
|
self.max_iters = 0;
|
|
|
|
} else {
|
|
|
|
self.max_iters -= 1;
|
|
|
|
}
|
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Randomize our palette.
|
2021-09-13 15:14:54 +00:00
|
|
|
if self.input_state.randomize_palette {
|
|
|
|
self.fractal_pipeline.randomize_palette();
|
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Toggle full-screen.
|
2022-02-20 09:55:34 +00:00
|
|
|
if self.input_state.toggle_full_screen {
|
2022-06-24 15:27:33 +00:00
|
|
|
let is_full_screen = renderer.window().fullscreen().is_some();
|
|
|
|
renderer.window().set_fullscreen(if !is_full_screen {
|
|
|
|
Some(Fullscreen::Borderless(renderer.window().current_monitor()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
});
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Update input state.
|
2022-06-24 15:27:33 +00:00
|
|
|
pub fn handle_input(&mut self, window_size: [f32; 2], event: &Event<()>) {
|
2021-09-13 15:14:54 +00:00
|
|
|
self.input_state.handle_input(window_size, event);
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Reset input state at the end of the frame.
|
2021-09-13 15:14:54 +00:00
|
|
|
pub fn reset_input_state(&mut self) {
|
|
|
|
self.input_state.reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn state_is_pressed(state: ElementState) -> bool {
|
|
|
|
match state {
|
|
|
|
ElementState::Pressed => true,
|
|
|
|
ElementState::Released => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Just a very simple input state (mappings). Winit only has `Pressed` and `Released` events, thus
|
|
|
|
/// continuous movement needs toggles. Panning is one of those things where continuous movement
|
|
|
|
/// feels better.
|
2021-09-13 15:14:54 +00:00
|
|
|
struct InputState {
|
2022-06-24 15:27:33 +00:00
|
|
|
pub window_size: [f32; 2],
|
2021-09-13 15:14:54 +00:00
|
|
|
pub pan_up: bool,
|
|
|
|
pub pan_down: bool,
|
|
|
|
pub pan_right: bool,
|
|
|
|
pub pan_left: bool,
|
|
|
|
pub increase_iterations: bool,
|
|
|
|
pub decrease_iterations: bool,
|
|
|
|
pub randomize_palette: bool,
|
2022-02-20 09:55:34 +00:00
|
|
|
pub toggle_full_screen: bool,
|
2021-09-13 15:14:54 +00:00
|
|
|
pub toggle_julia: bool,
|
|
|
|
pub toggle_c: bool,
|
|
|
|
pub should_quit: bool,
|
|
|
|
pub scroll_delta: f32,
|
2024-02-21 08:30:15 +00:00
|
|
|
pub mouse_pos: Vec2,
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InputState {
|
|
|
|
fn new() -> InputState {
|
|
|
|
InputState {
|
2022-06-24 15:27:33 +00:00
|
|
|
window_size: [
|
|
|
|
WindowDescriptor::default().width,
|
|
|
|
WindowDescriptor::default().height,
|
|
|
|
],
|
2021-09-13 15:14:54 +00:00
|
|
|
pan_up: false,
|
|
|
|
pan_down: false,
|
|
|
|
pan_right: false,
|
|
|
|
pan_left: false,
|
|
|
|
increase_iterations: false,
|
|
|
|
decrease_iterations: false,
|
|
|
|
randomize_palette: false,
|
2022-02-20 09:55:34 +00:00
|
|
|
toggle_full_screen: false,
|
2021-09-13 15:14:54 +00:00
|
|
|
toggle_julia: false,
|
|
|
|
toggle_c: false,
|
|
|
|
should_quit: false,
|
|
|
|
scroll_delta: 0.0,
|
2024-02-21 08:30:15 +00:00
|
|
|
mouse_pos: Vec2::new(0.0, 0.0),
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 08:30:15 +00:00
|
|
|
fn normalized_mouse_pos(&self) -> Vec2 {
|
|
|
|
Vec2::new(
|
2022-12-18 16:23:40 +00:00
|
|
|
(self.mouse_pos.x / self.window_size[0]).clamp(0.0, 1.0),
|
|
|
|
(self.mouse_pos.y / self.window_size[1]).clamp(0.0, 1.0),
|
2021-09-13 15:14:54 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Resets values that should be reset. All incremental mappings and toggles should be reset.
|
2021-09-13 15:14:54 +00:00
|
|
|
fn reset(&mut self) {
|
|
|
|
*self = InputState {
|
|
|
|
scroll_delta: 0.0,
|
2022-02-20 09:55:34 +00:00
|
|
|
toggle_full_screen: false,
|
2021-09-13 15:14:54 +00:00
|
|
|
toggle_julia: false,
|
|
|
|
toggle_c: false,
|
|
|
|
randomize_palette: false,
|
|
|
|
increase_iterations: false,
|
|
|
|
decrease_iterations: false,
|
|
|
|
..*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 15:27:33 +00:00
|
|
|
fn handle_input(&mut self, window_size: [f32; 2], event: &Event<()>) {
|
2021-09-13 15:14:54 +00:00
|
|
|
self.window_size = window_size;
|
2023-10-31 17:47:17 +00:00
|
|
|
if let Event::WindowEvent { event, .. } = event {
|
2021-09-13 15:14:54 +00:00
|
|
|
match event {
|
2023-10-31 17:47:17 +00:00
|
|
|
WindowEvent::KeyboardInput { event, .. } => self.on_keyboard_event(event),
|
2021-09-13 15:14:54 +00:00
|
|
|
WindowEvent::MouseInput { state, button, .. } => {
|
|
|
|
self.on_mouse_click_event(*state, *button)
|
|
|
|
}
|
|
|
|
WindowEvent::CursorMoved { position, .. } => self.on_cursor_moved_event(position),
|
|
|
|
WindowEvent::MouseWheel { delta, .. } => self.on_mouse_wheel_event(delta),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Matches keyboard events to our defined inputs.
|
2023-10-31 17:47:17 +00:00
|
|
|
fn on_keyboard_event(&mut self, event: &KeyEvent) {
|
|
|
|
match event.logical_key.as_ref() {
|
|
|
|
Key::Named(NamedKey::Escape) => self.should_quit = state_is_pressed(event.state),
|
|
|
|
Key::Character("w") => self.pan_up = state_is_pressed(event.state),
|
|
|
|
Key::Character("a") => self.pan_left = state_is_pressed(event.state),
|
|
|
|
Key::Character("s") => self.pan_down = state_is_pressed(event.state),
|
|
|
|
Key::Character("d") => self.pan_right = state_is_pressed(event.state),
|
|
|
|
Key::Character("f") => self.toggle_full_screen = state_is_pressed(event.state),
|
|
|
|
Key::Named(NamedKey::Enter) => self.randomize_palette = state_is_pressed(event.state),
|
|
|
|
Key::Character("=") => self.increase_iterations = state_is_pressed(event.state),
|
|
|
|
Key::Character("-") => self.decrease_iterations = state_is_pressed(event.state),
|
|
|
|
Key::Named(NamedKey::Space) => self.toggle_julia = state_is_pressed(event.state),
|
|
|
|
_ => (),
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Updates mouse scroll delta.
|
2021-09-13 15:14:54 +00:00
|
|
|
fn on_mouse_wheel_event(&mut self, delta: &MouseScrollDelta) {
|
|
|
|
let change = match delta {
|
|
|
|
MouseScrollDelta::LineDelta(_x, y) => *y,
|
|
|
|
MouseScrollDelta::PixelDelta(pos) => pos.y as f32,
|
|
|
|
};
|
|
|
|
self.scroll_delta += change;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update mouse position
|
|
|
|
fn on_cursor_moved_event(&mut self, pos: &PhysicalPosition<f64>) {
|
2024-02-21 08:30:15 +00:00
|
|
|
self.mouse_pos = Vec2::new(pos.x as f32, pos.y as f32);
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Update toggle julia state (if right mouse is clicked)
|
|
|
|
fn on_mouse_click_event(&mut self, state: ElementState, mouse_btn: winit::event::MouseButton) {
|
2022-08-12 10:18:35 +00:00
|
|
|
if mouse_btn == MouseButton::Right {
|
|
|
|
self.toggle_c = state_is_pressed(state)
|
|
|
|
}
|
2021-09-13 15:14:54 +00:00
|
|
|
}
|
|
|
|
}
|