Skip to content

Commit

Permalink
feat: use cargo-gpu instead of out-of-workspace shaders binary (#138)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
schell authored Nov 11, 2024
1 parent bf28d73 commit 9b0d04d
Show file tree
Hide file tree
Showing 99 changed files with 7,516 additions and 1,762 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[alias]
shaders = "gpu build --output-dir crates/renderling/shaders --shader-crate crates/renderling/ --shader-manifest crates/renderling/shaders/manifest.json"

[build]
rustflags = ["--cfg=web_sys_unstable_apis"]
rustdocflags = ["--cfg=web_sys_unstable_apis"]
Expand Down
18 changes: 11 additions & 7 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: moonrepo/setup-rust@v1
- uses: Swatinem/rust-cache@v2
- uses: actions/cache@v4
with:
workspaces: shaders
# ensure the shader binaries were properly checked in
- run: rm -rf crates/renderling/src/linkage/*.spv
- run: cd shaders && cargo run --release && cd ..
- run: git diff --exit-code --no-ext-diff crates/renderling/src/linkage
path: ~/.cache/rust-gpu
key: ${{ runner.os }}
- uses: moonrepo/setup-rust@v1
- run: rustup toolchain add nightly-2024-04-24
- run: rustup component add --toolchain nightly-2024-04-24 rust-src rustc-dev llvm-tools
- run: cargo install --git https://github.com/rust-gpu/cargo-gpu
- run: rm -rf crates/renderling/src/linkage/* crates/renderling/shaders
- run: RUST_LOG=trace cargo shaders
- run: cargo build -p renderling
- run: git diff --exit-code --no-ext-diff

renderling-clippy:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/target/
shaders/target
shaders/shader-crate/target
**/spirv-manifest.json

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
61 changes: 17 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ glam = { version = "0.24.2", default-features = false }
gltf = { version = "1.4,1", features = ["KHR_lights_punctual", "KHR_materials_unlit", "KHR_materials_emissive_strength", "extras", "extensions"] }
image = "0.24"
log = "0.4"
naga = { version = "0.19", features = ["spv-in", "wgsl-out", "wgsl-in", "msl-out"] }
naga = { version = "22.1.0", features = ["spv-in", "wgsl-out", "wgsl-in", "msl-out"] }
pretty_assertions = "1.4.0"
proc-macro2 = { version = "1.0", features = ["span-locations"] }
rustc-hash = "1.1"
Expand All @@ -40,6 +40,7 @@ web-sys = "0.3"
winit = { version = "0.30" }
wgpu = "22.1.0"


[profile.dev]
opt-level = 1

Expand Down
37 changes: 19 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,22 @@ A small beast that looks cute up close, ready to do your graphics bidding.
> Cute technology.
## Project Organization
* crates/renderling-shader

Contains Rust shader code that can be shared on CPU and GPU (using `rust-gpu` to compile to SPIR-V).
Most of the shader code is here!
Certain tasks require atomics which doesn't work from `rust-gpu` to `wgpu` yet. See [NOTES.md](NOTES.md).
This crate is a member of the workspace so you get nice editor tooling while writing shaders in Rust.
You can also write sanity tests that run with `cargo test`.
Things just work like BAU.

* shaders

Contains a thin crate wrapper around `renderling-shader`.
Provides the spirv annotations for shaders.
Contains a program that compiles Rust into SPIR-V and copies **.spv** files into the main `renderling` crate.

* crates/renderling

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

* crates/renderling/shaders

Contains **.spv** and **.wgsl** files generated by [`cargo-gpu`](https://github.com/rust-gpu/cargo-gpu).

* crates/renderling/src/linkage*

Contains autogenerated `wgpu` linkage for the generated shaders.

* img

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

## Building the shaders

The `shaders/` folder is a crate that is excluded from the cargo workspace.
It compiles into a program that can be run to generate the shaders:
The `crates/renderling/shaders/` folder contains the generated SPIR-V files.

To regenerate the shaders, run:

```
cd shaders/ && cargo run --release
cargo shaders
```

There is a `.cargo/config.toml` alias for `cargo shaders` that expands into a larger
shader compilation command.

## Building on WASM

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

- [`cargo-gpu`](https://githu.com/rust-gpu/cargo-gpu)
A shader compilation cli tool.
- [`crabslab`](https://github.com/schell/crabslab)
A slab allocator for working across CPU/GPU boundaries.
- [`loading-bytes`](crates/loading-bytes)
Expand Down
14 changes: 7 additions & 7 deletions crates/renderling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "MIT OR Apache-2.0"
keywords = ["game", "graphics", "shader", "rendering"]
categories = ["rendering", "game-development", "graphics"]
readme = "../../README.md"
build = "src/build.rs"

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

Expand Down Expand Up @@ -87,8 +88,12 @@ tutorial_slabbed_renderlet = []

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

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

# dependencies for CPU and GPU code
[dependencies]
Expand Down Expand Up @@ -134,8 +139,3 @@ winit.workspace = true

[target.'cfg(not(target_arch = "spirv"))'.dev-dependencies]
glam = { workspace = true, features = ["std", "debug-glam-assert"] }


# [[bench]]
# name = "benchmarks"
# harness = false
65 changes: 65 additions & 0 deletions crates/renderling/shaders/bloom-bloom_downsample_fragment.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
struct type_8 {
member: array<u32>,
}

@group(0) @binding(0)
var<storage> global: type_8;
var<private> global_1: vec2<f32>;
var<private> global_2: u32;
@group(0) @binding(2)
var global_3: sampler;
@group(0) @binding(1)
var global_4: texture_2d<f32>;
var<private> global_5: vec4<f32>;

fn function() {
var phi_329_: bool;
var phi_80_: vec2<f32>;

let _e20 = arrayLength((&global.member));
let _e21 = global_1;
let _e22 = global_2;
if (_e20 >= 2u) {
phi_329_ = (_e22 <= (_e20 - 2u));
} else {
phi_329_ = false;
}
let _e27 = phi_329_;
if _e27 {
let _e30 = global.member[_e22];
let _e35 = global.member[(_e22 + 1u)];
phi_80_ = vec2<f32>(bitcast<f32>(_e30), bitcast<f32>(_e35));
} else {
phi_80_ = vec2<f32>(0f, 0f);
}
let _e39 = phi_80_;
let _e44 = fma(-2f, _e39.x, _e21.x);
let _e47 = fma(2f, _e39.y, _e21.y);
let _e49 = textureSample(global_4, global_3, vec2<f32>(_e44, _e47));
let _e51 = textureSample(global_4, global_3, vec2<f32>(_e21.x, _e47));
let _e52 = vec2<f32>((2f * _e39.x), (2f * _e39.y));
let _e54 = textureSample(global_4, global_3, (_e21 + _e52));
let _e56 = textureSample(global_4, global_3, vec2<f32>(_e44, _e21.y));
let _e57 = textureSample(global_4, global_3, _e21);
let _e58 = fma(2f, _e39.x, _e21.x);
let _e60 = textureSample(global_4, global_3, vec2<f32>(_e58, _e21.y));
let _e62 = textureSample(global_4, global_3, (_e21 - _e52));
let _e63 = fma(-2f, _e39.y, _e21.y);
let _e65 = textureSample(global_4, global_3, vec2<f32>(_e21.x, _e63));
let _e67 = textureSample(global_4, global_3, vec2<f32>(_e58, _e63));
let _e71 = textureSample(global_4, global_3, vec2<f32>((_e21.x - _e39.x), (_e21.y + _e39.y)));
let _e73 = textureSample(global_4, global_3, (_e21 + _e39));
let _e75 = textureSample(global_4, global_3, (_e21 - _e39));
let _e79 = textureSample(global_4, global_3, vec2<f32>((_e21.x + _e39.x), (_e21.y - _e39.y)));
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));
return;
}

@fragment
fn bloombloom_downsample_fragment(@location(0) param: vec2<f32>, @location(1) @interpolate(flat) param_1: u32) -> @location(0) vec4<f32> {
global_1 = param;
global_2 = param_1;
function();
let _e5 = global_5;
return _e5;
}
Loading

0 comments on commit 9b0d04d

Please sign in to comment.