Skip to content
systemcoding.dev
GPU

Request an adapter with Instance::request_adapter in wgpu

Ask wgpu 24 for a high-performance GPU adapter with Instance::request_adapter, handle the Option it returns and print the adapter's name and backend.

2 min read
Summarize in AI

This is the first call in any wgpu program: pick the physical GPU you will use. An adapter represents one GPU on one backend, and requesting one is asynchronous, so a native program drives it with pollster::block_on. The code below is for wgpu 24.

rust
use wgpu::{Instance, PowerPreference, RequestAdapterOptions};

fn main() {
    let instance = Instance::default();
    let adapter = pollster::block_on(instance.request_adapter(&RequestAdapterOptions {
        power_preference: PowerPreference::HighPerformance,
        force_fallback_adapter: false,
        compatible_surface: None,
    }))
    .expect("no suitable GPU adapter");

    let info = adapter.get_info();
    println!("adapter: {} ({:?})", info.name, info.backend);
}
Text
adapter: Apple M2 (Metal)

That output is from a MacBook with an M2; on Linux it names the Vulkan device and on Windows it is usually DX12. The result is an Option in wgpu 24, so None means no adapter matched, and later releases changed it to a Result that carries the reason.

compatible_surface should be set to your window's surface whenever you will draw to one, because on a laptop the adapter that can present to the window may not be the one that was fastest in the abstract. HighPerformance prefers a discrete GPU, LowPower the integrated one, and setting force_fallback_adapter selects a software renderer where one exists, which is useful in CI.

Use instance.enumerate_adapters(Backends::all()) on native targets to list all adapters when you want to let the user choose. That function does not exist in the browser build.

Taggedrustwgpu
Share

More snippets

GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.