mirror of
https://github.com/bevyengine/bevy
synced 2024-12-23 11:33:06 +00:00
992681b59b
*This PR description is an edited copy of #5007, written by @alice-i-cecile.* # Objective Follow-up to https://github.com/bevyengine/bevy/pull/2254. The `Resource` trait currently has a blanket implementation for all types that meet its bounds. While ergonomic, this results in several drawbacks: * it is possible to make confusing, silent mistakes such as inserting a function pointer (Foo) rather than a value (Foo::Bar) as a resource * it is challenging to discover if a type is intended to be used as a resource * we cannot later add customization options (see the [RFC](https://github.com/bevyengine/rfcs/blob/main/rfcs/27-derive-component.md) for the equivalent choice for Component). * dependencies can use the same Rust type as a resource in invisibly conflicting ways * raw Rust types used as resources cannot preserve privacy appropriately, as anyone able to access that type can read and write to internal values * we cannot capture a definitive list of possible resources to display to users in an editor ## Notes to reviewers * Review this commit-by-commit; there's effectively no back-tracking and there's a lot of churn in some of these commits. *ira: My commits are not as well organized :')* * I've relaxed the bound on Local to Send + Sync + 'static: I don't think these concerns apply there, so this can keep things simple. Storing e.g. a u32 in a Local is fine, because there's a variable name attached explaining what it does. * I think this is a bad place for the Resource trait to live, but I've left it in place to make reviewing easier. IMO that's best tackled with https://github.com/bevyengine/bevy/issues/4981. ## Changelog `Resource` is no longer automatically implemented for all matching types. Instead, use the new `#[derive(Resource)]` macro. ## Migration Guide Add `#[derive(Resource)]` to all types you are using as a resource. If you are using a third party type as a resource, wrap it in a tuple struct to bypass orphan rules. Consider deriving `Deref` and `DerefMut` to improve ergonomics. `ClearColor` no longer implements `Component`. Using `ClearColor` as a component in 0.8 did nothing. Use the `ClearColorConfig` in the `Camera3d` and `Camera2d` components instead. Co-authored-by: Alice <alice.i.cecile@gmail.com> Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: devil-ira <justthecooldude@gmail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
96 lines
3.9 KiB
Rust
96 lines
3.9 KiB
Rust
use std::borrow::Cow;
|
|
|
|
use bevy_ecs::system::Resource;
|
|
pub use wgpu::{Backends, 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 [`WgpuAdapterInfo`](crate::render_resource::WgpuAdapterInfo)
|
|
/// 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, 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(Resource, 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>,
|
|
}
|
|
|
|
impl Default for WgpuSettings {
|
|
fn default() -> Self {
|
|
let default_backends = if cfg!(feature = "webgl") {
|
|
Backends::GL
|
|
} else {
|
|
Backends::PRIMARY
|
|
};
|
|
|
|
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!(feature = "webgl") || 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
|
|
};
|
|
|
|
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,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 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,
|
|
},
|
|
)
|
|
}
|