tpwrules / nixos-apple-silicon

Resources to install NixOS bare metal on Apple Silicon Macs
MIT License
743 stars 73 forks source link

WGPU Support? #140

Open johnbchron opened 6 months ago

johnbchron commented 6 months ago

Hi, was wondering if WGPU support is possible? I develop with Bevy which uses WGPU, but it only uses the CPU renderer, llvmpipe. As far as I understand we don't have a vulkan driver, so that makes sense. If I add libGL to the LD_LIBRARY_PATH though, I get

2024-01-05T16:52:44.089246Z ERROR log: Handling wgpu errors as fatal by default
thread 'main' panicked at /home/jlewis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.17.2/src/backend/direct.rs:3056:5:
wgpu error: Validation Error

Caused by:
    In Device::create_bind_group_layout
      note: label = `mesh2d_layout`
    Binding 0 entry is invalid
    Downlevel flags DownlevelFlags(VERTEX_STORAGE) are required but not supported on the device.
This is not an invalid use of WebGPU: the underlying API or device does not support enough features to be a fully compliant implementation. A subset of the features can still be used. If you are running this program on native and not in a browser and wish to work around this issue, call Adapter::downlevel_properties or Device::downlevel_properties to get a listing of the features the current platform supports.

I generally have hardware acceleration and the output of glxinfo | grep 'OpenGL renderer string' is OpenGL renderer string: Apple M2 Pro (G14S B1). Not sure what to make of this. My nixos config includes

  hardware.asahi = {
    withRust = true;
    addEdgeKernelConfig = true;
    useExperimentalGPUDriver = true;
    experimentalGPUInstallMode = "replace";
    setupAsahiSound = true;
  };

  hardware.opengl.enable = true;
  hardware.opengl.package = pkgs.lib.mkForce pkgs.mesa-asahi-edge.drivers;

Not sure if I'm doing this config right.

The easiest way to test WGPU is to clone their repo and run their cube example:

cargo run --bin wgpu-examples cube
tpwrules commented 5 months ago

It sounds like you are correctly using hardware acceleration but maybe a feature that is not supported.

How did you get libGL? Are you sure it's pointing to the correct Mesa? If you just used nix-shell or similar it won't be.

See also https://github.com/bevyengine/bevy/issues/8789 .

johnbchron commented 5 months ago

How should I be getting libGL?

tpwrules commented 5 months ago

It has to use the pkgs in your system configuration so that the overlay and/or system.replaceRuntimeDependencies gets applied.

johnbchron commented 5 months ago

If I use "GLOBAL_LIBGL" = "${pkgs.lib.makeLibraryPath pkgs.libGL}"; to leak the one from my system config, the store path in GLOBAL_LIBGL is the same as the one in my devshell. I don't know how to tell if it's pointing to the right mesa.

ingenieroariel commented 4 months ago

This is not strictly related to WGPU on NixOS but touches on WGPU on Asahi so might be useful.

Using the following repo: https://github.com/jinleili/wgpu-in-app

If you do:

WGPU_BACKEND=gl cargo run --features=angle

You get the same error you got DownlevelFlags(VERTEX_STORAGE) https://docs.rs/wgpu/latest/wgpu/struct.DownlevelFlags.html#associatedconstant.SURFACE_VIEW_FORMATS

This leads me to believe that even if you link the right driver it is picking it up in OpenGL mode instead of Vulkan mode.

But if you do:

WGPU_BACKEND=vk cargo run --features=vulkan-portability

It renders correctly on M2 Air with Fedora Asahi 39 and the OpenGL 4.6 driver.

Perhaps try this other repo on NixOS with those flags before going directly to Bevy?

Edit: Forgot to mention that I am using overlay mode in my NixOS machine and can try to reproduce any error you get to rule out a system.replaceRuntimeDependencies issue.