Skip to content

Commit ce0487c

Browse files
authored
feat: use cargo-gpu instead of out-of-workspace shaders binary (#138)
* feat: use cargo-gpu instead of out-of-workspace shaders binary * add shaders and shaders cargo alias, github workflow * ensure CI has nightly * ensure rustup components * ensure CI has nightly and components * web linkage on wasm32 only * generate with cargo shaders * gen shaders * install toolchain * shaders workflow * readme
1 parent b237d42 commit ce0487c

File tree

99 files changed

+7516
-1762
lines changed

Some content is hidden

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

99 files changed

+7516
-1762
lines changed

.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[alias]
2+
shaders = "gpu build --output-dir crates/renderling/shaders --shader-crate crates/renderling/ --shader-manifest crates/renderling/shaders/manifest.json"
3+
14
[build]
25
rustflags = ["--cfg=web_sys_unstable_apis"]
36
rustdocflags = ["--cfg=web_sys_unstable_apis"]

.github/workflows/push.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ jobs:
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v2
18-
- uses: moonrepo/setup-rust@v1
19-
- uses: Swatinem/rust-cache@v2
18+
- uses: actions/cache@v4
2019
with:
21-
workspaces: shaders
22-
# ensure the shader binaries were properly checked in
23-
- run: rm -rf crates/renderling/src/linkage/*.spv
24-
- run: cd shaders && cargo run --release && cd ..
25-
- run: git diff --exit-code --no-ext-diff crates/renderling/src/linkage
20+
path: ~/.cache/rust-gpu
21+
key: ${{ runner.os }}
22+
- uses: moonrepo/setup-rust@v1
23+
- run: rustup toolchain add nightly-2024-04-24
24+
- run: rustup component add --toolchain nightly-2024-04-24 rust-src rustc-dev llvm-tools
25+
- run: cargo install --git https://github.com/rust-gpu/cargo-gpu
26+
- run: rm -rf crates/renderling/src/linkage/* crates/renderling/shaders
27+
- run: RUST_LOG=trace cargo shaders
28+
- run: cargo build -p renderling
29+
- run: git diff --exit-code --no-ext-diff
2630

2731
renderling-clippy:
2832
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/target/
55
shaders/target
66
shaders/shader-crate/target
7+
**/spirv-manifest.json
78

89
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
910
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html

Cargo.lock

Lines changed: 17 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ glam = { version = "0.24.2", default-features = false }
2626
gltf = { version = "1.4,1", features = ["KHR_lights_punctual", "KHR_materials_unlit", "KHR_materials_emissive_strength", "extras", "extensions"] }
2727
image = "0.24"
2828
log = "0.4"
29-
naga = { version = "0.19", features = ["spv-in", "wgsl-out", "wgsl-in", "msl-out"] }
29+
naga = { version = "22.1.0", features = ["spv-in", "wgsl-out", "wgsl-in", "msl-out"] }
3030
pretty_assertions = "1.4.0"
3131
proc-macro2 = { version = "1.0", features = ["span-locations"] }
3232
rustc-hash = "1.1"
@@ -40,6 +40,7 @@ web-sys = "0.3"
4040
winit = { version = "0.30" }
4141
wgpu = "22.1.0"
4242

43+
4344
[profile.dev]
4445
opt-level = 1
4546

README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,22 @@ A small beast that looks cute up close, ready to do your graphics bidding.
133133
> Cute technology.
134134
135135
## Project Organization
136-
* crates/renderling-shader
137-
138-
Contains Rust shader code that can be shared on CPU and GPU (using `rust-gpu` to compile to SPIR-V).
139-
Most of the shader code is here!
140-
Certain tasks require atomics which doesn't work from `rust-gpu` to `wgpu` yet. See [NOTES.md](NOTES.md).
141-
This crate is a member of the workspace so you get nice editor tooling while writing shaders in Rust.
142-
You can also write sanity tests that run with `cargo test`.
143-
Things just work like BAU.
144-
145-
* shaders
146-
147-
Contains a thin crate wrapper around `renderling-shader`.
148-
Provides the spirv annotations for shaders.
149-
Contains a program that compiles Rust into SPIR-V and copies **.spv** files into the main `renderling` crate.
150136

151137
* crates/renderling
152138

153-
The main crate.
139+
Main library crate.
154140
Contains CPU Rust code for creating pipelines and managing resources, making render passes, etc.
141+
Contains GPU Rust code of the shader operations themselves.
155142
Contains tests, some using image comparison of actual frame renders for consistency and backwards compatibility.
156143

144+
* crates/renderling/shaders
145+
146+
Contains **.spv** and **.wgsl** files generated by [`cargo-gpu`](https://github.com/rust-gpu/cargo-gpu).
147+
148+
* crates/renderling/src/linkage*
149+
150+
Contains autogenerated `wgpu` linkage for the generated shaders.
151+
157152
* img
158153

159154
Image assets for tests (textures, etc.)
@@ -178,13 +173,17 @@ cargo test
178173

179174
## Building the shaders
180175

181-
The `shaders/` folder is a crate that is excluded from the cargo workspace.
182-
It compiles into a program that can be run to generate the shaders:
176+
The `crates/renderling/shaders/` folder contains the generated SPIR-V files.
177+
178+
To regenerate the shaders, run:
183179

184180
```
185-
cd shaders/ && cargo run --release
181+
cargo shaders
186182
```
187183

184+
There is a `.cargo/config.toml` alias for `cargo shaders` that expands into a larger
185+
shader compilation command.
186+
188187
## Building on WASM
189188

190189
```
@@ -208,6 +207,8 @@ If you use it (outright or for inspiration), please consider donating.
208207
Many projects were born from first solving a need within `renderling`.
209208
Some of these solutions were then spun off into their own projects.
210209

210+
- [`cargo-gpu`](https://githu.com/rust-gpu/cargo-gpu)
211+
A shader compilation cli tool.
211212
- [`crabslab`](https://github.com/schell/crabslab)
212213
A slab allocator for working across CPU/GPU boundaries.
213214
- [`loading-bytes`](crates/loading-bytes)

crates/renderling/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license = "MIT OR Apache-2.0"
88
keywords = ["game", "graphics", "shader", "rendering"]
99
categories = ["rendering", "game-development", "graphics"]
1010
readme = "../../README.md"
11+
build = "src/build.rs"
1112

1213
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1314

@@ -87,8 +88,12 @@ tutorial_slabbed_renderlet = []
8788

8889
wasm = ["wgpu/fragile-send-sync-non-atomic-wasm"]
8990

90-
[patch.crates-io]
91-
spirv-std = { git = "https://github.com/Rust-GPU/rust-gpu" }
91+
[build-dependencies]
92+
naga = {workspace = true}
93+
pathdiff = "0.2.2"
94+
quote = "1.0"
95+
serde = {version = "1.0", features = ["derive"]}
96+
serde_json = {workspace = true}
9297

9398
# dependencies for CPU and GPU code
9499
[dependencies]
@@ -134,8 +139,3 @@ winit.workspace = true
134139

135140
[target.'cfg(not(target_arch = "spirv"))'.dev-dependencies]
136141
glam = { workspace = true, features = ["std", "debug-glam-assert"] }
137-
138-
139-
# [[bench]]
140-
# name = "benchmarks"
141-
# harness = false
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
struct type_8 {
2+
member: array<u32>,
3+
}
4+
5+
@group(0) @binding(0)
6+
var<storage> global: type_8;
7+
var<private> global_1: vec2<f32>;
8+
var<private> global_2: u32;
9+
@group(0) @binding(2)
10+
var global_3: sampler;
11+
@group(0) @binding(1)
12+
var global_4: texture_2d<f32>;
13+
var<private> global_5: vec4<f32>;
14+
15+
fn function() {
16+
var phi_329_: bool;
17+
var phi_80_: vec2<f32>;
18+
19+
let _e20 = arrayLength((&global.member));
20+
let _e21 = global_1;
21+
let _e22 = global_2;
22+
if (_e20 >= 2u) {
23+
phi_329_ = (_e22 <= (_e20 - 2u));
24+
} else {
25+
phi_329_ = false;
26+
}
27+
let _e27 = phi_329_;
28+
if _e27 {
29+
let _e30 = global.member[_e22];
30+
let _e35 = global.member[(_e22 + 1u)];
31+
phi_80_ = vec2<f32>(bitcast<f32>(_e30), bitcast<f32>(_e35));
32+
} else {
33+
phi_80_ = vec2<f32>(0f, 0f);
34+
}
35+
let _e39 = phi_80_;
36+
let _e44 = fma(-2f, _e39.x, _e21.x);
37+
let _e47 = fma(2f, _e39.y, _e21.y);
38+
let _e49 = textureSample(global_4, global_3, vec2<f32>(_e44, _e47));
39+
let _e51 = textureSample(global_4, global_3, vec2<f32>(_e21.x, _e47));
40+
let _e52 = vec2<f32>((2f * _e39.x), (2f * _e39.y));
41+
let _e54 = textureSample(global_4, global_3, (_e21 + _e52));
42+
let _e56 = textureSample(global_4, global_3, vec2<f32>(_e44, _e21.y));
43+
let _e57 = textureSample(global_4, global_3, _e21);
44+
let _e58 = fma(2f, _e39.x, _e21.x);
45+
let _e60 = textureSample(global_4, global_3, vec2<f32>(_e58, _e21.y));
46+
let _e62 = textureSample(global_4, global_3, (_e21 - _e52));
47+
let _e63 = fma(-2f, _e39.y, _e21.y);
48+
let _e65 = textureSample(global_4, global_3, vec2<f32>(_e21.x, _e63));
49+
let _e67 = textureSample(global_4, global_3, vec2<f32>(_e58, _e63));
50+
let _e71 = textureSample(global_4, global_3, vec2<f32>((_e21.x - _e39.x), (_e21.y + _e39.y)));
51+
let _e73 = textureSample(global_4, global_3, (_e21 + _e39));
52+
let _e75 = textureSample(global_4, global_3, (_e21 - _e39));
53+
let _e79 = textureSample(global_4, global_3, vec2<f32>((_e21.x + _e39.x), (_e21.y - _e39.y)));
54+
global_5 = vec4<f32>(max(fma((((_e49.x + _e54.x) + _e62.x) + _e67.x), 0.03125f, fma(0.125f, (_e57.x + (((_e71.x + _e73.x) + _e75.x) + _e79.x)), ((((_e51.x + _e56.x) + _e65.x) + _e60.x) * 0.0625f))), 0.00000011920929f), max(fma((((_e49.y + _e54.y) + _e62.y) + _e67.y), 0.03125f, fma(0.125f, (_e57.y + (((_e71.y + _e73.y) + _e75.y) + _e79.y)), ((((_e51.y + _e56.y) + _e65.y) + _e60.y) * 0.0625f))), 0.00000011920929f), max(fma((((_e49.z + _e54.z) + _e62.z) + _e67.z), 0.03125f, fma(0.125f, (_e57.z + (((_e71.z + _e73.z) + _e75.z) + _e79.z)), ((((_e51.z + _e56.z) + _e65.z) + _e60.z) * 0.0625f))), 0.00000011920929f), max(fma((((_e49.w + _e54.w) + _e62.w) + _e67.w), 0.03125f, fma(0.125f, (_e57.w + (((_e71.w + _e73.w) + _e75.w) + _e79.w)), ((((_e51.w + _e56.w) + _e65.w) + _e60.w) * 0.0625f))), 1f));
55+
return;
56+
}
57+
58+
@fragment
59+
fn bloombloom_downsample_fragment(@location(0) param: vec2<f32>, @location(1) @interpolate(flat) param_1: u32) -> @location(0) vec4<f32> {
60+
global_1 = param;
61+
global_2 = param_1;
62+
function();
63+
let _e5 = global_5;
64+
return _e5;
65+
}

0 commit comments

Comments
 (0)