diff --git a/.github/workflows/comment-profiling-changes.yaml b/.github/workflows/comment-profiling-changes.yaml index 7dec9532e5..0b35c61096 100644 --- a/.github/workflows/comment-profiling-changes.yaml +++ b/.github/workflows/comment-profiling-changes.yaml @@ -34,12 +34,12 @@ jobs: uses: actions/cache@v4 with: path: ~/.cargo/bin/iai-callgrind-runner - key: ${{ runner.os }}-iai-callgrind-runner-0.12.3 + key: ${{ runner.os }}-iai-callgrind-runner-0.16.1 - name: Install iai-callgrind if: steps.cache-iai.outputs.cache-hit != 'true' run: | - cargo install iai-callgrind-runner@0.12.3 + cargo install iai-callgrind-runner@0.16.1 - name: Checkout master branch run: | diff --git a/Cargo.lock b/Cargo.lock index 3105ea07e4..97edb1e0c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -637,6 +637,75 @@ dependencies = [ "wayland-client", ] +[[package]] +name = "camino" +version = "1.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0b03af37dad7a14518b7691d81acb0f8222604ad3d1b02f6b4bed5188c0cd5" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo-gpu" +version = "0.1.0" +source = "git+https://github.com/rust-gpu/cargo-gpu?rev=f969528e87baa17a7d48eecf4a6fcfdcaaf30566#f969528e87baa17a7d48eecf4a6fcfdcaaf30566" +dependencies = [ + "anyhow", + "cargo_metadata", + "clap", + "crossterm", + "directories", + "env_logger", + "log", + "relative-path", + "rustc_codegen_spirv-target-specs 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "semver", + "serde", + "serde_json", + "spirv-builder", +] + +[[package]] +name = "cargo-platform" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84982c6c0ae343635a3a4ee6dedef965513735c8b183caa7289fa6e27399ebd4" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo-util-schemas" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dc1a6f7b5651af85774ae5a34b4e8be397d9cf4bc063b7e6dbd99a841837830" +dependencies = [ + "semver", + "serde", + "serde-untagged", + "serde-value", + "thiserror 2.0.16", + "toml", + "unicode-xid", + "url", +] + +[[package]] +name = "cargo_metadata" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cfca2aaa699835ba88faf58a06342a314a950d2b9686165e038286c30316868" +dependencies = [ + "camino", + "cargo-platform", + "cargo-util-schemas", + "semver", + "serde", + "serde_json", + "thiserror 2.0.16", +] + [[package]] name = "cast" version = "0.3.0" @@ -866,6 +935,15 @@ dependencies = [ "windows-sys 0.60.2", ] +[[package]] +name = "convert_case" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb402b8d4c85569410425650ce3eddc7d698ed96d39a73f941b08fb63082f1e7" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "convert_case" version = "0.8.0" @@ -1029,6 +1107,33 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "crossterm" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b9f2e4c67f833b660cdb0a3523065869fb35570177239812ed4c905aeff87b" +dependencies = [ + "bitflags 2.9.3", + "crossterm_winapi", + "derive_more", + "document-features", + "mio", + "parking_lot", + "rustix 1.0.8", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + [[package]] name = "crunchy" version = "0.2.4" @@ -1105,6 +1210,7 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bda628edc44c4bb645fbe0f758797143e4e07926f7ebf4e9bdfbd3d2ce621df3" dependencies = [ + "convert_case 0.7.1", "proc-macro2", "quote", "syn 2.0.106", @@ -1126,6 +1232,15 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "directories" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f5094c54661b38d03bd7e50df373292118db60b585c08a411c6d840017fe7d" +dependencies = [ + "dirs-sys", +] + [[package]] name = "dirs" version = "6.0.0" @@ -1344,6 +1459,16 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "erased-serde" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e004d887f51fcb9fef17317a2f3525c887d8aa3f4f50fed920816a688284a5b7" +dependencies = [ + "serde", + "typeid", +] + [[package]] name = "errno" version = "0.3.13" @@ -1609,6 +1734,15 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fsevent-sys" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2" +dependencies = [ + "libc", +] + [[package]] name = "futures" version = "0.3.31" @@ -2045,6 +2179,7 @@ dependencies = [ "glam", "graphene-core", "graphene-core-shaders", + "graphene-raster-nodes-shaders", "image", "kurbo", "ndarray", @@ -2054,9 +2189,18 @@ dependencies = [ "rand_chacha 0.9.0", "serde", "specta", + "spirv-std", "tokio", ] +[[package]] +name = "graphene-raster-nodes-shaders" +version = "0.1.0" +dependencies = [ + "cargo-gpu", + "env_logger", +] + [[package]] name = "graphene-std" version = "0.1.0" @@ -2699,6 +2843,26 @@ version = "2.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c7245a08504955605670dbf141fceab975f15ca21570696aebe9d2e71576bd" +[[package]] +name = "inotify" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f37dccff2791ab604f9babef0ba14fbe0be30bd368dc541e2b08d07c8aa908f3" +dependencies = [ + "bitflags 2.9.3", + "inotify-sys", + "libc", +] + +[[package]] +name = "inotify-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb" +dependencies = [ + "libc", +] + [[package]] name = "interpolate_name" version = "0.2.4" @@ -2894,6 +3058,26 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" +[[package]] +name = "kqueue" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac30106d7dce88daf4a3fcb4879ea939476d5074a9b7ddd0fb97fa4bed5596a" +dependencies = [ + "kqueue-sys", + "libc", +] + +[[package]] +name = "kqueue-sys" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed9625ffda8729b85e45cf04090035ac368927b8cebc34898e7c120f52e4838b" +dependencies = [ + "bitflags 1.3.2", + "libc", +] + [[package]] name = "kurbo" version = "0.11.3" @@ -3157,6 +3341,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78bed444cc8a2160f01cbcf811ef18cac863ad68ae8ca62092e8db51d51c761c" dependencies = [ "libc", + "log", "wasi 0.11.1+wasi-snapshot-preview1", "windows-sys 0.59.0", ] @@ -3281,7 +3466,7 @@ dependencies = [ name = "node-macro" version = "0.0.0" dependencies = [ - "convert_case", + "convert_case 0.8.0", "graphene-core", "indoc", "proc-macro-crate", @@ -3308,6 +3493,30 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" +[[package]] +name = "notify" +version = "8.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d3d07927151ff8575b7087f245456e549fea62edf0ec4e565a5ee50c8402bc3" +dependencies = [ + "bitflags 2.9.3", + "fsevent-sys", + "inotify", + "kqueue", + "libc", + "log", + "mio", + "notify-types", + "walkdir", + "windows-sys 0.60.2", +] + +[[package]] +name = "notify-types" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e0826a989adedc2a244799e823aece04662b66609d96af8dff7ac6df9a8925d" + [[package]] name = "nu-ansi-term" version = "0.50.1" @@ -3794,6 +4003,15 @@ dependencies = [ "libredox", ] +[[package]] +name = "ordered-float" +version = "2.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68f19d67e5a2795c94e73e0bb1cc1a7edeb2e28efd39e2e1c9b7a40c1108b11c" +dependencies = [ + "num-traits", +] + [[package]] name = "ordered-float" version = "4.6.0" @@ -4435,6 +4653,12 @@ dependencies = [ "rgb", ] +[[package]] +name = "raw-string" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0501e134c6905fee1f10fed25b0a7e1261bf676cffac9543a7d0730dec01af2" + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -4545,6 +4769,15 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caf4aa5b0f434c91fe5c7f1ecb6a5ece2130b02ad2a590589dda5146df959001" +[[package]] +name = "relative-path" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bca40a312222d8ba74837cb474edef44b37f561da5f773981007a10bbaa992b0" +dependencies = [ + "serde", +] + [[package]] name = "renderdoc-sys" version = "1.1.0" @@ -4686,6 +4919,16 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" +[[package]] +name = "rspirv" +version = "0.12.0+sdk-1.3.268.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cf3a93856b6e5946537278df0d3075596371b1950ccff012f02b0f7eafec8d" +dependencies = [ + "rustc-hash 1.1.0", + "spirv", +] + [[package]] name = "rustc-demangle" version = "0.1.26" @@ -4704,6 +4947,33 @@ version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" +[[package]] +name = "rustc_codegen_spirv-target-specs" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c89eaf493b3dfc730cda42a77014aad65e03213992c7afe0dff60a9f7d3dd94" + +[[package]] +name = "rustc_codegen_spirv-target-specs" +version = "0.9.0" +source = "git+https://github.com/rust-gpu/rust-gpu?rev=c12f216121820580731440ee79ebc7403d6ea04f#c12f216121820580731440ee79ebc7403d6ea04f" +dependencies = [ + "serde", + "strum 0.27.2", + "thiserror 2.0.16", +] + +[[package]] +name = "rustc_codegen_spirv-types" +version = "0.9.0" +source = "git+https://github.com/rust-gpu/rust-gpu?rev=c12f216121820580731440ee79ebc7403d6ea04f#c12f216121820580731440ee79ebc7403d6ea04f" +dependencies = [ + "rspirv", + "serde", + "serde_json", + "spirv", +] + [[package]] name = "rustix" version = "0.38.44" @@ -4894,6 +5164,9 @@ name = "semver" version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0" +dependencies = [ + "serde", +] [[package]] name = "serde" @@ -4904,6 +5177,27 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-untagged" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34836a629bcbc6f1afdf0907a744870039b1e14c0561cb26094fa683b158eff3" +dependencies = [ + "erased-serde", + "serde", + "typeid", +] + +[[package]] +name = "serde-value" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" +dependencies = [ + "ordered-float 2.10.1", + "serde", +] + [[package]] name = "serde-wasm-bindgen" version = "0.6.5" @@ -5002,6 +5296,27 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signal-hook" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34db1a06d485c9142248b7a054f034b349b212551f3dfd19c94d45a754a217cd" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + [[package]] name = "signal-hook-registry" version = "1.4.6" @@ -5179,8 +5494,57 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eda41003dc44290527a59b13432d4a0379379fa074b70174882adfbdfd917844" dependencies = [ "bitflags 2.9.3", + "serde", +] + +[[package]] +name = "spirv-builder" +version = "0.9.0" +source = "git+https://github.com/rust-gpu/rust-gpu?rev=c12f216121820580731440ee79ebc7403d6ea04f#c12f216121820580731440ee79ebc7403d6ea04f" +dependencies = [ + "cargo_metadata", + "clap", + "log", + "memchr", + "notify", + "raw-string", + "rustc_codegen_spirv-target-specs 0.9.0 (git+https://github.com/rust-gpu/rust-gpu?rev=c12f216121820580731440ee79ebc7403d6ea04f)", + "rustc_codegen_spirv-types", + "semver", + "serde", + "serde_json", + "thiserror 2.0.16", ] +[[package]] +name = "spirv-std" +version = "0.9.0" +source = "git+https://github.com/rust-gpu/rust-gpu?rev=c12f216121820580731440ee79ebc7403d6ea04f#c12f216121820580731440ee79ebc7403d6ea04f" +dependencies = [ + "bitflags 1.3.2", + "glam", + "libm", + "num-traits", + "spirv-std-macros", + "spirv-std-types", +] + +[[package]] +name = "spirv-std-macros" +version = "0.9.0" +source = "git+https://github.com/rust-gpu/rust-gpu?rev=c12f216121820580731440ee79ebc7403d6ea04f#c12f216121820580731440ee79ebc7403d6ea04f" +dependencies = [ + "proc-macro2", + "quote", + "spirv-std-types", + "syn 2.0.106", +] + +[[package]] +name = "spirv-std-types" +version = "0.9.0" +source = "git+https://github.com/rust-gpu/rust-gpu?rev=c12f216121820580731440ee79ebc7403d6ea04f#c12f216121820580731440ee79ebc7403d6ea04f" + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -5659,9 +6023,16 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", + "toml_write", "winnow", ] +[[package]] +name = "toml_write" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801" + [[package]] name = "tower" version = "0.5.2" @@ -5792,6 +6163,12 @@ dependencies = [ "core_maths", ] +[[package]] +name = "typeid" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c" + [[package]] name = "typenum" version = "1.18.0" @@ -5881,6 +6258,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a1a07cc7db3810833284e8d372ccdc6da29741639ecc70c9ec107df0fa6154c" +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + [[package]] name = "unit-prefix" version = "0.5.1" @@ -6480,7 +6863,7 @@ dependencies = [ "naga", "ndk-sys 0.5.0+25.2.9519653", "objc", - "ordered-float", + "ordered-float 4.6.0", "parking_lot", "portable-atomic", "profiling", diff --git a/Cargo.toml b/Cargo.toml index 94671aec4c..ca1a6ed5df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ members = [ "node-graph/graph-craft", "node-graph/graphene-cli", "node-graph/graster-nodes", + "node-graph/graster-nodes/shaders", "node-graph/gstd", "node-graph/gsvg-renderer", "node-graph/interpreted-executor", @@ -37,6 +38,7 @@ default-members = [ "node-graph/gbrush", "node-graph/gcore", "node-graph/gcore-shaders", + "node-graph/graster-nodes/shaders", "node-graph/gmath-nodes", "node-graph/gpath-bool", "node-graph/graph-craft", @@ -191,6 +193,11 @@ open = "5.3" poly-cool = "0.3" spin = "0.10" clap = "4.5" +spirv-std = { git = "https://github.com/rust-gpu/rust-gpu", rev = "c12f216121820580731440ee79ebc7403d6ea04f" } +cargo-gpu = { git = "https://github.com/rust-gpu/cargo-gpu", rev = "f969528e87baa17a7d48eecf4a6fcfdcaaf30566" } + +[workspace.lints.rust] +unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] } [profile.dev] opt-level = 1 diff --git a/frontend/wasm/Cargo.toml b/frontend/wasm/Cargo.toml index 90b8ebe6ba..cdb0ad3e48 100644 --- a/frontend/wasm/Cargo.toml +++ b/frontend/wasm/Cargo.toml @@ -11,9 +11,10 @@ repository = "https://github.com/GraphiteEditor/Graphite" license = "Apache-2.0" [features] -default = ["gpu"] +default = ["gpu", "shader-nodes"] gpu = ["editor/gpu"] native = [] +shader-nodes = ["graphene-std/shader-nodes", "gpu"] [lib] crate-type = ["cdylib", "rlib"] diff --git a/node-graph/gcore-shaders/Cargo.toml b/node-graph/gcore-shaders/Cargo.toml index ebb23e1234..7278b62acc 100644 --- a/node-graph/gcore-shaders/Cargo.toml +++ b/node-graph/gcore-shaders/Cargo.toml @@ -7,7 +7,23 @@ authors = ["Graphite Authors "] license = "MIT OR Apache-2.0" [features] -std = ["dep:dyn-any", "dep:serde", "dep:specta", "dep:log", "glam/debug-glam-assert", "glam/std", "glam/serde", "half/std", "half/serde", "num-traits/std"] +# any feature that +# * must be usable in shaders +# * but requires std +# * and should be on by default +# should be in this list instead of `[workspace.dependency]` +std = [ + "dep:dyn-any", + "dep:serde", + "dep:specta", + "dep:log", + "glam/debug-glam-assert", + "glam/std", + "glam/serde", + "half/std", + "half/serde", + "num-traits/std" +] [dependencies] # Local std dependencies diff --git a/node-graph/gcore-shaders/src/color/color_types.rs b/node-graph/gcore-shaders/src/color/color_types.rs index 2cb4d231bd..63517341e8 100644 --- a/node-graph/gcore-shaders/src/color/color_types.rs +++ b/node-graph/gcore-shaders/src/color/color_types.rs @@ -3,6 +3,7 @@ use super::discrete_srgb::{float_to_srgb_u8, srgb_u8_to_float}; use bytemuck::{Pod, Zeroable}; use core::fmt::Debug; use core::hash::Hash; +use glam::Vec4; use half::f16; #[cfg(not(feature = "std"))] use num_traits::Euclid; @@ -1075,6 +1076,21 @@ impl Color { ..*self } } + + #[inline(always)] + pub const fn from_vec4(vec: Vec4) -> Self { + Self { + red: vec.x, + green: vec.y, + blue: vec.z, + alpha: vec.w, + } + } + + #[inline(always)] + pub fn to_vec4(&self) -> Vec4 { + Vec4::new(self.red, self.green, self.blue, self.alpha) + } } #[cfg(test)] diff --git a/node-graph/graster-nodes/Cargo.toml b/node-graph/graster-nodes/Cargo.toml index c0844b98b1..a66dbb5eb3 100644 --- a/node-graph/graster-nodes/Cargo.toml +++ b/node-graph/graster-nodes/Cargo.toml @@ -6,8 +6,18 @@ description = "graphene raster data format" authors = ["Graphite Authors "] license = "MIT OR Apache-2.0" +[lib] +crate-type = ["rlib", "dylib"] + +[lints] +workspace = true + [features] default = ["std"] +shader-nodes = [ + "std", + "dep:graphene-raster-nodes-shaders", +] std = [ "dep:graphene-core", "dep:dyn-any", @@ -19,8 +29,6 @@ std = [ "dep:serde", "dep:specta", "dep:kurbo", - "glam/debug-glam-assert", - "glam/serde", ] [dependencies] @@ -31,10 +39,12 @@ node-macro = { workspace = true } # Local std dependencies dyn-any = { workspace = true, optional = true } graphene-core = { workspace = true, optional = true } +graphene-raster-nodes-shaders = { path = "./shaders", optional = true } # Workspace dependencies bytemuck = { workspace = true } glam = { workspace = true } +spirv-std = { workspace = true } num-traits = { workspace = true } # Workspace std dependencies diff --git a/node-graph/graster-nodes/shaders/Cargo.toml b/node-graph/graster-nodes/shaders/Cargo.toml new file mode 100644 index 0000000000..65a4fed409 --- /dev/null +++ b/node-graph/graster-nodes/shaders/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "graphene-raster-nodes-shaders" +version = "0.1.0" +edition = "2024" +description = "graphene raster data format" +authors = ["Graphite Authors "] +license = "MIT OR Apache-2.0" + +[dependencies] + +[build-dependencies] +cargo-gpu = { workspace = true } +env_logger = { workspace = true } diff --git a/node-graph/graster-nodes/shaders/build.rs b/node-graph/graster-nodes/shaders/build.rs new file mode 100644 index 0000000000..96964e1adb --- /dev/null +++ b/node-graph/graster-nodes/shaders/build.rs @@ -0,0 +1,55 @@ +use cargo_gpu::InstalledBackend; +use cargo_gpu::spirv_builder::{MetadataPrintout, SpirvMetadata}; +use std::path::PathBuf; + +pub fn main() -> Result<(), Box> { + env_logger::builder().init(); + + // Skip building the shader if they are provided externally + println!("cargo:rerun-if-env-changed=GRAPHENE_RASTER_NODES_SHADER_PATH"); + if !std::env::var("GRAPHENE_RASTER_NODES_SHADER_PATH").unwrap_or_default().is_empty() { + return Ok(()); + } + + // Allows overriding the PATH to inject the rust-gpu rust toolchain when building the rest of the project with stable rustc. + // Used in nix shell. Do not remove without checking with developers using nix. + println!("cargo:rerun-if-env-changed=RUST_GPU_PATH_OVERRIDE"); + if let Ok(path_override) = std::env::var("RUST_GPU_PATH_OVERRIDE") { + let current_path = std::env::var("PATH").unwrap_or_default(); + let new_path = format!("{path_override}:{current_path}"); + // SAFETY: Build script is single-threaded therefore this cannot lead to undefined behavior. + unsafe { + std::env::set_var("PATH", &new_path); + } + } + + let shader_crate = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/..")); + + println!("cargo:rerun-if-env-changed=RUSTC_CODEGEN_SPIRV_PATH"); + let rustc_codegen_spirv_path = std::env::var("RUSTC_CODEGEN_SPIRV_PATH").unwrap_or_default(); + let backend = if rustc_codegen_spirv_path.is_empty() { + // install the toolchain and build the `rustc_codegen_spirv` codegen backend with it + cargo_gpu::Install::from_shader_crate(shader_crate.clone()).run()? + } else { + // use the `RUSTC_CODEGEN_SPIRV` environment variable to find the codegen backend + let mut backend = InstalledBackend::default(); + backend.rustc_codegen_spirv_location = PathBuf::from(rustc_codegen_spirv_path); + backend.toolchain_channel = "nightly".to_string(); + backend.target_spec_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + backend + }; + + // build the shader crate + let mut builder = backend.to_spirv_builder(shader_crate, "spirv-unknown-naga-wgsl"); + builder.print_metadata = MetadataPrintout::DependencyOnly; + builder.spirv_metadata = SpirvMetadata::Full; + builder.shader_crate_features.default_features = false; + let wgsl_result = builder.build()?; + let path_to_spv = wgsl_result.module.unwrap_single(); + + // needs to be fixed upstream + let path_to_wgsl = path_to_spv.with_extension("wgsl"); + + println!("cargo::rustc-env=GRAPHENE_RASTER_NODES_SHADER_PATH={}", path_to_wgsl.display()); + Ok(()) +} diff --git a/node-graph/graster-nodes/shaders/spirv-unknown-naga-wgsl.json b/node-graph/graster-nodes/shaders/spirv-unknown-naga-wgsl.json new file mode 100644 index 0000000000..00f17162c0 --- /dev/null +++ b/node-graph/graster-nodes/shaders/spirv-unknown-naga-wgsl.json @@ -0,0 +1,26 @@ +{ + "allows-weak-linkage": false, + "arch": "spirv", + "crt-objects-fallback": "false", + "crt-static-allows-dylibs": true, + "crt-static-respected": true, + "data-layout": "e-m:e-p:32:32:32-i64:64-n8:16:32:64", + "dll-prefix": "", + "dll-suffix": ".spv.json", + "dynamic-linking": true, + "emit-debug-gdb-scripts": false, + "env": "naga-wgsl", + "linker-flavor": "unix", + "linker-is-gnu": false, + "llvm-target": "spirv-unknown-naga-wgsl", + "main-needs-argc-argv": false, + "metadata": { + "description": null, + "host_tools": null, + "std": null, + "tier": null + }, + "panic-strategy": "abort", + "simd-types-indirect": false, + "target-pointer-width": "32" +} diff --git a/node-graph/graster-nodes/shaders/src/lib.rs b/node-graph/graster-nodes/shaders/src/lib.rs new file mode 100644 index 0000000000..19afe82bb9 --- /dev/null +++ b/node-graph/graster-nodes/shaders/src/lib.rs @@ -0,0 +1 @@ +pub const WGSL_SHADER: &str = include_str!(env!("GRAPHENE_RASTER_NODES_SHADER_PATH")); diff --git a/node-graph/graster-nodes/src/adjustments.rs b/node-graph/graster-nodes/src/adjustments.rs index 1274ef5fe4..35de34c76b 100644 --- a/node-graph/graster-nodes/src/adjustments.rs +++ b/node-graph/graster-nodes/src/adjustments.rs @@ -33,6 +33,7 @@ use num_traits::float::Float; #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash, node_macro::ChoiceType)] #[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))] #[widget(Dropdown)] +#[repr(u32)] pub enum LuminanceCalculation { #[default] #[label("sRGB")] @@ -52,6 +53,7 @@ fn luminance>( Table, GradientStops, )] + #[gpu_image] mut input: T, luminance_calc: LuminanceCalculation, ) -> T { @@ -68,7 +70,7 @@ fn luminance>( input } -#[node_macro::node(category("Raster"), shader_node(PerPixelAdjust))] +#[node_macro::node(category("Raster"), cfg(feature = "std"))] fn gamma_correction>( _: impl Ctx, #[implementations( @@ -77,6 +79,7 @@ fn gamma_correction>( Table, GradientStops, )] + #[gpu_image] mut input: T, #[default(2.2)] #[range((0.01, 10.))] @@ -98,6 +101,7 @@ fn extract_channel>( Table, GradientStops, )] + #[gpu_image] mut input: T, channel: RedGreenBlueAlpha, ) -> T { @@ -122,6 +126,7 @@ fn make_opaque>( Table, GradientStops, )] + #[gpu_image] mut input: T, ) -> T { input.adjust(|color| { @@ -139,7 +144,7 @@ fn make_opaque>( // // Some further analysis available at: // https://geraldbakker.nl/psnumbers/brightness-contrast.html -#[node_macro::node(name("Brightness/Contrast"), category("Raster: Adjustment"), properties("brightness_contrast_properties"), shader_node(PerPixelAdjust))] +#[node_macro::node(name("Brightness/Contrast"), category("Raster: Adjustment"), properties("brightness_contrast_properties"), cfg(feature = "std"))] fn brightness_contrast>( _: impl Ctx, #[implementations( @@ -148,6 +153,7 @@ fn brightness_contrast>( Table, GradientStops, )] + #[gpu_image] mut input: T, brightness: SignedPercentageF32, contrast: SignedPercentageF32, @@ -238,6 +244,7 @@ fn levels>( Table, GradientStops, )] + #[gpu_image] mut image: T, #[default(0.)] shadows: PercentageF32, #[default(50.)] midtones: PercentageF32, @@ -306,6 +313,7 @@ fn black_and_white>( Table, GradientStops, )] + #[gpu_image] mut image: T, #[default(Color::BLACK)] tint: Color, #[default(40.)] @@ -379,6 +387,7 @@ fn hue_saturation>( Table, GradientStops, )] + #[gpu_image] mut input: T, hue_shift: AngleF32, saturation_shift: SignedPercentageF32, @@ -414,6 +423,7 @@ fn invert>( Table, GradientStops, )] + #[gpu_image] mut input: T, ) -> T { input.adjust(|color| { @@ -437,6 +447,7 @@ fn threshold>( Table, GradientStops, )] + #[gpu_image] mut image: T, #[default(50.)] min_luminance: PercentageF32, #[default(100.)] max_luminance: PercentageF32, @@ -483,6 +494,7 @@ fn vibrance>( Table, GradientStops, )] + #[gpu_image] mut image: T, vibrance: SignedPercentageF32, ) -> T { @@ -551,6 +563,7 @@ pub enum RedGreenBlue { #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)] #[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))] #[widget(Radio)] +#[repr(u32)] pub enum RedGreenBlueAlpha { #[default] Red, @@ -640,7 +653,7 @@ pub enum DomainWarpType { // Aims for interoperable compatibility with: // https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27mixr%27%20%3D%20Channel%20Mixer // https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=Lab%20color%20only-,Channel%20Mixer,-Key%20is%20%27mixr -#[node_macro::node(category("Raster: Adjustment"), properties("channel_mixer_properties"), shader_node(PerPixelAdjust))] +#[node_macro::node(category("Raster: Adjustment"), properties("channel_mixer_properties"), cfg(feature = "std"))] fn channel_mixer>( _: impl Ctx, #[implementations( @@ -649,6 +662,7 @@ fn channel_mixer>( Table, GradientStops, )] + #[gpu_image] mut image: T, monochrome: bool, @@ -769,7 +783,7 @@ pub enum SelectiveColorChoice { // // Algorithm based on: // https://blog.pkh.me/p/22-understanding-selective-coloring-in-adobe-photoshop.html -#[node_macro::node(category("Raster: Adjustment"), properties("selective_color_properties"), shader_node(PerPixelAdjust))] +#[node_macro::node(category("Raster: Adjustment"), properties("selective_color_properties"), cfg(feature = "std"))] fn selective_color>( _: impl Ctx, #[implementations( @@ -778,6 +792,7 @@ fn selective_color>( Table, GradientStops, )] + #[gpu_image] mut image: T, mode: RelativeAbsolute, @@ -921,6 +936,7 @@ fn posterize>( Table, GradientStops, )] + #[gpu_image] mut input: T, #[default(4)] #[hard_min(2.)] @@ -955,6 +971,7 @@ fn exposure>( Table, GradientStops, )] + #[gpu_image] mut input: T, exposure: f32, offset: f32, diff --git a/node-graph/graster-nodes/src/blending_nodes.rs b/node-graph/graster-nodes/src/blending_nodes.rs index 6efc335fbb..182f6a802d 100644 --- a/node-graph/graster-nodes/src/blending_nodes.rs +++ b/node-graph/graster-nodes/src/blending_nodes.rs @@ -141,6 +141,7 @@ fn blend + Send>( Table, GradientStops, )] + #[gpu_image] over: T, #[expose] #[implementations( @@ -149,6 +150,7 @@ fn blend + Send>( Table, GradientStops, )] + #[gpu_image] under: T, blend_mode: BlendMode, #[default(100.)] opacity: PercentageF32, @@ -165,6 +167,7 @@ fn color_overlay>( Table, GradientStops, )] + #[gpu_image] mut image: T, #[default(Color::BLACK)] color: Color, blend_mode: BlendMode, diff --git a/node-graph/gstd/Cargo.toml b/node-graph/gstd/Cargo.toml index 6eb7e8a075..7f634c96ac 100644 --- a/node-graph/gstd/Cargo.toml +++ b/node-graph/gstd/Cargo.toml @@ -21,6 +21,7 @@ image-compare = [] vello = ["dep:vello", "gpu"] resvg = [] wayland = ["graph-craft/wayland"] +shader-nodes = ["graphene-raster-nodes/shader-nodes"] [dependencies] # Local dependencies diff --git a/node-graph/interpreted-executor/benches/run_cached_iai.rs b/node-graph/interpreted-executor/benches/run_cached_iai.rs index 0d4cab719d..4fdef67c2d 100644 --- a/node-graph/interpreted-executor/benches/run_cached_iai.rs +++ b/node-graph/interpreted-executor/benches/run_cached_iai.rs @@ -2,8 +2,9 @@ mod benchmark_util; use benchmark_util::setup_network; use graphene_std::application_io::RenderConfig; -use iai_callgrind::{black_box, library_benchmark, library_benchmark_group, main}; +use iai_callgrind::{library_benchmark, library_benchmark_group, main}; use interpreted_executor::dynamic_executor::DynamicExecutor; +use std::hint::black_box; fn setup_run_cached(name: &str) -> DynamicExecutor { let (executor, _) = setup_network(name); diff --git a/node-graph/interpreted-executor/benches/run_once_iai.rs b/node-graph/interpreted-executor/benches/run_once_iai.rs index f28798ad82..5fed663ce8 100644 --- a/node-graph/interpreted-executor/benches/run_once_iai.rs +++ b/node-graph/interpreted-executor/benches/run_once_iai.rs @@ -2,8 +2,9 @@ mod benchmark_util; use benchmark_util::setup_network; use graphene_std::application_io; -use iai_callgrind::{black_box, library_benchmark, library_benchmark_group, main}; +use iai_callgrind::{library_benchmark, library_benchmark_group, main}; use interpreted_executor::dynamic_executor::DynamicExecutor; +use std::hint::black_box; fn setup_run_once(name: &str) -> DynamicExecutor { let (executor, _) = setup_network(name); diff --git a/node-graph/interpreted-executor/benches/update_executor_iai.rs b/node-graph/interpreted-executor/benches/update_executor_iai.rs index 9a016d4441..08dff9f6d2 100644 --- a/node-graph/interpreted-executor/benches/update_executor_iai.rs +++ b/node-graph/interpreted-executor/benches/update_executor_iai.rs @@ -2,8 +2,9 @@ mod benchmark_util; use benchmark_util::setup_network; use graph_craft::proto::ProtoNetwork; -use iai_callgrind::{black_box, library_benchmark, library_benchmark_group, main}; +use iai_callgrind::{library_benchmark, library_benchmark_group, main}; use interpreted_executor::dynamic_executor::DynamicExecutor; +use std::hint::black_box; fn setup_update_executor(name: &str) -> (DynamicExecutor, ProtoNetwork) { let (_, proto_network) = setup_network(name); diff --git a/node-graph/node-macro/src/codegen.rs b/node-graph/node-macro/src/codegen.rs index 57a8b75cfd..05b8d19cc5 100644 --- a/node-graph/node-macro/src/codegen.rs +++ b/node-graph/node-macro/src/codegen.rs @@ -295,6 +295,7 @@ pub(crate) fn generate_node_code(parsed: &ParsedNodeFn) -> syn::Result syn::Result, pub number_mode_range: Option, pub implementations: Punctuated, + pub gpu_image: bool, } +/// a param of `impl Node` with `#[implementation(in -> out)]` #[derive(Clone, Debug)] pub struct NodeParsedField { pub input_type: Type, @@ -529,6 +533,7 @@ fn parse_field(pat_ident: PatIdent, ty: Type, attrs: &[Attribute]) -> syn::Resul .map_err(|e| Error::new_spanned(attr, format!("Invalid `step` for argument '{ident}': {e}\nUSAGE EXAMPLE: #[step(2.)]"))) }) .transpose()?; + let gpu_image = extract_attribute(attrs, "gpu_image").is_some(); let (is_node, node_input_type, node_output_type) = parse_node_type(&ty); let description = attrs @@ -590,6 +595,7 @@ fn parse_field(pat_ident: PatIdent, ty: Type, attrs: &[Attribute]) -> syn::Resul ty, value_source, implementations, + gpu_image, }), name, description, @@ -829,6 +835,7 @@ mod tests { number_hard_max: None, number_mode_range: None, implementations: Punctuated::new(), + gpu_image: false, }), number_display_decimal_places: None, number_step: None, @@ -909,6 +916,7 @@ mod tests { number_hard_max: None, number_mode_range: None, implementations: Punctuated::new(), + gpu_image: false, }), number_display_decimal_places: None, number_step: None, @@ -972,6 +980,7 @@ mod tests { number_hard_max: None, number_mode_range: None, implementations: Punctuated::new(), + gpu_image: false, }), number_display_decimal_places: None, number_step: None, @@ -1038,6 +1047,7 @@ mod tests { p.push(parse_quote!(f64)); p }, + gpu_image: false, }), number_display_decimal_places: None, number_step: None, @@ -1106,6 +1116,7 @@ mod tests { number_hard_max: None, number_mode_range: Some(parse_quote!((0., 100.))), implementations: Punctuated::new(), + gpu_image: false, }), number_display_decimal_places: None, number_step: None, @@ -1167,6 +1178,7 @@ mod tests { number_hard_max: None, number_mode_range: None, implementations: Punctuated::new(), + gpu_image: false, }), number_display_decimal_places: None, number_step: None, diff --git a/node-graph/node-macro/src/shader_nodes.rs b/node-graph/node-macro/src/shader_nodes/mod.rs similarity index 50% rename from node-graph/node-macro/src/shader_nodes.rs rename to node-graph/node-macro/src/shader_nodes/mod.rs index 919d3ef878..0720869d01 100644 --- a/node-graph/node-macro/src/shader_nodes.rs +++ b/node-graph/node-macro/src/shader_nodes/mod.rs @@ -1,10 +1,13 @@ -use crate::parsing::NodeFnAttributes; +use crate::parsing::{NodeFnAttributes, ParsedNodeFn}; +use crate::shader_nodes::per_pixel_adjust::PerPixelAdjust; use proc_macro2::{Ident, TokenStream}; use quote::quote; -use strum::{EnumString, VariantNames}; +use strum::VariantNames; use syn::Error; use syn::parse::{Parse, ParseStream}; +pub mod per_pixel_adjust; + pub const STD_FEATURE_GATE: &str = "std"; pub fn modify_cfg(attributes: &NodeFnAttributes) -> TokenStream { @@ -16,17 +19,33 @@ pub fn modify_cfg(attributes: &NodeFnAttributes) -> TokenStream { } } -#[derive(Debug, EnumString, VariantNames)] +#[derive(Debug, VariantNames)] pub(crate) enum ShaderNodeType { - PerPixelAdjust, + PerPixelAdjust(PerPixelAdjust), } impl Parse for ShaderNodeType { fn parse(input: ParseStream) -> syn::Result { let ident: Ident = input.parse()?; Ok(match ident.to_string().as_str() { - "PerPixelAdjust" => ShaderNodeType::PerPixelAdjust, + "PerPixelAdjust" => ShaderNodeType::PerPixelAdjust(PerPixelAdjust::parse(input)?), _ => return Err(Error::new_spanned(&ident, format!("attr 'shader_node' must be one of {:?}", Self::VARIANTS))), }) } } + +pub trait CodegenShaderEntryPoint { + fn codegen_shader_entry_point(&self, parsed: &ParsedNodeFn) -> syn::Result; +} + +impl CodegenShaderEntryPoint for ShaderNodeType { + fn codegen_shader_entry_point(&self, parsed: &ParsedNodeFn) -> syn::Result { + if parsed.is_async { + return Err(Error::new_spanned(&parsed.fn_name, "Shader nodes must not be async")); + } + + match self { + ShaderNodeType::PerPixelAdjust(x) => x.codegen_shader_entry_point(parsed), + } + } +} diff --git a/node-graph/node-macro/src/shader_nodes/per_pixel_adjust.rs b/node-graph/node-macro/src/shader_nodes/per_pixel_adjust.rs new file mode 100644 index 0000000000..0e220c8aae --- /dev/null +++ b/node-graph/node-macro/src/shader_nodes/per_pixel_adjust.rs @@ -0,0 +1,110 @@ +use crate::parsing::{ParsedFieldType, ParsedNodeFn, RegularParsedField}; +use crate::shader_nodes::CodegenShaderEntryPoint; +use proc_macro2::{Ident, TokenStream}; +use quote::{ToTokens, format_ident, quote}; +use std::borrow::Cow; +use syn::parse::{Parse, ParseStream}; + +#[derive(Debug)] +pub struct PerPixelAdjust {} + +impl Parse for PerPixelAdjust { + fn parse(_input: ParseStream) -> syn::Result { + Ok(Self {}) + } +} + +impl CodegenShaderEntryPoint for PerPixelAdjust { + fn codegen_shader_entry_point(&self, parsed: &ParsedNodeFn) -> syn::Result { + let fn_name = &parsed.fn_name; + let gpu_mod = format_ident!("{}_gpu", parsed.fn_name); + let spirv_image_ty = quote!(Image2d); + + // bindings for images start at 1 + let mut binding_cnt = 0; + let params = parsed + .fields + .iter() + .map(|f| { + let ident = &f.pat_ident; + match &f.ty { + ParsedFieldType::Node { .. } => Err(syn::Error::new_spanned(ident, "PerPixelAdjust shader nodes cannot accept other nodes as generics")), + ParsedFieldType::Regular(RegularParsedField { gpu_image: false, ty, .. }) => Ok(Param { + ident: Cow::Borrowed(&ident.ident), + ty: Cow::Owned(ty.to_token_stream()), + param_type: ParamType::Uniform, + }), + ParsedFieldType::Regular(RegularParsedField { gpu_image: true, .. }) => { + binding_cnt += 1; + Ok(Param { + ident: Cow::Owned(format_ident!("image_{}", &ident.ident)), + ty: Cow::Borrowed(&spirv_image_ty), + param_type: ParamType::Image { binding: binding_cnt }, + }) + } + } + }) + .collect::>>()?; + + let uniform_members = params + .iter() + .filter_map(|Param { ident, ty, param_type }| match param_type { + ParamType::Image { .. } => None, + ParamType::Uniform => Some(quote! {#ident: #ty}), + }) + .collect::>(); + let image_params = params + .iter() + .filter_map(|Param { ident, ty, param_type }| match param_type { + ParamType::Image { binding } => Some(quote! {#[spirv(descriptor_set = 0, binding = #binding)] #ident: &#ty}), + ParamType::Uniform => None, + }) + .collect::>(); + let call_args = params + .iter() + .map(|Param { ident, param_type, .. }| match param_type { + ParamType::Image { .. } => quote!(Color::from_vec4(#ident.fetch_with(texel_coord, lod(0)))), + ParamType::Uniform => quote!(uniform.#ident), + }) + .collect::>(); + let context = quote!(()); + + Ok(quote! { + pub mod #gpu_mod { + use super::*; + use graphene_core_shaders::color::Color; + use spirv_std::spirv; + use spirv_std::glam::{Vec4, Vec4Swizzles}; + use spirv_std::image::{Image2d, ImageWithMethods}; + use spirv_std::image::sample_with::lod; + + pub struct Uniform { + #(#uniform_members),* + } + + #[spirv(fragment)] + pub fn entry_point( + #[spirv(frag_coord)] frag_coord: Vec4, + color_out: &mut Vec4, + #[spirv(descriptor_set = 0, binding = 0, storage_buffer)] uniform: &Uniform, + #(#image_params),* + ) { + let texel_coord = frag_coord.xy().as_uvec2(); + let color: Color = #fn_name(#context, #(#call_args),*); + *color_out = color.to_vec4(); + } + } + }) + } +} + +struct Param<'a> { + ident: Cow<'a, Ident>, + ty: Cow<'a, TokenStream>, + param_type: ParamType, +} + +enum ParamType { + Image { binding: u32 }, + Uniform, +}