vulkano-rs / vulkano

Safe and rich Rust wrapper around the Vulkan API
Apache License 2.0
4.54k stars 438 forks source link

can't find crate for `vulkano_shaders` #2559

Open nulvinge opened 3 months ago

nulvinge commented 3 months ago

Issue

I'm trying to run one of the examples. I have the repo checked out and run: SHADERC_LIB_DIR=/home/nik/prj/shaderc/install/lib cargo run -v --bin triangle

Which results in this:

     Running `rustc --crate-name triangle --edition=2021 examples/triangle/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=105 --crate-type bin --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=5a98d40c8cfda99c -C extra-filename=-5a98d40c8cfda99c --out-dir /home/nik/prj/vulkano/target/release/deps -L dependency=/home/nik/prj/vulkano/target/release/deps --extern vulkano=/home/nik/prj/vulkano/target/release/deps/libvulkano-6ff585a2bb4978f9.rlib --extern vulkano_shaders=/home/nik/prj/vulkano/target/release/deps/libvulkano_shaders-db6fd8155ed47d7b.so --extern winit=/home/nik/prj/vulkano/target/release/deps/libwinit-83039023fbbc3866.rlib`

error[E0463]: can't find crate for `vulkano_shaders`
   --> examples/triangle/main.rs:306:9
    |
306 |         vulkano_shaders::shader! {
    |         ^^^^^^^^^^^^^^^ can't find crate

I've tried many things, but nothing seems to work. Does anyone know what is going wrong?

marc0246 commented 3 months ago

I suggest making a fresh crate and running it like this:

cargo new shaderc-test && cd shaderc-test && cargo add shaderc && cargo build

Because you aren't going to go far with that diagnostic that Rust outputs when a linker error happens when building a proc macro dependency.

nulvinge commented 3 months ago

Thanks for being so quick to answer, sorry for being so slow.

That succeeded:

cargo new shaderc-test && cd shaderc-test && cargo add shaderc && cargo build
     Created binary (application) `shaderc-test` package
    Updating crates.io index
      Adding shaderc v0.8.3 to dependencies.
             Features:
             - build-from-source
             - prefer-static-linking
    Updating crates.io index
   Compiling shlex v1.3.0
   Compiling xmlparser v0.13.6
   Compiling libc v0.2.158
   Compiling cc v1.1.14
   Compiling roxmltree v0.14.1
   Compiling cmake v0.1.51
   Compiling shaderc-sys v0.8.3
   Compiling shaderc v0.8.3
   Compiling shaderc-test v0.1.0 (/home/nik/prj/shaderc-test)
    Finished dev [unoptimized + debuginfo] target(s) in 2.36s

I'm quite new at rust, so I'm unsure of what exactly is going wrong. But I guess looking into shaderc is a good start?

marc0246 commented 3 months ago

I was expecting that to fail, actually, and show you the errors (which Rust doesn't display when compiling the proc macro). I don't know what to do now.

Oughie commented 3 days ago

Same issue here on NixOS 24.11 (Linux 6.10.9), when following the tutorial:

error[E0463]: can't find crate for `vulkano_shaders`
  --> src/main.rs:47:5
   |
47 | use vulkano_shaders::shader;
   |     ^^^^^^^^^^^^^^^ can't find crate

main.rs

// ...
// LSP does show the crate and its contents.
// Running it throws the error.
use vulkano_shaders::shader; 

mod vs {
    shader! {
        ty: "vertex",
        src: r"
            #version 460

            layout(location = 0) in vec2 position;

            void main() {
                gl_Position = vec4(position, 0.0, 1.0);
            }
        ",
    }
}
// ...

Cargo: 1.82.0 Rust Compiler: 1.82.0

$ rustup default    
stable-x86_64-unknown-linux-gnu (default)

Cargo.toml:

[package]
name = "name"
version = "0.1.0"
edition = "2021"

[dependencies]
vulkano = "0.34.0"
vulkano-shaders = "0.34.0"
# ...

I run $ nix develop as well as the setup script inside the VulkanSDK:

# Copyright (c) 2015-2023 LunarG, Inc.

# source this file into an existing shell to setup your environment.
#
# See docs for in depth documentation:
# https://vulkan.lunarg.com/doc/sdk/latest/linux/getting_started.html

ARCH="$(uname -m)"
VULKAN_SDK="$(dirname "$(readlink -f "${BASH_SOURCE:-$0}" )" )/$ARCH"
export VULKAN_SDK
PATH="$VULKAN_SDK/bin:$PATH"
export PATH
LD_LIBRARY_PATH="$VULKAN_SDK/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH
VK_ADD_LAYER_PATH="$VULKAN_SDK/share/vulkan/explicit_layer.d${VK_ADD_LAYER_PATH:+:$VK_ADD_LAYER_PATH}"
export VK_ADD_LAYER_PATH
if [ -n "${VK_LAYER_PATH-}" ]; then
    echo "Unsetting VK_LAYER_PATH environment variable for SDK usage"
    unset VK_LAYER_PATH
fi

flake.nix:

{
  description = "Vulkan Dev Flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs =
    { self, nixpkgs }@inputs:
    let
      lib = nixpkgs.lib;
      pkgs = import inputs.nixpkgs {
        system = "x86_64-linux";
      };
    in
    {
      devShells.x86_64-linux.default = pkgs.mkShell rec {
        name = "Test Env";

        buildInputs = with pkgs; [
          libxkbcommon
          libGL

          wayland
          cmake

          pkg-config

          xorg.libX11
          xorg.libXrandr
          xorg.libXcursor
          xorg.libXi
          xorg.libXxf86vm

          glfw3
          glslang
          fontconfig
          spirv-tools
          vulkan-volk
          vulkan-tools
          vulkan-loader
          vulkan-headers
          vulkan-validation-layers
          vulkan-tools-lunarg
          vulkan-extension-layer
        ];

        LD_LIBRARY_PATH = "${lib.makeLibraryPath buildInputs}";
        VK_LAYER_PATH = "${pkgs.vulkan-validation-layers}/share/vulkan/explicit_layer.d";
        VULKAN_SDK = "${pkgs.vulkan-validation-layers}/share/vulkan/";
      };
    };
}
marc0246 commented 3 days ago

@Oughie vulkano-shaders needs shaderc. I'm not seeing shaderc in your flake.

Oughie commented 3 days ago

@marc0246 Thank you, that did it for me ^^

happenslol commented 1 day ago

@Oughie Hey, can you post the complete flake you're using to build/run a vulkano project on NixOS? Sounds like you got it working in the end. I've been able to make it work building shaderc from source, but not using shaderc from nixpkgs.

Oughie commented 21 hours ago

@happenslol I adjusted the flake.nix by adding the shaderc package. (see below)

For some reason, only this specific order works for me:

nix develop
source setup-env.sh # inside the VulkanSDK directory
# Compile other dependencies until the end/source code is reached (-> "vulkano_shaders" error occurs)
cargo build 
nix develop
# Compile the remaining source code, including the "vulkano_shaders"-part
cargo  build

flake.nix:

{
  description = "Vulkan Dev Flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };

  outputs =
    { self, nixpkgs }@inputs:
    let
      lib = nixpkgs.lib;
      pkgs = import inputs.nixpkgs {
        system = "x86_64-linux";
      };
    in
    {
      devShells.x86_64-linux.default = pkgs.mkShell rec {
        name = "Test Env";

        buildInputs = with pkgs; [
          libxkbcommon
          libGL

          wayland
          cmake
          shaderc # NEW

          pkg-config

          xorg.libX11
          xorg.libXrandr
          xorg.libXcursor
          xorg.libXi
          xorg.libXxf86vm

          glfw3
          glslang
          fontconfig
          spirv-tools
          vulkan-volk
          vulkan-tools
          vulkan-loader
          vulkan-headers
          vulkan-validation-layers
          vulkan-tools-lunarg
          vulkan-extension-layer
        ];

        LD_LIBRARY_PATH = "${lib.makeLibraryPath buildInputs}";
        VK_LAYER_PATH = "${pkgs.vulkan-validation-layers}/share/vulkan/explicit_layer.d";
        VULKAN_SDK = "${pkgs.vulkan-validation-layers}/share/vulkan/";
      };
    };
}
marc0246 commented 11 hours ago

@Oughie May I ask why run that VulkanSDK shell script? Seems unnecessary given that your flake can (and already does) set up the environment variables. But also, how does it work without setting shaderc (the crate)'s SHADERC_LIB_DIR environment variable? My impression is that NixOS doesn't have standard paths and so the crate won't be able to find your shaderc system library to link against. The only other option is that it builds shaderc from source, which takes millenia.

Oughie commented 57 minutes ago

@marc0246 If I don't run the VulkanSDK script, the following error occurs:

error: failed to run custom build command for `shaderc-sys v0.8.3`

Caused by:
  process didn't exit successfully: `/some/path/target/debug/build/shaderc-sys-cee44ea4f2edcc8c/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at /some/path/.cargo/registry/src/index.crates.io-6f17d22bba15001f/shaderc-sys-0.8.3/build/build.rs:205:59:
  called `Result::unwrap()` on an `Err` value: "could not read vk.xml in $VULKAN_SDK: No such file or directory (os error 2)"
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Running $ source ~/Downloads/VulkanSDK/1.3.296.0/setup-env.sh and it doesn't occur anymore.

But I found out adding the following line to my flake.nix fixes this as well as the original one:

SHADERC_LIB_DIR = "${pkgs.shaderc}/libs";
marc0246 commented 32 minutes ago

Oh, I didn't know shaderc (the crate) searched using the VULKAN_SDK environment variable as well. But yeah, the safest bet is probably to set SHADERC_LIB_DIR. At least I've never done it differently as I've never downloaded the Vulkan SDK manually or ran any script like that. NixOS unstable and Arch usually have very up-to-date packages after all.