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

Pass InstanceDescriptor by reference and make it clonable #6849

Merged
merged 8 commits into from
Jan 6, 2025
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ There are some limitations to keep in mind with this new functionality:

By @ErichDonGubler in [#6456](https://github.com/gfx-rs/wgpu/pull/6456), [#6148](https://github.com/gfx-rs/wgpu/pull/6148), [#6533](https://github.com/gfx-rs/wgpu/pull/6533), [#6353](https://github.com/gfx-rs/wgpu/pull/6353), [#6537](https://github.com/gfx-rs/wgpu/pull/6537).

#### `wgpu::Instance::new` now takes `InstanceDescriptor` by reference

Previously `wgpu::Instance::new` took `InstanceDescriptor` by value (which is overall fairly uncommon in wgpu).
Furthermore, `InstanceDescriptor` is now cloneable.

```diff
- let instance = wgpu::Instance::new(instance_desc);
+ let instance = wgpu::Instance::new(&instance_desc);
```

By @wumpf in [#6849](https://github.com/gfx-rs/wgpu/pull/6849).

#### New Features

##### Naga
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl DeviceState {
wgpu::Backends::all()
};

let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends: wgpu::util::backend_bits_from_env().unwrap_or(base_backend),
flags: wgpu::InstanceFlags::empty(),
dx12_shader_compiler: wgpu::util::dx12_shader_compiler_from_env()
Expand Down
2 changes: 1 addition & 1 deletion deno_webgpu/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ pub fn op_webgpu_request_adapter(
} else {
state.put(std::sync::Arc::new(wgpu_core::global::Global::new(
"webgpu",
wgpu_types::InstanceDescriptor {
&wgpu_types::InstanceDescriptor {
backends,
flags: wgpu_types::InstanceFlags::from_build_config(),
dx12_shader_compiler: wgpu_types::Dx12Compiler::Fxc,
Expand Down
2 changes: 1 addition & 1 deletion examples/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl ExampleContext {
async fn init_async<E: Example>(surface: &mut SurfaceWrapper, window: Arc<Window>) -> Self {
log::info!("Initializing wgpu...");

let instance = wgpu::Instance::new(wgpu::util::instance_descriptor_from_env());
let instance = wgpu::Instance::new(&wgpu::util::instance_descriptor_from_env());
surface.pre_adapter(&instance, window);

let adapter = get_adapter_with_capabilities_or_from_env(
Expand Down
2 changes: 1 addition & 1 deletion examples/src/hello_triangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
size.width = size.width.max(1);
size.height = size.height.max(1);

let instance = wgpu::Instance::new(wgpu::util::instance_descriptor_from_env());
let instance = wgpu::Instance::new(&wgpu::util::instance_descriptor_from_env());

let surface = instance.create_surface(&window).unwrap();
let adapter = instance
Expand Down
2 changes: 1 addition & 1 deletion examples/src/timestamp_queries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Queries {
async fn run() {
// Instantiates instance of wgpu
let backends = wgpu::util::backend_bits_from_env().unwrap_or_default();
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends,
flags: wgpu::InstanceFlags::from_build_config().with_env(),
dx12_shader_compiler: wgpu::Dx12Compiler::default(),
Expand Down
2 changes: 1 addition & 1 deletion player/src/bin/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn main() {
.build(&event_loop)
.unwrap();

let global = wgc::global::Global::new("player", wgt::InstanceDescriptor::default());
let global = wgc::global::Global::new("player", &wgt::InstanceDescriptor::default());
let mut command_buffer_id_manager = wgc::identity::IdentityManager::new();

#[cfg(feature = "winit")]
Expand Down
2 changes: 1 addition & 1 deletion player/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl Corpus {

let global = wgc::global::Global::new(
"test",
wgt::InstanceDescriptor {
&wgt::InstanceDescriptor {
backends: backend.into(),
flags: wgt::InstanceFlags::debugging(),
dx12_shader_compiler: wgt::Dx12Compiler::Fxc,
Expand Down
2 changes: 1 addition & 1 deletion tests/compile_tests/fail/cpass_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// See #6145 for more info.

fn main() {
let instance = wgpu::Instance::new(Default::default());
let instance = wgpu::Instance::default();
let adapter = pollster::block_on(instance.request_adapter(&Default::default())).unwrap();
let (device, queue) =
pollster::block_on(adapter.request_device(&Default::default(), None)).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/compile_tests/fail/rpass_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// See #6145 for more info.

fn main() {
let instance = wgpu::Instance::new(Default::default());
let instance = wgpu::Instance::default();
let adapter = pollster::block_on(instance.request_adapter(&Default::default())).unwrap();
let (device, queue) =
pollster::block_on(adapter.request_device(&Default::default(), None)).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion tests/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn initialize_instance(backends: wgpu::Backends, force_fxc: bool) -> Instanc
wgpu::util::dx12_shader_compiler_from_env().unwrap_or(wgpu::Dx12Compiler::StaticDxc)
};
let gles_minor_version = wgpu::util::gles_minor_version_from_env().unwrap_or_default();
Instance::new(wgpu::InstanceDescriptor {
Instance::new(&wgpu::InstanceDescriptor {
backends,
flags: wgpu::InstanceFlags::debugging().with_env(),
dx12_shader_compiler,
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/multi-instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

async fn get() -> wgpu::Adapter {
let adapter = {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends: wgpu::util::backend_bits_from_env().unwrap_or_default(),
..Default::default()
});
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct Global {
}

impl Global {
pub fn new(name: &str, instance_desc: wgt::InstanceDescriptor) -> Self {
pub fn new(name: &str, instance_desc: &wgt::InstanceDescriptor) -> Self {
profiling::scope!("Global::new");
Self {
instance: Instance::new(name, instance_desc),
Expand Down
10 changes: 5 additions & 5 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct Instance {
}

impl Instance {
pub fn new(name: &str, instance_desc: wgt::InstanceDescriptor) -> Self {
pub fn new(name: &str, instance_desc: &wgt::InstanceDescriptor) -> Self {
fn init<A: HalApi>(
_: A,
instance_desc: &wgt::InstanceDescriptor,
Expand Down Expand Up @@ -99,13 +99,13 @@ impl Instance {
let mut instance_per_backend = Vec::new();

#[cfg(vulkan)]
init(hal::api::Vulkan, &instance_desc, &mut instance_per_backend);
init(hal::api::Vulkan, instance_desc, &mut instance_per_backend);
#[cfg(metal)]
init(hal::api::Metal, &instance_desc, &mut instance_per_backend);
init(hal::api::Metal, instance_desc, &mut instance_per_backend);
#[cfg(dx12)]
init(hal::api::Dx12, &instance_desc, &mut instance_per_backend);
init(hal::api::Dx12, instance_desc, &mut instance_per_backend);
#[cfg(gles)]
init(hal::api::Gles, &instance_desc, &mut instance_per_backend);
init(hal::api::Gles, instance_desc, &mut instance_per_backend);

Self {
name: name.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion wgpu-info/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct GpuReport {

impl GpuReport {
pub fn generate() -> Self {
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
backends: wgpu::util::backend_bits_from_env().unwrap_or_default(),
flags: wgpu::InstanceFlags::debugging().with_env(),
dx12_shader_compiler: wgpu::util::dx12_shader_compiler_from_env()
Expand Down
2 changes: 1 addition & 1 deletion wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7707,7 +7707,7 @@ pub enum Gles3MinorVersion {
}

/// Options for creating an instance.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct InstanceDescriptor {
/// Which `Backends` to enable.
pub backends: Backends,
Expand Down
4 changes: 2 additions & 2 deletions wgpu/src/api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Default for Instance {
/// If no backend feature for the active target platform is enabled,
/// this method will panic, see [`Instance::enabled_backend_features()`].
fn default() -> Self {
Self::new(InstanceDescriptor::default())
Self::new(&InstanceDescriptor::default())
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ impl Instance {
/// If no backend feature for the active target platform is enabled,
/// this method will panic, see [`Instance::enabled_backend_features()`].
#[allow(unreachable_code)]
pub fn new(_instance_desc: InstanceDescriptor) -> Self {
pub fn new(_instance_desc: &InstanceDescriptor) -> Self {
if Self::enabled_backend_features().is_empty() {
panic!(
"No wgpu backend feature that is implemented for the target platform was enabled. \
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/backend/webgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ impl dispatch::InterfaceTypes for ContextWebGpu {
}

impl dispatch::InstanceInterface for ContextWebGpu {
fn new(_desc: crate::InstanceDescriptor) -> Self
fn new(_desc: &crate::InstanceDescriptor) -> Self
where
Self: Sized,
{
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ impl InterfaceTypes for ContextWgpuCore {
}

impl dispatch::InstanceInterface for ContextWgpuCore {
fn new(desc: wgt::InstanceDescriptor) -> Self
fn new(desc: &wgt::InstanceDescriptor) -> Self
where
Self: Sized,
{
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub trait InterfaceTypes {
}

pub trait InstanceInterface: CommonTraits {
fn new(desc: crate::InstanceDescriptor) -> Self
fn new(desc: &wgt::InstanceDescriptor) -> Self
where
Self: Sized;

Expand Down
5 changes: 3 additions & 2 deletions wgpu/src/util/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ pub async fn is_browser_webgpu_supported() -> bool {
/// this method will panic, see [`Instance::enabled_backend_features()`].
#[allow(unused_mut)]
pub async fn new_instance_with_webgpu_detection(
mut instance_desc: wgt::InstanceDescriptor,
instance_desc: &wgt::InstanceDescriptor,
) -> crate::Instance {
let mut instance_desc = instance_desc.clone();
if instance_desc
.backends
.contains(wgt::Backends::BROWSER_WEBGPU)
Expand All @@ -209,5 +210,5 @@ pub async fn new_instance_with_webgpu_detection(
instance_desc.backends.remove(wgt::Backends::BROWSER_WEBGPU);
}

crate::Instance::new(instance_desc)
crate::Instance::new(&instance_desc)
}
Loading