Skip to content

Commit

Permalink
update winit
Browse files Browse the repository at this point in the history
  • Loading branch information
shiinamiyuki committed Nov 30, 2023
1 parent 57eb645 commit 9ed12f0
Show file tree
Hide file tree
Showing 12 changed files with 315 additions and 254 deletions.
4 changes: 2 additions & 2 deletions luisa_compute/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ parking_lot = "0.12.1"

serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
winit = "0.28.3"
raw-window-handle = "0.5.1"
winit = "0.29.4"
raw-window-handle = "0.6.0"
indexmap = "2.0.0"

luisa_compute_api_types = { path = "../luisa_compute_sys/LuisaCompute/src/rust/luisa_compute_api_types", version = "0.1.1-alpha.1" }
Expand Down
137 changes: 71 additions & 66 deletions luisa_compute/examples/denoiser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use luisa_compute_api_types::StreamTag;
use rand::Rng;
use std::time::Instant;
use winit::event::{Event as WinitEvent, WindowEvent};
use winit::event_loop::EventLoop;
use winit::event_loop::{ControlFlow, EventLoop};

use luisa::lang::types::vector::alias::*;
use luisa::lang::types::vector::*;
Expand Down Expand Up @@ -555,7 +555,7 @@ fn run_pt(device: Device) {
.collect::<Vec<_>>();
seed_img.view(0).copy_from(&seed_buffer);
}
let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let window = winit::window::WindowBuilder::new()
.with_title("Luisa Compute Rust - Ray Tracing")
.with_inner_size(winit::dpi::LogicalSize::new(img_w, img_h))
Expand All @@ -572,72 +572,77 @@ fn run_pt(device: Device) {
3,
);
let display_img = device.create_tex2d::<Float4>(swapchain.pixel_storage(), img_w, img_h, 1);
event_loop.run(move |event, _, control_flow| {
control_flow.set_poll();
match event {
WinitEvent::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
// FIXME: support half4 pixel storage
let mut img_buffer = vec![[0u8; 4]; (img_w * img_h) as usize];
{
let scope = device.default_stream().scope();
scope.submit([display_img.view(0).copy_to_async(&mut img_buffer)]);
event_loop.set_control_flow(ControlFlow::Poll);
event_loop
.run(move |event, elwt| {
match event {
WinitEvent::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
// FIXME: support half4 pixel storage
let mut img_buffer = vec![[0u8; 4]; (img_w * img_h) as usize];
{
let scope = device.default_stream().scope();
scope.submit([display_img.view(0).copy_to_async(&mut img_buffer)]);
}
{
let img = image::RgbImage::from_fn(img_w, img_h, |x, y| {
let i = x + y * img_w;
let px = img_buffer[i as usize];
Rgb([px[0], px[1], px[2]])
});
img.save("cbox.png").unwrap();
}
elwt.exit();
}
{
let img = image::RgbImage::from_fn(img_w, img_h, |x, y| {
let i = x + y * img_w;
let px = img_buffer[i as usize];
Rgb([px[0], px[1], px[2]])
});
img.save("cbox.png").unwrap();
WinitEvent::AboutToWait => {
window.request_redraw();
}
control_flow.set_exit();
}
WinitEvent::MainEventsCleared => {
window.request_redraw();
}
WinitEvent::RedrawRequested(_) => {
let tic = Instant::now();
{
let scope = device.default_stream().scope();
scope.present(&swapchain, &display_img);
scope.submit([
path_tracer.dispatch_async(
[img_w, img_h, 1],
&acc_img,
&albedo_img,
&normal_img,
&seed_img,
&accel,
&Uint2::new(img_w, img_h),
),
acc_to_hdr.dispatch_async([img_w, img_h, 1], &acc_img, &hdr_img),
hdr_img.view(0).copy_to_buffer_async(&color_buf.view(..)),
albedo_img
.view(0)
.copy_to_buffer_async(&albedo_buf.view(..)),
normal_img
.view(0)
.copy_to_buffer_async(&normal_buf.view(..)),
]);
denoiser.execute(true);
scope.submit([
hdr_img.view(0).copy_from_buffer_async(&output_buf.view(..)),
display.dispatch_async([img_w, img_h, 1], &hdr_img, &display_img),
]);
WinitEvent::WindowEvent {
event: WindowEvent::RedrawRequested,
window_id,
} if window_id == window.id() => {
let tic = Instant::now();
{
let scope = device.default_stream().scope();
scope.present(&swapchain, &display_img);
scope.submit([
path_tracer.dispatch_async(
[img_w, img_h, 1],
&acc_img,
&albedo_img,
&normal_img,
&seed_img,
&accel,
&Uint2::new(img_w, img_h),
),
acc_to_hdr.dispatch_async([img_w, img_h, 1], &acc_img, &hdr_img),
hdr_img.view(0).copy_to_buffer_async(&color_buf.view(..)),
albedo_img
.view(0)
.copy_to_buffer_async(&albedo_buf.view(..)),
normal_img
.view(0)
.copy_to_buffer_async(&normal_buf.view(..)),
]);
denoiser.execute(true);
scope.submit([
hdr_img.view(0).copy_from_buffer_async(&output_buf.view(..)),
display.dispatch_async([img_w, img_h, 1], &hdr_img, &display_img),
]);
}
let toc = Instant::now();
let elapsed = (toc - tic).as_secs_f32();
log::info!(
"time: {}ms {}ms/spp",
elapsed * 1e3,
elapsed * 1e3 / SPP_PER_DISPATCH as f32
);
window.request_redraw();
}
let toc = Instant::now();
let elapsed = (toc - tic).as_secs_f32();
log::info!(
"time: {}ms {}ms/spp",
elapsed * 1e3,
elapsed * 1e3 / SPP_PER_DISPATCH as f32
);
window.request_redraw();
_ => (),
}
_ => (),
}
});
})
.unwrap();
}
19 changes: 10 additions & 9 deletions luisa_compute/examples/fluid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {
"cpu"
});

let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let window = winit::window::WindowBuilder::new()
.with_title("Luisa Compute Rust - Fluid")
.with_inner_size(winit::dpi::LogicalSize::new(N_GRID, N_GRID))
Expand Down Expand Up @@ -225,7 +225,6 @@ fn main() {
}),
);


let clear_pressure = Kernel::<fn()>::new_async(&device, &|| {
let idx = index(dispatch_id().xy());
p0.var().write(idx, 0.0f32);
Expand All @@ -244,20 +243,22 @@ fn main() {
);
}),
);

event_loop.run(move |event, _, control_flow| {
control_flow.set_poll();
event_loop.set_control_flow(ControlFlow::Poll);
event_loop.run(move |event, elwt| {
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
elwt.exit();
}
Event::MainEventsCleared => {
Event::AboutToWait => {
window.request_redraw();
}
Event::RedrawRequested(_) => {
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
window_id,
} if window_id == window.id() => {
let tic = Instant::now();
{
let scope = device.default_stream().scope();
Expand Down Expand Up @@ -330,5 +331,5 @@ fn main() {
}
_ => (),
}
});
}).unwrap();
}
63 changes: 31 additions & 32 deletions luisa_compute/examples/mpm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn main() {
let grid_v = device.create_buffer::<f32>(N_GRID * N_GRID * 2);
let grid_m = device.create_buffer::<f32>(N_GRID * N_GRID);

let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let window = winit::window::WindowBuilder::new()
.with_title("Luisa Compute Rust - MPM")
.with_inner_size(winit::dpi::LogicalSize::new(RESOLUTION, RESOLUTION))
Expand Down Expand Up @@ -94,14 +94,14 @@ fn main() {

let clear_grid = Kernel::<fn()>::new(&device, &|| {
let idx = index(dispatch_id().xy());
grid_v.var().write(idx * 2, 0.0f32);
grid_v.var().write(idx * 2 + 1, 0.0f32);
grid_m.var().write(idx, 0.0f32);
grid_v.write(idx * 2, 0.0f32);
grid_v.write(idx * 2 + 1, 0.0f32);
grid_m.write(idx, 0.0f32);
});

let point_to_grid = Kernel::<fn()>::new(&device, &|| {
let p = dispatch_id().x;
let xp = x.var().read(p) / DX;
let xp = x.read(p) / DX;
let base = (xp - 0.5f32).cast_i32();
let fx = xp - base.cast_f32();

Expand All @@ -110,10 +110,9 @@ fn main() {
0.75f32 - (fx - 1.0f32) * (fx - 1.0f32),
0.5f32 * (fx - 0.5f32) * (fx - 0.5f32),
];
let stress = -4.0f32 * DT * E * P_VOL * (J.var().read(p) - 1.0f32) / (DX * DX);
let affine =
Mat2::diag_expr(Float2::expr(stress, stress)) + P_MASS as f32 * C.var().read(p);
let vp = v.var().read(p);
let stress = -4.0f32 * DT * E * P_VOL * (J.read(p) - 1.0f32) / (DX * DX);
let affine = Mat2::diag_expr(Float2::expr(stress, stress)) + P_MASS as f32 * C.read(p);
let vp = v.read(p);
for_unrolled(0..9usize, |ii| {
let (i, j) = (ii % 3, ii / 3);
let offset = Int2::expr(i as i32, j as i32);
Expand All @@ -132,10 +131,10 @@ fn main() {
let i = index(coord);
let v = Var::<Float2>::zeroed();
v.store(Float2::expr(
grid_v.var().read(i * 2u32),
grid_v.var().read(i * 2u32 + 1u32),
grid_v.read(i * 2u32),
grid_v.read(i * 2u32 + 1u32),
));
let m = grid_m.var().read(i);
let m = grid_m.read(i);

v.store(select(m > 0.0f32, v.load() / m, v.load()));
let vx = v.load().x;
Expand All @@ -150,13 +149,13 @@ fn main() {
0.0f32.expr(),
vy,
);
grid_v.var().write(i * 2, vx);
grid_v.var().write(i * 2 + 1, vy);
grid_v.write(i * 2, vx);
grid_v.write(i * 2 + 1, vy);
});

let grid_to_point = Kernel::<fn()>::new(&device, &|| {
let p = dispatch_id().x;
let xp = x.var().read(p) / DX;
let xp = x.read(p) / DX;
let base = (xp - 0.5f32).cast_i32();
let fx = xp - base.cast_f32();

Expand All @@ -176,23 +175,20 @@ fn main() {
let dpos = (offset.cast_f32() - fx) * DX.expr();
let weight = w[i].x * w[j].y;
let idx = index((base + offset).cast_u32());
let g_v = Float2::expr(
grid_v.var().read(idx * 2u32),
grid_v.var().read(idx * 2u32 + 1u32),
);
let g_v = Float2::expr(grid_v.read(idx * 2u32), grid_v.read(idx * 2u32 + 1u32));
new_v.store(new_v.load() + weight * g_v);
new_C.store(new_C.load() + 4.0f32 * weight * g_v.outer_product(dpos) / (DX * DX));
});

v.var().write(p, new_v);
x.var().write(p, x.var().read(p) + new_v.load() * DT);
v.write(p, new_v);
x.write(p, x.read(p) + new_v.load() * DT);
J.var()
.write(p, J.var().read(p) * (1.0f32 + DT * trace(new_C.load())));
C.var().write(p, new_C);
.write(p, J.read(p) * (1.0f32 + DT * trace(new_C.load())));
C.write(p, new_C);
});

let clear_display = Kernel::<fn()>::new(&device, &|| {
display.var().write(
display.write(
dispatch_id().xy(),
Float4::expr(0.1f32, 0.2f32, 0.3f32, 1.0f32),
);
Expand All @@ -201,13 +197,13 @@ fn main() {
let p = dispatch_id().x;
for i in -1..=1 {
for j in -1..=1 {
let pos = (x.var().read(p) * RESOLUTION as f32).cast_i32() + Int2::expr(i, j);
let pos = (x.read(p) * RESOLUTION as f32).cast_i32() + Int2::expr(i, j);
if pos.x >= (0i32)
&& pos.x < (RESOLUTION as i32)
&& pos.y >= (0i32)
&& pos.y < (RESOLUTION as i32)
{
display.var().write(
display.write(
Uint2::expr(pos.x.cast_u32(), RESOLUTION - 1u32 - pos.y.cast_u32()),
Float4::expr(0.4f32, 0.6f32, 0.6f32, 1.0f32),
);
Expand All @@ -216,19 +212,22 @@ fn main() {
}
});
escape!({
event_loop.run(move |event, _, control_flow| {
control_flow.set_poll();
event_loop.set_control_flow(ControlFlow::Poll);
event_loop.run(move |event, elwt| {
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
elwt.exit();
}
Event::MainEventsCleared => {
Event::AboutToWait => {
window.request_redraw();
}
Event::RedrawRequested(_) => {
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
window_id,
} if window_id == window.id() => {
let tic = Instant::now();
{
let scope = device.default_stream().scope();
Expand Down Expand Up @@ -263,6 +262,6 @@ fn main() {
}
_ => (),
}
});
}).unwrap();
});
}
Loading

0 comments on commit 9ed12f0

Please sign in to comment.