3 Commits

Author SHA1 Message Date
bspeice dfc9cf821c Fix palette blend modes, add unit tests
CI / cargo fmt (push) Successful in 21s
CI / cargo test (GPU) (push) Has been cancelled
CI / cargo test (push) Has been cancelled
2026-07-03 16:18:01 -04:00
bspeice 54ba76b380 Implement camera colors
CI / cargo fmt (push) Successful in 22s
CI / cargo test (push) Successful in 12m34s
CI / cargo test (GPU) (push) Successful in 12m51s
Still needs some unit tests, and to fix the gasket example
2026-07-03 14:07:45 -04:00
bspeice a1b9a274f7 Implement transform colors 2026-07-03 11:30:40 -04:00
15 changed files with 1218 additions and 387 deletions
+2 -5
View File
@@ -21,10 +21,7 @@ jobs:
steps:
- uses: actions/checkout@v6
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
components: clippy
- run: cargo check --all-targets
- run: cargo clippy --all-targets
- run: cargo check
- run: cargo test
test-gpu:
@@ -33,5 +30,5 @@ jobs:
steps:
- uses: actions/checkout@v6
- uses: actions-rust-lang/setup-rust-toolchain@v1
- run: cargo install --git https://github.com/rust-gpu/rust-gpu cargo-gpu --rev 67f1ff2
- run: cargo install --git https://github.com/rust-gpu/rust-gpu cargo-gpu
- run: cargo gpu check --auto-install-rust-toolchain -p enkou-shaders
Generated
+687 -3
View File
@@ -130,6 +130,9 @@ name = "bitflags"
version = "2.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8"
dependencies = [
"serde_core",
]
[[package]]
name = "bitstream-io"
@@ -178,6 +181,68 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
[[package]]
name = "camino"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4ce8d3bd5823c7504d3f579f13e7b2f3da252fcb938c594d5680ee508bf846f"
dependencies = [
"serde_core",
]
[[package]]
name = "cargo-gpu-install"
version = "0.10.0-alpha.1"
source = "git+https://github.com/Rust-GPU/rust-gpu.git?rev=67f1ff2#67f1ff27272a1dff82776a5ad7388c738e0b31be"
dependencies = [
"anyhow",
"cargo_metadata",
"directories",
"log",
"serde",
"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",
"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",
]
[[package]]
name = "cc"
version = "1.2.65"
@@ -242,6 +307,38 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
[[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-sys"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys",
]
[[package]]
name = "displaydoc"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ac70aa55017e108007fbaf5aa0f54b021c98f92ff8af59d42eda9da96e3dd4f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "either"
version = "1.16.0"
@@ -263,6 +360,15 @@ dependencies = [
"tempfile",
]
[[package]]
name = "enkou-shaders-tests"
version = "0.1.0"
dependencies = [
"anyhow",
"cargo-gpu-install",
"rspirv 0.13.0+sdk-1.4.341.0",
]
[[package]]
name = "equator"
version = "0.4.2"
@@ -283,6 +389,23 @@ dependencies = [
"syn",
]
[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "erased-serde"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2add8a07dd6a8d93ff627029c51de145e12686fbc36ecb298ac22e74cf02dec"
dependencies = [
"serde",
"serde_core",
"typeid",
]
[[package]]
name = "errno"
version = "0.3.14"
@@ -344,6 +467,26 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "form_urlencoded"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf"
dependencies = [
"percent-encoding",
]
[[package]]
name = "getrandom"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "getrandom"
version = "0.3.4"
@@ -387,6 +530,115 @@ dependencies = [
"zerocopy",
]
[[package]]
name = "hashbrown"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
[[package]]
name = "icu_collections"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2984d1cd16c883d7935b9e07e44071dca8d917fd52ecc02c04d5fa0b5a3f191c"
dependencies = [
"displaydoc",
"potential_utf",
"utf8_iter",
"yoke",
"zerofrom",
"zerovec",
]
[[package]]
name = "icu_locale_core"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92219b62b3e2b4d88ac5119f8904c10f8f61bf7e95b640d25ba3075e6cac2c29"
dependencies = [
"displaydoc",
"litemap",
"tinystr",
"writeable",
"zerovec",
]
[[package]]
name = "icu_normalizer"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c56e5ee99d6e3d33bd91c5d85458b6005a22140021cc324cea84dd0e72cff3b4"
dependencies = [
"icu_collections",
"icu_normalizer_data",
"icu_properties",
"icu_provider",
"smallvec",
"zerovec",
]
[[package]]
name = "icu_normalizer_data"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da3be0ae77ea334f4da67c12f149704f19f81d1adf7c51cf482943e84a2bad38"
[[package]]
name = "icu_properties"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bee3b67d0ea5c2cca5003417989af8996f8604e34fb9ddf96208a033901e70de"
dependencies = [
"icu_collections",
"icu_locale_core",
"icu_properties_data",
"icu_provider",
"zerotrie",
"zerovec",
]
[[package]]
name = "icu_properties_data"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e2bbb201e0c04f7b4b3e14382af113e17ba4f63e2c9d2ee626b720cbce54a14"
[[package]]
name = "icu_provider"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "139c4cf31c8b5f33d7e199446eff9c1e02decfc2f0eec2c8d71f65befa45b421"
dependencies = [
"displaydoc",
"icu_locale_core",
"writeable",
"yoke",
"zerofrom",
"zerotrie",
"zerovec",
]
[[package]]
name = "idna"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de"
dependencies = [
"idna_adapter",
"smallvec",
"utf8_iter",
]
[[package]]
name = "idna_adapter"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714"
dependencies = [
"icu_normalizer",
"icu_properties",
]
[[package]]
name = "image"
version = "0.25.10"
@@ -426,6 +678,16 @@ version = "1.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89194689a993ab15268672e99e7b0e19da2da3268ac682e8f02d29d4d1434cd7"
[[package]]
name = "indexmap"
version = "2.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "interpolate_name"
version = "0.2.4"
@@ -446,13 +708,19 @@ dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
[[package]]
name = "jobserver"
version = "0.1.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9afb3de4395d6b3e67a780b6de64b51c978ecf11cb9a462c66be7d4ca9039d33"
dependencies = [
"getrandom",
"getrandom 0.3.4",
"libc",
]
@@ -484,12 +752,27 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
[[package]]
name = "libredox"
version = "0.1.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f02ab6bace2054fb888a3c16f990117b579d14a3088e472d63c6011fa185c9d3"
dependencies = [
"libc",
]
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
[[package]]
name = "litemap"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0"
[[package]]
name = "log"
version = "0.4.32"
@@ -627,6 +910,21 @@ version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[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 = "paste"
version = "1.0.15"
@@ -639,6 +937,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35fb2e5f958ec131621fdd531e9fc186ed768cbe395337403ae56c17a74c68ec"
[[package]]
name = "percent-encoding"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "png"
version = "0.18.1"
@@ -652,6 +956,15 @@ dependencies = [
"miniz_oxide",
]
[[package]]
name = "potential_utf"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0103b1cef7ec0cf76490e969665504990193874ea05c85ff9bab8b911d0a0564"
dependencies = [
"zerovec",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"
@@ -760,7 +1073,7 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
dependencies = [
"getrandom",
"getrandom 0.3.4",
]
[[package]]
@@ -827,6 +1140,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 = "rayon"
version = "1.12.0"
@@ -847,12 +1166,62 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "redox_users"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4e608c6638b9c18977b00b475ac1f28d14e84b27d8d42f70e0bf1e3dec127ac"
dependencies = [
"getrandom 0.2.17",
"libredox",
"thiserror",
]
[[package]]
name = "rgb"
version = "0.8.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47b34b781b31e5d73e9fbc8689c70551fd1ade9a19e3e28cfec8580a79290cc4"
[[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",
"spirv 0.3.0+sdk-1.3.268.0",
]
[[package]]
name = "rspirv"
version = "0.13.0+sdk-1.4.341.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "091dca2e1d6fd3098417b5ec88e77e80d1ba5945750943419dc976858082c296"
dependencies = [
"rustc-hash",
"spirv 0.4.0+sdk-1.4.341.0",
]
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustc_codegen_spirv-types"
version = "0.10.0-alpha.1"
source = "git+https://github.com/Rust-GPU/rust-gpu.git?rev=67f1ff2#67f1ff27272a1dff82776a5ad7388c738e0b31be"
dependencies = [
"rspirv 0.12.0+sdk-1.3.268.0",
"semver",
"serde",
"serde_json",
"spirv 0.3.0+sdk-1.3.268.0",
"thiserror",
]
[[package]]
name = "rustix"
version = "1.1.4"
@@ -872,6 +1241,90 @@ version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "semver"
version = "1.0.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd"
dependencies = [
"serde",
"serde_core",
]
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
"serde_derive",
]
[[package]]
name = "serde-untagged"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9faf48a4a2d2693be24c6289dbe26552776eb7737074e6722891fadbe6c5058"
dependencies = [
"erased-serde",
"serde",
"serde_core",
"typeid",
]
[[package]]
name = "serde-value"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c"
dependencies = [
"ordered-float",
"serde",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "serde_spanned"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3"
dependencies = [
"serde",
]
[[package]]
name = "shlex"
version = "2.0.1"
@@ -899,6 +1352,41 @@ version = "1.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90"
[[package]]
name = "spirv"
version = "0.3.0+sdk-1.3.268.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eda41003dc44290527a59b13432d4a0379379fa074b70174882adfbdfd917844"
dependencies = [
"bitflags 2.13.0",
"serde",
]
[[package]]
name = "spirv"
version = "0.4.0+sdk-1.4.341.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9571ea910ebd84c86af4b3ed27f9dbdc6ad06f17c5f96146b2b671e2976744f"
dependencies = [
"bitflags 2.13.0",
]
[[package]]
name = "spirv-builder"
version = "0.10.0-alpha.1"
source = "git+https://github.com/Rust-GPU/rust-gpu.git?rev=67f1ff2#67f1ff27272a1dff82776a5ad7388c738e0b31be"
dependencies = [
"cargo_metadata",
"log",
"memchr",
"raw-string",
"rustc_codegen_spirv-types",
"semver",
"serde",
"serde_json",
"thiserror",
]
[[package]]
name = "spirv-std"
version = "0.10.0-alpha.1"
@@ -945,6 +1433,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tempfile"
version = "3.27.0"
@@ -952,7 +1451,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
dependencies = [
"fastrand",
"getrandom",
"getrandom 0.3.4",
"once_cell",
"rustix",
"windows-sys",
@@ -992,12 +1491,93 @@ dependencies = [
"zune-jpeg",
]
[[package]]
name = "tinystr"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8323304221c2a851516f22236c5722a72eaa19749016521d6dff0824447d96d"
dependencies = [
"displaydoc",
"zerovec",
]
[[package]]
name = "toml"
version = "0.8.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
dependencies = [
"indexmap",
"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 = "typeid"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c"
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "unicode-xid"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
[[package]]
name = "url"
version = "2.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
"serde",
]
[[package]]
name = "utf8_iter"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "v_frame"
version = "0.3.9"
@@ -1009,6 +1589,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "wasip2"
version = "1.0.4+wasi-0.2.12"
@@ -1084,18 +1670,56 @@ dependencies = [
"windows-link",
]
[[package]]
name = "winnow"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945"
dependencies = [
"memchr",
]
[[package]]
name = "wit-bindgen"
version = "0.57.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e"
[[package]]
name = "writeable"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ffae5123b2d3fc086436f8834ae3ab053a283cfac8fe0a0b8eaae044768a4c4"
[[package]]
name = "y4m"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5a4b21e1a62b67a2970e6831bc091d7b87e119e7f9791aef9702e3bef04448"
[[package]]
name = "yoke"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5"
dependencies = [
"stable_deref_trait",
"yoke-derive",
"zerofrom",
]
[[package]]
name = "yoke-derive"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de844c262c8848816172cef550288e7dc6c7b7814b4ee56b3e1553f275f1858e"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zerocopy"
version = "0.8.52"
@@ -1116,6 +1740,66 @@ dependencies = [
"syn",
]
[[package]]
name = "zerofrom"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272"
dependencies = [
"zerofrom-derive",
]
[[package]]
name = "zerofrom-derive"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11532158c46691caf0f2593ea8358fed6bbf68a0315e80aae9bd41fbade684a1"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zerotrie"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f9152d31db0792fa83f70fb2f83148effb5c1f5b8c7686c3459e361d9bc20bf"
dependencies = [
"displaydoc",
"yoke",
"zerofrom",
]
[[package]]
name = "zerovec"
version = "0.11.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90f911cbc359ab6af17377d242225f4d75119aec87ea711a880987b18cd7b239"
dependencies = [
"yoke",
"zerofrom",
"zerovec-derive",
]
[[package]]
name = "zerovec-derive"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "625dc425cab0dca6dc3c3319506e6593dcb08a9f387ea3b284dbd52a92c40555"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zmij"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
[[package]]
name = "zune-core"
version = "0.5.1"
+3 -2
View File
@@ -1,6 +1,7 @@
[workspace]
members = [
"enkou-shaders",
"enkou-shaders-tests",
]
resolver = "3"
@@ -12,7 +13,6 @@ license = "MIT"
repository = ""
[workspace.lints.rust]
missing_docs = { level = "warn" }
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(target_arch, values("spirv"))'] }
[workspace.dependencies]
@@ -24,6 +24,7 @@ bytemuck = { version = "1.25.0", features = ["derive"] }
glam = { version = "0.33.1", default-features = false, features = ["bytemuck", "scalar-math"] }
image = { version = "0.25.10", default-features = false, features = ["default-formats"]}
libm = "0.2.16"
rand = { version = "0.10.1", default-features = false }
rand = { version = "0.10.1", default-features = false }
rand_xoshiro = "0.8.1"
rspirv = "0.13.0"
tempfile = "3.27.0"
+18
View File
@@ -0,0 +1,18 @@
[package]
name = "enkou-shaders-tests"
publish = false
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
[lints]
workspace = true
[dependencies]
rspirv.workspace = true
[build-dependencies]
anyhow.workspace = true
cargo-gpu-install.workspace = true
+30
View File
@@ -0,0 +1,30 @@
use cargo_gpu_install::install::Install;
use cargo_gpu_install::spirv_builder::{Capability, ShaderPanicStrategy, SpirvMetadata};
use std::path::PathBuf;
pub fn main() -> anyhow::Result<()> {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let crate_path = [manifest_dir, "..", "enkou-shaders"]
.iter()
.copied()
.collect::<PathBuf>();
let install = Install::from_shader_crate(crate_path.clone())
.within_build_script()
.run()?;
let mut builder = install.to_spirv_builder(crate_path, "spirv-unknown-vulkan1.3");
builder.build_script.defaults = true;
builder.shader_panic_strategy = ShaderPanicStrategy::SilentExit;
builder.spirv_metadata = SpirvMetadata::Full;
builder.capabilities = vec![
Capability::Int8,
Capability::Int16,
Capability::Int64,
Capability::Float64,
];
let compile_result = builder.build()?;
let spv_path = compile_result.module.unwrap_single();
println!("cargo::rustc-env=SHADER_SPV_PATH={}", spv_path.display());
Ok(())
}
+73
View File
@@ -0,0 +1,73 @@
#[cfg(test)]
mod test {
use rspirv::binary::parse_bytes;
use rspirv::dr::{Module, Operand};
use rspirv::spirv::ExecutionModel;
use std::sync::OnceLock;
static SHADER_MODULE: OnceLock<Module> = OnceLock::new();
fn shader() -> &'static Module {
SHADER_MODULE.get_or_init(|| {
let shader_bytes = include_bytes!(env!("SHADER_SPV_PATH"));
let mut loader = rspirv::dr::Loader::new();
parse_bytes(shader_bytes, &mut loader).expect("Unable to parse shader");
loader.module()
})
}
fn has_entry_point(execution_model: ExecutionModel, name: &str) -> bool {
for ref entry_point in shader().entry_points.iter() {
let operands: Vec<Operand> = entry_point
.operands
.iter()
.filter(|op| match op {
Operand::ExecutionModel(_) | Operand::LiteralString(_) => true,
_ => false,
})
.map(|op| op.clone())
.collect();
assert_eq!(operands.len(), 2);
match &operands[0] {
Operand::ExecutionModel(actual) => {
if execution_model != *actual {
continue;
}
}
op => panic!("Unexpected operand; {}", op),
}
match &operands[1] {
Operand::LiteralString(actual) => {
if name != actual {
continue;
}
}
op => panic!("Unexpected operand; {}", op),
}
return true;
}
false
}
#[test]
pub fn has_entry_main_chaos_game() {
assert!(has_entry_point(
ExecutionModel::GLCompute,
"main_chaos_game"
))
}
#[test]
pub fn has_entry_main_image_render() {
assert!(has_entry_point(
ExecutionModel::GLCompute,
"main_image_render"
))
}
}
-106
View File
@@ -1,106 +0,0 @@
//! # Fractal flame
//!
//! The `flam3` sources include example parameters (`test.flam3`) to demonstrate the fractal flame algorithm,
//! which are copied here and used to generate an image that can be compared against the reference `flam3` renderer
use anyhow::Result;
use enkou_shaders::chaos_game::ChaosGame;
use enkou_shaders::transform::Transform;
use enkou_shaders::variation::{Variation, VariationKind};
use glam::{UVec2, UVec3, UVec4, Vec2, Vec2Swizzles, Vec4};
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256StarStar;
use enkou_shaders::camera::Camera;
use enkou_shaders::image::BlendMode;
const ITERATIONS_DISCARD: usize = 100;
const IMAGE_DIMENSIONS: UVec2 = UVec2::new(640, 480);
const IMAGE_QUALITY: f32 = 10.0;
// Palette data copied from `flam3-palettes.xml`.
// Palettes are ASCII hexadecimal strings; two characters form a byte,
// and bytes are grouped in triplets to get a single RGB color.
const PALETTE: &'static str = "\
00dadebc00eee6c500eef2ce00eef2cf00e6eee100eaeed800f2f1eb00f2f5d8
00e6f2ce00deeac500d6dac600ced2bc00c2caa900becaa000ced6aa00dee2c5
00eaedce00eaf2c500dee2c500c2caaa00aebeaa00a5b29600a2a98d0096a284
008d8d7a0085897100858d710085856700797d6700797d670071795e00656d55
004d5d4200344025003040250030381c002c3c1c002c341c00242c1200242400
00242c0900283409003840120030401c0040502f0055694200657555006c7d5e
00748d710074898400748d8400788d840079897a0079857100757d670071795e
006c715e006d705e006c795e0068755e00697155006d7555006d755500697155
0065715500696d550064715e006870670068706700686c67006c6c5e0071715e
0079796700818571007d91710085927a0085927a007d9284007992840078928d
00788d8d00748d84007492840075927a006c85670064795e0059694b00aa5700
0038441c00303c1c002c3c1c003440250050614b005d6d5e0064715e0060715e
0060755e0068755e006c795e006c795e0071796700707967006c7d6700687967
006c7967006c75670071755e0071755e0075795e00757d5e00818d5e008d925e
008d9267009a9a71009aa27a009aa27a009aa17a00929a71008992670081855e
007d7d550069794b00616d4200444c250038441c0040512500454d2500716d42
00797d4b00817d5500797955006d755500697d55006c795e006579540068795e
00647967006479670068755e0064715e00646c5e00656d55004d584200344025
002c381c0020281c001c1409001818000004140000081000000c1800001c2809
00243012003c4425005d6555007579550085895e008991710096a271009aa27a
009eaa7a009eaa7a00aaae7100a6aa7a00a2aa7a00a1a57a00969e7a0085967a
0081927a0078927a0075927a00758d7a00708167007d7d670089896700929a71
009eaa7a00aab68400b2b68d00b6ba9700c2ca9700b2be8d00b2b68d00aab28d
00a2ae84009aa67a00929e7a00859a7a007d967a007d927a007d9284007d9284
0081968400859684008596840081928400859a8400859a84008d9a8400929684
009ea98400aeb28400aaba8400b2be8d00b6c2a000c6caa000c6ceaa00d6dab3
00dae2c500d2d6bc00bec2a000aab68d009ea67a00929a710089897100817d67
007d7d6700817867007d7d5e0079795e0079815e00817d6700817d6700818167
008189710085917a0089927a00969d7a00969e7a0092968400969a8d00929284
0089918400819284007d928d0078928d0074928d0078928d007896970081968d
0081968d00819a8d00859a8d00899e8d00899e8d008da2970095a297008da297
0096a68d009aa18d009ea984009ea67a00a2a571009ea671009aa67100959d71";
/// Generate an image
pub fn main() -> Result<()> {
let transforms: &[Transform] = &[];
let weights = [0.25; 4];
let variations = [Variation::new(
VariationKind::Spherical,
1.0,
[0.0; 4].into(),
)];
let camera = Camera::new(IMAGE_DIMENSIONS, Vec2::ZERO, 0.0, Vec2::ZERO, (IMAGE_DIMENSIONS / 2).as_vec2());
let palette_chars: String = PALETTE.chars().filter(|c| c.is_alphanumeric()).collect();
let palette_bytes: Vec<u8> = (0..palette_chars.len())
.step_by(2)
.map(|i| {
let s = &palette_chars[i..i + 2];
let b = u8::from_str_radix(s, 16).expect("illegal palette value");
b
})
.collect();
let palette_colors: Vec<Vec4> = palette_bytes
.chunks(3)
.map(|chunk| [chunk[0] as f32, chunk[1] as f32, chunk[2] as f32, 255.0].into())
.collect();
let mut rng = Xoshiro256StarStar::from_seed([4u8; 32]);
let chaos_game = ChaosGame::new(&mut rng, transforms, &weights, &variations);
let mut image_accum: Vec<Vec4> = Vec::new();
image_accum.resize(IMAGE_DIMENSIONS.element_product() as usize, Vec4::ZERO);
let iterations: usize = (IMAGE_DIMENSIONS.element_product() as f32 * IMAGE_QUALITY) as usize;
chaos_game.skip(ITERATIONS_DISCARD).take(iterations).for_each(|(ifs_point, ifs_color)| {
let pixel_point = camera.transform_point_to_image(ifs_point);
if pixel_point.is_none() {
return;
}
let pixel_point = pixel_point.unwrap();
let pixel_index = pixel_point.y * IMAGE_DIMENSIONS.x + pixel_point.x;
let pixel_color = BlendMode::Linear.ifs_to_rgb(ifs_color, palette_colors.as_ref());
image_accum[pixel_index as usize] += pixel_color;
});
Ok(())
}
+67 -48
View File
@@ -1,51 +1,58 @@
//! # Sierpinski Gasket
//!
//! The Sierpinski Gasket is a simple iterated function system (IFS) that demonstrates
//! the basic principles of using the "chaos game" to generate images. Using a set of three
//! transforms, generate new points in the function system using the chaos game,
//! then plot those points and display the resulting image.
use anyhow::{Context, Result};
use enkou_shaders::Coefficients2;
use enkou_shaders::camera::Camera;
use enkou_shaders::chaos_game::ChaosGame;
use enkou_shaders::camera::entry::main_image_render;
use enkou_shaders::chaos_game::entry::main_chaos_game;
use enkou_shaders::transform::Transform;
use enkou_shaders::variation::Variation;
use glam::{Affine2, UVec2, Vec2, uvec2};
use image::{GrayImage, Luma};
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256StarStar;
use glam::{Affine2, UVec2, Vec2, Vec4, uvec2, vec2};
use image::{Rgba, Rgba32FImage};
use std::mem;
use std::process::Command;
use tempfile::NamedTempFile;
const ITERATIONS_DISCARD: usize = 20;
const ITERATIONS_DISCARD: u32 = 20;
const ITERATIONS: u32 = 50_000;
const IMAGE_DIMENSION: UVec2 = uvec2(600, 600);
const IMAGE_QUALITY: f32 = 1.0;
/// Build and display a simple fractal - the Sierpinski Gasket
pub fn main() -> Result<()> {
let mut rng = Xoshiro256StarStar::from_seed([4u8; 32]);
let transforms = [
{
// F_0: (x / 2, y / 2)
let coefficients = Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.0);
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1), 0.0, 0.5)
},
{
// F_1: ((x + 1) / 2, y / 2)
let coefficients = Affine2::from_coefficients(0.5, 0.0, 0.5, 0.0, 0.5, 0.0);
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1), 0.5, 0.5)
},
{
// F_2: (x / 2, (y + 1) / 2)
let coefficients = Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.5);
Transform::new(coefficients, Affine2::IDENTITY, uvec2(0, 1), 1.0, 0.5)
},
// F_0: (x / 2, y / 2)
Transform::new(
Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.0),
uvec2(0, 1),
vec2(0.0, 0.0),
),
// F_1: ((x + 1) / 2, y / 2)
Transform::new(
Affine2::from_coefficients(0.5, 0.0, 0.5, 0.0, 0.5, 0.0),
uvec2(0, 1),
vec2(0.0, 0.0),
),
// F_2: (x / 2, (y + 1) / 2)
Transform::new(
Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.5),
uvec2(0, 1),
vec2(0.0, 0.0),
),
];
let weights = [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0];
let variations = [Variation::IDENTITY];
let mut output_points_ifs = Vec::new();
output_points_ifs.resize(ITERATIONS as usize, Vec4::ZERO);
main_chaos_game(
ITERATIONS_DISCARD,
&[4u8; 32],
&transforms,
&weights,
&variations,
&mut output_points_ifs,
);
// The gasket is defined on the range [0, 1] for both X and Y
let camera = Camera::new(
IMAGE_DIMENSION,
@@ -55,28 +62,40 @@ pub fn main() -> Result<()> {
IMAGE_DIMENSION.as_vec2(),
);
let mut image = GrayImage::new(IMAGE_DIMENSION.x, IMAGE_DIMENSION.y);
let chaos_game = ChaosGame::new(&mut rng, &transforms, &weights, &variations);
let palette = &[Vec4::ONE; 2];
let iterations = (IMAGE_DIMENSION.element_product() as f32 * IMAGE_QUALITY) as usize;
chaos_game
.skip(ITERATIONS_DISCARD)
.take(iterations)
.for_each(|(ifs_point, _)| {
if let Some(pixel_point) = camera.transform_point_to_image(ifs_point) {
image.put_pixel(pixel_point.x, pixel_point.y, Luma([255u8]));
}
});
let mut output_points_pixel = Vec::new();
output_points_pixel.resize(ITERATIONS as usize, Vec4::ZERO);
main_image_render(
&camera,
palette,
&output_points_ifs,
&mut output_points_pixel,
);
let mut image = Rgba32FImage::new(IMAGE_DIMENSION.x, IMAGE_DIMENSION.y);
for x in 0..image.dimensions().0 {
for y in 0..image.dimensions().1 {
let pixel_index = y * IMAGE_DIMENSION.x + x;
let pixel = output_points_pixel[pixel_index as usize];
image.put_pixel(x, y, Rgba(pixel.into()));
}
}
let temp = NamedTempFile::with_suffix(".png").context("Unable to create file for image")?;
image.save(temp.path()).context("Unable to save image")?;
let mut command = cfg_select! {
unix => Command::new("xdg-open").arg(temp.path()).spawn(),
windows => Command::new("PowerShell").arg("-Command").arg(format!("start {}", temp.path().display())).spawn(),
_ => Err(anyhow::anyhow!("No available program to open images"))?
}?;
command.wait()?;
let open_program: &str = cfg_select! {
unix => Some("xdg-open"),
_ => None,
}
.expect("No available program to open images");
Command::new(open_program)
.arg(temp.path())
.spawn()?
.wait()?;
// In case the image viewer forks and gives control back prior to reading the file,
// drop it and don't run the destructor
+137 -47
View File
@@ -2,8 +2,47 @@
//!
//! Map points from the IFS coordinate system to pixel coordinates. This is a lossy transformation.
use bytemuck::{Pod, Zeroable};
use glam::{Affine2, IVec2, UVec2, Vec2, vec2};
use libm::powf;
use glam::{Affine2, IVec2, UVec2, Vec2, Vec4, Vec4Swizzles, vec2};
use libm::{floorf, powf};
/// Blending modes for mapping IFS color values (which are on a scale `[0, 1]`)
/// to RGBA colors.
#[derive(Copy, Clone)]
#[repr(u32)]
pub enum BlendMode {
/// Map IFS color values to a linear blend of the nearest two palette colors
Linear = 0,
/// Map IFS color values to the nearest single palette color
Step = 1,
}
impl Default for BlendMode {
fn default() -> Self {
BlendMode::Linear
}
}
impl BlendMode {
/// Map an IFS color value to RGBA color from the provided palette.
pub fn ifs_to_rgb(&self, color: f32, palette: &[Vec4]) -> Vec4 {
let colors_m_one = palette.len() - 1;
let period = 1.0 / colors_m_one as f32;
let index_lower = floorf(color / period) as usize;
let index_upper = (index_lower + 1).clamp(0, colors_m_one);
let rem = color % period / period;
match self {
BlendMode::Linear => palette[index_lower].lerp(palette[index_upper], rem),
BlendMode::Step => palette[index_lower],
}
}
}
// UNSAFE: Sound because enum has guaranteed layout (u32) and defined zero-value
unsafe impl bytemuck::Zeroable for BlendMode {}
// UNSAFE: Sound because enum has guaranteed layout (u32) and defined zero-value
unsafe impl bytemuck::Pod for BlendMode {}
/// Settings used to map IFS coordinates to pixel coordinates.
///
@@ -14,6 +53,8 @@ use libm::powf;
pub struct Camera {
dimensions: UVec2,
transform: Affine2,
blend_mode: BlendMode,
image_gamma: f32,
}
impl Camera {
@@ -26,14 +67,14 @@ impl Camera {
///
/// * `dimensions` - Width and height of the output image (in pixels).
/// * `center` - Location of the origin in IFS coordinates. Positive `x` shifts the image
/// left, and positive `y` position shifts the image up.
/// left, and positive `y` position shifts the image up.
/// * `rotate` - Rotation angle (in radians) of IFS coordinates. Rotation is applied after the
/// `center` translation, so it is about the new origin.
/// `center` translation, so it is about the new origin.
/// * `zoom` - Zoom factor applied to IFS coordinates. IFS coordinates are scaled by
/// `pow(2, zoom)`, so a zoom factor of 0 is the identity.
/// `pow(2, zoom)`, so a zoom factor of 0 is the identity.
/// * `scale` - Pixels per unit of IFS coordinates. This parameter is usually chosen such
/// that the largest dimension will cover the range `[-2, 2]`, but values higher or lower
/// can be used as a secondary zoom.
/// that the largest dimension will cover the range `[-2, 2]`, but values higher or lower
/// can be used as a secondary zoom.
pub fn new(dimensions: UVec2, center: Vec2, rotate: f32, zoom: Vec2, scale: Vec2) -> Camera {
let ifs_center_transform = Affine2::from_translation(-center);
let zoom_transform = Affine2::from_scale(vec2(powf(2.0, zoom.x), powf(2.0, zoom.y)));
@@ -50,73 +91,122 @@ impl Camera {
Camera {
dimensions,
transform,
blend_mode: BlendMode::Linear,
image_gamma: 1.5,
}
}
/// Map a point from IFS coordinates to pixel coordinates.
///
/// ```
/// # use glam::{vec2, ivec2, uvec2, Vec2};
/// # use crate::enkou_shaders::camera::Camera;
/// // Output image is 600x600 pixels, centered at the origin, no rotation, no zoom,
/// // and scaled such that it covers the range [-2, 2].
/// // Use the origin as the IFS coordinate, so the pixel coordinate is the center of the image
/// let camera = Camera::new(
/// uvec2(600, 600),
/// Vec2::ZERO,
/// 0.0,
/// Vec2::ZERO,
/// vec2(150.0, 150.0)
/// );
/// assert_eq!(camera.transform_point(vec2(0.0, 0.0)), ivec2(300, 300));
/// ```
pub fn transform_point(&self, point: Vec2) -> IVec2 {
fn transform_point(&self, point: Vec2) -> IVec2 {
self.transform.transform_point2(point).as_ivec2()
}
/// Map a point from IFS coordinates to pixel coordinates (like [`transform_point`](Camera::transform_point)),
/// and check that the result is within the provided image dimensions.
pub fn transform_point_to_image(&self, point: Vec2) -> Option<UVec2> {
let pixel_coordinates = self.transform_point(point);
/// Map a point from IFS coordinates to a pixel index and RGBA value; if the IFS coordinate
/// is outside the viewable range, return [`None`].
pub fn transform_point_to_image(&self, point: Vec4, palette: &[Vec4]) -> Option<(usize, Vec4)> {
let pixel_coordinates = self.transform_point(point.xy());
if pixel_coordinates.x < 0
|| pixel_coordinates.y < 0
|| (pixel_coordinates.x as u32) >= self.dimensions.x
|| (pixel_coordinates.y as u32) >= self.dimensions.y
{
None
} else {
Some(pixel_coordinates.as_uvec2())
return None;
}
let to_pixel_index = self.dimensions.with_y(0);
let pixel_index = pixel_coordinates.as_uvec2().dot(to_pixel_index) as usize;
let rgba = self.blend_mode.ifs_to_rgb(point.w, palette);
Some((pixel_index, rgba))
}
}
/// Shader entry point for running the camera transformation over a list of IFS coordinates
#[allow(missing_docs)]
pub mod entry {
use crate::camera::Camera;
use spirv_std::glam::{IVec2, Vec2};
use glam::Vec4;
use libm::log10f;
use spirv_std::spirv;
/// Transform IFS coordinates to pixel coordinates
#[spirv(compute(entry_point_name = "main_camera", threads(1)))]
pub fn main_camera(
/// Render an output image from a list of IFS coordinates.
///
/// Arguments:
/// * `camera` - Camera settings for mapping IFS coordinates to pixel coordinates
/// * `palette` - Color palette to use when mapping IFS color to RGB colors. Individual elements
/// are assumed to be RGBA values on the scale of `[0, 1]`
/// * `coordinates_ifs` - IFS coordinates to use for the output image
/// * `image` - Buffer for the output image
#[spirv(compute(entry_point_name = "main_image_render", threads(1)))]
pub fn main_image_render(
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] camera: &Camera,
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] coordinates_ifs: &[Vec2],
#[spirv(storage_buffer, descriptor_set = 1, binding = 0)] coordinates_pixel: &mut [IVec2],
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] palette: &[Vec4],
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] coordinates_ifs: &[Vec4],
#[spirv(storage_buffer, descriptor_set = 1, binding = 0)] image: &mut [Vec4],
) {
for i in 0..coordinates_ifs.len() {
coordinates_pixel[i] = camera.transform_point(coordinates_ifs[i])
for coordinate_index in 0..coordinates_ifs.len() {
camera
.transform_point_to_image(coordinates_ifs[coordinate_index], palette)
.map(|(pixel_index, rgba)| image[pixel_index] += rgba);
}
for pixel_index in 0..image.len() {
// TODO: Fix the bootleg gamma adjustment
let pixel_unscaled = image[pixel_index];
let pixel =
pixel_unscaled * log10f(pixel_unscaled.w) / (pixel_unscaled.w * camera.image_gamma);
image[pixel_index] = pixel;
}
}
}
#[cfg(test)]
mod test {
use crate::camera::Camera;
use glam::{Affine2, Vec2, ivec2, uvec2, vec2};
use crate::camera::{BlendMode, Camera};
use glam::{Affine2, Vec2, Vec4, ivec2, uvec2, vec2};
use libm::powf;
fn vec4s(value: f32) -> Vec4 {
Vec4::splat(value)
}
#[test]
pub fn manual_camera() {
fn blend_linear() {
let ifs_to_rgb = |color, palette| BlendMode::Linear.ifs_to_rgb(color, palette);
let palette = &[vec4s(0.0), vec4s(1.0)];
assert_eq!(ifs_to_rgb(0.0, palette), vec4s(0.0));
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(0.5));
assert_eq!(ifs_to_rgb(1.0, palette), vec4s(1.0));
let palette = &[vec4s(1.0), vec4s(2.0), vec4s(3.0)];
assert_eq!(ifs_to_rgb(0.0, palette), vec4s(1.0));
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(2.0));
assert_eq!(ifs_to_rgb(1.0, palette), vec4s(3.0));
let palette = &[vec4s(1.0), vec4s(2.0), vec4s(3.0), vec4s(4.0)];
assert_eq!(ifs_to_rgb(0.0, palette), vec4s(1.0));
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(2.5));
assert_eq!(ifs_to_rgb(1.0, palette), vec4s(4.0));
}
#[test]
fn blend_step() {
let ifs_to_rgb = |color, palette| BlendMode::Step.ifs_to_rgb(color, palette);
let palette = &[vec4s(0.0), vec4s(1.0)];
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(0.0));
let palette = &[vec4s(1.0), vec4s(2.0), vec4s(3.0)];
assert_eq!(ifs_to_rgb(0.0, palette), palette[0]);
assert_eq!(ifs_to_rgb(0.25, palette), palette[0]);
assert_eq!(ifs_to_rgb(0.4, palette), palette[0]);
assert_eq!(ifs_to_rgb(0.5, palette), palette[1]);
assert_eq!(ifs_to_rgb(0.7, palette), palette[1]);
assert_eq!(ifs_to_rgb(1.0, palette), palette[2]);
}
#[test]
fn camera_manual() {
let starting_point = vec2(1.0, 1.0);
// Move the origin; points move right and up by one unit, giving us (2.0, 2.0)
@@ -154,7 +244,7 @@ mod test {
}
#[test]
pub fn point_outside_camera() {
fn camera_point_outside_image() {
// Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2]
let camera = Camera::new(
uvec2(1000, 1000),
@@ -169,7 +259,7 @@ mod test {
}
#[test]
pub fn point_outside_camera_negative() {
fn camera_point_outside_image_negative() {
// Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2]
let camera = Camera::new(
uvec2(1000, 1000),
@@ -184,7 +274,7 @@ mod test {
}
#[test]
pub fn aspect_ratio() {
fn camera_aspect_ratio() {
// Scale 100 for an image 1600 x 900 gives an effective X range of [-8, 8],
// and effective Y range of [-4.5, 4.5]
let camera = Camera::new(
+52 -9
View File
@@ -12,7 +12,6 @@
//!
//! This algorithm is also known as the ["chaos game"](https://en.wikipedia.org/wiki/Chaos_game),
//! and it forms the basic system for producing images.
use crate::transform::Transform;
use crate::variation::Variation;
use rand::distr::{Distribution, StandardUniform};
@@ -41,9 +40,9 @@ pub fn step_chaos_game<R: Rng>(
transforms: &[Transform],
weights: &[f32],
variations: &[Variation],
) -> (Vec2, f32) {
) -> (Vec2, f32, usize) {
let mut choice_weight = rng.sample::<f32, _>(StandardUniform);
let mut transform_index: u32 = 0;
let mut transform_index: usize = 0;
for i in 0..weights.len() {
choice_weight -= weights[i];
@@ -54,10 +53,11 @@ pub fn step_chaos_game<R: Rng>(
transform_index += 1;
}
let transform = &transforms[transform_index as usize];
let ref transform = transforms[transform_index];
(
transform.transform_point(rng, variations, point),
transform.transform_color(color),
transform_index,
)
}
@@ -82,11 +82,9 @@ impl<'a, R: Rng> ChaosGame<'a, R> {
weights: &'a [f32],
variations: &'a [Variation],
) -> Self {
let current_point = vec2(rng.sample(BiUnit), rng.sample(BiUnit));
let current_color = rng.sample(StandardUniform);
ChaosGame {
current_point,
current_color,
current_point: vec2(rng.sample(BiUnit), rng.sample(BiUnit)),
current_color: rng.sample(StandardUniform),
rng,
transforms,
weights,
@@ -99,7 +97,7 @@ impl<'a, R: Rng> Iterator for ChaosGame<'a, R> {
type Item = (Vec2, f32);
fn next(&mut self) -> Option<Self::Item> {
let (next_point, next_color) = step_chaos_game(
let (next_point, next_color, _) = step_chaos_game(
self.current_point,
self.current_color,
self.rng,
@@ -113,3 +111,48 @@ impl<'a, R: Rng> Iterator for ChaosGame<'a, R> {
Some((next_point, next_color))
}
}
/// Shader entry point for running the chaos game to produce new IFS coordinates
pub mod entry {
use crate::chaos_game::ChaosGame;
use crate::rng::xoshiro256starstar_from_seed;
use crate::transform::Transform;
use crate::variation::Variation;
use glam::Vec4;
use spirv_std::spirv;
/// Given a set of fractal flame parameters, generate new IFS coordinates
/// and store them in the output array.
///
/// Arguments:
/// * `iteration_discard` - Choas game steps to discard prior to recording into the output buffer
/// * `output` - Output buffer to record chaos game steps into. Because of alignment issues,
/// the output is recorded as a [`Vec4`]; the IFS (x, y) coordinate is in `x` and `y`,
/// and color is in `w`
#[spirv(compute(entry_point_name = "main_chaos_game", threads(1)))]
pub fn main_chaos_game(
#[spirv(spec_constant(id = 1, default = 20))] iteration_discard: u32,
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] rng_seed: &[u8],
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] transforms: &[Transform],
#[spirv(storage_buffer, descriptor_set = 0, binding = 2)] weights: &[f32],
#[spirv(storage_buffer, descriptor_set = 0, binding = 3)] variations: &[Variation],
#[spirv(storage_buffer, descriptor_set = 1, binding = 0)] output: &mut [Vec4],
) {
let mut rng_seed_actual = [0u8; 32];
(0..32).for_each(|i| rng_seed_actual[i] = rng_seed[i]);
let mut rng = xoshiro256starstar_from_seed(rng_seed_actual);
let mut chaos_game = ChaosGame::new(&mut rng, transforms, weights, variations);
for _ in 0..iteration_discard {
chaos_game.next().unwrap();
}
for i in 0..output.len() {
output[i] = chaos_game
.next()
.map(|output| (output.0, 0.0, output.1).into())
.unwrap();
}
}
}
-84
View File
@@ -1,84 +0,0 @@
//! Image
use glam::Vec4;
use libm::floorf;
/// Blending modes for mapping IFS color values (which are on a scale `[0, 1]`)
/// to RGBA colors.
#[derive(Copy, Clone, Default)]
#[repr(u32)]
pub enum BlendMode {
/// Map IFS color values to a linear blend of the nearest two palette colors
#[default]
Linear = 0,
/// Map IFS color values to the nearest single palette color
Step = 1,
}
impl BlendMode {
/// Map an IFS color value to RGBA color from the provided palette.
pub fn ifs_to_rgb(&self, color: f32, palette: &[Vec4]) -> Vec4 {
let colors_m_one = palette.len() - 1;
let period = 1.0 / colors_m_one as f32;
let index_lower = floorf(color / period) as usize;
let index_upper = (index_lower + 1).clamp(0, colors_m_one);
let rem = color % period / period;
match self {
BlendMode::Linear => palette[index_lower].lerp(palette[index_upper], rem),
BlendMode::Step => palette[index_lower],
}
}
}
// UNSAFE: Sound because enum has guaranteed layout (u32) and defined zero-value
unsafe impl bytemuck::Zeroable for BlendMode {}
// UNSAFE: Sound because enum has guaranteed layout (u32) and defined zero-value
unsafe impl bytemuck::Pod for BlendMode {}
#[cfg(test)]
mod test {
use crate::image::BlendMode;
use glam::Vec4;
#[test]
fn blend_linear() {
let ifs_to_rgb = |color, palette| BlendMode::Linear.ifs_to_rgb(color, palette);
let palette = &[Vec4::splat(0.0), Vec4::splat(1.0)];
assert_eq!(ifs_to_rgb(0.0, palette), Vec4::splat(0.0));
assert_eq!(ifs_to_rgb(0.5, palette), Vec4::splat(0.5));
assert_eq!(ifs_to_rgb(1.0, palette), Vec4::splat(1.0));
let palette = &[Vec4::splat(1.0), Vec4::splat(2.0), Vec4::splat(3.0)];
assert_eq!(ifs_to_rgb(0.0, palette), Vec4::splat(1.0));
assert_eq!(ifs_to_rgb(0.5, palette), Vec4::splat(2.0));
assert_eq!(ifs_to_rgb(1.0, palette), Vec4::splat(3.0));
let palette = &[
Vec4::splat(1.0),
Vec4::splat(2.0),
Vec4::splat(3.0),
Vec4::splat(4.0),
];
assert_eq!(ifs_to_rgb(0.0, palette), Vec4::splat(1.0));
assert_eq!(ifs_to_rgb(0.5, palette), Vec4::splat(2.5));
assert_eq!(ifs_to_rgb(1.0, palette), Vec4::splat(4.0));
}
#[test]
fn blend_step() {
let ifs_to_rgb = |color, palette| BlendMode::Step.ifs_to_rgb(color, palette);
let palette = &[Vec4::splat(0.0), Vec4::splat(1.0)];
assert_eq!(ifs_to_rgb(0.5, palette), Vec4::splat(0.0));
let palette = &[Vec4::splat(1.0), Vec4::splat(2.0), Vec4::splat(3.0)];
assert_eq!(ifs_to_rgb(0.0, palette), palette[0]);
assert_eq!(ifs_to_rgb(0.25, palette), palette[0]);
assert_eq!(ifs_to_rgb(0.4, palette), palette[0]);
assert_eq!(ifs_to_rgb(0.5, palette), palette[1]);
assert_eq!(ifs_to_rgb(0.7, palette), palette[1]);
assert_eq!(ifs_to_rgb(1.0, palette), palette[2]);
}
}
+2 -2
View File
@@ -1,10 +1,10 @@
//! # Enkou
#![no_std]
#![allow(clippy::needless_range_loop)] // SPIR-V backend has issues with iteration over items
#![warn(missing_docs)]
pub mod camera;
pub mod chaos_game;
pub mod image;
mod rng;
pub mod transform;
pub mod variation;
+55
View File
@@ -0,0 +1,55 @@
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256StarStar;
/// Convert an RNG state buffer to an instance of [`Xoshiro256StarStar`].
///
/// While [`SeedableRng::from_seed`] is an infallible function,
/// it relies on some methods that can't be compiled by the SPIR-V
/// backend (specifically, formatting functions in the core crate).
///
/// In practice, the xoshiro RNG state is entirely defined by its seed,
/// so this function does the work of [`SeedableRng::from_seed`] by
/// transmuting the seed value to an RNG instance.
///
/// This function assumes a properly-initialized state array;
/// output may silently degenerate if the initial state is all zeros,
/// so this module is private to the crate.
pub(crate) fn xoshiro256starstar_from_seed(
rng_state: <Xoshiro256StarStar as SeedableRng>::Seed,
) -> Xoshiro256StarStar {
let mut rng_state_actual = [0u64; 4];
// NOTE: Bit shifting is tedious, but we don't have great alternatives:
// - `chunks_exact` has issues with pointer casting
// - `u64::from_le_bytes` has issues with `OpBitcast` in SPIR-V validation
for i in 0..rng_state_actual.len() {
for j in 0..size_of::<u64>() {
rng_state_actual[i] |= (rng_state[i * size_of::<u64>() + j] as u64) << j * 8;
}
}
unsafe { core::mem::transmute(rng_state_actual) }
}
#[cfg(test)]
mod test {
use crate::rng::xoshiro256starstar_from_seed;
use core::iter::zip;
use rand::{RngExt, SeedableRng};
use rand_xoshiro::Xoshiro256StarStar;
#[test]
fn match_seeded() {
let mut seed: <Xoshiro256StarStar as SeedableRng>::Seed = [0u8; 32];
for i in 0..seed.len() {
seed[i] = i as u8;
}
let rng1 = Xoshiro256StarStar::from_seed(seed).random_iter::<u64>();
let rng2 = xoshiro256starstar_from_seed(seed).random_iter::<u64>();
zip(rng1, rng2)
.take(100)
.for_each(|(rng1_value, rng2_value)| assert_eq!(rng1_value, rng2_value));
}
}
+89 -71
View File
@@ -13,27 +13,22 @@ use rand::Rng;
#[repr(C)]
pub struct Transform {
coefficients: Affine2,
coefficients_post: Affine2,
variation_range: UVec2,
color: f32,
color_speed: f32,
color: Vec2,
}
impl Transform {
/// Create a new transform from an affine transformation matrix
pub fn new(
coefficients: Affine2,
coefficients_post: Affine2,
variation_range: UVec2,
color: f32,
color_speed: f32,
) -> Self {
///
/// Arguments:
/// * `coefficients` - Affine transform coefficients for this transformation. Applied prior to variations
/// * `variation_range` - (half-open) range of variations to apply during [`Self::transform_point`]
/// * `color` - Color value and speed to apply during [`Self::transform_color`]
pub fn new(coefficients: Affine2, variation_range: UVec2, color: Vec2) -> Self {
Transform {
coefficients,
coefficients_post,
variation_range,
color,
color_speed,
}
}
@@ -51,88 +46,111 @@ impl Transform {
let variation_start = self.variation_range.x;
let variation_end = self.variation_range.y;
for variation_index in variation_start..variation_end {
let variation = &variations[variation_index as usize];
let ref variation = variations[variation_index as usize];
point_output += variation.transform_point(point, rng, &self.coefficients)
}
self.coefficients_post.transform_point2(point)
point_output
}
/// Apply this transform to a color in IFS coordinates, producing a new color
/// Mix an existing color with this transform's color
pub fn transform_color(&self, color: f32) -> f32 {
self.color.lerp(color, self.color_speed)
color.lerp(self.color.x, self.color.y)
}
}
#[cfg(test)]
mod test {
use crate::Coefficients2;
use crate::transform::Transform;
use crate::variation::{Variation, VariationKind};
use glam::{Affine2, uvec2, vec2};
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256StarStar;
use core::convert::Infallible;
use glam::{Affine2, Vec2, uvec2, vec2};
use rand::TryRng;
struct NullRng;
impl NullRng {
pub fn new() -> Self {
NullRng
}
}
impl TryRng for NullRng {
type Error = Infallible;
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(0)
}
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
Ok(0)
}
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
dst.iter_mut().for_each(|b| *b = 0);
Ok(())
}
}
#[test]
fn transform_scaling() {
let scale_coefficients = vec2(2.0, 0.5);
let transform = Transform::new(
Affine2::from_scale(scale_coefficients),
Affine2::IDENTITY,
uvec2(0, 1),
0.0,
0.0,
);
let mut rng = Xoshiro256StarStar::from_seed([0u8; 32]);
fn test_transform_point_identity() {
let transform = Transform::new(Affine2::IDENTITY, uvec2(0, 1), vec2(0.0, 0.0));
let variations = [Variation::IDENTITY];
let point = vec2(1.0, 1.0);
assert_eq!(
transform.transform_point(&mut rng, &variations, point),
scale_coefficients
);
let transform_point =
|point: Vec2| transform.transform_point(&mut NullRng::new(), &variations, point);
for (input, expected) in [
(vec2(0.0, 1.0), vec2(0.0, 1.0)),
(vec2(1.0, 0.0), vec2(1.0, 0.0)),
(vec2(1.0, 1.0), vec2(1.0, 1.0)),
] {
assert_eq!(transform_point(input), expected);
}
}
#[test]
fn transform_scaling_post() {
let scale_coefficients = vec2(2.0, 0.5);
let transform_pdj =
Transform::new(Affine2::IDENTITY, Affine2::IDENTITY, uvec2(0, 1), 0.0, 0.0);
let transform_pdj_post = Transform::new(
Affine2::IDENTITY,
Affine2::from_scale(scale_coefficients),
uvec2(0, 1),
0.0,
0.0,
);
let mut rng = Xoshiro256StarStar::from_seed([0; 32]);
let variations = [Variation::new(VariationKind::Pdj, 1.0, [0.0f32; 4].into())];
let point = vec2(1.0, 1.0);
let point_pdj = transform_pdj.transform_point(&mut rng, &variations, point);
let point_pdj_post = transform_pdj_post.transform_point(&mut rng, &variations, point);
assert_eq!(point_pdj * scale_coefficients, point_pdj_post);
}
#[test]
fn transform_color() {
let starting_color = 0.0;
let transform_color = 1.0;
let transform_speed = 0.5;
fn test_transform_point_scaling() {
let transform = Transform::new(
Affine2::IDENTITY,
Affine2::IDENTITY,
Affine2::from_coefficients(0.5, 0.0, 0.0, 0.0, 0.5, 0.0),
uvec2(0, 1),
starting_color,
transform_speed,
vec2(0.0, 0.0),
);
let variations = [Variation::IDENTITY];
assert_eq!(
transform.transform_color(starting_color),
starting_color * (1.0 - transform_speed) + transform_color * transform_speed
);
let transform_point =
|point: Vec2| transform.transform_point(&mut NullRng::new(), &variations, point);
for (input, expected) in [
(vec2(0.0, 1.0), vec2(0.0, 0.5)),
(vec2(1.0, 0.0), vec2(0.5, 0.0)),
(vec2(1.0, 1.0), vec2(0.5, 0.5)),
] {
assert_eq!(transform_point(input), expected);
}
}
#[test]
fn test_transform_point_scaling_variation() {
let transform = Transform::new(Affine2::IDENTITY, uvec2(0, 1), vec2(0.0, 0.0));
let variations = [Variation::new(VariationKind::Linear, 2.0, [0.0; 4].into())];
let transform_point =
|point: Vec2| transform.transform_point(&mut NullRng::new(), &variations, point);
for (input, expected) in [
(vec2(0.0, 1.0), vec2(0.0, 2.0)),
(vec2(1.0, 0.0), vec2(2.0, 0.0)),
(vec2(1.0, 1.0), vec2(2.0, 2.0)),
] {
assert_eq!(transform_point(input), expected);
}
}
#[test]
fn test_color_mixing() {
let transform = Transform::new(Affine2::IDENTITY, uvec2(0, 1), vec2(0.0, 0.5));
assert_eq!(transform.transform_color(0.0), 0.0);
assert_eq!(transform.transform_color(1.0), 0.5);
assert_eq!(transform.transform_color(0.5), 0.25);
}
}
+3 -10
View File
@@ -21,8 +21,8 @@ use rand::{Rng, RngExt};
pub struct VariationParams([f32; 4]);
impl From<[f32; 4]> for VariationParams {
fn from(v: [f32; 4]) -> Self {
VariationParams(v)
fn from(value: [f32; 4]) -> Self {
VariationParams(value)
}
}
@@ -35,7 +35,7 @@ impl From<[f32; 4]> for VariationParams {
pub enum VariationKind {
/// Identity variation, returns the point as-is
Linear = 0,
Spherical = 2,
Julia = 13,
Popcorn = 17,
Pdj = 24,
@@ -87,7 +87,6 @@ impl Variation {
) -> Vec2 {
(match self.kind {
VariationKind::Linear => transform_point_linear(point),
VariationKind::Spherical => transform_point_spherical(point),
VariationKind::Julia => transform_point_julia(point, rng),
VariationKind::Popcorn => transform_point_popcorn(point, coefficients),
VariationKind::Pdj => transform_point_pdj(point, &self.params),
@@ -99,12 +98,6 @@ fn transform_point_linear(point: Vec2) -> Vec2 {
point
}
fn transform_point_spherical(point: Vec2) -> Vec2 {
let r2 = (point * point).element_sum();
let r2_inv = 1.0 / r2;
point * Vec2::splat(r2_inv)
}
fn transform_point_julia<R: Rng>(point: Vec2, rng: &mut R) -> Vec2 {
let x2 = powf(point.x, 2.0);
let y2 = powf(point.y, 2.0);