Skip to content

Commit

Permalink
Get raw window handle only after Resume event
Browse files Browse the repository at this point in the history
  • Loading branch information
Gordon-F committed Jan 11, 2022
1 parent bc49959 commit f887d72
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 35 deletions.
9 changes: 5 additions & 4 deletions crates/bevy_internal/src/default_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ impl PluginGroup for DefaultPlugins {
#[cfg(feature = "bevy_ui")]
group.add(bevy_ui::UiPlugin::default());

#[cfg(feature = "bevy_pbr")]
group.add(bevy_pbr::PbrPlugin::default());
//#[cfg(feature = "bevy_pbr")]
//group.add(bevy_pbr::PbrPlugin::default());

#[cfg(feature = "bevy_gltf")]
group.add(bevy_gltf::GltfPlugin::default());

#[cfg(feature = "bevy_audio")]
#[cfg(all(feature = "bevy_audio", not(target_os = "android")))]
group.add(bevy_audio::AudioPlugin::default());

#[cfg(feature = "bevy_gilrs")]
// Gilrs does not support android platform
#[cfg(all(feature = "bevy_gilrs", not(target_os = "android")))]
group.add(bevy_gilrs::GilrsPlugin::default());
}
}
Expand Down
28 changes: 11 additions & 17 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,21 @@ impl Plugin for RenderPlugin {

if let Some(backends) = options.backends {
let instance = wgpu::Instance::new(backends);
let surface = {
let world = app.world.cell();
let windows = world.get_resource_mut::<bevy_window::Windows>().unwrap();
let raw_handle = windows.get_primary().map(|window| unsafe {
let handle = window.raw_window_handle().get_handle();
instance.create_surface(&handle)
});
raw_handle
};
let request_adapter_options = wgpu::RequestAdapterOptions {
power_preference: options.power_preference,
compatible_surface: surface.as_ref(),
compatible_surface: None,
..Default::default()
};
let (device, queue) = futures_lite::future::block_on(renderer::initialize_renderer(
&instance,
&mut options,
&request_adapter_options,
));
debug!("Configured wgpu adapter Limits: {:#?}", &options.limits);
debug!("Configured wgpu adapter Features: {:#?}", &options.features);
let (device, adapter, queue) = futures_lite::future::block_on(
renderer::initialize_renderer(&instance, &mut options, &request_adapter_options),
);
debug!("Configured wgpu adapter Limits: {:#?}", &adapter.limits());
debug!(
"Configured wgpu adapter Features: {:#?}",
&adapter.features()
);
app.insert_resource(device.clone())
.insert_resource(adapter.clone())
.insert_resource(queue.clone())
.insert_resource(options.clone())
.init_resource::<ScratchRenderWorld>()
Expand Down Expand Up @@ -170,6 +163,7 @@ impl Plugin for RenderPlugin {
.add_stage(RenderStage::Cleanup, SystemStage::parallel())
.insert_resource(instance)
.insert_resource(device)
.insert_resource(adapter)
.insert_resource(queue)
.insert_resource(options)
.insert_resource(render_pipeline_cache)
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Default for WgpuOptions {
let default_backends = if cfg!(feature = "webgl") {
Backends::GL
} else {
Backends::PRIMARY
Backends::all()
};

let backends = Some(wgpu::util::backend_bits_from_env().unwrap_or(default_backends));
Expand Down
9 changes: 6 additions & 3 deletions crates/bevy_render/src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};
use bevy_ecs::prelude::*;
use std::sync::Arc;
use wgpu::{CommandEncoder, Instance, Queue, RequestAdapterOptions};
use wgpu::{Adapter, CommandEncoder, Instance, Queue, RequestAdapterOptions};

/// Updates the [`RenderGraph`] with all of its nodes and then runs it to render the entire frame.
pub fn render_system(world: &mut World) {
Expand Down Expand Up @@ -57,6 +57,8 @@ pub fn render_system(world: &mut World) {
/// This queue is used to enqueue tasks for the GPU to execute asynchronously.
pub type RenderQueue = Arc<Queue>;

pub type RenderAdapter = Arc<Adapter>;

/// The GPU instance is used to initialize the [`RenderQueue`] and [`RenderDevice`],
/// aswell as to create [`WindowSurfaces`](crate::view::window::WindowSurfaces).
pub type RenderInstance = Instance;
Expand All @@ -67,7 +69,7 @@ pub async fn initialize_renderer(
instance: &Instance,
options: &mut WgpuOptions,
request_adapter_options: &RequestAdapterOptions<'_>,
) -> (RenderDevice, RenderQueue) {
) -> (RenderDevice, RenderAdapter, RenderQueue) {
let adapter = instance
.request_adapter(request_adapter_options)
.await
Expand Down Expand Up @@ -103,8 +105,9 @@ pub async fn initialize_renderer(
.await
.unwrap();
let device = Arc::new(device);
let adapter = Arc::new(adapter);
let queue = Arc::new(queue);
(RenderDevice::from(device), queue)
(RenderDevice::from(device), adapter, queue)
}

/// The context with all information required to interact with the GPU.
Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_render/src/view/window.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
render_resource::TextureView,
renderer::{RenderDevice, RenderInstance},
renderer::{RenderAdapter, RenderDevice, RenderInstance},
texture::BevyDefault,
RenderApp, RenderStage, RenderWorld,
};
Expand Down Expand Up @@ -120,6 +120,7 @@ pub fn prepare_windows(
_marker: NonSend<NonSendMarker>,
mut windows: ResMut<ExtractedWindows>,
mut window_surfaces: ResMut<WindowSurfaces>,
render_adapter: Res<RenderAdapter>,
render_device: Res<RenderDevice>,
render_instance: Res<RenderInstance>,
) {
Expand All @@ -133,8 +134,12 @@ pub fn prepare_windows(
render_instance.create_surface(&window.handle.get_handle())
});

let surface_format = surface
.get_preferred_format(&render_adapter)
.unwrap_or(TextureFormat::bevy_default());

let swap_chain_descriptor = wgpu::SurfaceConfiguration {
format: TextureFormat::bevy_default(),
format: surface_format,
width: window.physical_width,
height: window.physical_height,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
Expand Down
11 changes: 6 additions & 5 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl Plugin for WinitPlugin {
.set_runner(winit_runner)
.add_system_to_stage(CoreStage::PostUpdate, change_window.exclusive_system());
let event_loop = EventLoop::new();
#[cfg(not(target_os = "android"))]
handle_initial_window_events(&mut app.world, &event_loop);
app.insert_non_send_resource(event_loop);
}
Expand Down Expand Up @@ -495,12 +496,12 @@ pub fn winit_runner_with(mut app: App) {
active = true;
}
event::Event::MainEventsCleared => {
handle_create_window_events(
&mut app.world,
event_loop,
&mut create_window_event_reader,
);
if active {
handle_create_window_events(
&mut app.world,
event_loop,
&mut create_window_event_reader,
);
app.update();
}
}
Expand Down
10 changes: 7 additions & 3 deletions examples/android/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use bevy::prelude::*;
// the `bevy_main` proc_macro generates the required android boilerplate
#[bevy_main]
fn main() {
std::env::set_var("RUST_LOG", "debug");
std::env::set_var("RUST_BACKTRACE", "full");
App::new()
.insert_resource(Msaa { samples: 2 })
//.insert_resource(Msaa { samples: 2 })
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
Expand All @@ -13,9 +15,10 @@ fn main() {
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
//mut meshes: ResMut<Assets<Mesh>>,
//mut materials: ResMut<Assets<StandardMaterial>>,
) {
/*
// plane
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
Expand All @@ -29,6 +32,7 @@ fn setup(
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
*/
// light
commands.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
Expand Down

0 comments on commit f887d72

Please sign in to comment.