
# Objective - Support WebGPU - alternative to #5027 that doesn't need any async / await - fixes #8315 - Surprise fix #7318 ## Solution ### For async renderer initialisation - Update the plugin lifecycle: - app builds the plugin - calls `plugin.build` - registers the plugin - app starts the event loop - event loop waits for `ready` of all registered plugins in the same order - returns `true` by default - then call all `finish` then all `cleanup` in the same order as registered - then execute the schedule In the case of the renderer, to avoid anything async: - building the renderer plugin creates a detached task that will send back the initialised renderer through a mutex in a resource - `ready` will wait for the renderer to be present in the resource - `finish` will take that renderer and place it in the expected resources by other plugins - other plugins (that expect the renderer to be available) `finish` are called and they are able to set up their pipelines - `cleanup` is called, only custom one is still for pipeline rendering ### For WebGPU support - update the `build-wasm-example` script to support passing `--api webgpu` that will build the example with WebGPU support - feature for webgl2 was always enabled when building for wasm. it's now in the default feature list and enabled on all platforms, so check for this feature must also check that the target_arch is `wasm32` --- ## Migration Guide - `Plugin::setup` has been renamed `Plugin::cleanup` - `Plugin::finish` has been added, and plugins adding pipelines should do it in this function instead of `Plugin::build` ```rust // Before impl Plugin for MyPlugin { fn build(&self, app: &mut App) { app.insert_resource::<MyResource> .add_systems(Update, my_system); let render_app = match app.get_sub_app_mut(RenderApp) { Ok(render_app) => render_app, Err(_) => return, }; render_app .init_resource::<RenderResourceNeedingDevice>() .init_resource::<OtherRenderResource>(); } } // After impl Plugin for MyPlugin { fn build(&self, app: &mut App) { app.insert_resource::<MyResource> .add_systems(Update, my_system); let render_app = match app.get_sub_app_mut(RenderApp) { Ok(render_app) => render_app, Err(_) => return, }; render_app .init_resource::<OtherRenderResource>(); } fn finish(&self, app: &mut App) { let render_app = match app.get_sub_app_mut(RenderApp) { Ok(render_app) => render_app, Err(_) => return, }; render_app .init_resource::<RenderResourceNeedingDevice>(); } } ```
108 lines
4.3 KiB
Rust
108 lines
4.3 KiB
Rust
use std::borrow::Cow;
|
|
|
|
pub use wgpu::{
|
|
Backends, Dx12Compiler, Features as WgpuFeatures, Limits as WgpuLimits, PowerPreference,
|
|
};
|
|
|
|
/// Configures the priority used when automatically configuring the features/limits of `wgpu`.
|
|
#[derive(Clone)]
|
|
pub enum WgpuSettingsPriority {
|
|
/// WebGPU default features and limits
|
|
Compatibility,
|
|
/// The maximum supported features and limits of the adapter and backend
|
|
Functionality,
|
|
/// WebGPU default limits plus additional constraints in order to be compatible with WebGL2
|
|
WebGL2,
|
|
}
|
|
|
|
/// Provides configuration for renderer initialization. Use [`RenderDevice::features`](crate::renderer::RenderDevice::features),
|
|
/// [`RenderDevice::limits`](crate::renderer::RenderDevice::limits), and the [`RenderAdapterInfo`](crate::renderer::RenderAdapterInfo)
|
|
/// resource to get runtime information about the actual adapter, backend, features, and limits.
|
|
/// NOTE: [`Backends::DX12`](Backends::DX12), [`Backends::METAL`](Backends::METAL), and
|
|
/// [`Backends::VULKAN`](Backends::VULKAN) are enabled by default for non-web and the best choice
|
|
/// is automatically selected. Web using the `webgl` feature uses [`Backends::GL`](Backends::GL).
|
|
/// NOTE: If you want to use [`Backends::GL`](Backends::GL) in a native app on `Windows` and/or `macOS`, you must
|
|
/// use [`ANGLE`](https://github.com/gfx-rs/wgpu#angle). This is because wgpu requires EGL to
|
|
/// create a GL context without a window and only ANGLE supports that.
|
|
#[derive(Clone)]
|
|
pub struct WgpuSettings {
|
|
pub device_label: Option<Cow<'static, str>>,
|
|
pub backends: Option<Backends>,
|
|
pub power_preference: PowerPreference,
|
|
pub priority: WgpuSettingsPriority,
|
|
/// The features to ensure are enabled regardless of what the adapter/backend supports.
|
|
/// Setting these explicitly may cause renderer initialization to fail.
|
|
pub features: WgpuFeatures,
|
|
/// The features to ensure are disabled regardless of what the adapter/backend supports
|
|
pub disabled_features: Option<WgpuFeatures>,
|
|
/// The imposed limits.
|
|
pub limits: WgpuLimits,
|
|
/// The constraints on limits allowed regardless of what the adapter/backend supports
|
|
pub constrained_limits: Option<WgpuLimits>,
|
|
/// The shader compiler to use for the DX12 backend.
|
|
pub dx12_shader_compiler: Dx12Compiler,
|
|
}
|
|
|
|
impl Default for WgpuSettings {
|
|
fn default() -> Self {
|
|
let default_backends = if cfg!(all(feature = "webgl", target_arch = "wasm32")) {
|
|
Backends::GL
|
|
} else {
|
|
Backends::all()
|
|
};
|
|
|
|
let backends = Some(wgpu::util::backend_bits_from_env().unwrap_or(default_backends));
|
|
|
|
let priority = settings_priority_from_env().unwrap_or(WgpuSettingsPriority::Functionality);
|
|
|
|
let limits = if cfg!(all(feature = "webgl", target_arch = "wasm32"))
|
|
|| matches!(priority, WgpuSettingsPriority::WebGL2)
|
|
{
|
|
wgpu::Limits::downlevel_webgl2_defaults()
|
|
} else {
|
|
#[allow(unused_mut)]
|
|
let mut limits = wgpu::Limits::default();
|
|
#[cfg(feature = "ci_limits")]
|
|
{
|
|
limits.max_storage_textures_per_shader_stage = 4;
|
|
limits.max_texture_dimension_3d = 1024;
|
|
}
|
|
limits
|
|
};
|
|
|
|
let dx12_compiler =
|
|
wgpu::util::dx12_shader_compiler_from_env().unwrap_or(Dx12Compiler::Dxc {
|
|
dxil_path: None,
|
|
dxc_path: None,
|
|
});
|
|
|
|
Self {
|
|
device_label: Default::default(),
|
|
backends,
|
|
power_preference: PowerPreference::HighPerformance,
|
|
priority,
|
|
features: wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES,
|
|
disabled_features: None,
|
|
limits,
|
|
constrained_limits: None,
|
|
dx12_shader_compiler: dx12_compiler,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Get a features/limits priority from the environment variable `WGPU_SETTINGS_PRIO`
|
|
pub fn settings_priority_from_env() -> Option<WgpuSettingsPriority> {
|
|
Some(
|
|
match std::env::var("WGPU_SETTINGS_PRIO")
|
|
.as_deref()
|
|
.map(str::to_lowercase)
|
|
.as_deref()
|
|
{
|
|
Ok("compatibility") => WgpuSettingsPriority::Compatibility,
|
|
Ok("functionality") => WgpuSettingsPriority::Functionality,
|
|
Ok("webgl2") => WgpuSettingsPriority::WebGL2,
|
|
_ => return None,
|
|
},
|
|
)
|
|
}
|