Skip to content

Commit

Permalink
android stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
msiglreith committed Sep 17, 2020
1 parent 1fb9344 commit 991c081
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 57 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,22 @@ gfx-backend-vulkan = { version = "0.5", features = ["x11"] }
cgmath = "0.17"
log = "0.4"
png = "0.16"
winit = { version = "0.22.1", features = ["web-sys"] }
winit = { git = "https://github.com/msiglreith/winit.git", branch = "ndk" }
rand = { version = "0.7.2", features = ["wasm-bindgen"] }
bytemuck = "1"
noise = "0.6.0"
ndk-glue = "0.2"

[[example]]
name="hello-compute"
path="examples/hello-compute/main.rs"
test = true

[[example]]
name="hello_triangle"
path="examples/hello-triangle/main.rs"
crate-type = ["cdylib"]

[patch."https://github.com/gfx-rs/wgpu"]
#wgpu-types = { version = "0.5.0", path = "../wgpu/wgpu-types" }
#wgpu-core = { version = "0.5.0", path = "../wgpu/wgpu-core" }
Expand Down
134 changes: 78 additions & 56 deletions examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::Window,
platform::desktop::EventLoopExtDesktop,
};
use ndk_glue;

async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::TextureFormat) {
async fn run(mut event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::TextureFormat) {
let size = window.inner_size();
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let surface = unsafe { instance.create_surface(&window) };
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default,
// Request an adapter which can render to our surface
compatible_surface: Some(&surface),
compatible_surface: None,
})
.await
.expect("Failed to find an appropiate adapter");
Expand Down Expand Up @@ -77,66 +78,83 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
present_mode: wgpu::PresentMode::Mailbox,
};

let mut swap_chain = device.create_swap_chain(&surface, &sc_desc);
let mut surface = None;
let mut swap_chain = None;

event_loop.run(move |event, _, control_flow| {
// Have the closure take ownership of the resources.
// `event_loop.run` never returns, therefore we must do this to ensure
// the resources are properly cleaned up.
let _ = (
&instance,
&adapter,
&vs_module,
&fs_module,
&pipeline_layout,
);
// Have the closure take ownership of the resources.
// `event_loop.run` never returns, therefore we must do this to ensure
// the resources are properly cleaned up.
let _ = (
&instance,
&adapter,
&vs_module,
&fs_module,
&pipeline_layout,
);

*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
// Recreate the swap chain with the new size
sc_desc.width = size.width;
sc_desc.height = size.height;
swap_chain = device.create_swap_chain(&surface, &sc_desc);
}
Event::RedrawRequested(_) => {
let frame = swap_chain
.get_current_frame()
.expect("Failed to acquire next swap chain texture")
.output;
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: Borrowed(&[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
store: true,
},
}]),
depth_stencil_attachment: None,
});
rpass.set_pipeline(&render_pipeline);
rpass.draw(0..3, 0..1);
match event {
Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
println!("----------------------RUST RESIZE----------------------");
// Recreate the swap chain with the new size
sc_desc.width = size.width;
sc_desc.height = size.height;
swap_chain = Some(device.create_swap_chain(&surface.as_ref().unwrap(), &sc_desc));
}
Event::Resumed =>
{
println!("----------------------RUST RESUME----------------------");
surface = Some(unsafe { instance.create_surface(&window) });
swap_chain = Some(device.create_swap_chain(&surface.as_ref().unwrap(), &sc_desc));
println!("surface: {:?}", surface);
},
Event::Suspended =>
{
swap_chain.take();
surface.take();
},
Event::RedrawRequested(_) => {
if let Some(swap_chain) = swap_chain.as_mut() {
let frame = swap_chain
.get_current_frame()
.expect("Failed to acquire next swap chain texture")
.output;
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
{
let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: Borrowed(&[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &frame.view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::GREEN),
store: true,
},
}]),
depth_stencil_attachment: None,
});
rpass.set_pipeline(&render_pipeline);
rpass.draw(0..3, 0..1);
}

queue.submit(Some(encoder.finish()));
queue.submit(Some(encoder.finish()));
}
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => {}
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
_ => {}
}
});
});
}

fn main() {
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
fn do_main() {
let event_loop = EventLoop::new();
let window = winit::window::Window::new(&event_loop).unwrap();
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -145,7 +163,7 @@ fn main() {
wgpu::util::initialize_default_subscriber(None);

// Temporarily avoid srgb formats for the swapchain on the web
futures::executor::block_on(run(event_loop, window, wgpu::TextureFormat::Bgra8UnormSrgb));
futures::executor::block_on(run(event_loop, window, wgpu::TextureFormat::Rgba8UnormSrgb));
}
#[cfg(target_arch = "wasm32")]
{
Expand All @@ -161,6 +179,10 @@ fn main() {
.ok()
})
.expect("couldn't append canvas to document body");
wasm_bindgen_futures::spawn_local(run(event_loop, window, wgpu::TextureFormat::Bgra8Unorm));
wasm_bindgen_futures::spawn_local(run(event_loop, window, wgpu::TextureFormat::Rgba8Unorm));
}
}

fn main() {
do_main()
}

0 comments on commit 991c081

Please sign in to comment.