gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
11.51k stars 858 forks source link

Minimal example does not work on NixOS #4792

Open VirgileHenry opened 7 months ago

VirgileHenry commented 7 months ago

Description

I tried running a minimal WGPU example, as well as the hello_triangle example under NixOS. both fails. I have an Nvidia gtx 3050 (driver version 535.113.01) as well as an integrated intel GPU. I tried on my own NixOS setup, as well as on a NixOS fresh install using the gnome / wayland ISO.

Repro steps

Expected vs observed behavior

Expected was the hello_triangle (or my minimal app setup), observed is the following error

Extra materials

The error is the following :

thread 'main' panicked at <my home folder>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-hal-0.18.1/src/gles/egl.rs:751:18:
called `Result::unwrap()` on an `Err` value: BadAlloc
stack backtrace:
   0: rust_begin_unwind
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5
   1: core::panicking::panic_fmt
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1652:5
   3: core::result::Result<T,E>::unwrap
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/result.rs:1077:23
   4: <wgpu_hal::gles::egl::Instance as wgpu_hal::Instance<wgpu_hal::gles::Api>>::init
             at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-hal-0.18.1/src/gles/egl.rs:744:31
   5: wgpu_core::instance::Instance::new::init
             at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-core-0.18.1/src/instance.rs:83:32
   6: wgpu_core::instance::Instance::new
             at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-core-0.18.1/src/instance.rs:114:17
   7: wgpu_core::global::Global<G>::new
             at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-core-0.18.1/src/global.rs:36:23
   8: <wgpu::backend::direct::Context as wgpu::context::Context>::init
             at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.18.0/src/backend/direct.rs:584:14
   9: wgpu::Instance::new
             at /home/eclipse/.cargo/registry/src/index.crates.io-6f17d22bba15001f/wgpu-0.18.0/src/lib.rs:1772:32
  10: morpheus::main
             at ./src/main.rs:12:20
  11: core::ops::function::FnOnce::call_once
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5

Platform I'm running NixOS unstable, but the same have been experienced on NixOS 23.05 wayland / gnome ditribution. This was done with the latest version of wgpu.

cwfitzgerald commented 7 months ago

I'm not really sure how to debug this, I know NixOS is generally pretty hard to get graphical stuff working.

Make sure you have the EGL available, the appropriate mesa drivers, etc.

This is probably EGL looking for something it can't find.

VirgileHenry commented 7 months ago

I managed to make a shell that expose the libs used by WGPU, it woks so far. I'm putting it here in case anyone needs it in the future :

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  nativeBuildInputs = with pkgs; [
    libxkbcommon
    libGL
    # WINIT_UNIX_BACKEND=wayland
    wayland
  ];
  LD_LIBRARY_PATH = "${pkgs.libxkbcommon}/lib:${pkgs.libGL}/lib:${pkgs.wayland}/lib";
  RUST_BACKTRACE=1;
}

I'll keep learning WGPU, and I'll update it if there are other needs. Cheers!

teoxoy commented 7 months ago

It would be worth documenting this in the wiki or on the readme. @cwfitzgerald thoughts?

VirgileHenry commented 7 months ago

This works on my current setup, but I thing I'll need to try it again on a plain NixOS install to check it is a sound solution. I can't do it right now, but I can take the time to do proper testing and creating the minimal nix shell for this.

However, I'm still learning NixOS, so I'm trying my best but I'm unsure I am the best person to do this.

cwfitzgerald commented 7 months ago

Yeah this would be good to document in the wiki once we figure it out!

zh4ngx commented 6 months ago

Struggling with NixOS + wgpu as well. Do you have a good understanding as to why LD_LIBRARY_PATH needs manual intervention?

Also, were you able to get a larger flake.nix that you can share? I'm still working on mine. Seems like this would be a great template for wgpu-based projects.

kaimast commented 5 months ago

Can you try forcing X11? There are still plenty of issues with NVIDIA and Wayland, e.g., #4775.

techieAgnostic commented 1 month ago

Tangentially related but on an Intel Integrated GPU on NixOS, my problem was not having VK_ICD_FILENAMES set properly, I set it to /run/opengl-driver/share/vulkan/icd.d/intel_icd.x86_64.json which worked.

I tried each of the files in that folder to find the one I needed