Skip to content
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
26 changes: 23 additions & 3 deletions crates/spirv-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,16 @@ impl Default for ShaderCrateFeatures {
#[cfg_attr(feature = "clap", derive(clap::Parser))]
#[non_exhaustive]
pub struct SpirvBuilder {
/// The path to the shader crate to compile
#[cfg_attr(feature = "clap", clap(skip))]
pub path_to_crate: Option<PathBuf>,
/// The cargo command to run, formatted like `cargo {cargo_cmd} ...`. Defaults to `rustc`.
#[cfg_attr(feature = "clap", clap(skip))]
pub cargo_cmd: Option<String>,
/// Whether the cargo command set in `cargo_cmd` behaves like `cargo rustc` and allows passing args such as
/// `--crate-type dylib`. Defaults to true if `cargo_cmd` is `None` or `Some("rustc")`.
#[cfg_attr(feature = "clap", clap(skip))]
pub cargo_cmd_like_rustc: Option<bool>,
/// Whether to print build.rs cargo metadata (e.g. cargo:rustc-env=var=val). Defaults to [`MetadataPrintout::None`].
/// Within build scripts, set it to [`MetadataPrintout::DependencyOnly`] or [`MetadataPrintout::Full`] to ensure the build script is rerun on code changes.
#[cfg_attr(feature = "clap", clap(skip))]
Expand Down Expand Up @@ -497,6 +505,8 @@ impl Default for SpirvBuilder {
fn default() -> Self {
Self {
path_to_crate: None,
cargo_cmd: None,
cargo_cmd_like_rustc: None,
print_metadata: MetadataPrintout::default(),
release: true,
target: None,
Expand Down Expand Up @@ -995,21 +1005,31 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
.join(target_dir_path)
};

let profile = if builder.release { "release" } else { "dev" };

let mut cargo = cargo_cmd::CargoCmd::new();
if let Some(toolchain) = &builder.toolchain_overwrite {
cargo.arg(format!("+{toolchain}"));
}

let cargo_cmd = builder.cargo_cmd.as_ref().map_or("rustc", |s| s.as_str());
let cargo_cmd_like_rustc = builder.cargo_cmd_like_rustc.unwrap_or(cargo_cmd == "rustc");
let profile = if builder.release { "release" } else { "dev" };
cargo.args([
"build",
cargo_cmd,
"--lib",
"--message-format=json-render-diagnostics",
"-Zbuild-std=core",
"-Zbuild-std-features=compiler-builtins-mem",
"--profile",
profile,
]);
if cargo_cmd_like_rustc {
// About `crate-type`: We use it to determine whether the crate needs to be linked into shaders. For `rlib`,
// we're emitting regular rust libraries as is expected. For `dylib` or `cdylib`, we're linking all `rlib`s
// together, legalize them in many passes and emit a final `*.spv` file. Quirk: If you depend on a crate
// that has crate-type `dylib`, we also link it, and it will fail if it has no shaders, which may not be
// desired. (Gathered from reading source code and experimenting, @firestar99)
cargo.args(["--crate-type", "dylib"]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you include a comment for why we need dylib at all?

}

if let Ok(extra_cargoflags) = tracked_env_var_get("RUSTGPU_CARGOFLAGS") {
cargo.args(extra_cargoflags.split_whitespace());
Expand Down
5 changes: 1 addition & 4 deletions docs/src/writing-shader-crates.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,9 @@ can give to a renderer.

## Writing your first shader

Configure your shader crate as a `"dylib"` type crate, and add `spirv-std` to its dependencies:
Add `spirv-std` to its dependencies:

```toml
[lib]
crate-type = ["dylib"]

[dependencies]
spirv-std = { version = "0.9" }
```
Expand Down
1 change: 1 addition & 0 deletions examples/runners/wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license.workspace = true
repository.workspace = true

[lib]
# required by cargo apk / Android
crate-type = ["lib", "cdylib"]

# See rustc_codegen_spirv/Cargo.toml for details on these features
Expand Down
3 changes: 0 additions & 3 deletions examples/shaders/compute-shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ repository.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib", "lib"]

[dependencies]
spirv-std = { workspace = true }

Expand Down
3 changes: 0 additions & 3 deletions examples/shaders/mouse-shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ repository.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

[dependencies]
shared = { path = "../../shaders/shared" }
spirv-std = { workspace = true }
3 changes: 0 additions & 3 deletions examples/shaders/reduce/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,5 @@ repository.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib", "lib"]

[dependencies]
spirv-std = { workspace = true }
3 changes: 0 additions & 3 deletions examples/shaders/simplest-shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ repository.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

[dependencies]
spirv-std = { workspace = true }
shared = { path = "../shared" }
3 changes: 0 additions & 3 deletions examples/shaders/sky-shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ repository.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["lib", "dylib"]

[dependencies]
shared = { path = "../../shaders/shared" }
spirv-std = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# Common deps
[dependencies]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
name = "workgroup_memory_ash_shader"
crate-type = ["dylib"]

# Common deps
[dependencies]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
name = "workgroup_memory_rust_shader"
crate-type = ["dylib"]

# Common deps
[dependencies]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
name = "abi-vector-layout-rust-gpu"
edition.workspace = true

[lib]
crate-type = ["lib", "dylib"]

[lints]
workspace = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
name = "abi-vector-layout-cuda-rust-gpu"
edition.workspace = true

[lib]
crate-type = ["lib", "dylib"]

[lints]
workspace = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
name = "abi-vector-layout-scalar-math-rust-gpu"
edition.workspace = true

[lib]
crate-type = ["lib", "dylib"]

[lints]
workspace = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# Common deps
[dependencies]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
const_fold_int-const-fold-cpu = { path = "../const-fold-cpu" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
const_fold_int-const-fold-cpu = { path = "../const-fold-cpu" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["rlib", "dylib"]

# GPU deps
[dependencies]
const_fold_int-const-fold-cpu = { path = "../const-fold-cpu" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# Common deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# Common deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

[dependencies]
spirv-std.workspace = true
difftest.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# GPU deps
[dependencies]
spirv-std.workspace = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# Common deps
[dependencies]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ edition.workspace = true
[lints]
workspace = true

[lib]
crate-type = ["dylib"]

# Common deps
[dependencies]

Expand Down