Skip to content

Commit 307d0da

Browse files
authored
Add spirv-tools and spirv-tools-sys crates to wrap usage of spirv-tools (#179)
* Add spirv-headers and spirv-tools as submodules * Add simple generator and the generated code needed for compilation * Add first pass on spirv-tools-sys * Add first pass on spirv-tools * Replace invocation of spirv-opt with spirv-tools crate * Use C++11 * Placate clippy * Add validation, replacing spirv-val with the spirv-tools crate * Fix MSVC warning * Use patched spirv-tools * Fixup metadata * Add same compiler flags as "official" build scripts * Update spirv-tools and generated files * Fixup * Add assembler and example * Use assembler in tests * Oops, fix macos TARGET_OS * write -> write_all * Start splitting spirv-tools into a compiled vs tool feature set * Checkpointing * Checkpoint * Boop * Get tests to work both with installed and compiled tools * Cleanup CI config * Splits steps to clearly show how long each part of a longer (eg test) step actually takes * Label all steps * Explicitly disable submodule checkout * Rustfmt * Rename features for consistency and fix clippy warnings * Split "core" crates from examples * Add run_clippy bash script * Add test script * Remove x flag * Newline * Actually print out errors from running val/opt * Revert drive-by import merging * Change intro to take the changes this PR has into account * Actually run tests on Windows * Fetch only the host target to reduce fetch times * Add more info when a spirv tool returns a non-zero exit code * Rustfmt * Switch tool assembler to use files to see if it fixes windows * Use files for input and output for now until I can figure out Windows being dumb * Fix API docs generation * Compile and use C++ code to check Windows issue * Return to use installed tools
1 parent 5aa7453 commit 307d0da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+6514
-177
lines changed

.github/workflows/ci.yaml

+44-12
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,40 @@ jobs:
1111
name: Test
1212
strategy:
1313
matrix:
14-
os: [macos-latest, ubuntu-latest, windows-latest]
14+
os: [macOS-latest, ubuntu-latest, windows-latest]
15+
include:
16+
- os: ubuntu-latest
17+
target: x86_64-unknown-linux-gnu
18+
- os: windows-latest
19+
target: x86_64-pc-windows-msvc
20+
- os: macOS-latest
21+
target: x86_64-apple-darwin
1522
runs-on: ${{ matrix.os }}
1623
env:
1724
spirv_tools_version: "20200928"
1825
steps:
26+
# Note that we are explicitly NOT checking out submodules, to validate
27+
# that we haven't accidentally enabled spirv-tools native compilation
28+
# and regressed CI times
1929
- uses: actions/checkout@v2
30+
with:
31+
submodules: "false"
2032
# Ubuntu does have `brew install spirv-tools`, but it installs from
2133
# source and so takes >8 minutes.
2234
- if: ${{ runner.os == 'Linux' }}
35+
name: Linux - Install native dependencies
2336
run: |
2437
sudo apt install libwayland-cursor0 libxkbcommon-dev libwayland-dev
2538
mkdir "${HOME}/spirv-tools"
2639
curl -fL https://storage.googleapis.com/spirv-tools/artifacts/prod/graphics_shader_compiler/spirv-tools/linux-clang-release/continuous/1305/20201026-063148/install.tgz | tar -xz -C "${HOME}/spirv-tools"
2740
echo "${HOME}/spirv-tools/install/bin" >> $GITHUB_PATH
2841
- if: ${{ runner.os == 'macOS' }}
42+
name: Mac - Install spirv-tools
2943
run: brew install spirv-tools
3044
# Currently SPIR-V tools aren't available in any package manager
3145
# on Windows that put the tools in the path.
3246
- if: ${{ runner.os == 'Windows' }}
47+
name: Windows - Install spirv-tools
3348
shell: bash
3449
run: |
3550
tmparch=$(mktemp)
@@ -39,25 +54,42 @@ jobs:
3954
- if: ${{ runner.os == 'Windows' }}
4055
# Runs separately to add spir-v tools to Powershell's Path.
4156
run: echo "$HOME/spirv-tools/install/bin" >> $env:GITHUB_PATH
42-
- run: rustup component add rust-src rustc-dev llvm-tools-preview
43-
# See: https://github.com/EmbarkStudios/rust-gpu/issues/84
44-
- if: ${{ runner.os == 'macOS' }}
45-
run: cargo test --workspace --exclude example-runner
46-
- if: ${{ runner.os != 'macOS' }}
47-
run: cargo test --workspace
57+
- name: Install rustup components
58+
run: rustup component add rust-src rustc-dev llvm-tools-preview
59+
# Fetch dependencies in a separate step to clearly show how long each part
60+
# of the testing takes
61+
- name: cargo fetch
62+
run: cargo fetch --target ${{ matrix.target }}
63+
- name: Run tests
64+
shell: bash
65+
run: .github/workflows/test.sh
66+
4867
lint:
4968
name: Lint
5069
runs-on: ubuntu-latest
5170
steps:
71+
# Note that we are explicitly NOT checking out submodules, to validate
72+
# that we haven't accidentally enabled spirv-tools native compilation
73+
# and regressed CI times
5274
- uses: actions/checkout@v2
53-
- run: sudo apt install libwayland-cursor0 libxkbcommon-dev libwayland-dev
54-
- run: |
75+
with:
76+
submodules: "false"
77+
- name: Install native dependencies
78+
run: sudo apt install libwayland-cursor0 libxkbcommon-dev libwayland-dev
79+
- name: Install spirv-tools
80+
run: |
5581
mkdir "${HOME}/spirv-tools"
5682
curl -fL https://storage.googleapis.com/spirv-tools/artifacts/prod/graphics_shader_compiler/spirv-tools/linux-clang-release/continuous/1305/20201026-063148/install.tgz | tar -xz -C "${HOME}/spirv-tools"
5783
echo "${HOME}/spirv-tools/install/bin" >> $GITHUB_PATH
58-
- run: rustup component add rustfmt clippy rust-src rustc-dev llvm-tools-preview
59-
- run: cargo fmt --all -- --check
60-
- run: cargo clippy --workspace --all-targets -- -D warnings
84+
- name: Install rustup components
85+
run: rustup component add rustfmt clippy rust-src rustc-dev llvm-tools-preview
86+
- name: Rustfmt
87+
run: cargo fmt --all -- --check
88+
- name: cargo fetch
89+
run: cargo fetch
90+
- name: Clippy
91+
run: .github/workflows/clippy.sh
92+
6193
cargo-deny:
6294
runs-on: ubuntu-latest
6395
steps:

.github/workflows/clippy.sh

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
if [[ -z "${CI}" ]]; then
5+
FEAT="use-compiled-tools"
6+
else
7+
FEAT="use-installed-tools"
8+
fi
9+
10+
function clippy() {
11+
echo ::group::"$1"
12+
cargo clippy \
13+
--manifest-path "$1/Cargo.toml" \
14+
--no-default-features \
15+
--features "$FEAT" \
16+
--all-targets \
17+
-- -D warnings
18+
echo ::endgroup::
19+
}
20+
21+
function clippy_no_features() {
22+
echo ::group::"$1"
23+
cargo clippy \
24+
--manifest-path "$1/Cargo.toml" \
25+
--all-targets \
26+
-- -D warnings
27+
echo ::endgroup::
28+
}
29+
30+
# Core crates
31+
clippy spirv-tools-sys
32+
clippy spirv-tools
33+
clippy rustc_codegen_spirv
34+
clippy spirv-builder
35+
36+
# Examples
37+
clippy examples/example-runner
38+
clippy examples/wgpu-example-runner
39+
40+
clippy_no_features examples/example-runner-cpu
41+
clippy_no_features examples/example-shader
42+
clippy_no_features examples/wgpu-example-shader

.github/workflows/deploy_docs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- run: brew install mdbook spirv-tools
1414
- run: mkdir docs-build/
1515
- run: $(cd docs && mdbook build -d ../docs-build/book)
16-
- run: cargo doc
16+
- run: .github/workflows/docs.sh
1717
- run: mv target/doc docs-build/api
1818
- uses: JamesIves/[email protected]
1919
with:

.github/workflows/docs.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
if [[ -z "${CI}" ]]; then
5+
FEAT="use-compiled-tools"
6+
else
7+
FEAT="use-installed-tools"
8+
fi
9+
10+
function doc() {
11+
echo ::group::"$1"
12+
cargo doc \
13+
--manifest-path "$1/Cargo.toml" \
14+
--no-default-features \
15+
--features "$FEAT"
16+
echo ::endgroup::
17+
}
18+
19+
# Core crates only!
20+
doc spirv-tools-sys
21+
doc spirv-tools
22+
doc rustc_codegen_spirv
23+
doc spirv-builder

.github/workflows/test.sh

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
if [[ -z "${CI}" ]]; then
5+
FEAT="use-compiled-tools"
6+
else
7+
FEAT="use-installed-tools"
8+
fi
9+
10+
os=$1
11+
12+
function cargo_test() {
13+
echo ::group::"$1 build"
14+
cargo test \
15+
--manifest-path "$1/Cargo.toml" \
16+
--no-default-features \
17+
--features "$FEAT" \
18+
--no-run
19+
echo ::endgroup::
20+
21+
echo ::group::"$1 test"
22+
cargo test \
23+
--manifest-path "$1/Cargo.toml" \
24+
--no-default-features \
25+
--features "$FEAT"
26+
echo ::endgroup::
27+
}
28+
29+
function cargo_test_no_features() {
30+
echo ::group::"$1 build"
31+
cargo test --manifest-path "$1/Cargo.toml" --no-run
32+
echo ::endgroup::
33+
34+
echo ::group::"$1 test"
35+
cargo test --manifest-path "$1/Cargo.toml"
36+
echo ::endgroup::
37+
}
38+
39+
# Core crates
40+
cargo_test spirv-tools-sys
41+
cargo_test spirv-tools
42+
cargo_test rustc_codegen_spirv
43+
cargo_test spirv-builder
44+
45+
# Examples
46+
# See: https://github.com/EmbarkStudios/rust-gpu/issues/84
47+
if [[ -z "${CI}" && "$os" != "macOS" ]]; then
48+
cargo_test examples/example-runner
49+
fi
50+
51+
cargo_test examples/wgpu-example-runner
52+
53+
cargo_test_no_features examples/example-runner-cpu
54+
cargo_test_no_features examples/example-shader
55+
cargo_test_no_features examples/wgpu-example-shader

.gitmodules

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[submodule "spirv-tools-sys/spirv-headers"]
2+
path = spirv-tools-sys/spirv-headers
3+
url = https://github.com/KhronosGroup/SPIRV-Headers.git
4+
[submodule "spirv-tools-sys/spirv-tools"]
5+
path = spirv-tools-sys/spirv-tools
6+
url = https://github.com/EmbarkStudios/SPIRV-Tools.git
7+
branch = patch-to-string

Cargo.lock

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ members = [
55
"examples/wgpu-example-runner",
66
"examples/example-shader",
77
"examples/wgpu-example-shader",
8+
89
"rustc_codegen_spirv",
910
"spirv-builder",
1011
"spirv-std",
12+
"spirv-tools",
13+
"spirv-tools-sys",
1114
]

docs/src/introduction.md

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
# Introduction
2+
23
Welcome to the Rust-GPU dev guide! This documentation is meant for documenting
34
how to use and develop on Rust-GPU.
45

56
## Getting started
67

78
1. Clone the repository.
89

10+
```shell
11+
git clone --recurse-submodules https://github.com/EmbarkStudios.com/rust-gpu
12+
```
13+
914
1. Install the prerequisites using the provided setup script. From the root of the project, run:
1015

1116
MacOS, Linux:
17+
1218
```shell
1319
sh setup.sh
1420
```
1521

1622
Windows:
17-
```
23+
24+
```shell
1825
setup.bat
1926
```
2027

2128
The setup script installs nightly Rust (required for now, see [#78](https://github.com/EmbarkStudios/rust-gpu/issues/78) for tracking issue).
2229

23-
1. Install [SPIRV-Tools](https://github.com/KhronosGroup/SPIRV-Tools#downloads) and add it to your PATH (for now, eventually we will automatically build and link it instead of calling executables)
30+
1. **optional** Install [SPIRV-Tools](https://github.com/KhronosGroup/SPIRV-Tools#downloads) and add it to your `PATH`. You can skip this step if you just want to run examples with the defaults. See [Using installed SPIRV-Tools](#using-installed-spirv-tools) if you decide to go with this option.
2431

2532
1. Next, look at the [examples](examples) folder. There are two projects here: [examples/example-shader](examples/example-shader) and [examples/example-runner](examples/example-runner). The example-shader project is a "GPU crate", one that will be compiled to a SPIR-V module. The example-runner project is a normal, CPU crate that uses vulkan to consume the example-shader SPIR-V module to display a shader.
2633

@@ -36,15 +43,13 @@ how to use and develop on Rust-GPU.
3643

3744
Be aware that this project is in a very early phase - if the above doesn't work, please [file an issue](https://github.com/EmbarkStudios/rust-gpu/issues)!
3845
39-
## Getting started, for power users who don't want to use spirv-builder.
46+
## Getting started, for power users who don't want to use spirv-builder
4047

4148
If you would like to build the compiler, `rustc_codegen_spirv` is the relevant folder. Install the prerequisites, as above, then, `cd rustc_codegen_spirv && cargo build`. This produces an .so file, located at `./target/debug/librustc_codegen_spirv.so` (or `.dll`/`.dylib` depending on your platform).
4249

4350
This file is a dynamically loaded backend for rustc - you may tell rustc to use it as a backend through the `-Z codegen-backend=...` flag. To pass this to rustc through cargo, set the environment variable `RUSTFLAGS="-Z codegen-backend=$PATH_TO_FILE"`.
4451

45-
Then, when building a GPU crate, we need to configure some flags when we call cargo. First, we need to build libcore
46-
ourselves - we obviously have no SPIR-V libcore installed on our system! Use the flag `-Z build-std=core`. Then, we need
47-
to tell rustc to generate SPIR-V instead of x86 code: `--target spirv-unknown-unknown`.
52+
Then, when building a GPU crate, we need to configure some flags when we call cargo. First, we need to build libcore ourselves - we obviously have no SPIR-V libcore installed on our system! Use the flag `-Z build-std=core`. Then, we need to tell rustc to generate SPIR-V instead of x86 code: `--target spirv-unknown-unknown`.
4853

4954
Overall, building your own SPIR-V crate looks like:
5055

@@ -61,3 +66,15 @@ To create a GPU crate, look at the [examples/example-shader](examples/example-sh
6166
6267
This is all a little convoluted, hence the [spirv-builder](spirv-builder) crate handles a lot of this.
6368
69+
## Using installed SPIRV-Tools
70+
71+
By default, all of the crates and examples in this repo will compile the `spirv-tools-sys` crate, including a lot of C++ code from [SPIRV-Tools](https://github.com/EmbarkStudios/SPIRV-Tools). If you don't want to build the C++ code because you already have [SPIRV-Tools](https://github.com/KhronosGroup/SPIRV-Tools#downloads) installed, or just don't want to spend more time compiling, you can build/run the crate with the `use-installed-tools` feature.
72+
73+
```shell
74+
cargo run \
75+
--manifest-path examples/example-runner/Cargo.toml \
76+
--features use-installed-tools \
77+
--no-default-features
78+
```
79+
80+
You should see `warning: use-installed-tools feature on, skipping compilation of C++ code` during the compilation, but otherwise the build will function just the same as if you compiled the C++ code, with the exception that it will fail if you don't have SPIRV-Tools installed correctly.

examples/example-runner-cpu/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ spirv-std = { path = "../../spirv-std" }
1414

1515
# for parallelism, not really needed though
1616
rayon = "1.5"
17-

examples/example-runner/Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ authors = ["Embark <[email protected]>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"
77

8+
# See rustc_codegen_spirv/Cargo.toml for details on these features
9+
[features]
10+
default = ["use-compiled-tools"]
11+
use-installed-tools = ["spirv-builder/use-installed-tools"]
12+
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
13+
814
[dependencies]
915
ash = "0.31"
1016
ash-window = "0.5"
@@ -16,4 +22,4 @@ winit = "0.23.0"
1622
ash-molten = { git = "https://github.com/EmbarkStudios/ash-molten", branch = "moltenvk-1.1.0" }
1723

1824
[build-dependencies]
19-
spirv-builder = { path = "../../spirv-builder" }
25+
spirv-builder = { path = "../../spirv-builder", default-features = false }

examples/wgpu-example-runner/Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ authors = ["Embark <[email protected]>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"
77

8+
# See rustc_codegen_spirv/Cargo.toml for details on these features
9+
[features]
10+
default = ["use-compiled-tools"]
11+
use-installed-tools = ["spirv-builder/use-installed-tools"]
12+
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
13+
814
[dependencies]
915
wgpu = "0.6.0"
1016
futures = { version = "0.3", default-features = false, features = ["std", "executor"] }
1117
winit = { version = "0.22.1", features = ["web-sys"] }
1218

1319
[build-dependencies]
14-
spirv-builder = { path = "../../spirv-builder" }
20+
spirv-builder = { path = "../../spirv-builder", default-features = false }
1521

1622
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
1723
wgpu-subscriber = "0.1.0"

0 commit comments

Comments
 (0)