diff --git a/Cargo.lock b/Cargo.lock index a103724f..104ca804 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,7 +59,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef6978589202a00cd7e118380c448a08b6ed394c3a8df3a430d0898e3a42d046" dependencies = [ "android-properties", - "bitflags 2.5.0", + "bitflags 2.6.0", "cc", "cesu8", "jni", @@ -178,6 +178,15 @@ dependencies = [ "libloading 0.7.4", ] +[[package]] +name = "ash" +version = "0.38.0+1.3.281" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bb44936d800fea8f016d7f2311c6a4f97aebd5dc86f09906139ec848cf3a46f" +dependencies = [ + "libloading 0.8.3", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -228,7 +237,16 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" dependencies = [ - "bit-vec", + "bit-vec 0.6.3", +] + +[[package]] +name = "bit-set" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0481a0e032742109b1133a095184ee93d88f3dc9e0d28a5d033dc77a073f44f" +dependencies = [ + "bit-vec 0.7.0", ] [[package]] @@ -237,6 +255,12 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" +[[package]] +name = "bit-vec" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2c54ff287cfc0a34f38a6b832ea1bd8e448a330b3e40a50859e6488bee07f22" + [[package]] name = "bitflags" version = "1.3.2" @@ -245,9 +269,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "block" @@ -322,7 +346,7 @@ version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fba7adb4dd5aa98e5553510223000e7148f621165ec5f9acd7113f6ca4995298" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "log", "polling", "rustix", @@ -680,11 +704,11 @@ dependencies = [ [[package]] name = "d3d12" -version = "0.20.0" +version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b28bfe653d79bd16c77f659305b195b82bb5ce0c0eb2a4846b82ddbd77586813" +checksum = "bdbd1f579714e3c809ebd822c81ef148b1ceaeb3d535352afc73fd0c4c6a0017" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "libloading 0.8.3", "winapi", ] @@ -747,9 +771,9 @@ dependencies = [ [[package]] name = "document-features" -version = "0.2.8" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef5282ad69563b5fc40319526ba27e0e7363d552a896f0297d54f767717f9b95" +checksum = "cb6969eaabd2421f8a2775cfd2471a2b634372b4a25d41e3bd647b79912850a0" dependencies = [ "litrs", ] @@ -1158,9 +1182,9 @@ dependencies = [ [[package]] name = "glutin_wgl_sys" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8098adac955faa2d31079b65dc48841251f69efd3ac25477903fc424362ead" +checksum = "0a4e1951bbd9434a81aa496fe59ccc2235af3820d27b85f9314e279609211e2c" dependencies = [ "gl_generator", ] @@ -1181,7 +1205,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbcd2dba93594b227a1f57ee09b8b9da8892c34d55aa332e034a228d0fe6a171" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "gpu-alloc-types 0.3.0", ] @@ -1200,14 +1224,14 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98ff03b468aa837d70984d55f5d3f846f6ec31fe34bbb97c4f85219caeee1ca4" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", ] [[package]] name = "gpu-allocator" -version = "0.25.0" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f56f6318968d03c18e1bcf4857ff88c61157e9da8e47c5f29055d60e1228884" +checksum = "fdd4240fc91d3433d5e5b0fc5b67672d771850dc19bbee03c1381e19322803d7" dependencies = [ "log", "presser", @@ -1222,7 +1246,7 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc11df1ace8e7e564511f53af41f3e42ddc95b56fd07b3f4445d2a6048bc682c" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "gpu-descriptor-types 0.1.2", "hashbrown 0.14.5", ] @@ -1233,7 +1257,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c08c1f623a8d0b722b8b99f821eb0ba672a1618f0d3b16ddbee1cedd2dd8557" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "gpu-descriptor-types 0.2.0", "hashbrown 0.14.5", ] @@ -1244,7 +1268,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6bf0b36e6f090b7e1d8a4b49c0cb81c1f8376f72198c65dd3ad9ff3556b8b78c" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", ] [[package]] @@ -1253,7 +1277,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdf242682df893b86f33a73828fb09ca4b2d3bb6cc95249707fc684d27484b91" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", ] [[package]] @@ -1278,7 +1302,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af2a7e73e1f34c48da31fb668a907f250794837e08faa144fd24f0b8b741e890" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "com", "libc", "libloading 0.8.3", @@ -1513,7 +1537,7 @@ version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3af92c55d7d839293953fcd0fda5ecfe93297cfde6ffbdec13b41d99c0ba6607" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "libc", "redox_syscall 0.4.1", ] @@ -1683,11 +1707,11 @@ dependencies = [ [[package]] name = "metal" -version = "0.28.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5637e166ea14be6063a3f8ba5ccb9a4159df7d8f6d61c02fc3d480b1f90dcfcb" +checksum = "7ecfd3296f8c56b7c1f6fbac3c71cefa9d78ce009850c45000015f206dc7fa21" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "block", "core-graphics-types", "foreign-types 0.5.0", @@ -1724,7 +1748,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f50357e1167a3ab92d6b3c7f4bf5f7fd13fde3f4b28bf0d5ea07b5100fdb6c0" dependencies = [ - "bit-set", + "bit-set 0.5.3", "bitflags 1.3.2", "codespan-reporting", "hexf-parse", @@ -1740,18 +1764,18 @@ dependencies = [ [[package]] name = "naga" -version = "0.20.0" +version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e536ae46fcab0876853bd4a632ede5df4b1c2527a58f6c5a4150fe86be858231" +checksum = "09eeccb9b50f4f7839b214aa3e08be467159506a986c18e0702170ccf720a453" dependencies = [ "arrayvec 0.7.4", - "bit-set", - "bitflags 2.5.0", + "bit-set 0.6.0", + "bitflags 2.6.0", + "cfg_aliases 0.1.1", "codespan-reporting", "hexf-parse", "indexmap 2.2.6", "log", - "num-traits", "rustc-hash", "spirv 0.3.0+sdk-1.3.268.0", "termcolor", @@ -1779,7 +1803,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3f42e7bbe13d351b6bead8286a43aac9534b82bd3cc43e47037f012ebfd62d4" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "jni-sys", "log", "ndk-sys 0.6.0+11769913", @@ -2368,7 +2392,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "469052894dcb553421e483e4209ee581a45100d31b4018de03e5a7ad86374a7e" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", ] [[package]] @@ -2430,7 +2454,7 @@ version = "0.38.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "errno", "libc", "linux-raw-sys", @@ -2443,7 +2467,7 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0ae5692c5beaad6a9e22830deeed7874eae8a4e3ba4076fb48e12c56856222c" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "bytemuck", "smallvec", "ttf-parser", @@ -2619,7 +2643,7 @@ version = "0.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "922fd3eeab3bd820d76537ce8f582b1cf951eceb5475c28500c7457d9d17f53a" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "calloop 0.12.4", "calloop-wayland-source", "cursor-icon", @@ -2663,7 +2687,7 @@ version = "0.3.0+sdk-1.3.268.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eda41003dc44290527a59b13432d4a0379379fa074b70174882adfbdfd917844" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", ] [[package]] @@ -2704,8 +2728,8 @@ dependencies = [ "lyon", "tiny-skia-path 0.11.4", "usvg", - "wgpu 0.13.1", - "winit 0.27.5", + "wgpu 22.0.0", + "winit 0.30.0", ] [[package]] @@ -2784,18 +2808,18 @@ checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" [[package]] name = "thiserror" -version = "1.0.59" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0126ad08bff79f29fc3ae6a55cc72352056dfff61e3ff8bb7129476d44b23aa" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.59" +version = "1.0.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1cd413b5d558b4c5bf3680e324a6fa5014e7b7c067a51e69dbdf47eb7148b66" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" dependencies = [ "proc-macro2", "quote", @@ -3140,7 +3164,7 @@ version = "0.31.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82fb96ee935c2cea6668ccb470fb7771f6215d1691746c2d896b447a00ad3f1f" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "rustix", "wayland-backend", "wayland-scanner 0.31.1", @@ -3164,7 +3188,7 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "625c5029dbd43d25e6aa9615e88b829a5cad13b2819c4ae129fdbb7c31ab4c7e" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "cursor-icon", "wayland-backend", ] @@ -3209,7 +3233,7 @@ version = "0.31.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f81f365b8b4a97f422ac0e8737c438024b5951734506b0e1d775c73030561f4" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "wayland-backend", "wayland-client 0.31.2", "wayland-scanner 0.31.1", @@ -3221,7 +3245,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23803551115ff9ea9bce586860c5c5a971e360825a0309264102a9495a5ff479" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "wayland-backend", "wayland-client 0.31.2", "wayland-protocols 0.31.2", @@ -3234,7 +3258,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad1f61b76b6c2d8742e10f9ba5c3737f6530b4c243132c2a2ccc8aa96fe25cd6" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "wayland-backend", "wayland-client 0.31.2", "wayland-protocols 0.31.2", @@ -3329,17 +3353,16 @@ dependencies = [ [[package]] name = "wgpu" -version = "0.20.0" +version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32ff1bfee408e1028e2e3acbf6d32d98b08a5a059ccbf5f33305534453ba5d3e" +checksum = "c87e07e87a179614940ad845397e03201847453a37b43a31a3b54eee2e6e32ce" dependencies = [ "arrayvec 0.7.4", - "cfg-if", "cfg_aliases 0.1.1", "document-features", "js-sys", "log", - "naga 0.20.0", + "naga 22.0.0", "parking_lot", "profiling", "raw-window-handle 0.6.1", @@ -3348,9 +3371,9 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "web-sys", - "wgpu-core 0.20.0", - "wgpu-hal 0.20.0", - "wgpu-types 0.20.0", + "wgpu-core 22.0.0", + "wgpu-hal 22.0.0", + "wgpu-types 22.0.0", ] [[package]] @@ -3360,7 +3383,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89b92788dec9d0c1bed849a1b83f01b2ee12819bf04a79c90f68e4173f7b5ba2" dependencies = [ "arrayvec 0.7.4", - "bit-vec", + "bit-vec 0.6.3", "bitflags 1.3.2", "cfg_aliases 0.1.1", "codespan-reporting", @@ -3380,19 +3403,18 @@ dependencies = [ [[package]] name = "wgpu-core" -version = "0.20.0" +version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac6a86eaa5e763e59c73cf9e97d55fffd4dfda69fd8bda19589fcf851ddfef1f" +checksum = "e0f191908a21968991463fcf3b42cb6c9648c0fb7fa301b8fc733bc21a9ed9bd" dependencies = [ "arrayvec 0.7.4", - "bit-vec", - "bitflags 2.5.0", + "bit-vec 0.7.0", + "bitflags 2.6.0", "cfg_aliases 0.1.1", - "codespan-reporting", "document-features", "indexmap 2.2.6", "log", - "naga 0.20.0", + "naga 22.0.0", "once_cell", "parking_lot", "profiling", @@ -3400,9 +3422,8 @@ dependencies = [ "rustc-hash", "smallvec", "thiserror", - "web-sys", - "wgpu-hal 0.20.0", - "wgpu-types 0.20.0", + "wgpu-hal 22.0.0", + "wgpu-types 22.0.0", ] [[package]] @@ -3413,7 +3434,7 @@ dependencies = [ "env_logger 0.11.3", "futures", "lyon", - "wgpu 0.20.0", + "wgpu 22.0.0", "winit 0.30.0", ] @@ -3425,8 +3446,8 @@ checksum = "20cbdfc3d0637dba3d5536b93adef3d26023a0b96f0e1ee5ee9560a401d9f646" dependencies = [ "android_system_properties", "arrayvec 0.7.4", - "ash", - "bit-set", + "ash 0.37.3+1.3.251", + "bit-set 0.5.3", "bitflags 1.3.2", "block", "core-graphics-types", @@ -3458,19 +3479,19 @@ dependencies = [ [[package]] name = "wgpu-hal" -version = "0.20.0" +version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d71c8ae05170583049b65ee562fd839fdc0b3e9ddb84f4e40c9d5f8ea0d4c8c" +checksum = "f6bbf4b4de8b2a83c0401d9e5ae0080a2792055f25859a02bf9be97952bbed4f" dependencies = [ "android_system_properties", "arrayvec 0.7.4", - "ash", - "bit-set", - "bitflags 2.5.0", + "ash 0.38.0+1.3.281", + "bit-set 0.6.0", + "bitflags 2.6.0", "block", "cfg_aliases 0.1.1", "core-graphics-types", - "d3d12 0.20.0", + "d3d12 22.0.0", "glow 0.13.1", "glutin_wgl_sys", "gpu-alloc 0.6.0", @@ -3482,8 +3503,8 @@ dependencies = [ "libc", "libloading 0.8.3", "log", - "metal 0.28.0", - "naga 0.20.0", + "metal 0.29.0", + "naga 22.0.0", "ndk-sys 0.5.0+25.2.9519653", "objc", "once_cell", @@ -3497,7 +3518,7 @@ dependencies = [ "thiserror", "wasm-bindgen", "web-sys", - "wgpu-types 0.20.0", + "wgpu-types 22.0.0", "winapi", ] @@ -3512,11 +3533,11 @@ dependencies = [ [[package]] name = "wgpu-types" -version = "0.20.0" +version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1353d9a46bff7f955a680577f34c69122628cc2076e1d6f3a9be6ef00ae793ef" +checksum = "bc9d91f0e2c4b51434dfa6db77846f2793149d8e73f800fa2e41f52b8eac3c5d" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "js-sys", "web-sys", ] @@ -3867,7 +3888,7 @@ dependencies = [ "ahash", "android-activity", "atomic-waker", - "bitflags 2.5.0", + "bitflags 2.6.0", "bytemuck", "calloop 0.12.4", "cfg_aliases 0.2.0", @@ -3970,7 +3991,7 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d039de8032a9a8856a6be89cea3e5d12fdd82306ab7c94d74e6deab2460651c5" dependencies = [ - "bitflags 2.5.0", + "bitflags 2.6.0", "dlib", "log", "once_cell", diff --git a/examples/wgpu/Cargo.toml b/examples/wgpu/Cargo.toml index 6e256a8a..214b8493 100644 --- a/examples/wgpu/Cargo.toml +++ b/examples/wgpu/Cargo.toml @@ -15,5 +15,5 @@ lyon = { path = "../../crates/lyon", features = ["extra"] } bytemuck = { version = "1.15.0", features = ["derive"] } env_logger = "0.11.3" futures = "0.3.30" -wgpu = "0.20.0" +wgpu = "22.0" winit = "0.30.0" diff --git a/examples/wgpu/src/main.rs b/examples/wgpu/src/main.rs index bd0b0521..aee31072 100644 --- a/examples/wgpu/src/main.rs +++ b/examples/wgpu/src/main.rs @@ -520,6 +520,7 @@ impl GfxState { label: None, required_features: wgpu::Features::empty(), required_limits: wgpu::Limits::default(), + memory_hints:wgpu::MemoryHints::MemoryUsage, }, None, ) @@ -699,6 +700,7 @@ impl GfxState { alpha_to_coverage_enabled: false, }, multiview: None, + cache: None, }; let geo_pipeline = device.create_render_pipeline(&render_pipeline_descriptor); @@ -750,6 +752,7 @@ impl GfxState { alpha_to_coverage_enabled: false, }, multiview: None, + cache: None, }); Self { diff --git a/examples/wgpu_svg/Cargo.toml b/examples/wgpu_svg/Cargo.toml index e88c0e77..1795bdf1 100644 --- a/examples/wgpu_svg/Cargo.toml +++ b/examples/wgpu_svg/Cargo.toml @@ -14,8 +14,8 @@ path = "src/main.rs" lyon = { path = "../../crates/lyon", features = ["extra"] } clap = "2.32.0" -wgpu = "0.13.1" -winit = "0.27.5" +wgpu = "22.0" +winit = "0.30.0" usvg = { version = "0.40", features = []} tiny-skia-path = "0.11" futures = "0.3.5" diff --git a/examples/wgpu_svg/src/main.rs b/examples/wgpu_svg/src/main.rs index 4d198548..f8036f33 100644 --- a/examples/wgpu_svg/src/main.rs +++ b/examples/wgpu_svg/src/main.rs @@ -5,11 +5,13 @@ use lyon::tessellation::geometry_builder::*; use lyon::tessellation::{self, FillOptions, FillTessellator, StrokeOptions, StrokeTessellator}; use usvg::*; use wgpu::include_wgsl; +use winit::application::ApplicationHandler; use winit::dpi::PhysicalSize; -use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}; -use winit::event_loop::{ControlFlow, EventLoop}; -use winit::window::{Window, WindowBuilder}; - +use winit::event::{ElementState, KeyEvent, WindowEvent}; +use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop}; +use winit::keyboard::{KeyCode, PhysicalKey}; +use winit::window::{Window, WindowId}; +use std::sync::Arc; use futures::executor::block_on; use wgpu::util::DeviceExt; @@ -33,12 +35,112 @@ pub const FALLBACK_COLOR: usvg::Color = usvg::Color { // // Most of the code in this example is related to working with the GPU. +struct App { + window: Option>, + gfx: Option, +} + +pub struct GfxState { + scene: SceneGlobals, + mesh: VertexBuffers, + surface_desc: wgpu::SurfaceConfiguration, + surface: wgpu::Surface<'static>, + device: wgpu::Device, + queue: wgpu::Queue, + vbo: wgpu::Buffer, + ibo: wgpu::Buffer, + globals_ubo: wgpu::Buffer, + bind_group: wgpu::BindGroup, + render_pipeline: wgpu::RenderPipeline, + wireframe_render_pipeline: wgpu::RenderPipeline, + msaa_texture: Option, + msaa_samples: u32, +} + +impl ApplicationHandler for App { + fn resumed(&mut self, event_loop: &ActiveEventLoop) { + let win_attrs = Window::default_attributes() + .with_title("Basic SVG example"); + let window = Arc::new(event_loop.create_window(win_attrs).unwrap()); + + self.gfx = init(window.clone()); + self.window = Some(window); + } + + fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) { + let gfx = if let Some(gfx) = &mut self.gfx { + gfx + } else { + event_loop.exit(); + return; + }; + + let scene = &mut gfx.scene; + let mut should_render = false; + + let initial_pan = scene.pan; + let initial_zoom = scene.zoom; + + match event { + WindowEvent::RedrawRequested => { + should_render = true; + } + WindowEvent::Destroyed | WindowEvent::CloseRequested => event_loop.exit(), + WindowEvent::Resized(size) => { + scene.window_size = size; + scene.size_changed = true; + } + WindowEvent::KeyboardInput { + event: + KeyEvent { + physical_key: PhysicalKey::Code(key_code), + state: ElementState::Pressed, + .. + }, + .. + } => match key_code { + KeyCode::Escape => event_loop.exit(), + KeyCode::PageDown => scene.zoom *= 0.8, + KeyCode::PageUp => scene.zoom *= 1.25, + KeyCode::ArrowLeft => scene.pan[0] += 50.0 / scene.zoom, + KeyCode::ArrowRight => scene.pan[0] -= 50.0 / scene.zoom, + KeyCode::ArrowUp => scene.pan[1] -= 50.0 / scene.zoom, + KeyCode::ArrowDown => scene.pan[1] += 50.0 / scene.zoom, + _key => {} + }, + _evt => {} + }; + + if event_loop.exiting() { + return; + } + + should_render |= scene.pan != initial_pan || scene.zoom != initial_zoom; + + if should_render { + render(gfx); + } + } +} + fn main() { // Grab some parameters from the command line. env_logger::init(); - let app = App::new("Lyon svg_render example") + let event_loop = EventLoop::new().unwrap(); + event_loop.set_control_flow(ControlFlow::Poll); + + let mut app = App { + window: None, + gfx: None, + }; + + event_loop.run_app(&mut app).unwrap(); +} + +fn init(window: Arc) -> Option { + let args = clap::App::new("Lyon svg_render example") .version("0.1") .arg( Arg::with_name("MSAA") @@ -67,11 +169,11 @@ fn main() { ) .get_matches(); - let msaa_samples = if app.is_present("MSAA") { 4 } else { 1 }; + let msaa_samples = if args.is_present("MSAA") { 4 } else { 1 }; // Parse and tessellate the geometry - let filename = app.value_of("INPUT").unwrap(); + let filename = args.value_of("INPUT").unwrap(); let mut fill_tess = FillTessellator::new(); let mut stroke_tess = StrokeTessellator::new(); @@ -103,8 +205,8 @@ fn main() { &mut stroke_tess, ); - if app.is_present("TESS_ONLY") { - return; + if args.is_present("TESS_ONLY") { + return None; } println!( @@ -129,22 +231,17 @@ fn main() { let pan = [vb_width / -2.0, vb_height / -2.0]; let zoom = 2.0 / f32::max(vb_width, vb_height); - let mut scene = SceneGlobals { + let scene = SceneGlobals { zoom, pan, window_size: PhysicalSize::new(width as u32, height as u32), wireframe: false, size_changed: true, - render: false, }; - let event_loop = EventLoop::new(); - let window_builder = WindowBuilder::new().with_inner_size(scene.window_size); - let window = window_builder.build(&event_loop).unwrap(); - - let instance = wgpu::Instance::new(wgpu::Backends::PRIMARY); + let instance = wgpu::Instance::default(); - let surface = unsafe { instance.create_surface(&window) }; + let surface = instance.create_surface(window.clone()).unwrap(); // create an adapter let adapter = block_on(instance.request_adapter(&wgpu::RequestAdapterOptions { @@ -158,8 +255,9 @@ fn main() { let (device, queue) = block_on(adapter.request_device( &wgpu::DeviceDescriptor { label: None, - features: wgpu::Features::default() | wgpu::Features::POLYGON_MODE_LINE, - limits: wgpu::Limits::default(), + required_features: wgpu::Features::default() | wgpu::Features::POLYGON_MODE_LINE, + required_limits: wgpu::Limits::default(), + memory_hints: wgpu::MemoryHints::MemoryUsage, }, // trace_path can be used for API call tracing None, @@ -168,16 +266,18 @@ fn main() { let size = window.inner_size(); - let mut surface_desc = wgpu::SurfaceConfiguration { + let surface_desc = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, format: wgpu::TextureFormat::Bgra8Unorm, width: size.width, height: size.height, present_mode: wgpu::PresentMode::AutoVsync, + // defaults from `surface.get_default_config(...)`` + desired_maximum_frame_latency: 2, + alpha_mode: wgpu::CompositeAlphaMode::Auto, + view_formats: vec![], }; - let mut msaa_texture = None; - let vbo = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: None, contents: bytemuck::cast_slice(&mesh.vertices), @@ -304,6 +404,7 @@ fn main() { }, ], }], + compilation_options: wgpu::PipelineCompilationOptions::default(), }, fragment: Some(wgpu::FragmentState { module: &fs_module, @@ -313,6 +414,7 @@ fn main() { blend: None, write_mask: wgpu::ColorWrites::ALL, })], + compilation_options: wgpu::PipelineCompilationOptions::default(), }), primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, @@ -330,6 +432,7 @@ fn main() { alpha_to_coverage_enabled: false, }, multiview: None, + cache: None, }; let render_pipeline = device.create_render_pipeline(&render_pipeline_descriptor); @@ -345,105 +448,114 @@ fn main() { window.request_redraw(); - // The main loop. + return Some(GfxState { + scene, + mesh, + device, + queue, + vbo, + ibo, + globals_ubo, + surface_desc, + surface, + bind_group, + render_pipeline, + wireframe_render_pipeline, + msaa_samples, + msaa_texture: None, + }); +} - event_loop.run(move |event, _, control_flow| { - if !update_inputs(event, &window, control_flow, &mut scene) { - // keep polling inputs. - return; - } - if scene.size_changed { - scene.size_changed = false; - let physical = scene.window_size; - surface_desc.width = physical.width; - surface_desc.height = physical.height; - surface.configure(&device, &surface_desc); - if msaa_samples > 1 { - msaa_texture = Some( - device - .create_texture(&wgpu::TextureDescriptor { - label: Some("Multisampled frame descriptor"), - size: wgpu::Extent3d { - width: surface_desc.width, - height: surface_desc.height, - depth_or_array_layers: 1, - }, - mip_level_count: 1, - sample_count: msaa_samples, - dimension: wgpu::TextureDimension::D2, - format: surface_desc.format, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - }) - .create_view(&wgpu::TextureViewDescriptor::default()), - ); - } +fn render(gfx: &mut GfxState) { + if gfx.scene.size_changed { + gfx.scene.size_changed = false; + let physical = gfx.scene.window_size; + gfx.surface_desc.width = physical.width; + gfx.surface_desc.height = physical.height; + gfx.surface.configure(&gfx.device, &gfx.surface_desc); + if gfx.msaa_samples > 1 { + gfx.msaa_texture = Some( + gfx.device + .create_texture(&wgpu::TextureDescriptor { + label: Some("Multisampled frame descriptor"), + size: wgpu::Extent3d { + width: gfx.surface_desc.width, + height: gfx.surface_desc.height, + depth_or_array_layers: 1, + }, + mip_level_count: 1, + sample_count: gfx.msaa_samples, + dimension: wgpu::TextureDimension::D2, + format: gfx.surface_desc.format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }) + .create_view(&wgpu::TextureViewDescriptor::default()), + ); } + } - if !scene.render { + let frame = match gfx.surface.get_current_texture() { + Ok(frame) => frame, + Err(e) => { + println!("Swap-chain error: {e:?}"); return; } - scene.render = false; + }; - let frame = match surface.get_current_texture() { - Ok(frame) => frame, - Err(e) => { - println!("Swap-chain error: {e:?}"); - return; - } - }; + let frame_view = frame + .texture + .create_view(&wgpu::TextureViewDescriptor::default()); - let frame_view = frame - .texture - .create_view(&wgpu::TextureViewDescriptor::default()); + let mut encoder = gfx.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("Encoder"), + }); - let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { - label: Some("Encoder"), - }); + gfx.queue.write_buffer( + &gfx.globals_ubo, + 0, + bytemuck::cast_slice(&[GpuGlobals { + aspect_ratio: gfx.scene.window_size.width as f32 / gfx.scene.window_size.height as f32, + zoom: [gfx.scene.zoom, gfx.scene.zoom], + pan: gfx.scene.pan, + _pad: 0.0, + }]), + ); - queue.write_buffer( - &globals_ubo, - 0, - bytemuck::cast_slice(&[GpuGlobals { - aspect_ratio: scene.window_size.width as f32 / scene.window_size.height as f32, - zoom: [scene.zoom, scene.zoom], - pan: scene.pan, - _pad: 0.0, - }]), - ); - - { - let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: None, - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: msaa_texture.as_ref().unwrap_or(&frame_view), - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), - store: true, - }, - resolve_target: if msaa_texture.is_some() { - Some(&frame_view) - } else { - None - }, - })], - depth_stencil_attachment: None, - }); - - if scene.wireframe { - pass.set_pipeline(&wireframe_render_pipeline); - } else { - pass.set_pipeline(&render_pipeline); - } - pass.set_bind_group(0, &bind_group, &[]); - pass.set_index_buffer(ibo.slice(..), wgpu::IndexFormat::Uint32); - pass.set_vertex_buffer(0, vbo.slice(..)); + { + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: gfx.msaa_texture.as_ref().unwrap_or(&frame_view), + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), + store: wgpu::StoreOp::Store, + }, + resolve_target: if gfx.msaa_texture.is_some() { + Some(&frame_view) + } else { + None + }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + }); - pass.draw_indexed(0..(mesh.indices.len() as u32), 0, 0..1); + if gfx.scene.wireframe { + pass.set_pipeline(&gfx.wireframe_render_pipeline); + } else { + pass.set_pipeline(&gfx.render_pipeline); } + pass.set_bind_group(0, &gfx.bind_group, &[]); + pass.set_index_buffer(gfx.ibo.slice(..), wgpu::IndexFormat::Uint32); + pass.set_vertex_buffer(0, gfx.vbo.slice(..)); - queue.submit(Some(encoder.finish())); - frame.present(); - }); + pass.draw_indexed(0..(gfx.mesh.indices.len() as u32), 0, 0..1); + } + + gfx.queue.submit(Some(encoder.finish())); + frame.present(); } fn collect_geom( @@ -603,98 +715,6 @@ pub struct SceneGlobals { pub window_size: PhysicalSize, pub wireframe: bool, pub size_changed: bool, - pub render: bool, -} - -fn update_inputs( - event: Event<()>, - window: &Window, - control_flow: &mut ControlFlow, - scene: &mut SceneGlobals, -) -> bool { - let mut redraw = false; - match event { - Event::RedrawRequested(_) => { - scene.render = true; - } - Event::WindowEvent { - event: WindowEvent::Destroyed, - .. - } - | Event::WindowEvent { - event: WindowEvent::CloseRequested, - .. - } => { - *control_flow = ControlFlow::Exit; - return false; - } - Event::WindowEvent { - event: WindowEvent::Resized(size), - .. - } => { - scene.window_size = size; - scene.size_changed = true; - redraw = true; - } - Event::WindowEvent { - event: - WindowEvent::KeyboardInput { - input: - KeyboardInput { - state: ElementState::Pressed, - virtual_keycode: Some(key), - .. - }, - .. - }, - .. - } => match key { - VirtualKeyCode::Escape => { - *control_flow = ControlFlow::Exit; - return false; - } - VirtualKeyCode::PageDown => { - scene.zoom *= 0.8; - redraw = true; - } - VirtualKeyCode::PageUp => { - scene.zoom *= 1.25; - redraw = true; - } - VirtualKeyCode::Left => { - scene.pan[0] -= 50.0 / scene.pan[0]; - redraw = true; - } - VirtualKeyCode::Right => { - scene.pan[0] += 50.0 / scene.pan[0]; - redraw = true; - } - VirtualKeyCode::Up => { - scene.pan[1] += 50.0 / scene.pan[1]; - redraw = true; - } - VirtualKeyCode::Down => { - scene.pan[1] -= 50.0 / scene.pan[1]; - redraw = true; - } - VirtualKeyCode::W => { - scene.wireframe = !scene.wireframe; - redraw = true; - } - _key => {} - }, - _evt => { - //println!("{:?}", _evt); - } - } - - *control_flow = ControlFlow::Poll; - - if redraw { - window.request_redraw(); - } - - true } /// Some glue between usvg's iterators and lyon's.