From 3d4b63c19e0dde3e6aab138e056e2445dab0c42a Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Fri, 23 Oct 2026 15:25:10 +0200 Subject: [PATCH 01/96] wasmtime 48 LTS: one wasm-tools version for the runtime, packing and the SDK wasmtime 36 read WebAssembly with wasm-tools 0.236, wit-component (cargo horizon-mod, the sandbox tests) used 0.239 and the SDK's wit-bindgen 0.247: three copies of wasmparser, wasm-encoder, wit-parser and wasm-metadata. wasmtime 48, the next long-term-support line (fixes until August 2028), wit-component 0.254 and wit-bindgen 0.60 all use 0.254. No code change; horizon-mods now needs Rust 1.95. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 541 +++++++++++++----------------- crates/horizon-mod-sdk/Cargo.toml | 2 +- crates/horizon-mods/Cargo.toml | 10 +- docs/mods.md | 8 +- 4 files changed, 241 insertions(+), 320 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f832e248..420f2226c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,9 +20,9 @@ checksum = "366ffbaa4442f4684d91e2cd7c5ea7c4ed8add41959a31447066e279e432b618" [[package]] name = "addr2line" -version = "0.25.1" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b" +checksum = "59317f77929f0e679d39364702289274de2f0f0b22cbf50b2b8cff2169a0b27a" dependencies = [ "gimli", ] @@ -155,7 +155,7 @@ dependencies = [ "objc2-foundation 0.3.2", "parking_lot", "percent-encoding", - "windows-sys 0.59.0", + "windows-sys 0.60.2", "wl-clipboard-rs", "x11rb", ] @@ -196,6 +196,17 @@ dependencies = [ "libloading", ] +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -572,7 +583,7 @@ dependencies = [ "jni 0.21.1", "js-sys", "libc", - "mach2", + "mach2 0.4.3", "ndk", "ndk-context", "num-derive", @@ -586,6 +597,15 @@ dependencies = [ "windows 0.54.0", ] +[[package]] +name = "cpp_demangle" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0667304c32ea56cb4cd6d2d7c0cfe9a2f8041229db8c033af7f8d69492429def" +dependencies = [ + "cfg-if", +] + [[package]] name = "cpufeatures" version = "0.2.17" @@ -597,46 +617,48 @@ dependencies = [ [[package]] name = "cranelift-assembler-x64" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e92993516bcc43fd4e511b31d2e0c0c24f6b7ac18bb39e32cac2bfc05ea378e" +checksum = "e6071a4f1df4837aa03d1e4d53067784acafe270ffc7dad66a7f39832204da3f" dependencies = [ "cranelift-assembler-x64-meta", ] [[package]] name = "cranelift-assembler-x64-meta" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97eeeb61b08ec484f274119faf3a8a1b80dd300b0208c0247d16d05982fc89fe" +checksum = "5e3b92a9cc90388fe70e63640af30b734387449445aef0176ab2028e9892d70e" dependencies = [ "cranelift-srcgen", ] [[package]] name = "cranelift-bforest" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "782eb44158681a105d157cb0ce63e3dc83f78ef5674266c12234f49da3f5c6e8" +checksum = "f45c85646c4425cc85b868effaf20c302b437a3fb79bb27a64513dd522f07411" dependencies = [ "cranelift-entity", + "wasmtime-internal-core", ] [[package]] name = "cranelift-bitset" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edd59af193204c4f025fb2c2d9ee0aeb1f60929edf000b0dfa3d45543a62f264" +checksum = "94f56b5c7d564cfa03aa269a6b98c916cab4d545196a391d31403a23ef39333a" dependencies = [ "serde", "serde_derive", + "wasmtime-internal-core", ] [[package]] name = "cranelift-codegen" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1c29bd985057d790db25f6bc60b369b311aa2d75c2f496a7e9993585bfc7d60" +checksum = "2c073641ce6e382814a8220331cc1ea2c782a9ea5d5da6d0f7f7c0541192693e" dependencies = [ "bumpalo", "cranelift-assembler-x64", @@ -648,22 +670,26 @@ dependencies = [ "cranelift-entity", "cranelift-isle", "gimli", - "hashbrown 0.15.5", + "hashbrown 0.17.1", + "libm", "log", + "postcard", "pulley-interpreter", "regalloc2", "rustc-hash 2.1.3", "serde", + "serde_derive", + "sha2", "smallvec", "target-lexicon", - "wasmtime-internal-math", + "wasmtime-internal-core", ] [[package]] name = "cranelift-codegen-meta" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9ef8a8495f19212f92db8425f768ffc67000c5cb417a0664475a8d09596700f" +checksum = "db226fafea5194921de10dccf35ac2863500313990f770cbe8b26c51eebec4d4" dependencies = [ "cranelift-assembler-x64-meta", "cranelift-codegen-shared", @@ -674,37 +700,39 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3aa5099097666baf45bd68ef036327c8f29d8785fffaaa7db3b3b6b7e94bcc6" +checksum = "cf8ac46b2c8e328ca979810426ea635ef90ecc8c54fd3296bde31ab470a48a1d" [[package]] name = "cranelift-control" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26f67f4f4e92c2934f40c2ff4574400bd27326932aad937a438b7fb459f60a63" +checksum = "6754e0ab97915209ea6ce6549f6443b018e1338142ada4c856cec213ee82e4b8" dependencies = [ "arbitrary", ] [[package]] name = "cranelift-entity" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8393d09f0abaa504896069e79f4eb6f15eb1853e5645732b082dac1ed292aaa8" +checksum = "774d25e36df18696f63a284e9c4ea43455e2ebcca6308e0e02301f18c8e777e2" dependencies = [ "cranelift-bitset", "serde", "serde_derive", + "wasmtime-internal-core", ] [[package]] name = "cranelift-frontend" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "580cfaecbd11476dd82f6a393123dcbd9dfe583467df8d0c8441c641444d4985" +checksum = "69cfcb31405e2de2a48e5a6de8e07a3d8b77f35caa3e944d627780734b36c138" dependencies = [ "cranelift-codegen", + "hashbrown 0.17.1", "log", "smallvec", "target-lexicon", @@ -712,15 +740,15 @@ dependencies = [ [[package]] name = "cranelift-isle" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c044843adb7d94b1facebaad846c0a762f79a77953417e3f0182093bcbbdc38" +checksum = "2f5a26df22db546ab92be6feaf21835ae91082af0a32bbb398b277df3765412f" [[package]] name = "cranelift-native" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d4c1707214e53f2040e4d13430069ea22623287b2750ed6d62e40eba88d91a" +checksum = "19f258c08e590f90dcaf9904d25ba142ee2980c3aaeac4bb45f1452b1a407c2a" dependencies = [ "cranelift-codegen", "libc", @@ -729,9 +757,9 @@ dependencies = [ [[package]] name = "cranelift-srcgen" -version = "0.123.18" +version = "0.135.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d355a35edc49f628db7b793f3527eed31b560e77c3e8ed0e5258aad8df8a5249" +checksum = "67055b4f315e8f6af3e8eccec74ad38f7407f0fd854adbd49b4b3b27bdb2e3be" [[package]] name = "crc32fast" @@ -1003,12 +1031,6 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b5343afd4a8365a643ac588dab4cf234a190c7f6c88c9f6dd6ffe00837661b7" -[[package]] -name = "fallible-iterator" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" - [[package]] name = "fdeflate" version = "0.3.7" @@ -1101,12 +1123,48 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "futures" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a31d2a3fbaaeb2af2368bbdd904aa8e812d3c04a1ee10d3171f52d556e5d0a3" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", + "futures-sink", +] + [[package]] name = "futures-core" version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" +[[package]] +name = "futures-io" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53c0fa8157de1303bfffdaa1cc2a673bfffb60102f76b0ef4441659124373fed" + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + [[package]] name = "futures-task" version = "0.3.34" @@ -1119,8 +1177,12 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ + "futures-channel", "futures-core", + "futures-io", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "slab", ] @@ -1215,11 +1277,12 @@ dependencies = [ [[package]] name = "gimli" -version = "0.32.3" +version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7" +checksum = "0bf7f043f89559805f8c7cacc432749b2fa0d0a0a9ee46ce47164ed5ba7f126c" dependencies = [ - "fallible-iterator", + "fnv", + "hashbrown 0.16.1", "indexmap", "stable_deref_trait", ] @@ -1315,7 +1378,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" dependencies = [ "foldhash 0.1.5", - "serde", ] [[package]] @@ -1336,6 +1398,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" dependencies = [ "foldhash 0.2.0", + "serde", + "serde_core", ] [[package]] @@ -1486,7 +1550,7 @@ dependencies = [ name = "horizon-mod-sdk" version = "0.1.0" dependencies = [ - "wit-bindgen", + "wit-bindgen 0.60.0", ] [[package]] @@ -1499,8 +1563,8 @@ dependencies = [ "serde_json", "toml", "wasmtime", - "wit-component 0.239.0", - "wit-parser 0.239.0", + "wit-component", + "wit-parser", "zip", ] @@ -2127,6 +2191,12 @@ dependencies = [ "libc", ] +[[package]] +name = "mach2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b" + [[package]] name = "memchr" version = "2.8.3" @@ -2662,12 +2732,12 @@ dependencies = [ [[package]] name = "object" -version = "0.37.3" +version = "0.39.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe" +checksum = "2e5a6c098c7a3b6547378093f5cc30bc54fd361ce711e05293a5cc589562739b" dependencies = [ "crc32fast", - "hashbrown 0.15.5", + "hashbrown 0.17.1", "indexmap", "memchr", ] @@ -2972,21 +3042,21 @@ checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" [[package]] name = "pulley-interpreter" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "334ae8982dcf354e5f28e79fc6f935601e028b729cc174d3937eeeaa859057b8" +checksum = "2b8187c8e7db87d085865fc106502e7b34d5c25f8ce4c272945b21503371f6a0" dependencies = [ "cranelift-bitset", "log", "pulley-macros", - "wasmtime-internal-math", + "wasmtime-internal-core", ] [[package]] name = "pulley-macros" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cd207bca4bc5e46cb4fcea87196dcbd78befc628c64c67d11dc99fce5cd5369" +checksum = "7f98ce0e381d9e45e63ea1d20bba3cf131797086077f43af9203f1fb7de0ed6b" dependencies = [ "proc-macro2", "quote", @@ -3094,15 +3164,16 @@ dependencies = [ [[package]] name = "regalloc2" -version = "0.12.2" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5216b1837de2149f8bc8e6d5f88a9326b63b8c836ed58ce4a0a29ec736a59734" +checksum = "757712e8e61590d6d4f5d563483755538b5aa13467837a3b41cd9832509a7f85" dependencies = [ "allocator-api2", "bumpalo", - "hashbrown 0.15.5", + "hashbrown 0.17.1", "log", "rustc-hash 2.1.3", + "serde", "smallvec", ] @@ -3155,6 +3226,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustc-demangle" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb" + [[package]] name = "rustc-hash" version = "1.1.0" @@ -3995,7 +4072,7 @@ version = "1.0.4+wasi-0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" dependencies = [ - "wit-bindgen", + "wit-bindgen 0.57.1", ] [[package]] @@ -4056,124 +4133,66 @@ dependencies = [ [[package]] name = "wasm-encoder" -version = "0.236.1" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "724fccfd4f3c24b7e589d333fc0429c68042897a7e8a5f8694f31792471841e7" +checksum = "ebaca0b540bdb15d195ff34fb23074255948708b8916a11a339ba4513dc61906" dependencies = [ "leb128fmt", - "wasmparser 0.236.1", -] - -[[package]] -name = "wasm-encoder" -version = "0.239.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5be00faa2b4950c76fe618c409d2c3ea5a3c9422013e079482d78544bb2d184c" -dependencies = [ - "leb128fmt", - "wasmparser 0.239.0", -] - -[[package]] -name = "wasm-encoder" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30b6733b8b91d010a6ac5b0fb237dc46a19650bc4c67db66857e2e787d437204" -dependencies = [ - "leb128fmt", - "wasmparser 0.247.0", + "wasmparser", ] [[package]] name = "wasm-metadata" -version = "0.239.0" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20b3ec880a9ac69ccd92fbdbcf46ee833071cf09f82bb005b2327c7ae6025ae2" +checksum = "5d1880dd9a848aa68f387966256c34ad49abb4607e3cfa35720d2723d0591cd3" dependencies = [ "anyhow", "indexmap", - "wasm-encoder 0.239.0", - "wasmparser 0.239.0", -] - -[[package]] -name = "wasm-metadata" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "665fe59e56cc9b419ca6fcca56673e3421d1a5011e3b65caf6b726fd9e041d10" -dependencies = [ - "anyhow", - "indexmap", - "wasm-encoder 0.247.0", - "wasmparser 0.247.0", + "wasm-encoder", + "wasmparser", ] [[package]] name = "wasmparser" -version = "0.236.1" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9b1e81f3eb254cf7404a82cee6926a4a3ccc5aad80cc3d43608a070c67aa1d7" +checksum = "c5ff8685b587722894b6f25958fc608d315b93b8d23647bfb567664e7006d757" dependencies = [ "bitflags 2.13.2", - "hashbrown 0.15.5", + "hashbrown 0.17.1", "indexmap", "semver", "serde", ] -[[package]] -name = "wasmparser" -version = "0.239.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9d90bb93e764f6beabf1d02028c70a2156a6583e63ac4218dd07ef733368b0" -dependencies = [ - "bitflags 2.13.2", - "hashbrown 0.15.5", - "indexmap", - "semver", -] - -[[package]] -name = "wasmparser" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e6fb4c2bee46c5ea4d40f8cdb5c131725cd976718ec56f1c8e82fbde5fa2a80" -dependencies = [ - "bitflags 2.13.2", - "hashbrown 0.17.1", - "indexmap", - "semver", -] - [[package]] name = "wasmprinter" -version = "0.236.1" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2df225df06a6df15b46e3f73ca066ff92c2e023670969f7d50ce7d5e695abbb1" +checksum = "8b03c8f270f3f384f70f42869b5ff7f6253fe4f0aa8590f7edd332c1943e5f51" dependencies = [ "anyhow", "termcolor", - "wasmparser 0.236.1", + "wasmparser", ] [[package]] name = "wasmtime" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70095b9fe63cc2a87ed206729e11544610decc184c62898c5d201ff1d666d82e" +checksum = "12300d040e8bea7e7ccde75b9919d27ebba47a217a64b09ba058ae8d3b4290b6" dependencies = [ "addr2line", - "anyhow", + "async-trait", "bitflags 2.13.2", "bumpalo", "cc", - "cfg-if", "encoding_rs", - "hashbrown 0.15.5", - "indexmap", + "futures", "libc", "log", - "mach2", + "mach2 0.6.0", "memfd", "object", "once_cell", @@ -4185,62 +4204,56 @@ dependencies = [ "serde_derive", "smallvec", "target-lexicon", - "wasmparser 0.236.1", + "wasmparser", "wasmtime-environ", - "wasmtime-internal-asm-macros", "wasmtime-internal-component-macro", "wasmtime-internal-component-util", + "wasmtime-internal-core", "wasmtime-internal-cranelift", "wasmtime-internal-fiber", "wasmtime-internal-jit-debug", "wasmtime-internal-jit-icache-coherence", - "wasmtime-internal-math", - "wasmtime-internal-slab", "wasmtime-internal-unwinder", "wasmtime-internal-versioned-export-macros", - "wasmtime-internal-winch", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] name = "wasmtime-environ" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a67ea8904b6e369e2218780bd1f9186f1980cb616445eb2c1f3c4e45c8397865" +checksum = "805d6554eb6dd6e62891061a31383a89d69fd891f785e5ae68f0835a554b7a2a" dependencies = [ "anyhow", + "cpp_demangle", + "cranelift-bforest", "cranelift-bitset", "cranelift-entity", "gimli", + "hashbrown 0.17.1", "indexmap", "log", "object", "postcard", + "rustc-demangle", "semver", "serde", "serde_derive", + "sha2", "smallvec", "target-lexicon", - "wasm-encoder 0.236.1", - "wasmparser 0.236.1", + "wasm-encoder", + "wasmparser", "wasmprinter", "wasmtime-internal-component-util", -] - -[[package]] -name = "wasmtime-internal-asm-macros" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0488a52ae93e654baecb08ce04d15d478756eb2168c2bdab7915710109161ce" -dependencies = [ - "cfg-if", + "wasmtime-internal-core", ] [[package]] name = "wasmtime-internal-component-macro" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "377e406c3f8e7bea64db3cb3ee0d406c331e8fc09d8cb2e7bf9ad74cb5d3b31a" +checksum = "b832b80853365d9d3a741681044554be291a4f977aae074b318b55c7a4835f83" dependencies = [ "anyhow", "proc-macro2", @@ -4248,23 +4261,32 @@ dependencies = [ "syn 2.0.119", "wasmtime-internal-component-util", "wasmtime-internal-wit-bindgen", - "wit-parser 0.236.1", + "wit-parser", ] [[package]] name = "wasmtime-internal-component-util" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97c24503bb712afe2c797dc291b96e57556ee62c35fd951f87e96088324de37c" +checksum = "4c8a11aaf68d8945991429d45950f32490a30ddcdcdb56629816cb41a255d8fe" + +[[package]] +name = "wasmtime-internal-core" +version = "48.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea266d6cfb31c324c075c8432386654bcbb1559aa8d29be9d47eacf7dcc41012" +dependencies = [ + "hashbrown 0.17.1", + "libm", + "serde", +] [[package]] name = "wasmtime-internal-cranelift" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f63ea7ee133de9109a4a1fc63dae3afc9ea32e92b8dada824fa4595dbf175f80" +checksum = "d427759553b07c296c8ed7bff8ac930a94b9ba99e6a79243ea79bcc51aa58d96" dependencies = [ - "anyhow", - "cfg-if", "cranelift-codegen", "cranelift-control", "cranelift-entity", @@ -4278,33 +4300,32 @@ dependencies = [ "smallvec", "target-lexicon", "thiserror 2.0.21", - "wasmparser 0.236.1", + "wasmparser", "wasmtime-environ", - "wasmtime-internal-math", + "wasmtime-internal-core", + "wasmtime-internal-unwinder", "wasmtime-internal-versioned-export-macros", ] [[package]] name = "wasmtime-internal-fiber" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69662a0a4dbe9784d1e8add9e3c5d2db0ca348085c624c27a70992dbe2eff4a3" +checksum = "b54c8c5b02ea985149a90f917e1761af99734c6c27e6d7fdd5b6f69fc1db3950" dependencies = [ - "anyhow", "cc", - "cfg-if", "libc", "rustix 1.1.5", - "wasmtime-internal-asm-macros", + "wasmtime-environ", "wasmtime-internal-versioned-export-macros", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] name = "wasmtime-internal-jit-debug" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00cb07d23903c0be359d6b920d94774ea322759c3f4d6721b61afa03602e4804" +checksum = "72bb10c94724f072b6eca515085033e61da9efae65d054545126df37a15530c3" dependencies = [ "cc", "wasmtime-internal-versioned-export-macros", @@ -4312,83 +4333,50 @@ dependencies = [ [[package]] name = "wasmtime-internal-jit-icache-coherence" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa2b47c37a6d026e2755fe2cf7ec5c1f4b0fba62e448ea910cdf6a07a4982c6a" +checksum = "1b781a27e8958fd07fd3135c0be216f77301efecd15cbf4f14ff210cdad5f169" dependencies = [ - "anyhow", - "cfg-if", "libc", - "windows-sys 0.60.2", + "wasmtime-internal-core", + "windows-sys 0.61.2", ] -[[package]] -name = "wasmtime-internal-math" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b269f0ba44102c57fd5f94f1bf4395ccd4fc2b84698fc78f627a6f2a47131a62" -dependencies = [ - "libm", -] - -[[package]] -name = "wasmtime-internal-slab" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e35ac9d503d503703cd24d532e53e2e93c86109b451df40a9801500045aee25" - [[package]] name = "wasmtime-internal-unwinder" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83ce1d0930da44ed375701e6b4f187b46249f7efe2b1d8c1c77229a4adc82de9" +checksum = "121188de1b2741b8e35530a9b0c8adf525b3aa5913a2ca3d4522509cfbc0494a" dependencies = [ - "anyhow", - "cfg-if", "cranelift-codegen", "log", "object", + "wasmtime-environ", ] [[package]] name = "wasmtime-internal-versioned-export-macros" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b6633be262400bd7abab4ff767a16e8b6a41ff117be54bf76dabecea9f96225" +checksum = "933f1990e4e5177547a4abd51dcfa5cb7616fe42b9fb6f0f2b7423627758223c" dependencies = [ "proc-macro2", "quote", "syn 2.0.119", ] -[[package]] -name = "wasmtime-internal-winch" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "509d1e1cf8e3b3306f291407c92bca8381d9294f8c54ad74b692538f1f5b23c7" -dependencies = [ - "anyhow", - "cranelift-codegen", - "gimli", - "object", - "target-lexicon", - "wasmparser 0.236.1", - "wasmtime-environ", - "wasmtime-internal-cranelift", - "winch-codegen", -] - [[package]] name = "wasmtime-internal-wit-bindgen" -version = "36.0.18" +version = "48.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6d9a5b51b3400b4df1055724c4536ad5aa58fafe83cea5ca94f88cc9eb97563" +checksum = "417ebd61d2581d86d65413517f3a41294a9ad105da26f0dc7039912a0d97e1ed" dependencies = [ "anyhow", "bitflags 2.13.2", "heck", "indexmap", - "wit-parser 0.236.1", + "wit-component", + "wit-parser", ] [[package]] @@ -4698,26 +4686,6 @@ dependencies = [ "windows-sys 0.61.2", ] -[[package]] -name = "winch-codegen" -version = "36.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e470d7e674c41ad32dddc02d8d8a94983e0a4c516028bda60747e6dd862a567" -dependencies = [ - "anyhow", - "cranelift-assembler-x64", - "cranelift-codegen", - "gimli", - "regalloc2", - "smallvec", - "target-lexicon", - "thiserror 2.0.21", - "wasmparser 0.236.1", - "wasmtime-environ", - "wasmtime-internal-cranelift", - "wasmtime-internal-math", -] - [[package]] name = "windows" version = "0.54.0" @@ -5164,42 +5132,48 @@ name = "wit-bindgen" version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "wit-bindgen" +version = "0.60.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a301904d6657d6364c758d869e5389d05d393b16d5b65db60b4f03cbe71bb80d" dependencies = [ "wit-bindgen-rust-macro", ] [[package]] name = "wit-bindgen-core" -version = "0.57.1" +version = "0.60.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02dee27a2dc20d1008016c742ec9fc6ea498492994ba3750be7454cbc97ff04c" +checksum = "48521bb96e56cbb9e031ad306c80dc20e89e69e49ada02781ee06f60fbcb545f" dependencies = [ "anyhow", "heck", - "wit-parser 0.247.0", + "wit-parser", ] [[package]] name = "wit-bindgen-rust" -version = "0.57.1" +version = "0.60.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5007dae772945b7a5003d69d90a3a4a78929d41f19d004e980c4259a6af4484" +checksum = "3df3fe9b9a0066f82fd0c2f07f79d0ffc0f85c53fa5f998516d8722712479042" dependencies = [ "anyhow", "heck", "indexmap", "prettyplease", "syn 2.0.119", - "wasm-metadata 0.247.0", + "wasm-metadata", "wit-bindgen-core", - "wit-component 0.247.0", + "wit-component", ] [[package]] name = "wit-bindgen-rust-macro" -version = "0.57.1" +version = "0.60.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af9237d678e3513ad24e96fe98beacdc0db6405284ba2a2400418cf0d42caa89" +checksum = "d689c4bc9d6af067c651cfba444766343c9a4bdef15fad1e2efab95c0c277af0" dependencies = [ "anyhow", "prettyplease", @@ -5212,9 +5186,9 @@ dependencies = [ [[package]] name = "wit-component" -version = "0.239.0" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88a866b19dba2c94d706ec58c92a4c62ab63e482b4c935d2a085ac94caecb136" +checksum = "1c96b3cea6838dbea55173baa56991fcb087896c321b0a770db201e5a0bd3506" dependencies = [ "anyhow", "bitflags 2.13.2", @@ -5223,72 +5197,17 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "wasm-encoder 0.239.0", - "wasm-metadata 0.239.0", - "wasmparser 0.239.0", - "wit-parser 0.239.0", -] - -[[package]] -name = "wit-component" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d567162a6b9843080e5e0053f696623ff694bae8ae017c9ec536d1873bbe3d8" -dependencies = [ - "anyhow", - "bitflags 2.13.2", - "indexmap", - "log", - "serde", - "serde_derive", - "serde_json", - "wasm-encoder 0.247.0", - "wasm-metadata 0.247.0", - "wasmparser 0.247.0", - "wit-parser 0.247.0", + "wasm-encoder", + "wasm-metadata", + "wasmparser", + "wit-parser", ] [[package]] name = "wit-parser" -version = "0.236.1" +version = "0.254.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16e4833a20cd6e85d6abfea0e63a399472d6f88c6262957c17f546879a80ba15" -dependencies = [ - "anyhow", - "id-arena", - "indexmap", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", - "wasmparser 0.236.1", -] - -[[package]] -name = "wit-parser" -version = "0.239.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55c92c939d667b7bf0c6bf2d1f67196529758f99a2a45a3355cc56964fd5315d" -dependencies = [ - "anyhow", - "id-arena", - "indexmap", - "log", - "semver", - "serde", - "serde_derive", - "serde_json", - "unicode-xid", - "wasmparser 0.239.0", -] - -[[package]] -name = "wit-parser" -version = "0.247.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ffe4064318cdf3c08cb99343b44c039fcefe61ccdf58aa9975285f13d74d1fc" +checksum = "22672d61bfb9df46a16b46ce38522293aee1cc4749fc84a3464ce4083b3d5fd6" dependencies = [ "anyhow", "hashbrown 0.17.1", @@ -5299,8 +5218,8 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "unicode-xid", - "wasmparser 0.247.0", + "unicode-ident", + "wasmparser", ] [[package]] diff --git a/crates/horizon-mod-sdk/Cargo.toml b/crates/horizon-mod-sdk/Cargo.toml index df223b28c..899fb555a 100644 --- a/crates/horizon-mod-sdk/Cargo.toml +++ b/crates/horizon-mod-sdk/Cargo.toml @@ -7,7 +7,7 @@ rust-version = "1.85" description = "Write Horizon mods in Rust: WebAssembly components for the horizon:mod world (docs/mods.md)" [dependencies] -wit-bindgen = { version = "0.57.1", default-features = false, features = ["macros", "realloc"] } +wit-bindgen = { version = "0.60.0", default-features = false, features = ["macros", "realloc"] } [lints] workspace = true diff --git a/crates/horizon-mods/Cargo.toml b/crates/horizon-mods/Cargo.toml index 8db24ac35..d44648691 100644 --- a/crates/horizon-mods/Cargo.toml +++ b/crates/horizon-mods/Cargo.toml @@ -3,7 +3,7 @@ name = "horizon-mods" version = "0.1.0" edition.workspace = true license.workspace = true -rust-version = "1.86" +rust-version = "1.95" description = "Horizon's mod runtime: sandboxed WebAssembly components (docs/mods.md)" [dependencies] @@ -12,11 +12,11 @@ serde_json = "1" toml = "1.1.6" zip = { version = "2", default-features = false, features = ["deflate"] } log = "0.4" -# The 36 LTS line: security fixes until August 2027, and it builds on the client's Rust. -wasmtime = { version = "36.0.18", default-features = false, features = ["runtime", "cranelift", "component-model", "std"], optional = true } +# The 48 LTS line: security fixes until August 2028, on the same wasm-tools (0.254) as wit-component and the SDK's wit-bindgen. +wasmtime = { version = "48.0.7", default-features = false, features = ["runtime", "cranelift", "component-model", "std"], optional = true } # Turns a mod's core module into a component (cargo horizon-mod build, the tests). -wit-component = { version = "0.239", optional = true } -wit-parser = { version = "0.239", optional = true } +wit-component = { version = "0.254", optional = true } +wit-parser = { version = "0.254", optional = true } [features] default = ["runtime"] diff --git a/docs/mods.md b/docs/mods.md index 4b356d336..6aef6295a 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -14,7 +14,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage; tick, frame, chat and command hooks | | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | | Example mod | `crates/horizon-mod-sdk/examples/coords-hud` | coordinates, facing and time HUD, `/where`, `/coords` | -| Runtime | `crates/horizon-mods` | wasmtime 36 LTS, component model | +| Runtime | `crates/horizon-mods` | wasmtime 48 LTS, component model | | Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run` | | In the game | `crates/horizon-client/src/game/mods.rs`, Options > Mods | client mods | | Launcher mod browser (Modrinth, CurseForge with a key) | [mod-browser.md](mod-browser.md) | resource packs, shader packs, modpacks | @@ -130,8 +130,10 @@ There is no file, socket, process or raw-input interface, on purpose. | native `cdylib` plugins | none | no: per OS and arch, unstable Rust ABI | yes | none | unsafe for players | | an embedded JVM | none per mod | yes | Java | 100+ MB, seconds of startup | see the research | -wasmtime 36 is a long-term-support line (security fixes until August 2027) -that builds on the client's Rust 1.90. Only its `runtime`, `cranelift`, +wasmtime 48 is a long-term-support line (security fixes until August 2028) +and needs Rust 1.95. It reads WebAssembly with the same wasm-tools release +(0.254) as the `wit-component` that packs mods and the SDK's `wit-bindgen`, so +the build carries one copy of them. Only its `runtime`, `cranelift`, `component-model` and `std` features are on: no WASI, no async, no GC proposal, no profiling. Its cranelift backend generates native code for x86-64 and ARM64 on Windows, macOS and Linux, which is why one `.hmod` serves them From 0f9de6dbb0ce0e1aeef50e5b960a3040357a6132 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 01:39:28 +0200 Subject: [PATCH 02/96] Shader packs, clean room 14: terrain drawn by the pack's program With a pack whose gbuffers_terrain (or a fallback) loads, solid and cutout terrain is drawn by it into the pack's buffers, in place, between prepare and deferred, and Horizon's own solid terrain draw is skipped. - depthtex0 is now a Depth32Float attachment: the blit writes the world's depth there (GL window depth, GL rows), terrain tests and writes against it, so entities and terrain occlude each other; later programs sample it as before. - The vertex glue reads Horizon's TerrainVertex on both draw paths (instanced, and flat with the page tables in bind group 1) and gives atlas coordinates, lightmap coordinates in 0-240, the vertex colour, mc_midTexCoord at the sprite's centre and mc_Entity = -1. Normals, tangents and block ids come later. - texture/gtexture/tex sample the block atlas, lightmap the lightmap; atlasSize is set. - Occlusion culling is off while a pack draws terrain (it reads the depth of Horizon's terrain draw), and flat draws stay on. - Uniforms a program reads that are not provided yet are reported. - Program building is shared by quads and terrain (build_pass, bind). - terrain-test: Horizon's own pack for this slice. Co-Authored-By: Claude Opus 5.5 --- .../horizon-client/src/game/shader_packs.rs | 3 +- crates/horizon-client/src/gfx.rs | 106 ++- crates/horizon-client/src/gfx/flat.rs | 3 +- crates/horizon-client/src/gfx/pack_atlas.rs | 69 +- crates/horizon-client/src/gfx/pack_post.rs | 716 +++++++++++------- crates/horizon-client/src/gfx/pack_post.wgsl | 7 +- .../horizon-client/src/gfx/pack_uniforms.rs | 5 + .../tests/packs/terrain-test/README.md | 8 + .../packs/terrain-test/shaders/composite.fsh | 19 + .../packs/terrain-test/shaders/composite.vsh | 8 + .../terrain-test/shaders/gbuffers_terrain.fsh | 22 + .../terrain-test/shaders/gbuffers_terrain.vsh | 16 + 12 files changed, 702 insertions(+), 280 deletions(-) create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/README.md create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.fsh create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.vsh create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh create mode 100644 crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh diff --git a/crates/horizon-client/src/game/shader_packs.rs b/crates/horizon-client/src/game/shader_packs.rs index 04af297b6..d819eddef 100644 --- a/crates/horizon-client/src/game/shader_packs.rs +++ b/crates/horizon-client/src/game/shader_packs.rs @@ -42,7 +42,8 @@ impl Game<'_> { ); } let folder = dimension_folder(&pack, dimension); - let post = load_post(&pack, &folder, &properties, ¯os, None); + let terrain = renderer.pack_terrain_inputs(); + let post = load_post(&pack, &folder, &properties, ¯os, terrain.as_ref()); if let Some((programs, notes)) = renderer.set_shader_pack(dimension, Some(post)) { for note in notes { diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 9f05adb65..c117b190f 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -471,6 +471,8 @@ pub struct Renderer { pack_post: Option<(i32, Option)>, /// The block sprites as one atlas, while a shader pack is loaded. pack_atlas: Option, + /// The pack's terrain program reads flat draws' vertices (`pack_terrain_inputs`). + pack_terrain_flat: bool, pub hud: HudRenderer, /// Vanilla's menu background blur, made on first use. menu_blur: Option, @@ -2319,6 +2321,7 @@ impl Renderer { world_post, pack_post: None, pack_atlas: None, + pack_terrain_flat: false, hud, menu_blur: None, info, @@ -2744,17 +2747,50 @@ impl Renderer { dimension: i32, pack: Option, ) -> Option<(Vec, Vec)> { + if pack.is_none() { + self.pack_atlas = None; + } + let instance = wgpu::vertex_attr_array![5=>Float32x4]; + let vertex = wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &TERRAIN_ATTRIBUTES, + }; + let instances = wgpu::VertexBufferLayout { + array_stride: 16, + step_mode: wgpu::VertexStepMode::Instance, + attributes: &instance, + }; + let buffers = [Some(vertex), Some(instances)]; + let flat = self.pack_terrain_flat; let post = pack.map(|post| { + let world = pack_post::World { + atlas: self.pack_atlas.as_ref().map(|a| (a.view.clone(), a.size())), + lightmap: self.lightmap_view.clone(), + terrain: self.pack_atlas.as_ref().map(|_| pack_post::Geometry { + buffers: if flat { &buffers[..1] } else { &buffers }, + table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout), + }), + }; pack_post::PackPost::new( &self.device, &self.queue, post, self.config.format.remove_srgb_suffix(), + world, ) }); - if post.is_none() { - self.pack_atlas = None; - } else if self.pack_atlas.is_none() { + let report = post.as_ref().map(|p| { + let programs = p.programs().into_iter().map(str::to_owned).collect(); + (programs, p.notes.clone()) + }); + self.pack_post = Some((dimension, post)); + report + } + /// What a shader pack's terrain program reads from Horizon's terrain vertices, in the + /// pack's block atlas (made here when missing); `None` when the atlas does not fit. + pub fn pack_terrain_inputs(&mut self) -> Option { + if self.pack_atlas.is_none() { let mut encoder = self.device.create_command_encoder(&Default::default()); let layers = self .sprite_textures @@ -2771,12 +2807,13 @@ impl Renderer { ); self.queue.submit([encoder.finish()]); } - let report = post.as_ref().map(|p| { - let programs = p.programs().into_iter().map(str::to_owned).collect(); - (programs, p.notes.clone()) - }); - self.pack_post = Some((dimension, post)); - report + // Flat draws whenever the device has them (`render` keeps them on with a pack). + self.pack_terrain_flat = self.flat.is_some() && self.flat_on; + Some( + self.pack_atlas + .as_ref()? + .terrain_inputs(self.pack_terrain_flat), + ) } /// The dimension the shader pack was last set for. pub fn shader_pack_dimension(&self) -> Option { @@ -3184,11 +3221,23 @@ impl Renderer { // Occlusion culling leaves the sections its last result did not see // to phase 2, after the others, with instance counts of 0. Whether // the frame has draws enough goes by the last one's. + // A shader pack drawing terrain draws all of it: occlusion culling reads the depth of + // Horizon's own terrain draw. + let pack_terrain = !frame.title + && self + .pack_post + .as_ref() + .and_then(|p| p.1.as_ref()) + .is_some_and(|p| p.has_terrain()); let mut cull = self .cull .as_mut() .filter(|c| { - self.cull_on && !frame.title && self.multi_draw && self.section_draws >= c.min_draws + self.cull_on + && !frame.title + && !pack_terrain + && self.multi_draw + && self.section_draws >= c.min_draws }) .and_then(|cull| cull.begin(&self.device).then_some(cull)); self.phase2_draws.clear(); @@ -3591,7 +3640,9 @@ impl Renderer { } let flat_frame = match &mut self.flat { Some(flat) - if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws => + if self.flat_on + && !self.draws.is_empty() + && (self.draws.len() >= flat.min_draws || pack_terrain) => { let sections = &self.sections; let lods = &self.lods; @@ -3651,6 +3702,8 @@ impl Renderer { } // The world's targets: the screen, or the scaled target of `post`. let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some()); + // The pack's program reads the vertices this frame's draws bind. + let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat; let scaled = self.world_post.prepare( &self.device, [self.config.width, self.config.height], @@ -3739,7 +3792,14 @@ impl Renderer { pass.draw(0..3, 0..1); } } - match self.flat.as_ref().filter(|_| flat_frame) { + match self + .flat + .as_ref() + .filter(|_| flat_frame && !pack_draws_terrain) + { + _ if pack_draws_terrain => { + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + } Some(flat) => { pass.set_pipeline(&flat.solid); let vertices = |p| self.arena.buffer(p); @@ -3947,9 +4007,27 @@ impl Renderer { } if let Some((color, depth)) = &scaled && pack_post - && let Some((_, Some(post))) = &mut self.pack_post + && let Some((dimension, Some(mut post))) = self.pack_post.take() { let size = self.world_post.size().unwrap_or([1, 1]); + let flat = self.flat.as_ref().filter(|_| flat_frame); + let mut terrain = |rp: &mut wgpu::RenderPass<'_>| { + if !pack_draws_terrain { + return; + } + match flat { + Some(flat) => { + flat.draw(rp, &flat.calls[..flat.solid_calls], |p| { + self.arena.buffer(p) + }); + } + None => { + rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); + rp.set_vertex_buffer(1, self.instances.slice(..)); + self.draw_sections(rp, &self.indirect, 0, 0, solid_draws); + } + } + }; post.run( &self.device, &self.queue, @@ -3961,7 +4039,9 @@ impl Renderer { &frame.pack, size, [self.config.width, self.config.height], + &mut terrain, ); + self.pack_post = Some((dimension, Some(post))); } else if scaled.is_some() { self.world_post.finish(&mut encoder, &view_target); } diff --git a/crates/horizon-client/src/gfx/flat.rs b/crates/horizon-client/src/gfx/flat.rs index f6823c5d8..4c337025a 100644 --- a/crates/horizon-client/src/gfx/flat.rs +++ b/crates/horizon-client/src/gfx/flat.rs @@ -73,7 +73,8 @@ pub struct Flat { phase2_call_data: Vec<[u32; 5]>, /// The copy of phase 2's draws, and its ranges. phase2_group: Option<(wgpu::BindGroup, u32)>, - draw_layout: wgpu::BindGroupLayout, + /// The layout of a page's bind group 1 (shader packs' terrain pipelines share it). + pub(super) draw_layout: wgpu::BindGroupLayout, blocks_per_page: u64, pages: Vec>, free_ids: Vec, diff --git a/crates/horizon-client/src/gfx/pack_atlas.rs b/crates/horizon-client/src/gfx/pack_atlas.rs index c90fa6ac1..011dff379 100644 --- a/crates/horizon-client/src/gfx/pack_atlas.rs +++ b/crates/horizon-client/src/gfx/pack_atlas.rs @@ -3,7 +3,6 @@ //! layer is copied into a grid, row by row from the top left, and an animated layer again //! whenever its frame changes. -#[expect(dead_code, reason = "the gbuffers pass binds it (next slice)")] pub(super) struct PackAtlas { texture: wgpu::Texture, pub view: wgpu::TextureView, @@ -64,10 +63,6 @@ impl PackAtlas { } /// The atlas's size in pixels (`atlasSize`). - #[cfg_attr( - not(test), - expect(dead_code, reason = "the gbuffers pass reads it (next slice)") - )] pub fn size(&self) -> [u32; 2] { [self.texture.width(), self.texture.height()] } @@ -118,6 +113,70 @@ impl PackAtlas { } } +impl PackAtlas { + /// The vertex inputs of a gbuffers program drawing terrain (`TerrainVertex`, terrain.wgsl's + /// `vs_terrain` or, for `flat` draws, `vs_terrain_flat`), in atlas coordinates. + pub fn terrain_inputs(&self, flat: bool) -> horizon_shaders::GbuffersInputs { + let mut declarations = String::from( + "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uvec2 hz_v_uv;\n\ + layout(location = 2) in uint hz_v_layer;\nlayout(location = 3) in vec4 hz_v_color;\n\ + layout(location = 4) in uvec2 hz_v_light;\n", + ); + let origin = if flat { + declarations.push_str( + "layout(set = 1, binding = 0) readonly buffer HzBlockSlots { uint hz_block_slots[]; };\n\ + layout(set = 1, binding = 1) readonly buffer HzSlotOrigins { vec4 hz_slot_origins[]; };\n", + ); + "hz_slot_origins[hz_block_slots[uint(gl_VertexIndex) >> 8u]]" + } else { + declarations.push_str("layout(location = 5) in vec4 hz_v_origin;\n"); + "hz_v_origin" + }; + let [width, height] = self.size(); + let scale = [ + self.sprite as f32 / width as f32, + self.sprite as f32 / height as f32, + ]; + let columns = self.columns; + // UVs past a sprite (models up to 2, distant tiles) cannot repeat in an atlas: clamped. + let body = format!( + " vec4 hz_origin = {origin};\n \ + uint hz_layer = hz_v_layer & 32767u;\n \ + hz_cell = vec2(float(hz_layer % {columns}u), float(hz_layer / {columns}u));\n \ + hz_scale = vec2({:?}, {:?});\n \ + hz_Vertex = vec4(hz_v_pos + hz_origin.xyz, 1.0);\n \ + hz_MultiTexCoord0 = vec4((hz_cell + clamp(vec2(hz_v_uv) * hz_origin.w, 0.0, 1.0)) * hz_scale, 0.0, 1.0);\n \ + hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \ + hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \ + hz_Color = hz_v_color;\n \ + hz_Normal = vec3(0.0, 1.0, 0.0);\n", + scale[0], scale[1], + ); + declarations.push_str("vec2 hz_cell; vec2 hz_scale;\n"); + let attributes = [ + ("mc_Entity", "vec4(-1.0, -1.0, 0.0, 0.0)"), + ( + "mc_midTexCoord", + "vec4((hz_cell + 0.5) * hz_scale, 0.0, 1.0)", + ), + ("at_tangent", "vec4(1.0, 0.0, 0.0, 1.0)"), + ("vaPosition", "hz_Vertex.xyz"), + ("vaUV0", "hz_MultiTexCoord0.xy"), + ("vaUV2", "hz_MultiTexCoord1.xy"), + ("vaColor", "hz_Color"), + ("vaNormal", "hz_Normal"), + ]; + horizon_shaders::GbuffersInputs { + declarations, + body, + attributes: attributes + .iter() + .map(|&(n, v)| (n.to_owned(), v.to_owned())) + .collect(), + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index d5aa9fb7c..81a517dba 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -29,6 +29,9 @@ enum Source { Dummy, /// A 1×1 depth of 1.0 behind a comparison sampler, until shadow maps exist: no shadow. Shadow, + /// The block atlas (pack_atlas.rs), in gbuffers programs. + Atlas, + Lightmap, } struct Binding { @@ -66,9 +69,22 @@ struct Targets { depth: wgpu::TextureView, } +/// What a pack's gbuffers programs read and draw from the game's world. +pub(super) struct World<'a> { + /// The block atlas and its size. + pub atlas: Option<(wgpu::TextureView, [u32; 2])>, + pub lightmap: wgpu::TextureView, + /// Terrain's geometry, when the pack draws it. + pub terrain: Option>, +} + pub struct PackPost { pub notes: Vec, passes: Vec, + /// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`. + terrain: Option, + atlas: Option, + lightmap: wgpu::TextureView, deferred_pre: u16, composite_pre: u16, formats: [wgpu::TextureFormat; BUFFERS], @@ -85,6 +101,7 @@ pub struct PackPost { linear: wgpu::Sampler, nearest: wgpu::Sampler, repeat: wgpu::Sampler, + nearest_repeat: wgpu::Sampler, blit: (wgpu::RenderPipeline, wgpu::BindGroupLayout), /// The blit also writes depthtex0 (some program reads it) and clears these buffers. blit_depth: bool, @@ -133,6 +150,8 @@ fn source(name: &str) -> Source { match name { "depthtex0" | "gdepthtex" | "depthtex1" | "depthtex2" => Source::Depth, "noisetex" => Source::Noise, + "texture" | "gtexture" | "tex" => Source::Atlas, + "lightmap" => Source::Lightmap, n => horizon_shaders::buffer_index(n).map_or(Source::Dummy, Source::Color), } } @@ -228,6 +247,7 @@ fn helper( entry: &str, entries: &[wgpu::BindGroupLayoutEntry], targets: &[Option], + depth: Option, ) -> (wgpu::RenderPipeline, wgpu::BindGroupLayout) { let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("Shader pack helpers"), @@ -252,7 +272,7 @@ fn helper( buffers: &[], }, primitive: Default::default(), - depth_stencil: None, + depth_stencil: depth, multisample: Default::default(), fragment: Some(wgpu::FragmentState { module: &module, @@ -306,6 +326,211 @@ fn attach( }) } +/// The game's geometry a gbuffers program draws: its vertex buffers and, for flat draws +/// (flat.rs), the layout of their bind group 1. +pub(super) struct Geometry<'a> { + pub buffers: &'a [Option>], + pub table: Option<&'a wgpu::BindGroupLayout>, +} + +/// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when +/// wgpu refuses it or it writes nothing. +fn build_pass( + device: &wgpu::Device, + p: horizon_shaders::PostProgram, + formats: &[wgpu::TextureFormat; BUFFERS], + screen: wgpu::TextureFormat, + notes: &mut Vec, + geometry: Option<&Geometry<'_>>, +) -> Option { + let features = device.features(); + let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); + let interface = &p.program.interface; + let mut entries = vec![entry( + 0, + wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: None, + }, + )]; + let mut samplers = Vec::new(); + let mut reads = 0u16; + for s in &interface.samplers { + let mut src = source(&s.name); + if src == Source::Atlas && geometry.is_none() { + src = Source::Dummy; + } + let float = wgpu::TextureSampleType::Float { filterable: true }; + let mut sample_type = match src { + Source::Color(n) => formats[n] + .sample_type(None, Some(features)) + .unwrap_or(float), + Source::Depth => wgpu::TextureSampleType::Float { filterable: false }, + // Unfilterable on WebGPU-level devices; the lightmap is sampled at texel centres. + Source::Lightmap => float, + _ => float, + }; + let int = !matches!(sample_type, wgpu::TextureSampleType::Float { .. }); + if s.ty == "sampler2DShadow" { + (src, sample_type) = (Source::Shadow, wgpu::TextureSampleType::Depth); + } + let wanted = match s.ty.as_str() { + "sampler2DShadow" => true, + "sampler2D" => !int, + "isampler2D" => sample_type == wgpu::TextureSampleType::Sint, + "usampler2D" => sample_type == wgpu::TextureSampleType::Uint, + _ => false, + }; + if !wanted { + notes.push(format!( + "{}: `{} {}` is not provided yet", + p.name, s.ty, s.name + )); + (src, sample_type) = (Source::Dummy, float); + } + let filtering = sample_type == float; + if let Source::Color(n) = src { + reads |= 1 << n; + } + entries.push(texture_entry(s.binding, sample_type)); + entries.push(entry( + s.binding + 1, + wgpu::BindingType::Sampler(if src == Source::Shadow { + wgpu::SamplerBindingType::Comparison + } else if filtering { + wgpu::SamplerBindingType::Filtering + } else { + wgpu::SamplerBindingType::NonFiltering + }), + )); + samplers.push(Binding { + source: src, + binding: s.binding, + filtering, + }); + } + let final_pass = p.group == Group::Final; + let mut targets = Vec::new(); + let mut writes = Vec::new(); + for &location in &interface.outputs { + let l = location as usize; + if targets.len() <= l { + targets.resize(l + 1, None); + } + if final_pass { + if location == 0 { + targets[0] = target(screen); + } + } else if let Some(&buffer) = p.buffers.get(l) { + targets[l] = target(formats[buffer as usize]); + writes.push((location, buffer as usize)); + } + } + if !final_pass && writes.is_empty() { + notes.push(format!("{}: writes no buffer; skipped", p.name)); + return None; + } + let written = writes.iter().fold(0u16, |m, w| m | 1 << w.1); + let kept = p + .flips + .iter() + .filter(|f| !f.1) + .fold(0u16, |m, f| m | 1 << f.0); + // Gbuffers programs write in place (6.1.6). + let flip = if final_pass || geometry.is_some() { + 0 + } else { + (written & !kept) | mask(&p.flips) + }; + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some(&p.name), + entries: &entries, + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some(&p.name), + bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)], + ..Default::default() + }); + let created = checked(device, || { + let module = |m: naga::Module| { + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some(&p.name), + source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)), + }) + }; + let (vs, fs) = (module(p.program.vertex), module(p.program.fragment)); + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(&p.name), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &vs, + entry_point: Some("main"), + compilation_options: Default::default(), + buffers: geometry.map_or(&[], |g| g.buffers), + }, + primitive: Default::default(), + depth_stencil: geometry.map(|_| wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(true), + depth_compare: Some(wgpu::CompareFunction::LessEqual), + stencil: Default::default(), + bias: Default::default(), + }), + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &fs, + entry_point: Some("main"), + compilation_options: Default::default(), + targets: &targets, + }), + multiview_mask: None, + cache: None, + }) + }); + let pipeline = match created { + Ok(p) => p, + Err(e) => { + notes.push(format!("{}: skipped: {e}", p.name)); + return None; + } + }; + let interface = &p.program.interface; + let missing: Vec<&str> = interface + .uniforms + .iter() + .filter(|u| !u.name.starts_with("hz_") && pack_uniforms::index(&u.name).is_none()) + .map(|u| u.name.as_str()) + .collect(); + if !missing.is_empty() { + notes.push(format!("{}: zero for now: {}", p.name, missing.join(" "))); + } + Some(Pass { + ubo: device.create_buffer(&wgpu::BufferDescriptor { + label: Some(&p.name), + size: u64::from(interface.uniform_size.max(16)), + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }), + block: vec![0; interface.uniform_size as usize], + uniforms: interface + .uniforms + .iter() + .map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset)) + .collect(), + name: p.name, + group: p.group, + pipeline, + layout, + samplers, + writes, + flip, + reads, + written, + groups: HashMap::new(), + }) +} + impl PackPost { /// Builds the pipelines of `post` for a screen of `screen` format. pub fn new( @@ -313,6 +538,7 @@ impl PackPost { queue: &wgpu::Queue, post: Post, screen: wgpu::TextureFormat, + world: World<'_>, ) -> Self { let features = device.features(); let formats: [wgpu::TextureFormat; BUFFERS] = @@ -329,205 +555,56 @@ impl PackPost { let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let mut used = 1u16; - let mut passes = Vec::new(); - for p in post.programs { - let interface = &p.program.interface; - let mut entries = vec![entry( - 0, - wgpu::BindingType::Buffer { - ty: wgpu::BufferBindingType::Uniform, - has_dynamic_offset: false, - min_binding_size: None, - }, - )]; - let mut samplers = Vec::new(); - let mut reads = 0u16; - for s in &interface.samplers { - let mut src = source(&s.name); - let float = wgpu::TextureSampleType::Float { filterable: true }; - let mut sample_type = match src { - Source::Color(n) => formats[n] - .sample_type(None, Some(features)) - .unwrap_or(float), - Source::Depth => wgpu::TextureSampleType::Float { filterable: false }, - _ => float, - }; - let int = !matches!(sample_type, wgpu::TextureSampleType::Float { .. }); - if s.ty == "sampler2DShadow" { - (src, sample_type) = (Source::Shadow, wgpu::TextureSampleType::Depth); - } - let wanted = match s.ty.as_str() { - "sampler2DShadow" => true, - "sampler2D" => !int, - "isampler2D" => sample_type == wgpu::TextureSampleType::Sint, - "usampler2D" => sample_type == wgpu::TextureSampleType::Uint, - _ => false, - }; - if !wanted { - notes.push(format!( - "{}: `{} {}` is not provided yet", - p.name, s.ty, s.name - )); - (src, sample_type) = (Source::Dummy, float); - } - let filtering = sample_type == float; - if let Source::Color(n) = src { - reads |= 1 << n; - } - entries.push(texture_entry(s.binding, sample_type)); - entries.push(entry( - s.binding + 1, - wgpu::BindingType::Sampler(if src == Source::Shadow { - wgpu::SamplerBindingType::Comparison - } else if filtering { - wgpu::SamplerBindingType::Filtering - } else { - wgpu::SamplerBindingType::NonFiltering - }), - )); - samplers.push(Binding { - source: src, - binding: s.binding, - filtering, - }); - } - let final_pass = p.group == Group::Final; - let mut targets = Vec::new(); - let mut writes = Vec::new(); - for &location in &interface.outputs { - let l = location as usize; - if targets.len() <= l { - targets.resize(l + 1, None); - } - if final_pass { - if location == 0 { - targets[0] = target(screen); - } - } else if let Some(&buffer) = p.buffers.get(l) { - targets[l] = target(formats[buffer as usize]); - writes.push((location, buffer as usize)); - } - } - if !final_pass && writes.is_empty() { - notes.push(format!("{}: writes no buffer; skipped", p.name)); - continue; - } - let written = writes.iter().fold(0u16, |m, w| m | 1 << w.1); - let kept = p - .flips - .iter() - .filter(|f| !f.1) - .fold(0u16, |m, f| m | 1 << f.0); - let flip = if final_pass { - 0 - } else { - (written & !kept) | mask(&p.flips) - }; - used |= reads | written | flip; - let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some(&p.name), - entries: &entries, - }); - let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some(&p.name), - bind_group_layouts: &[Some(&layout)], - ..Default::default() - }); - let created = checked(device, || { - let module = |m: naga::Module| { - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some(&p.name), - source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)), - }) - }; - let (vs, fs) = (module(p.program.vertex), module(p.program.fragment)); - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some(&p.name), - layout: Some(&pipeline_layout), - vertex: wgpu::VertexState { - module: &vs, - entry_point: Some("main"), - compilation_options: Default::default(), - buffers: &[], - }, - primitive: Default::default(), - depth_stencil: None, - multisample: Default::default(), - fragment: Some(wgpu::FragmentState { - module: &fs, - entry_point: Some("main"), - compilation_options: Default::default(), - targets: &targets, - }), - multiview_mask: None, - cache: None, - }) - }); - let pipeline = match created { - Ok(p) => p, - Err(e) => { - notes.push(format!("{}: skipped: {e}", p.name)); - continue; - } - }; - let interface = &p.program.interface; - passes.push(Pass { - ubo: device.create_buffer(&wgpu::BufferDescriptor { - label: Some(&p.name), - size: u64::from(interface.uniform_size.max(16)), - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }), - block: vec![0; interface.uniform_size as usize], - uniforms: interface - .uniforms - .iter() - .map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset)) - .collect(), - name: p.name, - group: p.group, - pipeline, - layout, - samplers, - writes, - flip, - reads, - written, - groups: HashMap::new(), - }); + let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| { + let pass = build_pass(device, p, &formats, screen, &mut notes, geometry)?; + used |= pass.reads | pass.written | pass.flip; + Some(pass) + }; + let terrain = post + .terrain + .zip(world.terrain.as_ref()) + .and_then(|(p, g)| build(p, Some(g))); + let passes: Vec = post + .programs + .into_iter() + .filter_map(|p| build(p, None)) + .collect(); + if let Some(t) = &terrain { + notes.push(format!("terrain draws with {}", t.name)); } // A clear only matters when a program reads the buffer before one writes all of it - // (composite passes cover their whole target); the others are skipped. + // (composite passes cover their whole target, terrain does not); the others are skipped. let mut cleared = Vec::new(); for b in 1..BUFFERS { let bit = 1u16 << b; - let first = passes - .iter() - .find(|p| (p.reads | p.flip | p.written) & bit != 0); - if clears[b].is_some() - && first.is_some_and(|p| p.reads & bit != 0 || p.written & bit == 0) - { + let touched = |p: &&Pass| (p.reads | p.flip | p.written) & bit != 0; + let first = terrain.iter().find(touched).map(|_| true).or_else(|| { + let p = passes.iter().find(touched)?; + Some(p.reads & bit != 0 || p.written & bit == 0) + }); + if clears[b].is_some() && first == Some(true) { cleared.push(b); } } - let blit_depth = passes - .iter() - .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth)); - let room = 8 - 1 - usize::from(blit_depth); + // Terrain tests against the world's depth (entities...), so the blit copies it then. + let blit_depth = terrain.is_some() + || passes + .iter() + .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth)); + let room = 8 - 1; let other_clears = cleared.split_off(room.min(cleared.len())); let float = wgpu::TextureSampleType::Float { filterable: true }; - let mut blit_targets = vec![target(formats[0])]; - if blit_depth { - blit_targets.push(target(wgpu::TextureFormat::R32Float)); - } + let blit_targets = std::iter::once(target(formats[0])); // Cleared by the pass's load operation; the shader writes nothing there. - blit_targets.extend(cleared.iter().map(|&b| { - Some(wgpu::ColorTargetState { - format: formats[b], - blend: None, - write_mask: wgpu::ColorWrites::empty(), - }) - })); + let blit_targets: Vec<_> = blit_targets + .chain(cleared.iter().map(|&b| { + Some(wgpu::ColorTargetState { + format: formats[b], + blend: None, + write_mask: wgpu::ColorWrites::empty(), + }) + })) + .collect(); let blit = helper( device, "vs_full", @@ -541,6 +618,14 @@ impl PackPost { texture_entry(1, wgpu::TextureSampleType::Depth), ], &blit_targets, + // GL's window depth, written for depthtex0 and the terrain's depth test. + blit_depth.then_some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(true), + depth_compare: Some(wgpu::CompareFunction::Always), + stencil: Default::default(), + bias: Default::default(), + }), ); let copy = helper( device, @@ -554,11 +639,15 @@ impl PackPost { ), ], &[target(screen)], + None, ); let white = [255u8; 4]; Self { notes, passes, + terrain, + atlas: world.atlas.as_ref().map(|a| a.0.clone()), + lightmap: world.lightmap, deferred_pre: mask(&post.deferred_pre), composite_pre: mask(&post.composite_pre), formats, @@ -608,12 +697,153 @@ impl PackPost { wgpu::AddressMode::ClampToEdge, ), repeat: sampler(device, wgpu::FilterMode::Linear, wgpu::AddressMode::Repeat), + nearest_repeat: sampler(device, wgpu::FilterMode::Nearest, wgpu::AddressMode::Repeat), blit, copy, - uniforms: FrameUniforms::default(), + uniforms: { + let mut u = FrameUniforms::default(); + u.atlas = world.atlas.map_or([0; 2], |a| a.1); + u + }, } } + /// Writes `pass`'s uniforms for a target of `out` pixels; its bind group for the buffers' + /// current mapping. + fn bind( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + pass: &mut Pass, + out: [u32; 2], + ) -> wgpu::BindGroup { + let fog = self.uniforms.fog(); + // Our block members first (`hz_target`, `hz_fog_color`, `hz_fog`), then the pack's. + let ours = [ + [ + out[0] as f32, + out[1] as f32, + 1.0 / out[0] as f32, + 1.0 / out[1] as f32, + ], + fog[0].to_array(), + fog[1].to_array(), + ]; + let head: &[u8] = bytemuck::cast_slice(&ours); + let n = head.len().min(pass.block.len()); + pass.block[..n].copy_from_slice(&head[..n]); + for (index, ty, offset) in &pass.uniforms { + if let Some(v) = index.and_then(|i| self.uniforms.values.get(i)) { + v.write(ty, &mut pass.block, *offset as usize); + } + } + if !pass.block.is_empty() { + queue.write_buffer(&pass.ubo, 0, &pass.block); + } + let targets = self.targets.as_ref().expect("resized"); + let key = self.main & pass.reads; + let group = pass.groups.entry(key).or_insert_with(|| { + let mut entries = vec![wgpu::BindGroupEntry { + binding: 0, + resource: pass.ubo.as_entire_binding(), + }]; + for s in &pass.samplers { + let filter = if s.filtering { + &self.linear + } else { + &self.nearest + }; + let (texture, sampler) = match s.source { + Source::Color(i) => ( + targets.color[i] + .as_ref() + .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), + filter, + ), + Source::Depth => (&targets.depth, &self.nearest), + Source::Noise => (&self.noise, &self.repeat), + Source::Dummy => (&self.dummy, &self.linear), + Source::Shadow => (&self.shadow.0, &self.shadow.1), + // Blocks look as the game draws them: nearest texels. + Source::Atlas => ( + self.atlas.as_ref().unwrap_or(&self.dummy), + &self.nearest_repeat, + ), + Source::Lightmap => (&self.lightmap, &self.linear), + }; + entries.push(wgpu::BindGroupEntry { + binding: s.binding, + resource: wgpu::BindingResource::TextureView(texture), + }); + entries.push(wgpu::BindGroupEntry { + binding: s.binding + 1, + resource: wgpu::BindingResource::Sampler(sampler), + }); + } + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some(&pass.name), + layout: &pass.layout, + entries: &entries, + }) + }); + group.clone() + } + + /// Terrain with the pack's program into the buffers it writes, in place, tested against + /// the world's depth; `draw` issues the game's draws. + fn draw_terrain( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + size: [u32; 2], + draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), + ) { + let Some(mut pass) = self.terrain.take() else { + return; + }; + let group = self.bind(device, queue, &mut pass, size); + let targets = self.targets.as_ref().expect("resized"); + let load = wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }; + let mut attachments: Vec> = Vec::new(); + for &(location, buffer) in &pass.writes { + let l = location as usize; + if attachments.len() <= l { + attachments.resize(l + 1, None); + } + attachments[l] = targets.color[buffer] + .as_ref() + .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)); + } + { + let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some(&pass.name), + color_attachments: &attachments, + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &targets.depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Load, + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }), + ..Default::default() + }); + rp.set_pipeline(&pass.pipeline); + rp.set_bind_group(0, &group, &[]); + draw(&mut rp); + } + self.terrain = Some(pass); + } + + /// Whether the pack draws terrain itself. + pub fn has_terrain(&self) -> bool { + self.terrain.is_some() + } + /// The names of the programs that run, in order. pub fn programs(&self) -> Vec<&str> { self.passes.iter().map(|p| p.name.as_str()).collect() @@ -651,9 +881,9 @@ impl PackPost { self.targets = Some(Targets { size, color, - depth: make("depthtex0", wgpu::TextureFormat::R32Float), + depth: make("depthtex0", wgpu::TextureFormat::Depth32Float), }); - for pass in &mut self.passes { + for pass in self.passes.iter_mut().chain(&mut self.terrain) { pass.groups.clear(); } } @@ -673,6 +903,7 @@ impl PackPost { inputs: &PackInputs, size: [u32; 2], window: [u32; 2], + terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), ) { self.resize(device, size); self.uniforms.update(frame, inputs, size); @@ -751,9 +982,6 @@ impl PackPost { view(self.main, 0, false).expect("colortex0"), overwrite, )]; - if self.blit_depth { - attachments.push(attach(&targets.depth, overwrite)); - } for &b in &self.blit_clears { attachments.push(attach( view(self.main, b, false).expect("used buffer"), @@ -763,6 +991,16 @@ impl PackPost { let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Shader pack world"), color_attachments: &attachments, + depth_stencil_attachment: self.blit_depth.then_some( + wgpu::RenderPassDepthStencilAttachment { + view: &targets.depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Store, + }), + stencil_ops: None, + }, + ), ..Default::default() }); pass.set_pipeline(&self.blit.0); @@ -770,10 +1008,15 @@ impl PackPost { pass.draw(0..3, 0..1); } - let fog = self.uniforms.fog(); + let mut passes = std::mem::take(&mut self.passes); let mut seen = (false, false); let mut shown = false; - for pass in &mut self.passes { + let mut terrain_drawn = self.terrain.is_none(); + for pass in &mut passes { + if !terrain_drawn && pass.group != Group::Prepare { + terrain_drawn = true; + self.draw_terrain(device, queue, encoder, size, terrain); + } if pass.group == Group::Deferred && !seen.0 { seen.0 = true; self.main ^= self.deferred_pre; @@ -783,63 +1026,13 @@ impl PackPost { self.main ^= self.composite_pre; } let final_pass = pass.group == Group::Final; - let out = if final_pass { window } else { size }; - // Our block members first (`hz_target`, `hz_fog_color`, `hz_fog`), then the pack's. - let ours = [ - [ - out[0] as f32, - out[1] as f32, - 1.0 / out[0] as f32, - 1.0 / out[1] as f32, - ], - fog[0].to_array(), - fog[1].to_array(), - ]; - let head: &[u8] = bytemuck::cast_slice(&ours); - let n = head.len().min(pass.block.len()); - pass.block[..n].copy_from_slice(&head[..n]); - for (index, ty, offset) in &pass.uniforms { - if let Some(v) = index.and_then(|i| self.uniforms.values.get(i)) { - v.write(ty, &mut pass.block, *offset as usize); - } - } - if !pass.block.is_empty() { - queue.write_buffer(&pass.ubo, 0, &pass.block); - } - let key = self.main & pass.reads; - let group = pass.groups.entry(key).or_insert_with(|| { - let mut entries = vec![wgpu::BindGroupEntry { - binding: 0, - resource: pass.ubo.as_entire_binding(), - }]; - for s in &pass.samplers { - let filter = if s.filtering { - &self.linear - } else { - &self.nearest - }; - let (texture, sampler) = match s.source { - Source::Color(i) => (view(key, i, false).unwrap_or(&self.dummy), filter), - Source::Depth => (&targets.depth, &self.nearest), - Source::Noise => (&self.noise, &self.repeat), - Source::Dummy => (&self.dummy, &self.linear), - Source::Shadow => (&self.shadow.0, &self.shadow.1), - }; - entries.push(wgpu::BindGroupEntry { - binding: s.binding, - resource: wgpu::BindingResource::TextureView(texture), - }); - entries.push(wgpu::BindGroupEntry { - binding: s.binding + 1, - resource: wgpu::BindingResource::Sampler(sampler), - }); - } - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some(&pass.name), - layout: &pass.layout, - entries: &entries, - }) - }); + let group = self.bind(device, queue, pass, if final_pass { window } else { size }); + let targets = self.targets.as_ref().expect("resized"); + let view = |i: usize| { + targets.color[i] + .as_ref() + .map(|t| &t[usize::from(self.main & 1 << i == 0)]) + }; let mut attachments: Vec> = Vec::new(); if final_pass { attachments.push(attach(screen, overwrite)); @@ -850,8 +1043,7 @@ impl PackPost { if attachments.len() <= l { attachments.resize(l + 1, None); } - attachments[l] = - view(self.main, buffer, true).and_then(|v| attach(v, overwrite)); + attachments[l] = view(buffer).and_then(|v| attach(v, overwrite)); } } { @@ -861,11 +1053,21 @@ impl PackPost { ..Default::default() }); rp.set_pipeline(&pass.pipeline); - rp.set_bind_group(0, &*group, &[]); + rp.set_bind_group(0, &group, &[]); rp.draw(0..6, 0..1); } self.main ^= pass.flip; } + self.passes = passes; + if !terrain_drawn { + self.draw_terrain(device, queue, encoder, size, terrain); + } + let targets = self.targets.as_ref().expect("resized"); + let view = |m: u16, i: usize, alt: bool| { + targets.color[i] + .as_ref() + .map(|t| &t[main(m, i) ^ usize::from(alt)]) + }; if !shown { let group = device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("Shader pack colortex0"), diff --git a/crates/horizon-client/src/gfx/pack_post.wgsl b/crates/horizon-client/src/gfx/pack_post.wgsl index 30345b00f..75877dddc 100644 --- a/crates/horizon-client/src/gfx/pack_post.wgsl +++ b/crates/horizon-client/src/gfx/pack_post.wgsl @@ -1,5 +1,6 @@ // Helpers of a shader pack's post-processing (pack_post.rs): the world into colortex0 and -// depthtex0, and colortex0 onto the screen for packs without `final`. +// depthtex0 (a depth attachment, for terrain's depth test), and colortex0 onto the screen for +// packs without `final`. @vertex fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { @@ -12,7 +13,7 @@ fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { struct World { @location(0) color: vec4, - @location(1) depth: vec4, + @builtin(frag_depth) depth: f32, } // Pack buffers keep GL's rows (row 0 at the bottom), and GL's window depth: Horizon's @@ -23,7 +24,7 @@ fn fs_blit(@builtin(position) p: vec4) -> World { let at = vec2(i32(p.x), size.y - 1 - i32(p.y)); var out: World; out.color = textureLoad(source, at, 0); - out.depth = vec4(1.0 - textureLoad(world_depth, at, 0), 0.0, 0.0, 1.0); + out.depth = 1.0 - textureLoad(world_depth, at, 0); return out; } diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index 7d2a73a6a..92eac4a75 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -140,11 +140,14 @@ pub const NAMES: &[&str] = &[ "gbufferProjectionInverse", "gbufferPreviousModelView", "gbufferPreviousProjection", + "atlasSize", ]; /// Values carried from frame to frame. pub struct FrameUniforms { pub values: Vec, + /// The block atlas's size (`atlasSize`), 0 without one. + pub atlas: [u32; 2], start: std::time::Instant, last: Option, frame: u32, @@ -157,6 +160,7 @@ impl Default for FrameUniforms { fn default() -> Self { Self { values: Vec::new(), + atlas: [0; 2], start: std::time::Instant::now(), last: None, frame: 0, @@ -243,6 +247,7 @@ impl FrameUniforms { "worldDay" => Value::i(&[world_day as i32]), "moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]), "frameCounter" => Value::i(&[self.frame as i32]), + "atlasSize" => Value::i(&[self.atlas[0] as i32, self.atlas[1] as i32]), "frameTime" => Value::f(&[dt]), "frameTimeCounter" => Value::f(&[seconds]), "viewWidth" => Value::f(&[size[0] as f32]), diff --git a/crates/horizon-shaders/tests/packs/terrain-test/README.md b/crates/horizon-shaders/tests/packs/terrain-test/README.md new file mode 100644 index 000000000..bb8ddd060 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/README.md @@ -0,0 +1,8 @@ +# terrain-test + +Horizon's own shader pack for testing gbuffers terrain (written for Horizon, no other pack's code). +`gbuffers_terrain` draws blocks from the atlas with their vertex colour and lightmap, tinted by +the lightmap coordinate's sky light (cooler where the sky is hidden); `mc_midTexCoord` marks each +face's centre with a dot. `composite` puts the depth buffer's view-space distance into the blue +channel of the far half. Copy the folder into `shaderpacks/` and run the game with +`HORIZON_SHADER_PACK=terrain-test`. diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.fsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.fsh new file mode 100644 index 000000000..d473ef240 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.fsh @@ -0,0 +1,19 @@ +#version 120 + +uniform sampler2D colortex0; +uniform sampler2D depthtex0; +uniform mat4 gbufferProjectionInverse; +uniform float far; + +varying vec2 texcoord; + +void main() { + vec3 color = texture2D(colortex0, texcoord).rgb; + if (texcoord.x > 0.5) { + float depth = texture2D(depthtex0, texcoord).r; + vec4 view = gbufferProjectionInverse * vec4(vec3(texcoord, depth) * 2.0 - 1.0, 1.0); + color.b = clamp(length(view.xyz / view.w) / far, 0.0, 1.0); + } + /* DRAWBUFFERS:0 */ + gl_FragData[0] = vec4(color, 1.0); +} diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.vsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.vsh new file mode 100644 index 000000000..38e4e455c --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/composite.vsh @@ -0,0 +1,8 @@ +#version 120 + +varying vec2 texcoord; + +void main() { + gl_Position = ftransform(); + texcoord = gl_MultiTexCoord0.xy; +} diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh new file mode 100644 index 000000000..902cf3525 --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.fsh @@ -0,0 +1,22 @@ +#version 120 + +uniform sampler2D texture; +uniform sampler2D lightmap; +uniform ivec2 atlasSize; + +varying vec2 texcoord; +varying vec2 lmcoord; +varying vec2 mid; +varying vec4 glcolor; + +void main() { + vec4 color = texture2D(texture, texcoord) * glcolor; + if (color.a < 0.1) discard; + color.rgb *= texture2D(lightmap, lmcoord).rgb; + // Less sky light: bluer. + color.rgb = mix(color.rgb * vec3(0.6, 0.7, 1.2), color.rgb, lmcoord.y); + // A dot at the face's centre, one texel wide. + if (length((texcoord - mid) * vec2(atlasSize)) < 1.0) color.rgb = vec3(1.0, 0.0, 1.0); + /* DRAWBUFFERS:0 */ + gl_FragData[0] = color; +} diff --git a/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh new file mode 100644 index 000000000..bc16f4aad --- /dev/null +++ b/crates/horizon-shaders/tests/packs/terrain-test/shaders/gbuffers_terrain.vsh @@ -0,0 +1,16 @@ +#version 120 + +attribute vec4 mc_midTexCoord; + +varying vec2 texcoord; +varying vec2 lmcoord; +varying vec2 mid; +varying vec4 glcolor; + +void main() { + gl_Position = ftransform(); + texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy; + lmcoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy; + mid = mc_midTexCoord.xy; + glcolor = gl_Color; +} From 1fa46979c84c216f3e5c0d10d0704160db705f12 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:57:26 +0200 Subject: [PATCH 03/96] Server clients: primed TNT falls and burns its fuse by itself between updates PrimedTnt keeps Entity's NO_OP InterpolationHandler and ticks on a client too: a position packet puts it there at once, its own tick moves it on (gravity, collisions, bounces, drag) and burns the fuse down, and a client discards it when the fuse runs out. Horizon interpolated it towards each update instead, so it trailed about two ticks behind the server. lan_e2e's falling check now drops primed TNT too and checks that the guest has it exactly where its fall from rest puts it for the fuse the server sent. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/remote.rs | 19 +++++--- .../src/game/remote_projectiles.rs | 19 ++++++-- crates/horizon-client/src/game/tnt.rs | 2 +- tools/lan_e2e.py | 44 +++++++++++++++---- 4 files changed, 64 insertions(+), 20 deletions(-) diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 1c7e30abb..4a563f01c 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -1465,10 +1465,10 @@ impl Game<'_> { if let Some(p) = r.pending_items.get_mut(&id) { p.0 = position; } - // ItemEntity and FallingBlockEntity keep Entity's NO_OP - // InterpolationHandler: moveOrInterpolateTo puts them there at - // once, and their own tick moves them on between the server's - // updates (`tick_remote_entities`). + // ItemEntity, FallingBlockEntity and PrimedTnt keep Entity's + // NO_OP InterpolationHandler: moveOrInterpolateTo puts them there + // at once, and their own tick moves them on between the server's + // updates (`tick_remote_entities`, `tick_remote_projectiles`). let at = Vec3::new(position[0], position[1], position[2]); if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) { d.position = at; @@ -1479,6 +1479,11 @@ impl Game<'_> { f.position = at; return; } + if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id as u32) { + t.position = at; + t.on_ground = on_ground; + return; + } // AbstractInterpolationHandler.interpolateTo: without a rotation, // the one it was heading for. let rotation = rotation.or_else(|| r.lerps.get(&id).and_then(|l| l.rotation)); @@ -2974,14 +2979,16 @@ impl Game<'_> { }); } /// `Entity.lerpMotion` (and `recreateFromPacket`'s motion) for the - /// entities whose own tick moves them on a client: dropped items and - /// falling blocks. + /// entities whose own tick moves them on a client: dropped items, falling + /// blocks and primed TNT. fn remote_motion(&mut self, id: i32, velocity: [f64; 3]) { let v = Vec3::new(velocity[0], velocity[1], velocity[2]); if let Some(d) = self.dropped.iter_mut().find(|d| d.id == id as u32) { d.velocity = v; } else if let Some(f) = self.falling.iter_mut().find(|f| f.id == id as u32) { f.velocity = v; + } else if let Some(t) = self.tnts.iter_mut().find(|t| t.id == id as u32) { + t.velocity = v; } else if let Some(p) = self .remote .as_mut() diff --git a/crates/horizon-client/src/game/remote_projectiles.rs b/crates/horizon-client/src/game/remote_projectiles.rs index 33c69728b..5098e79da 100644 --- a/crates/horizon-client/src/game/remote_projectiles.rs +++ b/crates/horizon-client/src/game/remote_projectiles.rs @@ -181,10 +181,21 @@ impl Game<'_> { /// One tick of interpolation, as `tick_remote_objects`. pub(super) fn tick_remote_projectiles(&mut self, mut step: impl FnMut(u32, Vec3) -> Vec3) { - for t in &mut self.tnts { - t.previous = t.position; - t.position = step(t.id, t.position); - } + // PrimedTnt.tick runs on a client too: it falls, bounces and burns its + // fuse down (the swell and flash follow it) between the server's + // updates (every 10 ticks), and a client discards it when the fuse + // runs out; only the server explodes it. + let (world, collision, assets, facts) = ( + &self.world, + self.collision, + &self.pack.assets, + self.res.facts(), + ); + self.tnts.retain_mut(|t| { + // A fluid lookup that fails leaves it where its move put it. + let _ = t.tick(world, collision, assets, facts); + t.fuse > 0 + }); // FallingBlockEntity.tick runs on a client too: the block falls by // itself between the server's updates (every 20 ticks), then waits // where it lands for the server to place it and remove it. diff --git a/crates/horizon-client/src/game/tnt.rs b/crates/horizon-client/src/game/tnt.rs index fddc02125..6bd2ae956 100644 --- a/crates/horizon-client/src/game/tnt.rs +++ b/crates/horizon-client/src/game/tnt.rs @@ -33,7 +33,7 @@ impl PrimedTnt { ), ) } - fn tick( + pub(super) fn tick( &mut self, world: &horizon_client::world::World, collision: &horizon_world::block_collision::BlockCollisionRegistry<'_>, diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index a0fe0e181..fad55dfe8 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -228,9 +228,10 @@ address. The script then checks, from both sides, what a guest's game used to ge - villagers: the guest sees each villager's and zombie villager's type, profession and level as the host has them, and an unemployed villager that takes a composter becomes a farmer on the guest too (the guest used to draw every villager as an unemployed plains one). -- falling: an item and a falling block the host summons high up fall smoothly on the guest, a - little further every tick between the server's updates (every 20 ticks), as their own - client tick moves them (they used to hang, then jump down once a second, #1568). +- falling: an item, a falling block and primed TNT the host summons high up fall smoothly on the + guest, a little further every tick between the server's updates (every 20 ticks, 10 for + TNT), as their own client tick moves them (they used to hang, then jump down once a + second, #1568), and the TNT's fuse burns down on the guest too. Each check is reported on its own (a failing one doesn't stop the others) and the screenshots of both sides go to --out (host-*.png, guest-*.png). @@ -5500,10 +5501,11 @@ CHECK_FNS.update(villagers=check_villagers) def check_falling(host, guest, out): - """ItemEntity and FallingBlockEntity tick on a client too (gravity, collisions, drag), and their - position packets snap them (Entity's NO_OP InterpolationHandler). The server sends an item's - and a falling block's position every 20 ticks, so between two updates the guest's own tick is - what moves them: sampled every 2 ticks over the first 16, both are lower nearly every time.""" + """ItemEntity, FallingBlockEntity and PrimedTnt tick on a client too (gravity, collisions, drag), + and their position packets snap them (Entity's NO_OP InterpolationHandler). The server sends an + item's and a falling block's position every 20 ticks and TNT's every 10, so between two updates + the guest's own tick is what moves them: sampled every 2 ticks over the first 16, all three are + lower nearly every time. The TNT's fuse (sent once, at spawn) counts down on the guest too.""" x, y, z = (int(v // 1) for v in pos_of(guest)) top = y + 30 tag = 'falling_e2e' @@ -5512,7 +5514,10 @@ def check_falling(host, guest, out): f'{{Tags:["{tag}"],Item:{{id:"minecraft:diamond",count:1}},PickupDelay:32767}}') host('command', command=f'summon minecraft:falling_block {x + 2.5} {top} {z - 0.5} ' f'{{Tags:["{tag}"],BlockState:{{Name:"minecraft:sand"}},DropItem:0b}}') - heights = {'item': [], 'falling block': []} + # Horizon's selectors don't see primed TNT yet: its fuse ends it in the air, 19 blocks down. + host('command', command=f'summon minecraft:tnt {x + 4.5} {top} {z + 0.5} {{fuse:34s}}') + heights = {'item': [], 'falling block': [], 'primed tnt': []} + fuses = [] try: for _ in range(8): items = [e['pos'][1] for e in guest.raw('entities', 48, 'minecraft:item')['entities'] @@ -5520,8 +5525,12 @@ def check_falling(host, guest, out): blocks = [o['position'][1] for o in guest('status')['projectiles'] if o.get('kind') == 'minecraft:falling_block' and abs(o['position'][0] - x - 2.5) < 0.5 and abs(o['position'][2] - z + 0.5) < 0.5] + tnts = [o for o in guest('status')['projectiles'] if o.get('kind') == 'minecraft:tnt' + and abs(o['position'][0] - x - 4.5) < 0.5 and abs(o['position'][2] - z - 0.5) < 0.5] heights['item'].append(items[0] if items else None) heights['falling block'].append(blocks[0] if blocks else None) + heights['primed tnt'].append(tnts[0]['position'][1] if tnts else None) + fuses.append(tnts[0]['fuse'] if tnts else None) guest.wait(2) guest.shot(out, 'falling') finally: @@ -5533,7 +5542,24 @@ def check_falling(host, guest, out): drops = sum(b < a for a, b in zip(seen, seen[1:])) # A tick or two can pass between two samples with no move; never most of them. assert drops >= len(seen) - 2, f'the {name} fell in {drops} of {len(seen) - 1} samples: {heights}' - return heights + # The server sends the TNT's fuse with each move, so each sample says how many ticks it has + # fallen from rest: snapped to the server's place and ticking on, the guest has it exactly + # there (it used to trail two ticks behind, interpolating towards each update). + fell = [(h, 34 - f) for h, f in zip(heights['primed tnt'], fuses) if f is not None] + assert fell and fell[-1][1] > fell[0][1], f'the TNT\'s fuse on the guest: {fuses}' + off = max(abs(h - tnt_fall(top, n)) for h, n in fell) + assert off < 0.01, f'the guest\'s TNT is {off:.3f} off its fall (height, ticks): {fell}' + return {**heights, 'fuse': fuses} + + +def tnt_fall(top, ticks): + """PrimedTnt.tick in open air from rest: its height after `ticks` ticks.""" + y, v = top, 0.0 + for _ in range(ticks): + v -= 0.04 + y += v + v *= 0.98 + return y CHECKS += ['falling'] From 6c8880fbd887a69af1f12acc5f2f9c3b64cbec85 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:00:40 +0100 Subject: [PATCH 04/96] Shader packs, clean room 15: globals initialized at run time, switch options naga keeps only constant initializers of globals, so every initialized global that is not `const` (and every conditional one) gets its value from a function of ours that main calls first, one per declarator, converting as GLSL's initialization does (`vec2 v = atlasSize`). `vec3 a = x, b = y;` no longer loses `b`. Program-enable expressions (3.3) now see the pack's switch options that are on by default (8.1): `#define NAME` with nothing after it, tested with `#ifdef`/`#ifndef` anywhere in the pack (OQ-13 working answer). BSL's `deferred` (ambient occlusion) and its TAA ran with options off before. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 3.3, 8.1; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-shaders/src/lib.rs | 2 + crates/horizon-shaders/src/options.rs | 80 +++++++++++++ crates/horizon-shaders/src/pack.rs | 33 ++++++ crates/horizon-shaders/src/post.rs | 23 +++- crates/horizon-shaders/src/translate/mod.rs | 15 +++ crates/horizon-shaders/src/translate/scan.rs | 116 +++++++++++++++---- 6 files changed, 245 insertions(+), 24 deletions(-) create mode 100644 crates/horizon-shaders/src/options.rs diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index e31eaec6f..d29a0a632 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -17,6 +17,7 @@ mod config; mod discover; mod lang; mod macros; +mod options; mod pack; mod post; mod preprocess; @@ -28,6 +29,7 @@ pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffe pub use discover::{Found, discover}; pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; +pub use options::switches_on; pub use pack::{Pack, PackError}; pub use post::{Group, Post, PostProgram, dimension_folder, load_post}; pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; diff --git a/crates/horizon-shaders/src/options.rs b/crates/horizon-shaders/src/options.rs new file mode 100644 index 000000000..687fd40e0 --- /dev/null +++ b/crates/horizon-shaders/src/options.rs @@ -0,0 +1,80 @@ +//! Switch options (spec 8.1), as far as program-enable expressions (3.3) need them: which are on +//! by default. A switch is `#define NAME` (on) or `//#define NAME` (off) with nothing after the +//! name but a comment, in any stage or included file of the pack. Packs keep their options in +//! included `.glsl` files and test them elsewhere (OQ-13), so the test the spec asks for +//! (`#ifdef NAME` / `#ifndef NAME`) may be in any file of the pack. + +use std::collections::BTreeSet; + +use crate::Pack; + +const SOURCES: [&str; 8] = ["vsh", "fsh", "gsh", "csh", "tcs", "tes", "glsl", "inc"]; + +/// The switch options that are on by default. +pub fn switches_on(pack: &Pack) -> BTreeSet { + let (mut on, mut off, mut tested) = (BTreeSet::new(), BTreeSet::new(), BTreeSet::new()); + for path in pack.all_files() { + let ext = path.rsplit_once('.').map_or("", |(_, e)| e); + if !SOURCES.contains(&ext) { + continue; + } + let Some(text) = pack.read_text(&path) else { + continue; + }; + for line in text.lines() { + let line = line.trim(); + let (commented, rest) = match line.strip_prefix("//") { + Some(rest) => (true, rest.trim_start()), + None => (false, line), + }; + let Some((directive, rest)) = rest + .strip_prefix('#') + .map(str::trim_start) + .and_then(|r| r.split_once(|c: char| c.is_whitespace())) + else { + continue; + }; + let mut words = rest.split_whitespace(); + let Some(name) = words.next() else { continue }; + match directive { + "ifdef" | "ifndef" if !commented => _ = tested.insert(name.to_owned()), + "define" if words.next().is_none_or(|w| w.starts_with("//")) => { + let set = if commented { &mut off } else { &mut on }; + set.insert(name.to_owned()); + } + _ => {} + } + } + } + // Defined both ways is ambiguous (8.1): it keeps its default only where it is on everywhere. + on.retain(|name| tested.contains(name) && !off.contains(name)); + on +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::pack::tests::zip; + + #[test] + fn switches_need_a_test_and_no_value() { + let pack = Pack::from_zip_bytes( + "P.zip", + zip(&[ + ( + "shaders/lib/settings.glsl", + " #define AO // ambient occlusion\n//#define BLOOM\n#define LEVEL 2 //[1 2]\n\ + #define SPLIT\n#define UNTESTED\n", + ), + ( + "shaders/world0/deferred.fsh", + "#ifdef AO\n#endif\n#ifndef BLOOM\n#endif\n#ifdef SPLIT\n#endif\n#ifdef LEVEL\n#endif\n", + ), + ("shaders/world-1/deferred.fsh", "//#define SPLIT\n"), + ("shaders/shaders.properties", "#define NOT_SOURCE\n#ifdef NOT_SOURCE\n"), + ]), + ) + .unwrap(); + assert_eq!(switches_on(&pack), BTreeSet::from(["AO".to_owned()])); + } +} diff --git a/crates/horizon-shaders/src/pack.rs b/crates/horizon-shaders/src/pack.rs index ae2230618..77aeded35 100644 --- a/crates/horizon-shaders/src/pack.rs +++ b/crates/horizon-shaders/src/pack.rs @@ -167,6 +167,39 @@ impl Pack { names.sort(); names } + + /// Every file of the pack, by its path inside `shaders`, sorted. + pub fn all_files(&self) -> Vec { + let mut names = match &self.files { + Files::Folder(root) => { + let mut names = Vec::new(); + let mut dirs = vec![String::new()]; + while let Some(dir) = dirs.pop() { + for e in fs::read_dir(root.join(&dir)) + .into_iter() + .flatten() + .flatten() + { + let name = e.file_name().to_string_lossy().into_owned(); + let path = if dir.is_empty() { + name + } else { + format!("{dir}/{name}") + }; + match e.file_type() { + Ok(t) if t.is_dir() => dirs.push(path), + Ok(t) if t.is_file() => names.push(path), + _ => {} + } + } + } + names + } + Files::Zip { entries, .. } => entries.keys().cloned().collect(), + }; + names.sort(); + names + } } /// `a/./b/../c` → `a/c`; `None` for a path that climbs out of `shaders`. diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index cdf7fae80..c96730f0a 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -10,6 +10,8 @@ //! - A program that fails to translate is reported and treated as missing (2.5). //! - Terrain draws with the first program of its fallback chain that loads (2.3). +use std::collections::BTreeSet; + use crate::config::{self, Config}; use crate::{GbuffersInputs, Macros, Pack, Preprocessed, Program, Properties, Target}; use crate::{eval_condition, preprocess, translate_composite, translate_gbuffers}; @@ -99,6 +101,7 @@ pub fn load_post( folder, properties, macros, + switches: crate::switches_on(pack), post: &mut post, }; for (group, base) in GROUPS { @@ -135,6 +138,7 @@ struct Loader<'a> { folder: &'a str, properties: &'a Properties, macros: &'a Macros, + switches: BTreeSet, post: &'a mut Post, } @@ -156,7 +160,13 @@ impl Loader<'_> { if !self.pack.exists(&fsh) || !self.pack.exists(&vsh) { return None; } - if !enabled(self.properties, self.folder, &name, self.macros) { + if !enabled( + self.properties, + self.folder, + &name, + self.macros, + &self.switches, + ) { return None; } if self.pack.exists(&path("gsh")) { @@ -215,8 +225,14 @@ fn diagnostics(pp: &Preprocessed) -> Vec { } /// `program..enabled` (or `program./.enabled`): a boolean expression over -/// switch options (3.3). An option is on when its macro is defined. -fn enabled(properties: &Properties, folder: &str, name: &str, macros: &Macros) -> bool { +/// switch options (3.3): those on by default (8.1) and the defined macros. +fn enabled( + properties: &Properties, + folder: &str, + name: &str, + macros: &Macros, + switches: &BTreeSet, +) -> bool { let keys = [ format!("program.{folder}/{name}.enabled"), format!("program.{name}.enabled"), @@ -230,6 +246,7 @@ fn enabled(properties: &Properties, folder: &str, name: &str, macros: &Macros) - match token { "true" => condition.push('1'), "false" => condition.push('0'), + t if switches.contains(t) => condition.push('1'), t if t.starts_with(|c: char| c.is_alphabetic() || c == '_') => { condition.push_str(&format!("defined({t})")); } diff --git a/crates/horizon-shaders/src/translate/mod.rs b/crates/horizon-shaders/src/translate/mod.rs index 32eecb3cc..83dcb3af9 100644 --- a/crates/horizon-shaders/src/translate/mod.rs +++ b/crates/horizon-shaders/src/translate/mod.rs @@ -472,6 +472,21 @@ mod tests { assert_eq!(i.outputs, [0, 1]); } + /// naga drops non-constant global initializers: they run in our `main`, one per declarator, + /// converting as GLSL's initialization does. + #[test] + fn global_initializers_read_uniforms() { + let fsh = "#version 120\nuniform float rainStrength;\nuniform ivec2 atlasSize;\n\ + vec2 size = atlasSize;\nvec3 a = vec3(rainStrength), b = a * 2.0, c;\n\ + const float k = rainStrength > 0.5 ? 1.0 : 0.0;\nfloat w[] = float[](rainStrength, 1.0);\n\ + void main() { c = b; gl_FragColor = vec4(a + c, size.x * k + w[0]); }\n"; + let p = program( + &[("composite.vsh", VSH_120), ("composite.fsh", fsh)], + Target::Window, + ); + assert!(p.is_ok(), "{:?}", p.err()); + } + #[test] fn core_programs_translate() { let vsh = "#version 330 core\nin vec3 vaPosition;\nin vec2 vaUV0;\nuniform mat4 modelViewMatrix, projectionMatrix;\n\ diff --git a/crates/horizon-shaders/src/translate/scan.rs b/crates/horizon-shaders/src/translate/scan.rs index 6789893f4..392538f03 100644 --- a/crates/horizon-shaders/src/translate/scan.rs +++ b/crates/horizon-shaders/src/translate/scan.rs @@ -338,41 +338,115 @@ impl<'p> Stage<'p> { } } - /// A global `T name = a ? b : c;`: naga can't evaluate a conditional at global scope, so - /// the initializer becomes a function of ours on the same line (`T name; T hz_init_name() { - /// return a ? b : c; }`), called by our `main` before the pack's. + /// A global `T name = a ? b : c;` or, without `const`, any initialized global: naga only + /// evaluates constant initializers at global scope (a conditional is not one; GL drivers + /// also take uniforms and other globals there), so each initializer becomes a function of + /// ours on the same line (`T a; T hz_init_a() { return T(...); } T b; ...`), called by our + /// `main` before the pack's, in file order. The return converts as GLSL's initialization + /// does (`vec2 v = ivec2(...)`). pub(super) fn initializer(&mut self, words: &[usize], to: usize) { let text = |i: usize| self.tokens[i]; let Some(eq) = words.iter().position(|&i| text(i) == "=") else { return; }; - if !words[eq..].iter().any(|&i| text(i) == "?") - || words[..eq].iter().any(|&i| text(i) == ",") - { + // Declarators, split at top-level commas: (first word, `=` if any, end). + let mut declarators = Vec::new(); + let (mut begin, mut nesting, mut assign) = (0, 0i32, None); + for (n, &i) in words.iter().enumerate() { + match text(i) { + "(" | "[" | "{" => nesting += 1, + ")" | "]" | "}" => nesting -= 1, + "=" if nesting == 0 && assign.is_none() => assign = Some(n), + "," if nesting == 0 => { + declarators.push((begin, assign.take(), n)); + begin = n + 1; + } + _ => {} + } + } + declarators.push((begin, assign, words.len())); + let constant = words[..eq].iter().any(|&i| text(i) == "const"); + let conditional = + |d: &(usize, Option, usize)| words[d.0..d.2].iter().any(|&i| text(i) == "?"); + if constant && !declarators.iter().any(conditional) { return; } - let Some(name_at) = words[..eq].iter().rposition(|&i| is_ident(text(i))) else { + // The first declarator's name ends the type. + let first_end = declarators[0].1.unwrap_or(declarators[0].2); + let Some(name_at) = words[..first_end].iter().rposition(|&i| is_ident(text(i))) else { return; }; - let name = text(words[name_at]).to_owned(); - let array: String = words[name_at + 1..eq].iter().map(|&i| text(i)).collect(); - let ty: Vec<&str> = words[..name_at] - .iter() - .map(|&i| text(i)) - .filter(|&t| t != "const") - .collect(); - let Some(ty) = ty.last() else { return }; + // The type, with a `vec3[]`'s count written in. + let mut ty = String::new(); + for &i in &words[..name_at] { + match text(i) { + "const" => {} + "]" if ty.ends_with('[') => match self.array_length(i) { + Some(n) => write!(ty, "{n}]").unwrap(), + None => return, + }, + t if ty.is_empty() || t == "[" => ty.push_str(t), + // Qualifiers before the type (`highp`...): the type is the last word. + t => ty = t.to_owned(), + } + } + if ty.is_empty() { + return; + } + let mut replaced = Vec::new(); + let mut names = Vec::new(); + for (n, &(begin, assign, end)) in declarators.iter().enumerate() { + let name_at = if n == 0 { name_at } else { begin }; + let name = text(words[name_at]).to_owned(); + if !is_ident(&name) { + return; + } + let stop = assign.unwrap_or(end); + let mut array: String = words[name_at + 1..stop].iter().map(|&i| text(i)).collect(); + if array == "[]" { + // `T x[] = T[](a, b)`: the count is written in, as the rewrite does for the name. + match assign.and_then(|a| self.array_length(words[a - 1])) { + Some(n) => array = format!("[{n}]"), + None => return, + } + } + let next = if n + 1 < declarators.len() { + format!(" {ty}") + } else { + String::new() + }; + let end_token = words.get(end).copied().unwrap_or(to); + match assign { + Some(a) => { + // Scalars, vectors and matrices convert; arrays and structs must match. + let scalar = array.is_empty() + && !ty.contains('[') + && (matches!(ty.as_str(), "float" | "int" | "uint" | "bool") + || ty.contains("vec") + || ty.starts_with("mat")); + let (open, close) = if scalar { + (format!("{ty}("), ")") + } else { + (String::new(), "") + }; + replaced.push(( + words[a], + format!("; {ty}{array} hz_init_{name}() {{ return {open}"), + )); + replaced.push((end_token, format!("{close}; }}{next}"))); + names.push(name); + } + None if n + 1 < declarators.len() => replaced.push((end_token, format!(";{next}"))), + None => {} + } + } for &i in &words[..name_at] { if text(i) == "const" { self.removed.push(i..i + 1); } } - self.replaced.insert( - words[eq], - format!("; {ty}{array} hz_init_{name}() {{ return"), - ); - self.replaced.insert(to, "; }".into()); - self.inits.push(name); + self.replaced.extend(replaced); + self.inits.extend(names); } /// A function definition or prototype: remember which parameters are samplers. From 505e8e960f9956a51bde92a52b737d4f7f413c69 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:01:48 +0100 Subject: [PATCH 05/96] Shader packs, clean room 16: custom uniforms `uniform..` and `variable..` of shaders.properties (3.4): an expression parser with C precedence, the function table (math, vectors, `if`, `between`, `equals`, `smooth`, `random`...), evaluated on the CPU once per frame in file order after the frame's own uniforms. `smooth` keeps its state per call site and halves the distance to its target every fade time (OQ-26 working answer). A name neither the frame nor the pack defines reads 0 and is listed once in the log. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, section 3.4; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx/pack_post.rs | 36 +- .../horizon-client/src/gfx/pack_uniforms.rs | 41 ++ crates/horizon-shaders/src/custom.rs | 570 ++++++++++++++++++ crates/horizon-shaders/src/lib.rs | 2 + crates/horizon-shaders/src/post.rs | 4 + crates/horizon-shaders/tests/real_packs.rs | 7 + 6 files changed, 656 insertions(+), 4 deletions(-) create mode 100644 crates/horizon-shaders/src/custom.rs diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 81a517dba..6520565ed 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -13,7 +13,7 @@ use std::collections::HashMap; -use horizon_shaders::{Format, Group, Kind, Post}; +use horizon_shaders::{Format, Group, Kind, Post, Val}; use super::pack_uniforms::{self, FrameUniforms, PackInputs}; @@ -83,6 +83,8 @@ pub struct PackPost { passes: Vec, /// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`. terrain: Option, + /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. + custom: horizon_shaders::Custom, atlas: Option, lightmap: wgpu::TextureView, deferred_pre: u16, @@ -342,8 +344,16 @@ fn build_pass( screen: wgpu::TextureFormat, notes: &mut Vec, geometry: Option<&Geometry<'_>>, + custom: &[String], ) -> Option { let features = device.features(); + // The frame's uniforms, then the pack's custom ones. + let index = |name: &str| { + pack_uniforms::index(name).or_else(|| { + let k = custom.iter().position(|c| c == name)?; + Some(pack_uniforms::NAMES.len() + k) + }) + }; let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let interface = &p.program.interface; let mut entries = vec![entry( @@ -499,7 +509,7 @@ fn build_pass( let missing: Vec<&str> = interface .uniforms .iter() - .filter(|u| !u.name.starts_with("hz_") && pack_uniforms::index(&u.name).is_none()) + .filter(|u| !u.name.starts_with("hz_") && index(&u.name).is_none()) .map(|u| u.name.as_str()) .collect(); if !missing.is_empty() { @@ -516,7 +526,7 @@ fn build_pass( uniforms: interface .uniforms .iter() - .map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset)) + .map(|u| (index(&u.name), u.ty.clone(), u.offset)) .collect(), name: p.name, group: p.group, @@ -552,11 +562,27 @@ impl PackPost { }); let mut notes = post.notes; notes.extend(post.config.notes); + let mut custom = post.custom; + let custom_names: Vec = custom.uniforms().map(|u| u.0.to_owned()).collect(); + // A first run finds the names neither the frame nor the pack defines. + custom.eval(0.0, &|n| pack_uniforms::index(n).map(|_| Val::scalar(0.0))); + if !custom.notes.is_empty() { + notes.push(format!("custom uniforms: {}", custom.notes.join("; "))); + custom.notes.clear(); + } let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0); let mut used = 1u16; let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| { - let pass = build_pass(device, p, &formats, screen, &mut notes, geometry)?; + let pass = build_pass( + device, + p, + &formats, + screen, + &mut notes, + geometry, + &custom_names, + )?; used |= pass.reads | pass.written | pass.flip; Some(pass) }; @@ -646,6 +672,7 @@ impl PackPost { notes, passes, terrain, + custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), lightmap: world.lightmap, deferred_pre: mask(&post.deferred_pre), @@ -907,6 +934,7 @@ impl PackPost { ) { self.resize(device, size); self.uniforms.update(frame, inputs, size); + self.uniforms.eval_custom(&mut self.custom); let Some(targets) = &self.targets else { return }; let main = |m: u16, i: usize| usize::from(m & 1 << i != 0); let view = |m: u16, i: usize, alt: bool| { diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index 92eac4a75..5a27e62a0 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -304,6 +304,47 @@ impl FrameUniforms { } } +impl FrameUniforms { + /// Evaluates the pack's custom uniforms with this frame's values and puts them after + /// `NAMES`' values, in `custom.uniforms()` order. + pub fn eval_custom(&mut self, custom: &mut horizon_shaders::Custom) { + self.values.truncate(NAMES.len()); + let values = &self.values; + let builtin = |name: &str| { + let v = values.get(index(name)?)?; + let n = components(name); + Some(match *v { + Value::F(f) => horizon_shaders::Val::vector(&f.map(f64::from)[..n]), + Value::I(i) => horizon_shaders::Val::vector(&i.map(f64::from)[..n.min(3)]), + }) + }; + let dt = match index("frameTime").and_then(|i| values.get(i)) { + Some(Value::F(f)) => f64::from(f[0]), + _ => 0.0, + }; + let out = custom.eval(dt, &builtin); + self.values + .extend(out.iter().map(|v| Value::f(&v.v.map(|x| x as f32)[..v.n]))); + } +} + +/// How many components the uniform `name` has (16 for a matrix). +fn components(name: &str) -> usize { + match name { + "cameraPosition" + | "previousCameraPosition" + | "upPosition" + | "sunPosition" + | "moonPosition" + | "shadowLightPosition" + | "fogColor" + | "skyColor" => 3, + "eyeBrightness" | "eyeBrightnessSmooth" | "atlasSize" => 2, + n if n.starts_with("gbuffer") || n.starts_with("shadow") && n != "shadowAngle" => 16, + _ => 1, + } +} + /// The position of `name` in [`NAMES`]. pub fn index(name: &str) -> Option { NAMES.iter().position(|n| *n == name) diff --git a/crates/horizon-shaders/src/custom.rs b/crates/horizon-shaders/src/custom.rs new file mode 100644 index 000000000..34510247e --- /dev/null +++ b/crates/horizon-shaders/src/custom.rs @@ -0,0 +1,570 @@ +//! Custom uniforms and variables of `shaders.properties` (spec 3.4): `uniform..` and +//! `variable..` expressions, parsed once and evaluated on the CPU every frame in +//! file order. Uniforms reach every program; variables only later expressions. +//! +//! Decisions (impl-plan.md): +//! - OQ-25: numbers are doubles; booleans are 1 and 0; `%` is C's remainder (`fmod` floors); +//! comparisons and logic read the first component; a value takes its declared type at the +//! end (`int` truncates toward zero, `bool` is "not zero"). Unknown names read 0 and are +//! reported once. +//! - OQ-26: `smooth` runs per frame with the frame time; its fade times are seconds (1 by +//! default) to cover half of the remaining distance, `fadeIn` while rising, `fadeOut` while +//! falling. Each call keeps its own state, so the optional id is not needed. + +use std::collections::{HashMap, HashSet}; + +use crate::Properties; + +/// A value: up to 16 components (matrices only as names, for `m.row.column`). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Val { + pub v: [f64; 16], + /// Components: 1 for a scalar. + pub n: usize, +} + +impl Val { + pub fn scalar(x: f64) -> Self { + let mut v = [0.0; 16]; + v[0] = x; + Self { v, n: 1 } + } + pub fn vector(c: &[f64]) -> Self { + let mut v = [0.0; 16]; + v[..c.len()].copy_from_slice(c); + Self { v, n: c.len() } + } + fn x(&self) -> f64 { + self.v[0] + } + fn truth(&self) -> bool { + self.v[0] != 0.0 + } + /// `f` per component; a scalar operand stretches to the other's size. + fn zip(self, o: Val, f: impl Fn(f64, f64) -> f64) -> Val { + let n = self.n.max(o.n).min(4); + let at = |a: &Val, i: usize| if a.n == 1 { a.v[0] } else { a.v[i] }; + let mut out = Val::scalar(0.0); + out.n = n; + for i in 0..n { + out.v[i] = f(at(&self, i), at(&o, i)); + } + out + } + fn map(self, f: impl Fn(f64) -> f64) -> Val { + let mut out = self; + for i in 0..self.n.min(4) { + out.v[i] = f(self.v[i]); + } + out + } +} + +#[derive(Clone, Debug)] +enum Expr { + Num(f64), + /// A name and its component (`x`, `2`, `2.1` as row 2, column 1). + Name(String, Option<(usize, Option)>), + Call(String, Vec, usize), + Neg(Box), + Not(Box), + Bin(&'static str, Box, Box), +} + +#[derive(Debug)] +struct Item { + name: String, + ty: String, + uniform: bool, + expr: Expr, +} + +/// The pack's custom uniforms and variables, with the state `smooth` and `random` keep. +#[derive(Debug, Default)] +pub struct Custom { + items: Vec, + /// `smooth` state per call site. + smooth: HashMap, + seed: u64, + unknown: HashSet, + /// Expressions that did not parse, and unknown names, as `key: message`. + pub notes: Vec, + calls: usize, +} + +impl Custom { + /// Reads every `uniform.*` and `variable.*` key of `properties`, in file order. + pub fn parse(properties: &Properties) -> Self { + let mut out = Custom { + seed: 0x9e37_79b9_7f4a_7c15, + ..Default::default() + }; + for e in &properties.entries { + let (uniform, rest) = match e.key.split_once('.') { + Some(("uniform", r)) => (true, r), + Some(("variable", r)) => (false, r), + _ => continue, + }; + let Some((ty, name)) = rest.split_once('.') else { + continue; + }; + if !matches!(ty, "float" | "int" | "bool" | "vec2" | "vec3" | "vec4") { + out.notes + .push(format!("{}: type `{ty}` is not supported", e.key)); + continue; + } + match Parser::new(&e.value, &mut out.calls).expr_all() { + Ok(expr) => out.items.push(Item { + name: name.to_owned(), + ty: ty.to_owned(), + uniform, + expr, + }), + Err(m) => out.notes.push(format!("{}: {m}", e.key)), + } + } + out + } + + /// The uniforms' names and declared types, in file order. + pub fn uniforms(&self) -> impl Iterator { + self.items + .iter() + .filter(|i| i.uniform) + .map(|i| (i.name.as_str(), i.ty.as_str())) + } + + /// Evaluates everything for a frame `dt` seconds after the last; `builtin` gives the + /// values of the game's uniforms and constants (`BIOME_*`...). Returns the uniforms' + /// values, in the order of `uniforms`. + pub fn eval(&mut self, dt: f64, builtin: &dyn Fn(&str) -> Option) -> Vec { + let mut vars: HashMap = HashMap::new(); + let mut out = Vec::new(); + let items = std::mem::take(&mut self.items); + for item in &items { + let v = self.value(&item.expr, dt, &vars, builtin); + let v = match item.ty.as_str() { + "int" => Val::scalar(v.x().trunc()), + "bool" => Val::scalar(f64::from(u8::from(v.truth()))), + "float" => Val::scalar(v.x()), + t => { + let n = (t.as_bytes()[3] - b'0') as usize; + let mut v = v; + if v.n == 1 { + let x = v.v[0]; + v.v[1..n].fill(x); + } + v.n = n; + v + } + }; + vars.insert(item.name.clone(), v); + if item.uniform { + out.push(v); + } + } + self.items = items; + out + } + + fn random(&mut self) -> f64 { + self.seed ^= self.seed << 13; + self.seed ^= self.seed >> 7; + self.seed ^= self.seed << 17; + (self.seed >> 11) as f64 / (1u64 << 53) as f64 + } + + fn value( + &mut self, + e: &Expr, + dt: f64, + vars: &HashMap, + builtin: &dyn Fn(&str) -> Option, + ) -> Val { + let ev = |e: &Expr, s: &mut Self| s.value(e, dt, vars, builtin); + match e { + Expr::Num(x) => Val::scalar(*x), + Expr::Name(name, component) => { + let v = match name.as_str() { + "pi" => Some(Val::scalar(std::f64::consts::PI)), + "true" => Some(Val::scalar(1.0)), + "false" => Some(Val::scalar(0.0)), + n => vars.get(n).copied().or_else(|| builtin(n)), + }; + let Some(v) = v else { + if self.unknown.insert(name.clone()) { + self.notes.push(format!("`{name}` is unknown; it reads 0")); + } + return Val::scalar(0.0); + }; + match component { + None => v, + // GLSL's m[column][row] is stored column after column. + Some((row, Some(column))) => Val::scalar(v.v[column * 4 + row]), + Some((i, None)) => Val::scalar(v.v[*i]), + } + } + Expr::Neg(a) => ev(a, self).map(|x| -x), + Expr::Not(a) => Val::scalar(f64::from(u8::from(!ev(a, self).truth()))), + Expr::Bin(op, a, b) => { + let (a, b) = (ev(a, self), ev(b, self)); + let bool_ = |c: bool| Val::scalar(f64::from(u8::from(c))); + match *op { + "+" => a.zip(b, |x, y| x + y), + "-" => a.zip(b, |x, y| x - y), + "*" => a.zip(b, |x, y| x * y), + "/" => a.zip(b, |x, y| x / y), + "%" => a.zip(b, |x, y| x % y), + "<" => bool_(a.x() < b.x()), + "<=" => bool_(a.x() <= b.x()), + ">" => bool_(a.x() > b.x()), + ">=" => bool_(a.x() >= b.x()), + "==" => bool_(a.x() == b.x()), + "!=" => bool_(a.x() != b.x()), + "&&" => bool_(a.truth() && b.truth()), + _ => bool_(a.truth() || b.truth()), + } + } + Expr::Call(f, args, site) => { + let a: Vec = args.iter().map(|e| ev(e, self)).collect(); + let arg = |i: usize| a.get(i).copied().unwrap_or(Val::scalar(0.0)); + let x = arg(0).x(); + let one = |g: fn(f64) -> f64| arg(0).map(g); + match f.as_str() { + "sin" => one(f64::sin), + "cos" => one(f64::cos), + "tan" => one(f64::tan), + "asin" => one(f64::asin), + "acos" => one(f64::acos), + "atan" if a.len() == 2 => Val::scalar(x.atan2(arg(1).x())), + "atan" => one(f64::atan), + "atan2" => Val::scalar(x.atan2(arg(1).x())), + "torad" | "radians" => one(f64::to_radians), + "todeg" | "degrees" => one(f64::to_degrees), + "min" => a + .iter() + .copied() + .reduce(|p, q| p.zip(q, f64::min)) + .unwrap_or(arg(0)), + "max" => a + .iter() + .copied() + .reduce(|p, q| p.zip(q, f64::max)) + .unwrap_or(arg(0)), + "clamp" => arg(0).zip(arg(1), f64::max).zip(arg(2), f64::min), + "abs" => one(f64::abs), + "floor" => one(f64::floor), + "ceil" => one(f64::ceil), + "exp" => one(f64::exp), + "exp2" => one(f64::exp2), + "exp10" => one(|v| 10f64.powf(v)), + "frac" => one(|v| v - v.floor()), + "log" if a.len() == 2 => Val::scalar(arg(1).x().log(x)), + "log" => one(f64::ln), + "log2" => one(f64::log2), + "log10" => one(f64::log10), + "sqrt" => one(f64::sqrt), + "round" => one(f64::round), + "signum" | "sign" => one(|v| if v == 0.0 { 0.0 } else { v.signum() }), + "pow" => arg(0).zip(arg(1), f64::powf), + "fmod" => arg(0).zip(arg(1), |p, q| p - q * (p / q).floor()), + "lerp" => arg(1).zip(arg(2), |p, q| p + (q - p) * x), + "mix" => arg(0).zip(arg(1), |p, q| p + (q - p) * arg(2).x()), + "edge" => Val::scalar(if arg(1).x() < x { 0.0 } else { 1.0 }), + "random" if a.len() == 2 => Val::scalar(x + (arg(1).x() - x) * self.random()), + "random" => Val::scalar(self.random()), + "randomInt" if a.len() == 2 => { + Val::scalar((x + (arg(1).x() - x) * self.random()).floor()) + } + "randomInt" => Val::scalar((self.random() * f64::from(i32::MAX)).floor()), + "if" | "ifb" => { + let mut i = 0; + while i + 1 < a.len() { + if a[i].truth() { + return a[i + 1]; + } + i += 2; + } + a.last().copied().unwrap_or(Val::scalar(0.0)) + } + "smooth" => { + let (val, fade_in, fade_out) = match a.len() { + 4 => (arg(1).x(), arg(2).x(), arg(3).x()), + 3 => (x, arg(1).x(), arg(2).x()), + 2 => (x, arg(1).x(), arg(1).x()), + _ => (x, 1.0, 1.0), + }; + let state = self.smooth.entry(*site).or_insert(val); + let fade = if val > *state { fade_in } else { fade_out }; + if fade > 0.0 { + *state = val + (*state - val) * 0.5f64.powf(dt / fade); + } else { + *state = val; + } + Val::scalar(*state) + } + "print" => arg(2), + "between" => { + Val::scalar(f64::from(u8::from(x >= arg(1).x() && x <= arg(2).x()))) + } + "equals" => { + Val::scalar(f64::from(u8::from((x - arg(1).x()).abs() <= arg(2).x()))) + } + "in" => Val::scalar(f64::from(u8::from(a[1..].iter().any(|v| v.x() == x)))), + "vec2" | "vec3" | "vec4" => { + let n = (f.as_bytes()[3] - b'0') as usize; + let mut c: Vec = a.iter().flat_map(|v| v.v[..v.n].to_vec()).collect(); + if c.len() == 1 { + c = vec![c[0]; n]; + } + c.resize(n, 0.0); + Val::vector(&c) + } + _ => { + if self.unknown.insert(f.clone()) { + self.notes.push(format!("`{f}()` is unknown; it gives 0")); + } + Val::scalar(0.0) + } + } + } + } + } +} + +struct Parser<'a> { + tokens: Vec, + at: usize, + calls: &'a mut usize, +} + +const OPERATORS: [&str; 18] = [ + "||", "&&", "==", "!=", "<=", ">=", "<", ">", "+", "-", "*", "/", "%", "!", "(", ")", ",", ".", +]; + +impl<'a> Parser<'a> { + fn new(text: &str, calls: &'a mut usize) -> Self { + let mut tokens = Vec::new(); + let b = text.as_bytes(); + let mut i = 0; + while i < b.len() { + let c = b[i] as char; + if c.is_whitespace() { + i += 1; + } else if c.is_ascii_digit() + || (c == '.' + && b.get(i + 1).is_some_and(u8::is_ascii_digit) + && !tokens + .last() + .is_some_and(|t: &String| t == "." || is_name(t))) + { + let start = i; + while i < b.len() + && (b[i].is_ascii_alphanumeric() + || b[i] == b'.' + || ((b[i] == b'-' || b[i] == b'+') && matches!(b[i - 1], b'e' | b'E'))) + { + i += 1; + } + tokens.push(text[start..i].to_owned()); + } else if c.is_ascii_alphabetic() || c == '_' { + let start = i; + while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_') { + i += 1; + } + tokens.push(text[start..i].to_owned()); + } else { + let op = OPERATORS + .iter() + .find(|o| text[i..].starts_with(**o)) + .map_or(&text[i..i + c.len_utf8()], |o| o); + i += op.len(); + tokens.push(op.to_owned()); + } + } + Parser { + tokens, + at: 0, + calls, + } + } + fn peek(&self) -> Option<&str> { + self.tokens.get(self.at).map(String::as_str) + } + fn eat(&mut self, t: &str) -> bool { + let hit = self.peek() == Some(t); + self.at += usize::from(hit); + hit + } + fn expr_all(&mut self) -> Result { + let e = self.binary(0)?; + match self.peek() { + None => Ok(e), + Some(t) => Err(format!("unexpected `{t}`")), + } + } + /// Precedence levels from the loosest: `||`, `&&`, equality, relations, sums, products. + fn binary(&mut self, level: usize) -> Result { + const LEVELS: [&[&str]; 6] = [ + &["||"], + &["&&"], + &["==", "!="], + &["<", "<=", ">", ">="], + &["+", "-"], + &["*", "/", "%"], + ]; + if level == LEVELS.len() { + return self.unary(); + } + let mut left = self.binary(level + 1)?; + while let Some(op) = self + .peek() + .and_then(|t| LEVELS[level].iter().find(|o| **o == t)) + { + self.at += 1; + let right = self.binary(level + 1)?; + left = Expr::Bin(op, Box::new(left), Box::new(right)); + } + Ok(left) + } + fn unary(&mut self) -> Result { + if self.eat("-") { + return Ok(Expr::Neg(Box::new(self.unary()?))); + } + if self.eat("!") { + return Ok(Expr::Not(Box::new(self.unary()?))); + } + self.eat("+"); + self.primary() + } + fn primary(&mut self) -> Result { + let Some(t) = self.peek().map(str::to_owned) else { + return Err("the expression ends early".into()); + }; + self.at += 1; + if t == "(" { + let e = self.binary(0)?; + return if self.eat(")") { + Ok(e) + } else { + Err("missing `)`".into()) + }; + } + if t.starts_with(|c: char| c.is_ascii_digit() || c == '.') { + return t + .trim_end_matches(['f', 'F']) + .parse() + .map(Expr::Num) + .map_err(|_| format!("bad number `{t}`")); + } + if !is_name(&t) { + return Err(format!("unexpected `{t}`")); + } + if self.eat("(") { + let mut args = Vec::new(); + if !self.eat(")") { + loop { + args.push(self.binary(0)?); + if self.eat(")") { + break; + } + if !self.eat(",") { + return Err(format!("missing `)` after the arguments of `{t}`")); + } + } + } + *self.calls += 1; + return Ok(Expr::Call(t, args, *self.calls)); + } + let component = if self.eat(".") { + let c = self.peek().map(str::to_owned).ok_or("nothing after `.`")?; + self.at += 1; + let index = |s: &str| match s { + "x" | "r" | "s" | "0" => Some(0), + "y" | "g" | "t" | "1" => Some(1), + "z" | "b" | "p" | "2" => Some(2), + "w" | "a" | "q" | "3" => Some(3), + _ => None, + }; + Some(match c.split_once('.') { + Some((r, col)) => ( + index(r).ok_or(format!("bad component `{c}`"))?, + Some(index(col).ok_or(format!("bad component `{c}`"))?), + ), + None => (index(&c).ok_or(format!("bad component `{c}`"))?, None), + }) + } else { + None + }; + Ok(Expr::Name(t, component)) + } +} + +fn is_name(t: &str) -> bool { + t.starts_with(|c: char| c.is_ascii_alphabetic() || c == '_') +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Macros; + + fn custom(text: &str) -> Custom { + Custom::parse(&Properties::parse(text, &Macros::standard())) + } + + #[test] + fn expressions_follow_c_precedence_and_file_order() { + let mut c = custom( + "variable.float.a = 1 + 2 * 3 - 4 % 3\n\ + uniform.float.b = a / 2\n\ + uniform.bool.c = a > 5 && !(b < 1) || false\n\ + uniform.int.d = -7 / 2\n\ + uniform.vec3.e = vec3(a, cameraPosition.y, 2) * 2\n\ + uniform.float.f = if(a < 0, 1, a < 7, 2, 3) + clamp(fmod(-1, 4), 0, 2.5) + min(4, 2, 3)\n\ + uniform.float.g = gbufferModelView.1.0 + frac(sunAngle - 0.25) + pi * 0\n\ + uniform.float.h = unknownThing + in(biome, 3, BIOME_DESERT)\n", + ); + assert!(c.notes.is_empty(), "{:?}", c.notes); + let names: Vec<_> = c.uniforms().map(|u| u.0).collect(); + assert_eq!(names, ["b", "c", "d", "e", "f", "g", "h"]); + let builtin = |n: &str| match n { + "cameraPosition" => Some(Val::vector(&[1.0, 64.0, 2.0])), + "sunAngle" => Some(Val::scalar(0.125)), + "biome" => Some(Val::scalar(5.0)), + "BIOME_DESERT" => Some(Val::scalar(5.0)), + "gbufferModelView" => { + let mut m = Val::scalar(0.0); + m.v[1] = 9.0; // column 0, row 1 + m.n = 16; + Some(m) + } + _ => None, + }; + let v = c.eval(0.016, &builtin); + let x: Vec = v.iter().map(|v| v.v[0]).collect(); + assert_eq!(x, [3.0, 1.0, -3.0, 12.0, 2.0 + 2.5 + 2.0, 9.0 + 0.875, 1.0]); + assert_eq!(&v[3].v[..3], &[12.0, 128.0, 4.0]); + assert_eq!(c.notes, ["`unknownThing` is unknown; it reads 0"]); + } + + #[test] + fn smooth_halves_the_distance_per_fade_time() { + let mut c = custom("uniform.float.s = smooth(1, target, 2, 0.5)\n"); + let mut target = 0.0; + let mut run = |t: f64, dt: f64| { + target = t; + c.eval(dt, &|n: &str| (n == "target").then(|| Val::scalar(target)))[0].v[0] + }; + assert_eq!(run(0.0, 0.0), 0.0); + assert!((run(1.0, 2.0) - 0.5).abs() < 1e-9, "rising: fadeIn"); + assert!((run(0.0, 0.5) - 0.25).abs() < 1e-9, "falling: fadeOut"); + } + + #[test] + fn bad_expressions_are_reported() { + let c = custom("uniform.float.a = (1 + \nuniform.mat4.m = 1\nuniform.float.ok = 2\n"); + assert_eq!(c.uniforms().count(), 1); + assert_eq!(c.notes.len(), 2, "{:?}", c.notes); + } +} diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index d29a0a632..47cbcca11 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -14,6 +14,7 @@ mod condition; mod config; +mod custom; mod discover; mod lang; mod macros; @@ -26,6 +27,7 @@ mod translate; pub use condition::eval_condition; pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffers}; +pub use custom::{Custom, Val}; pub use discover::{Found, discover}; pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index c96730f0a..d6142c70a 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -13,6 +13,7 @@ use std::collections::BTreeSet; use crate::config::{self, Config}; +use crate::custom::Custom; use crate::{GbuffersInputs, Macros, Pack, Preprocessed, Program, Properties, Target}; use crate::{eval_condition, preprocess, translate_composite, translate_gbuffers}; @@ -62,6 +63,8 @@ pub struct Post { pub composite_pre: Vec<(u32, bool)>, /// What draws solid and cutout terrain, when the game asked for it and one loads. pub terrain: Option, + /// The custom uniforms of `shaders.properties` (3.4). + pub custom: Custom, pub config: Config, /// Programs skipped and why, as `program: message` or `file:line: message`. pub notes: Vec, @@ -93,6 +96,7 @@ pub fn load_post( deferred_pre: flips(properties, "deferred_pre"), composite_pre: flips(properties, "composite_pre"), terrain: None, + custom: Custom::parse(properties), config: Config::default(), notes: Vec::new(), }; diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index d0a28d71d..78ba97bfb 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -182,6 +182,13 @@ fn real_packs_read() { post.notes.len() + post.config.notes.len(), start.elapsed().as_secs_f64() * 1000.0 ); + let mut custom = post.custom; + custom.eval(0.016, &|_| None); + println!( + " {} custom uniforms; notes: {:?}", + custom.uniforms().count(), + custom.notes.iter().take(4).collect::>() + ); for n in post.notes.iter().chain(&post.config.notes).take(12) { println!(" {n}"); } From a2ec3fc791e773c69b852922ca53af1be298cd47 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:03:17 +0100 Subject: [PATCH 06/96] Shader packs, clean room 17: real packs' buffers and samplers - RGBA16 and R11F_G11F_B10F buffers get their own formats where the device has them (16-bit normalized render targets need the adapter's format features): the float stand-in kept NaN and values past 1, and BSL's TAA history went black for good. - Textures share one sampler variable per kind (linear, nearest, repeat, nearest-repeat, comparison): devices have as few as 16 sampler slots, Complementary's terrain program declares 23 textures. - A gbuffers pass reading the depth buffer or a buffer it draws into reads a blank texture instead (a feedback loop: undefined in GL, refused by wgpu, and a crash here). - A bind group layout that fails validation skips the program with the error's details instead of ending the game. - shadowModelView / shadowProjection (and inverses) for programs that read them before the shadow pass exists: an orthographic camera 160 blocks each side, toward the shadow light. - HORIZON_SHADER_PACK_SKIP=a,b leaves programs out (a developers' aid to bisect a pack). Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 6.1, 6.7, 5.2; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 9 +- crates/horizon-client/src/gfx/pack_post.rs | 243 ++++++++++++++---- .../horizon-client/src/gfx/pack_uniforms.rs | 16 ++ 3 files changed, 209 insertions(+), 59 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index c117b190f..a33d54fa3 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -816,12 +816,17 @@ async fn open_device( continue; } // Mappable primary buffers: section pages that no longer fit in - // video memory go to system memory. + // video memory go to system memory. Shader packs' 16-bit normalized buffers (render + // targets with the adapter's own format features) and R11F_G11F_B10F: a float + // stand-in neither clamps nor drops NaN. let features = timing::GpuTimer::features(&adapter) | cull::Cull::features(&adapter) | (adapter.features() & (wgpu::Features::INDIRECT_FIRST_INSTANCE - | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS)); + | wgpu::Features::MAPPABLE_PRIMARY_BUFFERS + | wgpu::Features::TEXTURE_FORMAT_16BIT_NORM + | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | wgpu::Features::RG11B10UFLOAT_RENDERABLE)); let driver = format!("{} {}", info.driver, info.driver_info); startup_step(format_args!( "requesting the device (driver {}) with features [{features}] and the adapter's \ diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 6520565ed..0201c7976 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -39,6 +39,82 @@ struct Binding { binding: u32, /// A filtering sampler (the texture is filterable). filtering: bool, + /// Where this texture's sampler object is bound: the first texture of each kind binds it + /// (`binding + 1`), the others share it (devices have as few as 16 sampler slots). + sampler: u32, +} + +/// The sampler objects a pass binds, one slot per kind it uses. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +enum SamplerKind { + Linear, + Nearest, + Repeat, + NearestRepeat, + Comparison, +} + +impl SamplerKind { + fn of(source: Source, filtering: bool) -> Self { + match source { + Source::Color(_) | Source::Dummy if !filtering => Self::Nearest, + Source::Depth => Self::Nearest, + Source::Noise => Self::Repeat, + Source::Shadow => Self::Comparison, + // Blocks look as the game draws them: nearest texels. + Source::Atlas => Self::NearestRepeat, + Source::Color(_) | Source::Dummy | Source::Lightmap => Self::Linear, + } + } + + fn binding_type(self) -> wgpu::SamplerBindingType { + match self { + Self::Linear | Self::Repeat => wgpu::SamplerBindingType::Filtering, + Self::Nearest | Self::NearestRepeat => wgpu::SamplerBindingType::NonFiltering, + Self::Comparison => wgpu::SamplerBindingType::Comparison, + } + } +} + +/// Points every sampler object of `module` at the slot `slot` gives its binding, so that +/// samplers sharing a slot become one variable (two variables may not share a binding). +fn share_samplers(module: &mut naga::Module, slot: &HashMap) { + let mut first = HashMap::new(); + let mut same = HashMap::new(); + for (h, g) in module.global_variables.iter_mut() { + let Some(b) = g.binding.as_mut() else { + continue; + }; + if !matches!(module.types[g.ty].inner, naga::TypeInner::Sampler { .. }) { + continue; + } + let Some(&to) = slot.get(&b.binding) else { + continue; + }; + b.binding = to; + match first.entry(to) { + std::collections::hash_map::Entry::Occupied(e) => _ = same.insert(h, *e.get()), + std::collections::hash_map::Entry::Vacant(e) => _ = e.insert(h), + } + } + if same.is_empty() { + return; + } + let fix = |expressions: &mut naga::Arena| { + for (_, e) in expressions.iter_mut() { + if let naga::Expression::GlobalVariable(g) = e + && let Some(&to) = same.get(g) + { + *g = to; + } + } + }; + for (_, f) in module.functions.iter_mut() { + fix(&mut f.expressions); + } + for ep in &mut module.entry_points { + fix(&mut ep.function.expressions); + } } struct Pass { @@ -128,7 +204,12 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat { let half = pick(T::R16Float, T::Rg16Float, T::Rgba16Float); match (f.kind, f.bits) { (Kind::Unorm, 10) => T::Rgb10a2Unorm, - (Kind::Unorm, 16) if features.contains(wgpu::Features::TEXTURE_FORMAT_16BIT_NORM) => { + (Kind::Unorm, 16) + if features.contains( + wgpu::Features::TEXTURE_FORMAT_16BIT_NORM + | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES, + ) => + { pick(T::R16Unorm, T::Rg16Unorm, T::Rgba16Unorm) } (Kind::Unorm, b) if b <= 8 => pick(T::R8Unorm, T::Rg8Unorm, T::Rgba8Unorm), @@ -229,8 +310,11 @@ fn checked(device: &wgpu::Device, create: impl FnOnce() -> T) -> Result { - // The details are in the error's sources. - let mut message = e.to_string(); + // The details are in the error's description or its sources. + let mut message = match &e { + wgpu::Error::Validation { source, .. } => format!("{source:?}"), + e => e.to_string(), + }; let mut source = std::error::Error::source(&e); while let Some(s) = source { message = format!("{message}: {s}"); @@ -365,12 +449,22 @@ fn build_pass( }, )]; let mut samplers = Vec::new(); + let (mut kinds, mut slots) = (HashMap::new(), HashMap::new()); let mut reads = 0u16; + // A gbuffers pass draws into its buffers and the depth buffer: reading them as well would + // be a feedback loop (undefined in GL, refused by wgpu), so those read a blank texture. + let in_place = match geometry { + Some(_) => p.buffers.iter().fold(0u16, |m, &b| m | 1 << b), + None => 0, + }; for s in &interface.samplers { let mut src = source(&s.name); - if src == Source::Atlas && geometry.is_none() { - src = Source::Dummy; - } + let blank = match src { + Source::Atlas => geometry.is_none(), + Source::Depth => geometry.is_some(), + Source::Color(n) => in_place & 1 << n != 0, + _ => false, + }; let float = wgpu::TextureSampleType::Float { filterable: true }; let mut sample_type = match src { Source::Color(n) => formats[n] @@ -399,25 +493,32 @@ fn build_pass( )); (src, sample_type) = (Source::Dummy, float); } + if blank { + // The blank texture is float: an integer sampler then fails the pipeline (a note). + src = Source::Dummy; + if int { + sample_type = float; + } + } let filtering = sample_type == float; if let Source::Color(n) = src { reads |= 1 << n; } entries.push(texture_entry(s.binding, sample_type)); - entries.push(entry( - s.binding + 1, - wgpu::BindingType::Sampler(if src == Source::Shadow { - wgpu::SamplerBindingType::Comparison - } else if filtering { - wgpu::SamplerBindingType::Filtering - } else { - wgpu::SamplerBindingType::NonFiltering - }), - )); + let kind = SamplerKind::of(src, filtering); + let sampler = *kinds.entry(kind).or_insert_with(|| { + entries.push(entry( + s.binding + 1, + wgpu::BindingType::Sampler(kind.binding_type()), + )); + s.binding + 1 + }); + slots.insert(s.binding + 1, sampler); samplers.push(Binding { source: src, binding: s.binding, filtering, + sampler, }); } let final_pass = p.group == Group::Final; @@ -453,24 +554,25 @@ fn build_pass( } else { (written & !kept) | mask(&p.flips) }; - let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some(&p.name), - entries: &entries, - }); - let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some(&p.name), - bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)], - ..Default::default() - }); let created = checked(device, || { - let module = |m: naga::Module| { + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some(&p.name), + entries: &entries, + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some(&p.name), + bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)], + ..Default::default() + }); + let module = |mut m: naga::Module| { + share_samplers(&mut m, &slots); device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some(&p.name), source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)), }) }; let (vs, fs) = (module(p.program.vertex), module(p.program.fragment)); - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some(&p.name), layout: Some(&pipeline_layout), vertex: wgpu::VertexState { @@ -496,10 +598,11 @@ fn build_pass( }), multiview_mask: None, cache: None, - }) + }); + (layout, pipeline) }); - let pipeline = match created { - Ok(p) => p, + let (layout, pipeline) = match created { + Ok(created) => created, Err(e) => { notes.push(format!("{}: skipped: {e}", p.name)); return None; @@ -590,9 +693,12 @@ impl PackPost { .terrain .zip(world.terrain.as_ref()) .and_then(|(p, g)| build(p, Some(g))); + // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out. + let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default(); let passes: Vec = post .programs .into_iter() + .filter(|p| !skip.split(',').any(|s| s == p.name)) .filter_map(|p| build(p, None)) .collect(); if let Some(t) = &terrain { @@ -775,37 +881,34 @@ impl PackPost { resource: pass.ubo.as_entire_binding(), }]; for s in &pass.samplers { - let filter = if s.filtering { - &self.linear - } else { - &self.nearest + let texture = match s.source { + Source::Color(i) => targets.color[i] + .as_ref() + .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), + Source::Depth => &targets.depth, + Source::Noise => &self.noise, + Source::Dummy => &self.dummy, + Source::Shadow => &self.shadow.0, + Source::Atlas => self.atlas.as_ref().unwrap_or(&self.dummy), + Source::Lightmap => &self.lightmap, }; - let (texture, sampler) = match s.source { - Source::Color(i) => ( - targets.color[i] - .as_ref() - .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), - filter, - ), - Source::Depth => (&targets.depth, &self.nearest), - Source::Noise => (&self.noise, &self.repeat), - Source::Dummy => (&self.dummy, &self.linear), - Source::Shadow => (&self.shadow.0, &self.shadow.1), - // Blocks look as the game draws them: nearest texels. - Source::Atlas => ( - self.atlas.as_ref().unwrap_or(&self.dummy), - &self.nearest_repeat, - ), - Source::Lightmap => (&self.lightmap, &self.linear), + let sampler = match SamplerKind::of(s.source, s.filtering) { + SamplerKind::Linear => &self.linear, + SamplerKind::Nearest => &self.nearest, + SamplerKind::Repeat => &self.repeat, + SamplerKind::NearestRepeat => &self.nearest_repeat, + SamplerKind::Comparison => &self.shadow.1, }; entries.push(wgpu::BindGroupEntry { binding: s.binding, resource: wgpu::BindingResource::TextureView(texture), }); - entries.push(wgpu::BindGroupEntry { - binding: s.binding + 1, - resource: wgpu::BindingResource::Sampler(sampler), - }); + if s.sampler == s.binding + 1 { + entries.push(wgpu::BindGroupEntry { + binding: s.sampler, + resource: wgpu::BindingResource::Sampler(sampler), + }); + } } device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some(&pass.name), @@ -1130,6 +1233,31 @@ mod tests { use super::*; use wgpu::TextureFormat as T; + /// Two textures with a sampler each become two textures sharing one sampler variable. + #[test] + fn samplers_of_a_kind_share_one_variable() { + let mut module = naga::front::wgsl::parse_str( + "@group(0) @binding(1) var a: texture_2d;\n\ + @group(0) @binding(2) var sa: sampler;\n\ + @group(0) @binding(3) var b: texture_2d;\n\ + @group(0) @binding(4) var sb: sampler;\n\ + @fragment fn main() -> @location(0) vec4 {\n\ + return textureSample(a, sa, vec2(0.5)) + textureSample(b, sb, vec2(0.5));\n}\n", + ) + .unwrap(); + share_samplers(&mut module, &HashMap::from([(2, 2), (4, 2)])); + let info = naga::valid::Validator::new(Default::default(), Default::default()) + .validate(&module) + .unwrap(); + let used: Vec = module + .global_variables + .iter() + .filter(|(h, _)| !info.get_entry_point(0)[*h].is_empty()) + .filter_map(|(_, g)| g.binding.as_ref().map(|b| b.binding)) + .collect(); + assert_eq!(used, [1, 2, 3]); + } + #[test] fn pack_formats_map_to_renderable_ones() { let f = |name| texture_format(Format::parse(name).unwrap(), wgpu::Features::empty()); @@ -1142,8 +1270,9 @@ mod tests { assert_eq!(f("RGBA16"), T::Rgba16Float, "16-bit norm needs a feature"); assert_eq!(f("RGBA8_SNORM"), T::Rgba16Float, "snorm is not renderable"); assert_eq!(f("R11F_G11F_B10F"), T::Rgba16Float); - let all = - wgpu::Features::TEXTURE_FORMAT_16BIT_NORM | wgpu::Features::RG11B10UFLOAT_RENDERABLE; + let all = wgpu::Features::TEXTURE_FORMAT_16BIT_NORM + | wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES + | wgpu::Features::RG11B10UFLOAT_RENDERABLE; let g = |name| texture_format(Format::parse(name).unwrap(), all); assert_eq!(g("RGBA16"), T::Rgba16Unorm); assert_eq!(g("R11F_G11F_B10F"), T::Rg11b10Ufloat); diff --git a/crates/horizon-client/src/gfx/pack_uniforms.rs b/crates/horizon-client/src/gfx/pack_uniforms.rs index 5a27e62a0..c7c4cfd8b 100644 --- a/crates/horizon-client/src/gfx/pack_uniforms.rs +++ b/crates/horizon-client/src/gfx/pack_uniforms.rs @@ -141,6 +141,10 @@ pub const NAMES: &[&str] = &[ "gbufferPreviousModelView", "gbufferPreviousProjection", "atlasSize", + "shadowModelView", + "shadowModelViewInverse", + "shadowProjection", + "shadowProjectionInverse", ]; /// Values carried from frame to frame. @@ -226,6 +230,14 @@ impl FrameUniforms { ), None => (frame.sky.fog_color, frame.distance * 0.9, frame.distance), }; + // The shadow camera (OQ-6, impl-plan.md): orthographic, `shadowDistance` (160) blocks + // each side, 100 blocks from the player toward the shadow light, world +z up. + let light = view + .inverse() + .transform_vector3(if day { sun } else { moon }) + .normalize(); + let shadow_view = Mat4::look_at_rh(light * 100.0, Vec3::ZERO, Vec3::Z); + let shadow_projection = Mat4::orthographic_rh_gl(-160.0, 160.0, -160.0, 160.0, 0.05, 256.0); let day_time = inputs.day_time.rem_euclid(24000) as i32; let world_day = inputs.day_time.div_euclid(24000); let seconds = (now - self.start).as_secs_f32() % 3600.0; @@ -247,6 +259,10 @@ impl FrameUniforms { "worldDay" => Value::i(&[world_day as i32]), "moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]), "frameCounter" => Value::i(&[self.frame as i32]), + "shadowModelView" => m4(shadow_view), + "shadowModelViewInverse" => m4(shadow_view.inverse()), + "shadowProjection" => m4(shadow_projection), + "shadowProjectionInverse" => m4(shadow_projection.inverse()), "atlasSize" => Value::i(&[self.atlas[0] as i32, self.atlas[1] as i32]), "frameTime" => Value::f(&[dt]), "frameTimeCounter" => Value::f(&[seconds]), From 3e2c30a852452a3ee0a4cf778d85aee55b98c205 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:47:14 +0100 Subject: [PATCH 07/96] Shader packs, clean room 18: translucent terrain drawn by the pack With a pack whose gbuffers_water (or a fallback down gbuffers_terrain's chain) loads, translucent terrain (water, stained glass) is drawn by it after the deferred programs and before the composites, in place, sorted back to front as the game sorts it, and Horizon's own translucent terrain draw is skipped. - blend. (off, or four GL factors); translucent terrain defaults to SRC_ALPHA ONE_MINUS_SRC_ALPHA ONE ONE_MINUS_SRC_ALPHA, and only blendable buffer formats blend. - depthtex1/depthtex2: depth without translucent terrain, a copy taken just before it when a later program reads them (before it, depthtex0 itself). The hand is drawn after the pack, so depthtex2 = depthtex1. Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1, sections 2.1, 2.3, 3.2 (blend), 6.2 (depthtex1/2); no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 26 ++- crates/horizon-client/src/gfx/pack_post.rs | 213 +++++++++++++++++---- crates/horizon-shaders/src/lib.rs | 4 +- crates/horizon-shaders/src/post.rs | 130 ++++++++++++- crates/horizon-shaders/tests/real_packs.rs | 3 +- 5 files changed, 327 insertions(+), 49 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index a33d54fa3..9a7356d6e 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -3709,6 +3709,12 @@ impl Renderer { let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some()); // The pack's program reads the vertices this frame's draws bind. let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat; + let pack_draws_water = pack_draws_terrain + && self + .pack_post + .as_ref() + .and_then(|p| p.1.as_ref()) + .is_some_and(|p| p.has_water()); let scaled = self.world_post.prepare( &self.device, [self.config.width, self.config.height], @@ -3940,6 +3946,8 @@ impl Renderer { pass.draw(0..4, debug_depth..debug_depth + debug_top); } match self.flat.as_ref().filter(|_| flat_frame) { + // The pack draws translucent terrain after its deferred programs. + _ if pack_draws_water => {} Some(flat) => { pass.set_pipeline(&flat.translucent); flat.draw(&mut pass, &flat.calls[flat.solid_calls..], |p| { @@ -4016,20 +4024,28 @@ impl Renderer { { let size = self.world_post.size().unwrap_or([1, 1]); let flat = self.flat.as_ref().filter(|_| flat_frame); - let mut terrain = |rp: &mut wgpu::RenderPass<'_>| { + let mut terrain = |rp: &mut wgpu::RenderPass<'_>, translucent: bool| { if !pack_draws_terrain { return; } match flat { Some(flat) => { - flat.draw(rp, &flat.calls[..flat.solid_calls], |p| { - self.arena.buffer(p) - }); + let calls = if translucent { + &flat.calls[flat.solid_calls..] + } else { + &flat.calls[..flat.solid_calls] + }; + flat.draw(rp, calls, |p| self.arena.buffer(p)); } None => { rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); rp.set_vertex_buffer(1, self.instances.slice(..)); - self.draw_sections(rp, &self.indirect, 0, 0, solid_draws); + let (first, count) = if translucent { + (solid_draws, translucent_draws) + } else { + (0, solid_draws) + }; + self.draw_sections(rp, &self.indirect, 0, first, count); } } }; diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index 0201c7976..b96da2599 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -24,6 +24,8 @@ const BUFFERS: usize = horizon_shaders::BUFFERS; enum Source { Color(usize), Depth, + /// `depthtex1`/`depthtex2`: depth without translucent terrain (a copy taken before it). + Depth1, Noise, /// A 1×1 stand-in for names this slice does not provide yet (custom textures...). Dummy, @@ -58,7 +60,7 @@ impl SamplerKind { fn of(source: Source, filtering: bool) -> Self { match source { Source::Color(_) | Source::Dummy if !filtering => Self::Nearest, - Source::Depth => Self::Nearest, + Source::Depth | Source::Depth1 => Self::Nearest, Source::Noise => Self::Repeat, Source::Shadow => Self::Comparison, // Blocks look as the game draws them: nearest texels. @@ -143,6 +145,9 @@ struct Targets { /// A and B of each buffer used. color: [Option<[wgpu::TextureView; 2]>; BUFFERS], depth: wgpu::TextureView, + depth_texture: wgpu::Texture, + /// `depthtex1`, when a program after translucent terrain reads it. + depth1: Option<(wgpu::Texture, wgpu::TextureView)>, } /// What a pack's gbuffers programs read and draw from the game's world. @@ -159,6 +164,10 @@ pub struct PackPost { passes: Vec, /// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`. terrain: Option, + /// What draws translucent terrain (`gbuffers_water` or its fallback), after `deferred`. + water: Option, + /// A program after translucent terrain reads `depthtex1`: it is copied before water. + depth1: bool, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. custom: horizon_shaders::Custom, atlas: Option, @@ -231,7 +240,9 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat { fn source(name: &str) -> Source { match name { - "depthtex0" | "gdepthtex" | "depthtex1" | "depthtex2" => Source::Depth, + "depthtex0" | "gdepthtex" => Source::Depth, + // The hand is drawn after the pack, so `depthtex2` (without it) is `depthtex1`. + "depthtex1" | "depthtex2" => Source::Depth1, "noisetex" => Source::Noise, "texture" | "gtexture" | "tex" => Source::Atlas, "lightmap" => Source::Lightmap, @@ -392,6 +403,34 @@ fn texture_entry(binding: u32, sample_type: wgpu::TextureSampleType) -> wgpu::Bi ) } +/// `blend.`'s factors as wgpu's, adding both terms. +fn blend_state(b: horizon_shaders::Blend) -> wgpu::BlendState { + use horizon_shaders::Factor as F; + use wgpu::BlendFactor as W; + let f = |f| match f { + F::Zero => W::Zero, + F::One => W::One, + F::SrcColor => W::Src, + F::OneMinusSrcColor => W::OneMinusSrc, + F::DstColor => W::Dst, + F::OneMinusDstColor => W::OneMinusDst, + F::SrcAlpha => W::SrcAlpha, + F::OneMinusSrcAlpha => W::OneMinusSrcAlpha, + F::DstAlpha => W::DstAlpha, + F::OneMinusDstAlpha => W::OneMinusDstAlpha, + F::SrcAlphaSaturate => W::SrcAlphaSaturated, + }; + let component = |src, dst| wgpu::BlendComponent { + src_factor: f(src), + dst_factor: f(dst), + operation: wgpu::BlendOperation::Add, + }; + wgpu::BlendState { + color: component(b[0], b[1]), + alpha: component(b[2], b[3]), + } +} + fn target(format: wgpu::TextureFormat) -> Option { Some(wgpu::ColorTargetState { format, @@ -462,6 +501,8 @@ fn build_pass( let blank = match src { Source::Atlas => geometry.is_none(), Source::Depth => geometry.is_some(), + // Translucent terrain reads the copy; opaque terrain would read itself. + Source::Depth1 => p.group == Group::Gbuffers, Source::Color(n) => in_place & 1 << n != 0, _ => false, }; @@ -470,7 +511,7 @@ fn build_pass( Source::Color(n) => formats[n] .sample_type(None, Some(features)) .unwrap_or(float), - Source::Depth => wgpu::TextureSampleType::Float { filterable: false }, + Source::Depth | Source::Depth1 => wgpu::TextureSampleType::Float { filterable: false }, // Unfilterable on WebGPU-level devices; the lightmap is sampled at texel centres. Source::Lightmap => float, _ => float, @@ -534,7 +575,17 @@ fn build_pass( targets[0] = target(screen); } } else if let Some(&buffer) = p.buffers.get(l) { - targets[l] = target(formats[buffer as usize]); + let format = formats[buffer as usize]; + let mut state = target(format); + if let (Some(t), Some(b)) = (&mut state, p.blend) + && format + .guaranteed_format_features(features) + .flags + .contains(wgpu::TextureFormatFeatureFlags::BLENDABLE) + { + t.blend = Some(blend_state(b)); + } + targets[l] = state; writes.push((location, buffer as usize)); } } @@ -693,6 +744,10 @@ impl PackPost { .terrain .zip(world.terrain.as_ref()) .and_then(|(p, g)| build(p, Some(g))); + let water = post + .water + .zip(world.terrain.as_ref()) + .and_then(|(p, g)| build(p, Some(g))); // Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out. let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default(); let passes: Vec = post @@ -704,25 +759,43 @@ impl PackPost { if let Some(t) = &terrain { notes.push(format!("terrain draws with {}", t.name)); } + if let Some(w) = &water { + notes.push(format!("translucent terrain draws with {}", w.name)); + } + let depth1 = water.is_some() + && passes + .iter() + .filter(|p| matches!(p.group, Group::Composite | Group::Final)) + .chain(&water) + .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth1)); // A clear only matters when a program reads the buffer before one writes all of it // (composite passes cover their whole target, terrain does not); the others are skipped. let mut cleared = Vec::new(); for b in 1..BUFFERS { let bit = 1u16 << b; let touched = |p: &&Pass| (p.reads | p.flip | p.written) & bit != 0; - let first = terrain.iter().find(touched).map(|_| true).or_else(|| { - let p = passes.iter().find(touched)?; - Some(p.reads & bit != 0 || p.written & bit == 0) - }); + // Gbuffers passes cover part of their targets: a clear is needed if they touch it. + let first = terrain + .iter() + .chain(&water) + .find(touched) + .map(|_| true) + .or_else(|| { + let p = passes.iter().find(touched)?; + Some(p.reads & bit != 0 || p.written & bit == 0) + }); if clears[b].is_some() && first == Some(true) { cleared.push(b); } } // Terrain tests against the world's depth (entities...), so the blit copies it then. let blit_depth = terrain.is_some() - || passes - .iter() - .any(|p| p.samplers.iter().any(|s| s.source == Source::Depth)); + || water.is_some() + || passes.iter().any(|p| { + p.samplers + .iter() + .any(|s| matches!(s.source, Source::Depth | Source::Depth1)) + }); let room = 8 - 1; let other_clears = cleared.split_off(room.min(cleared.len())); let float = wgpu::TextureSampleType::Float { filterable: true }; @@ -778,6 +851,8 @@ impl PackPost { notes, passes, terrain, + water, + depth1, custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), lightmap: world.lightmap, @@ -886,6 +961,18 @@ impl PackPost { .as_ref() .map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]), Source::Depth => &targets.depth, + // Before translucent terrain the two are the same. + Source::Depth1 => match &targets.depth1 { + Some(d) + if matches!( + pass.group, + Group::Composite | Group::Final | Group::Translucent + ) => + { + &d.1 + } + _ => &targets.depth, + }, Source::Noise => &self.noise, Source::Dummy => &self.dummy, Source::Shadow => &self.shadow.0, @@ -919,19 +1006,36 @@ impl PackPost { group.clone() } - /// Terrain with the pack's program into the buffers it writes, in place, tested against - /// the world's depth; `draw` issues the game's draws. + /// Terrain (or, `translucent`, translucent terrain) with the pack's program into the + /// buffers it writes, in place, tested against the world's depth; `draw` issues the + /// game's draws. fn draw_terrain( &mut self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, size: [u32; 2], - draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), + translucent: bool, + draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { - let Some(mut pass) = self.terrain.take() else { + let slot = if translucent { + &mut self.water + } else { + &mut self.terrain + }; + let Some(mut pass) = slot.take() else { return; }; + if translucent + && let Some(t) = &self.targets + && let Some((copy, _)) = &t.depth1 + { + encoder.copy_texture_to_texture( + t.depth_texture.as_image_copy(), + copy.as_image_copy(), + t.depth_texture.size(), + ); + } let group = self.bind(device, queue, &mut pass, size); let targets = self.targets.as_ref().expect("resized"); let load = wgpu::Operations { @@ -964,9 +1068,13 @@ impl PackPost { }); rp.set_pipeline(&pass.pipeline); rp.set_bind_group(0, &group, &[]); - draw(&mut rp); + draw(&mut rp, translucent); + } + if translucent { + self.water = Some(pass); + } else { + self.terrain = Some(pass); } - self.terrain = Some(pass); } /// Whether the pack draws terrain itself. @@ -974,6 +1082,11 @@ impl PackPost { self.terrain.is_some() } + /// Whether the pack draws translucent terrain itself. + pub fn has_water(&self) -> bool { + self.water.is_some() + } + /// The names of the programs that run, in order. pub fn programs(&self) -> Vec<&str> { self.passes.iter().map(|p| p.name.as_str()).collect() @@ -983,23 +1096,24 @@ impl PackPost { if self.targets.as_ref().is_some_and(|t| t.size == size) { return; } + let texture = |label: &str, format, usage| { + device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { + width: size[0], + height: size[1], + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: usage | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }) + }; let make = |label: &str, format| { - device - .create_texture(&wgpu::TextureDescriptor { - label: Some(label), - size: wgpu::Extent3d { - width: size[0], - height: size[1], - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT - | wgpu::TextureUsages::TEXTURE_BINDING, - view_formats: &[], - }) + texture(label, format, wgpu::TextureUsages::RENDER_ATTACHMENT) .create_view(&Default::default()) }; let color = std::array::from_fn(|i| { @@ -1008,12 +1122,26 @@ impl PackPost { [make(&label, self.formats[i]), make(&label, self.formats[i])] }) }); + let depth32 = wgpu::TextureFormat::Depth32Float; + let depth_texture = texture( + "depthtex0", + depth32, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, + ); + let depth1 = self.depth1.then(|| { + let t = texture("depthtex1", depth32, wgpu::TextureUsages::COPY_DST); + let view = t.create_view(&Default::default()); + (t, view) + }); self.targets = Some(Targets { size, color, - depth: make("depthtex0", wgpu::TextureFormat::Depth32Float), + depth: depth_texture.create_view(&Default::default()), + depth_texture, + depth1, }); - for pass in self.passes.iter_mut().chain(&mut self.terrain) { + let gbuffers = self.terrain.iter_mut().chain(&mut self.water); + for pass in self.passes.iter_mut().chain(gbuffers) { pass.groups.clear(); } } @@ -1033,7 +1161,7 @@ impl PackPost { inputs: &PackInputs, size: [u32; 2], window: [u32; 2], - terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>), + terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { self.resize(device, size); self.uniforms.update(frame, inputs, size); @@ -1143,10 +1271,15 @@ impl PackPost { let mut seen = (false, false); let mut shown = false; let mut terrain_drawn = self.terrain.is_none(); + let mut water_drawn = self.water.is_none(); for pass in &mut passes { if !terrain_drawn && pass.group != Group::Prepare { terrain_drawn = true; - self.draw_terrain(device, queue, encoder, size, terrain); + self.draw_terrain(device, queue, encoder, size, false, terrain); + } + if !water_drawn && matches!(pass.group, Group::Composite | Group::Final) { + water_drawn = true; + self.draw_terrain(device, queue, encoder, size, true, terrain); } if pass.group == Group::Deferred && !seen.0 { seen.0 = true; @@ -1191,7 +1324,10 @@ impl PackPost { } self.passes = passes; if !terrain_drawn { - self.draw_terrain(device, queue, encoder, size, terrain); + self.draw_terrain(device, queue, encoder, size, false, terrain); + } + if !water_drawn { + self.draw_terrain(device, queue, encoder, size, true, terrain); } let targets = self.targets.as_ref().expect("resized"); let view = |m: u16, i: usize, alt: bool| { @@ -1282,6 +1418,7 @@ mod tests { fn sampler_names() { assert!(source("gaux1") == Source::Color(4)); assert!(source("gdepthtex") == Source::Depth); + assert!(source("depthtex2") == Source::Depth1); assert!(source("noisetex") == Source::Noise); assert!(source("shadowcolor0") == Source::Dummy); } diff --git a/crates/horizon-shaders/src/lib.rs b/crates/horizon-shaders/src/lib.rs index 47cbcca11..f5819d435 100644 --- a/crates/horizon-shaders/src/lib.rs +++ b/crates/horizon-shaders/src/lib.rs @@ -33,7 +33,9 @@ pub use lang::Lang; pub use macros::{MC_VERSION, Macros, RENDER_STAGES}; pub use options::switches_on; pub use pack::{Pack, PackError}; -pub use post::{Group, Post, PostProgram, dimension_folder, load_post}; +pub use post::{ + Blend, Factor, Group, Post, PostProgram, TRANSLUCENT_BLEND, dimension_folder, load_post, +}; pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess}; pub use properties::{Entry, Note, Properties}; pub use translate::{ diff --git a/crates/horizon-shaders/src/post.rs b/crates/horizon-shaders/src/post.rs index d6142c70a..e50eb4797 100644 --- a/crates/horizon-shaders/src/post.rs +++ b/crates/horizon-shaders/src/post.rs @@ -24,10 +24,67 @@ pub enum Group { Deferred, Composite, Final, - /// A gbuffers program: the game draws its geometry with it. + /// A gbuffers program: the game draws its geometry with it, before deferred. Gbuffers, + /// A gbuffers program drawing translucent geometry, after deferred (2.1). + Translucent, } +/// A blend factor of `blend.` (3.2), as GL names it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Factor { + Zero, + One, + SrcColor, + OneMinusSrcColor, + DstColor, + OneMinusDstColor, + SrcAlpha, + OneMinusSrcAlpha, + DstAlpha, + OneMinusDstAlpha, + SrcAlphaSaturate, +} + +impl Factor { + fn parse(word: &str) -> Option { + Some(match word { + "ZERO" => Self::Zero, + "ONE" => Self::One, + "SRC_COLOR" => Self::SrcColor, + "ONE_MINUS_SRC_COLOR" => Self::OneMinusSrcColor, + "DST_COLOR" => Self::DstColor, + "ONE_MINUS_DST_COLOR" => Self::OneMinusDstColor, + "SRC_ALPHA" => Self::SrcAlpha, + "ONE_MINUS_SRC_ALPHA" => Self::OneMinusSrcAlpha, + "DST_ALPHA" => Self::DstAlpha, + "ONE_MINUS_DST_ALPHA" => Self::OneMinusDstAlpha, + "SRC_ALPHA_SATURATE" => Self::SrcAlphaSaturate, + _ => return None, + }) + } +} + +/// Source and destination factors for colour, then for alpha. +pub type Blend = [Factor; 4]; + +/// The default blend of translucent geometry (3.2). +pub const TRANSLUCENT_BLEND: Blend = [ + Factor::SrcAlpha, + Factor::OneMinusSrcAlpha, + Factor::One, + Factor::OneMinusSrcAlpha, +]; + +/// `gbuffers_water` and its fallbacks (2.3). +const WATER: [&str; 5] = [ + "gbuffers_water", + "gbuffers_terrain", + "gbuffers_textured_lit", + "gbuffers_textured", + "gbuffers_basic", +]; + /// `gbuffers_terrain` and its fallbacks (2.3). const TERRAIN: [&str; 4] = [ "gbuffers_terrain", @@ -52,6 +109,8 @@ pub struct PostProgram { pub buffers: Vec, /// `flip..`: buffers forced to flip (true) or kept (false) after this program. pub flips: Vec<(u32, bool)>, + /// How a gbuffers program's output blends into its buffers; `None` writes them. + pub blend: Option, } pub struct Post { @@ -63,6 +122,8 @@ pub struct Post { pub composite_pre: Vec<(u32, bool)>, /// What draws solid and cutout terrain, when the game asked for it and one loads. pub terrain: Option, + /// What draws translucent terrain (water, stained glass) after deferred. + pub water: Option, /// The custom uniforms of `shaders.properties` (3.4). pub custom: Custom, pub config: Config, @@ -96,6 +157,7 @@ pub fn load_post( deferred_pre: flips(properties, "deferred_pre"), composite_pre: flips(properties, "composite_pre"), terrain: None, + water: None, custom: Custom::parse(properties), config: Config::default(), notes: Vec::new(), @@ -127,11 +189,24 @@ pub fn load_post( } } if let Some(inputs) = terrain { - loader.post.terrain = TERRAIN.iter().find_map(|name| { - loader.load(name.to_string(), Group::Gbuffers, |v, f| { - translate_gbuffers(v, f, Target::Buffers, inputs) + let mut gbuffers = |names: &[&str], group| { + names.iter().find_map(|name| { + loader.load(name.to_string(), group, |v, f| { + translate_gbuffers(v, f, Target::Buffers, inputs) + }) }) + }; + let terrain = gbuffers(&TERRAIN, Group::Gbuffers); + // gbuffers_water falls back to gbuffers_terrain's chain (2.3). + let water = gbuffers(&WATER, Group::Translucent); + loader.post.terrain = terrain; + loader.post.water = water.map(|mut p| { + p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND)); + p }); + if let Some(p) = &mut loader.post.terrain { + p.blend = blend(properties, &p.name).flatten(); + } } post.config = std::mem::take(&mut post.config).finish(); post @@ -211,6 +286,7 @@ impl Loader<'_> { group, program, buffers, + blend: None, }) } } @@ -260,6 +336,20 @@ fn enabled( eval_condition(&condition, macros).ok() != Some(0) } +/// `blend.`: `Some(None)` for `off`, `Some(factors)`, or `None` when absent or not +/// four known factors. +fn blend(properties: &Properties, program: &str) -> Option> { + let value = properties.get(&format!("blend.{program}"))?.trim(); + if value == "off" { + return Some(None); + } + let factors: Vec = value + .split_whitespace() + .map(Factor::parse) + .collect::>()?; + Some(Some(factors.try_into().ok()?)) +} + /// `flip..=` keys of one program. fn flips(properties: &Properties, program: &str) -> Vec<(u32, bool)> { let prefix = format!("flip.{program}."); @@ -355,6 +445,38 @@ mod tests { assert!(post.programs.iter().any(|p| p.name == "composite3")); } + /// Water falls back to the terrain chain and blends by default; `blend.*` overrides. + #[test] + fn water_falls_back_to_terrain_and_blends() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/terrain-test"); + let pack = Pack::open(&dir).unwrap(); + let macros = Macros::standard(); + let inputs = crate::GbuffersInputs { + declarations: "layout(location = 0) in vec3 hz_v_pos;".into(), + body: " hz_Vertex = vec4(hz_v_pos, 1.0);\n hz_Color = vec4(1.0);\n".into(), + attributes: Vec::new(), + }; + let post = load_post(&pack, "", &Properties::default(), ¯os, Some(&inputs)); + let (terrain, water) = (post.terrain.unwrap(), post.water.unwrap()); + assert_eq!((terrain.group, terrain.blend), (Group::Gbuffers, None)); + assert_eq!(water.name, "gbuffers_terrain"); + assert_eq!( + (water.group, water.blend), + (Group::Translucent, Some(TRANSLUCENT_BLEND)) + ); + let properties = Properties::parse( + "blend.gbuffers_terrain = ONE ONE ZERO ONE\nblend.gbuffers_water = off\n", + ¯os, + ); + let terrain_blend = blend(&properties, "gbuffers_terrain"); + assert_eq!( + terrain_blend, + Some(Some([Factor::One, Factor::One, Factor::Zero, Factor::One])) + ); + assert_eq!(blend(&properties, "gbuffers_water"), Some(None)); + assert_eq!(blend(&properties, "gbuffers_basic"), None); + } + #[test] fn horizons_test_pack_loads() { let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/packs/post-test"); diff --git a/crates/horizon-shaders/tests/real_packs.rs b/crates/horizon-shaders/tests/real_packs.rs index 78ba97bfb..c5d4defac 100644 --- a/crates/horizon-shaders/tests/real_packs.rs +++ b/crates/horizon-shaders/tests/real_packs.rs @@ -176,9 +176,10 @@ fn real_packs_read() { Some(&terrain), ); println!( - " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, {} notes, {:.0} ms", + " dimension {id} (folder `{folder}`): {} post programs, terrain {:?}, water {:?}, {} notes, {:.0} ms", post.programs.len(), post.terrain.as_ref().map(|t| &t.name), + post.water.as_ref().map(|t| &t.name), post.notes.len() + post.config.notes.len(), start.elapsed().as_secs_f64() * 1000.0 ); From b6345fc61d640de07afc9a2f3695039e02ac3201 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:56:31 +0100 Subject: [PATCH 08/96] Shader packs, clean room 19: GPU time of each pass (developers' aid) HORIZON_SHADER_PACK_TIMING=1 puts timestamps around every pass of a shader pack, one frame in 30, and logs each pass's median every 30 timed frames. The frame after a timed one waits for the GPU before reading them (Metal resolves timestamps without waiting for the passes that write them). Passes overlap on tiled GPUs, so a pass is charged from the later of its beginning and the previous pass's end. This is what attributes a pack's cost to its own programs or to ours when the machine is shared and FPS comparisons are noisy. Clean-room: Horizon's own tooling, no spec section; no other loader's code read. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 1 + crates/horizon-client/src/gfx/pack_post.rs | 11 ++ crates/horizon-client/src/gfx/pack_timing.rs | 147 +++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 crates/horizon-client/src/gfx/pack_timing.rs diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 9a7356d6e..f0987dddd 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -70,6 +70,7 @@ mod lod; mod occlusion; mod pack_atlas; mod pack_post; +mod pack_timing; pub mod pack_uniforms; mod page_runs; pub mod panorama; diff --git a/crates/horizon-client/src/gfx/pack_post.rs b/crates/horizon-client/src/gfx/pack_post.rs index b96da2599..739688784 100644 --- a/crates/horizon-client/src/gfx/pack_post.rs +++ b/crates/horizon-client/src/gfx/pack_post.rs @@ -168,6 +168,8 @@ pub struct PackPost { water: Option, /// A program after translucent terrain reads `depthtex1`: it is copied before water. depth1: bool, + /// `HORIZON_SHADER_PACK_TIMING`: GPU time of each pass, in the log. + timing: Option, /// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`. custom: horizon_shaders::Custom, atlas: Option, @@ -853,6 +855,7 @@ impl PackPost { terrain, water, depth1, + timing: super::pack_timing::PassTiming::new(device, queue), custom, atlas: world.atlas.as_ref().map(|a| a.0.clone()), lightmap: world.lightmap, @@ -1053,9 +1056,11 @@ impl PackPost { .and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)); } { + let timestamp_writes = self.timing.as_mut().and_then(|t| t.writes(&pass.name)); let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some(&pass.name), color_attachments: &attachments, + timestamp_writes, depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { view: &targets.depth, depth_ops: Some(wgpu::Operations { @@ -1164,6 +1169,9 @@ impl PackPost { terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>, bool), ) { self.resize(device, size); + if let Some(t) = &mut self.timing { + t.begin(device, queue); + } self.uniforms.update(frame, inputs, size); self.uniforms.eval_custom(&mut self.custom); let Some(targets) = &self.targets else { return }; @@ -1214,6 +1222,7 @@ impl PackPost { encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Shader pack clear"), color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes("clear")), ..Default::default() }); } @@ -1250,6 +1259,7 @@ impl PackPost { let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Shader pack world"), color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes("world")), depth_stencil_attachment: self.blit_depth.then_some( wgpu::RenderPassDepthStencilAttachment { view: &targets.depth, @@ -1314,6 +1324,7 @@ impl PackPost { let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some(&pass.name), color_attachments: &attachments, + timestamp_writes: self.timing.as_mut().and_then(|t| t.writes(&pass.name)), ..Default::default() }); rp.set_pipeline(&pass.pipeline); diff --git a/crates/horizon-client/src/gfx/pack_timing.rs b/crates/horizon-client/src/gfx/pack_timing.rs new file mode 100644 index 000000000..61ab721e5 --- /dev/null +++ b/crates/horizon-client/src/gfx/pack_timing.rs @@ -0,0 +1,147 @@ +//! Developers' aid: `HORIZON_SHADER_PACK_TIMING=1` times every pass of a shader pack on the +//! GPU, one frame in `EVERY`, and logs each pass's median every `REPORT` timed frames. The +//! frame after a timed one waits for the GPU before reading the timestamps (Metal resolves +//! them without waiting for the passes that write them), so timing slows the game. + +const EVERY: u32 = 30; +const REPORT: usize = 30; +/// Passes timed per frame at most (two timestamps each). +const CAPACITY: u32 = 64; + +pub(super) struct PassTiming { + queries: wgpu::QuerySet, + resolve: wgpu::Buffer, + read: wgpu::Buffer, + period_ns: f32, + frame: u32, + /// The passes timed in the frame being recorded or waiting to be read, in order. + names: Vec, + timed: bool, + samples: Vec<(String, Vec)>, + reports: usize, +} + +impl PassTiming { + pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option { + if std::env::var_os("HORIZON_SHADER_PACK_TIMING").is_none() + || !device.features().contains(wgpu::Features::TIMESTAMP_QUERY) + { + return None; + } + let size = u64::from(CAPACITY) * 2 * 8; + let buffer = |label, usage| { + device.create_buffer(&wgpu::BufferDescriptor { + label: Some(label), + size, + usage, + mapped_at_creation: false, + }) + }; + Some(Self { + queries: device.create_query_set(&wgpu::QuerySetDescriptor { + label: Some("Shader pack pass timing"), + ty: wgpu::QueryType::Timestamp, + count: CAPACITY * 2, + }), + resolve: buffer( + "Shader pack timing resolve", + wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC, + ), + read: buffer( + "Shader pack timing read", + wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + ), + period_ns: queue.get_timestamp_period(), + frame: 0, + names: Vec::new(), + timed: false, + samples: Vec::new(), + reports: 0, + }) + } + + /// At the start of a frame: reads the last timed frame's passes, and decides whether this + /// one is timed. + pub fn begin(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) { + if self.timed && !self.names.is_empty() { + self.collect(device, queue); + } + self.names.clear(); + self.frame += 1; + self.timed = self.frame.is_multiple_of(EVERY); + } + + /// Timestamp writes for the pass `name`, in a timed frame. + pub fn writes(&mut self, name: &str) -> Option> { + let i = self.names.len() as u32; + if !self.timed || i >= CAPACITY { + return None; + } + self.names.push(name.to_owned()); + Some(wgpu::RenderPassTimestampWrites { + query_set: &self.queries, + beginning_of_pass_write_index: Some(2 * i), + end_of_pass_write_index: Some(2 * i + 1), + }) + } + + fn collect(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) { + // The timed frame first, then the resolve, so every timestamp is written. + _ = device.poll(wgpu::PollType::wait_indefinitely()); + let count = self.names.len() as u32 * 2; + let bytes = u64::from(count) * 8; + let mut encoder = device.create_command_encoder(&Default::default()); + encoder.resolve_query_set(&self.queries, 0..count, &self.resolve, 0); + encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.read, 0, bytes); + queue.submit([encoder.finish()]); + let slice = self.read.slice(..bytes); + slice.map_async(wgpu::MapMode::Read, |_| {}); + _ = device.poll(wgpu::PollType::wait_indefinitely()); + let Ok(data) = slice.get_mapped_range() else { + return; + }; + let stamps: Vec = data + .as_chunks::<8>() + .0 + .iter() + .map(|b| u64::from_le_bytes(*b)) + .collect(); + drop(data); + self.read.unmap(); + // Passes overlap on tiled GPUs (a pass begins before the one before it ends): a pass + // costs the time from the later of its beginning and the previous pass's end. + let mut previous_end = 0; + for (name, pair) in self.names.iter().zip(stamps.as_chunks::<2>().0) { + if pair[0] == 0 || pair[1] < pair[0] { + continue; + } + let from = pair[0].max(previous_end).min(pair[1]); + previous_end = pair[1]; + let ms = (pair[1] - from) as f32 * self.period_ns / 1e6; + match self.samples.iter_mut().find(|s| s.0 == *name) { + Some(s) => s.1.push(ms), + None => self.samples.push((name.clone(), vec![ms])), + } + } + self.reports += 1; + if self.reports.is_multiple_of(REPORT) { + self.report(); + } + } + + fn report(&mut self) { + let mut total = 0.0; + let mut parts = Vec::new(); + for (name, v) in &mut self.samples { + v.sort_by(f32::total_cmp); + let median = v[v.len() / 2]; + total += median; + parts.push(format!("{name} {median:.3}")); + v.clear(); + } + log::info!( + "shader pack GPU time per pass (median ms of {REPORT} frames): {} = {total:.3}", + parts.join(", ") + ); + } +} From 1d90d72a64418071f943e404166fa39b09f50981 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 25 Oct 2026 02:14:59 +0100 Subject: [PATCH 09/96] Server dialogs: the packets and the dialog model show_dialog and clear_dialog in configuration and play, and custom_click_action, the answer, in both. Dialog.DIRECT_CODEC as horizon_client::dialog: the five dialog types, their bodies, inputs and actions with vanilla's defaults, the values inputs hold, and Action.createAction (a command template's $(key) substitution with escapeWithoutQuotes, a custom action's payload of every input's tag). Nothing uses them yet: the screen comes next. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/dialog.rs | 794 ++++++++++++++++++ crates/horizon-client/src/lib.rs | 1 + .../src/packets/configuration.rs | 52 ++ crates/horizon-protocol/src/packets/play.rs | 60 +- 4 files changed, 905 insertions(+), 2 deletions(-) create mode 100644 crates/horizon-client/src/dialog.rs diff --git a/crates/horizon-client/src/dialog.rs b/crates/horizon-client/src/dialog.rs new file mode 100644 index 000000000..7acd3e86a --- /dev/null +++ b/crates/horizon-client/src/dialog.rs @@ -0,0 +1,794 @@ +//! Server dialogs (`net.minecraft.server.dialog`): what `Dialog.DIRECT_CODEC` +//! decodes from a `show_dialog` packet, the values its inputs hold, and the +//! click event an action makes of them (`Action.createAction`). +use crate::text::Text; +use horizon_world::nbt::Tag; +use serde_json::{Value, json}; + +/// `CommonDialogData` and the type's own fields. +#[derive(Clone, Debug)] +pub struct Dialog { + pub kind: Kind, + pub title: Text, + pub external_title: Option, + pub can_close_with_escape: bool, + pub pause: bool, + pub after_action: AfterAction, + pub body: Vec, + pub inputs: Vec, +} + +/// The dialog types (`DialogTypes`). +#[derive(Clone, Debug)] +pub enum Kind { + /// `NoticeDialog`: one button, "OK" when the server names none. + Notice(Button), + Confirmation { + yes: Button, + no: Button, + }, + MultiAction { + actions: Vec