-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Merged by Bors] - WebGL2 support #3039
Changes from 7 commits
80175a5
d852db3
9e6eccd
c2b89d3
52f9f4a
6716a28
20a761c
dbe09fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,8 +33,6 @@ use winit::dpi::LogicalSize; | |
target_os = "netbsd", | ||
target_os = "openbsd" | ||
))] | ||
use winit::platform::unix::EventLoopExtUnix; | ||
|
||
#[derive(Default)] | ||
pub struct WinitPlugin; | ||
|
||
|
@@ -43,6 +41,9 @@ impl Plugin for WinitPlugin { | |
app.init_resource::<WinitWindows>() | ||
.set_runner(winit_runner) | ||
.add_system_to_stage(CoreStage::PostUpdate, change_window.exclusive_system()); | ||
let event_loop = EventLoop::new(); | ||
handle_initial_window_events(&mut app.world, &event_loop); | ||
app.insert_non_send_resource(event_loop); | ||
} | ||
} | ||
|
||
|
@@ -207,21 +208,22 @@ where | |
} | ||
|
||
pub fn winit_runner(app: App) { | ||
winit_runner_with(app, EventLoop::new()); | ||
winit_runner_with(app); | ||
} | ||
|
||
#[cfg(any( | ||
target_os = "linux", | ||
target_os = "dragonfly", | ||
target_os = "freebsd", | ||
target_os = "netbsd", | ||
target_os = "openbsd" | ||
))] | ||
pub fn winit_runner_any_thread(app: App) { | ||
winit_runner_with(app, EventLoop::new_any_thread()); | ||
} | ||
|
||
pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) { | ||
// #[cfg(any( | ||
// target_os = "linux", | ||
// target_os = "dragonfly", | ||
// target_os = "freebsd", | ||
// target_os = "netbsd", | ||
// target_os = "openbsd" | ||
// ))] | ||
// pub fn winit_runner_any_thread(app: App) { | ||
// winit_runner_with(app, EventLoop::new_any_thread()); | ||
// } | ||
|
||
pub fn winit_runner_with(mut app: App) { | ||
let mut event_loop = app.world.remove_non_send::<EventLoop<()>>().unwrap(); | ||
let mut create_window_event_reader = ManualEventReader::<CreateWindow>::default(); | ||
let mut app_exit_event_reader = ManualEventReader::<AppExit>::default(); | ||
app.world.insert_non_send(event_loop.create_proxy()); | ||
|
@@ -525,3 +527,22 @@ fn handle_create_window_events( | |
}); | ||
} | ||
} | ||
|
||
fn handle_initial_window_events(world: &mut World, event_loop: &EventLoop<()>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you do this in the winit_runner_with and keep the EventLoop argument of winit_runner_with? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cart ? (it is your code :) But it seems There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup sadly its an order of operations thing. We need to init these windows before the RenderPlugin is setup, not before the app starts running. |
||
let world = world.cell(); | ||
let mut winit_windows = world.get_resource_mut::<WinitWindows>().unwrap(); | ||
let mut windows = world.get_resource_mut::<Windows>().unwrap(); | ||
let mut create_window_events = world.get_resource_mut::<Events<CreateWindow>>().unwrap(); | ||
let mut window_created_events = world.get_resource_mut::<Events<WindowCreated>>().unwrap(); | ||
for create_window_event in create_window_events.drain() { | ||
let window = winit_windows.create_window( | ||
event_loop, | ||
create_window_event.id, | ||
&create_window_event.descriptor, | ||
); | ||
windows.add(window); | ||
window_created_events.send(WindowCreated { | ||
id: create_window_event.id, | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,15 +84,40 @@ struct ScratchRenderWorld(World); | |
|
||
impl Plugin for RenderPlugin { | ||
fn build(&self, app: &mut App) { | ||
let (instance, device, queue) = | ||
futures_lite::future::block_on(renderer::initialize_renderer( | ||
wgpu::util::backend_bits_from_env().unwrap_or(Backends::PRIMARY), | ||
&wgpu::RequestAdapterOptions { | ||
power_preference: wgpu::PowerPreference::HighPerformance, | ||
..Default::default() | ||
let default_backend = if cfg!(not(target_arch = "wasm32")) { | ||
Backends::PRIMARY | ||
} else { | ||
Backends::GL | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wgpu be changed to default to webgl on wasm? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, it would be great to prefer webgpu over webgl if available (probably). I'll try to figure out how to do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think its worth blocking on this, but I agree that its a "nice to have". |
||
let backends = wgpu::util::backend_bits_from_env().unwrap_or(default_backend); | ||
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 (device, queue) = futures_lite::future::block_on(renderer::initialize_renderer( | ||
&instance, | ||
&wgpu::RequestAdapterOptions { | ||
power_preference: wgpu::PowerPreference::HighPerformance, | ||
compatible_surface: surface.as_ref(), | ||
..Default::default() | ||
}, | ||
&wgpu::DeviceDescriptor { | ||
features: wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES, | ||
#[cfg(not(target_arch = "wasm32"))] | ||
limits: wgpu::Limits::default(), | ||
#[cfg(target_arch = "wasm32")] | ||
limits: wgpu::Limits { | ||
..wgpu::Limits::downlevel_webgl2_defaults() | ||
}, | ||
&wgpu::DeviceDescriptor::default(), | ||
)); | ||
..Default::default() | ||
}, | ||
)); | ||
app.insert_resource(device.clone()) | ||
.insert_resource(queue.clone()) | ||
.init_resource::<ScratchRenderWorld>(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ pub trait BevyDefault { | |
|
||
impl BevyDefault for wgpu::TextureFormat { | ||
fn bevy_default() -> Self { | ||
if cfg!(target_os = "android") { | ||
if cfg!(target_os = "android") || cfg!(target_arch = "wasm32") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use rgba everywhere? Are there any advantages to bgra on other platforms? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remember hearing that a lot of hardware only supports (or prefers) bgra swap chains. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wgpu used to use bgra as a default in their examples, but it looks like at some point they switched to rgba. @kvark is there any reason to default to bgra at this point? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point we can default to RGBA. Originally, we weren't sure if RGBA is really supported everywhere for presentation, as weird as it sounds. |
||
// Bgra8UnormSrgb texture missing on some Android devices | ||
wgpu::TextureFormat::Rgba8UnormSrgb | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the cfg before should be removed also
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bawhaha well that wasn't smart of me