Skip to content
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] - Ported WgpuOptions to bevy_render2 #3282

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 11 additions & 17 deletions pipelined/bevy_render2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod camera;
pub mod color;
pub mod mesh;
pub mod options;
pub mod primitives;
pub mod render_asset;
pub mod render_component;
Expand All @@ -26,7 +27,6 @@ use bevy_app::{App, AppLabel, Plugin};
use bevy_asset::{AddAsset, AssetServer};
use bevy_ecs::prelude::*;
use std::ops::{Deref, DerefMut};
use wgpu::Backends;

/// Contains the default Bevy rendering backend based on wgpu.
#[derive(Default)]
Expand Down Expand Up @@ -90,13 +90,12 @@ struct ScratchRenderWorld(World);
impl Plugin for RenderPlugin {
/// Initializes the renderer, sets up the [`RenderStage`](RenderStage) and creates the rendering sub-app.
fn build(&self, app: &mut App) {
let default_backend = if cfg!(not(target_arch = "wasm32")) {
Backends::PRIMARY
} else {
Backends::GL
};
let backends = wgpu::util::backend_bits_from_env().unwrap_or(default_backend);
let instance = wgpu::Instance::new(backends);
let options = app
.world
.get_resource::<options::WgpuOptions>()
.cloned()
.unwrap_or_default();
let instance = wgpu::Instance::new(options.backends);
let surface = {
let world = app.world.cell();
let windows = world.get_resource_mut::<bevy_window::Windows>().unwrap();
Expand All @@ -109,19 +108,14 @@ impl Plugin for RenderPlugin {
let (device, queue) = futures_lite::future::block_on(renderer::initialize_renderer(
&instance,
&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
power_preference: options.power_preference,
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()
},
..Default::default()
label: options.device_label.as_ref().map(|a| a.as_ref()),
features: options.features,
limits: options.limits,
},
));
app.insert_resource(device.clone())
Expand Down
38 changes: 38 additions & 0 deletions pipelined/bevy_render2/src/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::borrow::Cow;
folke marked this conversation as resolved.
Show resolved Hide resolved

pub use wgpu::{Backends, Features, Limits, PowerPreference};

#[derive(Clone)]
pub struct WgpuOptions {
pub device_label: Option<Cow<'static, str>>,
pub backends: Backends,
pub power_preference: PowerPreference,
pub features: Features,
pub limits: Limits,
}

impl Default for WgpuOptions {
fn default() -> Self {
let default_backends = if cfg!(target_arch = "wasm32") {
Backends::GL
} else {
Backends::PRIMARY
};

let backends = wgpu::util::backend_bits_from_env().unwrap_or(default_backends);

let limits = if cfg!(target_arch = "wasm32") {
wgpu::Limits::downlevel_webgl2_defaults()
} else {
wgpu::Limits::default()
};

Self {
device_label: Default::default(),
backends,
power_preference: PowerPreference::HighPerformance,
features: wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES,
limits,
}
}
}