2023-03-05 18:56:35 +00:00
|
|
|
// A multi windowed game of life application. You could use this to learn:
|
|
|
|
//
|
|
|
|
// - how to handle multiple window inputs,
|
|
|
|
// - how to draw on a canvas,
|
|
|
|
// - how to organize compute shader with graphics,
|
|
|
|
// - how to do a cellular automata simulation using compute shaders.
|
|
|
|
//
|
|
|
|
// The possibilities are limitless. ;)
|
|
|
|
|
2022-02-02 16:21:32 +00:00
|
|
|
mod app;
|
|
|
|
mod game_of_life;
|
|
|
|
mod pixels_draw;
|
|
|
|
mod render_pass;
|
|
|
|
|
2022-06-24 15:27:33 +00:00
|
|
|
use crate::app::{App, RenderPipeline};
|
2024-02-21 08:30:15 +00:00
|
|
|
use glam::{f32::Vec2, IVec2};
|
2024-03-27 21:00:56 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2022-06-24 15:27:33 +00:00
|
|
|
use vulkano_util::renderer::VulkanoWindowRenderer;
|
2022-02-02 16:21:32 +00:00
|
|
|
use winit::{
|
2022-03-06 19:30:49 +00:00
|
|
|
event::{ElementState, Event, MouseButton, WindowEvent},
|
2022-02-02 16:21:32 +00:00
|
|
|
event_loop::{ControlFlow, EventLoop},
|
|
|
|
};
|
|
|
|
|
2022-06-24 15:27:33 +00:00
|
|
|
pub const WINDOW_WIDTH: f32 = 1024.0;
|
|
|
|
pub const WINDOW_HEIGHT: f32 = 1024.0;
|
|
|
|
pub const WINDOW2_WIDTH: f32 = 512.0;
|
|
|
|
pub const WINDOW2_HEIGHT: f32 = 512.0;
|
|
|
|
pub const SCALING: f32 = 2.0;
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-10-31 17:47:17 +00:00
|
|
|
fn main() -> Result<(), impl Error> {
|
2023-03-05 18:56:35 +00:00
|
|
|
println!("Welcome to Vulkano Game of Life\nUse the mouse to draw life on the grid(s)\n");
|
|
|
|
|
|
|
|
// Create event loop.
|
2023-10-31 17:47:17 +00:00
|
|
|
let event_loop = EventLoop::new().unwrap();
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Create app with vulkano context.
|
2022-02-02 16:21:32 +00:00
|
|
|
let mut app = App::default();
|
|
|
|
app.open(&event_loop);
|
|
|
|
|
|
|
|
// Time & inputs...
|
|
|
|
let mut time = Instant::now();
|
2024-02-21 08:30:15 +00:00
|
|
|
let mut cursor_pos = Vec2::ZERO;
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// An extremely crude way to handle input state... but works for this example.
|
2022-02-02 16:21:32 +00:00
|
|
|
let mut mouse_is_pressed_w1 = false;
|
|
|
|
let mut mouse_is_pressed_w2 = false;
|
2023-03-05 18:56:35 +00:00
|
|
|
|
2023-10-31 17:47:17 +00:00
|
|
|
event_loop.run(move |event, elwt| {
|
|
|
|
elwt.set_control_flow(ControlFlow::Poll);
|
|
|
|
|
2023-06-04 07:15:21 +00:00
|
|
|
if process_event(
|
|
|
|
&event,
|
2022-02-02 16:21:32 +00:00
|
|
|
&mut app,
|
|
|
|
&mut cursor_pos,
|
|
|
|
&mut mouse_is_pressed_w1,
|
|
|
|
&mut mouse_is_pressed_w2,
|
|
|
|
) {
|
2023-10-31 17:47:17 +00:00
|
|
|
elwt.exit();
|
2023-06-04 07:15:21 +00:00
|
|
|
return;
|
2023-10-31 17:47:17 +00:00
|
|
|
} else if event == Event::AboutToWait {
|
|
|
|
for (_, renderer) in app.windows.iter() {
|
|
|
|
renderer.window().request_redraw();
|
|
|
|
}
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Draw life on windows if mouse is down.
|
2022-02-02 16:21:32 +00:00
|
|
|
draw_life(
|
|
|
|
&mut app,
|
|
|
|
cursor_pos,
|
|
|
|
mouse_is_pressed_w1,
|
|
|
|
mouse_is_pressed_w2,
|
|
|
|
);
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Compute life & render 60fps.
|
2022-03-06 19:30:49 +00:00
|
|
|
if (Instant::now() - time).as_secs_f64() > 1.0 / 60.0 {
|
2022-02-02 16:21:32 +00:00
|
|
|
compute_then_render_per_window(&mut app);
|
|
|
|
time = Instant::now();
|
|
|
|
}
|
2023-06-04 07:15:21 +00:00
|
|
|
})
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-04 07:15:21 +00:00
|
|
|
/// Processes a single event for an event loop.
|
|
|
|
/// Returns true only if the window is to be closed.
|
|
|
|
pub fn process_event(
|
|
|
|
event: &Event<()>,
|
2022-02-02 16:21:32 +00:00
|
|
|
app: &mut App,
|
2024-02-21 08:30:15 +00:00
|
|
|
cursor_pos: &mut Vec2,
|
2022-02-02 16:21:32 +00:00
|
|
|
mouse_pressed_w1: &mut bool,
|
|
|
|
mouse_pressed_w2: &mut bool,
|
|
|
|
) -> bool {
|
2023-06-17 19:01:50 +00:00
|
|
|
if let Event::WindowEvent {
|
|
|
|
event, window_id, ..
|
|
|
|
} = &event
|
|
|
|
{
|
|
|
|
match event {
|
2023-06-04 07:15:21 +00:00
|
|
|
WindowEvent::CloseRequested => {
|
|
|
|
if *window_id == app.windows.primary_window_id().unwrap() {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Destroy window by removing its renderer.
|
|
|
|
app.windows.remove_renderer(*window_id);
|
|
|
|
app.pipelines.remove(window_id);
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
2023-06-04 07:15:21 +00:00
|
|
|
}
|
|
|
|
// Resize window and its images.
|
|
|
|
WindowEvent::Resized(..) | WindowEvent::ScaleFactorChanged { .. } => {
|
|
|
|
let vulkano_window = app.windows.get_renderer_mut(*window_id).unwrap();
|
|
|
|
vulkano_window.resize();
|
|
|
|
}
|
|
|
|
// Handle mouse position events.
|
|
|
|
WindowEvent::CursorMoved { position, .. } => {
|
2024-02-21 08:30:15 +00:00
|
|
|
*cursor_pos = Vec2::new(position.x as f32, position.y as f32)
|
2023-06-04 07:15:21 +00:00
|
|
|
}
|
|
|
|
// Handle mouse button events.
|
|
|
|
WindowEvent::MouseInput { state, button, .. } => {
|
|
|
|
let mut mouse_pressed = false;
|
|
|
|
if button == &MouseButton::Left && state == &ElementState::Pressed {
|
|
|
|
mouse_pressed = true;
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
2023-06-04 07:15:21 +00:00
|
|
|
if button == &MouseButton::Left && state == &ElementState::Released {
|
|
|
|
mouse_pressed = false;
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
2023-06-04 07:15:21 +00:00
|
|
|
if window_id == &app.windows.primary_window_id().unwrap() {
|
|
|
|
*mouse_pressed_w1 = mouse_pressed;
|
|
|
|
} else {
|
|
|
|
*mouse_pressed_w2 = mouse_pressed;
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
2023-06-04 07:15:21 +00:00
|
|
|
}
|
2022-02-02 16:21:32 +00:00
|
|
|
_ => (),
|
2023-06-17 19:01:50 +00:00
|
|
|
}
|
2023-06-04 07:15:21 +00:00
|
|
|
}
|
|
|
|
false
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_life(
|
|
|
|
app: &mut App,
|
2024-02-21 08:30:15 +00:00
|
|
|
cursor_pos: Vec2,
|
2022-02-02 16:21:32 +00:00
|
|
|
mouse_is_pressed_w1: bool,
|
|
|
|
mouse_is_pressed_w2: bool,
|
|
|
|
) {
|
2022-06-24 15:27:33 +00:00
|
|
|
let primary_window_id = app.windows.primary_window_id().unwrap();
|
2022-02-02 16:21:32 +00:00
|
|
|
for (id, window) in app.windows.iter_mut() {
|
2022-06-24 15:27:33 +00:00
|
|
|
if id == &primary_window_id && !mouse_is_pressed_w1 {
|
2022-02-02 16:21:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-24 15:27:33 +00:00
|
|
|
if id != &primary_window_id && !mouse_is_pressed_w2 {
|
2022-02-02 16:21:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-03-05 18:56:35 +00:00
|
|
|
|
2022-02-02 16:21:32 +00:00
|
|
|
let window_size = window.window_size();
|
|
|
|
let compute_pipeline = &mut app.pipelines.get_mut(id).unwrap().compute;
|
2024-02-21 08:30:15 +00:00
|
|
|
let mut normalized_pos = Vec2::new(
|
2022-12-18 16:23:40 +00:00
|
|
|
(cursor_pos.x / window_size[0]).clamp(0.0, 1.0),
|
|
|
|
(cursor_pos.y / window_size[1]).clamp(0.0, 1.0),
|
2022-02-02 16:21:32 +00:00
|
|
|
);
|
2023-03-05 18:56:35 +00:00
|
|
|
|
|
|
|
// Flip y.
|
2022-02-02 16:21:32 +00:00
|
|
|
normalized_pos.y = 1.0 - normalized_pos.y;
|
2023-07-06 08:52:11 +00:00
|
|
|
let image_extent = compute_pipeline.color_image().image().extent();
|
2024-02-21 08:30:15 +00:00
|
|
|
compute_pipeline.draw_life(IVec2::new(
|
2023-07-06 08:52:11 +00:00
|
|
|
(image_extent[0] as f32 * normalized_pos.x) as i32,
|
|
|
|
(image_extent[1] as f32 * normalized_pos.y) as i32,
|
2022-02-02 16:21:32 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Compute and render per window.
|
2022-02-02 16:21:32 +00:00
|
|
|
fn compute_then_render_per_window(app: &mut App) {
|
2022-06-24 15:27:33 +00:00
|
|
|
let primary_window_id = app.windows.primary_window_id().unwrap();
|
|
|
|
for (window_id, window_renderer) in app.windows.iter_mut() {
|
2022-02-02 16:21:32 +00:00
|
|
|
let pipeline = app.pipelines.get_mut(window_id).unwrap();
|
2022-06-24 15:27:33 +00:00
|
|
|
if *window_id == primary_window_id {
|
|
|
|
compute_then_render(window_renderer, pipeline, [1.0, 0.0, 0.0, 1.0], [0.0; 4]);
|
2022-02-02 16:21:32 +00:00
|
|
|
} else {
|
2022-06-24 15:27:33 +00:00
|
|
|
compute_then_render(window_renderer, pipeline, [0.0, 0.0, 0.0, 1.0], [1.0; 4]);
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
/// Compute game of life, then display result on target image.
|
2022-02-02 16:21:32 +00:00
|
|
|
fn compute_then_render(
|
2022-06-24 15:27:33 +00:00
|
|
|
window_renderer: &mut VulkanoWindowRenderer,
|
2022-02-02 16:21:32 +00:00
|
|
|
pipeline: &mut RenderPipeline,
|
|
|
|
life_color: [f32; 4],
|
|
|
|
dead_color: [f32; 4],
|
|
|
|
) {
|
2023-03-05 18:56:35 +00:00
|
|
|
// Skip this window when minimized.
|
2022-06-24 15:27:33 +00:00
|
|
|
match window_renderer.window_size() {
|
2022-05-09 12:25:26 +00:00
|
|
|
[w, h] => {
|
2022-06-24 15:27:33 +00:00
|
|
|
if w == 0.0 || h == 0.0 {
|
2022-05-09 12:25:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Start the frame.
|
2024-03-27 21:00:56 +00:00
|
|
|
let before_pipeline_future =
|
|
|
|
match window_renderer.acquire(Some(Duration::from_millis(1)), |swapchain_image_views| {
|
|
|
|
pipeline
|
|
|
|
.place_over_frame
|
|
|
|
.recreate_framebuffers(swapchain_image_views)
|
|
|
|
}) {
|
|
|
|
Err(e) => {
|
|
|
|
println!("{e}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok(future) => future,
|
|
|
|
};
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Compute.
|
2022-02-02 16:21:32 +00:00
|
|
|
let after_compute = pipeline
|
|
|
|
.compute
|
|
|
|
.compute(before_pipeline_future, life_color, dead_color);
|
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Render.
|
2022-02-02 16:21:32 +00:00
|
|
|
let color_image = pipeline.compute.color_image();
|
2022-06-24 15:27:33 +00:00
|
|
|
let target_image = window_renderer.swapchain_image_view();
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2024-02-21 08:08:50 +00:00
|
|
|
let after_render = pipeline.place_over_frame.render(
|
|
|
|
after_compute,
|
|
|
|
color_image,
|
|
|
|
target_image,
|
|
|
|
window_renderer.image_index(),
|
|
|
|
);
|
2022-02-02 16:21:32 +00:00
|
|
|
|
2023-03-05 18:56:35 +00:00
|
|
|
// Finish the frame. Wait for the future so resources are not in use when we render.
|
2022-06-24 15:27:33 +00:00
|
|
|
window_renderer.present(after_render, true);
|
2022-02-02 16:21:32 +00:00
|
|
|
}
|