2021-12-09 21:14:17 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
Make `Resource` trait opt-in, requiring `#[derive(Resource)]` V2 (#5577)
*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>
2022-08-08 21:36:35 +00:00
|
|
|
use bevy_ecs::system::Resource;
|
2021-12-14 03:58:23 +00:00
|
|
|
pub use wgpu::{Backends, Features as WgpuFeatures, Limits as WgpuLimits, PowerPreference};
|
2021-12-09 21:14:17 +00:00
|
|
|
|
2022-02-16 21:17:37 +00:00
|
|
|
/// Configures the priority used when automatically configuring the features/limits of `wgpu`.
|
2022-01-04 20:08:12 +00:00
|
|
|
#[derive(Clone)]
|
2022-02-16 21:17:37 +00:00
|
|
|
pub enum WgpuSettingsPriority {
|
|
|
|
/// WebGPU default features and limits
|
2022-01-04 20:08:12 +00:00
|
|
|
Compatibility,
|
2022-02-16 21:17:37 +00:00
|
|
|
/// The maximum supported features and limits of the adapter and backend
|
2022-01-04 20:08:12 +00:00
|
|
|
Functionality,
|
2022-02-16 21:17:37 +00:00
|
|
|
/// WebGPU default limits plus additional constraints in order to be compatible with WebGL2
|
2022-01-04 20:08:12 +00:00
|
|
|
WebGL2,
|
|
|
|
}
|
|
|
|
|
2022-02-16 21:17:37 +00:00
|
|
|
/// 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.
|
2022-06-25 16:22:28 +00:00
|
|
|
/// 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.
|
Make `Resource` trait opt-in, requiring `#[derive(Resource)]` V2 (#5577)
*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>
2022-08-08 21:36:35 +00:00
|
|
|
#[derive(Resource, Clone)]
|
2022-02-16 21:17:37 +00:00
|
|
|
pub struct WgpuSettings {
|
2021-12-09 21:14:17 +00:00
|
|
|
pub device_label: Option<Cow<'static, str>>,
|
2022-01-08 10:39:43 +00:00
|
|
|
pub backends: Option<Backends>,
|
2021-12-09 21:14:17 +00:00
|
|
|
pub power_preference: PowerPreference,
|
2022-02-16 21:17:37 +00:00
|
|
|
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.
|
2021-12-14 03:58:23 +00:00
|
|
|
pub features: WgpuFeatures,
|
2022-02-13 04:24:52 +00:00
|
|
|
/// The features to ensure are disabled regardless of what the adapter/backend supports
|
|
|
|
pub disabled_features: Option<WgpuFeatures>,
|
2022-02-16 21:17:37 +00:00
|
|
|
/// The imposed limits.
|
2021-12-14 03:58:23 +00:00
|
|
|
pub limits: WgpuLimits,
|
2022-02-13 04:24:52 +00:00
|
|
|
/// The constraints on limits allowed regardless of what the adapter/backend supports
|
|
|
|
pub constrained_limits: Option<WgpuLimits>,
|
2021-12-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 21:17:37 +00:00
|
|
|
impl Default for WgpuSettings {
|
2021-12-09 21:14:17 +00:00
|
|
|
fn default() -> Self {
|
2021-12-29 21:04:28 +00:00
|
|
|
let default_backends = if cfg!(feature = "webgl") {
|
2021-12-09 21:14:17 +00:00
|
|
|
Backends::GL
|
|
|
|
} else {
|
|
|
|
Backends::PRIMARY
|
|
|
|
};
|
|
|
|
|
2022-01-08 10:39:43 +00:00
|
|
|
let backends = Some(wgpu::util::backend_bits_from_env().unwrap_or(default_backends));
|
2021-12-09 21:14:17 +00:00
|
|
|
|
2022-02-16 21:17:37 +00:00
|
|
|
let priority = settings_priority_from_env().unwrap_or(WgpuSettingsPriority::Functionality);
|
2022-01-04 20:08:12 +00:00
|
|
|
|
2022-02-16 21:17:37 +00:00
|
|
|
let limits = if cfg!(feature = "webgl") || matches!(priority, WgpuSettingsPriority::WebGL2)
|
|
|
|
{
|
2021-12-09 21:14:17 +00:00
|
|
|
wgpu::Limits::downlevel_webgl2_defaults()
|
|
|
|
} else {
|
2021-12-14 03:58:23 +00:00
|
|
|
#[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
|
2021-12-09 21:14:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
|
|
|
device_label: Default::default(),
|
|
|
|
backends,
|
|
|
|
power_preference: PowerPreference::HighPerformance,
|
2022-01-04 20:08:12 +00:00
|
|
|
priority,
|
2021-12-09 21:14:17 +00:00
|
|
|
features: wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES,
|
2022-02-13 04:24:52 +00:00
|
|
|
disabled_features: None,
|
2021-12-09 21:14:17 +00:00
|
|
|
limits,
|
2022-02-13 04:24:52 +00:00
|
|
|
constrained_limits: None,
|
2021-12-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 20:08:12 +00:00
|
|
|
|
2022-02-16 21:17:37 +00:00
|
|
|
/// Get a features/limits priority from the environment variable `WGPU_SETTINGS_PRIO`
|
|
|
|
pub fn settings_priority_from_env() -> Option<WgpuSettingsPriority> {
|
2022-01-04 20:08:12 +00:00
|
|
|
Some(
|
2022-02-16 21:17:37 +00:00
|
|
|
match std::env::var("WGPU_SETTINGS_PRIO")
|
2022-01-04 20:08:12 +00:00
|
|
|
.as_deref()
|
|
|
|
.map(str::to_lowercase)
|
|
|
|
.as_deref()
|
|
|
|
{
|
2022-02-16 21:17:37 +00:00
|
|
|
Ok("compatibility") => WgpuSettingsPriority::Compatibility,
|
|
|
|
Ok("functionality") => WgpuSettingsPriority::Functionality,
|
|
|
|
Ok("webgl2") => WgpuSettingsPriority::WebGL2,
|
2022-01-04 20:08:12 +00:00
|
|
|
_ => return None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|