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

Make initialize_adapter_from_env take a compatible surface #3905

Merged
merged 4 commits into from
Jul 10, 2023
Merged
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
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)
Wumpf marked this conversation as resolved.
Show resolved Hide resolved

#### 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