From 9845d5717b787d7136bdcb00786b582f7004c887 Mon Sep 17 00:00:00 2001 From: zack Date: Sun, 29 Dec 2024 20:00:14 -0500 Subject: [PATCH] wip 2 --- Cargo.lock | 578 +++++++++++++++++++++++++++++- Cargo.toml | 1 + crates/shaders-shared/Cargo.toml | 3 +- crates/shaders-shared/src/lib.rs | 11 +- crates/shaders/Cargo.toml | 1 - crates/shaders/src/lib.rs | 56 +-- crates/vk-rs/Cargo.toml | 5 +- crates/vk-rs/shaders/main_fs.spv | Bin 2312 -> 576 bytes crates/vk-rs/shaders/main_vs.spv | Bin 6364 -> 6512 bytes crates/vk-rs/src/main.rs | 26 +- crates/vk-rs/src/renderer.rs | 324 +++++++++++------ crates/vk-rs/src/texture_cache.rs | 46 +++ 12 files changed, 903 insertions(+), 148 deletions(-) create mode 100644 crates/vk-rs/src/texture_cache.rs diff --git a/Cargo.lock b/Cargo.lock index f44eb23..1ffe959 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,6 +61,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "aligned-vec" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4aa90d7ce82d4be67b64039a3d588d38dbcc6736577de4a847025ce5b0c468d1" + [[package]] name = "allocator-api2" version = "0.2.21" @@ -136,6 +142,12 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d67af77d68a931ecd5cbd8a3b5987d63a1d1d1278f7f6a60ae33db485cdebb69" +[[package]] +name = "arbitrary" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dde20b3d026af13f561bdd0f15edf01fc734f0dafcedbaf42bba506a9517f223" + [[package]] name = "arboard" version = "3.4.1" @@ -151,6 +163,17 @@ dependencies = [ "x11rb", ] +[[package]] +name = "arg_enum_proc_macro" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ae92a5119aa49cdbcf6b9f893fe4e1d98b04ccbf82ee0584ad948a44a734dea" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "arrayref" version = "0.3.9" @@ -201,6 +224,29 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "av1-grain" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6678909d8c5d46a42abcf571271e15fdbc0a225e3646cf23762cd415046c78bf" +dependencies = [ + "anyhow", + "arrayvec", + "log", + "nom", + "num-rational", + "v_frame", +] + +[[package]] +name = "avif-serialize" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e335041290c43101ca215eed6f43ec437eb5a42125573f600fc3fa42b9bddd62" +dependencies = [ + "arrayvec", +] + [[package]] name = "backtrace" version = "0.3.71" @@ -231,6 +277,12 @@ dependencies = [ "serde", ] +[[package]] +name = "bit_field" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" + [[package]] name = "bitflags" version = "1.3.2" @@ -243,6 +295,12 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bitstream-io" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6099cdc01846bc367c4e7dd630dc5966dccf36b652fae7a74e17b640411a91b2" + [[package]] name = "block" version = "0.1.6" @@ -277,6 +335,12 @@ dependencies = [ "objc2 0.5.2", ] +[[package]] +name = "built" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c360505aed52b7ec96a3636c3f039d99103c37d1d9b4f7a8c743d3ea9ffcd03b" + [[package]] name = "bumpalo" version = "3.16.0" @@ -390,6 +454,16 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" +[[package]] +name = "cfg-expr" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d067ad48b8650848b989a59a86c6c36a995d02d2bf778d45c3c5d57bc2718f02" +dependencies = [ + "smallvec", + "target-lexicon", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -484,6 +558,12 @@ dependencies = [ "tracing-error", ] +[[package]] +name = "color_quant" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" + [[package]] name = "combine" version = "4.6.7" @@ -558,12 +638,37 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-utils" version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "cursor-icon" version = "1.1.0" @@ -747,6 +852,21 @@ version = "3.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a5d9305ccc6942a704f4335694ecd3de2ea531b114ac2d51f5f843750787a92f" +[[package]] +name = "exr" +version = "1.73.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83197f59927b46c04a183a619b7c29df34e63e63c7869320862268c0ef687e0" +dependencies = [ + "bit_field", + "half", + "lebe", + "miniz_oxide 0.8.2", + "rayon-core", + "smallvec", + "zune-inflate", +] + [[package]] name = "eyre" version = "0.6.12" @@ -845,6 +965,16 @@ dependencies = [ "wasi", ] +[[package]] +name = "gif" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb2d69b19215e18bb912fa30f7ce15846e301408695e44e0ef719f1da9e19f2" +dependencies = [ + "color_quant", + "weezl", +] + [[package]] name = "gimli" version = "0.28.1" @@ -862,15 +992,6 @@ dependencies = [ "stable_deref_trait", ] -[[package]] -name = "glam" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12f597d56c1bd55a811a1be189459e8fad2bbc272616375602443bdfb37fa774" -dependencies = [ - "num-traits", -] - [[package]] name = "glam" version = "0.29.2" @@ -942,6 +1063,16 @@ dependencies = [ "puffin", ] +[[package]] +name = "half" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" +dependencies = [ + "cfg-if", + "crunchy", +] + [[package]] name = "hashbrown" version = "0.14.5" @@ -961,6 +1092,12 @@ dependencies = [ "foldhash", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.4.0" @@ -1134,12 +1271,37 @@ checksum = "cd6f44aed642f18953a158afeb30206f4d50da59fbc66ecb53c66488de73563b" dependencies = [ "bytemuck", "byteorder-lite", + "color_quant", + "exr", + "gif", + "image-webp", "num-traits", "png", + "qoi", + "ravif", + "rayon", + "rgb", + "tiff", "zune-core", "zune-jpeg", ] +[[package]] +name = "image-webp" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e031e8e3d94711a9ccb5d6ea357439ef3dcbed361798bd4071dc4d9793fbe22f" +dependencies = [ + "byteorder-lite", + "quick-error", +] + +[[package]] +name = "imgref" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0263a3d970d5c054ed9312c0057b4f3bde9c0b33836d3637361d4a9e6e7a408" + [[package]] name = "indenter" version = "0.3.3" @@ -1169,6 +1331,17 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "969ee3fc68ec2e88eb21434ce4d9b7e1600d1ce92ff974560a6c4a304f5124b9" +[[package]] +name = "interpolate_name" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34819042dc3d3971c46c2190835914dfbe0c3c13f61449b2997f4e9722dfa60" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "itertools" version = "0.10.5" @@ -1178,6 +1351,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.14" @@ -1215,6 +1397,12 @@ dependencies = [ "libc", ] +[[package]] +name = "jpeg-decoder" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5d4a7da358eff58addd2877a45865158f0d78c911d43a5784ceb7bbf52833b0" + [[package]] name = "js-sys" version = "0.3.76" @@ -1231,12 +1419,28 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +[[package]] +name = "lebe" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8" + [[package]] name = "libc" version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +[[package]] +name = "libfuzzer-sys" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b9569d2f74e257076d8c6bfa73fb505b46b851e51ddaecc825944aa3bed17fa" +dependencies = [ + "arbitrary", + "cc", +] + [[package]] name = "libloading" version = "0.7.4" @@ -1308,6 +1512,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b3bd0dd2cd90571056fdb71f6275fada10131182f84899f4b2a916e565d81d86" +[[package]] +name = "loop9" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fae87c125b03c1d2c0150c90365d7d6bcc53fb73a9acaef207d2d065860f062" +dependencies = [ + "imgref", +] + [[package]] name = "lz4_flex" version = "0.11.3" @@ -1323,6 +1536,16 @@ dependencies = [ "libc", ] +[[package]] +name = "maybe-rayon" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea1f30cedd69f0a2954655f7188c6a834246d2bcf1e315e2ac40c4b24dc9519" +dependencies = [ + "cfg-if", + "rayon", +] + [[package]] name = "memchr" version = "2.7.4" @@ -1338,6 +1561,12 @@ dependencies = [ "libc", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.7.4" @@ -1418,18 +1647,81 @@ dependencies = [ "jni-sys", ] +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + [[package]] name = "nohash-hasher" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "noop_proc_macro" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-conv" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num-derive" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -1766,6 +2058,12 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "percent-encoding" version = "2.3.1" @@ -1838,6 +2136,15 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + [[package]] name = "presser" version = "0.3.1" @@ -1862,6 +2169,25 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "profiling" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afbdc74edc00b6f6a218ca6a5364d6226a259d4b8ea1af4a0ea063f27e179f4d" +dependencies = [ + "profiling-procmacros", +] + +[[package]] +name = "profiling-procmacros" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a65f2e60fbf1063868558d69c6beacf412dc755f9fc020f514b7955fc914fe30" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "puffin" version = "0.19.0" @@ -1893,6 +2219,21 @@ dependencies = [ "web-time 0.2.4", ] +[[package]] +name = "qoi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6d64c71eb498fe9eae14ce4ec935c555749aef511cca85b5568910d6e48001" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "quick-error" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" + [[package]] name = "quick-xml" version = "0.36.2" @@ -1911,6 +2252,86 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rav1e" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd87ce80a7665b1cce111f8a16c1f3929f6547ce91ade6addf4ec86a8dda5ce9" +dependencies = [ + "arbitrary", + "arg_enum_proc_macro", + "arrayvec", + "av1-grain", + "bitstream-io", + "built", + "cfg-if", + "interpolate_name", + "itertools 0.12.1", + "libc", + "libfuzzer-sys", + "log", + "maybe-rayon", + "new_debug_unreachable", + "noop_proc_macro", + "num-derive", + "num-traits", + "once_cell", + "paste", + "profiling", + "rand", + "rand_chacha", + "simd_helpers", + "system-deps", + "thiserror", + "v_frame", + "wasm-bindgen", +] + +[[package]] +name = "ravif" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2413fd96bd0ea5cdeeb37eaf446a22e6ed7b981d792828721e74ded1980a45c6" +dependencies = [ + "avif-serialize", + "imgref", + "loop9", + "quick-error", + "rav1e", + "rayon", + "rgb", +] + [[package]] name = "raw-string" version = "0.3.5" @@ -1941,6 +2362,26 @@ dependencies = [ "raw-window-handle 0.5.2", ] +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redox_syscall" version = "0.3.5" @@ -1997,6 +2438,12 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "rgb" +version = "0.8.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57397d16646700483b67d2dd6511d79318f9d057fdbd21a4066aeac8b41d310a" + [[package]] name = "rspirv" version = "0.12.0+sdk-1.3.268.0" @@ -2038,7 +2485,7 @@ dependencies = [ "bytemuck", "either", "indexmap", - "itertools", + "itertools 0.10.5", "lazy_static", "libc", "log", @@ -2199,11 +2646,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + [[package]] name = "shaders" version = "0.1.0" dependencies = [ - "glam 0.22.0", "shaders-shared", "spirv-std", ] @@ -2212,7 +2667,8 @@ dependencies = [ name = "shaders-shared" version = "0.1.0" dependencies = [ - "glam 0.22.0", + "bytemuck", + "spirv-std", ] [[package]] @@ -2236,6 +2692,15 @@ version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" +[[package]] +name = "simd_helpers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95890f873bec569a0362c235787f3aca6e1e887302ba4840839bcc6459c42da6" +dependencies = [ + "quote", +] + [[package]] name = "slab" version = "0.4.9" @@ -2336,7 +2801,7 @@ dependencies = [ "elsa", "indexmap", "internal-iterator", - "itertools", + "itertools 0.10.5", "lazy_static", "longest-increasing-subsequence", "rustc-hash", @@ -2373,7 +2838,7 @@ version = "0.9.0" source = "git+https://github.com/Rust-GPU/rust-gpu.git#bfa63c15a921357b38aa78986670c15f36df76dc" dependencies = [ "bitflags 1.3.2", - "glam 0.29.2", + "glam", "num-traits", "spirv-std-macros", "spirv-std-types", @@ -2453,6 +2918,25 @@ dependencies = [ "syn", ] +[[package]] +name = "system-deps" +version = "6.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e535eb8dded36d55ec13eddacd30dec501792ff23a0b1682c38601b8cf2349" +dependencies = [ + "cfg-expr", + "heck", + "pkg-config", + "toml", + "version-compare", +] + +[[package]] +name = "target-lexicon" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" + [[package]] name = "thiserror" version = "1.0.69" @@ -2495,6 +2979,17 @@ dependencies = [ "once_cell", ] +[[package]] +name = "tiff" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba1310fcea54c6a9a4fd1aad794ecc02c31682f6bfbecdf460bf19533eed1e3e" +dependencies = [ + "flate2", + "jpeg-decoder", + "weezl", +] + [[package]] name = "time" version = "0.3.37" @@ -2570,11 +3065,26 @@ dependencies = [ "ahash", ] +[[package]] +name = "toml" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + [[package]] name = "toml_datetime" version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -2583,6 +3093,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ "indexmap", + "serde", + "serde_spanned", "toml_datetime", "winnow", ] @@ -2697,6 +3209,17 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "v_frame" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f32aaa24bacd11e488aa9ba66369c7cd514885742c9fe08cfe85884db3e92b" +dependencies = [ + "aligned-vec", + "num-traits", + "wasm-bindgen", +] + [[package]] name = "valuable" version = "0.1.0" @@ -2709,6 +3232,12 @@ version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eab68b56840f69efb0fefbe3ab6661499217ffdc58e2eef7c3f6f69835386322" +[[package]] +name = "version-compare" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b" + [[package]] name = "version_check" version = "0.9.5" @@ -2721,20 +3250,23 @@ version = "0.1.0" dependencies = [ "ash", "ash-window", + "bytemuck", "cfg-if", "cgmath", "color-eyre", "egui", "egui-ash", - "glam 0.22.0", "gltf", "gpu-allocator", "gpu-profiler", + "image", "puffin", "puffin_egui", "raw-window-handle 0.6.2", + "rayon", "shaders-shared", "spirv-builder", + "spirv-std", "tobj", "winit 0.30.7", ] @@ -3025,6 +3557,12 @@ dependencies = [ "web-sys", ] +[[package]] +name = "weezl" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" + [[package]] name = "winapi" version = "0.3.9" @@ -3498,6 +4036,7 @@ version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ + "byteorder", "zerocopy-derive", ] @@ -3561,6 +4100,15 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f423a2c17029964870cfaabb1f13dfab7d092a62a29a89264f4d36990ca414a" +[[package]] +name = "zune-inflate" +version = "0.2.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ab332fe2f6680068f3582b16a24f90ad7096d5d39b974d1c0aff0125116f02" +dependencies = [ + "simd-adler32", +] + [[package]] name = "zune-jpeg" version = "0.4.14" diff --git a/Cargo.toml b/Cargo.toml index bd6a824..a555e18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ winit = { version = "0.30.7", features = ["rwh_06"] } raw-window-handle = "0.6" gpu-allocator = { version = "0.25.0", features = ["vulkan"] } glam = { version = "0.22", default-features = false, features = ["libm"] } +bytemuck = "1.21.0" spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu.git" } spirv-std = { git = "https://github.com/Rust-GPU/rust-gpu.git" } diff --git a/crates/shaders-shared/Cargo.toml b/crates/shaders-shared/Cargo.toml index d18a001..8578fdd 100644 --- a/crates/shaders-shared/Cargo.toml +++ b/crates/shaders-shared/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] -glam.workspace = true +spirv-std.workspace = true +bytemuck.workspace = true diff --git a/crates/shaders-shared/src/lib.rs b/crates/shaders-shared/src/lib.rs index 338b322..6221973 100644 --- a/crates/shaders-shared/src/lib.rs +++ b/crates/shaders-shared/src/lib.rs @@ -1,6 +1,6 @@ #![cfg_attr(target_arch = "spirv", no_std)] -use glam::{Mat4, Vec3}; +use spirv_std::glam::{Mat4, Vec3, Vec4}; #[repr(C)] #[derive(Clone)] @@ -10,3 +10,12 @@ pub struct UniformBufferObject { pub proj: Mat4, pub model_color: Vec3, } + +#[repr(C)] +#[derive(Debug, Clone, Copy)] +pub struct PushConstants { + pub texture_size: Vec4, +} + +unsafe impl bytemuck::Pod for PushConstants {} +unsafe impl bytemuck::Zeroable for PushConstants {} diff --git a/crates/shaders/Cargo.toml b/crates/shaders/Cargo.toml index 635d6e0..69ea8cf 100644 --- a/crates/shaders/Cargo.toml +++ b/crates/shaders/Cargo.toml @@ -8,6 +8,5 @@ crate-type = ["dylib"] [dependencies] spirv-std.workspace = true -glam.workspace = true shaders-shared = { path = "../shaders-shared" } diff --git a/crates/shaders/src/lib.rs b/crates/shaders/src/lib.rs index 6974cbb..f314f90 100644 --- a/crates/shaders/src/lib.rs +++ b/crates/shaders/src/lib.rs @@ -1,15 +1,18 @@ #![cfg_attr(target_arch = "spirv", no_std)] use shaders_shared::UniformBufferObject; -use spirv_std::{glam::Vec2, image::Image2d, spirv, Image, Sampler}; - -use glam::{Mat3, Vec3, Vec4, Vec4Swizzles}; +use spirv_std::{ + glam::{Mat3, UVec2, Vec2, Vec3, Vec4, Vec4Swizzles}, + image::Image2d, + spirv, Sampler, +}; #[spirv(vertex)] pub fn main_vs( // Vertex inputs in_pos: Vec3, in_normal: Vec3, + in_tex_coord: Vec2, // Uniform buffer #[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UniformBufferObject, @@ -17,6 +20,7 @@ pub fn main_vs( // Vertex outputs out_world_position: &mut Vec3, out_world_normal: &mut Vec3, + out_tex_coord: &mut Vec2, #[spirv(position)] gl_position: &mut Vec4, ) { // Transform position to world space @@ -29,6 +33,11 @@ pub fn main_vs( // Calculate clip space position *gl_position = ubo.proj * ubo.view * pos; + *out_tex_coord = in_tex_coord; +} + +pub fn get_uv_u(pix: UVec2, tex_size: Vec4) -> spirv_std::glam::Vec2 { + (pix.as_vec2() + Vec2::splat(0.5)) * tex_size.zw() } #[spirv(fragment)] @@ -36,29 +45,30 @@ pub fn main_fs( frag_world_position: Vec3, frag_world_normal: Vec3, frag_tex_coord: Vec2, - #[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UniformBufferObject, #[spirv(descriptor_set = 0, binding = 1)] texture: &Image2d, #[spirv(descriptor_set = 0, binding = 2)] sampler: &Sampler, out_color: &mut Vec4, ) { + // Convert fragment coordinate to UV coordinate + // Sample the texture let base_color = texture.sample(*sampler, frag_tex_coord); - let light_pos = Vec3::new(2.0, 2.0, -2.0); - - // Calculate light direction - let l = (light_pos - frag_world_position).normalize(); - let n = frag_world_normal.normalize(); - - // Calculate lambertian lighting - let lambertian = f32::max(n.dot(l), 0.0); - - // Ambient lighting - let ambient = Vec3::splat(0.2); - - // Combine texture color with model color - let color_from_texture = Vec3::new(base_color.x, base_color.y, base_color.z); - let combined_color = color_from_texture * ubo.model_color; - - // Final color calculation with gamma correction - let color = (combined_color * lambertian + ambient).powf(2.2); - *out_color = Vec4::from((color, 1.0)); + // let light_pos = Vec3::new(2.0, 2.0, -2.0); + // + // // Calculate light direction + // let l = (light_pos - frag_world_position).normalize(); + // let n = frag_world_normal.normalize(); + // + // // Calculate lambertian lighting + // let lambertian = f32::max(n.dot(l), 0.0); + // + // // Ambient lighting + // let ambient = Vec3::splat(0.2); + // + // // Combine texture color with model color + // let color_from_texture = Vec3::new(base_color.x, base_color.y, base_color.z); + // let combined_color = color_from_texture; + // + // // Final color calculation with gamma correction + // let color = (combined_color * lambertian + ambient).powf(2.2); + *out_color = Vec4::new(base_color.x, base_color.y, base_color.z, base_color.w); } diff --git a/crates/vk-rs/Cargo.toml b/crates/vk-rs/Cargo.toml index c7119e3..24f4c06 100644 --- a/crates/vk-rs/Cargo.toml +++ b/crates/vk-rs/Cargo.toml @@ -10,7 +10,7 @@ winit.workspace = true raw-window-handle.workspace = true cfg-if = "1.0.0" cgmath = "0.18.0" -glam.workspace = true +spirv-std.workspace = true gpu-allocator.workspace = true egui-ash = { version = "0.4.0", features = ["gpu-allocator"] } tobj = "4.0.2" @@ -23,6 +23,9 @@ gpu-profiler = { git = "https://github.com/zackartz/gpu-profiler", features = [ "use-ash", ] } gltf = { version = "1.4.1", features = ["import"] } +image = "0.25.5" +rayon = "1.10.0" +bytemuck.workspace = true [build-dependencies] spirv-builder.workspace = true diff --git a/crates/vk-rs/shaders/main_fs.spv b/crates/vk-rs/shaders/main_fs.spv index 611f5752221519952b9ce177c0245fd5e2f8b809..889bbec6a912ba367930a2abc535d03380afe6d7 100644 GIT binary patch literal 576 zcmZQ(Qf6mhWn^Gr;AgO4fB-=TCI*lQh<+%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R0vOmCKxM#xP vk::Bool32 { + vk::FALSE + } + fn create_entry() -> Entry { unsafe { Entry::load().unwrap() } } @@ -639,7 +653,7 @@ impl AppCreator>> for MyAppCreator { camera_position: Vec3::new(0.0, 0.0, -5.0), camera_pitch: 0., camera_yaw: 0., - camera_fov: 45., + camera_fov: 90., bg_color: Vec3::splat(0.1).into(), model_color: Vec3::splat(0.8).into(), last_mouse_pos: None, diff --git a/crates/vk-rs/src/renderer.rs b/crates/vk-rs/src/renderer.rs index 0c6c9aa..1ee523f 100644 --- a/crates/vk-rs/src/renderer.rs +++ b/crates/vk-rs/src/renderer.rs @@ -1,21 +1,23 @@ use ash::{ extensions::khr::{Surface, Swapchain}, - vk::{self, Buffer, ImageAspectFlags}, + vk::{self, Buffer}, Device, }; use egui::Vec2; use egui_ash::EguiCommand; -use glam::{Mat4, Vec3}; -use gltf::json::buffer::View; use gpu_allocator::vulkan::{Allocation, AllocationCreateDesc, Allocator}; -use shaders_shared::UniformBufferObject; +use shaders_shared::{PushConstants, UniformBufferObject}; +use spirv_std::glam::{Mat4, Vec3, Vec4}; use std::{ ffi::CString, mem::ManuallyDrop, - path::{Path, PathBuf}, + path::Path, sync::{Arc, Mutex}, + time::Instant, }; +use crate::texture_cache::TextureCache; + macro_rules! include_spirv { ($file:literal) => {{ let bytes = include_bytes!($file); @@ -91,6 +93,8 @@ impl Texture { let mut allocator = allocator.lock().unwrap(); let buffer_size = data.len() as u64; + + println!("DATA LEN: {}", buffer_size); let staging_buffer = unsafe { device .create_buffer( @@ -163,7 +167,7 @@ impl Texture { .expect("failed to bind image memory") }; - let command_buffer = unsafe { + unsafe { let command_buffer = device .allocate_command_buffers( &vk::CommandBufferAllocateInfo::builder() @@ -218,7 +222,10 @@ impl Texture { width, height, depth: 1, - }); + }) + .buffer_offset(0) + .buffer_row_length(width) + .buffer_image_height(height); device.cmd_copy_buffer_to_image( command_buffer, @@ -329,7 +336,7 @@ impl Texture { } } - fn destroy(&mut self, device: &Device, allocator: &mut Allocator) { + pub fn destroy(&mut self, device: &Device, allocator: &mut Allocator) { unsafe { device.destroy_sampler(self.sampler, None); device.destroy_image_view(self.image_view, None); @@ -346,11 +353,29 @@ pub struct Mesh { vertex_buffer_allocation: Option, vertex_count: u32, transform: Mat4, - texture: Option, + texture: Option>, + descriptor_sets: Vec, } pub struct Model { meshes: Vec, + texture_cache: TextureCache, +} + +impl Model { + fn destroy(&mut self, device: &Device, allocator: &mut Allocator) { + for mesh in &mut self.meshes { + unsafe { + device.destroy_buffer(mesh.vertex_buffer, None); + if let Some(vertex_buffer_allocation) = mesh.vertex_buffer_allocation.take() { + allocator + .free(vertex_buffer_allocation) + .expect("Failed to free memory"); + } + self.texture_cache.cleanup(device, allocator); + } + } + } } fn load_texture_from_gltf( @@ -362,28 +387,27 @@ fn load_texture_from_gltf( buffers: &[gltf::buffer::Data], path: &Path, ) -> Option { - let image = texture.source(); - let data = match image.source() { - gltf::image::Source::View { view, .. } => { - // Handle embedded buffer view - match view.buffer().source() { - gltf::buffer::Source::Bin => { - let start = view.offset(); - let end = start + view.length(); - buffers[0][start..end].to_vec() - } - _ => return None, - } - } - gltf::image::Source::Uri { .. } => { - // For URI sources, we'll get the data directly from gltf::image::Data - let img_data = - gltf::image::Data::from_source(image.source(), Some(path), buffers).ok()?; - img_data.pixels.clone().to_vec() - } - }; + let img_data = + gltf::image::Data::from_source(texture.source().source(), Some(path), buffers).ok()?; - let img_data = gltf::image::Data::from_source(image.source(), Some(path), buffers).ok()?; + println!( + "Original texture dimensions: {}x{}", + img_data.width, img_data.height + ); + + // Convert to RGB/RGBA + let pixels_rgba = if img_data.pixels.len() == (img_data.width * img_data.height * 3) as usize { + // Image is RGB, convert to RGBA + println!("Converting RGB to RGBA"); + let mut rgba_data = Vec::with_capacity((img_data.width * img_data.height * 4) as usize); + for chunk in img_data.pixels.chunks_exact(3) { + rgba_data.extend_from_slice(&[chunk[0], chunk[1], chunk[2], 255]); + } + rgba_data + } else { + // Assume it's already RGBA + img_data.pixels + }; Some(Texture::new( device, @@ -392,7 +416,7 @@ fn load_texture_from_gltf( queue, img_data.width, img_data.height, - &data, + &pixels_rgba, )) } @@ -408,6 +432,7 @@ impl Model { let base_path = path.parent(); let (document, buffers, _) = gltf::import(path).expect("failed to load GLTF"); let mut meshes = Vec::new(); + let mut texture_cache = TextureCache::new(); for scene in document.scenes() { for node in scene.nodes() { @@ -420,11 +445,15 @@ impl Model { command_pool, queue, base_path.unwrap(), + &mut texture_cache, )); } } - Self { meshes } + Self { + meshes, + texture_cache, + } } } @@ -437,6 +466,7 @@ fn process_node( command_pool: vk::CommandPool, queue: vk::Queue, path: &Path, + texture_cache: &mut TextureCache, ) -> Vec { let mut meshes = Vec::new(); @@ -444,7 +474,7 @@ fn process_node( let (translation, rotation, scale) = node.transform().decomposed(); Mat4::from_scale_rotation_translation( Vec3::from(scale), - glam::Quat::from_array(rotation), + spirv_std::glam::Quat::from_array(rotation), Vec3::from(translation), ) }; @@ -457,17 +487,22 @@ fn process_node( .pbr_metallic_roughness() .base_color_texture() .and_then(|tex| { - load_texture_from_gltf( - device, - allocator.clone(), - command_pool, - queue, - &tex.texture(), - buffers, - path, - ) + // Create a unique key for the texture + let key = format!("{:?}", tex.texture().source().source()); + texture_cache.get_or_load_texture(key, || { + load_texture_from_gltf( + device, + allocator.clone(), + command_pool, + queue, + &tex.texture(), + buffers, + path, + ) + }) }); + // Rest of the mesh creation code... let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()])); if let (Some(positions), Some(normals), Some(tex_coords)) = ( @@ -477,16 +512,16 @@ fn process_node( ) { let mut vertices = Vec::new(); - let indicies = reader + let indices = reader .read_indices() - .map(|indicies| indicies.into_u32().collect::>()) + .map(|indices| indices.into_u32().collect::>()) .unwrap_or_else(|| (0..positions.len() as u32).collect()); let positions: Vec<_> = positions.collect(); let normals: Vec<_> = normals.collect(); let tex_coords: Vec<_> = tex_coords.into_f32().collect(); - for &index in indicies.iter() { + for &index in indices.iter() { let i = index as usize; let vertex = Vertex { position: Vec3::new(positions[i][0], positions[i][1], positions[i][2]), @@ -505,6 +540,7 @@ fn process_node( vertex_count: vertices.len() as u32, transform: world_transform, texture, + descriptor_sets: Vec::new(), }); } } @@ -520,6 +556,7 @@ fn process_node( command_pool, queue, path, + texture_cache, )); } @@ -680,7 +717,6 @@ pub struct RendererInner { uniform_buffer_allocations: Vec, descriptor_pool: vk::DescriptorPool, descriptor_set_layouts: Vec, - descriptor_sets: Vec, render_pass: vk::RenderPass, framebuffers: Vec, depth_images_and_allocations: Vec<(vk::Image, Allocation)>, @@ -849,17 +885,26 @@ impl RendererInner { (buffers, buffer_allocations) } - fn create_descriptor_pool(device: &Device, swapchain_count: usize) -> vk::DescriptorPool { - let pool_size = vk::DescriptorPoolSize::builder() - .ty(vk::DescriptorType::UNIFORM_BUFFER) - .descriptor_count(swapchain_count as u32); + fn create_descriptor_pool(device: &Device, total_sets_needed: usize) -> vk::DescriptorPool { + let pool_sizes = [ + vk::DescriptorPoolSize::builder() + .ty(vk::DescriptorType::UNIFORM_BUFFER) + .descriptor_count(total_sets_needed as u32) + .build(), + vk::DescriptorPoolSize::builder() + .ty(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) + .descriptor_count(total_sets_needed as u32) + .build(), + ]; + let descriptor_pool_create_info = vk::DescriptorPoolCreateInfo::builder() - .pool_sizes(std::slice::from_ref(&pool_size)) - .max_sets(swapchain_count as u32); + .pool_sizes(&pool_sizes) + .max_sets(total_sets_needed as u32); + unsafe { device .create_descriptor_pool(&descriptor_pool_create_info, None) - .expect("Failed to create descriptor pool") + .expect("failed to create descriptor pool") } } @@ -867,18 +912,35 @@ impl RendererInner { device: &Device, swapchain_count: usize, ) -> Vec { - let ubo_layout_binding = vk::DescriptorSetLayoutBinding::builder() - .binding(0) - .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER) - .descriptor_count(1) - .stage_flags(vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT); - let ubo_layout_create_info = vk::DescriptorSetLayoutCreateInfo::builder() - .bindings(std::slice::from_ref(&ubo_layout_binding)); + let bindings = [ + // Binding 0: Uniform buffer + vk::DescriptorSetLayoutBinding::builder() + .binding(0) + .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER) + .descriptor_count(1) + .stage_flags(vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT) + .build(), + // Binding 1: Texture sampler + vk::DescriptorSetLayoutBinding::builder() + .binding(1) + .descriptor_type(vk::DescriptorType::SAMPLED_IMAGE) + .descriptor_count(1) + .stage_flags(vk::ShaderStageFlags::FRAGMENT) + .build(), + vk::DescriptorSetLayoutBinding::builder() + .binding(2) + .descriptor_type(vk::DescriptorType::SAMPLER) + .descriptor_count(1) + .stage_flags(vk::ShaderStageFlags::FRAGMENT) + .build(), + ]; + + let layout_create_info = vk::DescriptorSetLayoutCreateInfo::builder().bindings(&bindings); (0..swapchain_count) .map(|_| unsafe { device - .create_descriptor_set_layout(&ubo_layout_create_info, None) + .create_descriptor_set_layout(&layout_create_info, None) .expect("Failed to create descriptor set layout") }) .collect() @@ -889,28 +951,65 @@ impl RendererInner { descriptor_pool: vk::DescriptorPool, descriptor_set_layouts: &[vk::DescriptorSetLayout], uniform_buffers: &[Buffer], + mesh: &Mesh, ) -> Vec { - let descriptor_set_allocate_info = vk::DescriptorSetAllocateInfo::builder() - .descriptor_pool(descriptor_pool) - .set_layouts(descriptor_set_layouts); let descriptor_sets = unsafe { device - .allocate_descriptor_sets(&descriptor_set_allocate_info) - .expect("Failed to allocate descriptor sets") + .allocate_descriptor_sets( + &vk::DescriptorSetAllocateInfo::builder() + .descriptor_pool(descriptor_pool) + .set_layouts(descriptor_set_layouts), + ) + .expect("failed to allocate descriptor sets") }; - for index in 0..descriptor_sets.len() { + + for (index, &descriptor_set) in descriptor_sets.iter().enumerate() { let buffer_info = vk::DescriptorBufferInfo::builder() .buffer(uniform_buffers[index]) .offset(0) .range(vk::WHOLE_SIZE) .build(); - let descriptor_write = vk::WriteDescriptorSet::builder() - .dst_set(descriptor_sets[index]) + + let mut descriptor_writes = vec![vk::WriteDescriptorSet::builder() + .dst_set(descriptor_set) .dst_binding(0) .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER) - .buffer_info(std::slice::from_ref(&buffer_info)); + .buffer_info(std::slice::from_ref(&buffer_info)) + .build()]; + + if let Some(ref texture) = mesh.texture { + // Image view descriptor + let image_info = vk::DescriptorImageInfo::builder() + .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL) + .image_view(texture.image_view) + .build(); + + descriptor_writes.push( + vk::WriteDescriptorSet::builder() + .dst_set(descriptor_set) + .dst_binding(1) + .descriptor_type(vk::DescriptorType::SAMPLED_IMAGE) + .image_info(std::slice::from_ref(&image_info)) + .build(), + ); + + // Sampler descriptor + let sampler_info = vk::DescriptorImageInfo::builder() + .sampler(texture.sampler) + .build(); + + descriptor_writes.push( + vk::WriteDescriptorSet::builder() + .dst_set(descriptor_set) + .dst_binding(2) + .descriptor_type(vk::DescriptorType::SAMPLER) + .image_info(std::slice::from_ref(&sampler_info)) + .build(), + ); + } + unsafe { - device.update_descriptor_sets(std::slice::from_ref(&descriptor_write), &[]); + device.update_descriptor_sets(&descriptor_writes, &[]); } } @@ -1119,10 +1218,19 @@ impl RendererInner { .name(&main_function_name_fs) .build(), ]; + + let push_constant_range = vk::PushConstantRange::builder() + .stage_flags(vk::ShaderStageFlags::FRAGMENT) + .offset(0) + .size(std::mem::size_of::() as u32) + .build(); + let pipeline_layout = unsafe { device .create_pipeline_layout( - &vk::PipelineLayoutCreateInfo::builder().set_layouts(descriptor_set_layouts), + &vk::PipelineLayoutCreateInfo::builder() + .set_layouts(descriptor_set_layouts) + .push_constant_ranges(&[push_constant_range]), None, ) .expect("Failed to create pipeline layout") @@ -1139,7 +1247,7 @@ impl RendererInner { .rasterizer_discard_enable(false) .polygon_mode(vk::PolygonMode::FILL) .cull_mode(vk::CullModeFlags::BACK) - .front_face(vk::FrontFace::COUNTER_CLOCKWISE) + .front_face(vk::FrontFace::CLOCKWISE) .depth_bias_enable(false) .line_width(1.0); let stencil_op = vk::StencilOpState::builder() @@ -1437,15 +1545,26 @@ impl RendererInner { swapchain_images.len(), ); - let descriptor_pool = Self::create_descriptor_pool(&device, swapchain_images.len()); + let t = Instant::now(); + println!("loading model!"); + let mut model = Model::load( + &device, + allocator.clone(), + command_pool, + queue, + "./sponza/NewSponza_Main_glTF_003.gltf", + ); + println!( + "loaded {} meshes in {:.2} seconds!", + model.meshes.len(), + t.elapsed().as_secs_f32() + ); + + let descriptor_pool = + Self::create_descriptor_pool(&device, model.meshes.len() * swapchain_images.len()); let descriptor_set_layouts = Self::create_descriptor_set_layouts(&device, swapchain_images.len()); - let descriptor_sets = Self::create_descriptor_sets( - &device, - descriptor_pool, - &descriptor_set_layouts, - &uniform_buffers, - ); + let render_pass = Self::create_render_pass(&device, surface_format); let CreateFramebuffersResult( framebuffers, @@ -1462,20 +1581,21 @@ impl RendererInner { ); let (pipeline, pipeline_layout) = Self::create_graphics_pipeline(&device, &descriptor_set_layouts, render_pass); - println!("loading model!"); - let model = Model::load( - &device, - allocator.clone(), - command_pool, - queue, - "./sponza/NewSponza_Main_glTF_003.gltf", - ); - println!("loaded!"); let command_buffers = Self::create_command_buffers(&device, command_pool, swapchain_images.len()); let (in_flight_fences, image_available_semaphores, render_finished_semaphores) = Self::create_sync_objects(&device, swapchain_images.len()); + for mesh in &mut model.meshes { + mesh.descriptor_sets = Self::create_descriptor_sets( + &device, + descriptor_pool, + &descriptor_set_layouts, + &uniform_buffers, + mesh, + ); + } + Self { width, height, @@ -1494,7 +1614,6 @@ impl RendererInner { uniform_buffer_allocations, descriptor_pool, descriptor_set_layouts, - descriptor_sets, render_pass, framebuffers, depth_images_and_allocations, @@ -1514,7 +1633,7 @@ impl RendererInner { camera_position: Vec3::new(0.0, 0.0, -5.0), camera_yaw: 0., camera_pitch: 0., - camera_fov: 45., + camera_fov: 90., bg_color: Vec3::splat(0.1), model_color: Vec3::splat(0.8), accumulation_reset_needed: true, @@ -1685,6 +1804,18 @@ impl RendererInner { ); for mesh in &self.model.meshes { + let push_constants = PushConstants { + texture_size: Vec4::new(1024.0, 1024.0, 1.0 / 1024.0, 1.0 / 1024.0), + }; + unsafe { + self.device.cmd_push_constants( + self.command_buffers[self.current_frame], + self.pipeline_layout, + vk::ShaderStageFlags::FRAGMENT, + 0, + bytemuck::cast_slice(&[push_constants]), + ); + } let model_matrix = Mat4::from_rotation_y(rotate_y.to_radians()) * mesh.transform; let ubo = UniformBufferObject { @@ -1710,7 +1841,7 @@ impl RendererInner { vk::PipelineBindPoint::GRAPHICS, self.pipeline_layout, 0, - &[self.descriptor_sets[self.current_frame]], + &[mesh.descriptor_sets[self.current_frame]], &[], ); @@ -1807,14 +1938,7 @@ impl RendererInner { for semaphore in self.render_finished_semaphores.drain(..) { self.device.destroy_semaphore(semaphore, None); } - for mesh in &mut self.model.meshes { - self.device.destroy_buffer(mesh.vertex_buffer, None); - if let Some(vertex_buffer_allocation) = mesh.vertex_buffer_allocation.take() { - allocator - .free(vertex_buffer_allocation) - .expect("Failed to free memory"); - } - } + self.model.destroy(&self.device, &mut allocator); self.device.destroy_pipeline(self.pipeline, None); self.device .destroy_pipeline_layout(self.pipeline_layout, None); diff --git a/crates/vk-rs/src/texture_cache.rs b/crates/vk-rs/src/texture_cache.rs new file mode 100644 index 0000000..171589b --- /dev/null +++ b/crates/vk-rs/src/texture_cache.rs @@ -0,0 +1,46 @@ +use std::{ + collections::HashMap, + sync::{Arc, Mutex}, +}; + +use ash::Device; +use gpu_allocator::vulkan::Allocator; + +use super::renderer::Texture; + +pub struct TextureCache { + cache: HashMap>, +} + +impl TextureCache { + pub fn new() -> Self { + Self { + cache: HashMap::new(), + } + } + + pub fn get_or_load_texture( + &mut self, + key: String, + load_fn: impl FnOnce() -> Option, + ) -> Option> { + if let Some(texture) = self.cache.get(&key) { + Some(Arc::clone(texture)) + } else { + load_fn().map(|texture| { + let texture = Arc::new(texture); + self.cache.insert(key, Arc::clone(&texture)); + texture + }) + } + } + + pub fn cleanup(&mut self, device: &Device, allocator: &mut Allocator) { + for (_, texture) in self.cache.drain() { + if let Ok(texture) = Arc::try_unwrap(texture) { + let mut texture = texture; + texture.destroy(device, allocator); + } + } + } +}