Skip to content

Commit

Permalink
Make initialize_adapter_from_env take a compatible surface (#3905)
Browse files Browse the repository at this point in the history
* Make initialize_adapter_from_env take a compatible surface

Add a compatible surface parameter to initialize_adapter_from_env, and
use that to make initialize_adapter_from_env_or_default always respect
its compatible_surface parameter.

---------

Co-authored-by: Andreas Reich <[email protected]>
  • Loading branch information
fornwall and Wumpf authored Jul 10, 2023
1 parent a885840 commit 89f721f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Bottom level categories:
#### Misc Breaking Changes

- Change `AdapterInfo::{device,vendor}` to be `u32` instead of `usize`. By @ameknite in [#3760](https://github.com/gfx-rs/wgpu/pull/3760)
- Remove the `backend_bits` parameter in `initialize_adapter_from_env` and `initialize_adapter_from_env_or_default` - use [InstanceDescriptor::backends](https://docs.rs/wgpu/latest/wgpu/struct.InstanceDescriptor.html#structfield.backends) instead. By @fornwall in [#3904](https://github.com/gfx-rs/wgpu/pull/3904)
- Remove the `backend_bits` parameter in `initialize_adapter_from_env` and `initialize_adapter_from_env_or_default` - use [InstanceDescriptor::backends](https://docs.rs/wgpu/latest/wgpu/struct.InstanceDescriptor.html#structfield.backends) instead. By @fornwall in [#3904](https://github.com/gfx-rs/wgpu/pull/3904)
- Add a `compatible_surface` parameter to `initialize_adapter_from_env` and use that to make `initialize_adapter_from_env_or_default` always respect its `compatible_surface` parameter. By @fornwall in [#3905](https://github.com/gfx-rs/wgpu/pull/3905)

#### Vulkan

Expand Down
18 changes: 15 additions & 3 deletions wgpu/src/util/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ pub fn power_preference_from_env() -> Option<PowerPreference> {

/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable.
#[cfg(not(target_arch = "wasm32"))]
pub fn initialize_adapter_from_env(instance: &Instance) -> Option<Adapter> {
pub fn initialize_adapter_from_env(
instance: &Instance,
compatible_surface: Option<&Surface>,
) -> Option<Adapter> {
let desired_adapter_name = std::env::var("WGPU_ADAPTER_NAME")
.as_deref()
.map(str::to_lowercase)
Expand All @@ -49,6 +52,12 @@ pub fn initialize_adapter_from_env(instance: &Instance) -> Option<Adapter> {
for adapter in adapters {
let info = adapter.get_info();

if let Some(surface) = compatible_surface {
if !adapter.is_surface_supported(surface) {
continue;
}
}

if info.name.to_lowercase().contains(&desired_adapter_name) {
chosen_adapter = Some(adapter);
break;
Expand All @@ -60,7 +69,10 @@ pub fn initialize_adapter_from_env(instance: &Instance) -> Option<Adapter> {

/// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable.
#[cfg(target_arch = "wasm32")]
pub fn initialize_adapter_from_env(_instance: &Instance) -> Option<Adapter> {
pub fn initialize_adapter_from_env(
_instance: &Instance,
_compatible_surface: Option<&Surface>,
) -> Option<Adapter> {
None
}

Expand All @@ -69,7 +81,7 @@ pub async fn initialize_adapter_from_env_or_default(
instance: &Instance,
compatible_surface: Option<&Surface>,
) -> Option<Adapter> {
match initialize_adapter_from_env(instance) {
match initialize_adapter_from_env(instance, compatible_surface) {
Some(a) => Some(a),
None => {
instance
Expand Down

0 comments on commit 89f721f

Please sign in to comment.